<?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
// GOES HERE ....................
include("../public/include_dao.php");


//Storedetail
require_once('../models/dao/StoredetailDAO.class.php');
require_once('../models/dto/Storedetail.class.php');
require_once('../models/mysql/StoredetailMySqlDAO.class.php');
require_once('../models/mysql/ext/StoredetailMySqlExtDAO.class.php');

//Product
require_once('../models/dao/ProductDAO.class.php');
require_once('../models/dto/Product.class.php');
require_once('../models/mysql/ProductMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductMySqlExtDAO.class.php');

//Productunit
require_once('../models/dao/ProductunitDAO.class.php');
require_once('../models/dto/Productunit.class.php');
require_once('../models/mysql/ProductunitMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductunitMySqlExtDAO.class.php');

//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');


//Bankaccount
require_once('../models/dao/BankaccountDAO.class.php');
require_once('../models/dto/Bankaccount.class.php');
require_once('../models/mysql/BankaccountMySqlDAO.class.php');
require_once('../models/mysql/ext/BankaccountMySqlExtDAO.class.php');


//Capital
require_once('../models/dao/CapitalDAO.class.php');
require_once('../models/dto/Capital.class.php');
require_once('../models/mysql/CapitalMySqlDAO.class.php');
require_once('../models/mysql/ext/CapitalMySqlExtDAO.class.php');

//Kempiala
require_once('../models/dao/KempialaDAO.class.php');
require_once('../models/dto/Kempiala.class.php');
require_once('../models/mysql/KempialaMySqlDAO.class.php');
require_once('../models/mysql/ext/KempialaMySqlExtDAO.class.php');

//Dated Checked
require_once('../models/dao/DatedcheckedDAO.class.php');
require_once('../models/dto/Datedchecked.class.php');
require_once('../models/mysql/DatedcheckedMySqlDAO.class.php');
require_once('../models/mysql/ext/DatedcheckedMySqlExtDAO.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');


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');
//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');
//Asset
require_once('../models/dao/AssetsDAO.class.php');
require_once('../models/dto/Asset.class.php');
require_once('../models/mysql/AssetsMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetsMySqlExtDAO.class.php');

//Partnerwithdrawal
require_once('../models/dao/PartnerwithdrawalDAO.class.php');
require_once('../models/dto/Partnerwithdrawal.class.php');
require_once('../models/mysql/PartnerwithdrawalMySqlDAO.class.php');
require_once('../models/mysql/ext/PartnerwithdrawalMySqlExtDAO.class.php');
//Breadcrumb
require_once("../library/breadcrumb.php");



//get the do the action
$do = $_GET['do'];


/* ======================

  Controller Name :- profitreportCTRL تقرير يومية الخزينة

  OPERTATION in Controller

  1-load save data
  2-display show form
  3-display details form for save
  4-display details for operation in savedaily

  ======================== */


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


//here goes the instances and general variables
//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();
$myStoredetailDAO = new StoredetailMySqlDAO();


//Product
$myProductRecord = new ProductMySqlDAO();
$myProductEx = new ProductMySqlExtDAO();

//Productunit
$myProductunitEx = new ProductunitMySqlExtDAO();
$myProductunitRecord = new ProductunitMySqlDAO();

//Save
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();

//Bankaccount
$myBankaccountEx = new BankaccountMySqlExtDAO();
$myBankaccountDAO = new BankaccountMySqlDAO();
//Kempiala
$myKempialaEx = new KempialaMySqlExtDAO();

//Supplier
$mySupplierRecord = new SupplierMySqlDAO();
$mySupplierEx = new SupplierMySqlExtDAO();

//Capital
$myCapitalRecord = new CapitalMySqlDAO();
$myCapitalEx = new CapitalMySqlExtDAO();

//Client
$myClientRecord = new ClientMySqlDAO();
$myClientEx = new ClientMySqlExtDAO();

