HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/profitproductcatController.php
<?php

/* * *************** EDITED at 1/12/2013 --- Yasmeen ************* */

//the global file operation
include("../public/impOpreation.php");
include("reportfunctions.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");
//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');


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

//Buybill
require_once('../models/dao/BuybillDAO.class.php');
require_once('../models/dto/Buybill.class.php');
require_once('../models/mysql/BuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybillMySqlExtDAO.class.php');

//Buybilldetail
require_once('../models/dao/BuybilldetailDAO.class.php');
require_once('../models/dto/Buybilldetail.class.php');
require_once('../models/mysql/BuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybilldetailMySqlExtDAO.class.php');

//Buyandruternbill
require_once('../models/dao/BuyandruternbillDAO.class.php');
require_once('../models/dto/Buyandruternbill.class.php');
require_once('../models/mysql/BuyandruternbillMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbillMySqlExtDAO.class.php');

//Buyandruternbilldetail
require_once('../models/dao/BuyandruternbilldetailDAO.class.php');
require_once('../models/dto/Buyandruternbilldetail.class.php');
require_once('../models/mysql/BuyandruternbilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbilldetailMySqlExtDAO.class.php');


//Returnbuybill
require_once('../models/dao/ReturnbuybillDAO.class.php');
require_once('../models/dto/Returnbuybill.class.php');
require_once('../models/mysql/ReturnbuybillMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybillMySqlExtDAO.class.php');

//Returnbuybilldetail
require_once('../models/dao/ReturnbuybilldetailDAO.class.php');
require_once('../models/dto/Returnbuybilldetail.class.php');
require_once('../models/mysql/ReturnbuybilldetailMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybilldetailMySqlExtDAO.class.php');


//Productcat
require_once('../models/dao/ProductcatDAO.class.php');
require_once('../models/dto/Productcat.class.php');
require_once('../models/mysql/ProductcatMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductcatMySqlExtDAO.class.php');

//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.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');

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


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

//Returnsellbill
$myReturnsellbillEx = new ReturnsellbillMySqlExtDAO();
$myReturnsellbillRecord = new ReturnsellbillMySqlDAO();

//Sellbillandrutern
$mySellbillandruternRecord = new SellbillandruternMySqlDAO();
$mySellbillandruternEx = new SellbillandruternMySqlExtDAO();

//Sellbilldetail
$mySellbilldetailEx = new SellbilldetailMySqlExtDAO();

//Returnsellbilldetail
$myReturnsellbilldetailEx = new ReturnsellbilldetailMySqlExtDAO();

//Sellandruternbilldetail
$mySellandruternbilldetailEx = new SellandruternbilldetailMySqlExtDAO();



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

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


//Buybill
$myBuybillRecord = new BuybillMySqlDAO();

//Buybilldetail
$myBuybilldetailEx = new BuybilldetailMySqlExtDAO();

//Buyandruternbill
$myBuyandruternbillRecord = new BuyandruternbillMySqlDAO();

//Buyandruternbilldetail
$myBuyandruternbilldetailEx = new BuyandruternbilldetailMySqlExtDAO();

//Returnbuybill
$myReturnbuybillRecord = new ReturnbuybillMySqlDAO();

//Returnbuybilldetail
$myReturnbuybilldetailEx = new ReturnbuybilldetailMySqlExtDAO();

//Productcat
$myProductcatRecord = new ProductcatMySqlDAO();
$myProductcatEx = new ProductcatMySqlExtDAO();

//billsproducts
$billsProducts = new Billsproduct();
$billsProductsDAO = new BillsproductsMySqlDAO();
$billsProductsEX = new BillsproductsMySqlExtDAO();

//billsproducts
$billsReturnProducts = new Billsreturnproduct();
$billsReturnProductsDAO = new BillsreturnproductsMySqlDAO();
$billsReturnProductsEX = new BillsreturnproductsMySqlExtDAO();


