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

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

//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.class.php');

//Returnsellbill
require_once('../models/dao/ReturnsellbillDAO.class.php');
require_once('../models/dto/Returnsellbill.class.php');
require_once('../models/mysql/ReturnsellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbillMySqlExtDAO.class.php');


//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');

//Sellbilldetail
require_once('../models/dao/SellbilldetailDAO.class.php');
require_once('../models/dto/Sellbilldetail.class.php');
require_once('../models/mysql/SellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbilldetailMySqlExtDAO.class.php');


//Returnsellbilldetail
require_once('../models/dao/ReturnsellbilldetailDAO.class.php');
require_once('../models/dto/Returnsellbilldetail.class.php');
require_once('../models/mysql/ReturnsellbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnsellbilldetailMySqlExtDAO.class.php');

//Sellandruternbilldetail
require_once('../models/dao/SellandruternbilldetailDAO.class.php');
require_once('../models/dto/Sellandruternbilldetail.class.php');
require_once('../models/mysql/SellandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/SellandruternbilldetailMySqlExtDAO.class.php');

//Expenses
require_once('../models/dao/ExpensesDAO.class.php');
require_once('../models/dto/Expense.class.php');
require_once('../models/mysql/ExpensesMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensesMySqlExtDAO.class.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');


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

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



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

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

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

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

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

//Breadcrumb
require_once("../library/breadcrumb.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 :- 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");

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

//here goes the instances and general variables
//Sellbill
$mySellbillEx = new SellbillMySqlExtDAO();

//Returnsellbill
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();

//Sellbillandrutern
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();

//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();

//Expenses
$myExpensesEx = new ExpensesMySqlExtDAO();

//Storedetail
$myStoredetailEx = new StoredetailMySqlExtDAO();


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

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

//Asset
$myAssetEx = new AssetsMySqlExtDAO();

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

//Bankaccount
$myBankaccountEx = new BankaccountMySqlExtDAO();

//Kempiala
$myKempialaEx = new KempialaMySqlExtDAO();

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

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

//Partnerwithdrawal
$myPartnerwithdrawalEx = new PartnerwithdrawalMySqlExtDAO();

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

//Breadcrumb
$breadcrumbObject = new Breadcrumb();



