<?php

/*
  session_start();
  ob_start(); */
//the global file operation
include("reportfunctions.php");
// get the config file
include_once("../public/config.php");



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


//GovernArea
require_once('../models/dao/GoverareaDAO.class.php');
require_once('../models/dto/Goverarea.class.php');
require_once('../models/mysql/GoverareaMySqlDAO.class.php');
require_once('../models/mysql/ext/GoverareaMySqlExtDAO.class.php');

$expensesTypeDAO = new ExpensestypeMySqlDAO();

//Checkdeposit
$Checkdeposit = new Checkdeposit();
$CheckdepositDAO = new CheckdepositMySqlDAO();
$CheckdepositEX = new CheckdepositMySqlExtDAO();

$checkWithdrawalDAO = new CheckwithdrawalMySqlDAO;
$checkWithdrawalExtDAO = new CheckwithdrawalMySqlExtDAO;
$checkWithdrawal = new Checkwithdrawal;

//Bank
$myBank = new Bank();
$myBankRecord = new BankMySqlDAO();
$myBankEx = new BankMySqlExtDAO();

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

$incomeType = new Incometype();
$IncometypeDAO = new IncometypeMySqlDAO();
$IncometypeEX = new IncometypeMySqlExtDAO();

$income = new Income();
$incomeDAO = new IncomeMySqlDAO();
$incomeEX = new IncomeMySqlExtDAO();

//Bankaccount
$myBankaccount = new Bankaccount();
$myBankaccountRecord = new BankaccountMySqlDAO();
$myBankaccountEx = new BankaccountMySqlExtDAO();

// cash transfer
$cashTransferExt = new CashtransferMySqlExtDAO();

// store
$myStoreRecord = new StoreMySqlDAO();

// unit
$unitDAO = new UnitMySqlDAO();

//Product
$product = new Product();
$productDAO = new ProductMySqlDAO();
$ProductEX = new ProductMySqlExtDAO();

//Productcat
$productCat = new Productcat();
$productCatDAO = new ProductcatMySqlDAO();
$productCatExt = new ProductcatMySqlExtDAO();

//# governArea
$governAreaDAO = new GoverareaMySqlDAO();

//#
$ClientareaEX = new ClientareaMySqlExtDAO();

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


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

  Controller Name :- savedailyAjaxCTRL

  OPERTATION in Controller


  1-display show form where search by saveid using ajax


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


//here goes the instances and general variables
//check and use the condetion that suite this action

$lastLevelCatIDS = array();
$catsIDS = "";