$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//check and use the condetion that suite this action
$lastLevelCatIDS = array();
$catsIDS = "";
if ($do == "show" || empty($do)) {

    //here the permssion check
    include_once("../public/authentication.php");
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $Programsetting = $ProgramsettingDAO->load(1);
    $smarty->assign('programsettingsdata', $Programsetting);

    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    $level = filter_input(INPUT_POST, 'level');
    $productCatId = filter_input(INPUT_POST, 'productCatId' . $level);
    if (empty($productCatId) || $productCatId == -1) {
        $productCatId = filter_input(INPUT_POST, 'productCatId' . ($level - 1));
    }

    $catsIDS = '' . $productCatId;
    if ($productCatId != '') {
        getAllSubCat($productCatId, 1); //mode = 1 get all sub cats
    }


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

    $dis = filter_input(INPUT_GET, 'dis'); // if dis = T means discount = true
    $smarty->assign('dis', $dis);
    if (isset($startDate) && isset($endDate) && $startDate != "" && $endDate != "" && isset($productCatId) && $productCatId != "-1") {
        $productCatSearchData = loadProductCatById($productCatId);
        $smarty->assign('productCatSearchData', $productCatSearchData);
        ########
        $Programsetting = $ProgramsettingDAO->load(1);
        $smarty->assign('programsettingsdata', $Programsetting);
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $endDate = date('Y-m-d H:i:s', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d H:i:s', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
        } else {
            $endDate = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
        }
        #############
        getProductInBillByDateAndCatId($startDate, $endDate, $catsIDS);

        $message = "تقرير ارباح صنف <h4>" . $productCatSearchData->productCatName . "</h4> التاريخ : من " . $startDate . " إلى " . $endDate;
        $smarty->assign("message", $message);
    }

    //$smarty->assign("salesreport", 1);
    //here the smarty templates
    if ($dis == 'T') {
        $smarty->display("profitproductcatview/showdiscount.html");
    } else {
        $smarty->display("profitproductcatview/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 getProductInBillByDateAndCatId($startDate, $endDate, $productcatId) {
    global $mySellbilldetailEx;
    global $myProductRecord;
    global $myProductEx;

    global $smarty;

    $productIdData = $mySellbilldetailEx->queryFromAllTableWithDateAndConditionsAndCatId($startDate, $endDate, $productcatId);

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

    $totalSellPrice = 0;
    $totalQuantityUnit = 0;
    $totalDiscountPrice = 0;

    $totalReturnSellPrice = 0;
    $totalreturnQuantityUnit = 0;

    $totalBuybillPrice = 0;
    $totalBuyQuantityUnit = 0;

    $totalReturnBuybillPrice = 0;
    $totalReturnBuyQuantityUnit = 0;

    $finalProductcatProfit = 0;

    $h = 1;
    foreach ($productIdData as $productIdDa) {
        $productId = $productIdDa->productid;


        $productData = $myProductRecord->load($productId);
        $overAllAveragePrice = $productData->overAllAveragePrice;
        $smarty->assign("productData", $productData);
        $productData = $myProductEx->getName($productId);
        $smarty->assign("productData" . $h . '', $productData);

        //sellbill
        $sellPriceData = getTotalSellPriceByDateAndProductId($startDate, $endDate, $productId, $overAllAveragePrice);
        $sellPricePulsData = getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate, $productId, $overAllAveragePrice);
        //sellbillOptic
        $sellOpticData = getTotalSellOpticByDateAndProductId($startDate, $endDate, $productId);

        $sellPrice = $sellPriceData[0] + $sellPricePulsData[0] + $sellOpticData[0];
        $quantityUnit = $sellPriceData[1] + $sellPricePulsData[1] + $sellOpticData[1];
        $discountPrice = $sellPriceData[3] + $sellPricePulsData[3] + $sellOpticData[3];

        $totalSellPrice = $totalSellPrice + $sellPrice;
        $totalQuantityUnit = $totalQuantityUnit + $quantityUnit;
        $totalDiscountPrice = $totalDiscountPrice + $discountPrice;
        //return sellbill
        $returnSellPriceData = getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId, $overAllAveragePrice);

        $returnSellPricePulsData = getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId, $overAllAveragePrice);
        //return sellbill Optic
        $returnSellOpticData = getTotalReturnOpticByDateAndProductId($startDate, $endDate, $productId);


        /* 	print_r("المردودات".$returnSellPriceData[0]);
          print_r("<br>سعر الاصلى المردودات".$returnSellPriceData[2]);
          //finalbuypric
          print_r("<br>");
          print_r("المبيعات فى المردودات".$returnSellPricePulsData[0]);

          print_r("<br>سعر الاصلى المردودات".$returnSellPricePulsData[2]);
          print_r("<br>");
          //print_r($sellPricePulsData);
          print_r("<br>");
         */
        $returnSellPrice = $returnSellPriceData[0] + $returnSellPricePulsData[0] + $returnSellOpticData[0];
        $returnQuantityUnit = $returnSellPriceData[1] + $returnSellPricePulsData[1] + $returnSellOpticData[1];

        $totalReturnSellPrice = $totalReturnSellPrice + $returnSellPrice;
        $totalreturnQuantityUnit = $totalreturnQuantityUnit + $returnQuantityUnit;

        //buybill
        $buybillPriceData = getTotalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $buybillPricePulsData = getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $buybillPrice = $buybillPriceData[0] + $buybillPricePulsData[0];
        $buyQuantityUnit = $buybillPriceData[1] + $buybillPricePulsData[1];


        $totalBuybillPrice = $totalBuybillPrice + $buybillPrice;
        $totalBuyQuantityUnit = $totalBuyQuantityUnit + $buyQuantityUnit;

        //return buybill
        $returnBuybillPriceData = getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $returnBuybillPricePulsData = getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

        $returnBuybillPrice = $returnBuybillPriceData[0] + $returnBuybillPricePulsData[0];
        $returnBuyQuantityUnit = $returnBuybillPriceData[1] + $returnBuybillPricePulsData[1];

        $totalReturnBuybillPrice = $totalReturnBuybillPrice + $returnBuybillPrice;
        $totalReturnBuyQuantityUnit = $totalReturnBuyQuantityUnit + $returnBuyQuantityUnit;


        $yy = $sellPrice - $returnSellPrice;


        //print_r('<br>quantityUnit'.$quantityUnit);
        //print_r('<br>returnQuantityUnit'.$returnQuantityUnit);
        $quantityForOneProduct = $quantityUnit - $returnQuantityUnit;

        //سعر الوحده
        @$xx = ($buybillPrice) / $quantityUnit;
        $discountForOneProduct = ($quantityForOneProduct * $xx);
        $profitForOneProduct = round((($yy) - ($quantityForOneProduct * $xx)), 2);
        /* $smarty->assign("profitForOneProduct", $profitForOneProduct);
          $smarty->assign("quantityForOneProduct", $quantityForOneProduct);
         */


        $smarty->assign("discountPrice" . $h . '', $discountPrice); // الخصومات لمنتج واحد
        $smarty->assign("profitForOneProduct" . $h . '', $profitForOneProduct);
        $smarty->assign("quantityForOneProduct" . $h . '', $quantityForOneProduct);

        $finalProductcatProfit += $profitForOneProduct;
        $h++;
    }

    /*

      $productData=$myProductEx->getName($productId);
      $smarty->assign("productData".$h.'', $productData);


      //sellbill
      $sellPriceData=getTotalSellPriceByDateAndProductId($startDate, $endDate, $productId);


      //print_r("المبيعات".$sellPriceData[0]);
      //print_r("<br>سعر الاصلى للمبيعات".$sellPriceData[2]);
      //finalbuypric
      //	print_r("<br>");
      $sellPricePulsData=getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate,$productId);

      //print_r("المبيعات فى الردودات".$sellPricePulsData[0]);

      //print_r("<br>سعر الاصلى للمبيعات".$sellPricePulsData[2]);
      //print_r("<br>");
      //print_r($sellPricePulsData);
      //print_r("<br>");
      $sellPrice = $sellPriceData[0] + $sellPricePulsData[0];
      $quantityUnit = $sellPriceData[1] + $sellPricePulsData[1];

      $totalSellPrice += $sellPrice;
      $totalQuantityUnit += $quantityUnit;


      //return sellbill
      $returnSellPriceData=getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);

      //print_r(" المردودات ".$returnSellPriceData[0]);

      //print_r("<br>سعر الاصلى المردودات".$returnSellPriceData[2]);
      //print_r("<br>");

      $returnSellPricePulsData=getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $productId);



      //	print_r(" فى المبيعات المردودات ".$returnSellPricePulsData[0]);

      //print_r("<br>سعر الاصلى المردودات".$returnSellPricePulsData[2]);
      //print_r("<br>");
      $returnSellPrice=$returnSellPriceData[0] + $returnSellPricePulsData[0];

      $returnQuantityUnit=$returnSellPriceData[1] + $returnSellPricePulsData[1];


      $totalReturnSellPrice += $returnSellPrice;
      $totalreturnQuantityUnit += $returnQuantityUnit;


      //buybill
      $buybillPriceData=getTotalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $buybillPricePulsData=getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $buybillPrice = $buybillPriceData[0] + $buybillPricePulsData[0];
      $buyQuantityUnit = $buybillPriceData[1] + $buybillPricePulsData[1];


      $totalBuybillPrice += $buybillPrice;
      $totalBuyQuantityUnit += $buyQuantityUnit;


      //return buybill
      $returnBuybillPriceData=getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $productId);

      $returnBuybillPricePulsData=getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $productId);


      $returnBuybillPrice = $returnBuybillPriceData[0] + $returnBuybillPricePulsData[0];
      $returnBuyQuantityUnit = $returnBuybillPriceData[1] + $returnBuybillPricePulsData[1];


      $totalReturnBuybillPrice += $returnBuybillPrice;
      $totalReturnBuyQuantityUnit += $returnBuyQuantityUnit;

      $quantityForOneProduct = $quantityUnit -  $returnQuantityUnit;


      //	$profitForOneProduct= ($sellPrice + $returnBuybillPrice) - ($buybillPrice + $returnSellPrice);

      $profitForOneProduct=(($sellPriceData[0] +$sellPricePulsData[0]) -($sellPriceData[2]+$sellPricePulsData[2]))-(($returnSellPriceData[0]+$returnSellPricePulsData[0])-($returnSellPriceData[2]+$returnSellPricePulsData[2]));




      $smarty->assign("profitForOneProduct".$h.'', $profitForOneProduct);
      $smarty->assign("quantityForOneProduct".$h.'', $quantityForOneProduct);

      $finalProductcatProfit+=$profitForOneProduct;
      $h++;

     */

    $finalQuantityUnit = $totalQuantityUnit - $totalreturnQuantityUnit;
    $smarty->assign("totalDiscountPrice", $totalDiscountPrice); // مجموع كل الخصومات
    $smarty->assign("totalreturnQuantityUnit", $totalreturnQuantityUnit);
    $smarty->assign("finalProductcatProfit", $finalProductcatProfit);
    $smarty->assign("finalQuantityUnit", $finalQuantityUnit);
}