//check and use the condetion that suite this action

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

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

    $Programsettingdata = $ProgramsettingDAO->load(1);

    $mess = "";
    switch ($Programsettingdata->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;
        case "tax":
            $mess .= 'اخر سعر شراء شامل القيمة المضافة ';
            break;
        case "mean_tax":
            $mess .= 'متوسط سعر الشراء شامل القيمة المضافة';
            break;
    }
    $smarty->assign("mess", $mess);

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];

    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "") {
        $forYou = 0;
        $forTo = 0;
        $message = "تقرير حسابات مجمعة  من  :" . $startDate . " الى تاريخ :" . $endDate;
        $smarty->assign('message', $message);

        $totalSellPrice = getTotalSellbillByDate($startDate, $endDate) + getTotalAditionalSellbillByDate($startDate, $endDate);

        $totalReturnSellPrice = getTotalReturnSellbillByDate($startDate, $endDate) + getTotalAditionalReturnSellbillByDate($startDate, $endDate);

        $totalQuantityBuyPrice = getTotalQuantityBuyPriceByDate($startDate, $endDate) + getTotalAditionalQuantityBuyPriceByDate($startDate, $endDate);

        $totalQuantityReturnBuyPrice = getTotalQuantityReturnBuyPriceByDate($startDate, $endDate) + getTotalAditionalQuantityReturnBuyPriceByDate($startDate, $endDate);

        $totalExpenses = getTotalExpensesByDate($startDate, $endDate);


        $totalSellCost = $totalSellPrice - $totalReturnSellPrice;
        $totalBuyCost = $totalQuantityBuyPrice - $totalQuantityReturnBuyPrice;




        $assetValue = getTotalAssetByDate($startDate, $endDate);
        $productPrice = getTotalProductPrice();
        $saveValue = getTotalSaveValue();
        $bankAccountbalance = getBankAccountbalance();
        $notSellingKempialaValue = getNotSellingKempialaValue();
        //clientcheckvalue
        $totalclientcheck = clientcheckvalue($startDate, $endDate);
        $smarty->assign("totalclientcheck", $totalclientcheck);

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


        $supplierDebt = getSupplierDebt();


        $capitalAmount = getCapitalamount();
        $withdrawalValue = getPartnerwithdrawalvalue($startDate, $endDate);
        $profitFinal = ($totalSellCost - $totalBuyCost) - $totalExpenses;
        $clientDebt = getClientDebt();
        $sellingKempialaValue = getSellingKempialaValue();


        $smarty->assign('assetValue', $assetValue);
        $smarty->assign('productPrice', $productPrice);
        $smarty->assign('saveValue', $saveValue);
        $smarty->assign('bankAccountbalance', $bankAccountbalance);
        $smarty->assign('notSellingKempialaValue', $notSellingKempialaValue);
        $smarty->assign('clientDebt', $clientDebt);


        $forYou = $assetValue + $productPrice + $saveValue + $bankAccountbalance + $notSellingKempialaValue + $clientDebt + $totalclientcheck;
        $smarty->assign('forYou', $forYou);

        $smarty->assign('capitalAmount', $capitalAmount);
        $smarty->assign('withdrawalValue', $withdrawalValue);
        $smarty->assign('profitFinal', $profitFinal);
        $smarty->assign('supplierDebt', $supplierDebt);
        $smarty->assign('sellingKempialaValue', $sellingKempialaValue);

        // $forTo = $capitalAmount + $withdrawalValue + $profitFinal + $supplierDebt + $totalsuppliercheck;
        $forTo = $capitalAmount + $supplierDebt + $totalsuppliercheck;
        $smarty->assign('forTo', $forTo);
    } else {
        $smarty->assign('totalSellPrice', 0);
        $smarty->assign('totalReturnSellPrice', 0);
        $smarty->assign('totalQuantityBuyPrice', 0);
        $smarty->assign('totalQuantityReturnBuyPrice', 0);
        $smarty->assign('totalExpenses', 0);
        $smarty->assign('totalSellCost', 0);
        $smarty->assign('totalBuyCost', 0);
        $smarty->assign('profitFinal', 0);
    }

    //here the smarty templates
    $smarty->display("balancereportview/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
  ================================ */

// get sellbillaftertotalbill from Sellbill tbl
function getTotalSellbillByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $mySellbillEx;

    $totalSellbill = 0;

    $totalSellbill = $mySellbillEx->queryWithDateAndConditionsExt($startDate, $endDate);

    return $totalSellbill;
}

// get sellbillaftertotalbill from  sellbillandrutern tbl
function getTotalAditionalSellbillByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $mySellbillandruternEx;

    $totalSellbill = 0;

    $totalSellbill = $mySellbillandruternEx->queryWithDateAndConditionsForSellBillOnly($startDate, $endDate);

    return $totalSellbill;
}

// get returnsellbillaftertotalbill from  returnsellbill tbl
function getTotalReturnSellbillByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $myReturnsellbillEx;

    $totalReturnSellbill = 0;

    $totalReturnSellbill = $myReturnsellbillEx->queryWithDateAndConditionsExt($startDate, $endDate);

    return $totalReturnSellbill;
}

// get returnsellbillaftertotalbill from  sellbillandreturn tbl
function getTotalAditionalReturnSellbillByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $mySellbillandruternEx;

    $totalReturnSellbill = 0;

    $totalReturnSellbill = $mySellbillandruternEx->queryWithDateAndConditionsForReturnSellBillOnly($startDate, $endDate);

    return $totalReturnSellbill;
}

// show the form by date
function getTotalQuantityBuyPriceByDate($startDate, $endDate)
{
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $myProductunitEx;

    $totalQuantityBuyPrice = 0;

    $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditionsExt($startDate, $endDate);


    foreach ($sellbilldetailData as $sellbilldetail) {
        //$productId=$sellbilldetail->sellbilldetailproductid;
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $productunitId = $sellbilldetail->productunitid;
        $buyPrice = $sellbilldetail->buyprice;
        $buyDiscount = $sellbilldetail->buydiscount;
        $buyDiscountType = $sellbilldetail->buydiscounttype;


        //$productData=getProoductPriceByonlyProductId($productId);

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }

        $productBuyPrice = $productBuyPrice * $productnumber;

        $totalQuantityBuyPrice = $totalQuantityBuyPrice + ($quantity * $productBuyPrice);
    }

    return $totalQuantityBuyPrice;
}

