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

//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');
//get the do the action

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


$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

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

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

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


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

   
    $smarty->display("bankAccountDeficitKindView/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();
        header("location:?do=error");
    }
} elseif ($do == "show") {
    include_once("../public/authentication.php");

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


    // $queryString = '';
    // if (isset($bankid) && $bankid > 0) {
    //     $queryString .= 'AND accountmovement.bankid = ' . $bankid . '';
    // }
    // if (isset($accountid) && $accountid > 0) {
    //     $queryString .= 'AND accountmovement.accountid = ' . $accountid . '';
    // }
    // 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);

    $shownData = $myAccountmovementkindRecord->queryAll();
    $smarty->assign("shownData", $shownData);

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

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

    $smarty->display("bankAccountDeficitKindView/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 $myAccountmovementkind;
    global $myAccountmovementkindRecord;
    global $today;


    $accountmovementkindname = $_POST["accountmovementkindname"];
    $accountmovementkindcomment = $_POST["accountmovementkindcomment"];

    $myAccountmovementkind->accountmovementkindname = $accountmovementkindname;
    $myAccountmovementkind->accountmovementkindcomment = $accountmovementkindcomment;
    $myAccountmovementkind->conditions = 0;
    $myAccountmovementkind->accountmovementkinddate = date('Y-m-d H:i:s');
    $myAccountmovementkind->tablename = "bankAccountDeficitkindController.php";
    $myAccountmovementkind->userid = $_SESSION['userid'];
    $accountMovementId = $myAccountmovementkindRecord->insert($myAccountmovementkind);
    return $accountMovementId;
}

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


    $data = $myAccountmovementEx->loadMovementEX($id);

    return $data;
}



?>