<?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
// GOES HERE ....................
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');


//Checkdeposit
require_once('../models/dao/CheckdepositDAO.class.php');
require_once('../models/dto/Checkdeposit.class.php');
require_once('../models/mysql/CheckdepositMySqlDAO.class.php');
require_once('../models/mysql/ext/CheckdepositMySqlExtDAO.class.php');

//check withdrwal
require_once('../models/dao/CheckwithdrawalDAO.class.php');
require_once('../models/dto/Checkwithdrawal.class.php');
require_once('../models/mysql/CheckwithdrawalMySqlDAO.class.php');
require_once('../models/mysql/ext/CheckwithdrawalMySqlExtDAO.class.php');

//Cashtransfer
require_once('../models/dao/CashtransferDAO.class.php');
require_once('../models/dto/Cashtransfer.class.php');
require_once('../models/mysql/CashtransferMySqlDAO.class.php');
require_once('../models/mysql/ext/CashtransferMySqlExtDAO.class.php');

//Dated Checked
require_once('../models/dao/DatedcheckedDAO.class.php');
require_once('../models/dto/Datedchecked.class.php');
require_once('../models/mysql/DatedcheckedMySqlDAO.class.php');
require_once('../models/mysql/ext/DatedcheckedMySqlExtDAO.class.php');

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

//Sellbill
require_once('../models/dao/SellbillDAO.class.php');
require_once('../models/dto/Sellbill.class.php');
require_once('../models/mysql/SellbillMySqlDAO.class.php');
require_once('../models/mysql/ext/SellbillMySqlExtDAO.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');

//Breadcrumb
require_once("../library/breadcrumb.php");



//get the do the action
$do = $_GET['do'];



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

  Controller Name :- saveCTRL اضافة وتعديل وعرض الخزن

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

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


//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();
$accountMovementExt = new AccountmovementMySqlExtDAO();

//Breadcrumb
$breadcrumbObject = new Breadcrumb();

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

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

//Checkdeposit
$Checkdeposit = new Checkdeposit();
$CheckdepositDAO = new CheckdepositMySqlDAO();
$CheckdepositEX = new CheckdepositMySqlExtDAO();

//check withdrwal
$checkWithdrawalDAO = new CheckwithdrawalMySqlDAO;
$checkWithdrawalExtDAO = new CheckwithdrawalMySqlExtDAO;
$checkWithdrawal = new Checkwithdrawal;

//Cashtransfer
$cashTransfer = new Cashtransfer();
$cashTransferDAO = new CashtransferMySqlDAO();
$cashTransferExt = new CashtransferMySqlExtDAO();

//datedChecked
$datedChecked = new Datedchecked();
$datedCheckedDAO = new DatedcheckedMySqlDAO();
$datedCheckedEX = new DatedcheckedMySqlExtDAO();

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

//Sellbill
$mySellbill = new Sellbill();
$mySellbillRecord = new SellbillMySqlDAO();
$mySellbillEx = new SellbillMySqlExtDAO();

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

    $allAccounts = $myBankaccountEx->queryAllWithBankName();
    $smarty->assign("allAccounts", $allAccounts);

    //breadcrumb
    $breadcrumbObject->add('ادارة البنوك><a href="bankaccountController.php?do=show">حسابات البنوك</a> > اضافة حساب', 'bankaccountController.php', 0);
    //Now output the navigation.
    $breadCrumb = $breadcrumbObject->output();
    $smarty->assign("breadCrumb", $breadCrumb);



    //here the smarty templates
    $smarty->display("bankAccountOpview/show.html");
} elseif ($do == "show") {
    //here the permssion check
    include_once("../public/authentication.php");


    $allAccounts = $myBankaccountEx->queryAllWithBankName();
    $smarty->assign("allAccounts", $allAccounts);

    $accountId = filter_input(INPUT_POST, 'accountid');
    $from = filter_input(INPUT_POST, 'startDate');
    $to = filter_input(INPUT_POST, 'endDate');

    $query = '';

    if ($accountId && $accountId != -1)
        $query .= ' and accountmovement.accountid = ' . $accountId;

    if ($from)
        $query .= ' and date(accountmovement.accountmovementdate) >= "' . $from . '"';

    if ($to)
        $query .= ' and date(accountmovement.accountmovementdate) <= "' . $to . '"';

    $allMovements = $accountMovementExt->queryAllMovements($query);
    foreach ($allMovements as $movement) {
        if ($movement->tablename == "depositcheckController.php") {
            $depositData = $CheckdepositEX->loadEX($movement->accountmovementmodelid);
            $movement->clientname = $depositData->clientname;
        } elseif ($movement->tablename == "checkwithdrawalController.php") {
            $withdrawalData = $checkWithdrawalExtDAO->queryByIdExtwithsuplier($movement->accountmovementmodelid);
            $movement->clientname = $withdrawalData->suppliername;
        } elseif ($movement->tablename == "cashTransferController.php") {
            $transferData = $cashTransferExt->loadExt($movement->accountmovementmodelid);
            if ($transferData->type == 1)
                $movement->processname = "تحويل تقدي من بنك (" . $movement->bankname . " ) إلى خزنة (" . $transferData->savename . ")";
        } elseif ($movement->tablename == "datedCheckedController.php") {
            $row = $datedCheckedDAO->load($movement->accountmovementmodelid);
            if ($row->addType == 1) {
                $supID = $row->clientNum;
                $allSup = $supplierDAO->load($supID);
                $clName = $allSup->suppliername;
            } else {
                $id1 = $row->clientNum;
                $allClient = $clientDAO->load($id1);
                $clName = $allClient->clientname;
            }
            $movement->clientname = $clName;
        } elseif ($movement->tablename == "sellbillController.php") {
            $sellData = $mySellbillEx->loadExtWithClient($movement->accountmovementmodelid);
            $movement->clientname = " " . $sellData->clientname . " / " . $movement->accountmovementmodelid . " ";
        }
    }
    $smarty->assign('allMovements', $allMovements);

    $accountData = $myBankaccountRecord->load($accountId);
    $smarty->assign('currentBalance', $accountData->accountbeginingbalance);


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


    $smarty->display("bankAccountOpview/show.html");
    $smarty->assign("customCheck", 1);
}

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