<?php

// كنترول المرتبات
//
//the global file operation
include("../public/impOpreation.php");
include_once("dailyentryfun.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
include("../public/include_dao.php");
require_once('../models/sql/Connection.class.php');
require_once('../models/sql/ConnectionFactory.class.php');
require_once('../models/sql/ConnectionProperty.class.php');
require_once('../models/sql/QueryExecutor.class.php');
require_once('../models/sql/Transaction.class.php');
require_once('../models/sql/SqlQuery.class.php');
require_once('../models/core/ArrayList.class.php');
require_once('../models/dao/DAOFactory.class.php');

require_once('../models/dao/EmployeepersonnelDAO.class.php');
require_once('../models/dto/Employeepersonnel.class.php');
require_once('../models/mysql/EmployeepersonnelMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeepersonnelMySqlExtDAO.class.php');

require_once('../models/dao/SalaryreportDAO.class.php');
require_once('../models/dto/Salaryreport.class.php');
require_once('../models/mysql/SalaryreportMySqlDAO.class.php');
require_once('../models/mysql/ext/SalaryreportMySqlExtDAO.class.php');

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

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

require_once('../models/dao/EmployeeDAO.class.php');
require_once('../models/dto/Employee.class.php');
require_once('../models/mysql/EmployeeMySqlDAO.class.php');
require_once('../models/mysql/ext/EmployeeMySqlExtDAO.class.php');

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

require_once('../models/dao/CostcenterDAO.class.php');
require_once('../models/dto/Costcenter.class.php');
require_once('../models/mysql/CostcenterMySqlDAO.class.php');
require_once('../models/mysql/ext/CostcenterMySqlExtDAO.class.php');

require_once('../models/dao/CostcenterdetailDAO.class.php');
require_once('../models/dto/Costcenterdetail.class.php');
require_once('../models/mysql/CostcenterdetailMySqlDAO.class.php');
require_once('../models/mysql/ext/CostcenterdetailMySqlExtDAO.class.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');







$do = $_GET['do'];
if (isset($_POST["fromCtrl"]) && isset($_POST["fromCtrl"]) == "employeePersonal") {
    //no header its curl request
} else {
    $smarty->display("header.html");
}


$langs = $_SESSION['erp_lang'];
include_once("../views/languages/$langs/success.php");
include_once("../views/languages/$langs/error.php");



//creat object from employeePersonal
$EmployeepersonnelDAO = new EmployeepersonnelMySqlDAO();
$Employeepersonnel = new Employeepersonnel();
$EmployeepersonnelEX = new EmployeepersonnelMySqlExtDAO();

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

//creat object from save
$saveDAo = new SaveMySqlDAO();
$save = new Save();
$saveEX = new SaveMySqlExtDAO();

//creat object from Savedaily
$SavedailyDAO = new SavedailyMySqlDAO();
$Savedaily = new Savedaily();
$SavedailyEX = new SavedailyMySqlExtDAO();

// create object from employee
$myEmpDAO = new EmployeeMySqlDAO();
$myEmp = new Employee();
$myEmpEX = new EmployeeMySqlExtDAO();
//create object from salary report
$SalaryreportDAO = new SalaryreportMySqlDAO();
$Salaryreport = new Salaryreport();
$SalaryreportEX = new SalaryreportMySqlExtDAO();

//create object from savedailay
$SavedailyDAO = new SavedailyMySqlDAO();
$Savedaily = new Savedaily();
$SavedailyEX = new SavedailyMySqlExtDAO();

$Costcenterdetail = new Costcenterdetail();
$CostcenterdetailDAO = new CostcenterdetailMySqlDAO();
$CostcenterdetailEX = new CostcenterdetailMySqlExtDAO();

//Costcenter
$Costcenter = new Costcenter();
$CostcenterDAO = new CostcenterMySqlDAO();
$CostcenterEX = new CostcenterMySqlExtDAO();

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



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

//user
$userDAO = new UserMySqlDAO();

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
//dispaly  add form
if (!isset($_GET['do'])) {

    $userData = $userDAO->load($_SESSION['userid']);
    if ($userData->searchinonesave == 1) {
        $userSave = $mySaveEx->loadEx($_SESSION['saveid']);
        $smarty->assign('userSave', $userSave);
    } else {
        if (!isset($userData->saveids) || $userData->saveids == 0)
            $queryString = '';
        else
            $queryString = ' and saveid in (' . $userData->saveids . ')';

        $userSaves = $mySaveEx->queryWithConditionWithQueryString($queryString);
        $smarty->assign('userSaves', $userSaves);
    }

    $allCostcenter = $CostcenterEX->queryAllnotdel();
    $smarty->assign("allCostcenter", $allCostcenter);


    $today = date("Y-m-d");
    $from = $_POST['from'];
    if ($from != "") {
        $start = $from . '-01';
        $end = $from . '-31';

        if ($_SESSION['branchId'] > 0)
            $queryString = ' AND branchid = ' . $_SESSION['branchId'];


        // $allEmp = $myEmpEX->queryAllemployeeSalary($from, $end);
        $allEmp = $myEmpEX->queryAllemployeeSalaryWithQueryString($from, $end, $queryString);
        $i = 0;
        $allEmpsalary = array();
        $z = array();
        $x[] = array();
        foreach ($allEmp as $emp) {
            $id = ($emp->employeeId);
            $allsalarys = $myEmpEX->getall($id);
            if ($allsalarys->employeeBouns == null || $allsalarys->employeeBouns == "") {
                $allsalarys->employeeBouns = 0;
            }

            if ($allsalarys->empdept == null || $allsalarys->empdept == "" || $allsalarys->empdept == 0) {
                $allsalarys->empdept = 0;
            }



            if ($allsalarys->Reward == null || $allsalarys->Reward == "") {
                $allsalarys->Reward = 0;
            }
            if ($allsalarys->Discount == null || $allsalarys->Discount == "") {
                $allsalarys->Discount = 0;
            }

            if ($allsalarys->Withdraw == null || $allsalarys->Withdraw == "") {
                $allsalarys->Withdraw = 0;
            }

            if ($allsalarys->Absence == null || $allsalarys->Absence == "") {
                $allsalarys->Absence = 0;
            }

            if ($allsalarys->Repayment == null || $allsalarys->Repayment == "") {
                $allsalarys->Repayment = 0;
            }
            if ($allsalarys->permission == null || $allsalarys->permission == "") {
                $allsalarys->permission = 0;
            }



            $x[0] = $allsalarys->employeeId;
            $x[1] = $allsalarys->employeeName;
            $x[2] = $allsalarys->employeeSalary;

            $x[3] = $allsalarys->employeeBouns;
            $x[4] = $allsalarys->Reward;
            $x[5] = $allsalarys->Discount;

            $x[6] = $allsalarys->Withdraw;
            $x[7] = $allsalarys->Absence;
            $x[9] = $allsalarys->Repayment;
            $x[10] = ($allsalarys->empdept) - ($allsalarys->Repayment);
            $x[12] = $allsalarys->permission;

            $lastSalary = $SalaryreportEX->getEmpLastRow($id);
            $prevDebt = 0;
            if ($lastSalary->salaryreportnet < 0)
                $prevDebt += $lastSalary->salaryreportnet;
            $x[11] = $prevDebt;

            $total = (($allsalarys->employeeBouns) + ($allsalarys->Reward) + ($allsalarys->employeeSalary)) -
                (($allsalarys->Discount) + ($allsalarys->Withdraw) + ($allsalarys->Absence) + ($allsalarys->Repayment) + ($allsalarys->permission)) + $prevDebt;

            $x[8] = $total;

            $allEmpsalary[$i] = $x;
            //$z[$i]= $allsalarys->employeeId;
            $i++;

            //print_r($x);
        } //end of foreach
        //  print_r($y);
        $smarty->assign('allEmpsalary', $allEmpsalary);
        //$smarty->assign('z', $z);
        $smarty->assign('salarymonthdate', $from);
    }

    $today = date("Y-m");
    $smarty->assign('today', $today);
    $smarty->display('salaryReportview/show.html');
}


//show all كل اللى قبض
elseif ($do == "show") {

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

    show();

    $smarty->display('salaryReportview/showfinsh.html');
} elseif ($do == "editprint") {

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

    editprint();

    $smarty->display('salaryReportview/editprint.html');
} elseif ($do == "Receipt") {
    //include_once("../public/authentication.php");
    if (isset($_POST["fromCtrl"]) && $_POST["fromCtrl"] == "employeePersonal") {
        try {
            Receipt();
            echo 1;
        } catch (Exception $e) {
            echo -1;
        }
    } else {
        try {
            Receipt();
            header("location:?do=sucess");
        } catch (Exception $e) {
            //echo $e;
            header("location:?do=error");
        }
    }
} elseif ($do == "delete") {

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

    try {
        delete();

        header("location:?do=sucess");
    } catch (Exception $e) {
        //echo $e;
        header("location:?do=error");
    }
} elseif ($_GET['do'] == "sucess") {
    $smarty->display("succes.html");
} elseif ($_GET['do'] == "error") {
    $smarty->display("error.html");
}

$smarty->assign("customValidation", 1);
if (isset($_POST["fromCtrl"]) && isset($_POST["fromCtrl"]) == "employeePersonal") {
    //no header its curl request
} else {
    $smarty->display("footer.html");
}

////////////////////////////save///////////////////////////
function show()
{

    global $smarty;
    global $Employeepersonnel;
    global $EmployeepersonnelDAO;
    global $EmployeepersonnelEX;
    global $Salaryreport;
    global $SalaryreportDAO;
    global $SalaryreportEX;
    global $myEmpEX;
    global $saveEX;

    $allemployeedata = allemployee();
    $smarty->assign('allemployeedata', $allemployeedata);

    $queryString = 'WHERE employee.conditions = 0 AND';

    $empName = $_REQUEST["empName"];
    $from = $_REQUEST["from"];

    if (isset($empName) && $empName != '-1') {
        $queryString .= ' salaryreport.employeeid = ' . $empName . ' AND';
    }

    if (isset($from) && $from != '') {
        $queryString .= ' salaryreport.salarymonthdate = "' . $from . '" AND';
    }

    if ($_SESSION['branchId'] > 0)
        $queryString = ' AND branchid = ' . $_SESSION['branchId'];

    $arr = explode(' ', $queryString);

    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') {
            //remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
            //$queryString .= ' ';
        } else if ($lastWord == 'WHERE') {
            //remove it
            array_pop($arr);
            //$queryString = implode(' ', $arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    $allmydata = $SalaryreportEX->queryAllEX($queryString);
    foreach ($allmydata as $data) {
        $lastSalary = $SalaryreportEX->getEmpLastRowPrev($data->employeeid, $data->salaryreportid);
        $prevDebt = 0;
        if ($lastSalary->salaryreportnet < 0)
            $prevDebt += $lastSalary->salaryreportnet;
        $data->prevDebt = $prevDebt;
        $saveid = $data->saveid ?: $_SESSION['saveid'];
        $data->saveid = $saveEX->loadEx($saveid)->savename;
    }
    //print_r($allmydata);

    $smarty->assign('allmydata', $allmydata);
}

function editprint()
{

    global $smarty;
    global $Employeepersonnel;
    global $EmployeepersonnelDAO;
    global $EmployeepersonnelEX;
    global $Salaryreport;
    global $SalaryreportDAO;
    global $SalaryreportEX;
    global $myEmpEX;



    $id = $_GET["id"];




    $queryString = 'WHERE';


    $empName = $_REQUEST["empName"];
    $from = $_REQUEST["from"];


    if (isset($id) && $id != '') {
        $queryString .= ' salaryreport.salarymonth ="' . $id . '" AND';
    }




    $arr = explode(' ', $queryString);

    if (isset($arr) && count($arr) > 0) {
        $lastWord = end($arr);
        if ($lastWord == 'AND') {
            //remove it
            array_pop($arr);
            //form the string again
            $queryString = implode(' ', $arr);
            //$queryString .= ' ';
        } else if ($lastWord == 'WHERE') {
            //remove it
            array_pop($arr);
            //$queryString = implode(' ', $arr);
            $queryString = ' ';
        }
        //print("<br>queryString: ".$queryString."<br>");
    }

    $allmydata = $SalaryreportEX->queryAllEX($queryString);
    //print_r($allmydata);

    $smarty->assign('allmydata', $allmydata);
}

function Receipt()
{

    global $smarty;
    global $Employeepersonnel;
    global $EmployeepersonnelDAO;
    global $EmployeepersonnelEX;
    global $Salaryreport;
    global $SalaryreportDAO;
    global $SalaryreportEX;
    global $mySaveRecord;

    global $myEmpDAO;
    global $myEmp;
    global $myEmpEX;

    global $CostcenterdetailDAO;
    global $Costcenterdetail;

    //Dailyentry
    global $dailyEntry;
    global $dailyEntryDAO;

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

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


    if (isset($_POST["fromCtrl"]) && $_POST["fromCtrl"] == "employeePersonal") {
        $_POST['empid'] = array($_POST["empid"]);
        $_SESSION['userid'] = $_POST["userid"];
        $_SESSION['saveid'] = $_POST["saveid"];
        $_SESSION['dbname'] = $_POST["dbname"];
    }
    $salarymonthdate = $_POST['salarymonthdate'];
    $Costcenterid = $_POST["Costcenterid"];
    $todayx = date("Ymd");

    if ($_POST['saveid'])
        $saveid = $_POST['saveid'];
    else
        $saveid = $_SESSION['saveid'];

    $Monthsalarydata = $SalaryreportEX->loadcase($todayx);
    $salarymonth = $Monthsalarydata->salarymonth;
    if ($salarymonth != "") {

        $year = substr($salarymonth, 8, 2);
        $year++;
        $todayx = $todayx . $year;
    } else {

        $todayx = $todayx . '1';
    }


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

    //
    $start = $salarymonthdate . '-01';
    $end = $salarymonthdate . '-31';


    $saveData = $mySaveRecord->load($saveid);
    $allsalarysfinal = 0;
    if (isset($_POST['empid'])) {
        foreach ($_POST['empid'] as $value) {
            $lastSalary = $SalaryreportEX->getEmpLastRow($value);
            $prevDebt = 0;
            if ($lastSalary->salaryreportnet < 0)
                $prevDebt += $lastSalary->salaryreportnet;


            $allsalarys = $myEmpEX->getall($value);

            if ($allsalarys->employeeBouns == null || $allsalarys->employeeBouns == "") {
                $allsalarys->employeeBouns = 0;
            }

            if ($allsalarys->Reward == null || $allsalarys->Reward == "") {
                $allsalarys->Reward = 0;
            }
            if ($allsalarys->Discount == null || $allsalarys->Discount == "") {
                $allsalarys->Discount = 0;
            }

            if ($allsalarys->Withdraw == null || $allsalarys->Withdraw == "") {
                $allsalarys->Withdraw = 0;
            }

            if ($allsalarys->Absence == null || $allsalarys->Absence == "") {
                $allsalarys->Absence = 0;
            }

            if ($allsalarys->Repayment == null || $allsalarys->Repayment == "") {
                $allsalarys->Repayment = 0;
            }

            if ($allsalarys->permission == null || $allsalarys->permission == "") {
                $allsalarys->permission = 0;
            }



            $x[0] = $allsalarys->employeeId;
            $x[1] = $allsalarys->employeeName;
            $x[2] = $allsalarys->employeeSalary;

            $x[3] = $allsalarys->employeeBouns;
            $x[4] = $allsalarys->Reward;
            $x[5] = $allsalarys->Discount;

            $x[6] = $allsalarys->Withdraw;
            $x[7] = $allsalarys->Absence;
            $x[9] = $allsalarys->Repayment;
            $x[12] = $allsalarys->permission;


            $total = (($allsalarys->employeeBouns) + ($allsalarys->Reward) + ($allsalarys->employeeSalary)) -
                (($allsalarys->Discount) + ($allsalarys->Withdraw) + ($allsalarys->Absence) + ($allsalarys->Repayment) + ($allsalarys->permission)) + $prevDebt;
            //eman
            $netsalary = $total;
            //eman
            //fatma
            if ($total < 0) {
                $total = 0; //if net salary <0 so he will recieve 0 money
            }

            $Salaryreport->employeeid = $value;
            $Salaryreport->salarymonthdate = $salarymonthdate;
            $Salaryreport->salarymonth = $todayx;
            $Salaryreport->salaryreportbouns = $allsalarys->employeeBouns + 0;
            $Salaryreport->Reward = $allsalarys->Reward + 0;
            $Salaryreport->salaryreportdate = $today;
            $Salaryreport->salaryreportdiscount = $allsalarys->Discount + 0;
            $Salaryreport->salaryreportnet = $netsalary;
            $Salaryreport->salaryreportsalary = $allsalarys->employeeSalary;
            $Salaryreport->salaryreportwithdrawal = $allsalarys->Withdraw + 0;
            $Salaryreport->Absence = $allsalarys->Absence + 0;

            $Salaryreport->Repayment = $allsalarys->Repayment + 0;
            $Salaryreport->userid = $_SESSION['userid'];
            $Salaryreport->costcenterid = $Costcenterid;
            $Salaryreport->saveid = $saveid;



            $salaryreportid = $SalaryreportDAO->insert($Salaryreport);



            $Costcenterdetail->costamount = $total;
            $Costcenterdetail->costcenterid = $Costcenterid;
            $Costcenterdetail->costdate = date('Y-m-d');
            $Costcenterdetail->del = 0;
            $Costcenterdetail->modelid = $salaryreportid;
            $Costcenterdetail->processname = 'قبض موظف';
            $Costcenterdetail->tablename = 'salaryReportController.php';
            $Costcenterdetail->type = 8;
            $Costcenterdetail->uesrid = $_SESSION['userid'];

            if (!empty($Costcenterid) && $Costcenterid != '-1') {
                $CostcenterdetailDAO->insert($Costcenterdetail);
            }
            //eman
            $EmployeepersonnelEX->updatedon($start, $end, 1, $value, $_SESSION['userid'], $salarymonthdate);

            //            $EmployeepersonnelEX->updatedonDateFromTo($start, $end, 1, $value, $_SESSION['userid'], $salarymonthdate);


            $myempdata = $myEmpDAO->load($value);
            $myEmp->empdept = ($myempdata->empdept) - ($allsalarys->Repayment + 0);
            $myEmp->employeeId = $value;
            $myEmpEX->updatedept($myEmp);


            //daily Entry
            $dailyEntry->entryComment = 'مرتب موظف ' . $myempdata->employeeName;

            $dailyEntryDebtorArray = array();
            $dailyEntryDebtor->value = $total;
            $dailyEntryDebtor->accountstreeid = $myempdata->treeId;


            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $total;
            $dailyEntryCreditor->accountstreeid = $saveData->treeId;

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

            $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray, 1);
            $did = $returnedData[1];
            $Salaryreport->dailyentryid = $did;
            $Salaryreport->salaryreportid = $salaryreportid;
            $SalaryreportDAO->update($Salaryreport);





            $allsalarysfinal = $allsalarysfinal + $total;
        }




        $saveValueBefore = getSaveValueBefore($saveid);

        $saveValueafterValid = $saveValueBefore - $allsalarysfinal;





        updateSave($saveid, $saveValueafterValid);

        insertSavedaily($saveValueBefore, $allsalarysfinal, 1, $saveid, 'تقبيض مرتبات', $todayx, $saveValueafterValid, 'salaryReportController.php');

        //print_r('allsalarysfinal'.$allsalarysfinal.'<br>');
    }
}

