<?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");
include("reportfunctions.php");

//here the db files that include in the file
include("../public/include_dao.php");
////
require_once('../models/dao/QuickprofitclientDAO.class.php');
require_once('../models/dto/Quickprofitclient.class.php');
require_once('../models/mysql/QuickprofitclientMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitclientMySqlExtDAO.class.php');

require_once('../models/dao/QuickprofitdayDAO.class.php');
require_once('../models/dto/Quickprofitday.class.php');
require_once('../models/mysql/QuickprofitdayMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitdayMySqlExtDAO.class.php');

require_once('../models/dao/QuickprofitgeneralDAO.class.php');
require_once('../models/dto/Quickprofitgeneral.class.php');
require_once('../models/mysql/QuickprofitgeneralMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitgeneralMySqlExtDAO.class.php');

require_once('../models/dao/QuickprofitproductDAO.class.php');
require_once('../models/dto/Quickprofitproduct.class.php');
require_once('../models/mysql/QuickprofitproductMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitproductMySqlExtDAO.class.php');

require_once('../models/dao/QuickprofitstoreDAO.class.php');
require_once('../models/dto/Quickprofitstore.class.php');
require_once('../models/mysql/QuickprofitstoreMySqlDAO.class.php');
require_once('../models/mysql/ext/QuickprofitstoreMySqlExtDAO.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');

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');
//get the do the action
$do = $_GET['do'];

/* ======================
  Controller Name :- quickprofitreportCTRL تقرير أرباح سريع

  OPERTATION in Controller

  1- search with product name or date
  ======================== */