// show the form by date
function getTotalAditionalQuantityBuyPriceByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $mySellandruternbilldetailEx;
    global $myProductunitRecord;
    global $myProductunitEx;

    $totalQuantityBuyPrice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeExt($startDate, $endDate, 0);


    foreach ($sellandreturndetailData as $sellandreturndetail) {
        //$productId=$sellandreturndetail->sellbilldetailproductid;
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;


        //$productData=getProoductPriceByonlyProductId($productId);

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;



        $totalQuantityBuyPrice = $totalQuantityBuyPrice + ($quantity * $productBuyPrice);
    }



    return $totalQuantityBuyPrice;
}

function getTotalQuantityReturnBuyPriceByDate($startDate, $endDate)
{
    global $myReturnsellbilldetailEx;
    global $myProductunitRecord;
    global $myProductunitEx;

    $totalQuantityReturnBuyPrice = 0;

    $returnsellbilldetailData = $myReturnsellbilldetailEx->queryWithDateAndConditionsExt($startDate, $endDate);


    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        //$productId=$returnsellbilldetail->returnsellbilldetailproductid;
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $productunitId = $returnsellbilldetail->productunitid;
        $buyPrice = $returnsellbilldetail->buyprice;
        $buyDiscount = $returnsellbilldetail->buydiscount;
        $buyDiscountType = $returnsellbilldetail->buydiscounttype;


        //$productData=getProoductPriceByonlyProductId($productId);

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;



        $totalQuantityReturnBuyPrice = $totalQuantityReturnBuyPrice + ($quantity * $productBuyPrice);
    }




    return $totalQuantityReturnBuyPrice;
}

function getTotalAditionalQuantityReturnBuyPriceByDate($startDate, $endDate)
{
    global $mySellandruternbilldetailEx;
    global $myProductunitRecord;
    global $myProductunitEx;

    $totalQuantityReturnBuyPrice = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeExt($startDate, $endDate, 1);


    foreach ($sellandreturndetailData as $sellandreturndetail) {
        //$productId=$sellandreturndetail->sellbilldetailproductid;
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $productunitId = $sellandreturndetail->productunitid;
        $buyPrice = $sellandreturndetail->buyprice;
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;


        //$productData=getProoductPriceByonlyProductId($productId);

        $productnumber = $myProductunitEx->getProductNumber($productunitId);

        if ($buyDiscount > 0 && $buyDiscountType == 0) {
            $productBuyPrice = $buyPrice - $buyDiscount;
        } elseif ($buyDiscount > 0 && $buyDiscountType == 1) {
            $productBuyPrice = $buyPrice - (($buyDiscount / 100) * $buyPrice);
        }


        $productBuyPrice = $productBuyPrice * $productnumber;



        $totalQuantityReturnBuyPrice = $totalQuantityReturnBuyPrice + ($quantity * $productBuyPrice);
    }



    return $totalQuantityReturnBuyPrice;
}

// get ProoductPrice from Prooduct tbl
function getProoductPriceByonlyProductId($productId)
{
    global $myProductEx;

    // get rawmaterialprice by rawmaterialid
    $productData = $myProductEx->loadByProductId($productId);

    return $productData;
}

function getTotalExpensesByDate($startDate, $endDate)
{
    global $myExpensesEx;

    $expensesValue = 0;

    $expensesValue = $myExpensesEx->queryWithDateAndConditionsExt($startDate, $endDate);

    return $expensesValue;
}

function getTotalAssetByDate($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $myAssetEx;
    global $smarty;

    $assetsValue = 0;

    $assetsData = $myAssetEx->queryWithDateGroupByAssetscatId($startDate, $endDate);
    $smarty->assign("assetsData", $assetsData);

    foreach ($assetsData as $assets) {
        $assetsValue = $assetsValue + $assets->sumasset;
    }

    return $assetsValue;
}

