<?php
include("../public/impOpreation.php");
$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {  
    $smarty->display("header.html");
    $smarty->display("closingAccountview/add.html");
    $smarty->display("footer.html");
}else if ($do ==  'save') {  

$client_id = filter_input(INPUT_POST, 'client_id');  
$date_close =  filter_input(INPUT_POST, 'date_close');  
$closing_account = R::dispense('closingaccountclient');
$closing_account->client_id = $client_id;    
$closing_account->date_close =  $date_close;  
$closing_account->today =  $today;  
$closing_account->userid = $_SESSION['userid'];
R::store($closing_account);
R::exec("UPDATE `client` SET `datecustomeraccount`= '$date_close' WHERE clientid = $client_id");
header("location:?do=sucess");
} elseif ($do == "sucess") {
$smarty->display("header.html");
$smarty->display("succes.html");
$smarty->display("footer.html");
}else if ($do ==  'show') {  
    $smarty->display("header.html");
    $smarty->display("closingAccountview/show.html");
    $smarty->display("footer.html");
} else if ($do == 'showajax') {
    showajax();    
} else if ($do == "getdata") { 

    $client_id =  filter_input(INPUT_POST, 'client_id');  
    $client = R::getRow("SELECT * FROM `client` WHERE clientid = ? ",[$client_id]);
    $datecustomeraccount = $client['datecustomeraccount'];

    $totals = 0;
    $debitQuery = '';
    $sellQuery = '';
    $returnQuery = '';
    if($datecustomeraccount != '0000-00-00' && $datecustomeraccount != ''){
        $sellQuery .=' and  sellbill.sellbilldate  >= "' . $datecustomeraccount . ' 00-00-00" and sellbill.sellbilldate <= "' . $datecustomeraccount . ' 23-59-55" ';

        $returnQuery .=' and  returnsellbill.returnsellbilldate  >= "' . $datecustomeraccount . ' 00-00-00" and returnsellbill.returnsellbilldate <= "' . $datecustomeraccount . ' 23-59-55" ';

        $debitQuery .=' and  clientdebtchange.clientdebtchangedate  >= "' . $datecustomeraccount . '" and clientdebtchange.clientdebtchangedate <= "' . $datecustomeraccount . '" ';
    }

        $totals += R::getcell("SELECT sum(clientdebtchangeamount) FROM clientdebtchange  where clientdebtchange.del = 0 and clientdebtchange.clientid =   $client_id  $debitQuery");
        $totals -= R::getcell("SELECT sum(clientdebtchangeamount) FROM clientdebtchange  where clientdebtchange.del = 1 and clientdebtchange.clientid =  $client_id  $debitQuery");

        $totals += R::getcell("SELECT sum(sellbilltotalpayed) FROM sellbill  where sellbill.conditions = 0 and sellbill.sellbillclientid =   $client_id  $sellQuery");
        $totals -= R::getcell("SELECT sum(sellbilltotalpayed) FROM sellbill  where sellbill.conditions = 1 and sellbill.sellbillclientid =  $client_id  $sellQuery");

        $totals += R::getcell("SELECT sum(returnsellbilltotalpayed) FROM returnsellbill  where returnsellbill.conditions = 1 and returnsellbill.returnsellbillclientid =  $client_id  $returnQuery");

        $totals -= R::getcell("SELECT sum(returnsellbilltotalpayed) FROM returnsellbill  where returnsellbill.conditions = 0 and returnsellbill.returnsellbillclientid =   $client_id  $returnQuery");
        
    echo $totals;

}else if ($do == 'select2client') {

    $name = $_POST['searchTerm'];
    $productsData = R::getAll("SELECT clientid, clientname as texts
    FROM client WHERE conditions = 0 and clientname LIKE '%" . $name . "%' limit 50");
    $return_arr = array();
    foreach ($productsData as $pro) {
        $row_array = array();
        $row_array['id'] = $pro['clientid'];
        $row_array['text'] = $pro['texts'];
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}



function showajax() {
    $columns = array('closingaccountclient.id', 'client_id', 'date_close', 'userid', 'today');

    $fromdate = $_POST['fromdate'];
    $todate = $_POST['todate'];
    $clientid = $_POST['data1'];



    $searchQuery = " ";
    if ($clientid != '') {
        $searchQuery .= " and closingaccountclient.client_id = " . $clientid . " ";
    }
   

    if ($fromdate != '' && $todate != '') {
        $searchQuery .='and  closingaccountclient.today  >= "' . $fromdate . ' 00-00-00" and closingaccountclient.today <= "' . $todate . ' 23-59-55" ';
    }else{
        $today = date("Y-m-d");
        $searchQuery .='and  closingaccountclient.today  >= "' . $today . ' 00-00-00" and closingaccountclient.today <= "' . $today . ' 23-59-55" ';
    }


    if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {
        $searchQuery .= "and ( clientname LIKE " % '.$_POST["search"]["value"].' % "
                    OR date_close LIKE " % '.$_POST["search"]["value"].' % "
                    OR closingaccountclient.today LIKE " % '.$_POST["search"]["value"].' % "
                    OR employeename LIKE " % '.$_POST["search"]["value"].' % "
        )
     ";
    }


    if (isset($_POST["order"])) {
        $searchQuery .= 'ORDER BY ' . $columns[$_POST['order']['0']['column']] . ' ' . $_POST['order']['0']['dir'] . '  ';
    }
    else
    {
     $searchQuery .= "ORDER BY  closingaccountclient.id DESC ";
    }
    $alltotal = R::count("closingaccountclient"," LEFT JOIN client ON closingaccountclient.client_id = client.clientid
            LEFT JOIN user ON  user.userid = closingaccountclient.userid
            WHERE 1 $searchQuery ");


    if (isset($_POST['start']) && $_POST['length'] != '-1') {
        $searchQuery .= "LIMIT " . intval($_POST['start']) . ", " .
                intval($_POST['length']);
    }

    $rResult = R::getAll("SELECT *,employeename, clientname FROM `closingaccountclient`
            LEFT JOIN client ON closingaccountclient.client_id = client.clientid
            LEFT JOIN user ON  user.userid = closingaccountclient.userid
            WHERE 1  $searchQuery ");

    $output = array(
        "recordsTotal" => intval($alltotal),
        "recordsFiltered" =>  count($rResult),
        "data" => array()
    );
    foreach ($rResult as $row) {
        
        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["clientname"];
        $sub_array[] = $row["date_close"];
        $sub_array[] = $row["employeename"];
        $sub_array[] = $row["today"];
        $output['data'][] = $sub_array;

    }

    echo json_encode($output, JSON_UNESCAPED_SLASHES);
}

?>