// show the form by date
function getTotalSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $mySellbillRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;


    $sellPrice = 0;
    $quantityUnit = 0;
    $finalbuypric = 0;
    $sellbilldetailData = $mySellbilldetailEx->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);
    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellbilldetailData as $sellbilldetail) {
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $price = $sellbilldetail->sellbilldetailprice;
        $totalPrice = $sellbilldetail->sellbilldetailtotalprice;
        $discountValue = $sellbilldetail->discountvalue;
        $productunitId = $sellbilldetail->productunitid;
        $sellbillTotalBill = $sellbilldetail->sellbilltotalbill;
        $sellbillDiscount = $sellbilldetail->sellbilldiscount;
        $sellbillDiscountType = $sellbilldetail->sellbilldiscounttype;
        //$buyprice = $sellbilldetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $sellbilldetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $sellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $sellbilldetail->meanbuyprice;
                break;
            case "last_discount":
                $buyprice = (float) $sellbilldetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyprice = (float) $sellbilldetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyprice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellbilldetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellbilldetail->meanbuyprice_withTax;
                break;
        }

        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $totalPriceBeforeDiscount = $price * $quantity; //product price before discount

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

        $quantityUnit += ($quantity * $productnumber); //الكمية * عدد القطع في وحدة القياس
        //first Step قيمة خصم المنتج على الوحدة
        //$firstStep = $discountValue;
        //second Stepقيمة المنتج من الخصم على المنتج
        //$secondStep = $totalPrice / $sellbillTotalBill;

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

        //fourth Step  قيمة خصم الفاتورة للوحدة
        //$fourthStep = $thirdStep;
        //$sellPriceForOneProduct= $totalPriceBeforeDiscount - ($firstStep + $fourthStep);
        $allbuyprice = $buyprice * $quantity;
        $finalbuypric += $allbuyprice;
        $totalPriceDiscount = $discountValue + $thirdStep;
        $sellPriceForOneProduct = $totalPriceBeforeDiscount - $totalPriceDiscount;
        $sellPrice += $sellPriceForOneProduct;
        $discountPrice += $totalPriceDiscount;
    }



    return array($sellPrice, $quantityUnit, $finalbuypric, $discountPrice);
}

