<?php
include("../public/impOpreation.php");

$do = $_GET['do'];
$today = date("Y-m-d H:i:s");
$date = date("Y-m-d");

if (empty($do)) {  


    $projectid = filter_input(INPUT_POST, 'projectid');
    $start_date = filter_input(INPUT_POST, 'start_date');
    $end_date = filter_input(INPUT_POST, 'end_date');


    $clientdebtchangeQuery = '';
    $expenseexchangeQuery = '';
    $projectexchmaterialchangeQuery = '';
    if ($start_date != '' ) {
        $clientdebtchangeQuery .=' and  clientdebtchange.clientdebtchangedate  >= "' . $start_date . ' 00-00-00" ';
        $expenseexchangeQuery .=' and  expenseexchange.expensedate  >= "' . $start_date . '" ';
        $projectexchmaterialchangeQuery .=' and  projectexchmaterial.projdate  >= "' . $start_date . '" ';

    }


    if ($end_date != '' ) {
        $clientdebtchangeQuery .=' and  clientdebtchange.clientdebtchangedate  <= "' . $end_date . ' 23-59-55" ';
        $expenseexchangeQuery .=' and  expenseexchange.expensedate  <= "' . $end_date . '" ';
        $projectexchmaterialchangeQuery .=' and  projectexchmaterial.projdate  <= "' . $end_date . '" ';
    }

    $project = R::load('project',$projectid);
    // // to
    $clientdebtchanges = R::getAll("SELECT clientdebtchange.* FROM `clientdebtchange` WHERE clientid = ? and del = 0 $clientdebtchangeQuery ",[$project->clientid]);
    $smarty->assign("clientdebtchanges", $clientdebtchanges);




    
    $expensestypeids = '0';
    $arrayexpensestypeids = [];
    $allexpensestype = [];
    $alltotals = 0;
    $alltotalvalues = 0;
    $projectexchmaterialdetails = R::getAll("select *, sum(pronumber * probuyprice) as totals from projectexchmaterialdetail  JOIN projectexchmaterial
         ON projectexchmaterialdetail.projectexchid = projectexchmaterial.id WHERE projectexchmaterial.projectid = ? $projectexchmaterialchangeQuery group by projectexchmaterialdetail.expensestypeid ",[$project->id]);
    foreach ($projectexchmaterialdetails as  $projectexchmaterialdetail) {
            $expensestypeids .= ',' . $projectexchmaterialdetail['expensestypeid'];
            $expensestypedata = R::getRow('SELECT expensestype.* FROM `expensestype` WHERE expensestypeid = ? ',[$projectexchmaterialdetail['expensestypeid']]);
            $expenseexchange = R::getcell("select *, sum(thevalue) as totals from expenseexchange where del = 0 and projectid = ? and expensetype = ? $expenseexchangeQuery ",[$project->id, $projectexchmaterialdetail['expensestypeid']]);
            $totalend =  $expenseexchange + $projectexchmaterialdetail['totals'];
             if ($expensestypedata['withinsupervision_ratio'] == 1) {
                 if ($expensestypedata['supervision_ratiotype'] == 1) {
                    $amount = $expensestypedata['supervision_amount'];
                    $amounttext =  'قيمة';
                 }else{
                    $amount = round(($totalend * $expensestypedata['supervision_amount']) / 100, 2);
                    $amounttext = $expensestypedata['supervision_amount'] . '%';
                 }
             }else{
                $amounttext = '';
                $amount = 0;
             }
             $expensestype = [
                'expensestypeid' => $expensestypedata['expensestypeid'],
                'expensestypename' => $expensestypedata['expensestypename'],
                'withinsupervision_ratio' => $expensestypedata['withinsupervision_ratio'],
                'supervision_ratiotype' => $expensestypedata['supervision_ratiotype'],
                'supervision_amount' => $expensestypedata['supervision_amount'],
                'totalend' =>  $totalend,
                'amounttext' =>  $amounttext,
                'amount' => $amount,
             ];
             $allexpensestype[] = $expensestype;
             $alltotals +=  $totalend;
             $alltotalvalues += $amount;


    }

     $expenseexchange = R::getAll("select *, sum(thevalue) as totals from expenseexchange where del = 0 and projectid = ? and expensetype not in ($expensestypeids) $expenseexchangeQuery  group by expenseexchange.expensetype ",[$project->id]);
    foreach ($expenseexchanges as  $expenseexchange) {
         $expensestypedata = R::getRow('SELECT expensestype.* FROM `expensestype` WHERE expensestypeid = ? ',[$expenseexchange['expensetype']]);
         $totalend = $expenseexchange['thevalue'];
         if ($expensestypedata['withinsupervision_ratio'] == 1) {
             if ($expensestypedata['supervision_ratiotype'] == 1) {
                $amount = $expensestypedata['supervision_amount'];
                $amounttext =  'قيمة';
             }else{
                $amount = round(($totalend * $expensestypedata['supervision_amount']) / 100, 2);
                $amounttext = $expensestypedata['supervision_amount'] . '%';
             }
         }else{
            $amounttext = '';
            $amount = 0;
         }
         $expensestype = [
            'expensestypeid' => $expensestypedata['expensestypeid'],
            'expensestypename' => $expensestypedata['expensestypename'],
            'withinsupervision_ratio' => $expensestypedata['withinsupervision_ratio'],
            'supervision_ratiotype' => $expensestypedata['supervision_ratiotype'],
            'supervision_amount' => $expensestypedata['supervision_amount'],
            'totalend' =>  $totalend,
            'amounttext' =>  $amounttext,
            'amount' => $amount,
         ];
         $allexpensestype[] = $expensestype;
         $alltotals +=  $totalend;
         $alltotalvalues += $amount;

            
    }




    $smarty->assign("allexpensestype", $allexpensestype);
    $smarty->assign("alltotals", $alltotals);
    $smarty->assign("alltotalvalues", $alltotalvalues);

    $smarty->assign("projectid", $projectid);
    $smarty->display("header.html");
    $smarty->display("projectsaccountreportview/show.html");
    $smarty->assign("project", 1);
    $smarty->display("footer.html");
}


?>