//Datedchecked
$DatedcheckedDAO = new DatedcheckedMySqlDAO();
$Datedchecked = new Datedchecked();
$DatedcheckedEX = new DatedcheckedMySqlExtDAO();
//Asset
$myAssetEx = new AssetsMySqlExtDAO();
$myAssetDAO = new AssetsMySqlDAO();

//Partnerwithdrawal
$myPartnerwithdrawal = new Partnerwithdrawal();
$myPartnerwithdrawalRecord = new PartnerwithdrawalMySqlDAO();
$myPartnerwithdrawalEx = new PartnerwithdrawalMySqlExtDAO();

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

//Breadcrumb
$breadcrumbObject = new Breadcrumb();

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$Programsetting = $ProgramsettingDAO->load(1);
$smarty->assign("Programsetting", $Programsetting);


//check and use the condetion that suite this action

if ($do == "show" || empty($do)) {
    include_once("../public/authentication.php");

    //Bankaccount
    $totalaccount = Bankaccount();
    $smarty->assign("totalaccount", $totalaccount);

    //clientdebt
    $totalclientdebt = clientdebt();
    $smarty->assign("totalclientdebt", $totalclientdebt);

    //supplierdebt
    $totalsupplierdebt = supplierdebt();
    $smarty->assign("totalsupplierdebt", $totalsupplierdebt);

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

    //kempilavalue
    $totalkempilavalue = kempilavalue();
    $smarty->assign("totalkempilavalue", $totalkempilavalue);

    $productserialvalue = productserialvalue();
    $smarty->assign("productserialvalue", ceil($productserialvalue));


    //clientcheckvalue
    $totalclientcheck = clientcheckvalue();
    $smarty->assign("totalclientcheck", $totalclientcheck);

    //suppliercheckvalue
    $totalsuppliercheck = suppliercheckvalue();
    $smarty->assign("totalsuppliercheck", $totalsuppliercheck);

    //totalproductinstores
    $totalprice = totalproduct();
    $smarty->assign("totalprice", $totalprice);

    //savevalue
    $savevaluex = savevalue();
    $smarty->assign("savevaluex", $savevaluex);

    //totalmoneyforyou
    $forYou = totalmoneyforyou();
    $smarty->assign("forYou", $forYou);

    //totaltoyou
    $forTox = totaltoyou();
    $smarty->assign("forTox", $forTox);

    //Profits
    $totalProfits = Profits();
    $smarty->assign("totalProfits", $totalProfits);

    //allcapitalvalue
    $totalcapitalmoney = allcapitalvalue();
    $smarty->assign("totalcapitalmoney", $totalcapitalmoney);

    $Fixedassetsdata = Fixedassets();
    $smarty->assign("Fixedassetsdata", $Fixedassetsdata);

    $totalFixedassetsvalue = calculateFixedassets();
    $smarty->assign("totalFixedassetsvalue", $totalFixedassetsvalue);

    $totalcollection = collection();
    $smarty->assign("totalcollection", $totalcollection);

    $totalCurrentassets = Currentassets();
    $smarty->assign("totalCurrentassets", $totalCurrentassets);

    $totalCurrentliabilities = Currentliabilities();
    $smarty->assign("totalCurrentliabilities", $totalCurrentliabilities);

    $totalallmoney = allmoney();
    $smarty->assign("totalallmoney", $totalallmoney);

    $totalpartnercheck = partnervalue();
    $smarty->assign("totalpartnercheck", $totalpartnercheck);
    $totalPropertyrights = Propertyrights();
    $smarty->assign("totalPropertyrights", $totalPropertyrights);
    //here the smarty templates
    $smarty->display("FinancialPositionview/show.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
  ================================ */

function productserialvalue() {
    $rResult = round(R::getCell("SELECT sum((buybilldetailprice * don) + ((buybilldetailprice * don * payedtax) / 100)) as totals
FROM productserial
    left join buybilldetail on buybilldetail.buybilldetailid = productserial.buybilldetailid
    left join buybill on buybilldetail.buybillid = buybill.buybillid
    WHERE productserial.don > 0 and productserial.del = 0"), 2);
    return $rResult;
}

//Bankaccount
function Bankaccount() {
    global $myBankaccountEx;

    $allbankdata = $myBankaccountEx->queryAllforbankaccount();
    $totalaccount;
    foreach ($allbankdata as $bankdata) {
        $totalaccount += $bankdata->accountbeginingbalance;
    }
    return $totalaccount;
}

//clientdebt
function clientdebt() {
    global $myClientRecord;

    $allclientdata = $myClientRecord->queryByCondition(0);
    $totalclientdebt;
    foreach ($allclientdata as $clientdata) {
        $totalclientdebt += $clientdata->clientdebt;
    }
    return $totalclientdebt;
}

//supplierdebt
function supplierdebt() {
    global $mySupplierRecord;

    $allsupplierdata = $mySupplierRecord->queryByCondition(0);
    $totalsupplierdebt;
    foreach ($allsupplierdata as $supplierdata) {
        $totalsupplierdebt += $supplierdata->suppliercurrentDebt;
    }
    return $totalsupplierdebt;
}

//kempilavalue
function kempilavalue() {
    global $myKempialaEx;
    $today = date("Y-m-d");

    //avilable kempila
//    $allavailabledata = $myKempialaEx->queryAlllavilablekempila(0, $today);
    $allavailabledata = $myKempialaEx->queryAlllavilablekempila2(0, $today);

    $totalkempilavalue;
    $totalavailablekempila;
    foreach ($allavailabledata as $avilabledata) {
        $totalavailablekempila += $avilabledata->value;
    }


    //expire kempila
//    $allexpiredata = $myKempialaEx->queryAllexpierkempila(0, $today);
    $allexpiredata = $myKempialaEx->queryAllexpierkempila2(0, $today);

    $totalexpirekempila;
    foreach ($allexpiredata as $expiredata) {
        $totalexpirekempila += $expiredata->value;
    }

    //$totalkempilavalue = $totalavailablekempila + $totalexpirekempila;
    $totalkempilavalue = $totalavailablekempila;

    return $totalkempilavalue;
}

//clientcheckvalue
function clientcheckvalue() {
    global $DatedcheckedEX;

//    $allcheckforclient = $DatedcheckedEX->queryallcheckforclientfinancial(0);
    $allcheckforclient = $DatedcheckedEX->queryallcheckforclientfinancial2(0);

    $totalclientcheck;
    foreach ($allcheckforclient as $clientcheck) {
        $totalclientcheck += $clientcheck->checkValue;
    }

    return $totalclientcheck;
}

function partnervalue() {
    // global $myPartnerwithdrawalRecord;
    //$allpartner = $myPartnerwithdrawalRecord->queryByConditions(0);
    // $totalpartnercheck = 0;
    // $totalone = 0;
    //$totaltwo = 0;
    // foreach ($allpartner as $partner) {
//        var_dump($partner);
    // if ($partner->partenrwithdrawaltype == 0) {
    //$totalone += $partner->partnerwithdrawalvalue;
//                var_dump($totalone);
    //} else {
    //  $totaltwo += $partner->partnerwithdrawalvalue;
//                var_dump($totaltwo);
    //  }
//        $totalpartnercheck +=$partner->partnerwithdrawalvalue;
    //}
    // $totalpartnercheck += $totalone - $totaltwo;

    $totalpartnercheck = R::getCell("SELECT sum(partnermoney) FROM partner where partner.conditions = 0 ");

    return $totalpartnercheck;
}

//suppliercheckvalue
function suppliercheckvalue() {
    global $DatedcheckedEX;

//    $allcheckforsupplier = $DatedcheckedEX->queryallcheckforclientfinancial(1);
    $allcheckforsupplier = $DatedcheckedEX->queryallcheckforclientfinancial2(1);

    $totalsuppliercheck;
    foreach ($allcheckforsupplier as $suppliercheck) {
        $totalsuppliercheck += $suppliercheck->checkValue;
    }

    return $totalsuppliercheck;
}

//totalproduct
//function totalproduct() {
//    global $myStoredetailDAO;
//    global $myStoredetailEx;
//    global $myProductRecord;
//
//    $allstoredata = $myStoredetailEx->queryallstoredata();
//
//    $totalprice;
//    $lastTotal;
//    $meanTotal;
//
//
//    //print_r('<pre>=================');
//
//    foreach ($allstoredata as $storedata) {
//
//        $quantity = $storedata->productquantity;
//        $buyprice = $storedata->productBuyPrice;
//        $lastbuyprice = $storedata->lastbuyprice;
//        $meanbuyprice = $storedata->meanbuyprice;
//
//        $totalprice +=$quantity * $lastbuyprice;
//
////        $totalprice +=$quantity * $buyprice;
////        $lastTotal += $quantity * $lastbuyprice;
////        $meanTotal += $quantity * $meanbuyprice;
//    }
////    print_r('<br>$totalprice = ' . $totalprice . '');
////    print_r('<br>$lastTotal = ' . $lastTotal . '');
////    print_r('<br>$meanTotal = ' . $meanTotal . '');
////    print_r('<pre>');
//
//
//
//    return $totalprice;
//}
function totalproduct() {
    global $myStoredetailDAO;
    global $myStoredetailEx;
    global $myProductRecord;
    global $ProgramsettingDAO;
    global $smarty;

    $allstoredata = $myStoredetailEx->queryallstoredata();

    $totalprice;
    $lastTotal;
    $meanTotal;


    //print_r('<pre>=================');
    $Programsetting = $ProgramsettingDAO->load(1);

    $mess = "";
    switch ($Programsetting->Inventoryevaluation) {
        case "first":
            $mess .= "  تقييم ب سعر شراء ";
            break;
        case "last":
            $mess .= " تقييم بـ اخر سعر شراء";
            break;
        case "mean":
            $mess .= " تقييم بـ متوسط سعر الشراء";
            break;
        case "last_discount":
            $mess .= " تقييم بـ اخر سعر شراء بعد الخصم";
            break;
        case "mean_discount":
            $mess .= " تقييم بـ متوسط سعر الشراء بعد الخصم";
            break;
        case "generalPrice":
            $mess .= " تقييم بـ المتوسط العام ";
            break;
    }
    $smarty->assign("mess", $mess);


count($allstoredata);
    foreach ($allstoredata as $storedata) {

//        var_dump($storedata);

        switch ($Programsetting->Inventoryevaluation) {
            case "first":
                $buyprice = (float) $storedata->productBuyPrice;
                break;
            case "last":
                $buyprice = (float) $storedata->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $storedata->meanbuyprice;
                break;
            case "last_discount":
                $buyprice = (float) $storedata->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyprice = (float) $storedata->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyprice = (float) $storedata->overAllAveragePrice;
                break;
        }
        if($storedata->hasSizeAndColor == 1)
        {
            
            $scquantity = R::getCell('select sum(quantity) from sizecolorstoredetail where productid = '.$storedata->productid);
        
            $quantity = $scquantity;
        }else{
            $quantity = $storedata->productquantity;
        }
        // $quantity = $storedata->productquantity;
//        $buyprice = $storedata->productBuyPrice;
//        $lastbuyprice = $storedata->lastbuyprice;
//        $meanbuyprice = $storedata->meanbuyprice;

        $totalprice += $quantity * $buyprice;

//        $totalprice +=$quantity * $buyprice;
//        $lastTotal += $quantity * $lastbuyprice;
//        $meanTotal += $quantity * $meanbuyprice;
    }
//    print_r('<br>$totalprice = ' . $totalprice . '');
//    print_r('<br>$lastTotal = ' . $lastTotal . '');
//    print_r('<br>$meanTotal = ' . $meanTotal . '');
//    print_r('<pre>');

    return $totalprice;
}

//savevalue
function savevalue() {
    global $mySaveRecord;
    global $mySaveEx;

    //$allsavedata = $mySaveRecord->queryAll();
    $allsavedata = $mySaveEx->queryAllEX2();
    $savevaluex;
    foreach ($allsavedata as $savedata) {
        //$savevaluex += $savedata->savecurrentvalue;
        $savevaluex += $savedata->savecurrentvalueInMainCurrency;
    }
    return $savevaluex;
}

//totalmoneyforyou

function totalmoneyforyou() {
    $totalFixedassetsvalue = calculateFixedassets();
    $totalCurrentassets = Currentassets();

    $forYou = $totalFixedassetsvalue + $totalCurrentassets;

    return $forYou;
}

//totalmoneytoyou

function Currentliabilities() {
    $totalsuppliercheck = suppliercheckvalue();
    $totalsupplierdebt = supplierdebt();

    $totalCurrentliabilities = $totalsuppliercheck + $totalsupplierdebt;

    return $totalCurrentliabilities;
}

function allmoney() {
    $totalpartnercheck = partnervalue();
    $totalcapitalmoney = allcapitalvalue();

    $totalallmoney = $totalcapitalmoney + $totalpartnercheck;

    return $totalallmoney;
}

//Profits
function Profits() {
    $forYou = totalmoneyforyou();
    $totalCurrentliabilities = Currentliabilities();
    $totalallmoney = allmoney();

    $totalProfits = round(($forYou - $totalCurrentliabilities - $totalallmoney), 2);

    return $totalProfits;
}

//allcapitalvalue
function allcapitalvalue() {
    global $myCapitalRecord;

    $allcapitaldata = $myCapitalRecord->queryAll();
    $totalcapitalmoney;
    foreach ($allcapitaldata as $capitaldata) {
        $totalcapitalmoney += $capitaldata->capitalamount;
    }

    return $totalcapitalmoney;
}

function Propertyrights() {
    $totalallmoney = allmoney();
    $totalProfits = Profits();

    $totalPropertyrights = $totalallmoney + $totalProfits;

    return $totalPropertyrights;
}

//totalmoneytoyou
function totaltoyou() {
    $totalCurrentliabilities = Currentliabilities();
    $totalPropertyrights = Propertyrights();

    $forTox = $totalCurrentliabilities + $totalPropertyrights;
    return $forTox;
}

//fixedassets
function Fixedassets() {
    global $myAssetEx;

    $Fixedassetsdata = $myAssetEx->queryallassetcategory();


    $totalassetvalue;
    foreach ($Fixedassetsdata as $assetdata) {
        $totalassetvalue += $assetdata->assetsValue;
    }

    return $Fixedassetsdata;
}

//calculateFixedassets
function calculateFixedassets() {
    global $myAssetEx;

    $Fixedassetsdata = $myAssetEx->queryallassetcategory();


    $totalFixedassetsvalue;
    foreach ($Fixedassetsdata as $assetdata) {
        $totalFixedassetsvalue += $assetdata->sumasset;
    }

    return $totalFixedassetsvalue;
}

function collection() {
    //kempilavalue
    $totalkempilavalue = kempilavalue();

    //clientcheckvalue
    $totalclientcheck = clientcheckvalue();

    $totalcollection = $totalkempilavalue + $totalclientcheck;

    return $totalcollection;
}

//Currentassets
function Currentassets() {
    $totalaccount = Bankaccount();
    $totalclientdebt = clientdebt();
    $totalprice = totalproduct();
    $savevaluex = savevalue();
    $totalcollection = collection();
    $productserialvalue = productserialvalue();
    // if ($ProgramsettingDAO->inventoryvaluation == 0) {
    //     $totalprice = $productserialvalue;
    // }




    $totalCurrentassets = $totalaccount + $totalclientdebt + $totalcollection + $totalprice + $savevaluex;

    return $totalCurrentassets;
}

?>