<?php
include_once("../public/config.php");
// $allStoreSettelMent = R::getAll('SELECT * FROM `storereport` where tablename = "settlementstoreController.php" and userid = 7 and storereportdate >= "2025-06-14"');

// $proArr = [];
// foreach ($allStoreSettelMent as $settle) {
//     $productid = $settle['productid'];
//     $storeid = $settle['storeid'];

//     R::exec('Delete from storereport where storereportid = ' . $settle['storereportid']);
//     $allStoreSettelMentDetail = R::getAll('SELECT * FROM `storereport` where productid = ' . $productid . ' and storeid = ' . $storeid . ' and storereportdate >= "2025-06-14" order by storereportid asc');
//     var_dump($allStoreSettelMentDetail);
//     $itr = 1;
//     $count = 0;
//     $before = $after = 0;
//     foreach ($allStoreSettelMentDetail as $report) {
//         echo 'in loop <br/>';
//         if ($itr == 1) {
//             echo 'in first loop <br/>';
//             $before = $report['productbefore'];
//             if ($report['storereporttype'] == 0)
//                 $after = $before + $report['productquantity'];
//             else
//                 $after = $before - $report['productquantity'];

//             if ($report['productafter'] != $after) {
//                 if (!in_array($productid, $proArr)) {
//                     echo 'storereportid = ' . $report['storereportid'] . '--------- ' . $after . '--------------' . $report['productbefore'] . '<br/>';
//                     array_push($proArr, $productid);
//                 }
//                 R::exec('UPDATE storereport SET productafter = ' . $after . ' where storereportid = ' . $report['storereportid']);
//             }
//         } else {
//         echo 'in loop '.$itr .' <br/>';
//             if ($after != $report['productbefore']) {
//                 if (!in_array($productid, $proArr)) {
//                     echo 'storereportid = ' . $report['storereportid'] . '--------- ' . $after . '--------------' . $report['productbefore'] . '<br/>';
//                     array_push($proArr, $productid);
//                 }
//                 $before = $after;
//                 if ($report['storereporttype'] == 0)
//                     $after = $before + $report['productquantity'];
//                 else
//                     $after = $before - $report['productquantity'];

//                 $count++;

//                 R::exec('UPDATE storereport SET productbefore = ' . $before . ', productafter = ' . $after . ' where storereportid = ' . $report['storereportid']);
//             } else {
//                 $before = $report['productbefore'];
//                 $after = $report['productafter'];
//             }
//         }
//         $itr++;
//     }
//     if (count($allStoreSettelMentDetail) > 0) {
//         $storedetailQuantity = R::getRow('SELECT * FROM storedetail where storeid = ' . $storeid . ' and productid = ' . $productid);
//         // R::debug(true);
//         // if (!$storedetailQuantity) {
//         //     R::debug(true);
//         //     R::exec('insert into storedetail (`productid`, `storeid`, `productquantity`, `userid`, `storedetaildate`, `unittype`, `inUse`) values('.$pro["sellbilldetailproductid"].','.$store["storeid"].','.$after.',-1,"'.date("Y-m-d").'",0,0)');
//         // } else {
//         if ($storedetailQuantity && $storedetailQuantity['productquantity'] != $after)
//             R::exec('update storedetail set productquantity = ' . $after . ' where storedetailid = ' . $storedetailQuantity['storedetailid']);
//         // }
//     }
// }
// var_dump($proArr);
//====================================================================================
// $allClientSellbill = R::getAll('SELECT DISTINCT returnsellbilldetailproductid FROM returnsellbilldetail 
//                                 JOIN returnsellbill ON returnsellbilldetail.returnsellbillid = returnsellbill.returnsellbillid 
//                                  where returnsellbill.returnsellbillid  =;
//                                  ');
// $count = 0;
// $proArr = [];
// foreach ($allClientSellbill as $pro) {
//     $allStore = R::getAll('SELECT storeid from store where conditions = 0');
//     foreach ($allStore as $store) {
//         $allStoreReport = R::getAll('SELECT * from storereport where storeid = ' . $store["storeid"] . ' AND productid = ' . $pro['sellbilldetailproductid']);
//         $itr = 1;
//         foreach ($allStoreReport as $report) {
//             if($itr == 1)
//             {
//                 $before = $report['productbefore'];
//                 $after = $report['productafter'];
//             }
//             else{
//                 if ($after != $report['productbefore']) {
//                     if(!in_array($pro['sellbilldetailproductid'],$proArr)){
//                         echo 'storereportid = '.$report['storereportid'].'--------- '.$after .'--------------'.$report['productbefore'].'<br/>';
//                         array_push($proArr,$pro['sellbilldetailproductid']);
//                     }
//                     $before = $after;
//                     if ($report['storereporttype'] == 0)
//                         $after = $before + $report['productquantity'];
//                     else
//                         $after = $before - $report['productquantity'];

