<?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 = '';
    $projectexchmaterialreturnQuery = '';
     $incomeQuery = '';
    if ($start_date != '' ) {
        $clientdebtchangeQuery .=' and  clientdebtchange.clientdebtchangedate  >= "' . $start_date . ' 00-00-00" ';
        $expenseexchangeQuery .=' and  expenseexchange.expensedate  >= "' . $start_date . '" ';
        $projectexchmaterialchangeQuery .=' and  projectexchmaterial.projdate  >= "' . $start_date . '" ';
        $projectexchmaterialreturnQuery .=' and  projectexchmaterialreturn.projdate  >= "' . $start_date . '" ';
        $projectexchmaterialreturnQuery .=' and  income.incomeDate  >= "' . $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 . '" ';
        $projectexchmaterialreturnQuery .=' and  projectexchmaterialreturn.projdate  <= "' . $end_date . '" ';
        $incomeQuery .=' and  income.incomeDate  >= "' . $start_date . '" ';

    }

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




    
    $expensestypeids = '0';
    $arrayexpensestypeids = [];
    $allexpensestype = [];
    $alltotals = 0;
    $alltotalvalues = 0;
    $projectexchmaterialdetails = R::getAll("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from projectexchmaterialdetail  JOIN projectexchmaterial
         ON projectexchmaterialdetail.projectexchid = projectexchmaterial.id WHERE projectexchmaterial.projectid = ? $projectexchmaterialchangeQuery group by projectexchmaterialdetail.expensestypeid ",[$project->id]);
         
        //  print_r($projectexchmaterialdetails);
        //  exit();
    foreach ($projectexchmaterialdetails as  $projectexchmaterialdetail) {
            $expensestypeids .= ',' . $projectexchmaterialdetail['expensestypeid'];
            $expensestypedata = R::getRow('SELECT expensestype.* FROM `expensestype` WHERE expensestypeid = ? ',[$projectexchmaterialdetail['expensestypeid']]);
            $expenseexchange = R::getROW("select sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions from expenseexchange where del = 0 and projectid = ? and expensetype = ? $expenseexchangeQuery ",[$project->id, $projectexchmaterialdetail['expensestypeid']]);
            $projectexchmaterialdetailreturn = R::getROW("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from  projectexchmaterialdetailreturn  JOIN projectexchmaterialreturn
         ON  projectexchmaterialdetailreturn.projectexchid = projectexchmaterialreturn.id WHERE projectexchmaterial.projectid = ? and projectexchmaterialdetailreturn.expensestypeid = ?  $projectexchmaterialreturnQuery  ",[$project->id, $projectexchmaterialdetail['expensestypeid']]);
            $totals =  $expenseexchange['totals'] + $projectexchmaterialdetail['totals'] - $projectexchmaterialdetailreturn['totals'];
            $finalsupervisions =  $expenseexchange['finalsupervisions'] + $projectexchmaterialdetail['finalsupervisions'] - $projectexchmaterialdetailreturn['finalsupervisions'];
             $expensestype = [
                'expensestypeid' => $expensestypedata['expensestypeid'],
                'expensestypename' => $expensestypedata['expensestypename'],
                'totals' =>  $totals,
                'finalsupervisions' =>  $finalsupervisions,
             ];
             $allexpensestype[] = $expensestype;
             $alltotals +=  $totals;
             $alltotalvalues += $finalsupervisions;


    }

    
    
     $projectexchmaterialdetailreturns = R::getAll("select *, sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions from projectexchmaterialdetailreturn  JOIN projectexchmaterialreturn
         ON projectexchmaterialdetailreturn.projectexchid = projectexchmaterialreturn.id WHERE projectexchmaterialreturn.projectid = ? and projectexchmaterialdetailreturn.expensestypeid not in ($expensestypeids) $projectexchmaterialreturnQuery group by projectexchmaterialdetailreturn.expensestypeid ",[$project->id]);
       
      
    foreach ($projectexchmaterialdetailreturns as  $projectexchmaterialdetailreturn) {
            $expensestypeids .= ',' . $projectexchmaterialdetailreturn['expensestypeid'];
            $expensestypedata = R::getRow('SELECT expensestype.* FROM `expensestype` WHERE expensestypeid = ? ',[$projectexchmaterialdetailreturn['expensestypeid']]);
            $expenseexchange = R::getROW("select sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions from expenseexchange where del = 0 and projectid = ? and expensetype = ? $expenseexchangeQuery ",[$project->id, $projectexchmaterialdetailreturn['expensestypeid']]);
            $totals =  $expenseexchange['totals'] - $projectexchmaterialdetailreturn['totals'];
            $finalsupervisions =  $expenseexchange['finalsupervisions'] - $projectexchmaterialdetailreturn['finalsupervisions'];
  
             $expensestype = [
                'expensestypeid' => $expensestypedata['expensestypeid'],
                'expensestypename' => $expensestypedata['expensestypename'],
                'totals' =>  $totals,
                'finalsupervisions' =>  $finalsupervisions,
             ];
             $allexpensestype[] = $expensestype;
             $alltotals +=  $totals;
             $alltotalvalues += $finalsupervisions;


    }
    // echo "select *, sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions from expenseexchange where del = 0 and projectid = 5 and expensetype not in ($expensestypeids) $expenseexchangeQuery  group by expenseexchange.expensetype ";
    $expenseexchanges = R::getAll("select *, sum(thevalue) as totals, sum(finalsupervision) as finalsupervisions 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']]);
    
         $expensestype = [
            'expensestypeid' => $expensestypedata['expensestypeid'],
            'expensestypename' => $expensestypedata['expensestypename'],
            'totals' =>  $expenseexchange['totals'],
            'finalsupervisions' =>  $expenseexchange['finalsupervisions'],
         ];
         $allexpensestype[] = $expensestype;
         $alltotals +=  $expenseexchange['totals'];
         $alltotalvalues += $expenseexchange['finalsupervisions'];

            
    }
    





    $smarty->assign("allexpensestype", $allexpensestype);
    $smarty->assign("alltotals", $alltotals);
    // if($project->supervision_type == 2){
        $smarty->assign("alltotalvalues", $alltotalvalues);
    // }else{
    //     $smarty->assign("alltotalvalues", $project->supervision_amount);
    // }
    
$smarty->assign("project", $project);
    $smarty->assign("projectid", $projectid);
    $smarty->display("header.html");
    $smarty->display("projectsaccountreportview/show.html");
    $smarty->assign("project", 1);
    $smarty->display("footer.html");
}


?>