<?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 clientdebtchange.clientid = $projectData->clientid");
       $clientdebtchangeamount = R::getCell("SELECT sum(clientdebtchangeamount) FROM clientdebtchange WHERE clientdebtchange.del = 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::getCell("SELECT sum(totalbuyprice) FROM projectexchmaterial WHERE  projectexchmaterial.projectid =  $projectId");
       $smarty->assign("projectexchmaterialtotal", $projectexchmaterialtotal);
       $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::getCell("SELECT sum(totalbuyprice) FROM projectexchmaterialreturn WHERE  projectexchmaterialreturn.projectid =  $projectId");
       $smarty->assign("projectexchmaterialreturntotal", $projectexchmaterialreturntotal);

       $smarty->assign("projectexchmaterialreturn", $projectexchmaterialreturn); 


       $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); 


       $proMasrofat = $clientdebtchangeamountint + $allincomeValue;
       $clientPaids = $projectexchmaterialtotal - $projectexchmaterialreturntotal + $allexpensesValue + $allsupplierdebtchangeamountv;
       $operation = abs($clientPaids) - $proMasrofat;

        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);





    $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");



?>