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


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

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

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

//Premiumautomatic
require_once('../models/dao/PremiumautomaticDAO.class.php');
require_once('../models/dto/Premiumautomatic.class.php');
require_once('../models/mysql/PremiumautomaticMySqlDAO.class.php');
require_once('../models/mysql/ext/PremiumautomaticMySqlExtDAO.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');

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

$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");

//here the global templates
$smarty->display("header.html");
//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

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

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

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

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

//Premiumautomatic
$Premiumautomatic = new Premiumautomatic();
$PremiumautomaticDAO = new PremiumautomaticMySqlDAO();
$PremiumautomaticExt = new PremiumautomaticMySqlExtDAO();

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

$today = date("Y-m-d H:i:s");


//check and use the condetion that suite this action
if (empty($do)) {
    include_once("../public/authentication.php");

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

    $smarty->display("premiumview/add.html");
    $smarty->assign("clientPremium", 1);

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

    try {
        // here the function that do the action
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
//        echo $e;
        header("location:?do=error");
    }
}
elseif ($do == "show") {
    include_once("../public/authentication.php");

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

    $allpremiums = show();

    $smarty->assign("allpremiums", $allpremiums);
    $smarty->display("premiumview/show.html");
    $smarty->assign("clientPremium", 1);
}
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);
    foreach ($alldataclient as $item){
        if ($item->conditions == 0 && $item->payed == 0){
            $total += $item->value - $item->rest;
        }
    }

    $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("premiumview/details.html");
    $smarty->assign("clientPremium", 1);
}
elseif ($do == "addPremiumformreport") {
    include_once("../public/authentication.php");

    try {
        // here the function that do the action
        addPremiumformreport();
        header("location:?do=sucess");
    } catch (Exception $e) {
        echo $e;
//        header("location:?do=error");
    }
}
elseif ($do == "edit") {
    include_once("../public/authentication.php");

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



    $id = $_GET['id'];

    $alldataclient = $PremiumclientDAO->load($id);


    $alldata = $premiumExt->queryByPremiumclientConditionPayed($id);
    foreach ($alldata as $item){
        $total += $item->value - $item->rest;
    }
    $itrNo = count($alldata);

    $client_id = $alldataclient->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("total", $total);
    $smarty->assign("clientData", $clientData);
    $smarty->assign("itrNo", $itrNo);
    $smarty->display("premiumview/edit.html");
    $smarty->assign("clientPremium", 1);
}
elseif ($do == "update") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        update($today);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }

}
elseif ($do == "updatefromreport") {
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        updatefromreport();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
elseif ($do == "delete") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        delete();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
elseif ($do == "deletefromRemport") {


    include_once("../public/authentication.php");
    //add();
    try {
        // here the function that do the action
        deletefromRemport();
        header("location:allpremiumReportController.php");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
//elseif ($do == "addAutomatic") {
//
//
//    include_once("../public/authentication.php");
//    //add();
//    try {
//        // here the function that do the action
//        addAutomatic();
//        header("location:?do=sucess");
//    } catch (Exception $e) {
////        echo $e;
//        header("location:?do=error");
//    }
//}
elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
}
elseif ($do == "error") {

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

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


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


function add() {
    global $Premiumclient;
    global $PremiumclientDAO;

            $client = filter_input(INPUT_POST, 'clientID');

            $Premiumclient->clientId = $client ;
            $Premiumclient->userid = $_SESSION['userid'];
            $Premiumclient->date = date("Y-m-d h:i:sa");
            $premiumClientid = $PremiumclientDAO->insert($Premiumclient);
            addPremium($premiumClientid);
}

function addPremium($premiumClientid) {
    global $premium;
    global $premiumDAO;


    $premiumItr = filter_input(INPUT_POST, 'premiumItr');
    for ($i = 1; $i <= $premiumItr; $i++) {
        $date = filter_input(INPUT_POST, 'date' . $i);
        $amount = filter_input(INPUT_POST, 'Amount' . $i);
        if (!empty($date)) {
            $premium->date = $date;
            $premium->value = $amount;
            $premium->premiumclientId = $premiumClientid;
            $premium->payed = 0;
            $premium->rest = 0;
            $premium->conditions = 0;
            $premiumDAO->insert($premium);
        }
    }

}

function show() {
    global $PremiumclientDAO;
    global $clientExt;
    global $userDAO;

    $allpremiums = $PremiumclientDAO->queryAll();
    foreach ($allpremiums as $item){
        $user = $userDAO->load($item->userid);
        $item->username = $user->username;


        $client_id = $item->clientId;
        $clientData = $clientExt->loadExt($client_id);
        foreach ($clientData as $single)
        $item->clientname = $single->clientname;

    }
    return $allpremiums;
}

///////////////////////////////update///////////////////////////////////
function update($today) {

    global $Premiumclient;
    global $PremiumclientDAO;
    global $premiumDAO;





    $Premiumcliented = filter_input(INPUT_POST, 'Premiumclient');
    $client = filter_input(INPUT_POST, 'clientID');

    $Premiumclient->id = $Premiumcliented ;
    $Premiumclient->clientId = $client ;
    $Premiumclient->userid = $_SESSION['userid'];
    $Premiumclient->date = $today;
    $PremiumclientDAO->update($Premiumclient);


    $premiumDAO->deleteByPremiumclientId($Premiumclient->id);

    addPremium($Premiumclient->id);

}

///////////////////////////////////////////delete///////////////////////////////////////
function delete() {
    global $PremiumclientDAO;
    global $premiumDAO;

    $id = $_GET['id'];
    $premiumDAO->deleteByPremiumclientId($id);
    $PremiumclientDAO->delete($id);
}

//تأجيل
function addPremiumformreport() {
    global $premium;
    global $premiumDAO;

        $id = filter_input(INPUT_POST, 'id');
        $premiumclientid = filter_input(INPUT_POST, 'premiumclientId');
        $date = filter_input(INPUT_POST, 'date');
        $amount = filter_input(INPUT_POST, 'Amount');
        $delayDate = filter_input(INPUT_POST, 'delayDate');


        $premium->id = $id;
        $premium->date = $date;
        $premium->value = $amount;
        $premium->premiumclientId = $premiumclientid ;
        $premium->payed = 0 ;
        $premium->rest = 0 ;
        $premium->conditions = 1 ;
        $premiumDAO->update($premium);


        $premium->date = $delayDate;
        $premium->value = $amount;
        $premium->premiumclientId = $premiumclientid ;
        $premium->payed = 0 ;
        $premium->rest = 0 ;
        $premium->conditions = 0 ;
        $premiumDAO->insert($premium);


}

//تعديل
function updatefromreport() {

    global $premium;
    global $premiumDAO;





    $id = filter_input(INPUT_POST, 'id');
    $Premiumcliented = filter_input(INPUT_POST, 'premiumclientId');
    $date = filter_input(INPUT_POST, 'date');
    $Amount = filter_input(INPUT_POST, 'Amount');
    $conditions = filter_input(INPUT_POST, 'conditions');
    $payed = filter_input(INPUT_POST, 'payed');

    $premium->id = $id ;
    $premium->premiumclientId = $Premiumcliented ;
    $premium->date = $date ;
    $premium->value = $Amount;
    $premium->conditions = $conditions;
    $premium->payed = $payed;

    $premiumDAO->update($premium);


}

//حذف
function deletefromRemport() {
    global $premiumExt;
    global $premiumDAO;
    global $PremiumclientDAO;

    $id = $_GET['id'];
    $premium = $premiumDAO->load($id);
    $premiumDAO->delete($id);
    $count = $premiumExt->selectallbyid($premium->premiumclientId);
    if ($count->countPremium <= 0){
        $PremiumclientDAO->delete($premium->premiumclientId);
    }
}


//function addAutomatic() {
//    global $Premiumautomatic;
//    global $PremiumautomaticDAO;
//
//    $first_payment = filter_input(INPUT_POST, 'first_payment');
//    $payment_dir = filter_input(INPUT_POST, 'payment_dir');
//    $payment_Per = filter_input(INPUT_POST, 'payment_Per');
//    $rest_payment = filter_input(INPUT_POST, 'rest_payment');
//    $installment_every_date = filter_input(INPUT_POST, 'installment_every_date');
//    $installment_nos = filter_input(INPUT_POST, 'installment_nos');
//    $installment_months = filter_input(INPUT_POST, 'installment_months');
//    $installment_days = filter_input(INPUT_POST, 'installment_days');
//    $client = filter_input(INPUT_POST, 'clientID');
//
//
//    $Premiumautomatic->totalinstallment = $first_payment ;
//    $Premiumautomatic->presenterDir = $payment_dir ;
//    $Premiumautomatic->presenterPer = $payment_Per ;
//    $Premiumautomatic->remainAmount = $rest_payment ;
//    $Premiumautomatic->date = $installment_every_date ;
//    $Premiumautomatic->installmentNos = $installment_nos ;
//    $Premiumautomatic->installmentMonths = $installment_months ;
//    $Premiumautomatic->installmentDays = $installment_days ;
//    $Premiumautomatic->clientId = $client ;
//    $Premiumautomatic->userid = $_SESSION['userid'];
//    $Premiumautomatic_id = $PremiumautomaticDAO->insert($Premiumautomatic);
//    addPremiumAutomatic($Premiumautomatic_id);
//}


//function addPremiumAutomatic($Premiumautomatic_id) {
//    global $premium;
//    global $premiumDAO;
//
//
//    $premiumItr = filter_input(INPUT_POST, 'installmentitr');
//    for ($i = 1; $i <= $premiumItr; $i++) {
//        $date = filter_input(INPUT_POST, 'installment_payment_date_' . $i);
//        $amount = filter_input(INPUT_POST, 'installment_payment_' . $i);
//
//        $premium->date = $date;
//        $premium->value = $amount;
//        $premium->premiumclientId = $Premiumautomatic_id ;
//        $premium->payed = 0;
//        $premium->conditions = 0 ;
//        $premiumDAO->insert($premium);
//    }
//
//}