function getTotalProductPrice()
{
    global $myStoredetailEx;
    global $ProgramsettingDAO;
    global $myProductRecord;
    global $smarty;

    $Programsettingdata = $ProgramsettingDAO->load(1);
    //select all data from storedetail
    $storedetailData = $myStoredetailEx->queryWithOrderExt();


    $productPrice = 0;
    foreach ($storedetailData as $storedetail) {
        //        $myproduct = $myProductRecord->load($storedetail->productid);
        //var_dump($storedetail);
        switch ($Programsettingdata->Inventoryevaluation) {
            case "first":
                $pro_price = (float) $storedetail->productBuyPrice;
                break;
            case "last":
                $pro_price = (float) $storedetail->lastbuyprice;
                break;
            case "mean":
                $pro_price = (float) $storedetail->meanbuyprice;
                break;
            case "last_discount":
                $pro_price = (float) $storedetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $pro_price = (float) $storedetail->meanbuyprice_withDiscount;
                break;
            case "tax":
                $pro_price = (float) $storedetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $pro_price = (float) $storedetail->meanbuyprice_withTax;
                break;
            default:
                $pro_price = (float) $storedetail->overAllAveragePrice;
                break;
        }


        $productBuyPrice = $pro_price;
        if($storedetail->hasSizeAndColor == 1)
        {
            $scquantity = R::getCell('select sum(quantity) from sizecolorstoredetail where productid = '.$storedetail->productId);
            $productQuantity = $scquantity;
        }else{
        $productQuantity = $storedetail->productquantity;
        }

        //        $productBuyPrice = $storedetail->productBuyPrice;
        //$productId=$storedetail->productid;

        $SumProductPrice = $productBuyPrice * $productQuantity;

        $productPrice += $SumProductPrice;
    }

    return $productPrice;
}

function getTotalSaveValue()
{
    global $mySaveEx;
    $saveValue = 0;

    $saveValue = $mySaveEx->getSaveTotalCurrentValue();

    return $saveValue;
}

function getBankAccountbalance()
{
    global $myBankaccountEx;
    $bankAccountBalance = 0;

    //select all data bankaccount save
    $bankAccountBalance = $myBankaccountEx->queryWithConditonsForBalanceReport();

    return $bankAccountBalance;
}

function getNotSellingKempialaValue()
{
    global $myKempialaEx;
    $kempialaValue = 0;

    $kempialaValue = $myKempialaEx->queryWithSellingAndConditionsExt(0);

    return $kempialaValue;
}

//clientcheckvalue
function clientcheckvalue($startDate, $endDate)
{
    global $DatedcheckedEX;

    //    $allcheckforclient = $DatedcheckedEX->queryallcheckforclientfinancial(0);
    $allcheckforclient = $DatedcheckedEX->queryallcheckforclientfinancial3(0, $startDate, $endDate);

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

    return $totalclientcheck;
}

//suppliercheckvalue
function suppliercheckvalue($startDate, $endDate)
{
    global $DatedcheckedEX;

    //    $allcheckforsupplier = $DatedcheckedEX->queryallcheckforclientfinancial(1);
    $allcheckforsupplier = $DatedcheckedEX->queryallcheckforclientfinancial3(1, $startDate, $endDate);

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

    return $totalsuppliercheck;
}

function getSupplierDebt()
{
    global $mySupplierEx;
    $supplierDebt = 0;

    $supplierDebt = $mySupplierEx->getSuppliersTotalDept();

    return $supplierDebt;
}

function getCapitalamount()
{
    global $myCapitalEx;

    //select all data capital
    $capitalAmount = $myCapitalEx->getCapitalAmount();

    return $capitalAmount;
}

function getPartnerwithdrawalvalue($startDate, $endDate)
{
    //to use the variable out side the funcion
    global $myPartnerwithdrawalEx;
    $withdrawalValue = 0;
    //select all data capital
    // $withdrawalValue = $myPartnerwithdrawalEx->queryWithDateAndConditionsExt($startDate, $endDate);
    $withdrawalValue = $totalpartnercheck = R::getCell("SELECT sum(partnermoney) FROM partner where partner.conditions = 0 ");

    return $withdrawalValue;
}

function getClientDebt()
{
    global $myClientEx;
    $clientDebt = 0;

    $clientDebt = $myClientEx->getClientsTotalDebt();

    return $clientDebt;
}

function getSellingKempialaValue()
{
    //to use the variable out side the funcion
    global $myKempialaEx;
    $kempialaValue = 0;

    $kempialaValue = $myKempialaEx->queryWithSellingAndConditionsExt(1);

    return $kempialaValue;
}
