<?php
$user = "root";
$pass = "hDY2Ln5Lq8";
$START_DATE = new DateTime('2019-07-29');

class DateIntervalEnhanced extends DateInterval {

    public function recalculate()
    {
        $from = new DateTime;
        $to = clone $from;
        $to = $to->add($this);
        $diff = $from->diff($to);
        foreach ($diff as $k => $v) $this->$k = $v;
        return $this;
    }

}

function hours_and_minutes($hours)
{
    return floor($hours) .":". str_pad(round(($hours - floor($hours))*60),2,"0", STR_PAD_LEFT);
}


$current_date = date('U');

$sql = "SELECT  app_entity_21.id as `project_id`,
            app_entity_21.field_158 as `project_name`,
            app_entity_1.id as `user_id`,
            CONCAT_WS(' ', `field_8`, `field_7`) AS `name`,
            app_entity_1.field_739 as `otdel`,
            app_entity_1.field_677 as `hourly_rate`,
            SUM(app_entity_38.field_486) as `hours`,
            SUM(app_entity_38.field_484) as `hours_fact`,
            SUM(app_entity_38.field_486*app_entity_1.field_677) as `money`,
            SUM(app_entity_38.field_484*app_entity_1.field_677) as `money_fact`
        FROM `app_entity_21` INNER JOIN (`app_entity_38`,`app_entity_37`,`app_entity_36`, `app_entity_1`) 
        ON (app_entity_1.id=app_entity_38.field_476 AND app_entity_38.parent_item_id=app_entity_37.id 
        AND app_entity_37.parent_item_id=app_entity_36.id AND app_entity_36.parent_item_id=app_entity_21.id)
        GROUP BY `project_id`, `user_id`";

$dbh = new PDO("mysql:host=localhost;dbname=rukovoditeldb;charset=utf8", $user, $pass);
$list = $dbh->query($sql)->FetchAll(PDO::FETCH_ASSOC);
// print_r($dbh->errorInfo());
$dbh->query( "DELETE FROM `app_entity_46`" );
// print_r($dbh->errorInfo());
// print_r($list);
foreach ($list as $row) {
    if ($row['money']>0) {
        $hours = hours_and_minutes($row['hours']);
        $hours_fact = hours_and_minutes($row['hours_fact']);
        $dbh->query("INSERT INTO `app_entity_46` 
            SET `parent_item_id` = {$row['project_id']}, 
            `created_by` = 0, 
            `date_added` = {$current_date}, 
            `field_701` = {$row['user_id']}, 
            `field_702` = {$row['money']},
            `field_749` = {$row['money_fact']},
            `field_704` = {$row['hourly_rate']}, 
            `field_750` = {$row['otdel']},
            `field_718` = '{$hours}',
            `field_742` = '{$hours_fact}'"
        );
        // print_r("INSERT INTO `app_entity_46` 
        //     SET `parent_item_id` = {$row['project_id']}, 
        //     `created_by` = 0, 
        //     `date_added` = {$current_date}, 
        //     `field_701` = {$row['user_id']}, 
        //     `field_702` = {$row['money']},
        //     `field_749` = {$row['money_fact']}, 
        //     `field_704` = {$row['hourly_rate']},
        //     `field_718` = '{$hours}',
        //     `field_742` = '{$hours_fact}'"
        // );
        print_r($dbh->errorInfo());
    }
}