// show the form by date
// show the form by date
function getTotalAditionalSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;
    $fianlbuyprice = 0;
    $sellbillTotalBillnor = 0;

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

    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellandreturndetailData as $sellandreturndetail) {

        //print_r($sellandreturndetail);
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        $sellbillTotalBill = $sellandreturndetail->sellbillprice;
        $sellbillDiscount = $sellandreturndetail->sellbilldiscount; //محتاجة تتغير
        $sellbillDiscountType = $sellandreturndetail->sellbilldiscounttype;
        //$buyprice = $sellandreturndetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $sellandreturndetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $sellandreturndetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $sellandreturndetail->meanbuyprice;
                break;
            case "last_discount":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyprice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withTax;
                break;
        }
        $returnsellbillprice = $sellandreturndetail->returnsellbillprice;



        if ($sellbillDiscountType == 2) {
            //$sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $sellbillTotalBillnor = $sellbillTotalBillnor + $totalPrice;
//
//			//load Product Data
//			$productData=$myProductRecord->load($productId);
//
//			$productSellPrice;
//			//تجزئة
//			if($priceType == 1)
//			{
//				$productSellPrice=$productData->productSellUnitPrice;
//			}
//			//جملة
//			elseif($priceType == 0)
//			{
//				$productSellPrice=$productData->productSellAllPrice;
//			}
//			//نصف جملة
//			elseif($priceType == 2)
//			{
//				$productSellPrice=$productData->productSellHalfPrice;
//			}
//

        $totalPriceBeforeDiscount = $price * $quantity;

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

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;
        $allbuyprice = $quantity * $buyprice;
        $fianlbuyprice += $allbuyprice;
        $totalPriceDiscount = $discountValue + $thirdStep;
        $sellPriceForOneProduct = $totalPriceBeforeDiscount - $totalPriceDiscount;
        $sellPrice += $sellPriceForOneProduct;
    }
    $sellbillTotalBillnor = $sellbillTotalBillnor - $returnsellbillprice;

    /* print_r('<br>sellbillTotalBillnor'.$sellbillTotalBillnor);
      print_r('<br>returnsellbillprice'.$returnsellbillprice);
      print_r('<br>sellbillDiscount'.$sellbillDiscount); */
    if ($sellbillDiscountType == 2) {
        $sellbillDiscountxx = ($sellbillDiscount / 100) * $sellbillTotalBillnor;
        //print_r('<br>sellbillDiscountxx'.$sellbillDiscountxx);
    } else {
        $sellbillDiscountxx = ($sellbillDiscount);
    }

    $sellPrice = $sellPrice - $sellbillDiscountxx;

    $thirdStep = @($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

    return array($sellPrice, $quantityUnit, $fianlbuyprice, $totalPriceDiscount);
}

function getTotalReturnSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $myReturnsellbilldetailEx;
    global $myReturnsellbillRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductEx;
    global $myProductunitEx;

    $ReturnSellPrice = 0;
    $quantityUnit = 0;
    $finalbuyprice = 0;

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

    $buyPriceType = $_POST["buyPriceType"];
    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $price = $returnsellbilldetail->returnsellbilldetailprice;
        $totalPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;
        $discountValue = $returnsellbilldetail->discountvalue;
        $productunitId = $returnsellbilldetail->productunitid;
        $sellbillTotalBill = $returnsellbilldetail->returnsellbilltotalbill;
        $sellbillDiscount = $returnsellbilldetail->returnsellbilldiscount;
        $sellbillDiscountType = $returnsellbilldetail->returnsellbilldiscounttype;
        //$buyprice = $returnsellbilldetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $returnsellbilldetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $returnsellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $returnsellbilldetail->meanbuyprice;
                break;
            case "last_discount":
                $buyprice = (float) $returnsellbilldetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyprice = (float) $returnsellbilldetail->meanbuyprice_withDiscount;
                break;
            case "tax":
                $buyprice = (float) $returnsellbilldetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $returnsellbilldetail->meanbuyprice_withTax;
                break;

            case "generalPrice":
                $buyprice = (float) $overAllAveragePrice;
                break;
        }

        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $totalPriceBeforeDiscount = $price * $quantity;

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

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;


        $allbuyprice = $quantity * $buyprice;

        //print_r("<br>buyprice".$buyprice);
        //print_r("<br>quantity".$quantity);
        $finalbuyprice += $allbuyprice;
        $ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        $ReturnSellPrice += $ReturnSellPriceForOneProduct;
    }

//print($ReturnSellPrice."ReturnSellPrice<br>");

    return array($ReturnSellPrice, $quantityUnit, $finalbuyprice);
}

