<?php

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

//global varable
global $showoutside;

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

//here the db files that include in the file
include("../public/include_dao.php");
//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');
//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');
//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');
//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');
//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- تكويد المنتجات

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

//here the global templates
$smarty->display("header.html");
//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();
//Supplierdebtchange
$supplierDebtChange = new Supplierdebtchange();
$supplierDebtChangeDAO = new SupplierdebtchangeMySqlDAO();
$supplierDebtChangeExt = new SupplierdebtchangeMySqlExtDAO();
//Storedetail
$storeDetail = new Storedetail();
$storeDetailDAO = new StoredetailMySqlDAO();
$storeDetailExt = new StoredetailMySqlExtDAO();
//Storereport
$storeReport = new Storereport;
$storeReportDAO = new StorereportMySqlDAO();
$storeReportExt = new StorereportMySqlExtDAO();
//here goes the instances and general variables
//Productserial
$productSerial = new Productserial();
$productSerialDAO = new ProductserialMySqlDAO();
$productSerialExt = new ProductserialMySqlExtDAO();

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

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

    $buyBill_Id = $_GET['id'];
    $billType = $_GET['type'];

    $smarty->assign("billType", $billType);
    $smarty->assign("buyBill_Id", $buyBill_Id);
    //print("buyBill_Id: ".$buyBill_Id."<br>billType: ".$billType);
    if (!empty($buyBill_Id) && !empty($billType)) {
        if ($billType == 1) {
            $productsData = getProductsFromReturnBuyBill($buyBill_Id); //undeleted products
        } elseif ($billType == 2) {
            $productsData = getProductsFromBuyBill($buyBill_Id);
        } else {//if($billType == 3)
            $productsData = getProductsFromBuyAndReturnBill($buyBill_Id);
        }

        //print("<pre>");
//			print_r($productsData);
//			print("</pre>");

        $numOfProducts = count($productsData);
        $smarty->assign("numOfProducts", $numOfProducts);
        $smarty->assign("productsData", $productsData);
    }
    $smarty->display("productsParcode/add.html");
} elseif ($do == "add") {
    //here the permssion check
    include_once("../public/authentication.php");
    try {
        $billType = add();

        if ($billType == 1) {
            $url = "returnBuyBillController.php?do=show";
        } elseif ($billType == 2) {
            $url = "buyBillController.php?do=show";
        } elseif ($billType == 3) {
            $url = "buyAndReturnBillController.php?do=show";
        }

        $smarty->assign('urldirect', $url);
        $note = "تمت العملية بنجاح";
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} 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 getProductsFromBuyBill($buyBill_Id) {
    global $buyBillDetailExt;
    global $productSerialDAO;
    global $productSerialExt;
    $buyBillDetails = $buyBillDetailExt->queryByBuybillidExt($buyBill_Id);
    if (count($buyBillDetails) > 0) {
        foreach ($buyBillDetails as $bill) {
            $serialData = $productSerialExt->queryLastWithProductId($bill->buybilldetailproductid);
            if (count($serialData) > 0) {
                $bill->oldSerial = $serialData->serialnumber;
            }
        }
    }
    return $buyBillDetails;
}

function getProductsFromReturnBuyBill($buyBill_Id) {
    global $returnBuyBillDetailExt;
    global $productSerialDAO;
    global $productSerialExt;
    $buyBillDetails = $returnBuyBillDetailExt->queryByBuyReturnbuybillIdExt($buyBill_Id);
    if (count($buyBillDetails) > 0) {
        foreach ($buyBillDetails as $bill) {
            $serialData = $productSerialExt->queryLastWithProductId($bill->returnbuybilldetailproductid);
            if (count($serialData) > 0) {
                $bill->oldSerial = $serialData->serialnumber;
            }

            $bill->buybilldetailproductid = $bill->returnbuybilldetailproductid;
        }
    }
    return $buyBillDetails;
}

function getProductsFromBuyAndReturnBill($buyBill_Id) {
    global $buyAndReturnBillDetailExt;
    global $productSerialDAO;
    global $productSerialExt;
    $buyBillDetails = $buyAndReturnBillDetailExt->queryByBuybillidExt($buyBill_Id);
    if (count($buyBillDetails) > 0) {
        foreach ($buyBillDetails as $bill) {
            $serialData = $productSerialExt->queryLastWithProductId($bill->buybilldetailproductid);
            if (count($serialData) > 0) {
                $bill->oldSerial = $serialData->serialnumber;
            }
        }
    }
    return $buyBillDetails;
}

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

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

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

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

function add() {
    global $productSerial;
    global $productSerialDAO;
    global $productSerialExt;

    $billType = $_POST['billType'];
    $itr = $_POST['itr'];
    if (!empty($itr)) {
        for ($i = 1; $i <= $itr; $i++) {
            $newParcode = $_POST['newParcode' . $i];
            $productId = $_POST['productId' . $i];

            if (!empty($productId)) {
                //check if there is a row for this product first
                $productData = $productSerialExt->queryLastWithProductId($productId);
                if (count($productData) > 0) {
                    $productSerialId = $productData->productserailid;

                    $productSerial->productid = $productId;
                    $productSerial->serialnumber = $newParcode;
                    $productSerial->productserailid = $productSerialId;

                    $productSerialDAO->update($productSerial);
                } else {
                    $productSerial->productid = $productId;
                    $productSerial->serialnumber = $newParcode;

                    $productSerialDAO->insert($productSerial);
                }
                //TODO: update parcode in buybilldetail tbl???!!!
            }
        }
    }
    return $billType;
}

function showAll() {
    global $buyBillExt;
    global $smarty;

    // required connect
    SmartyPaginate::connect();
    // set items per page
    SmartyPaginate::setLimit(25);

    // get the all colums number
    $allColums = $buyBillExt->queryByConditionExt(0);
    SmartyPaginate::setTotal(count($allColums));
    //get the selected data with limitation
    $buyBillData = $buyBillExt->queryByConditionExtLimited(0, SmartyPaginate::getCurrentIndex() + 1, SmartyPaginate::getLimit());

    // assign your db results to the template
    $smarty->assign('buyBillData', $buyBillData);

    // assign {$paginate} var
    SmartyPaginate::assign($smarty);
    //text that show in navgation
    SmartyPaginate::setNextText('التالى');
    SmartyPaginate::setPrevText('السابق');
    //add any varaible in url
    SmartyPaginate::setUrl('buyBillController.php?do=show&');
}

function showBySupplier($supplierId) {
    global $buyBillExt;
    global $smarty;

    // required connect
    SmartyPaginate::connect();
    // set items per page
    SmartyPaginate::setLimit(25);

    // get the all colums number
    $allColums = $buyBillExt->queryBySupplierId($supplierId);
    SmartyPaginate::setTotal(count($allColums));
    //get the selected data with limitation
    $buyBillData = $buyBillExt->queryBySupplierIdLimited($supplierId, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());

    // assign your db results to the template
    $smarty->assign('buyBillData', $buyBillData);

    // assign {$paginate} var
    SmartyPaginate::assign($smarty);
    //text that show in navgation
    SmartyPaginate::setNextText('التالى');
    SmartyPaginate::setPrevText('السابق');
    //add any varaible in url
    SmartyPaginate::setUrl('buyBillController.php?do=show&supplier=' . $supplierId);
}

function showByBillSerial($serial) {
    global $buyBillExt;
    global $smarty;

    // required connect
    SmartyPaginate::connect();
    // set items per page
    SmartyPaginate::setLimit(25);

    // get the all colums number
    $allColums = $buyBillExt->queryBySerial($serial);
    SmartyPaginate::setTotal(count($allColums));
    //get the selected data with limitation
    $buyBillData = $buyBillExt->queryBySerialLimited($serial, SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());

    // assign your db results to the template
    $smarty->assign('buyBillData', $buyBillData);

    // assign {$paginate} var
    SmartyPaginate::assign($smarty);
    //text that show in navgation
    SmartyPaginate::setNextText('التالى');
    SmartyPaginate::setPrevText('السابق');
    //add any varaible in url
    SmartyPaginate::setUrl('buyBillController.php?do=show&serial=' . $serial);
}

?>