PHP嵌套循环-根据每周的可用小时获取生产订单的准备日期

时间:2018-12-30 17:36:48

标签: mysql loops nested composer-php

我为此奋斗了好几天都没有成功:( 我有一个生产订单清单,其中包含需要一定小时数才能完成的订单。

然后我有一个单独的表,其中包含每周的可用时间,例如w52 / 2018-340h。

我需要遍历订单行和每周可用时间,并计算每个订单行的准备日期。

在代码中,带有大写字母的注释是我一直在努力的问题。可能还有其他事情也完全错了。

 $year = date("Y");
 $week = date("W");

 //outer loop query - loops through production order rows (every row has 
 production time hours needed for the row)
 $db->setQuery("
SELECT a.*
  FROM work_orders AS a 
 ORDER 
    BY a.id ASC
");
 $results_work_order = $db->loadObjectList();

 //inner loop query (loops through weeks database table where weekly 
 available production hours are retrieved)
 $db->setQuery("SELECT a.work_hours_per_week FROM work_schedule AS a 
 WHERE a.year >= " .$year ." AND a.week >= " . $week ." ORDER BY a.year, 
 a.week ASC");  
 $results_work_hours = $db->loadObjectList();


 foreach ($results_work_order as $result_work_order) {

foreach ($results_work_hours as $result_work_hours) {

//gets the amount of hours needed to complete the order
$work_order_hours_left = $result_work_order->work_order_hours_left;

//gets weekly available hours starting from current week
$weekly_hours = $result_work_hours->work_hours_per_week;


//if weekly available hour are greater than hours needed for current order row...
if($weekly_hours > $work_order_hours_left) {

    //gets ready weekday: divides current row hours with weekly hours and rounds (e.g. 150 / 330 = 0.45), 0.45 * 7 = 3.15, rounded up = 4 = Thursday
    $ready_weekday = roundup(($work_order_hours_left / $weekly_hours) * 7), 0)

    //HOW TO GET DATE FOR THE WEEK/YEAR's THURSDAY (CALCULATED READY DATE)
    $calculated_ready_date=???

    //updates the production order row's calculated ready date
    $db->setQuery("UPDATE work_orders SET calc_ready_date  = " . $db->Quote($calculated_ready_date) . " 
    WHERE id = " . $db->Quote($result_outer->id));
    $db->query();

    //SHOULD CONTINUE TO NEXT OUTER LOOP AND KEEP THE HOURS LEFT FROM THIS WEEK TO NEXT PRODUCTION ORDER ROW
    //NEXT INNER LOOP SHOULD CONTINUE WHERE IT LAST LEFT OFF 


    } else {
    //weekly available hours are smaller than hours needed for current production order row

    //SHOULD GET THE WEEKLY HOURS FROM PREVIOUS WEEK AND ADD HOURS FROM CURRENT WEEK, THEN CHECK IF THESE ARE ENOUGH TO FINISH CURRENT PRODCUTION ORDER
    //GETS THE READY DATE SIMILAR WAY AS IF THE CODE BEFORE "ELSE" ??? HOW???

       }

    }

}

我将非常感谢您对我的正确指导。

谢谢! 劳拉

1 个答案:

答案 0 :(得分:0)

要从星期数和年份中获取日期

$calculated_ready_date = date( "l, M jS, Y", strtotime($year."W".sprintf("%02u", $week)."1") ); 

在这种情况下,您将获得该周的第一天。您可以将数字1更改为7更改为一周的最后一天。

您可以使用break结束内部循环或继续进行下一个项目。