// show the form by date
function getTotalAditionalReturnSellPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $ReturnSellPrice = 0;
    $quantityUnit = 0;
    $finalbuyprice = 0;

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


    //print_r($sellandreturndetailData);
    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        //$sellbillTotalBill=$sellbillData->sellbillprice;
        $sellbillDiscount = $sellandreturndetail->sellbilldiscount; //محتاجة تتغير
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountvalue = $sellandreturndetail->discountvalue;
        //$buyprice = $sellandreturndetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $sellandreturndetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $sellandreturndetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $sellandreturndetail->meanbuyprice;
                break;
            case "last_discount":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyprice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withTax;
                break;
        }
        //print_r("<br>sellbillDiscount".$sellbillDiscount."<br>");
        //print_r("<br>price".$price."<br>");
        //print_r("<br>discountvalue".$discountvalue."<br>");
        //خصم نسبه
        /* if($sellbillDiscountType == 2)
          {
          $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
          }
         */
        $totalPriceBeforeDiscount = $price * $quantity;

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

        $quantityUnit += ($quantity * $productnumber);

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $allbuyprice = $quantity * $buyprice;
        $finalbuyprice += $allbuyprice;
        $thirdStep = ($totalPrice / $sellbillTotalBill);
        //print_r("<br>totalPriceBeforeDiscount".$totalPriceBeforeDiscount."<br>");
        $ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue * $quantity);
        //$ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($discountValue + $thirdStep);
        //print_r("--------------<br>ReturnSellPriceForOneProduct".$ReturnSellPriceForOneProduct."<br>-------");
        $ReturnSellPrice += $ReturnSellPriceForOneProduct;
    }
    //print_r("<br>".$ReturnSellPrice."<br>");

    return array($ReturnSellPrice, $quantityUnit, $finalbuyprice);
}

function getTotalBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellbilldetailEx;
    global $myProductunitRecord;
    global $mySellbillRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

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


    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellbilldetailData as $sellbilldetail) {
        $quantity = $sellbilldetail->sellbilldetailquantity;
        $price = $sellbilldetail->sellbilldetailprice;
        $totalPrice = $sellbilldetail->sellbilldetailtotalprice;
        $discountValue = $sellbilldetail->discountvalue;
        $productunitId = $sellbilldetail->productunitid;
        //$buyPrice = $sellbilldetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyPrice = (float) $sellbilldetail->buyprice;
                break;
            case "last":
                $buyPrice = (float) $sellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyPrice = (float) $sellbilldetail->meanbuyprice;
                break;
            case "last_discount":
                $buyPrice = (float) $sellbilldetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyPrice = (float) $sellbilldetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyPrice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellbilldetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellbilldetail->meanbuyprice_withTax;
                break;
        }
        $buyDiscount = $sellbilldetail->buydiscount;
        $buyDiscountType = $sellbilldetail->buydiscounttype;

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

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

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }



    return array($sellPrice, $quantityUnit);
}

function getTotalAditionalBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

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

    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        //$buyPrice = $sellandreturndetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyPrice = (float) $sellandreturndetail->buyprice;
                break;
            case "last":
                $buyPrice = (float) $sellandreturndetail->lastbuyprice;
                break;
            case "mean":
                $buyPrice = (float) $sellandreturndetail->meanbuyprice;
                break;
            case "last_discount":
                $buyPrice = (float) $sellandreturndetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyPrice = (float) $sellandreturndetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyPrice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withTax;
                break;
        }
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;

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

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

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }



    return array($sellPrice, $quantityUnit);
}

function getTotalAditionalReturnBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $mySellandruternbilldetailEx;
    global $mySellbillandruternRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;

    $sellandreturndetailData = $mySellandruternbilldetailEx->queryWithDateAndConditionsTypeAndProductIdExt($startDate, $endDate, 1, $ProductIdselected);
    $buyPriceType = $_POST["buyPriceType"];
    foreach ($sellandreturndetailData as $sellandreturndetail) {
        $quantity = $sellandreturndetail->sellbilldetailquantity;
        $price = $sellandreturndetail->sellbilldetailprice;
        $totalPrice = $sellandreturndetail->sellbilldetailtotalprice;
        $discountValue = $sellandreturndetail->discountvalue;
        $productunitId = $sellandreturndetail->productunitid;
        //$buyPrice = $sellandreturndetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyPrice = (float) $sellandreturndetail->buyprice;
                break;
            case "last":
                $buyPrice = (float) $sellandreturndetail->lastbuyprice;
                break;
            case "mean":
                $buyPrice = (float) $sellandreturndetail->meanbuyprice;
                break;
            case "last_discount":
                $buyPrice = (float) $sellandreturndetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyPrice = (float) $sellandreturndetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyPrice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $sellandreturndetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $sellandreturndetail->meanbuyprice_withTax;
                break;
        }
        $buyDiscount = $sellandreturndetail->buydiscount;
        $buyDiscountType = $sellandreturndetail->buydiscounttype;

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

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

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;


        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }
    return array($sellPrice, $quantityUnit);
}

