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

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

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

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

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

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

    }
    if ($clientId != '' && $clientId == '-1') {
        $clientData = $clientDAO->load($clientId);
        $clientName = $clientData->clientname;
        $message .= "العميل :" . $clientName;
        $queryString .= ' 1 = 1  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)));
        }
        #############
        $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>");
    }

    $allData = $premiumExt->getpremiumNew($queryString);

    $totalPremium = 0;
    $restPremium = 0;
    $restPremiumTotal = 0;
    $totalValueNotPayed = 0;
    $totalValueLate = 0;
    foreach ($allData as $single){

        $totalPremium += $single->totalPremium;
        $restPremium += $single->restPremium;
        $restPremiumTotal += $single->totalPremium - $single->restPremium;
        $totalValueNotPayed += $single->totalValueNotPayed;
        $totalValueLate += $single->totalValueLate;

    }

    $smarty->assign("allData", $allData);
    $smarty->assign("totalPremium", $totalPremium);
    $smarty->assign("restPremium", $restPremium);
    $smarty->assign("restPremiumTotal", $restPremiumTotal);
    $smarty->assign("totalValueNotPayed", $totalValueNotPayed);
    $smarty->assign("totalValueLate", $totalValueLate);

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

}
elseif ($do == "details") {
    include_once("../public/authentication.php");

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

    $id = $_GET['id'];
    $alldata = $PremiumclientDAO->load($id);

    $alldataclient = $premiumExt->queryByPremiumclientIdOrderBy($id);
    $totals = $premiumExt->getpremiumtotal($id);
    $total = $totals->totalValueNotPayed - $totals->totalRestNotPayed;
//    foreach ($alldataclient as $item){
//            $total += $item->value;
//    }

    $client_id = $alldata->clientId;
    $clientData = $clientExt->getalldata($client_id);

    $clientData->typeclientid = explode(',', $clientData->typeclientid);


    $typeClient = $TypeClientDAO->queryAll();
    $azmy = array();
    foreach ($typeClient as $item) {
//var_dump($item);
        if (in_array($item->typeId, $clientData->typeclientid)) {
            array_push($azmy, $item->typeName);
        }

    }

    if (in_array('-20', $clientData->typeclientid)){
        array_push($azmy, "كل انواع العملاء");

    };

    $aaaa = implode(",", $azmy);
    $clientData->typeName = $aaaa ;

    $smarty->assign("alldataclient", $alldataclient);
    $smarty->assign("alldata", $alldata);
    $smarty->assign("clientData", $clientData);
    $smarty->assign("total", $total);
    $smarty->display("premiumNewReportview/details.html");
    $smarty->assign("clientPremium", 1);
}
//here the global templates
$smarty->display("footer.html");

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

function getClients() {
    global $clientExt;
    $clientsData = $clientExt->getclientPremium();
    return $clientsData;
}

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

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





?>