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

//Billoperation
require_once('../models/dao/BilloperationDAO.class.php');
require_once('../models/dto/Billoperation.class.php');
require_once('../models/mysql/BilloperationMySqlDAO.class.php');
require_once('../models/mysql/ext/BilloperationMySqlExtDAO.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
//Billoperation
$billOperation = new Billoperation();
$billOperationDAO = new BilloperationMySqlDAO();
$billOperationEX = new BilloperationMySqlExtDAO();

$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("billoperationsview/add.html");

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

    try {
        $id = add();

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

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

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

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

    $id = $_GET['id'];

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

    $smarty->display("billoperationsview/edit.html");

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

    try {
        $id = update();

        header("location:?do=sucess");
    } catch (Exception $e) {
        // print_r($e);
        header("location:?do=error");
    }
} elseif ($do == "delete") {
    include_once("../public/authentication.php");

    try {
        delete();

        header("location:?do=sucess");
    } catch (Exception $e) {
        // print_r($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");
}

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

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

// add in clientdebtchange tbl
function add() {
    global $billOperation;
    global $billOperationDAO;


    $name = $_POST["name"];

    $billOperation->name = $name;
    $billOperation->del = 0;
    $billOperation->sydate = date('Y-m-d H:i:s');
    $billOperation->userid = $_SESSION['userid'];
    $billOperation->treeId = (int) addTreeElement($name, 409, 2, 0, 1, '', 0, 0, 2);
    $id = $billOperationDAO->insert($billOperation);
    return $id;
}

function edit($id) {
    global $billOperationDAO;

    $data = $billOperationDAO->load($id);

    return $data;
}

function update() {
    global $billOperation;
    global $billOperationDAO;


    $id = (int) $_POST["id"];
    $name = $_POST["name"];

    $billOperation->id = $id;
    $billOperation->name = $name;
    $billOperation->del = 0;
    $billOperation->sydate = date('Y-m-d H:i:s');
    $billOperation->userid = $_SESSION['userid'];
    $billOperationDAO->update($billOperation);

    $oldTree = $accountsTreeDAO->load($treeId);

    $oldTree->name = $name;
    $oldTree->customName = $name;
    editTreeElement($oldTree);



    return $id;
}

function delete() {
    global $billOperationDAO;

    $id = (int) $_GET["id"];

    $billOperationDAO->delete($id);
}

?>