<?php

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

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

require_once('../models/dao/ProjectDAO.class.php');
require_once('../models/dto/Project.class.php');
require_once('../models/mysql/ProjectMySqlDAO.class.php');
require_once('../models/mysql/ext/ProjectMySqlExtDAO.class.php');

//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');


require_once('../models/dao/CostcenterDAO.class.php');
require_once('../models/dto/Costcenter.class.php');
require_once('../models/mysql/CostcenterMySqlDAO.class.php');
require_once('../models/mysql/ext/CostcenterMySqlExtDAO.class.php');

//Accountstree
require_once('../models/dao/AccountstreeDAO.class.php');
require_once('../models/dto/Accountstree.class.php');
require_once('../models/mysql/AccountstreeMySqlDAO.class.php');
require_once('../models/mysql/ext/AccountstreeMySqlExtDAO.class.php');

//Expenseexchange
require_once('../models/dao/ExpenseexchangeDAO.class.php');
require_once('../models/dto/Expenseexchange.class.php');
require_once('../models/mysql/ExpenseexchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpenseexchangeMySqlExtDAO.class.php');

####Projectexchmaterial
require_once('../models/dao/ProjectexchmaterialDAO.class.php');
require_once('../models/dto/Projectexchmaterial.class.php');
require_once('../models/mysql/ProjectexchmaterialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProjectexchmaterialMySqlExtDAO.class.php');

####ProjectexchmaterialReturn
require_once('../models/dao/ProjectexchmaterialreturnDAO.class.php');
require_once('../models/dto/Projectexchmaterialreturn.class.php');
require_once('../models/mysql/ProjectexchmaterialreturnMySqlDAO.class.php');
require_once('../models/mysql/ext/ProjectexchmaterialreturnMySqlExtDAO.class.php');

//Income
require_once('../models/dao/IncomeDAO.class.php');
require_once('../models/dto/Income.class.php');
require_once('../models/mysql/IncomeMySqlDAO.class.php');
require_once('../models/mysql/ext/IncomeMySqlExtDAO.class.php');

//Incometype
require_once('../models/dao/IncometypeDAO.class.php');
require_once('../models/dto/Incometype.class.php');
require_once('../models/mysql/IncometypeMySqlDAO.class.php');
require_once('../models/mysql/ext/IncometypeMySqlExtDAO.class.php');


//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');

//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.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');


include_once("dailyentryfun.php");
//get the do the action
$do = $_GET['do'];
$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");
/* ======================

  Controller Name :- productCatController

  OPERTATION in Controller

  1-display add form
  2- add in productCat tble
  4-display sucess or error
  5-display show and delete
  6-delete from productCat tbl
  7-display edit
  8- update productCat tbl
  ======================== */

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

//here goes the instances and general variables
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();

$Project = new Project();
$ProjectDAO = new ProjectMySqlDAO();
$ProjectEx = new ProjectMySqlExtDAO();

//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();

//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$expenses = new Expense();
$expensesDao = new ExpensesMySqlDAO();
$expensesExt = new ExpensesMySqlExtDAO();

//Expenseexchange
$expenseExchange = new Expenseexchange();
$expenseExchangeDAO = new ExpenseexchangeMySqlDAO();
$expenseExchangeEX = new ExpenseexchangeMySqlExtDAO();

###Projectexchmaterial
$Projectexchmaterial = new Projectexchmaterial();
$ProjectexchmaterialDAO = new ProjectexchmaterialMySqlDAO();
$ProjectexchmaterialEx = new ProjectexchmaterialMySqlExtDAO();

###Projectexchmaterial
$ProjectexchmaterialReturn = new Projectexchmaterialreturn();
$ProjectexchmaterialReturnDAO = new ProjectexchmaterialreturnMySqlDAO();
$ProjectexchmaterialReturnEx = new ProjectexchmaterialreturnMySqlExtDAO();

//incomeType
$incomeType = new Incometype();
$IncomeTypeDAO = new IncometypeMySqlDAO();
$IncomeTypeEX = new IncometypeMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

//income
$income = new Income();
$incomeDAO = new IncomeMySqlDAO();
$incomeEX = new IncomeMySqlExtDAO();

//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();


