<?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");
include_once("dailyentryfun.php");

//here the db files that include in the file
include("../public/include_dao.php");
//Bank
require_once('../models/dao/BankDAO.class.php');
require_once('../models/dto/Bank.class.php');
require_once('../models/mysql/BankMySqlDAO.class.php');
require_once('../models/mysql/ext/BankMySqlExtDAO.class.php');

//Bankaccount
require_once('../models/dao/BankaccountDAO.class.php');
require_once('../models/dto/Bankaccount.class.php');
require_once('../models/mysql/BankaccountMySqlDAO.class.php');
require_once('../models/mysql/ext/BankaccountMySqlExtDAO.class.php');

//Accountmovement
require_once('../models/dao/AccountmovementDAO.class.php');
require_once('../models/dto/Accountmovement.class.php');
require_once('../models/mysql/AccountmovementMySqlDAO.class.php');
require_once('../models/mysql/ext/AccountmovementMySqlExtDAO.class.php');

//AccountmovementkindDAO
require_once('../models/dao/AccountmovementkindDAO.class.php');
require_once('../models/dto/Accountmovementkind.class.php');
require_once('../models/mysql/AccountmovementkindMySqlDAO.class.php');
require_once('../models/mysql/ext/AccountmovementkindMySqlExtDAO.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");

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

  Controller Name :- clientDeficitController

  OPERTATION in Controller

  1- display add form
  2- add in clientdebtchange tbl (subtracting or adding client debt)
  3- update client dept in client tbl
  ======================== */

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

//here goes the instances and general variables
//Bank
$myBank = new Bank();
$myBankRecord = new BankMySqlDAO();
$myBankEx = new BankMySqlExtDAO();

//Bankaccount
$myBankaccount = new Bankaccount();
$myBankaccountRecord = new BankaccountMySqlDAO();
$myBankaccountEx = new BankaccountMySqlExtDAO();

//Accountmovement
$myAccountmovement = new Accountmovement();
$myAccountmovementRecord = new AccountmovementMySqlDAO();
$myAccountmovementEx = new AccountmovementMySqlExtDAO();

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

//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();


//Dailyentry
$dailyEntry = new Dailyentry();
$dailyEntryDAO = new DailyentryMySqlDAO();
$dailyEntryEX = new DailyentryMySqlExtDAO();

//Dailyentrycreditor دائن
$dailyEntryCreditor = new Dailyentrycreditor();
$dailyEntryCreditorDAO = new DailyentrycreditorMySqlDAO();
$dailyEntryCreditorEX = new DailyentrycreditorMySqlExtDAO();

//Dailyentrydebtor مدين
$dailyEntryDebtor = new Dailyentrydebtor();
$dailyEntryDebtorDAO = new DailyentrydebtorMySqlDAO();
$dailyEntryDebtorEX = new DailyentrydebtorMySqlExtDAO();


//AccountmovementkindDAO
$myAccountmovementkind = new Accountmovementkind();
$myAccountmovementkindRecord = new AccountmovementkindMySqlDAO();
$myAccountmovementkindEx = new AccountmovementkindMySqlExtDAO();

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

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

    $bank = $myBankEx->queryAllExt();
    //print_r($bank);
    $smarty->assign("bank", $bank);

    $accountmovementkind = $myAccountmovementkindEx->queryAllExt();
    $smarty->assign("accountmovementkind", $accountmovementkind);

    //here the smarty templates
    $smarty->display("bankAccountDeficitView/add.html");

    //$smarty->assign("customClientDeficit", 1);
} elseif ($do == "add") {
    include_once("../public/authentication.php");

    //Transaction
    $mytransactions = new Transaction();
    try {
        $id = add();

        $mytransactions->commit();
        header("location:?do=sucess");
    } catch (Exception $e) {
        // print_r($e);
        $mytransactions->rollback($e);
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];
    $bankid = $_REQUEST['bankid'];
    $accountid = $_REQUEST['accountid'];
    $accountmovementkindid = $_REQUEST['accountmovementkindid'];


    $queryString = '';
    if (isset($bankid) && $bankid > 0) {
        $queryString .= ' AND accountmovement.bankid = ' . $bankid . '';
    }
    if (isset($accountid) && $accountid > 0) {
        $queryString .= ' AND accountmovement.accountid = ' . $accountid . '';
    }
    if (isset($accountmovementkindid) && $accountmovementkindid > 0) {
        $queryString .= ' AND accountmovement.accountmovementkindid = ' . $accountmovementkindid . '';
    }
    if (isset($startDate) && !empty($startDate)) {
        $queryString .= ' AND DATE(accountmovement.accountmovementdate) >=  "' . $startDate . '" ';
    }
    if (isset($endDate) && !empty($endDate)) {
        $queryString .= ' AND  DATE(accountmovement.accountmovementdate) <= "' . $endDate . '" ';
    }


    if ($queryString == '') {
        $queryString .= ' AND DATE(accountmovement.accountmovementdate) >=  "' . $today . '" AND  DATE(accountmovement.accountmovementdate) <= "' . $today . '" ';
    }
    $queryString .= ' AND accountmovement.tablename = "bankAccountDeficitController.php" order by accountmovement.accountmovementid desc';
    $shownData = $myAccountmovementEx->queryAllMovements($queryString);
    $smarty->assign("shownData", $shownData);

    $bank = $myBankEx->queryAllExt();
    $smarty->assign("bank", $bank);

    $accounts = $myBankaccountEx->queryWithOrder();
    $smarty->assign("accounts", $accounts);

    $accountmovementkind = $myAccountmovementkindEx->queryAllExt();
    $smarty->assign("accountmovementkind", $accountmovementkind);

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

    $smarty->display("bankAccountDeficitView/show.html");
} elseif ($do == "editprint") {
    include_once("../public/authentication.php");

    $id = $_GET['id'];

    $loadData = edit($id);
    $smarty->assign("loadData", $loadData);

    $smarty->display("bankAccountDeficitView/editprint.html");

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

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

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

// add in clientdebtchange tbl
function add() {
    global $myBankaccount;
    global $myBankaccountRecord;
    global $myAccountmovement;
    global $myAccountmovementRecord;
    global $today;


    //tree
    global $accountsTreeDAO;
    global $accountsTree;

    //Dailyentry
    global $dailyEntry;
    global $dailyEntryDAO;

    //Dailyentrycreditor دائن
    global $dailyEntryCreditor;
    global $dailyEntryCreditorDAO;

    //Dailyentrydebtor مدين
    global $dailyEntryDebtor;
    global $dailyEntryDebtorDAO;

    $bankid = $_POST["ddlBank"];
    $accountid = $_POST["accountid"];
    $changetype = (int) $_POST["changetype"];
    $changeamount = $_POST["changeamount"];
    $accountmovementcomment = $_POST["accountmovementcomment"];
    $accountmovementkindid = $_POST["accountmovementkindid"];

    $account = $myBankaccountRecord->load($accountid);
    $balanceBefore = $account->accountbeginingbalance;

    //$myAccountmovement->processname = $comment;
    if (isset($changetype) && $changetype == 1) {
        $myAccountmovement->processname = "معالجة عجز حساب بالخصم";
        $balanceAfter = $balanceBefore - $changeamount;
        //
        $dailyEntry->entryComment = 'تسويه بالمصاريف البنكيه - معالجة عجز حساب بالخصم';
        $dailyEntryDebtor->accountstreeid = 65; // من المصروفات العمومية - مصاريف بنكية
        $dailyEntryCreditor->accountstreeid = $account->treeId;
    } elseif (isset($changetype) && $changetype == 0) {
        $myAccountmovement->processname = "معالجة عجز حساب بالزيادة";
        $balanceAfter = $balanceBefore + $changeamount;
        //
        $dailyEntry->entryComment = 'تسويه بالفوائد البنكيه - معالجة عجز حساب بالزيادة';
        $dailyEntryDebtor->accountstreeid = $account->treeId;
        $dailyEntryCreditor->accountstreeid = 393; //ايرادات اخرى - فوائد بنكية
    }



    $myAccountmovement->accountmovementbefore = $balanceBefore;
    $myAccountmovement->accountmovementamount = $changeamount;
    $myAccountmovement->accountmovementafter = $balanceAfter;
    $myAccountmovement->accountmovementdate = date('Y-m-d H:i:s');
    $myAccountmovement->accountmovementtype = $changetype;
    $myAccountmovement->accountmovementmodelid = 0;
    $myAccountmovement->accountid = $accountid;
    $myAccountmovement->tablename = "bankAccountDeficitController.php";
    $myAccountmovement->userid = $_SESSION['userid'];
    $myAccountmovement->bankid = $bankid;
    $myAccountmovement->accountmovementcomment = $accountmovementcomment;
    $myAccountmovement->accountmovementkindid = $accountmovementkindid;



    //Insert
    $accountMovementId = $myAccountmovementRecord->insert($myAccountmovement);

    //update current dept in client tbl
    $account->accountbeginingbalance = $balanceAfter;
    $account->userid = $_SESSION['userid'];
    $account->accountdate = $myAccountmovement->accountmovementdate;
    $myBankaccountRecord->update($account, $account->comment);


//////////////// start Al-Asl

    $dailyEntryDebtorArray = array();
    $dailyEntryDebtor->value = $changeamount;
    //
    $dailyEntryCreditorArray = array();
    $dailyEntryCreditor->value = $changeamount;
///////////// end Ras L maaaal
    array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
    array_push($dailyEntryCreditorArray, $dailyEntryCreditor);

    $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);

    $did = $returnedData[1];

    return $accountMovementId;
}

function edit($id) {
    global $myAccountmovementRecord;
    global $myAccountmovementEx;

    //$data = $myAccountmovementRecord->load($id);
    //$data = $myAccountmovementEx->loadMovement("bankAccountDeficitController.php", $id);
    $data = $myAccountmovementEx->loadMovementEX($id);

    return $data;
}

?>