//                     $count++;

//                     R::exec('UPDATE storereport SET productbefore = ' . $before . ', productafter = ' . $after . ' where storereportid = ' . $report['storereportid']);
//                 } else {
//                     $before = $report['productbefore'];
//                     $after = $report['productafter'];
//                 }
//             }
//             $itr++;
//         }
//         if (count($allStoreReport) > 0) {
//             $storedetailQuantity = R::getRow('SELECT * FROM storedetail where storeid = ' . $store["storeid"] . ' and productid = ' . $pro['sellbilldetailproductid']);
//             // R::debug(true);
//             // if (!$storedetailQuantity) {
//             //     R::debug(true);
//             //     R::exec('insert into storedetail (`productid`, `storeid`, `productquantity`, `userid`, `storedetaildate`, `unittype`, `inUse`) values('.$pro["sellbilldetailproductid"].','.$store["storeid"].','.$after.',-1,"'.date("Y-m-d").'",0,0)');
//             // } else {
//                 if ($storedetailQuantity && $storedetailQuantity['productquantity'] != $after)
//                     R::exec('update storedetail set productquantity = ' . $after . ' where storedetailid = ' . $storedetailQuantity['storedetailid']);
//             // }
//         }
//     }
// }




// $opNum = R::getAll('SELECT * FROM `storemovement` where operationnum = 2147483647 group by driverdate order by transferproductid');
// $lastOpNum = 523284700;
// foreach ($opNum as $data) {
//     $lastOpNum ++;
//     R::exec('UPDATE storemovement SET operationnum = '.$lastOpNum.' where driverdate = "'.$data['driverdate'].'"');
// }

// $allClientIds = R::getCol('select DISTINCT clientid from clientdebtchange where clientdebtchangeafter < 0');
// foreach ($allClientIds as $client_id) {
//     $after = $before =  0;
//     $alldebtChanges = R::getAll('select clientdebtchangeid, clientid, clientdebtchangebefore ,clientdebtchangeamount,clientdebtchangeafter,clientdebtchangetype from clientdebtchange where clientid = ' . $client_id . ' order by clientdebtchangeid asc');
//     var_dump('select clientdebtchangeid, clientid, clientdebtchangebefore ,clientdebtchangeamount,clientdebtchangeafter,clientdebtchangetype from clientdebtchange where clientid = ' . $client_id . ' order by clientdebtchangeid asc');
//     $i = 0;
//     $check = 0;
//     foreach ($alldebtChanges as $change) {
//         $before = $after;
//         if ($i == 0) {
//             $i++;
//         } else {
//             var_dump($before);
//             var_dump($change);
//             // if ($change['clientdebtchangeafter'] < 0) {
//             if ($change['clientdebtchangebefore'] != $before || $check > 0) {
//                 $check = 1;
//                 if ($change['clientdebtchangetype'] == 1)
//                     $after = $before - $change['clientdebtchangeamount'];
//                 else
//                     $after = $before + $change['clientdebtchangeamount'];

//                 var_dump('UPDATE clientdebtchange SET clientdebtchangebefore = "' . $before . '", clientdebtchangeafter = "' . $after . '" where clientdebtchangeid = ' . $change["clientdebtchangeid"]);
//                 R::exec('UPDATE clientdebtchange SET clientdebtchangebefore = "' . $before . '", clientdebtchangeafter = "' . $after . '" where clientdebtchangeid = ' . $change["clientdebtchangeid"]);
//                 var_dump($client_id);
//             } else
//                 $after = $change['clientdebtchangeafter'];
//         }
//     }
//     R::exec('UPDATE client set clientdebt = "' . $after . '" where clientid = ' . $client_id);
// }
//======================Edit Storereport & storedetails===================================
// $allClientSellbill = R::getAll('SELECT DISTINCT sellbilldetailproductid FROM sellbilldetail 
//                                 JOIN sellbill ON sellbilldetail.sellbillid = sellbill.sellbillid ');
// $count = 0;
// $proArr = [];
// foreach ($allClientSellbill as $pro) {
//     $allStore = R::getAll('SELECT storeid from store where conditions = 0');
//     foreach ($allStore as $store) {
//         $allStoreReport = R::getAll('SELECT * from storereport where storeid = ' . $store["storeid"] . ' AND productid = ' . $pro['sellbilldetailproductid']);
//         $itr = 1;
//         foreach ($allStoreReport as $report) {
//             if($itr == 1)
//             {
//                 $before = $report['productbefore'];
//                 $after = $report['productafter'];
//             }
//             else{
//                 if ($after != $report['productbefore']) {
//                     if(!in_array($pro['sellbilldetailproductid'],$proArr)){
//                         echo 'storereportid = '.$report['storereportid'].'--------- '.$after .'--------------'.$report['productbefore'].'<br/>';
//                         array_push($proArr,$pro['sellbilldetailproductid']);
//                     }
//                     $before = $after;
//                     if ($report['storereporttype'] == 0)
//                         $after = $before + $report['productquantity'];
//                     else
//                         $after = $before - $report['productquantity'];