if ($do == "getsubcat") {

    $parentid = (int) filter_input(INPUT_POST, "id");
    $level = (int) filter_input(INPUT_POST, "level");
    if ($parentid == 0) {
        echo -1;
    } else {
        ##التصنيفات
        $catDataReport = getCategoryChilds($parentid);
        if (count($catDataReport[1]) > 0) {
            $smarty->assign("catDataReport", $catDataReport[1]);
            $smarty->assign("level", $level);
            $smarty->display("salesreportview/catchilds.html");
        } else {
            echo -1;
        }
    }
} elseif ($do == "getareas") {

    $governmentid = filter_input(INPUT_POST, "id");
    ##المناطق
    $clientarea_ids = $governAreaDAO->queryByGovernmentid($governmentid);
    foreach ($clientarea_ids as $areaid) {
        $areaids .= ',' . $areaid->clientareaid;
    }
    $areaids = ltrim($areaids, ',');

    $areas_data = $ClientareaEX->queryAllbystring($areaids);
//    print_r('<pre>');
//        print_r($areaids);
//    print_r('<pre>');

    if (!empty($areas_data)) {
        $smarty->assign("areas_data", $areas_data);

        $smarty->display("salesreportview/area.html");
    } else {
        echo -1;
    }
} elseif ($do == "getsubcat2") {

    $parentid = filter_input(INPUT_POST, "id");
    $level = filter_input(INPUT_POST, "level");
    ##التصنيفات
    $catDataReport = getCategoryChilds($parentid);
    if (count($catDataReport[1]) > 0) {
        $smarty->assign("catDataReport", $catDataReport[1]);
        $smarty->assign("level", $level);
        $smarty->display("productview/catchilds.html");
    } else {
        echo -1;
    }
} elseif ($do == "getsubcatbills") {

    $parentid = filter_input(INPUT_POST, "id");
    $level = filter_input(INPUT_POST, "level");
    $itr = filter_input(INPUT_POST, "itr");
    $service = filter_input(INPUT_POST, "service");
    $ret = filter_input(INPUT_POST, "ret");
    if (!isset($ret) || empty($ret)) {
        $ret = 0;
    }

    ##التصنيفات
    $catDataReport = getCategoryChilds($parentid, $service);
    if (count($catDataReport[1]) > 0) {
        $smarty->assign("catDataReport", $catDataReport[1]);
        $smarty->assign("level", $level);
        $smarty->assign("itr", $itr);
        if ($ret == 1) {
            $smarty->display("salesreportview/catchildsbills_re.html");
        } else {
            $smarty->display("salesreportview/catchildsbills.html");
        }
    } else {
        echo -1;
    }
} elseif ($do == "getproducts") {
    $parentid = filter_input(INPUT_POST, "id");
    $products = getCatProducts($parentid);
    $smarty->assign("catData", $products[0]);
    $smarty->assign("products", $products[1]);
    $smarty->display("salesreportview/products.html");
} elseif ($do == "getproductsbills") {
    $parentid = filter_input(INPUT_POST, "id");
    $itr = filter_input(INPUT_POST, "itr");
    $aw_i = filter_input(INPUT_POST, "aw_i");
    if (!isset($aw_i) || empty($aw_i)) {
        $aw_i = "no";
    }
    $tabindexnum = filter_input(INPUT_POST, "tabindexnum");
    if (!isset($tabindexnum) || empty($tabindexnum)) {
        $tabindexnum = "no";
    }
    $ret = filter_input(INPUT_POST, "ret");
    if (!isset($ret) || empty($ret)) {
        $ret = 0;
    }
    $products = getCatProducts($parentid);
    $smarty->assign("catData", $products[0]);
    $smarty->assign("products", $products[1]);
    $smarty->assign("itr", $itr);
    $smarty->assign("aw_i", $aw_i);
    $smarty->assign("tabindexnum", $tabindexnum);

    if ($ret == 1) {
        $smarty->display("salesreportview/productsbills_re.html");
    } else {
        $smarty->display("salesreportview/productsbills.html");
    }
} elseif ($do == "getproductsbills2") {
    $parentid = filter_input(INPUT_POST, "id");
    $itr = filter_input(INPUT_POST, "itr");
    $aw_i = filter_input(INPUT_POST, "aw_i");
    if (!isset($aw_i) || empty($aw_i)) {
        $aw_i = "no";
    }
    $tabindexnum = filter_input(INPUT_POST, "tabindexnum");
    if (!isset($tabindexnum) || empty($tabindexnum)) {
        $tabindexnum = "no";
    }
    $ret = filter_input(INPUT_POST, "ret");
    if (!isset($ret) || empty($ret)) {
        $ret = 0;
    }
    $products = getCatProducts($parentid);
    $smarty->assign("catData", $products[0]);
    $smarty->assign("products", $products[1]);
    $smarty->assign("itr", $itr);
    $smarty->assign("aw_i", $aw_i);
    $smarty->assign("tabindexnum", $tabindexnum);

    $smarty->display("salesreportview/productsbills_2.html");
} elseif ($do == "getsizecolor") {
    $parentid = filter_input(INPUT_POST, "id");
    $itr = filter_input(INPUT_POST, "itr");
    $ret = filter_input(INPUT_POST, "ret");
    if (!isset($ret) || empty($ret)) {
        $ret = 0;
    }
    $products = getSizeColor($parentid);
    $smarty->assign("catData", $products[0]);
    $smarty->assign("products", $products[1]);
    $smarty->assign("itr", $itr);
    if ($ret == 1) {
        $smarty->display("salesreportview/sizecolorbills_re.html");
    } else {
        $smarty->display("salesreportview/sizecolorbills.html");
    }
}
##
elseif ($do == "getsizecolor2") {
    $parentid = filter_input(INPUT_POST, "id");
    $itr = filter_input(INPUT_POST, "itr");
    $ret = filter_input(INPUT_POST, "ret");
    if (!isset($ret) || empty($ret)) {
        $ret = 0;
    }
    $products = getSizeColor($parentid);
    $smarty->assign("catData", $products[0]);
    $smarty->assign("products", $products[1]);
    $smarty->assign("itr", $itr);

    $smarty->display("salesreportview/sizecolorbills_2.html");
}
##
elseif ($do == "getproductformail") {
    $id = filter_input(INPUT_POST, "id");
    $itr = filter_input(INPUT_POST, "itr");
    $existids = filter_input(INPUT_POST, "existids");
    $existids = rtrim($existids, ",");
    if (empty($existids)) {
        $existids = 0;
    }

    $proIsOptic = filter_input(INPUT_POST, "proIsOptic");
    if ($proIsOptic == 2) {
        $productsOfCat = $ProductEX->queryByProductCatIdIn($id);
        $id = '0';
        foreach ($productsOfCat as $value) {
            $id .= ',' . $value->productId;
        }
        $productsData = $productExt->loadExt2($id, $existids);
    } else {
        $productsData = $productExt->loadExt2($id, $existids);
    }
    $smarty->assign("productsData", $productsData);
    $newitr = $itr + count($productsData);
    $smarty->assign("itr", $itr);
    $smarty->assign("newitr", $newitr);
    $smarty->display("productmailview/products.html");
}
##
elseif ($do == "getsubcatproductsformail") {
    $productCatId = filter_input(INPUT_POST, "id");
    if (isset($productCatId) && !empty($productCatId)) {


        $itr = filter_input(INPUT_POST, "itr");
        $existids = filter_input(INPUT_POST, "existids");
        $datatype = filter_input(INPUT_POST, "datatype");

        $onlyNegative = 0;
        if (isset($datatype) && !empty($datatype) && $datatype == 1) {
            $onlyNegative = 1;
        }

        $existids = rtrim($existids, ",");
        if (empty($existids)) {
            $existids = 0;
        }
        $productsData = array();
        $lastLevelCatIDS = array();
        array_push($lastLevelCatIDS, $productCatId);
        getAllSubCat($productCatId, 2); //mode = 2 get last level cats
        foreach ($lastLevelCatIDS as $mycatId) {
            $mycat = $productCatDAO->load($mycatId);
            $isOpticCondition = "";
            if ($mycat->isOptic == 2) {
                $isOpticCondition = " and isOptic = 2 ";
            } else {
                $isOpticCondition = " and isOptic != 2 ";
            }
            ##get subcat of cat
            $catsIDS = '' . $mycatId;
            getAllSubCat($mycatId, 1); //mode = 1 get all sub cats
            $productsOfCat = $ProductEX->queryByProductCatIdIn($catsIDS, $isOpticCondition);
            $id = '0';
            foreach ($productsOfCat as $value) {
                $id .= ',' . $value->productId;
            }

            $prodata = $productExt->loadExt2($id, $existids);
            foreach ($prodata as $data) {
                if ($onlyNegative == 1) {
                    if ($data->sumProductQuantity < 0) {
                        array_push($productsData, $data);
                    }
                } else {
                    array_push($productsData, $data);
                }
            }
        }

        $smarty->assign("productsData", $productsData);
        $newitr = $itr + count($productsData);
        $smarty->assign("itr", $itr);
        $smarty->assign("newitr", $newitr);
        $smarty->display("productmailview/products.html");
    }
}
##
elseif ($do == 'checkChk') {
    $checkwithdrawalnumber = $_POST['chck'];
    $accountid = $_POST['accid'];
    $checkChk = $CheckdepositEX->checkChkNoAndAccountNo($checkwithdrawalnumber, $accountid);
    if (!empty($checkChk)) {
        echo -2;
    }
} elseif ($do == 'bankname') {
    $bankName = $_POST['getname'];
    $bankData = $myBankRecord->queryByBankname($bankName);
    if (empty($bankName)) {
        echo -2;
    } else if (!empty($bankData)) {
        echo -1;
    }
} elseif ($do == 'savename') {
    $savename = $_POST['savename'];
    $savData = $mySaveRecord->queryBySavename($savename);
    if (empty($savename)) {
        echo -2;
    } elseif (!empty($savData)) {
        echo -1;
    }
} elseif ($do == 'checkChkWithDraw') {
    $checkwithdrawalnumber = $_POST['chck'];
    $accountid = $_POST['accid'];
    $checkChk = $checkWithdrawalExtDAO->checkNoChkAndAccountNo($checkwithdrawalnumber, $accountid);
    if (!empty($checkChk)) {
        echo -2;
    }
} elseif ($do == 'checkName') {
    $name = $_POST['name'];
    $typeData = $expensesTypeDAO->queryByExpensestypename($name);
    if (!empty($typeData)) {
        echo -1;
    } elseif (empty($typeData)) {
        echo 0;
    }
} elseif ($do == 'checkIncType') {
    $name = $_POST['name'];
    $typeData = $IncometypeDAO->queryByIncomeTypeName($name);
    if (!empty($typeData)) {
        echo -1;
    } elseif (empty($typeData)) {
        echo 0;
    }
} elseif ($do == 'checkInc') {
    $name = $_POST['name'];
    $typeData = $incomeDAO->queryByIncomeName($name);
    if (!empty($typeData)) {
        echo -1;
    } elseif (empty($typeData)) {
        echo 0;
    }
} elseif ($do == 'checkBankAcc') {
    $accountname = $_POST['accountname'];
    $bankid = $_POST['bankid'];
    $accountnumber = $_POST['accountnumber'];
    $typeData = $myBankaccountEx->queryAllByAccNameAndBank($accountname, $bankid);
    $accDataNo = $myBankaccountEx->queryAllByAccNoAndBank($accountnumber, $bankid);

    if (!empty($typeData)) {
        echo -1;
    } elseif (!empty($accDataNo)) {
        echo -2;
    } elseif (empty($typeData)) {
        echo 0;
    }
} elseif ($do == 'checkStoreName') {
    $storeName = $_POST['storeName'];
    $storeData = $myStoreRecord->queryByStoreName($storeName);
    if (!empty($storeData)) {
        echo -1;
    } elseif (empty($storeData)) {
        echo 0;
    }
} elseif ($do == 'checkunit') {
    $name = $_POST['name'];
    $unitData = $unitDAO->queryByUnitName($name);
    if (!empty($unitData)) {
        echo -1;
    } elseif (empty($unitData)) {
        echo 0;
    }
}

//} elseif ($do == 'checkCashTransfer') {
//    $accountid = $_POST['accountid'];
//    $chckNo = $_POST['chckNo'];
//    $typeData = $cashTransferExt->selectByAccIdAndChckNo($accountid, $chckNo);
//    if (!empty($typeData)) {
//        echo -1;
//    } elseif (empty($typeData)) {
//        echo 0;
//    }
//}

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

?>