
<?
set_time_limit(0);

require_once "lib/DB.php";
require_once "lib/DBTable.php";
require_once "lib/DBFactory.php";
require_once "lib/ext/DateTimeUtil.php";
require_once "lib/PackageType.php";
require_once "lib/Customer.php";
require_once "lib/Util.php";
require_once "lib/Item.php";
require_once "lib/Stock.php";
require_once "lib/StockCardRecord.php";


$logger = Logger::getInstance($_SERVER['PHP_SELF']);

$PackageTypes = (new DBFactory())->query("SELECT * FROM PackageType WHERE item_dvsn_id=1");

$WHERE = "1=1";
$Customers = (new Customer())->get(Customer::$__COLUMNS,$WHERE);

$count_total = 0;
$current_stock_pcs = 0;
$stockcard_pack = 0;
$stockcard_pcs = 0;
$list_outbound_fix = (new DBFactory())->query("SELECT * , CONVERT(varchar,create_dt, 23) AS sum_date , CONVERT(varchar,update_dt, 23) AS re_sum_date
                                             FROM [wms-smpt].[dbo].[Outbound]
                                             WHERE CONVERT(varchar,create_dt, 23)='2023-09-20' AND CONVERT(varchar,update_dt, 23)='2023-09-25'");

//echo json_encode($list_outbound_fix);

$list_bug = array();
$list_success = 0;
$list_sql = array();

foreach( $list_outbound_fix as $row_fix ){
                         $sql =   " UPDATE StockCardRecord " .
                              " SET outbound_pack = outbound_pack - ".$row_fix['package_qty'].", ".
                              "     outbound_pcs = outbound_pcs - ".$row_fix['qty']." " .
                              " WHERE item_id='" . $row_fix['item_id'] . "'" .
                              "   AND sum_date='". $row_fix['sum_date'] ."' ";
                         //echo $sql;
                         //echo '<br/>';
                         $stmt = sqlsrv_query( (new DB())->getInstance(), $sql );
                         $rows_affected = sqlsrv_rows_affected( $stmt);

                         $sql =   " UPDATE StockCardRecord " .
                                   " SET outbound_pack = outbound_pack + ".$row_fix['package_qty'].", ".
                                   "     outbound_pcs = outbound_pcs + ".$row_fix['qty']." " .
                                   " WHERE item_id='" . $row_fix['item_id'] . "'" .
                                   "   AND sum_date='". $row_fix['re_sum_date'] ."' ";

                         //echo $sql;
                         //echo '<br/><br/>';       
                         $stmt = sqlsrv_query( (new DB())->getInstance(), $sql );
                         $rows_affected = sqlsrv_rows_affected( $stmt);                        

                         $current_stock_pcs = 0;
                         $stockcard_pack = 0;
                         $stockcard_pcs = 0;
                         
                         $last_stockcard = (new DBFactory())->query("SELECT top 1 balance_pack AS stockcard_pack , balance_pcs AS stockcard_pcs FROM StockCardRecord WHERE item_id ='".$row_fix['item_id']."' AND customer_id='".$row_fix['customer_id']."' AND sum_date < '".$row_fix['sum_date']."'  order by  sum_date DESC, create_dt DESC");    
                         if( isset($last_stockcard[0]['stockcard_pcs']) ){
                             $current_stock_pcs = $last_stockcard[0]['stockcard_pcs'];
                             $stockcard_pack = $last_stockcard[0]['stockcard_pack'];
                             $stockcard_pcs = $last_stockcard[0]['stockcard_pcs'];
                         }
                         $list_transaction = (new DBFactory())->query("SELECT * FROM StockCardRecord WHERE item_id='". $row_fix['item_id'] ."' AND customer_id='". $row_fix['customer_id'] ."' AND SUM_date BETWEEN '".$row_fix['sum_date']."' AND '".$row_fix['re_sum_date']."' order by  sum_date, create_dt ASC");             
                         foreach( $list_transaction as $row ){
                              $sql =   " UPDATE StockCardRecord " . 
                                        " SET current_stock_pcs = ".$current_stock_pcs." , " .
                                        "     stockcard_pack = ".$stockcard_pack." , " .
                                        "     stockcard_pcs = ".$stockcard_pcs." , " .
                                        "     balance_pack = (".$stockcard_pack." + inbound_pack) - outbound_pack, " .
                                        "     balance_pcs = (".$stockcard_pcs." + inbound_pcs) - outbound_pcs" .
                                        " WHERE item_id='" . $row['item_id'] . "'" .
                                        "   AND customer_id='" . $row['customer_id'] . "'" .
                                        "   AND sum_date='". $row['sum_date'] ."'";
                              $stmt = sqlsrv_query( (new DB())->getInstance(), $sql );
                              $rows_affected = sqlsrv_rows_affected( $stmt);

                              $stockcard_pack = ($stockcard_pack + $row['inbound_pack']) - abs($row['outbound_pack']);
                              $stockcard_pcs = ($stockcard_pcs + $row['inbound_pcs']) - abs($row['outbound_pcs']);
                              $current_stock_pcs = $stockcard_pcs;
                         }
          
          $sql =   " UPDATE Outbound " .
                    " SET create_dt = update_dt ".
                    " WHERE id=" . $row_fix['id'];
          $stmt = sqlsrv_query( (new DB())->getInstance(), $sql );
          $rows_affected = sqlsrv_rows_affected( $stmt);
          $list_success+=1;
}

echo '<br/><br/>';

echo $list_success;

//if( $list_item_fix!=null ){

//     foreach( $list_item_fix as $row_item ){

//     //echo "SELECT * FROM StockCardRecord  WHERE item_id='".$row_item['item_id']."' order by item_id , sum_date, create_dt";
//    // echo '<br/>';
//       $list_item_recal = (new DBFactory())->query("
//                                               SELECT * FROM StockCardRecord 
//                                               WHERE item_id='".$row_item['item_id']."' order by item_id , sum_date, create_dt
//                                               ");

//           // echo $SQl;
//           // echo '<br/>';

//           if( $list_item_recal !=null ){

//                 $current_stock_pcs = $list_item_recal[0]['current_stock_pcs'];
//                 $stockcard_pack = $list_item_recal[0]['stockcard_pack'];
//                 $stockcard_pcs = $list_item_recal[0]['stockcard_pcs'];

//                foreach( $list_item_recal as $row_recal ){
                
//                     // $sql =   " UPDATE StockCardRecord " .
//                     //           " SET current_stock_pcs = ".$current_stock_pcs." , " .
//                     //           "     stockcard_pack = ".$stockcard_pack." , " .
//                     //           "     stockcard_pcs = ".$stockcard_pcs." , " .
//                     //           "     balance_pack = (".$stockcard_pack." + inbound_pack) - outbound_pack , " .
//                     //           "     balance_pcs = (".$stockcard_pcs." + inbound_pcs) - outbound_pcs " .
//                     //           " WHERE item_id='" . $row_recal['item_id'] . "'" .
//                     //           "   AND item_dvsn_id=" . $row_recal['item_dvsn_id'] . 
//                     //           "   AND customer_id='" . $row_recal['customer_id'] . "'" .
//                     //           "   AND customer_dvsn_id=" . $row_recal['customer_dvsn_id'] .
//                     //           "   AND sum_date='". $row_recal['sum_date'] ."' ".
//                     //           "   AND create_dt=". $row_recal['create_dt'] ."'";

//                     $sql =   " UPDATE StockCardRecord " .
//                     " SET current_stock_pcs = ".$current_stock_pcs." , " .
//                     "     stockcard_pack = ".$stockcard_pack." , " .
//                     "     stockcard_pcs = ".$stockcard_pcs." , " .
//                     "     balance_pack = (".$stockcard_pack." + inbound_pack) - outbound_pack , " .
//                     "     balance_pcs = (".$stockcard_pcs." + inbound_pcs) - outbound_pcs " .
//                     " WHERE item_id='" . $row_recal['item_id'] . "'" .
//                     "   AND id= ". $row_recal['id'];


//                     $stmt = sqlsrv_query( (new DB())->getInstance(), $sql );
//                     $rows_affected = sqlsrv_rows_affected( $stmt);


//                    $list_sql[] = $sql;


//                     $stockcard_pack = ($stockcard_pack + $row_recal['inbound_pack']) - abs($row_recal['outbound_pack']);
//                     $stockcard_pcs = ($stockcard_pcs + $row_recal['inbound_pcs']) - abs($row_recal['outbound_pcs']);
//                     $current_stock_pcs = $stockcard_pcs;
                
//                }
//           }
//     }

//}

////echo '<br/><br/>';

//echo json_encode($list_sql);



?>