<?php
// تقرير لعميل معين
//تقارير العملاء (الدفعات)
//the global file operation
include("../public/impOpreation.php");
include("../library/uploadImages.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
//include("../public/include_dao.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');

//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.class.php');

//Billname
require_once('../models/dao/BillnameDAO.class.php');
require_once('../models/dto/Billname.class.php');
require_once('../models/mysql/BillnameMySqlDAO.class.php');
require_once('../models/mysql/ext/BillnameMySqlExtDAO.class.php');

//Billsetting
require_once('../models/dao/BillsettingsDAO.class.php');
require_once('../models/dto/Billsetting.class.php');
require_once('../models/mysql/BillsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/BillsettingsMySqlExtDAO.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');

//Unit
require_once('../models/dao/UnitDAO.class.php');
require_once('../models/dto/Unit.class.php');
require_once('../models/mysql/UnitMySqlDAO.class.php');
require_once('../models/mysql/ext/UnitMySqlExtDAO.class.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');

//Faida
require_once('../models/dao/FaidaDAO.class.php');
require_once('../models/dto/Faida.class.php');
require_once('../models/mysql/FaidaMySqlDAO.class.php');
require_once('../models/mysql/ext/FaidaMySqlExtDAO.class.php');


//Faida_product
require_once('../models/dao/FaidaproductDAO.class.php');
require_once('../models/dto/Faidaproduct.class.php');
require_once('../models/mysql/FaidaproductMySqlDAO.class.php');
require_once('../models/mysql/ext/FaidaproductMySqlExtDAO.class.php');

//Faida_Dirctory
require_once('../models/dao/FaidadirectorDAO.class.php');
require_once('../models/dto/Faidadirector.class.php');
require_once('../models/mysql/FaidadirectorMySqlDAO.class.php');
require_once('../models/mysql/ext/FaidadirectorMySqlExtDAO.class.php');

//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.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');

//Store
$myStoreRecord = new StoreMySqlDAO();

//Programsetting
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$programSettingExt = new ProgramsettingsMySqlExtDAO();

//Billname
$myBillnameRecord = new BillnameMySqlDAO();
$myBillnameEx = new BillnameMySqlExtDAO();

//Billsetting
$myBillsettingEx = new BillsettingsMySqlExtDAO();

//Client
$myClientRecord = new ClientMySqlDAO();
$myClient = new Client();
$myClientEx = new ClientMySqlExtDAO();

//user
$user = new User();
$userDAO = new UserMySqlDAO();
$userEX = new UserMySqlExtDAO();

//Faida
$myFaida = new Faida();
$myFaidaDAO = new FaidaMySqlDAO();
$myFaidaEx = new FaidaMySqlExtDAO();

//Faida_product
$myFaidaProduct = new Faidaproduct();
$myFaidaProductDAO = new FaidaproductMySqlDAO();
$myFaidaProductEx = new FaidaproductMySqlExtDAO();


//Faida_Dirctory
$myFaidaDirctory = new Faidadirector();
$myFaidaDirctoryDAO = new FaidadirectorMySqlDAO();
$myFaidaDirctoryEx = new FaidadirectorMySqlExtDAO();

//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

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

//Unit
$myUnitRecord = new UnitMySqlDAO();
//Save
//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");

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

  Controller Name :- clientReportsController

  OPERTATION in Controller

  1- تقرير مديونية عميل
  ======================== */

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

//here goes the instances and general variables


$today = date("Y-m-d");

//check and use the condition that suite this action



if (empty($do) || $do == "show") {

    include_once("../public/authentication.php");

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

    $billnameData = $myBillnameRecord->load(3);
    $smarty->assign("billnameData", $billnameData);

    $billsettingsData = loadBillProperty(3);
    $smarty->assign("billsettingsData", $billsettingsData);

    $smarty->assign("billnameid", 3);

    //get units
    $unitsData = loadUnit();
    $smarty->assign("unitsData", $unitsData);

    $allfaida = show();

    $smarty->assign("allfaida", $allfaida);

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


    $smarty->display("faidaview/showDir.html");

}
elseif ($do == "approval") {

    $faida_id = filter_input(INPUT_POST, 'faida_id');
    $comment = filter_input(INPUT_POST, 'comment');

    try {

        $myFaidaDirctorys = $myFaidaDirctoryDAO->queryByFaidaId($faida_id);
        if (count($myFaidaDirctorys) > 0){
            foreach ($myFaidaDirctorys as $item) {
                $myFaidaDirctory->id = $item->id;
                $myFaidaDirctory->faidaId = $item->faidaId;
                $myFaidaDirctory->comment = $item->comment;
                $myFaidaDirctory->conditions = 0;

                $myFaidaDirctoryDAO->update($myFaidaDirctory);
                $approval = $item->id;
            }

        }else{
            $myFaidaDirctory->faidaId = $faida_id ;
            $myFaidaDirctory->comment = $comment;
            $myFaidaDirctory->conditions = 0;
            $approval = $myFaidaDirctoryDAO->insert($myFaidaDirctory);

        }
//die();



        if (!empty($approval)) {

            $myFaidaDirctory = $myFaidaDirctoryDAO->load($approval);
            // $faidaData = $myFaidaProductEx->queryWithFaidaId($myFaidaDirctory->faidaId);
            // $faidatotal = 0;
            // foreach ($faidaData as $item) {
            //     $faidatotal += $item->faidaproducttotalprice;
            // }

            $faida = $myFaidaEx->queryWithFaidaId($myFaidaDirctory->faidaId);
            $myFaida->clientId = $faida->clientId ;
            $myFaida->premmium = $faida->premmium;
            $myFaida->installment_duration = $faida->Installment_duration;
            $myFaida->total_amount = $faida->total_amount;
            $myFaida->comment = $faida->comment;
            $myFaida->user_id = $_SESSION['userid'];
            $myFaida->storeId = $faida->storeId;
            $myFaida->conditions = 1;
            $myFaida->presenter = $faida->presenter;
            $myFaida->id = $faida->id;
            $myFaidaDAO->update($myFaida);


            $rowData = $myClientRecord->load($faida->clientId);
            $total = $rowData->clientdebt + $faida->total_amount;
            $myClient->clientcode = $rowData->clientcode;
            $myClient->clientaddress = $rowData->clientaddress;
            $myClient->clientdate = $rowData->clientdate;
            $myClient->clientdebt = $total;
            $myClient->clientdetails = $rowData->clientdetails;
            $myClient->clientmobile = $rowData->clientmobile;
            $myClient->clientname = $rowData->clientname;
            $myClient->clientphone = $rowData->clientphone;
            $myClient->conditions = $rowData->conditions;
            $myClient->userid = $rowData->userid;
            $myClient->clientareaid = $rowData->clientareaid;
            $myClient->clientStoreIds = $rowData->clientStoreIds;
            $myClient->dailyentryid = $rowData->dailyentryid;
            $myClient->typeclientid = $rowData->typeclientid;
            $myClient->clientid = $rowData->clientid;

            $myClientRecord->update($myClient);


            $rowDataClient = $clientDeptChangeExt->selectLastClientDept($faida->clientId);
            // var_dump($rowDataClient);
            foreach ($rowDataClient as $data) {
                $clientDeptChange->clientdebtchangeafter = $faida->total_amount + $data->clientdebtchangeafter;
                $clientDeptChange->clientdebtchangeamount = $faida->total_amount;
                $clientDeptChange->totalOperationCost = $faida->total_amount;
                $clientDeptChange->clientdebtchangebefore = $data->clientdebtchangeafter;
                $clientDeptChange->clientdebtchangedate = $today;
                $clientDeptChange->clientdebtchangemodelid = $data->clientid;
                $clientDeptChange->clientdebtchangetype = 0;
                $clientDeptChange->clientid = $data->clientid;
                $clientDeptChange->comment = "";
                $clientDeptChange->processname = "إضافة فايده لعميل ";
                $clientDeptChange->tablename = "FaidaController.php";
                $clientDeptChange->userid = $_SESSION['userid'];
                $clientDeptChange->dailyentryid = 0;
                $clientDeptChangeDAO->insert($clientDeptChange);
            }

        }


        // here the function that do the action
        header("location:?do=sucess");

    } catch (Exception $e) {
        echo $e;
//        header("location:?do=error");
    }
    $smarty->display("faidaview/showDir.html");

}
elseif ($do == "refusal") {

    $faida_id = filter_input(INPUT_POST, 'faida_id');
    $comment = filter_input(INPUT_POST, 'comment');

    try {
        $myFaidaDirctory->faidaId = $faida_id ;
        $myFaidaDirctory->comment = $comment;
        $myFaidaDirctory->conditions = 1;

        $approval = $myFaidaDirctoryDAO->insert($myFaidaDirctory);

        if (!empty($approval)) {

            $myFaidaDirctory = $myFaidaDirctoryDAO->load($approval);
            // $faidaData = $myFaidaProductEx->queryWithFaidaId($myFaidaDirctory->faidaId);
            // $faidatotal = 0;
            // foreach ($faidaData as $item) {
            //     $faidatotal += $item->faidaproducttotalprice;
            // }

            $faida = $myFaidaEx->queryWithFaidaId($myFaidaDirctory->faidaId);
            $myFaida->clientId = $faida->clientId ;
            $myFaida->premmium = $faida->premmium;
            $myFaida->installment_duration = $faida->Installment_duration;
            $myFaida->total_amount = $faida->total_amount;
            $myFaida->comment = $faida->comment;
            $myFaida->user_id = $_SESSION['userid'];
            $myFaida->storeId = $faida->storeId;
            $myFaida->conditions = 2;
            $myFaida->presenter = $faida->presenter;
            $myFaida->id = $faida->id;
            $myFaidaDAO->update($myFaida);

        }


        // here the function that do the action
        header("location:?do=sucess");

    } catch (Exception $e) {
//        echo $e;
        header("location:?do=error");
    }
    $smarty->display("faidaview/showDir.html");

}
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("faidaScript", 1);
$smarty->assign("customValidation", 1);
$smarty->assign("customsellbill", 1);


$smarty->display("footer.html");


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




function show(){

    global $myClientEx;
    global $myFaidaEx;
    global $userDAO;

    $allfaidas = $myFaidaEx->queryAllByConditions();
    foreach ($allfaidas as $item){
        $user = $userDAO->load($item->user_id);
        $item->username = $user->username;


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

    }
    return $allfaidas;
}
// select from Billname tbl
function loadBillProperty($billnameid) {
    global $myBillsettingEx;

    // select all data from Billname
    $billsettingsData = $myBillsettingEx->queryWithBillnameId($billnameid);
    return $billsettingsData;
}

function loadClient() {
    global $myClientRecord;

    $clientData = $myClientRecord->queryByCondition(0);
    return $clientData;
}

function loadUnit() {
    global $myUnitRecord;

    $unitsData = $myUnitRecord->queryByCondition(0);
    return $unitsData;
}

?>