HEX
Server: Apache
System: Linux server1.royalgt4.com 4.18.0-553.89.1.lve.el8.x86_64 #1 SMP Wed Dec 10 13:58:50 UTC 2025 x86_64
User: mostafedeg (1125)
PHP: 5.6.40
Disabled: mail,passthru,parse_ini_file,show_source,eval,assert,pcntl_exec,dl,putenv,proc_open,popen
Upload Files
File: /home/mostafedeg/public_html/erp/controllers/bindsettlement.php
<?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");

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

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

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

//Dailyentry
$dailyEntry = new Dailyentry();

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

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

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

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

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


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

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

$project = new Project();
$projectDAO = new ProjectMySqlDAO();
$projectEx = new ProjectMySqlExtDAO();

$expensesType = new Expensestype();
$expensesTypeDAO = new ExpensestypeMySqlDAO();
$expensesTypeExt = new ExpensestypeMySqlExtDAO();

$bindSettlement = new Bindsettlement();
$bindSettlementDAO = new BindsettlementMySqlDAO();
$bindSettlementEx = new BindsettlementMySqlExtDAO();

//Costcenter
$costCenter = new Costcenter();
$costCenterDAO = new CostcenterMySqlDAO();
$costCenterEX = new CostcenterMySqlExtDAO();


$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 binds
    $binds = $bindEx->getAllEmployeeWithBind();
    $smarty->assign('binds', $binds);

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

    ## expensesType
    $expensesType = $expensesTypeDAO->queryByCondition(0); //not deleted
    $smarty->assign("expensesType", $expensesType);

    ## Project
    $costCenter = $costCenterDAO->queryByCondation(0); //not deleted
    $smarty->assign("projects", $costCenter);

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

## add bill
elseif ($_GET['do'] == "add") {
    include_once("../public/authentication.php");
    $itr = filter_input(INPUT_POST, "itr");
    $employeeId = filter_input(INPUT_POST, "employee");
    $bindSettlement = $bindSettlementEx->getLastGroupNo($employeeId);
    $group = $bindSettlement->groupno + 1;
//    add();
    try {
        for ($i = 1; $i <= $itr; $i++) {
            add($i, $group);
        }

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

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

    $groupNo = filter_input(INPUT_GET, "groupno");       ## groupno
    $employeeId = filter_input(INPUT_GET, "employeeid");       ## $employeeId


    $bindSettlement = $bindSettlementEx->queryByEmployeeidAndDeletedAndGroupno($employeeId, 0, $groupNo);
    $smarty->assign('bindSettlements', $bindSettlement);
    $smarty->assign('itr', count($bindSettlement));

    $sumBindS = 0;

    foreach ($bindSettlement as $bindSett) {
        $sumBindS = $sumBindS + $bindSett->value;
    }


############################################################# #####################################################################################
    $bs = $bindEx->queryByEmployeeidAndDeleted($employeeId, 0);
    $bSs = $bindSettlementEx->queryByEmployeeidAndDeleted($employeeId, 0);

    $sumBind = 0;
    $sumSettlement = 0 - $sumBindS;

    foreach ($bSs as $bS) {
        $sumSettlement = $sumSettlement + $bS->value;
    }


    foreach ($bs as $b) {
        ## external
        if ($b->bindtype == 1) {
            $sumBind = $sumBind + $b->savevalue;
        }
        ## internal
        else {
            $sumBind = $sumBind + $b->ckekvalue;
        }
    }

    $sumNet = $sumBind - $sumSettlement;

    $smarty->assign('sumNet', $sumNet);
    $smarty->assign('sumSettlement', $sumSettlement);
    $smarty->assign('sumBind', $sumBind);
##################################################################################################################################
    ## employee binds
    $binds = $bindEx->getAllEmployeeWithBind();
    $smarty->assign('binds', $binds);

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

    ## expensesType
    $expensesType = $expensesTypeDAO->queryByCondition(0); //not deleted
    $smarty->assign("expensesType", $expensesType);

    ## Project
    $project = $projectEx->queryByFinishAndDel(0, 0); //not deleted
    $smarty->assign("projects", $project);



    $smarty->display("bindSettlementview/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("bindSettlementview/show.html");
}

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

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

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

    $smarty->display("bindSettlementview/showreport.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");
    //update();
    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, $bindSettlementType, $employeeId, $id, $bindSettlementId, $project) {
    global $dailyEntry;      ## Dailyentry
    global $accountsTreeDAO;
    global $employee;
    global $employeeDAO;
    global $mySave;
    global $mySaveDAO;
    global $expensesType;
    global $expensesTypeDAO;
    global $bindSettlementDAO;

    $mytransactions = new Transaction();

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


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


    ##    creditor
##############################################################################################################################################
    $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);
    }

    $dailyEntryCreditor->value = $value;
    $dailyEntryCreditor->accountstreeid = $treeId3;

    if ($project && $project != -1) {
        $dailyEntryCreditor->pluginComment = "تسوية عهدة";
        $dailyEntryCreditor->pluginControllerName = "bindsettlement.php";
        $dailyEntryCreditor->costcenterid = $project;
        $dailyEntryCreditor->costcenterType = 13;
    }



    array_push($dailyEntryCreditorArray, $dailyEntryCreditor);



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

        $dailyEntryDebtor->value = $value;
        $dailyEntryDebtor->accountstreeid = $treeId2;
        array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
