<?php

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

//global varable
global $showoutside;

//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

//Clientarea
require_once('../models/dao/ClientareaDAO.class.php');
require_once('../models/dto/Clientarea.class.php');
require_once('../models/mysql/ClientareaMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientareaMySqlExtDAO.class.php');

//typeClient
require_once('../models/dao/TypeClientDAO.class.php');
require_once('../models/dto/TypeClient.class.php');
require_once('../models/mysql/TypeClientMySqlDAO.class.php');
require_once('../models/mysql/ext/TypeClientMySqlExtDAO.class.php');

//Premiumclient
require_once('../models/dao/PremiumclientDAO.class.php');
require_once('../models/dto/Premiumclient.class.php');
require_once('../models/mysql/PremiumclientMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumclientMySqlExtDAO.class.php');

//Premium
require_once('../models/dao/PremiumDAO.class.php');
require_once('../models/dto/Premium.class.php');
require_once('../models/mysql/PremiumMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumMySqlExtDAO.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'];

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


$ProgramsettingDAO = new ProgramsettingsMySqlDAO();


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

//typeClient
$type = new TypeClient();
$TypeClientDAO = new TypeClientMySqlDAO();
$TypeClientEX = new TypeClientMySqlExtDAO();

//Premiumclient
$Premiumclient = new Premiumclient();
$PremiumclientDAO = new PremiumclientMySqlDAO();
$PremiumclientExt = new PremiumclientMySqlExtDAO();

//Premium
$premium = new Premium();
$premiumDAO = new PremiumMySqlDAO();
$premiumExt = new PremiumMySqlExtDAO();
/* ======================

  Controller Name :- clientsAndProductsReportController تقرير اقساط عملاء

  OPERTATION in Controller

  1- show report
  2- search with clients and products and date

  ======================== */
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

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

    $clientsData = getClients();
    $smarty->assign("clientsData", $clientsData);

    $ClientareaData = getClientarea();
    $smarty->assign("ClientareaData", $ClientareaData);

    $TypeClientData = getTypeclient();
    $smarty->assign("TypeClientData", $TypeClientData);

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

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $clientId = $_REQUEST['clientId'];
    $areaId = $_REQUEST['areaId'];
    $typeclientId = $_REQUEST['typeclientId'];
    $check = (int)filter_input(INPUT_POST, "check");
    $smarty->assign("check", $check);


    if (empty($startDate) && empty($endDate)) {
        $startDate = date('Y-m-d');
        $endDate = date('Y-m-d');
    }

    $message = '';
    $queryString = ' WHERE';

    if ($clientId != '' && $clientId != '-1') {
        $clientData = $clientDAO->load($clientId);
        $clientName = $clientData->clientname;
        $message .= "العميل :" . $clientName;
        $queryString .= ' premiumclient.client_id = ' . $clientId . ' AND';

    }

    if ($areaId != '' && $areaId != '-1') {
        $ClientareaData = $ClientareaDAO->load($areaId);
        $areaName = $ClientareaData->name;
        $message .= "/ المنطقه: " . $areaName;
        $queryString .= ' client.clientareaid = ' . $areaId . ' AND';

    }

    if ($typeclientId != '' && $typeclientId != '-1') {
        $TypeClientData = $TypeClientDAO->load($typeclientId);
        $TypeClientName = $TypeClientData->typeName;
        $message .= "/ نوع العميل: " . $TypeClientName;
        $queryString .= ' client.typeclientid  in ("",' . $typeclientId . ')  AND';

    }


    if ($startDate != '' && $endDate != '') {

        $Programsetting = $ProgramsettingDAO->load(1);
        if (isset($Programsetting->reportsPlusHours) && !empty($Programsetting->reportsPlusHours)) {
            $reportsPlusHours = $Programsetting->reportsPlusHours + 24; //24 to get the end of the day and add search hours to it
            $endDate = date('Y-m-d', strtotime('+' . $reportsPlusHours . ' hour +0 minutes', strtotime($endDate)));
            $startDate = date('Y-m-d', strtotime('+' . $Programsetting->reportsPlusHours . ' hour +0 minutes', strtotime($startDate)));
        } else {
            $endDate = $endDate . ' 23:59:59';
            $startDate = $startDate . " 00:00:00";
        }
        #############
        $queryString .= ' premium.date >= "' . $startDate . '" AND premium.date <= "' . $endDate . '" AND';

        $message .= "<br> من تاريخ: " . $startDate . " إلى تاريخ: " . $endDate;
    }
    //$message = "العميل :" . $clientName . "<br> المنتج: " . $productName . "<br> من تاريخ: " . $startDate . " إلى تاريخ: " . $endDate;
    $smarty->assign('message', $message);

    $arr = explode(' ', $queryString);
    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') {//remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
        } else if ($lastWord == 'WHERE') {//remove it
            array_pop($arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    if (!$check && $check != 1){
        $allData = $premiumExt->getAllpremium($queryString);
        $smarty->assign("allData", $allData);
    }else{
        $allData = $premiumExt->getpremiumbyclientid($queryString);
        $smarty->assign("allData", $allData);
    }


    //here the smarty templates
    $smarty->assign("clientPremium", 1);
    $smarty->display("allpremiumReportview/showNew.html");

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

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

function getClients() {
    global $clientDAO;
    $clientsData = $clientDAO->queryByCondition(0);
    return $clientsData;
}

function getClientarea() {
    global $ClientareaDAO;
    $ClientareaData = $ClientareaDAO->queryAll();
    return $ClientareaData;
}

function getTypeclient() {
    global $TypeClientEX;
    $TypeClientData = $TypeClientEX->queryAllsup();
    return $TypeClientData;
}





?>