<?php

// get the config file
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");


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

//here the global templates
$smarty->assign("itIsMachine_NoLogin", 1);
if ($do != "getDataAjax") {
    $smarty->display("header-bill.html");
}
//$smarty->display("maintennanceHeader.html");
//here goes the instances and general variables


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

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


//check and use the condetion that suite this action
//Productcat
$productCat = new Productcat();
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();
//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$productExt = new ProductMySqlExtDAO();

//Storedetail
$myStoredetailRecord = new StoredetailMySqlDAO();
$myStoredetailEx = new StoredetailMySqlExtDAO();

//progsetting
$Programsettings = new Programsetting();
$ProgramsettingsDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingsExt = new ProgramsettingsMySqlExtDAO();


if (empty($do)) {
##1- set db name as last db name in dbNames.txt
    $Programs = $ProgramsettingsDAO->queryAll();
    $smarty->assign("Programs", $Programs);
    $smarty->display("productbycode/show.html");
} elseif ($do == "getDataAjax") {
    // Read in entire file
    $text = $_POST["search_text"];
// $text = 'i0000103';
    $smarty->assign("text", $text);


    $Programsettingdata = $ProgramsettingsDAO->load(1);
    $text = str_replace("ه", "i", $text); //as ه is arabic it wont be recognized as ffirst so replace it
    $text = str_replace("÷", "i", $text); //as ÷ it wont be recognized as ffirst so replace it
    $text = str_replace("/", "i", $text);
    $parcodePrefixList = array('i', 'I');
    if (in_array($text[0], $parcodePrefixList) && $Programsettingdata->usedParcode == 1) {//usedParcode == 1 it is id coming after i
        $type = "id";
        $trimmed = trim($text, "i,I,ه");
        $barcode = intval($trimmed);
    } else {
        $type = "barcode";
        $barcode = $text;
    }

    if ($barcode != '') {
        getData($barcode, $type);
    } else {
        $Programs = $ProgramsettingsDAO->queryAll();
        $smarty->assign("Programs", $Programs);
        $smarty->display("productbycode/result.html");
    }
}


if ($do != "getDataAjax") {
    $smarty->assign("offerclient", 1);
    $smarty->display("footer.html");
}

function getData($barcode, $type) {
    global $productExt;
    global $myStoredetailEx;
    global $ProgramsettingsDAO;
    global $smarty;
    if ($type == "id") {
        $productsData = $productExt->queryByProductCatIdInExts($barcode);
    } else {
        //barcode
        $productsData = $productExt->queryByProductCatIdInExts2($barcode);
    }
    $smarty->assign("productsData", $productsData);
    if (isset($productsData[0]->productId) && $productsData[0]->productId > 0) {
        $storesQuantity = $myStoredetailEx->getproductInStores($productsData[0]->productId, " and storedetail.productquantity > 0");
        $smarty->assign("storesQuantity", $storesQuantity);
    }

    $Programs = $ProgramsettingsDAO->queryAll();
    $smarty->assign("Programs", $Programs);

    $smarty->display("productbycode/result.html");
}

?>