<?php

//the global file operation
session_start();
ob_start();

//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");
//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');
//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');
//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');
//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');
//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');
//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');
//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.php');
//Store
require_once('../models/dao/StoreDAO.class.php');
require_once('../models/dto/Store.class.php');
require_once('../models/mysql/StoreMySqlDAO.class.php');
require_once('../models/mysql/ext/StoreMySqlExtDAO.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');
//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.php');
//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');
//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.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');
//Storereport
require_once('../models/dao/StorereportDAO.class.php');
require_once('../models/dto/Storereport.class.php');
require_once('../models/mysql/StorereportMySqlDAO.class.php');
require_once('../models/mysql/ext/StorereportMySqlExtDAO.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');
//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');

//Productserial
require_once('../models/dao/ProductserialDAO.class.php');
require_once('../models/dto/Productserial.class.php');
require_once('../models/mysql/ProductserialMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductserialMySqlExtDAO.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');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');
//Branch
require_once('../models/dao/BranchDAO.class.php');
require_once('../models/dto/Branch.class.php');
require_once('../models/mysql/BranchMySqlDAO.class.php');
require_once('../models/mysql/ext/BranchMySqlExtDAO.class.php');
//Buybilloffer
require_once('../models/dao/BuybillofferDAO.class.php');
require_once('../models/dto/Buybilloffer.class.php');
require_once('../models/mysql/BuybillofferMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybillofferMySqlExtDAO.class.php');
//Buybilldetailoffer
require_once('../models/dao/BuybilldetailofferDAO.class.php');
require_once('../models/dto/Buybilldetailoffer.class.php');
require_once('../models/mysql/BuybilldetailofferMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybilldetailofferMySqlExtDAO.class.php');
//Usergroup
require_once ('../models/dao/UsergroupDAO.class.php');
require_once ('../models/dto/Usergroup.class.php');
require_once ('../models/mysql/UsergroupMySqlDAO.class.php');
require_once ('../models/mysql/ext/UsergroupMySqlExtDAO.class.php');
//Buyandruternbillcurr
require_once('../models/dao/BuyandruternbillcurrDAO.class.php');
require_once('../models/dto/Buyandruternbillcurr.class.php');
require_once('../models/mysql/BuyandruternbillcurrMySqlDAO.class.php');
require_once('../models/mysql/ext/BuyandruternbillcurrMySqlExtDAO.class.php');
//Buybillcurr
require_once('../models/dao/BuybillcurrDAO.class.php');
require_once('../models/dto/Buybillcurr.class.php');
require_once('../models/mysql/BuybillcurrMySqlDAO.class.php');
require_once('../models/mysql/ext/BuybillcurrMySqlExtDAO.class.php');
//Returnbuybillcurr
require_once('../models/dao/ReturnbuybillcurrDAO.class.php');
require_once('../models/dto/Returnbuybillcurr.class.php');
require_once('../models/mysql/ReturnbuybillcurrMySqlDAO.class.php');
require_once('../models/mysql/ext/ReturnbuybillcurrMySqlExtDAO.class.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 :- buyBillController

  OPERTATION in Controller

  1- add buy bill
  2-  تقرير لمورد معين

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

//here the global templates
//here goes the instances and general variables
//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();

//Buybill
$buyBill = new Buybill();
$buyBillDAO = new BuybillMySqlDAO();
$buyBillExt = new BuybillMySqlExtDAO();
//Buybilldetail
$buyBillDetail = new Buybilldetail();
$buyBillDetailDAO = new BuybilldetailMySqlDAO();
$buyBillDetailExt = new BuybilldetailMySqlExtDAO();
//Returnbuybill
$returnBuyBill = new Returnbuybill();
$returnBuyBillDAO = new ReturnbuybillMySqlDAO();
$returnBuyBillExt = new ReturnbuybillMySqlExtDAO();
//Returnbuybilldetail
$returnBuyBillDetail = new Returnbuybilldetail();
$returnBuyBillDetailDAO = new ReturnbuybilldetailMySqlDAO();
$returnBuyBillDetailExt = new ReturnbuybilldetailMySqlExtDAO();
//Buyandruternbill
$buyAndReturnBill = new Buyandruternbill();
$buyAndReturnBillDAO = new BuyandruternbillMySqlDAO();
$buyAndReturnBillExt = new BuyandruternbillMySqlExtDAO();
//Buyandruternbilldetail
$buyAndReturnBillDetail = new Buyandruternbilldetail();
$buyAndReturnBillDetailDAO = new BuyandruternbilldetailMySqlDAO();
$buyAndReturnBillDetailExt = new BuyandruternbilldetailMySqlExtDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();
//Supplierdebtchange
$supplierDebtChange = new Supplierdebtchange();
$supplierDebtChangeDAO = new SupplierdebtchangeMySqlDAO();
$supplierDebtChangeExt = new SupplierdebtchangeMySqlExtDAO();
//Store
$storeDAO = new StoreMySqlDAO();
$storeExt = new StoreMySqlExtDAO();
//Programsetting
$programSettingExt = new ProgramsettingsMySqlExtDAO();
//Unit
$unitDAO = new UnitMySqlDAO();
$unitExt = new UnitMySqlExtDAO();
//Billname
$billNameDAO = new BillnameMySqlDAO();
$billNameExt = new BillnameMySqlExtDAO();
//Billsetting
$billSettingExt = new BillsettingsMySqlExtDAO();
//Storedetail
$storeDetail = new Storedetail();
$storeDetailDAO = new StoredetailMySqlDAO();
$storeDetailExt = new StoredetailMySqlExtDAO();
//Storereport
$storeReport = new Storereport;
$storeReportDAO = new StorereportMySqlDAO();
$storeReportExt = new StorereportMySqlExtDAO();
//Save
$save = new Save();
$saveDAO = new SaveMySqlDAO();
$saveExt = new SaveMySqlExtDAO();
//Savedaily
$saveDaily = new Savedaily();
$saveDailyDAO = new SavedailyMySqlDAO();
$saveDailyExt = new SavedailyMySqlExtDAO();
//Productserial
$Productserial = new Productserial();
$ProductserialDAO = new ProductserialMySqlDAO();
$ProductserialEX = new ProductserialMySqlExtDAO();

//User
$userDAO = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();
//Branch
$Branch = new Branch();
$BranchDAO = new BranchMySqlDAO();
$BranchEX = new BranchMySqlExtDAO();
//Buybilloffer
$buyBillOffer = new Buybilloffer();
$buyBillOfferDAO = new BuybillofferMySqlDAO();
$buyBillOfferEX = new BuybillofferMySqlExtDAO();
//Buybilldetailoffer
$buyBillDetailOffer = new Buybilldetailoffer();
$buyBillDetailOfferDAO = new BuybilldetailofferMySqlDAO();
$buyBillDetailOfferEX = new BuybilldetailofferMySqlExtDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();
//Buyandruternbillcurr
$buyAndRuternBillCurr = new Buyandruternbillcurr();
$buyAndRuternBillCurrDAO = new BuyandruternbillcurrMySqlDAO();
$buyAndRuternBillCurrEX = new BuyandruternbillcurrMySqlExtDAO();
//Buybillcurr
$buyBillCurr = new Buybillcurr();
$buyBillCurrDAO = new BuybillcurrMySqlDAO();
$buyBillCurrEX = new BuybillcurrMySqlExtDAO();
//Returnbuybillcurr
$returnBuyBllCurr = new Returnbuybillcurr();
$returnBuyBllCurrDAO = new ReturnbuybillcurrMySqlDAO();
$returnBuyBllCurrEX = new ReturnbuybillcurrMySqlExtDAO();


$today = date("Y-m-d");
$smarty->assign("today", $today);

//
$Usergroupdata = $UsergroupDAO->load($_SESSION['usergroupid']);
$smarty->assign("Usergroupdata", $Usergroupdata);

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

    $billNameData = getBuyBillNames();
    $smarty->assign("billNameData", $billNameData);

    $smarty->display("buyBillview/billType.html");
} elseif ($do == "editprint") {
    //include_once("../public/authentication.php");
    $countBillDetails = 0;
    $buyBill_Id = $_GET['id'];

    $detailsData = showBillDetails($buyBill_Id);
    $buyBillData = $detailsData[0];

    $buyBillDetails = $detailsData[1];
    $billCurrData = $detailsData[2];


    //getalldiscount
    $buybilldiscount = $detailsData[0]->buybilldiscount;
    $buybilldiscountrype = $detailsData[0]->buybilldiscountrype; //0 مباشر
    $buybilladdition = $detailsData[0]->buybilladdition;
    $buybilladditiontrype = $detailsData[0]->buybilladditiontrype;
    $buybilltotalbill = $detailsData[0]->buybilltotalbill;
    $buybillaftertotalbill = $detailsData[0]->buybillaftertotalbill;
    $tax = $detailsData[0]->payedtax;

    if ($buybilladditiontrype == 0) {
        $additionvalue = $buybilladdition;
    } else {
        $additionvalue = ($buybilltotalbill / 100) * $buybilladdition;
    }
    if ($buybilldiscountrype == 0) {

        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($buybilldiscount - $additionvalue));
        $discountvalue = $buybilldiscount;
    } else {
        //discount
        $discountvalue = ($buybilltotalbill / 100) * $buybilldiscount;
        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($discountvalue - $additionvalue));
    }

    $smarty->assign('additionvalue', $additionvalue);
    $smarty->assign('discountvalue', $discountvalue);
    $smarty->assign('taxvalue', $taxvalue);

    //get count
    if (count($buyBillDetails) > 0) {
        foreach ($buyBillDetails as $detail) {
            $countBillDetails += $detail->buybilldetailquantity;
        }
    }

    $smarty->assign('buyBillData', $buyBillData);
    $smarty->assign('buyBillDetails', $buyBillDetails);
    $smarty->assign('billCurrData', $billCurrData);
    $smarty->assign('countBillDetails', $countBillDetails);

    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);
    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->billnameid);



    $billnameAddData = $billNameDAO->load($buyBillData->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);


    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $userData = $userDAO->load($_SESSION['userid']);
    $smarty->assign("userData", $userData);
    if ($userData->branchId != 0 && $userData->branchId != -1) {
        $branchData = $BranchDAO->load($userData->branchId);
        $smarty->assign("branchData", $branchData);
    }

    //here the smarty templates
    $smarty->display("buyBillview/editprint2_new.html");
    // $smarty->display("buyBillview/editprint2.html");
} elseif ($do == "editprint1") {
    //include_once("../public/authentication.php");
    $countBillDetails = 0;
    $buyBill_Id = $_GET['id'];

    $detailsData = showBillDetails($buyBill_Id);
    $buyBillData = $detailsData[0];

    $buyBillDetails = $detailsData[1];
    $billCurrData = $detailsData[2];

    //getalldiscount
    $buybilldiscount = $detailsData[0]->buybilldiscount;
    $buybilldiscountrype = $detailsData[0]->buybilldiscountrype; //0 مباشر
    $buybilltotalbill = $detailsData[0]->buybilltotalbill;
    $buybillaftertotalbill = $detailsData[0]->buybillaftertotalbill;
    $tax = $detailsData[0]->payedtax;


    if ($buybilldiscountrype == 0) {

        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($buybilldiscount));
        $discountvalue = $buybilldiscount;
    } else {
        //discount
        $discountvalue = ($buybilltotalbill / 100) * $buybilldiscount;
        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($discountvalue));
    }

    $smarty->assign('discountvalue', $discountvalue);
    $smarty->assign('taxvalue', $taxvalue);



    //get count
    if (count($buyBillDetails) > 0) {
        foreach ($buyBillDetails as $detail) {
            $countBillDetails += $detail->buybilldetailquantity;
        }
    }

    $smarty->assign('buyBillData', $buyBillData);
    $smarty->assign('buyBillDetails', $buyBillDetails);
    $smarty->assign('billCurrData', $billCurrData);
    $smarty->assign('countBillDetails', $countBillDetails);

    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);
    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->billnameid);



    $billnameAddData = $billNameDAO->load($buyBillData->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);


    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);


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