function getTotalReturnBuyPriceByDateAndProductId($startDate, $endDate, $ProductIdselected, $overAllAveragePrice) {
    global $myReturnsellbilldetailEx;
    global $myReturnsellbillRecord;
    global $myProductunitRecord;
    global $myProductRecord;
    global $myProductunitEx;

    $sellPrice = 0;
    $quantityUnit = 0;


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

    $buyPriceType = $_POST["buyPriceType"];
    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $returnsellbilldetail->returnsellbilldetailquantity;
        $price = $returnsellbilldetail->returnsellbilldetailprice;
        $totalPrice = $returnsellbilldetail->returnsellbilldetailtotalprice;
        $discountValue = $returnsellbilldetail->discountvalue;
        $productunitId = $returnsellbilldetail->productunitid;
        //$buyPrice = $returnsellbilldetail->buyprice;
        switch ($buyPriceType) {
            case "first":
                $buyPrice = (float) $returnsellbilldetail->buyprice;
                break;
            case "last":
                $buyPrice = (float) $returnsellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyPrice = (float) $returnsellbilldetail->meanbuyprice;
                break;
            case "last_discount":
                $buyPrice = (float) $returnsellbilldetail->lastbuyprice_withDiscount;
                break;
            case "mean_discount":
                $buyPrice = (float) $returnsellbilldetail->meanbuyprice_withDiscount;
                break;
            case "generalPrice":
                $buyPrice = (float) $overAllAveragePrice;
                break;
            case "tax":
                $buyprice = (float) $returnsellbilldetail->lastbuyprice_withTax;
                break;
            case "mean_tax":
                $buyprice = (float) $returnsellbilldetail->meanbuyprice_withTax;
                break;
        }
        $buyDiscount = $returnsellbilldetail->buydiscount;
        $buyDiscountType = $returnsellbilldetail->buydiscounttype;

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

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

        $productBuyPrice = $productBuyPrice * $productnumber * $quantity;

        $quantityUnit += ($quantity * $productnumber);

        $sellPrice += $productBuyPrice;
    }
    return array($sellPrice, $quantityUnit);
}

function loadProductCat() {
    global $myProductcatEx;
    global $myProductunitEx;

    $productcatData = $myProductcatEx->queryProductcatInProduct();

    foreach ($productcatData as $productcat) {
        $productcatName;
        $productcat->productCatName = loadProductCatNameById($productcat->productCatId, $productcatName, 1);
    }
    return $productcatData;
}

// select from Product tbl
function loadProductCatNameById($productCatId, $productcatName, $itr) {
    global $myProductcatRecord;

    $productcatNamex = $productcatName;
    // select all data from producttbl
    $productcatData = $myProductcatRecord->load($productCatId);

    if (count($productcatData) > 0) {

        if ($itr == 1) {
            $productcatNamex = $productcatData->productCatName;
        } elseif ($itr == 2) {
            $productcatNamex = $productcatData->productCatName . "/" . $productcatNamex;
        }

        if ($productcatData->productCatParent != 0) {
            return loadProductCatNameById($productcatData->productCatParent, $productcatNamex, 2);
        }
    }
    return $productcatNamex;
}

function loadProductCatById($productCatId) {
    global $myProductcatRecord;

    $productCatSearchData = $myProductcatRecord->load($productCatId);
    return $productCatSearchData;
}

function getTotalSellOpticByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $billsProductsEX;

    $sellPrice = 0;
    $quantityUnit = 0;
    $finalbuypric = 0;
    $opticSellData = $billsProductsEX->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);
    $buyPriceType = $_POST["buyPriceType"];
    foreach ($opticSellData as $sellbilldetail) {
        $quantity = $sellbilldetail->productno;
        $price = $sellbilldetail->productprice;
        $totalPrice = $sellbilldetail->producttotalprice;
        $sellbillTotalBill = $sellbilldetail->netbillvalue;
        $sellbillDiscount = $sellbilldetail->discountvalue;
        $sellbillDiscountType = $sellbilldetail->discounttype;
        //$buyprice = $sellbilldetail->productBuyPrice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $sellbilldetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $sellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $sellbilldetail->meanbuyprice;
                break;
//            case "last_discount":
//                $buyPrice = (float) $sellbilldetail->lastbuyprice_withDiscount;
//                break;
//            case "mean_discount":
//                $buyPrice = (float) $sellbilldetail->meanbuyprice_withDiscount;
//                break;
            default:
                $buyprice = (float) $sellbilldetail->meanbuyprice;
                break;
        }
        if ($sellbillDiscountType == 2) {
            $sellbillDiscount = ($sellbillDiscount / 100) * $sellbillTotalBill;
        }

        $totalPriceBeforeDiscount = $price * $quantity; //product price before discount


        $quantityUnit += $quantity;
        //first Step قيمة خصم المنتج على الوحدة
        //$firstStep = $discountValue;
        //second Stepقيمة المنتج من الخصم على المنتج
        //$secondStep = $totalPrice / $sellbillTotalBill;

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = ($totalPrice / $sellbillTotalBill) * $sellbillDiscount;

        //fourth Step  قيمة خصم الفاتورة للوحدة
        //$fourthStep = $thirdStep;
        //$sellPriceForOneProduct= $totalPriceBeforeDiscount - ($firstStep + $fourthStep);
        $allbuyprice = $buyprice * $quantity;
        $finalbuypric += $allbuyprice;
        $sellPriceForOneProduct = $totalPriceBeforeDiscount - $thirdStep;
        $sellPrice += $sellPriceForOneProduct;
    }



    return array($sellPrice, $quantityUnit, $finalbuypric, $thirdStep);
}

