<?php

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

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

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


//here the db files that include in the file
include("reportfunctions.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');


//Sellbillandrutern
require_once('../models/dao/SellbillandruternDAO.class.php');
require_once('../models/dto/Sellbillandrutern.class.php');
require_once('../models/mysql/SellbillandruternMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillandruternMySqlExtDAO.class.php');


//Billsproduct
require_once('../models/dao/BillsproductsDAO.class.php');
require_once('../models/dto/Billsproduct.class.php');
require_once('../models/mysql/BillsproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsproductsMySqlExtDAO.class.php');
//Billsreturnproduct
require_once('../models/dao/BillsreturnproductsDAO.class.php');
require_once('../models/dto/Billsreturnproduct.class.php');
require_once('../models/mysql/BillsreturnproductsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsreturnproductsMySqlExtDAO.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');

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

//Government
require_once('../models/dao/GovernmentDAO.class.php');
require_once('../models/dto/Government.class.php');
require_once('../models/mysql/GovernmentMySqlDAO.class.php');
require_once('../models/mysql/ext/GovernmentMySqlExtDAO.class.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');

$do = $_GET['do'];

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

  Controller Name :- storedetailCTRL تقرير مخزون اول مدة

  OPERTATION in Controller

  1-load save data
  2-display show form

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

//here the global templates
$smarty->display("header.html");

//here goes the instances and general variables


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

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();

//Clientarea
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();
$ClientareaEX = new ClientareaMySqlExtDAO();

// Government
$government = new Government();
$governmentDAO = new GovernmentMySqlDAO();
$governmentExt = new GovernmentMySqlExtDAO();

// GovernArea
$governArea = new Goverarea();
$governAreaDAO = new GoverareaMySqlDAO();
$governAreaExt = new GoverareaMySqlExtDAO();

//Sellbill
$mySellbill = new Sellbill();
$mySellbillRecord = new SellbillMySqlDAO();
$mySellbillEx = new SellbillMySqlExtDAO();

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

    ##########################################################################
    //prepare search tools
    ##التصنيفات
    $catDataReport = getCategoryChilds();
    $smarty->assign("catDataReport", $catDataReport[1]);

    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $user = $userDAO->load($_SESSION['userid']);
    $smarty->assign("user", $user);
    if ($user->userstoreid == 0) {
        $storeData = loadStore();
        $smarty->assign("storeData", $storeData);
    }
    if ($user->branchId == 0) {
        $branchData = loadBranch();
        $smarty->assign("branchData", $branchData);
    }
    $sellerData = loadseller();
    $smarty->assign("sellerData", $sellerData);
    $userData = loadusers();
    $smarty->assign("userData", $userData);
    ###########################################################################
    //search
    $datefrom = filter_input(INPUT_POST, 'datefrom');
    $dateto = filter_input(INPUT_POST, 'dateto');

    $messageData = '';

    $queryString = " where 1  "; //bills
    $queryStringR = " where 1 "; //billsreturn
    $queryString1 = " where 1 "; //sellbill
    $queryString1R = " where 1 "; //sellbillreturn
    $queryString1SR = " where 1 "; //sellbillandreturn
    ##date

    if (isset($_POST['clientareaid']) && !empty($_POST['clientareaid'])) {
        $areaData = $ClientareaDAO->load($_POST['clientareaid']);
        $messageData .= 'المنطقة:  ' . $areaData->name . ' ';
    }

    if (isset($datefrom) && !empty($datefrom)) {
        $queryString .= ' and date(bills.billdate)  >= "' . $datefrom . '" ';
        $queryStringR .= ' and date(billsreturn.`date`)  >= "' . $datefrom . '" ';
        $queryString1 .= ' and date(sellbill.sellbilldate)  >= "' . $datefrom . '" ';
        $queryString1R .= ' and date(returnsellbill.returnsellbilldate)  >= "' . $datefrom . '" ';
        $queryString1SR .= ' and date(sellbillandrutern.sellbilldate)  >= "' . $datefrom . '" ';

        $messageData .= 'من: ' . $datefrom . ' ';
    }
    if (isset($dateto) && !empty($dateto)) {
        $queryString .= ' and date(bills.billdate)  <= "' . $dateto . '" ';
        $queryStringR .= ' and  date(billsreturn.`date`)  <= "' . $dateto . '" ';
        $queryString1 .= ' and date(sellbill.sellbilldate)  <= "' . $dateto . '" ';
        $queryString1R .= ' and date(returnsellbill.returnsellbilldate)  <= "' . $dateto . '" ';
        $queryString1SR .= ' and date(sellbillandrutern.sellbilldate)  <= "' . $dateto . '" ';

        $messageData .= ':إلى ' . $dateto;
    }
    $smarty->assign('messageData', $messageData);

    getData($datefrom, $dateto,  $queryString, $queryStringR, $queryString1, $queryString1R, $queryString1SR);
    $smarty->assign("salesreport", 1);
    //here the smarty templates
    $smarty->display("clientwithoutbills/show.html");

    //$smarty->assign("settlementstoreshow", 1);
}
//here the global templates
$smarty->display("footer.html");

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

function getData($datefrom, $dateto,  $queryString, $queryStringR, $queryString1, $queryString1R, $queryString1SR)
{
    global $smarty;
    // m7md
    global $clientExt;
    global $mySellbillEx;
    class clientData
    {
        public $clientid;
        public $clientname;
        public $lastBillDate;
        public $lastBillId;
    }

    $existId = array();
    $allBillsDataArr = array();
    $resultsCount = 0;

    if (empty($datefrom) && empty($dateto)) {
        ##if no search make it today
        ##date
        $today = date("Y-m-d");
        $queryString .= ' and date(bills.billdate)  = "' . $today . '" ';
        $queryStringR .= ' and date(billsreturn.`date`)  = "' . $today . '" ';
        $queryString1 .= ' and  date(sellbill.sellbilldate)  = "' . $today . '" ';
        $queryString1R .= ' and date(returnsellbill.returnsellbilldate)  = "' . $today . '" ';
        $queryString1SR .= ' and date(sellbillandrutern.sellbilldate)  = "' . $today . '" ';
    }

    if (isset($_POST['clientareaid']) && !empty($_POST['clientareaid'])) {
        $areaQueryString = ' AND client.clientareaid = ' . $_POST['clientareaid'] . ' ';
    } else {
        $areaQueryString = '';
    }

    // $allBillsData = $clientExt->clientsWithoutBills($queryString1, $queryString, $queryStringR, $queryString1R, $queryString1SR);
    $allBillsData = $clientExt->clientsWithoutBillsWithArea($areaQueryString, $queryString1, $queryString, $queryStringR, $queryString1R, $queryString1SR);

    $resultsCount =  count($allBillsData);
    $smarty->assign("resultsCount", $resultsCount);

    ##فاتورة المبيعات
    foreach ($allBillsData as $key => $value) {
        if (in_array($value->clientid, $existId)) {
        } else {
            $key = -1;
            $myclient = new clientData();
            $myclient->clientid = $value->clientid;
            array_push($existId, $value->clientid);
            $myclient->clientname = $value->clientname;

            $billData = $mySellbillEx->getlastbill($value->clientid);
            $myclient->lastBillDate = $billData->sellbilldate;
            $myclient->lastBillId = $billData->sellbillid;
            array_push($allBillsDataArr, $myclient);
        }
    }
    $smarty->assign("resultsCount", $resultsCount);
    $smarty->assign("sellbillDataArr", $allBillsDataArr);
}