//returneditprint

if ($do == "returneditprint") {


    $returnbuybillId = $_GET['id'];

    $detailsData = showreturnBillDetails($returnbuybillId);
    $returnbuybillData = $detailsData[0];
    $returnbuybilldetailData = $detailsData[1];
    $billCurrData = $detailsData[2];

    //get count
    if (count($returnbuybilldetailData) > 0) {
        foreach ($returnbuybilldetailData as $detail) {
            $countBillDetails += $detail->returnbuybilldetailquantity;
        }
    }

    $smarty->assign('returnbuybillData', $returnbuybillData);
    $smarty->assign('returnbuybilldetailData', $returnbuybilldetailData);
    $smarty->assign('billCurrData', $billCurrData);

    //print_r($returnbuybilldetailData);
    $smarty->assign('countBillDetails', $countBillDetails);


    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);

    /* print_r($detailsData[0]->returnbuybillid);
      print_r("<pre>");
      print_r($billSettingsData);
      print_r("<pre>"); */

    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->returnbuybillid);

    $billnameAddData = $billNameDAO->load($returnbuybillData->billnameid);

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

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $userData = $userDAO->load($_SESSION['userid']);
    $smarty->assign("userData", $userData);
    if ($userData->branchId != 0 && $userData->branchId != -1) {
        $branchData = $BranchDAO->load($userData->branchId);
        $smarty->assign("branchData", $branchData);
    }

    //here the smarty templates
    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);

    $smarty->display("returnbuybillview/editprint2.html");
}