################################################################################################################################################
    } else {    ## masrouf
        $expensesType = $expensesTypeDAO->load($id);
        if ($expensesType->treeId > 0) {
            $treeId2 = $expensesType->treeId;
        } else {
            $treeId2 = addTreeElement($expensesType->expensestypename, 14, 3, 0, 1, '', 0, 0);
        }

        $dailyEntryDebtor->value = $value;
        $dailyEntryDebtor->accountstreeid = $treeId2;
        array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
    }

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

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

        $dailyEntryId = $data[1];
        $bindSettlement = $bindSettlementDAO->load($bindSettlementId);
        $bindSettlement->dailyentryid = $dailyEntryId;
        $bindSettlementDAO->update($bindSettlement);
        $mytransactions->commit();

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

function add($itr, $group) {
    global $bindSettlement;
    global $bindSettlementDAO;

    $employee = filter_input(INPUT_POST, "employee");
    $bindSettlementType = filter_input(INPUT_POST, "bindSettlement" . $itr);
    $expensestype = filter_input(INPUT_POST, "expensestype" . $itr);
    $save = filter_input(INPUT_POST, "save" . $itr);
    $project = filter_input(INPUT_POST, "project" . $itr);
    $value = filter_input(INPUT_POST, "value" . $itr);
    $comment = filter_input(INPUT_POST, "comment" . $itr);

    ## save
    if ($bindSettlementType == 1) {
        $bindSettlement->settlementwayid = $save;
        $id = $save;
    }
    ## مصروفات
    else {

        $bindSettlement->settlementwayid = $expensestype;
        $id = $expensestype;
    }

    $bindSettlement->groupno = $group;
    $bindSettlement->employeeid = $employee;
    $bindSettlement->userid = $_SESSION['userid'];
    $bindSettlement->settlementway = $bindSettlementType;

    $bindSettlement->projectid = $project;
    $bindSettlement->value = $value;
    $bindSettlement->comment = $comment;
    $bindSettlement->deleted = 0;
    $bindSettlement->dailyentryid = 0;

    $bindSettlementId = $bindSettlementDAO->insert($bindSettlement);
    saveDailyEntry($value, $bindSettlementType, $employee, $id, $bindSettlementId, $project);
}

function update() {
    $itr = filter_input(INPUT_POST, "itr");
    $groupNo = filter_input(INPUT_POST, "groupNo");

    for ($i = 1; $i <= $itr; $i++) {
        $bindSettlementId = filter_input(INPUT_POST, "bindSettleId" . $i);
        if ($bindSettlementId) {
            updateOne($bindSettlementId, $groupNo, $i);
        } else {
            add($i, $groupNo);
        }
    }
}

function updateOne($bindSettlementId, $groupNo, $itr) {
    global $bindSettlement;
    global $bindSettlementDAO;



    $bindSettlement = $bindSettlementDAO->load($bindSettlementId);

    $employee = filter_input(INPUT_POST, "employee");
    $bindSettlementType = filter_input(INPUT_POST, "bindSettlement" . $itr);
    $expensestype = filter_input(INPUT_POST, "expensestype" . $itr);
    $save = filter_input(INPUT_POST, "save" . $itr);
    $project = filter_input(INPUT_POST, "project" . $itr);
    $value = filter_input(INPUT_POST, "value" . $itr);
    $comment = filter_input(INPUT_POST, "comment" . $itr);


    ## save
    if ($bindSettlementType == 1) {
        $bindSettlement->settlementwayid = $save;
        $id = $save;
    }
    ## مصروفات
    else {

        $bindSettlement->settlementwayid = $expensestype;
        $id = $expensestype;
    }

    $bindSettlement->groupno = $groupNo;
    $bindSettlement->employeeid = $employee;
    $bindSettlement->userid = $_SESSION['userid'];
    $bindSettlement->settlementway = $bindSettlementType;

    $bindSettlement->projectid = $project;
    $bindSettlement->value = $value;
    $bindSettlement->comment = $comment;
    $bindSettlement->deleted = 0;

    $bindSettlementDAO->update($bindSettlement);
    reverseEntryWithItsID($bindSettlement->dailyentryid);
    saveDailyEntry($value, $bindSettlementType, $employee, $id, $bindSettlementId, $project);
}

?>