////
$quickProfitClient = new Quickprofitclient();
$quickProfitClientDAO = new QuickprofitclientMySqlDAO();
$quickProfitClientEX = new QuickprofitclientMySqlExtDAO();
//
$quickProfitDay = new Quickprofitday();
$quickProfitDayDAO = new QuickprofitdayMySqlDAO();
$quickProfitDayEX = new QuickprofitdayMySqlExtDAO();
//
$quickProfitGeneral = new Quickprofitgeneral();
$quickProfitGeneralDAO = new QuickprofitgeneralMySqlDAO();
$quickProfitGeneralEX = new QuickprofitgeneralMySqlExtDAO();
//
$quickProfitProduct = new Quickprofitproduct();
$quickProfitProductDAO = new QuickprofitproductMySqlDAO();
$quickProfitProductEX = new QuickprofitproductMySqlExtDAO();
//
$quickProfitStore = new Quickprofitstore();
$quickProfitStoreDAO = new QuickprofitstoreMySqlDAO();
$quickProfitStoreEX = new QuickprofitstoreMySqlExtDAO();
////
//Store
$store = new Store();
$storeDAO = new StoreMySqlDAO();
$storeEX = new StoreMySqlExtDAO();
//Productcat
$productCatDAO = new ProductcatMySqlDAO();
$productCatEX = $productCatExt = new ProductcatMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientEX = new ClientMySqlExtDAO();

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
//check and use the condetion that suite this action
$lastLevelCatIDS = array();
$catsIDS = "";
//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");
    //get general profits
    $quickProfitGeneral = $quickProfitGeneralDAO->load(1);
    $smarty->assign('emptyDates', 1);
    $smarty->assign('quickProfitGeneral', $quickProfitGeneral);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/general.html");
}
##
elseif ($do == "day") {
    include_once("../public/authentication.php");
    //
    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    if (empty($from) && empty($to)) {
        //get general profits
        $quickProfitGeneral = $quickProfitGeneralDAO->load(1);
        $smarty->assign('emptyDates', 1);
        $smarty->assign('quickProfitGeneral', $quickProfitGeneral);
    } else {
        ##date
        $queryString = '';
        if (isset($from) && !empty($from)) {
            $queryString .= 'and  quickprofitday.theDate  >= "' . $from . '" ';
        }
        if (isset($to) && !empty($to)) {
            $queryString .= 'and  quickprofitday.theDate  <= "' . $to . '" ';
        }
        $quickProfitDay = $quickProfitDayEX->queryAllEX($queryString);
        $smarty->assign('emptyDates', 0);
        $smarty->assign('quickProfitDay', $quickProfitDay);
    }
    //here the smarty templates
    $smarty->display("quickprofitReportsView/day.html");
    //$smarty->assign("settlementstoreshow", 1);
}
##
elseif ($do == "storeday") {
    include_once("../public/authentication.php");

    $storeData = $storeDAO->queryAll();
    $smarty->assign('storeData', $storeData);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    //
    $storeId = filter_input(INPUT_POST, "storeId");
    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    ##date
    $queryString = '';
    if (isset($storeId) && !empty($storeId)) {
        $queryString .= 'and  quickprofitstore.storeId  = "' . $storeId . '" ';
    }
    if (isset($from) && !empty($from)) {
        $queryString .= 'and  quickprofitstore.theDate  >= "' . $from . '" ';
    }
    if (isset($to) && !empty($to)) {
        $queryString .= 'and  quickprofitstore.theDate  <= "' . $to . '" ';
    }
    $quickProfitDay = $quickProfitStoreEX->queryAllEX($queryString);
    $smarty->assign('emptyDates', 0);
    $smarty->assign('quickProfitDay', $quickProfitDay);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/storeDay.html");
    //$smarty->assign("settlementstoreshow", 1);
}
##
elseif ($do == "productday") {
    include_once("../public/authentication.php");

    $Programsetting = $ProgramsettingDAO->load(1);
    $smarty->assign('Programsetting', $Programsetting);
    //
    $productId = filter_input(INPUT_POST, "productId");
    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    ##date
    $queryString = '';
    if (isset($productId) && !empty($productId)) {
        $queryString .= 'and  quickprofitproduct.productId  = "' . $productId . '" ';
    }
    if (isset($from) && !empty($from)) {
        $queryString .= 'and  quickprofitproduct.theDate  >= "' . $from . '" ';
    }
    if (isset($to) && !empty($to)) {
        $queryString .= 'and  quickprofitproduct.theDate  <= "' . $to . '" ';
    }
    $quickProfitDay = $quickProfitProductEX->queryAllEX($queryString);
    $smarty->assign('emptyDates', 0);
    $smarty->assign('quickProfitDay', $quickProfitDay);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/productDay.html");
    //$smarty->assign("settlementstoreshow", 1);
}
##
elseif ($do == "catday") {
    include_once("../public/authentication.php");

    $Programsetting = $ProgramsettingDAO->load(1);
    $smarty->assign('Programsetting', $Programsetting);
    //
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $lastCatId = filter_input(INPUT_POST, "lastCatId");
    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    ##date
    $queryString = '';
    if (isset($lastCatId) && !empty($lastCatId)) {
        $queryString .= 'and  productcat.productCatId  = "' . $lastCatId . '" ';
    }
    if (isset($from) && !empty($from)) {
        $queryString .= 'and  quickprofitproduct.theDate  >= "' . $from . '" ';
    }
    if (isset($to) && !empty($to)) {
        $queryString .= 'and  quickprofitproduct.theDate  <= "' . $to . '" ';
    }
    $quickProfitDay = $quickProfitProductEX->queryAllEX($queryString);
    $smarty->assign('emptyDates', 0);
    $smarty->assign('quickProfitDay', $quickProfitDay);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/catDay.html");
    //$smarty->assign("settlementstoreshow", 1);
}
##
elseif ($do == "allcatday") {
    include_once("../public/authentication.php");

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

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);
    ##التصنيفات
    $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
    }

    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    ##date
    $queryString = '';

    if (isset($catsIDS) && !empty($catsIDS)) {
        $queryString .= 'and  productcat.productCatId  in (' . $catsIDS . ') ';
    }
    if (isset($from) && !empty($from)) {
        $queryString .= 'and  quickprofitproduct.theDate  >= "' . $from . '" ';
    }
    if (isset($to) && !empty($to)) {
        $queryString .= 'and  quickprofitproduct.theDate  <= "' . $to . '" ';
    }
    $quickProfitDay = $quickProfitProductEX->queryAllEXGroupByCatAndDate($queryString);
    $smarty->assign('emptyDates', 0);
    $smarty->assign('quickProfitDay', $quickProfitDay);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/allCatDay.html");
    //$smarty->assign("settlementstoreshow", 1);
}
##
elseif ($do == "clientday") {
    include_once("../public/authentication.php");

//    $Programsetting = $ProgramsettingDAO->load(1);
//    $smarty->assign('Programsetting', $Programsetting);
    //
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $clientId = filter_input(INPUT_POST, "clientId");
    $from = filter_input(INPUT_POST, "from");
    $to = filter_input(INPUT_POST, "to");
    $buyPriceType = filter_input(INPUT_POST, "buyPriceType");

    if (!isset($buyPriceType) || empty($buyPriceType)) {
        $buyPriceType = "first";
    }
    $smarty->assign('buyPriceType', $buyPriceType);

    ##date
    $queryString = '';
    if (isset($clientId) && !empty($clientId)) {
        $queryString .= 'and  quickprofitclient.clientId  = "' . $clientId . '" ';
    }
    if (isset($from) && !empty($from)) {
        $queryString .= 'and  quickprofitclient.theDate  >= "' . $from . '" ';
    }
    if (isset($to) && !empty($to)) {
        $queryString .= 'and  quickprofitclient.theDate  <= "' . $to . '" ';
    }
    $quickProfitDay = $quickProfitClientEX->queryAllEX($queryString);
    $smarty->assign('emptyDates', 0);
    $smarty->assign('quickProfitDay', $quickProfitDay);
    //here the smarty templates
    $smarty->display("quickprofitReportsView/clientDay.html");
    //$smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");

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

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

?>