if ($do == "returneditprint1") {


    $returnbuybillId = $_GET['id'];

    $detailsData = showreturnBillDetails($returnbuybillId);
    $returnbuybillData = $detailsData[0];
    $returnbuybilldetailData = $detailsData[1];
    $billCurrData = $detailsData[2];

    //getalldiscount
    $buybilldiscount = $detailsData[0]->returnbuybilldiscount;
    $buybilldiscountrype = $detailsData[0]->returnbuybilldiscountrype; //0 مباشر
    $buybilltotalbill = $detailsData[0]->returnbuybilltotalbill;
    $buybillaftertotalbill = $detailsData[0]->returnbuybillaftertotalbill;
    $tax = $detailsData[0]->payedtax;


    if ($buybilldiscountrype == 0) {
        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ((abs($buybilldiscount) * -1)));
        $discountvalue = $buybilldiscount;
    } else {
        //discount
        $discountvalue = ($buybilltotalbill / 100) * $buybilldiscount;
        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ((abs($discountvalue) * -1)));
    }

    $smarty->assign('discountvalue', $discountvalue);
    $smarty->assign('taxvalue', $taxvalue);


    //get count
    if (count($returnbuybilldetailData) > 0) {
        foreach ($returnbuybilldetailData as $detail) {
            $countBillDetails += $detail->returnbuybilldetailquantity;
        }
    }

    $smarty->assign('returnbuybillData', $returnbuybillData);
    $smarty->assign('returnbuybilldetailData', $returnbuybilldetailData);
    $smarty->assign('billCurrData', $billCurrData);
    $smarty->assign('countBillDetails', $countBillDetails);
    //print_r($countBillDetails);

    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);

    /* print_r($detailsData[0]->returnbuybillid);
      print_r("<pre>");
      print_r($billSettingsData);
      print_r("<pre>"); */

    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->returnbuybillid);

    $billnameAddData = $billNameDAO->load($returnbuybillData->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $userData = $userDAO->load($_SESSION['userid']);
    $smarty->assign("userData", $userData);
    if ($userData->branchId != 0 && $userData->branchId != -1) {
        $branchData = $BranchDAO->load($userData->branchId);
        $smarty->assign("branchData", $branchData);
    }

    //here the smarty templates
    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);

    $smarty->display("returnbuybillview/editprint2a4.html");
} elseif ($do == "buyandreturneditprint") {
    //editprint
    //here the permssion check



    $buyAndReturnBill_Id = $_GET['id'];

    $detailsData = showbuyAndReturnBillDetails($buyAndReturnBill_Id);
    $buyBillData = $detailsData[0];

    $buyBillDetails = $detailsData[1];
    $billCurrData = $detailsData[2];

    //get count
    $details = countBillDetails($buyAndReturnBill_Id);
    $countBillDetails_buy = $details[0];
    $countBillDetails_return = $details[1];
    $smarty->assign('countBillDetails_buy', $countBillDetails_buy);
    $smarty->assign('countBillDetails_return', $countBillDetails_return);
    $countBillDetails = $countBillDetails_buy + $countBillDetails_return;
    //	print_r($countBillDetails);
    $smarty->assign('countBillDetails', $countBillDetails);
    $smarty->assign('buyBillData', $buyBillData);
    $smarty->assign('buyBillDetails', $buyBillDetails);
    $smarty->assign('billCurrData', $billCurrData);

    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);

    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->billnameid);




    $billnameAddData = $billNameDAO->load($buyBillData->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);


    //here the smarty templates
    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);

    $userData = $userDAO->load($_SESSION['userid']);
    $smarty->assign("userData", $userData);
    if ($userData->branchId != 0 && $userData->branchId != -1) {
        $branchData = $BranchDAO->load($userData->branchId);
        $smarty->assign("branchData", $branchData);
    }

    $smarty->display("buyAndReturnBillview/editprint2.html");
} elseif ($do == "buyandreturneditprint1") {
    //editprint
    //here the permssion check



    $buyAndReturnBill_Id = $_GET['id'];

    $detailsData = showbuyAndReturnBillDetails($buyAndReturnBill_Id);
    $buyBillData = $detailsData[0];

    $buyBillDetails = $detailsData[1];
    $billCurrData = $detailsData[2];

    //getalldiscount
    $buybilldiscount = $detailsData[0]->buybilldiscount;
    $buybilldiscountrype = $detailsData[0]->buybilldiscountrype; //0 مباشر
    $buybilltotalbill = $detailsData[0]->buybilltotalbill;
    $buybillaftertotalbill = $detailsData[0]->buybillaftertotalbill;
    $tax = $detailsData[0]->payedtax;


    if ($buybilldiscountrype == 0) {

        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($buybilldiscount));
        $discountvalue = $buybilldiscount;
    } else {
        //discount
        $discountvalue = ($buybilltotalbill / 100) * $buybilldiscount;
        //tax
        $taxvalue = $buybillaftertotalbill - ($buybilltotalbill - ($discountvalue));
    }

    $smarty->assign('discountvalue', $discountvalue);
    $smarty->assign('taxvalue', $taxvalue);



    //get count
    $details = countBillDetails($buyAndReturnBill_Id);
    $countBillDetails_buy = $details[0];
    $countBillDetails_return = $details[1];
    $smarty->assign('countBillDetails_buy', $countBillDetails_buy);
    $smarty->assign('countBillDetails_return', $countBillDetails_return);
    $countBillDetails = $countBillDetails_buy + $countBillDetails_return;
    //	print_r($countBillDetails);
    $smarty->assign('countBillDetails', $countBillDetails);
    $smarty->assign('buyBillData', $buyBillData);
    $smarty->assign('buyBillDetails', $buyBillDetails);
    $smarty->assign('billCurrData', $billCurrData);

    $billSettingsData = loadBillProperties($detailsData[0]->billnameid);

    $smarty->assign("billsettingsData", $billSettingsData);
    $smarty->assign("billnameid", $detailsData[0]->billnameid);


    $billnameAddData = $billNameDAO->load($buyBillData->billnameid);
    $smarty->assign("billnameAddData", $billnameAddData);


    //here the smarty templates
    $smarty->assign("customPrint", 1);
    $smarty->assign("customBuyBill", 1);
    $smarty->assign("buyBillNewProductScript", 1);

    $Programsettingdata = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsettingdata);



    $smarty->display("buyAndReturnBillview/editprint2a4.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->assign("customValidation", 1);




/* ===============================
  function in this CONTROLLER
  ================================ */

function showbuyAndReturnBillDetails($buyAndReturnBill_Id) {
    global $buyAndReturnBillExt;
    global $buyAndRuternBillCurrEX;
    global $buyAndReturnBillDetailExt;

    $buyBillData = $buyAndReturnBillExt->loadExt($buyAndReturnBill_Id);
    $buyBillDetails = $buyAndReturnBillDetailExt->queryByBuybillidExt($buyAndReturnBill_Id);
    $billCurrData = $buyAndRuternBillCurrEX->queryOneByBuybillid($buyAndReturnBill_Id);

    ##use currency values
    if (isset($billCurrData)) {
        $buyBillData->buybilldiscount = $billCurrData->buybilldiscountC;
        $buyBillData->buybilltotalbill = $billCurrData->buybilltotalbillC;
        $buyBillData->buybillaftertotalbill = $billCurrData->buybillaftertotalbillC;
        $buyBillData->buybilltotalpayed = $billCurrData->buybilltotalpayedC;
        $buyBillData->buybillfinalbill = $billCurrData->buybillfinalbillC;
        $buyBillData->buybilldirectpayment = $billCurrData->buybilldirectpaymentC;
        $buyBillData->payedtax = $billCurrData->payedtaxC;
        ##
        $buyBillData->buybilltotaldeptbefor = $buyBillData->buybilltotaldeptbefor * $billCurrData->conversionFactor;
        $buyBillData->buybilltotaldeptafter = $buyBillData->buybilltotaldeptafter * $billCurrData->conversionFactor;
        foreach ($buyBillDetails as $value) {
            $value->buybilldetailprice = $value->buybilldetailpriceC;
            $value->buybilldetailtotalprice = $value->buybilldetailtotalpriceC;
            $value->discountvalue = $value->discountvalueC;
        }
    }
    //print_r($buyAndReturnBill_Id);
    return array($buyBillData, $buyBillDetails, $billCurrData);
}

function showreturnBillDetails($returnbuybillId) {
    global $returnBuyBillExt;
    global $returnBuyBillDetailExt;
    global $returnBuyBllCurrEX;

    $returnbuybillData = $returnBuyBillExt->loadExt($returnbuybillId);
    $returnbuybilldetailData = $returnBuyBillDetailExt->queryByBuyReturnbuybillIdExt($returnbuybillId);
    $billCurrData = $returnBuyBllCurrEX->queryOneByBuybillid($returnbuybillId);

    ##use currency values
    if (isset($billCurrData)) {
        $returnbuybillData->returnbuybilldiscount = $billCurrData->returnbuybilldiscountC;
        $returnbuybillData->returnbuybilltotalbill = $billCurrData->returnbuybilltotalbillC;
        $returnbuybillData->returnbuybillaftertotalbill = $billCurrData->returnbuybillaftertotalbillC;
        $returnbuybillData->returnbuybilltotalpayed = $billCurrData->returnbuybilltotalpayedC;
        $returnbuybillData->returnbuybillfinalbill = $billCurrData->returnbuybillfinalbillC;
        $returnbuybillData->returnbuybilldirectpayment = $billCurrData->returnbuybilldirectpaymentC;
        $returnbuybillData->payedtax = $billCurrData->payedtaxC;
        ##
        $returnbuybillData->returnbuybilltotaldeptbefor = $returnbuybillData->returnbuybilltotaldeptbefor * $billCurrData->conversionFactor;
        $returnbuybillData->returnbuybilltotaldeptafter = $returnbuybillData->returnbuybilltotaldeptafter * $billCurrData->conversionFactor;
        foreach ($returnbuybilldetailData as $value) {
            $value->returnbuybilldetailprice = $value->returnbuybilldetailpriceC;
            $value->returnbuybilldetailtotalprice = $value->returnbuybilldetailtotalpriceC;
            $value->discountvalue = $value->discountvalueC;
        }
    }

    return array($returnbuybillData, $returnbuybilldetailData);
}

function getBuyBillNames() {
    global $billNameDAO;
    global $billNameExt;
    $type = 1;
    // get all not deleted
    $billNameData = $billNameExt->queryByType($type);
    return $billNameData;
}

function loadBillProperties($billnameid) {
    global $billSettingExt;

    // select all data from Billname
    $billSettingsData = $billSettingExt->queryWithBillnameId($billnameid);

    return $billSettingsData;
}

function getProducts() {
    global $productExt;
    global $productCatDAO;
    global $smarty;
    $categories;
    $productsData = $productExt->queryAllExt();
    if (count($productsData) > 0) {
        $i = 0;
        foreach ($productsData as $pro) {
            $parentId = $pro->productCatId;
            //print("parentId: ".$parentId."");
            $pathArr = fetch_recursive($parentId, $categories);
            $smarty->assign("names" . $i, $pathArr);
            $i++;
        }
    }
    return $productsData;
}

function getSuppliers() {
    global $supplierDAO;
    $suppliersData = $supplierDAO->queryByCondition(0);
    return $suppliersData;
}

function getUnits() {
    global $unitDAO;

    $unitsData = $unitDAO->queryByCondition(0);
    return $unitsData;
}

function getStores() {
    global $storeDAO;
    $storesData = $storeDAO->queryByConditions(0);
    return $storesData;
}

function getProductCatParents() {
    global $productCatExt;
    global $productExt;

    $allParents = $productExt->queryAllCategories(); //$productCatExt->queryAllChildrenExt();
    return $allParents;
}

function checkRoundNumbers() {
    global $programSettingExt;
    $roundNumbersStatus = $programSettingExt->getRoundNumbersStatus();

    return $roundNumbersStatus;
}

function getProductCatId($productId) {
    global $productExt;
    $catId = $productExt->getProductCatId($productId);
    return $catId;
}

function showBillDetails($buyBill_Id) {
    global $buyBillExt;
    global $buyBillCurrEX;
    global $buyBillDetailExt;
    global $buyBillOfferEX;
    global $buyBillDetailOfferEX;

    $ordertype = $_GET['ordertype'];

    if ($ordertype == 0) {//bill
        $buyBillData = $buyBillExt->loadExt($buyBill_Id);
        $buyBillDetails = $buyBillDetailExt->queryByBuybillidExt($buyBill_Id);
        $billCurrData = $buyBillCurrEX->queryOneByBuybillid($buyBill_Id);
    } elseif ($ordertype == 1) { //offer
        $buyBillData = $buyBillOfferEX->loadExt($buyBill_Id);
        $buyBillDetails = $buyBillDetailOfferEX->queryByBuybillidExt($buyBill_Id);
    }

    ##use currency values
    if (isset($billCurrData)) {
        $buyBillData->buybilldiscount = $billCurrData->buybilldiscountC;
        $buyBillData->buybilltotalbill = $billCurrData->buybilltotalbillC;
        $buyBillData->buybillaftertotalbill = $billCurrData->buybillaftertotalbillC;
        $buyBillData->buybilltotalpayed = $billCurrData->buybilltotalpayedC;
        $buyBillData->buybillfinalbill = $billCurrData->buybillfinalbillC;
        $buyBillData->buybilldirectpayment = $billCurrData->buybilldirectpaymentC;
        $buyBillData->payedtax = $billCurrData->payedtaxC;
        ##
        $buyBillData->buybilltotaldeptbefor = $buyBillData->buybilltotaldeptbefor * $billCurrData->conversionFactor;
        $buyBillData->buybilltotaldeptafter = $buyBillData->buybilltotaldeptafter * $billCurrData->conversionFactor;
        foreach ($buyBillDetails as $value) {
            $value->buybilldetailprice = $value->buybilldetailpriceC;
            $value->buybilldetailtotalprice = $value->buybilldetailtotalpriceC;
            $value->discountvalue = $value->discountvalueC;
        }
    }
    return array($buyBillData, $buyBillDetails, $billCurrData);
}

function showBuyBills() {
    global $buyBillExt;
    $buyBills = $buyBillExt->queryAllSerialsNotDeleted();
    return $buyBills;
}

function countBillDetails($buyAndReturnBill_Id) {
    global $buyAndReturnBillExt;
    global $buyAndReturnBillDetailExt;

    $countBillDetails_return = 0;
    $countBillDetails_buy = 0;

    $buyBillDetails_buy = $buyAndReturnBillDetailExt->queryByBuybillidANDType($buyAndReturnBill_Id, 0);
    $buyBillDetails_return = $buyAndReturnBillDetailExt->queryByBuybillidANDType($buyAndReturnBill_Id, 1);

    //get count
    if (count($buyBillDetails_buy) > 0) {
        foreach ($buyBillDetails_buy as $detail) {
            $countBillDetails_buy += $detail->buybilldetailquantity;
        }
    }


    //get count
    if (count($buyBillDetails_return) > 0) {
        foreach ($buyBillDetails_return as $detail) {
            $countBillDetails_return += $detail->buybilldetailquantity;
        }
    }


    //print_r($buyAndReturnBill_Id);
    return array($countBillDetails_buy, $countBillDetails_return);
}

?>