function delete()
{
    global $smarty;
    global $Employeepersonnel;
    global $EmployeepersonnelDAO;
    global $EmployeepersonnelEX;
    global $Salaryreport;
    global $SalaryreportDAO;
    global $SalaryreportEX;
    global $myEmpEX;

    global $CostcenterdetailDAO;
    global $CostcenterdetailEX;






    $id = $_GET["id"];

    $mydata = $SalaryreportDAO->load($id);

    $CostcenterdetailEX->updatedellbytypeandmodelid(8, $id, 1);


    $fromx = $mydata->salarymonthdate;
    //print_r($mydata);
    $start = $fromx . '-01';
    $end = $fromx . '-31';


    $saveid = $mydata->saveid ?: $_SESSION['saveid'];
    $saveValueBefore = getSaveValueBefore($saveid);

    $saveValueafterValid = $saveValueBefore + $mydata->salaryreportnet;

    $EmployeepersonnelEX->updatedon($start, $end, 0, $mydata->employeeid, $_SESSION['userid'], $fromx);

    $Salaryreport->conditions = 1;
    $Salaryreport->salaryreportid = $id;
    $SalaryreportEX->updatedel($Salaryreport);

    updateSave($saveid, $saveValueafterValid);

    insertSavedaily($saveValueBefore, $mydata->salaryreportnet, 0, $saveid, 'حذف مرتب  موظف', $mydata->salarymonth, $saveValueafterValid, 'salaryReportController.php');
    reverseEntryWithItsID($mydata->dailyentryid);
}

