<?php
//the global file operation
include("../public/impOpreation.php");

require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');


$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
$do = $_GET['do'];

//here the global templates
$smarty->display("header.html");




if (empty($do)) {
     $projectId = filter_input(INPUT_POST, 'projectId');
    if($projectId > 0){
       $projectData = R::load('project', $projectId);
       $smarty->assign("projectData", $projectData); 

     


       $clientdebtchangeData = R::getAll("SELECT * FROM clientdebtchange WHERE clientdebtchange.del = 0 and paySerialNo >  0 and  clientdebtchange.clientid = $projectData->clientid");
       $clientdebtchangeamount = R::getCell("SELECT sum(clientdebtchangeamount) FROM clientdebtchange WHERE clientdebtchange.del = 0 and paySerialNo >  0 and clientdebtchange.clientid = $projectData->clientid");
       $clientdebtchangeamountint = abs($clientdebtchangeamount);
       $smarty->assign("clientdebtchangeamountint", $clientdebtchangeamountint);
       $smarty->assign("clientdebtchangeData", $clientdebtchangeData); 



       $incomeData = R::getAll("SELECT * FROM `income` left join incometype on income.incomeTypeId = incometype.incomeTypeId WHERE income.conditions = 0 and income.costcenterid = $projectData->costcenterid");
       $allincomeValue = R::getCell("SELECT sum(incomeValue) FROM `income` left join incometype on income.incomeTypeId = incometype.incomeTypeId WHERE income.conditions = 0 and income.costcenterid = $projectData->costcenterid");
       $smarty->assign("allincomeValue", $allincomeValue); 
       $smarty->assign("incomeData", $incomeData); 



       $expensesData = R::getAll("SELECT * FROM expenses left join expensestype on expenses.expensestypeid = expensestype.expensestypeid WHERE expenses.conditions = 0 and expenses.costcenterid =  $projectData->costcenterid");
       $allexpensesValue = R::getCell("SELECT sum(expensesValue) FROM expenses left join expensestype on expenses.expensestypeid = expensestype.expensestypeid WHERE expenses.conditions = 0 and expenses.costcenterid =  $projectData->costcenterid");
       $smarty->assign("allexpensesValue", $allexpensesValue); 
       $smarty->assign("expensesData", $expensesData); 


       // $projectexchmaterial = R::getAll("SELECT * FROM projectexchmaterial WHERE  projectexchmaterial.projectid =  $projectId");
      $projectexchmaterial = R::findAll('projectexchmaterial'," WHERE projectexchmaterial.projectid = $projectId"); 
       foreach($projectexchmaterial as $projecte){
         $projectepro = R::getAll("SELECT * FROM projectexchmaterialdetail left join product on projectexchmaterialdetail.productId = product.productId WHERE projectexchmaterialdetail.projectexchid = $projecte->id"); 
         $projecte->proname = $projectepro;
      }
      // echo '<pre>';
      // print_r($projectexchmaterial);
      // echo '</pre>';
       $projectexchmaterialtotal = R::getRow("SELECT sum(totalbuyprice) as totals, sum(finalsupervision) as finalsupervisions FROM projectexchmaterial WHERE  projectexchmaterial.projectid =  $projectId");
       $smarty->assign("projectexchmaterialtotal", $projectexchmaterialtotal['totals']);
       $smarty->assign("projectexchmaterial", $projectexchmaterial); 



       // $projectexchmaterialreturn = R::getAll("SELECT * FROM projectexchmaterialreturn WHERE  projectexchmaterialreturn.projectid =  $projectId");

       $projectexchmaterialreturn = R::findAll('projectexchmaterialreturn'," WHERE projectexchmaterialreturn.projectid = $projectId"); 
       foreach($projectexchmaterialreturn as $projecte){
         $projectepro = R::getAll("SELECT * FROM projectexchmaterialdetailreturn left join product on projectexchmaterialdetailreturn.productId = product.productId WHERE projectexchmaterialdetailreturn.projectexchid = $projecte->id"); 
         $projecte->proname = $projectepro;
       }

       $projectexchmaterialreturntotal = R::getRow("SELECT sum(totalbuyprice) as totals ,sum(finalsupervision) as finalsupervisions FROM projectexchmaterialreturn WHERE  projectexchmaterialreturn.projectid =  $projectId");
       $smarty->assign("projectexchmaterialreturntotal", $projectexchmaterialreturntotal['totals']);
       $smarty->assign("projectexchmaterialreturn", $projectexchmaterialreturn); 

    //   $expenseexchanges = R::getAll("select * from expenseexchange where del = 0 and projectid = ? ",[$projectId]);
       $sumexpenseexchanges = R::getRow("SELECT sum(thevalue) as totals,  sum(finalsupervision) as finalsupervisions  FROM expenseexchange WHERE expenseexchange.del = 0 and expenseexchange.projectid = $projectId");

    //   $supplierdebtchangeData = R::getAll("SELECT * FROM `supplier` 
    //   left join supplierdebtchange on supplier.supplierid = supplierdebtchange.supplierid
    //     WHERE supplierdebtchange.del = 0 and supplierdebtchange.costcenterid = $projectData->costcenterid");
    //   $allsupplierdebtchangeamountv = R::getCell("SELECT sum(supplierdebtchangeamount) FROM `supplierdebtchange` WHERE  supplierdebtchange.del = 0 and  supplierdebtchange.costcenterid = $projectData->costcenterid");
    //   $smarty->assign("allsupplierdebtchangeamountv", $allsupplierdebtchangeamountv); 
    //   $smarty->assign("supplierdebtchangeData", $supplierdebtchangeData); 
        $smarty->assign("expenseexchanges", $expenseexchanges);
        $smarty->assign("sumexpenseexchanges", $sumexpenseexchanges);
         $finalsupervisions = ($sumexpenseexchanges['finalsupervisions'] + $projectexchmaterialtotal['finalsupervisions']) -  $projectexchmaterialreturntotal['finalsupervisions'] ;

       $proMasrofat = $clientdebtchangeamountint + $allincomeValue;
    //   $clientPaids = $projectexchmaterialtotal['totals'] - $projectexchmaterialreturntotal['totals'] + $sumexpenseexchanges['totals'] + $allexpensesValue + $allsupplierdebtchangeamount + $finalsupervisions;
              $clientPaids = $projectexchmaterialtotal['totals'] - $projectexchmaterialreturntotal['totals']  + $allexpensesValue + $allsupplierdebtchangeamount + $finalsupervisions;

    //   echo $proMasrofat .' / '.$clientPaids;
       $operation = abs($clientPaids) - $proMasrofat;
       
//       echo 2222222222;
// exit();
      
        if($proMasrofat < abs($clientPaids))
        {
            
            $txt = 'صافي مديونية';
        }
        else
        {
            $txt = 'صافي رصيد ';
        }
        $operation = abs($operation);
         $smarty->assign("operation", $operation);
         $smarty->assign("txt", $txt);

    }


    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

$smarty->assign("proMasrofat", $proMasrofat); 
$smarty->assign("clientPaids", $clientPaids); 
$smarty->assign("finalsupervisions", $finalsupervisions); 

    $Programsettingdata = R::getRow('SELECT * FROM `programsettings` WHERE programsettingsid = 1');
    $smarty->assign("Programsettingdata", $Programsettingdata); 
    $smarty->assign("projectValidation", 1); 
    $smarty->display("projectview/reportdetail.html");
}
$smarty->display("footer.html");



?>