<?php
require_once("../public/con_reedbean.php");

// $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);
// }