//Supplierdebtchange
$Supplierdebtchange = new Supplierdebtchange();
$SupplierdebtchangeDAO = new SupplierdebtchangeMySqlDAO();
$SupplierdebtchangeEX = new SupplierdebtchangeMySqlExtDAO();

$today = date("Y-m-d");


//print_r($Programsettingdata);
//check and use the condition that suite this action
if (empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

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

    $smarty->assign("projectValidation", 1);
    $smarty->display("projectview/report.html");
} elseif ($do == 'search') {
    //here the permission check
    include_once("../public/authentication.php");
    



    $projectId = filter_input(INPUT_POST, 'projectId');
    if ($projectId < 1) {
        header("location:projectReportController.php");
    }
  
    if ($projectId != -1) {
        $projectData = $ProjectDAO->load($projectId);
        $allincomeData = $incomeEX->queryAllWithDateAndType2($projectData->costcenterid);
        $incomeData = 0;
        foreach ($allincomeData as $income) {
            $incomeData += $income->incomeValue;
        }
        $smarty->assign('incomeData', $incomeData);

        // $clientData = $clientDAO->load($projectData->clientid);
        // $clientPaid = $clientData->clientdebt;
        
        $allclientDeptChangeDAO = $clientDeptChangeExt->queryByDeptChangeProject($projectData->clientid);

        $clientPaid = 0;
        foreach ($allclientDeptChangeDAO as $clientDeptChangeDAO) {
            if($clientDeptChangeDAO->clientdebtchangetype == 0){
                $clientPaid += $clientDeptChangeDAO->clientdebtchangeamount;
            }else{
                $clientPaid -= $clientDeptChangeDAO->clientdebtchangeamount;
            }
            
        }


        if ($clientPaid < 0)
            $clientPaid = abs($clientPaid);



        $allMaterial = $ProjectexchmaterialDAO->queryByProjectid($projectId);
        $totalallMaterial = 0;
        foreach ($allMaterial as $Material) {
            $totalallMaterial += $Material->totalbuyprice;
        }
        if (!empty($allMaterial))
            $smarty->assign('material', 1);

        $allMaterialReturn = $ProjectexchmaterialReturnDAO->queryByProjectid($projectId);
        $totalallMaterialReturn = 0;
        foreach ($allMaterialReturn as $MaterialReturn) {
            $totalallMaterialReturn += $MaterialReturn->totalbuyprice;
        }
        if (!empty($allMaterialReturn))
            $smarty->assign('returnMaterial', 1);
        $supplies = $totalallMaterial - $totalallMaterialReturn;

        $masroofat = 0;
        $allMasroof = $expensesExt->queryByCostCenter($projectData->costcenterid);
        foreach ($allMasroof as $masroof) {
            $masroofat += $masroof->expensesValue;
        }


        $supplierdebtchangeamountto = 0;
        $allSupplierdebtchange = $SupplierdebtchangeEX->queryByCostCenter($projectData->costcenterid);
        foreach ($allSupplierdebtchange as $Supplierdebtchange) {
            $supplierdebtchangeamountto += $Supplierdebtchange->supplierdebtchangeamount;
        }



        $allExpenseExchange = $expenseExchangeDAO->queryByProjectid($projectId);
        foreach ($allExpenseExchange as $masroof) {
            $masroofat += $masroof->thevalue;
        }

        $proMasrofat = $supplies + $masroofat + $supplierdebtchangeamountto;
        $clientPaids = $clientPaid + $incomeData;
        $operation = $clientPaids - abs($proMasrofat);

        if ($operation < 0) {
            $operation = abs($operation);
            $txt = 'صافي الخسارة';
        } else {
            $txt = 'صافي الربح';
        }

        $smarty->assign("allProjects", $allProjects);
        $smarty->assign("paied", $clientPaid);
        $smarty->assign("supplies", $supplies);
        $smarty->assign("supplierdebtchangeamountto", $supplierdebtchangeamountto);
        $smarty->assign("expenses", $masroofat);
        $smarty->assign("operation", $operation);
        $smarty->assign("txt", $txt);
        $smarty->assign("project", $projectData);
    }

    $smarty->assign("projectValidation", 1);
    $smarty->display("projectview/report.html");
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}


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

/* ===============================
  function in this CONTROLLER
  ================================ */
?>