<?php

//the global file operation
include("../public/impOpreation.php");
include_once("initiateStaticSessionCommingWithCurl.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");
include("../library/uploadImages.php");
include '../library/Classes/PHPExcel/IOFactory.php';

//Programsetting
require_once('../models/dao/ProgramsettingsDAO.class.php');
require_once('../models/dto/Programsetting.class.php');
require_once('../models/mysql/ProgramsettingsMySqlDAO.class.php');
require_once('../models/mysql/ext/ProgramsettingsMySqlExtDAO.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');

//Save
require_once('../models/dao/SaveDAO.class.php');
require_once('../models/dto/Save.class.php');
require_once('../models/mysql/SaveMySqlDAO.class.php');
require_once('../models/mysql/ext/SaveMySqlExtDAO.class.php');

//Savedaily
require_once('../models/dao/SavedailyDAO.class.php');
require_once('../models/dto/Savedaily.class.php');
require_once('../models/mysql/SavedailyMySqlDAO.class.php');
require_once('../models/mysql/ext/SavedailyMySqlExtDAO.class.php');
//Employeedoctor
require_once('../models/dao/EmployeedoctorDAO.class.php');
require_once('../models/dto/Employeedoctor.class.php');
require_once('../models/mysql/EmployeedoctorMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeedoctorMySqlExtDAO.class.php');
//Employeedoctordebtandpay
require_once('../models/dao/EmployeedoctordebtandpayDAO.class.php');
require_once('../models/dto/Employeedoctordebtandpay.class.php');
require_once('../models/mysql/EmployeedoctordebtandpayMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeedoctordebtandpayMySqlExtDAO.class.php');
//Employeedoctordebtchange
require_once('../models/dao/EmployeedoctordebtchangeDAO.class.php');
require_once('../models/dto/Employeedoctordebtchange.class.php');
require_once('../models/mysql/EmployeedoctordebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeedoctordebtchangeMySqlExtDAO.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 :- productController

  OPERTATION in Controller

  1- display add form, add supplier cat and supplier with ajax
  2- add in supplier tbl
  ======================== */

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

//here goes the instances and general variables
//Accountstree
$accountsTree = new Accountstree();
$accountsTreeDAO = new AccountstreeMySqlDAO();
$accountsTreeEX = new AccountstreeMySqlExtDAO();

//Programsetting
$Programsetting = new Programsetting();
$ProgramsettingDAO = new ProgramsettingsMySqlDAO();
$ProgramsettingEX = new ProgramsettingsMySqlExtDAO();

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

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


//
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();
//Savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();

//Employeedoctor
$employeeDoctor = new Employeedoctor();
$employeeDoctorDAO = new EmployeedoctorMySqlDAO();
$employeeDoctorEX = new EmployeedoctorMySqlExtDAO();
//Employeedoctordebtandpay
$employeeDoctorDebtAndPay = new Employeedoctordebtandpay();
$employeeDoctorDebtAndPayDAO = new EmployeedoctordebtandpayMySqlDAO();
$employeeDoctorDebtAndPayEX = new EmployeedoctordebtandpayMySqlExtDAO();
//Employeedoctordebtchange
$employeeDoctorDebtChange = new Employeedoctordebtchange();
$employeeDoctorDebtChangeDAO = new EmployeedoctordebtchangeMySqlDAO();
$employeeDoctorDebtChangeEX = new EmployeedoctordebtchangeMySqlExtDAO();



$today = date("Y-m-d H:i:s");

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);

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

    //here the smarty templates
    $smarty->display("employeedoctorview/add.html");
} elseif ($do == "add") {
    include_once("../public/authentication.php");
    try {
        $flag = add();

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

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

    $shownData = $employeeDoctorDAO->queryByDel(0);
    $smarty->assign("shownData", $shownData);

    $smarty->assign("customCheck", 1);
    $smarty->display("employeedoctorview/show.html");
} elseif ($do == "deleteFinaly") {
    include_once("../public/authentication.php");
    try {
        $note = deleteFinaly();

        if ($note == 'success') {
            header("location:?do=sucess");
        } else {
            $url = "employeedoctorController.php?do=show";
            $smarty->assign('urldirect', $url);
            $note = "لقد حدث خطأ اثناء اجراء العملية";
            $smarty->assign('msgnote', $note);
            $smarty->display("notes.html");
        }
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
#
elseif ($do == "debtandpay") {
    //here the permssion check
    include_once("../public/authentication.php");

    $saveData = $mySaveEx->queryAllEX2();
    $smarty->assign("saveData", $saveData);

    //here the smarty templates
    $smarty->display("employeedoctorview/debtandpay.html");
} elseif ($do == "savedebtandpay") {
    include_once("../public/authentication.php");
    try {
        savedebtandpay();
        $url = "employeedoctorController.php?do=showdebtandpay";
        $smarty->assign('urldirect', $url);
        $note = 'تمت العملية بنجاح';
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "showdebtandpay") {
    include_once("../public/authentication.php");

    $shownData = $employeeDoctorDebtAndPayEX->queryAllEX();
    $smarty->assign("shownData", $shownData);

    //here the smarty templates
    $smarty->display("employeedoctorview/showdebtandpay.html");
} elseif ($do == "deletedebtandpay") {
    include_once("../public/authentication.php");

    try {
        deletedebtandpay();
        $url = "employeedoctorController.php?do=showdebtandpay";
        $smarty->assign('urldirect', $url);
        $note = 'تمت العملية بنجاح';
        $smarty->assign('msgnote', $note);
        $smarty->display("notes2.html");
    } catch (Exception $e) {
        header("location:?do=error");
    }
}
#
elseif ($do == "employeedoctordebtchange") {
    include_once("../public/authentication.php");

    $startDate = $_REQUEST['from'];
    $endDate = $_REQUEST['to'];

    $queryString = '';
    $message = '';
    if (isset($startDate) && !empty($startDate)) {
        $message .= " من تاريخ  " . $startDate;
        $queryString .= '  AND date( employeedoctordebtchange.debtchangedate ) >= "' . $startDate . '" ';
    }
    if (isset($endDate) && !empty($endDate)) {
        $message .= "  الى تاريخ" . $endDate;
        $queryString .= '  AND date( employeedoctordebtchange.debtchangedate ) <= "' . $endDate . '"  ';
    }
    $smarty->assign('message', $message);


    if (!empty($queryString)) {
        $shownData = $employeeDoctorDebtChangeEX->queryAllEX($queryString);
        $smarty->assign("shownData", $shownData);
    }

    //here the smarty templates
    $smarty->display("employeedoctorview/employeedoctordebtchange.html");
}
#
elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
//here the global templates
$smarty->assign("customSupplier", 1);

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


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

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

// add in supplier tbl
function add() {
    global $employeeDoctor;
    global $employeeDoctorDAO;
    global $employeeDoctorDebtChange;
    global $employeeDoctorDebtChangeDAO;

    global $today;

    //Dailyentry
    global $dailyEntry;
    global $dailyEntryDAO;

    //Dailyentrycreditor دائن
    global $dailyEntryCreditor;
    global $dailyEntryCreditorDAO;

    //Dailyentrydebtor مدين
    global $dailyEntryDebtor;
    global $dailyEntryDebtorDAO;


    $name = $_POST["txtName"];
    $debtVal = (float) $_POST["txtDebt"];
    $thetype = (int) $_POST["thetype"];

    if (!empty($name)) {
        $now = date('Y-m-d H:i:s');

        $parent = 63; //employee
        if ($thetype == 1) {
            $parent = 388; //doctor
        }
        $treeName = "سلفة " . $name;
        $treeId = addTreeElement($treeName, $parent, 3, 1, 1, '', 0, 0, 1);
        if ($debtVal != 0) {
            $dailyEntry->entryComment = 'إضافة ( ' . $treeName . ' ) ';

            //employeedoctor treeid
            $dailyEntryDebtorArray = array();
            $dailyEntryDebtor->value = $debtVal;
            $dailyEntryDebtor->accountstreeid = $treeId;
            // 7sab waseet arseda eftitahia "instead of ras elemal"
            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $debtVal;
            $dailyEntryCreditor->accountstreeid = 121;

            array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
            array_push($dailyEntryCreditorArray, $dailyEntryCreditor);

            $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 0);
            $did = $returnedData[1];
        }


        $employeeDoctor->thetype = $thetype;
        $employeeDoctor->name = $name;
        $employeeDoctor->val = $debtVal;
        $employeeDoctor->userid = $_SESSION['userid'];
        $employeeDoctor->del = 0;
        $employeeDoctor->sysdate = $now;
        $employeeDoctor->dailyentryid = $did;
        $employeeDoctor->treeId = $treeId;
        //Insert
        $employeeDoctorId = $employeeDoctorDAO->insert($employeeDoctor);


        if (!empty($employeeDoctorId)) {
            $employeeDoctorDebtChange->employeeOrDoctor = $thetype;
            $employeeDoctorDebtChange->employeeOrDoctorId = $employeeDoctorId;
            $employeeDoctorDebtChange->debtchangebefore = 0;
            $employeeDoctorDebtChange->debtchangeamount = $debtVal;
            $employeeDoctorDebtChange->debtchangetype = 1;
            $employeeDoctorDebtChange->processname = 'اضافة موظف او طبيب';
            $employeeDoctorDebtChange->debtchangemodelid = $employeeDoctorId;
            $employeeDoctorDebtChange->debtchangeafter = $debtVal;
            $employeeDoctorDebtChange->debtchangedate = $now;
            $employeeDoctorDebtChange->userid = $_SESSION['userid'];
            $employeeDoctorDebtChange->tablename = 'employeedoctorController.php';
            $employeeDoctorDebtChange->comment = '';
            $employeeDoctorDebtChange->del = 0;
            $employeeDoctorDebtChange->dailyentryid = $did;
            $employeeDoctorDebtChange->saveid = 0;

            $employeeDoctorDebtChangeDAO->insert($employeeDoctorDebtChange);
        }
    }
}

function deleteFinaly() {
    global $employeeDoctor;
    global $employeeDoctorDAO;
    global $employeeDoctorDebtChange;
    global $employeeDoctorDebtChangeDAO;


    $id = (int) $_GET["id"];
    $rowDelData = $employeeDoctorDAO->load($id);
    try {
        $rowDelData->del = 1;
        $employeeDoctorDAO->update($rowDelData);
        reverseEntryWithItsID($rowDelData->dailyentryid);
        delTreeElementById($rowDelData->treeId);
        $note = "success";
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء الحذف ";
    }


    return $note;
}

function savedebtandpay() {
    global $employeeDoctorDebtAndPayDAO;
    global $employeeDoctorDAO;
    global $mySaveRecord;
    global $employeeDoctorDebtChangeDAO;

    //Transaction
    $mytransactions = new Transaction();
    try {
        $thetype = (int) $_POST['thetype'];
        $employeeOrDoctor = (int) $_POST['employeeOrDoctor'];
        $employeeOrDoctorId = (int) $_POST['employeeOrDoctorId'];
        $val = (float) $_POST['val'];
        $comment = $_POST['comment'];
        if ($thetype == 0) {//debt "solfa"
            $saveid = (int) $_POST['saveidDebt'];

            #####daily entry
            $dailyEntry->entryComment = 'سلفة موظف او طبيب - ' . $comment;
            //من الموظف او الطبيب
            $dailyEntryDebtorArray = array();
            $dailyEntryDebtor->value = $val;
            $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
            $dailyEntryDebtor->accountstreeid = $empDoc->treeId;
            //الى الخزنة
            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $val;
            $saveData = $mySaveRecord->load($saveid);
            $dailyEntryCreditor->accountstreeid = $saveData->treeId;
            array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
            array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
            $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
            $did = $returnedData[1];


            ######
            $employeeDoctorDebtAndPay->thetype = $thetype;
            $employeeDoctorDebtAndPay->employeeOrDoctor = $employeeOrDoctor;
            $employeeDoctorDebtAndPay->employeeOrDoctorId = $employeeOrDoctorId;
            $employeeDoctorDebtAndPay->saveid = $saveid;
            $employeeDoctorDebtAndPay->paytype = 0;
            $employeeDoctorDebtAndPay->whatexpens = 0;
            $employeeDoctorDebtAndPay->val = $val;
            $employeeDoctorDebtAndPay->dailyentryid = $did;
            $employeeDoctorDebtAndPay->note = $comment;
            $employeeDoctorDebtAndPay->userid = $_SESSION['userid'];
            $employeeDoctorDebtAndPay->sysdate = date('Y-m-d H:i:s');
            $employeeDoctorDebtAndPay->del = 0;
            $employeeDoctorDebtAndPayid = $employeeDoctorDebtAndPayDAO->insert($employeeDoctorDebtAndPay);
            #####
            $saveData = $mySaveRecord->load($saveid);
            $before = $saveData->savecurrentvalue;
            $after = $before - $val;
            $saveData->savecurrentvalue = $after;
            $mySaveRecord->update($saveData, $saveData->savedetails);
            insertSavedaily($before, $val, 1, $saveid, 'سلفة موظف او طبيب - ' . $comment, $employeeDoctorDebtAndPayid, $after, 'employeedoctorController.php?do=debtandpay', date('Y-m-d H:i:s'));
            #####
            $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
            $currentDebt = $empDoc->val;
            $empDoc->val += $val;
            $employeeDoctorDAO->update($empDoc);
            if (!empty($employeeDoctorDebtAndPayid)) {
                $employeeDoctorDebtChange->employeeOrDoctor = $empDoc->thetype;
                $employeeDoctorDebtChange->employeeOrDoctorId = $employeeOrDoctorId;
                $employeeDoctorDebtChange->debtchangebefore = $currentDebt;
                $employeeDoctorDebtChange->debtchangeamount = $val;
                $employeeDoctorDebtChange->debtchangetype = 0;
                $employeeDoctorDebtChange->processname = 'سلفة موظف او طبيب';
                $employeeDoctorDebtChange->debtchangemodelid = $employeeDoctorDebtAndPayid;
                $employeeDoctorDebtChange->debtchangeafter = $empDoc->val;
                $employeeDoctorDebtChange->debtchangedate = date('Y-m-d H:i:s');
                $employeeDoctorDebtChange->userid = $_SESSION['userid'];
                $employeeDoctorDebtChange->tablename = "employeedoctorController.php?do=debtandpay";
                $employeeDoctorDebtChange->comment = $comment;
                $employeeDoctorDebtChange->del = 0;
                $employeeDoctorDebtChange->dailyentryid = $did;
                $employeeDoctorDebtChange->saveid = $saveid;

                $employeeDoctorDebtChangeDAO->insert($employeeDoctorDebtChange);
            }
        } else if ($thetype == 1) {//pay "sadad"
            $paytype = (int) $_POST['paytype'];
            $whatexpens = 0;
            if ($paytype == 0) {// direct with save
                $saveid = (int) $_POST['saveidpay'];

                #####daily entry
                $dailyEntry->entryComment = 'سداد سلفة لموظف او طبيب  - ' . $comment . '';
                //من الخزنة
                $dailyEntryDebtorArray = array();
                $dailyEntryDebtor->value = $val;
                $saveData = $mySaveRecord->load($saveid);
                $dailyEntryDebtor->accountstreeid = $saveData->treeId;
                //الى الموظف او الطبيب
                $dailyEntryCreditorArray = array();
                $dailyEntryCreditor->value = $val;
                $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
                $dailyEntryCreditor->accountstreeid = $empDoc->treeId;
                array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
                array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
                $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
                $did = $returnedData[1];
            } else if ($paytype == 1) {// with salary
                $whatexpens = (int) $_POST['whatexpens'];
                $ogoorTreeId = 0;
                $saveid = 0;
                if ($employeeOrDoctor == 0) {//employee
                    if ($whatexpens == 0) {//general expense
                        $ogoorTreeId = 417; //مصروفات عمومية //// فيه حساب أجور ومرتبات-م-ع
                    } elseif ($whatexpens == 0) {//running or working expense
                        $ogoorTreeId = 415; //مصروفات تشغيل //// حساب  أجور تشغيل ( موظفين)
                    }
                } elseif ($employeeOrDoctor == 1) {//doctor
                    $ogoorTreeId = 416; //مصروفات تشغيل ////   اجور تشغيل اطباء
                }
                #####daily entry
                $dailyEntry->entryComment = 'سداد سلفة لموظف او طبيب  - ' . $comment . '';
                //من الاجور
                $dailyEntryDebtorArray = array();
                $dailyEntryDebtor->value = $val;
                $dailyEntryDebtor->accountstreeid = $ogoorTreeId;
                //الى الموظف او الطبيب
                $dailyEntryCreditorArray = array();
                $dailyEntryCreditor->value = $val;
                $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
                $dailyEntryCreditor->accountstreeid = $empDoc->treeId;
                array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
                array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
                $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
                $did = $returnedData[1];
            }



            ######
            $employeeDoctorDebtAndPay->thetype = $thetype;
            $employeeDoctorDebtAndPay->employeeOrDoctor = $employeeOrDoctor;
            $employeeDoctorDebtAndPay->employeeOrDoctorId = $employeeOrDoctorId;
            $employeeDoctorDebtAndPay->saveid = $saveid;
            $employeeDoctorDebtAndPay->paytype = $paytype;
            $employeeDoctorDebtAndPay->whatexpens = $whatexpens;
            $employeeDoctorDebtAndPay->val = $val;
            $employeeDoctorDebtAndPay->dailyentryid = $did;
            $employeeDoctorDebtAndPay->note = $comment;
            $employeeDoctorDebtAndPay->userid = $_SESSION['userid'];
            $employeeDoctorDebtAndPay->sysdate = date('Y-m-d H:i:s');
            $employeeDoctorDebtAndPay->del = 0;
            $employeeDoctorDebtAndPayid = $employeeDoctorDebtAndPayDAO->insert($employeeDoctorDebtAndPay);

            #####
            if ($saveid > 0) {
                $saveData = $mySaveRecord->load($saveid);
                $before = $saveData->savecurrentvalue;
                $after = $before + $val;
                $saveData->savecurrentvalue = $after;
                $mySaveRecord->update($saveData, $saveData->savedetails);
                insertSavedaily($before, $val, 0, $saveid, 'سداد سلفة موظف او طبيب - ' . $comment, $employeeDoctorDebtAndPayid, $after, 'employeedoctorController.php?do=debtandpay', date('Y-m-d H:i:s'));
            }

            #####
            $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
            $currentDebt = $empDoc->val;
            $empDoc->val -= $val;
            $employeeDoctorDAO->update($empDoc);
            if (!empty($employeeDoctorDebtAndPayid)) {
                $employeeDoctorDebtChange->employeeOrDoctor = $empDoc->thetype;
                $employeeDoctorDebtChange->employeeOrDoctorId = $employeeOrDoctorId;
                $employeeDoctorDebtChange->debtchangebefore = $currentDebt;
                $employeeDoctorDebtChange->debtchangeamount = $val;
                $employeeDoctorDebtChange->debtchangetype = 1;
                $employeeDoctorDebtChange->processname = 'سداد سلفة موظف او طبيب';
                $employeeDoctorDebtChange->debtchangemodelid = $employeeDoctorDebtAndPayid;
                $employeeDoctorDebtChange->debtchangeafter = $empDoc->val;
                $employeeDoctorDebtChange->debtchangedate = date('Y-m-d H:i:s');
                $employeeDoctorDebtChange->userid = $_SESSION['userid'];
                $employeeDoctorDebtChange->tablename = "employeedoctorController.php?do=debtandpay";
                $employeeDoctorDebtChange->comment = $comment;
                $employeeDoctorDebtChange->del = 0;
                $employeeDoctorDebtChange->dailyentryid = $did;
                $employeeDoctorDebtChange->saveid = $saveid;

                $employeeDoctorDebtChangeDAO->insert($employeeDoctorDebtChange);
            }
        }

        $mytransactions->commit();
    } catch (Exception $exc) {
        $mytransactions->rollback($exc);
    }
}

function deletedebtandpay() {
    global $employeeDoctorDebtAndPayDAO;
    global $employeeDoctorDAO;
    global $mySaveRecord;
    global $employeeDoctorDebtChangeDAO;

    //Transaction
    $mytransactions = new Transaction();
    try {
        $id = (int) $_GET['id'];
        $row = $employeeDoctorDebtAndPayDAO->load($id);
        if (isset($row->id)) {
            reverseEntryWithItsID($row->dailyentryid);
            $row->del = 1;
            $employeeDoctorDebtAndPayDAO->update($row);


            $saveid = $row->saveid;
            $val = $row->val;
            $employeeDoctorDebtAndPayid = $id;
            $comment = $row->note;
            $employeeOrDoctorId = $row->employeeOrDoctorId;
            $did = $row->dailyentryid;
            if ($thetype == 0) {//debt "solfa"
                #####
                $saveData = $mySaveRecord->load($saveid);
                $before = $saveData->savecurrentvalue;
                $after = $before + $val;
                $saveData->savecurrentvalue = $after;
                $mySaveRecord->update($saveData, $saveData->savedetails);
                insertSavedaily($before, $val, 0, $saveid, 'حذف سلفة موظف او طبيب - ' . $comment, $employeeDoctorDebtAndPayid, $after, 'employeedoctorController.php?do=deletedebtandpay', date('Y-m-d H:i:s'));
                #####
                $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
                $currentDebt = $empDoc->val;
                $empDoc->val -= $val;
                $employeeDoctorDAO->update($empDoc);
                if (!empty($employeeDoctorDebtAndPayid)) {
                    $employeeDoctorDebtChange->employeeOrDoctor = $empDoc->thetype;
                    $employeeDoctorDebtChange->employeeOrDoctorId = $employeeOrDoctorId;
                    $employeeDoctorDebtChange->debtchangebefore = $currentDebt;
                    $employeeDoctorDebtChange->debtchangeamount = $val;
                    $employeeDoctorDebtChange->debtchangetype = 1;
                    $employeeDoctorDebtChange->processname = 'حذف سلفة موظف او طبيب';
                    $employeeDoctorDebtChange->debtchangemodelid = $employeeDoctorDebtAndPayid;
                    $employeeDoctorDebtChange->debtchangeafter = $empDoc->val;
                    $employeeDoctorDebtChange->debtchangedate = date('Y-m-d H:i:s');
                    $employeeDoctorDebtChange->userid = $_SESSION['userid'];
                    $employeeDoctorDebtChange->tablename = "employeedoctorController.php?do=deletedebtandpay";
                    $employeeDoctorDebtChange->comment = $comment;
                    $employeeDoctorDebtChange->del = 0;
                    $employeeDoctorDebtChange->dailyentryid = $did;
                    $employeeDoctorDebtChange->saveid = $saveid;

                    $employeeDoctorDebtChangeDAO->insert($employeeDoctorDebtChange);
                }
            } else if ($thetype == 1) {//pay "sadad"
                #####
                if ($saveid > 0) {
                    $saveData = $mySaveRecord->load($saveid);
                    $before = $saveData->savecurrentvalue;
                    $after = $before - $val;
                    $saveData->savecurrentvalue = $after;
                    $mySaveRecord->update($saveData, $saveData->savedetails);
                    insertSavedaily($before, $val, 1, $saveid, 'حذف سداد سلفة موظف او طبيب - ' . $comment, $employeeDoctorDebtAndPayid, $after, 'employeedoctorController.php?do=deletedebtandpay', date('Y-m-d H:i:s'));
                }

                #####
                $empDoc = $employeeDoctorDAO->load($employeeOrDoctorId);
                $currentDebt = $empDoc->val;
                $empDoc->val += $val;
                $employeeDoctorDAO->update($empDoc);
                if (!empty($employeeDoctorDebtAndPayid)) {
                    $employeeDoctorDebtChange->employeeOrDoctor = $empDoc->thetype;
                    $employeeDoctorDebtChange->employeeOrDoctorId = $employeeOrDoctorId;
                    $employeeDoctorDebtChange->debtchangebefore = $currentDebt;
                    $employeeDoctorDebtChange->debtchangeamount = $val;
                    $employeeDoctorDebtChange->debtchangetype = 0;
                    $employeeDoctorDebtChange->processname = 'حذف سداد سلفة موظف او طبيب';
                    $employeeDoctorDebtChange->debtchangemodelid = $employeeDoctorDebtAndPayid;
                    $employeeDoctorDebtChange->debtchangeafter = $empDoc->val;
                    $employeeDoctorDebtChange->debtchangedate = date('Y-m-d H:i:s');
                    $employeeDoctorDebtChange->userid = $_SESSION['userid'];
                    $employeeDoctorDebtChange->tablename = "employeedoctorController.php?do=deletedebtandpay";
                    $employeeDoctorDebtChange->comment = $comment;
                    $employeeDoctorDebtChange->del = 0;
                    $employeeDoctorDebtChange->dailyentryid = $did;
                    $employeeDoctorDebtChange->saveid = $saveid;

                    $employeeDoctorDebtChangeDAO->insert($employeeDoctorDebtChange);
                }
            }
        }




        $mytransactions->commit();
    } catch (Exception $exc) {
        $mytransactions->rollback($exc);
    }
}

function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename, $theDate) {

    //to use the variable out side the funcion
    global $mySavedaily;
    global $mySavedailyRecord;

    //insert in Savedaily tbl
    $mySavedaily->savedailydate = $theDate;
    $mySavedaily->userid = $_SESSION['userid'];
    $mySavedaily->savedailysavebefore = $savedailysavebefore;
    $mySavedaily->savedailychangeamount = $savedailychangeamount;
    $mySavedaily->savedailychangetype = $savedailychangetype;
    $mySavedaily->saveid = $saveid;
    $mySavedaily->processname = $processname;
    $mySavedaily->savedailymodelid = $savedailymodelid;
    $mySavedaily->savedailysaveafter = $savedailysaveafter;
    $mySavedaily->tablename = $tablename;
    $mySavedaily->clientid = 0;
    $mySavedailyRecord->insert($mySavedaily);
}

?>