function getTotalReturnOpticByDateAndProductId($startDate, $endDate, $ProductIdselected) {
    global $billsReturnProductsEX;

    $ReturnSellPrice = 0;
    $quantityUnit = 0;
    $finalbuyprice = 0;

    $returnsellbilldetailData = $billsReturnProductsEX->queryWithDateAndConditionsAndProductIdExt($startDate, $endDate, $ProductIdselected);

    $buyPriceType = $_POST["buyPriceType"];
    foreach ($returnsellbilldetailData as $returnsellbilldetail) {
        $quantity = $returnsellbilldetail->productno;
        $price = $returnsellbilldetail->productprice;
        $totalPrice = $returnsellbilldetail->producttotalprice;
        $sellbillTotalBill = $returnsellbilldetail->returnedprice;
        //$buyprice = $returnsellbilldetail->productBuyPrice;
        switch ($buyPriceType) {
            case "first":
                $buyprice = (float) $returnsellbilldetail->buyprice;
                break;
            case "last":
                $buyprice = (float) $returnsellbilldetail->lastbuyprice;
                break;
            case "mean":
                $buyprice = (float) $returnsellbilldetail->meanbuyprice;
                break;
//            case "last_discount":
//                $buyPrice = (float) $returnsellbilldetail->lastbuyprice_withDiscount;
//                break;
//            case "mean_discount":
//                $buyPrice = (float) $returnsellbilldetail->meanbuyprice_withDiscount;
//                break;
            default:
                $buyprice = (float) $returnsellbilldetail->meanbuyprice;
                break;
        }

        $totalPriceBeforeDiscount = $price * $quantity;
        $quantityUnit += $quantity;

        if ($sellbillTotalBill == 0) {
            $sellbillTotalBill = 1;
        }
        //third Step قيمة الخصم للمنتجات
        $thirdStep = $totalPrice / $sellbillTotalBill;


        $allbuyprice = $quantity * $buyprice;

        //print_r("<br>buyprice".$buyprice);
        //print_r("<br>quantity".$quantity);
        $finalbuyprice += $allbuyprice;
        $ReturnSellPriceForOneProduct = $totalPriceBeforeDiscount - ($thirdStep);
        $ReturnSellPrice += $ReturnSellPriceForOneProduct;
    }

//print($ReturnSellPrice."ReturnSellPrice<br>");

    return array($ReturnSellPrice, $quantityUnit, $finalbuyprice, $thirdStep);
}

function getAllSubCat($catid, $mode) {
    global $productCatExt;
    global $productCatDAO;
    global $catsIDS;
    global $lastLevelCatIDS;
    //mode = 1 get all sub cats
    //mode = 2 get last level cats

    $result = $productCatExt->queryByParentExt($catid);
    if (count($result) > 0) {
        foreach ($result as $data) {
            if ($mode == 1) {
                $catsIDS .= "," . $data->productCatId;
                getAllSubCat($data->productCatId, $mode);
            } elseif ($mode == 2) {
                $childData = $productCatExt->queryByParentExt($data->productCatId);
                if (count($childData) > 0) {
                    getAllSubCat($data->productCatId, $mode);
                } else {
                    if (!empty($data->productCatId)) {
                        array_push($lastLevelCatIDS, $data->productCatId);
                    }
                }
            }
        }
    } else {
//        if ($mode == 2) {
//            $childData = $productCatExt->queryByParentExt($data->productCatId);
//            if (count($childData) > 0) {
//                getAllSubCat($data->productCatId, $mode);
//            } else {
//                if (!empty($data->productCatId)) {
//                    array_push($lastLevelCatIDS, $data->productCatId);
//                }
//            }
//        }
    }
}

?>