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

//here the db files that include in the file
// GOES HERE ....................
include("../public/include_dao.php");

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

include_once("dailyentryfun.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 :-storeCTRL

  OPERTATION in Controller

  1-display add form
  2- add in storerawmaterails tble
  3-display sucess or error
  4-display show and delete
  5-display edit
  ======================== */

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



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


$employee = new Employee();
$employeeDAO = new EmployeeMySqlDAO();
$employeeEX = new EmployeeMySqlExtDAO();


$storeDetail = new Storedetail();
$storeDetailDAO = new StoredetailMySqlDAO();
$storeDetailEX = new StoredetailMySqlExtDAO();

$mySave = new Save();
$mySaveDAO = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();


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

//Dailyentry
$dailyEntry = new Dailyentry();

//Dailyentrycreditor دائن
$dailyEntryCreditor = new Dailyentrycreditor();

//Dailyentrydebtor مدين
$dailyEntryDebtor = new Dailyentrydebtor();

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


$save = new Save();
$saveDAO = new SaveMySqlDAO();
$saveEx = new SaveMySqlExtDAO();

//Bank
$myBank = new Bank();
$myBankDAO = new BankMySqlDAO();
$myBankEx = new BankMySqlExtDAO();


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

$bind = new Bind();
$bindDAO = new BindMySqlDAO();
$bindEx = new BindMySqlExtDAO();


$userdata = $userDAO->load($_SESSION['userid']);
$smarty->assign("userdata", $userdata);



//Breadcrumb
$breadcrumbObject = new Breadcrumb();

//create object from user
## dispaly  add form
if (!isset($_GET['do'])) {
    include_once("../public/authentication.php");

    ## employee
    $employee = $employeeDAO->queryByConditions(0);
    $smarty->assign('employees', $employee);

    ## save
    $save = $saveDAO->queryByConditions(0);
    $smarty->assign('saves', $save);

    $bankData = $myBankDAO->queryByCondition(0);
    $smarty->assign("bankData", $bankData);

    $smarty->display("bindview/add.html"); //the template page
}

## add bill
elseif ($_GET['do'] == "add") {
    include_once("../public/authentication.php");
    //add();
    try {
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

## edit bill details
elseif ($_GET['do'] == "edit") {
    include_once("../public/authentication.php");

    $bindId = filter_input(INPUT_GET, "id");
    $bind = $bindDAO->load($bindId);
    $smarty->assign('bind', $bind);

    $myBankaccount = $myBankaccountEx->queryByBankidAndaccountdele($bind->bankid);
    $smarty->assign('bankAccounts', $myBankaccount);

    ## employee
    $employee = $employeeDAO->queryByConditions(0);
    $smarty->assign('employees', $employee);

    ## save
    $save = $saveDAO->queryByConditions(0);
    $smarty->assign('saves', $save);

    $bankData = $myBankDAO->queryByCondition(0);
    $smarty->assign("bankData", $bankData);

    $smarty->display("bindview/edit.html"); //the template page
}

## show bills
elseif ($do == "show") {
    //here the permssion check

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

    ## employee
    $employee = $employeeDAO->queryByConditions(0);
    $smarty->assign('employees', $employee);
    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);

    $smarty->display("bindview/show.html");
}


## delete bill
elseif ($_GET['do'] == "delete") {
    include_once("../public/authentication.php");

    $billId = filter_input(INPUT_GET, "id");
    $bills = $billsDAO->load($billId);
    $bills->deleted = 1;
    try {
        $billsDAO->update($bills);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}






## update  bill
elseif ($_GET['do'] == "update") {
    include_once("../public/authentication.php");

    try {
        update();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}

##
elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
}

##
elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}






$smarty->assign("reports", 1);



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

#################### add  bill ####################################################################################################

function saveDailyEntry($value, $bindtype, $employeeId, $id, $bankaccount, $bindId) {
    global $dailyEntry;      ## Dailyentry
    global $accountsTreeDAO;
    global $employee;
    global $employeeDAO;
    global $mySave;
    global $mySaveDAO;
    global $myBank;
    global $myBankDAO;
    global $myBankaccountDAO;
    global $bindDAO;

    $mytransactions = new Transaction();

    $dailyEntryDebtorArray = array();
    $dailyEntryCreditorArray = array();

    $dailyEntry->dDateTime = date('Y-m-d h:i:s');
    $dailyEntry->entryComment = '';
    $dailyEntry->fromFlag = 2;


## edditor
##############################################################################################################################################
    $employee = $employeeDAO->load($employeeId);
    $accountsTree33 = $accountsTreeDAO->queryByName('عهدة ' . $employee->employeeName);
    if (count($accountsTree33) > 0) {
        $treeId3 = $accountsTree33[0]->id;
    } else {
        $treeId3 = addTreeElement('عهدة ' . $employee->employeeName, 46, 3, 0, 1, '', 0, 0);
    }

    $dailyEntryDebtor2->value = $value;
    $dailyEntryDebtor2->accountstreeid = $treeId3;
    array_push($dailyEntryDebtorArray, $dailyEntryDebtor2);



    ## creditor
##############################################################################################################################################
    if ($bindtype == 1) {  ## 5azna
        $mySave = $mySaveDAO->load($id);
        if ($mySave->treeId > 0) {
            $treeId2 = $mySave->treeId;
        } else {
            $treeId2 = addTreeElement($mySave->savename, 20, 3, 0, 1, '', 0, 0);
        }

        $dailyEntryCreditor->value = $value;
        $dailyEntryCreditor->accountstreeid = $treeId2;
        array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
################################################################################################################################################
    } else {    ## bank
        ## no add for bank in tree now
        $bankData = $myBankDAO->load($id);
        $bankAccountData = $myBankaccountDAO->load($bankaccount);
        if ($bankAccountData->treeId > 0) {
            $treeId2 = $bankAccountData->treeId;
        } else {
//            $treeId2 = addTreeElement($bankData->bankname, 21, 3, 0, 1, '', 0, 0);
            $treeId2 = addTreeElement("$bankAccountData->accountname / $bankData->bankname", 38, 3, 0, 1, '', 0, 0);
        }

        $dailyEntryCreditor->value = $value;
        $dailyEntryCreditor->accountstreeid = $treeId2;
        array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
    }

####################################################################################################################################################

    try {
        $data = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);

        $dailyEntryId = $data[1];
        $bind = $bindDAO->load($bindId);
        $bind->dailyentryid = $dailyEntryId;
        $bindDAO->update($bind);
        $mytransactions->commit();

        $message = 'تمت العملية بنجاح';
    } catch (Exception $ex) {
        $mytransactions->rollback();
        $message = 'عفوا لقد حدث خطأ';
    }
}

function add() {
    global $bind;
    global $bindDAO;

    $employee = filter_input(INPUT_POST, "employee");
    $bindtype = filter_input(INPUT_POST, "bindtype");
    $save = filter_input(INPUT_POST, "save");
    $saveValue = filter_input(INPUT_POST, "saveValue");
    $bank = filter_input(INPUT_POST, "bank");
    $account = filter_input(INPUT_POST, "account");
    $ckekNo = filter_input(INPUT_POST, "ckekNo");
    $benefitName = filter_input(INPUT_POST, "benefitName");
    $ckekValue = filter_input(INPUT_POST, "ckekValue");

    $value = 0;
    $id = 0;


    ## external
    if ($bindtype == 1) {
        $bind->saveid = $save;
        $bind->savevalue = $saveValue;
        $value = $saveValue;
        $id = $save;
    }
    ## internal
    else {
        $bind->bankid = $bank;
        $bind->bankaccount = $account;
        $bind->ckekno = $ckekNo;
        $bind->benefitname = $benefitName;
        $bind->ckekvalue = $ckekValue;
        $value = $ckekValue;
        $id = $bank;
    }


    $bind->employeeid = $employee;
    $bind->bindtype = $bindtype;
    $bind->userid = $_SESSION['userid'];
    $bind->binddate = date('Y-m-d');
    $bind->deleted = 0;

    $bindId = $bindDAO->insert($bind);
    saveDailyEntry($value, $bindtype, $employee, $id, $bind->bankaccount, $bindId);
}

function update() {
    global $bind;
    global $bindDAO;


    $bindId = filter_input(INPUT_POST, "bindId");
    $bind = $bindDAO->load($bindId);

    $employee = filter_input(INPUT_POST, "employee");
    $bindtype = filter_input(INPUT_POST, "bindtype");
    $save = filter_input(INPUT_POST, "save");
    $saveValue = filter_input(INPUT_POST, "saveValue");
    $bank = filter_input(INPUT_POST, "bank");
    $account = filter_input(INPUT_POST, "account");
    $ckekNo = filter_input(INPUT_POST, "ckekNo");
    $benefitName = filter_input(INPUT_POST, "benefitName");
    $ckekValue = filter_input(INPUT_POST, "ckekValue");

    $value = 0;
    $id = 0;


    ## external
    if ($bindtype == 1) {
        $bind->saveid = $save;
        $bind->savevalue = $saveValue;
        $value = $saveValue;
        $id = $save;
    }
    ## internal
    else {
        $bind->bankid = $bank;
        $bind->bankaccount = $account;
        $bind->ckekno = $ckekNo;
        $bind->benefitname = $benefitName;
        $bind->ckekvalue = $ckekValue;
        $value = $ckekValue;
        $id = $bank;
    }


    $bind->employeeid = $employee;
    $bind->bindtype = $bindtype;
    $bind->userid = $_SESSION['userid'];
    //$bind->binddate = date('Y-m-d');
    $bind->deleted = 0;

    $bindDAO->update($bind);
    reverseEntryWithItsID($bind->dailyentryid);
    saveDailyEntry($value, $bindtype, $employee, $id, $bind->bankaccount, $bindId);
}

?>