
<?
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_item_fix = (new DBFactory())->query("SELECT item_id FROM StockCardRecord WHERE (stockcard_pcs+inbound_pcs)-outbound_pcs != balance_pcs ");

//echo json_encode($list_item_fix);

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

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);



?>