<?php


include_once("../public/con_reedbean.php");

//get the do the action
$do = $_GET['do'];

$today = date("Y-m-d");
$smarty->assign("today", $today);




if ($do == "transfersell") {

    $databasefrom = filter_input(INPUT_POST, 'databasefrom');
    $databaseto = filter_input(INPUT_POST, 'databaseto');
    $transfervalue = filter_input(INPUT_POST, 'transfervalue');
    $transferdate = filter_input(INPUT_POST, 'transferdate');


 try {
  

    R::addDatabase('databasefrom','mysql:host=localhost;dbname=' . $databasefrom . '', 'root', '');
    R::addDatabase('databaseto','mysql:host=localhost;dbname=' . $databaseto . '', 'root', '');
    R::selectDatabase('databasefrom');
    $sellbillfromcols = R::getAll("DESCRIBE sellbill");
    $sellbilldetailfromcols = R::getAll("DESCRIBE sellbilldetail");
        $queryinsert1 = '';
        foreach($sellbillfromcols as $sellbillfromcol){
            $queryinsert1 .= ', ' . $sellbillfromcol['Field'];            
        }
        $queryinsert1d = '';
        foreach($sellbilldetailfromcols as $sellbilldetailfromcol){
            $queryinsert1d .= ', ' . $sellbilldetailfromcol['Field'];
        } 
        $sellbillfroms = R::getAll("SELECT sellbillid , sellbillaftertotalbill FROM `sellbill` where conditions = 0 and  sellbill.sellbilldate  >= '$transferdate 00-00-00' and sellbill.sellbilldate <= '$transferdate 23-59-55' ");
        $queryinsert1d = substr($queryinsert1d, 1);
        $queryinsert1din = str_replace(', sellbilldetailid', '', $queryinsert1d) ;
        $transfertotal = $transfervalue;
        
        foreach($sellbillfroms as $sellbillfrom){
            if ($transfertotal >= $sellbillfrom['sellbillaftertotalbill']) {
                $sellbillaftertotalbillu = $sellbillfrom['sellbillaftertotalbill'];
                 R::exec("INSERT INTO $databaseto.sellbill(".substr($queryinsert1, 13).")
                SELECT ".substr($queryinsert1, 13)." FROM $databasefrom.sellbill where $databasefrom.sellbill.sellbillid = ?",[$sellbillfrom['sellbillid']]); 
                 R::selectDatabase('databasefrom'); 
                $sellbilldetailfroms = R::getAll("SELECT sellbilldetailid FROM `sellbilldetail` WHERE sellbillid = ?",[$sellbillfrom['sellbillid']]); 
               
                R::selectDatabase('databaseto'); 
                $sellbilltolastid = R::getcell("SELECT sellbillid FROM `sellbill` order by sellbillid desc");
                $saveget = R::getRow("SELECT * FROM `save` WHERE saveid = 1 ");
                $savedailysavebefore = $saveget['savedailysavebefore'];
                $savedailysaveafter = $savedailysavebefore + $sellbillaftertotalbillu;
                $savedailydate = date('Y-m-d H:i:s');
                
                R::exec("INSERT INTO `savedaily`(`savedailysavebefore`, `savedailychangeamount`, `savedailychangetype`, `saveid`,
                  `processname`, `savedailymodelid`, `savedailysaveafter`, `savedailydate`, `userid`, `tablename`, `clientid`) 
                  VALUES ('$savedailysavebefore', '$sellbillaftertotalbillu', 0, 1, 'اضافة فاتورة مبيعات', $sellbilltolastid, '$savedailysaveafter', 
                '$savedailydate', 1, 'sellbillController.php', 0)");
                $queryinsert1ds = str_replace('sellbillid', "$sellbilltolastid as sellbillid", $queryinsert1din);
                foreach($sellbilldetailfroms as $sellbilldetailfrom){
                     R::exec("INSERT INTO $databaseto.sellbilldetail($queryinsert1din)
                 SELECT $queryinsert1ds  FROM $databasefrom.sellbilldetail where $databasefrom.sellbilldetail.sellbilldetailid =  ?",[$sellbilldetailfrom['sellbilldetailid']]); 
                  R::exec("UPDATE `save` SET `savecurrentvalue`= savecurrentvalue + $sellbillaftertotalbillu  WHERE saveid = 1 "); 
                }

            }
            $transfertotal -= $sellbillfrom['sellbillaftertotalbill']; 

        } 
         R::selectDatabase('default'); 
        $counttransfersell = R::count('transfersell');
        if($counttransfersell > 0){
            $transfersell = R::load('transfersell',1);
            $transfersell->databasefrom = $databasefrom;
            $transfersell->databaseto = $databaseto;
            $transfersell->transfervalue = $transfervalue;
            $transfersell->transfertoday = $transferdate;
            R::store($transfersell);
        }else{
            $transfersell = R::dispense('transfersell');
            $transfersell->databasefrom = $databasefrom;
            $transfersell->databaseto = $databaseto;
            $transfersell->transfervalue = $transfervalue;
            $transfersell->transfertoday = date("Y-m-d");  
            R::store($transfersell);
        }

       echo '1' ;
    } catch (Exception $e) {
           echo '0' ;
    }
   
}


?>