// get savecurrentvalue from save tbl
function getSaveValueBefore($saveid)
{

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveValuebefore = $saveData->savecurrentvalue;

    return $saveValuebefore;
}

// get initialvalue and plus from save tbl
function getSaveValueAndPlus($saveid, $savevaluechanged)
{

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore + $savevaluechanged;

    return array($saveId, $saveValuebefore, $saveValueafter);
}

// get initialvalue and mins from save tbl
function getSaveValueAndMins($saveid, $savevaluechanged)
{

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveRecord;

    $saveData = $mySaveRecord->load($saveid);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore - $savevaluechanged;

    return array($saveId, $saveValuebefore, $saveValueafter);
}

// update in Save tbl
function updateSave($saveid, $savevalueafter)
{

    //to use the variable out side the funcion
    global $mySave;
    global $mySaveEx;

    //update initialvalue in Save tbl
    $mySave->savecurrentvalue = $savevalueafter;
    $mySave->userid = $_SESSION['userid'];
    $mySave->saveid = $saveid;

    $mySaveEx->updateSaveValue($mySave);
}

// insert in Savedaily tbl
function insertSavedaily($savedailysavebefore, $savedailychangeamount, $savedailychangetype, $saveid, $processname, $savedailymodelid, $savedailysaveafter, $tablename)
{

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

    //insert in Savedaily tbl
    $mySavedaily->savedailydate = date("Y-m-d H:i:s");
    $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);
}

function allemployee()
{

    global $myEmpDAO;
    global $myEmpEX;

    if ($_SESSION['branchId'] > 0)
        $queryString = ' AND branchid = ' . $_SESSION['branchId'];

    $allemp = $myEmpEX->queryAllemployeebycondation(0, $queryString);
    return $allemp;
}