//                     $count++;

//                     R::exec('UPDATE storereport SET productbefore = ' . $before . ', productafter = ' . $after . ' where storereportid = ' . $report['storereportid']);
//                 } else {
//                     $before = $report['productbefore'];
//                     $after = $report['productafter'];
//                 }
//             }
//             $itr++;
//         }
//         if (count($allStoreReport) > 0) {
//             $storedetailQuantity = R::getRow('SELECT * FROM storedetail where storeid = ' . $store["storeid"] . ' and productid = ' . $pro['sellbilldetailproductid']);
//             // R::debug(true);
//             if (!$storedetailQuantity) {
//                 R::debug(true);
//                 R::exec('insert into storedetail (`productid`, `storeid`, `productquantity`, `userid`, `storedetaildate`, `unittype`, `inUse`) values('.$pro["sellbilldetailproductid"].','.$store["storeid"].','.$after.',-1,"'.date("Y-m-d").'",0,0)');
//             } else {
//                 if ($storedetailQuantity['productquantity'] != $after)
//                     R::exec('update storedetail set productquantity = ' . $after . ' where storedetailid = ' . $storedetailQuantity['storedetailid']);
//             }
//         }
//     }
// }
// ============================== Return delete returnsellbill ========================
// $laterNakdiClientId = R::getCell('SELECT laterNakdiClientId from programsettings where programsettingsid = 1');
// $allRetSellBills = R::getAll('select * from returnsellbill where delbyuserid = 7 and returnsellbilltotalpayed = 0 and returnsellbillclientid = ' . $laterNakdiClientId);
// $counter = 0;
// foreach ($allRetSellBills as $bill) {
//     $paymentData = R::getRow('SELECT billid, sum(clientdebtchangeamount) as clientdebtchangeamount
//                     FROM clientdebtchange where tablename = "clientPayedDeptReturnSellBillsController.php" and billid  = ' . $bill['returnsellbillid']);
//     if ($paymentData && $paymentData['clientdebtchangeamount'] > 0) {
//         $counter++;
//         R::exec(' update costcenterdetail SET del = 0 WHERE type in(1) and  modelid = ' . $bill['returnsellbillid']);
//         $retSellbillData = R::getRow('select * from returnsellbill where  returnsellbillid = ' . $bill['returnsellbillid']);
//         R::exec("UPDATE returnsellbill SET delbyuserid = 0, conditions = 0 WHERE returnsellbillid = " . $bill['returnsellbillid']);

//         $sellDetails = R::getAll('select * from returnsellbilldetail where returnsellbillid = ' . $bill['returnsellbillid']);
//         foreach ($sellDetails as $detail) {
//             $storedetailData = R::getRow('select * from storedetail where productid = ' . $detail['returnsellbilldetailproductid'] . ' and storeid = ' . $detail['storeid']);
//             R::exec('UPDATE storedetail SET  productquantity = productquantity + '.$detail['returnsellbilldetailquantity'] . ' WHERE storedetailid = ' . $storedetailData['storedetailid']);
//             R::exec('Delete from storereport where storereporttype = 1 and productid = ' . $detail['returnsellbilldetailproductid'] . ' and storeid = ' . $detail['storeid'] . ' and storereportmodelid = ' . $bill['returnsellbillid'] . ' and processname = "returnsellbillController.php"');
//         }
//         R::exec('UPDATE client SET clientdebt = clientdebt + '.$retSellbillData['returnsellbillaftertotalbill'] . ' WHERE clientid = ' . $laterNakdiClientId);
//         R::exec('Delete from clientdebtchange WHERE clientid = ' . $laterNakdiClientId . ' and clientdebtchangetype = 0 and tablename = "returnsellbillController.php" and clientdebtchangemodelid = ' . $bill['returnsellbillid']);
//     }
// }
// echo $bill['returnsellbillid'].', ';