<?php

//the global file operation
session_start();
ob_start();

//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");
//Expensetype
require_once('../models/dao/ExpensestypeDAO.class.php');
require_once('../models/dto/Expensestype.class.php');
require_once('../models/mysql/ExpensestypeMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensestypeMySqlExtDAO.class.php');
//ExpensesDAO
require_once('../models/dao/ExpensesDAO.class.php');
require_once('../models/dto/Expense.class.php');
require_once('../models/mysql/ExpensesMySqlDAO.class.php');
require_once('../models/mysql/ext/ExpensesMySqlExtDAO.class.php');
//Usergroup
require_once('../models/dao/UsergroupDAO.class.php');
require_once('../models/dto/Usergroup.class.php');
require_once('../models/mysql/UsergroupMySqlDAO.class.php');
require_once('../models/mysql/ext/UsergroupMySqlExtDAO.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');

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

//Assetscat
require_once('../models/dao/AssetscatDAO.class.php');
require_once('../models/dto/Assetscat.class.php');
require_once('../models/mysql/AssetscatMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetscatMySqlExtDAO.class.php');
//Assets
require_once('../models/dao/AssetsDAO.class.php');
require_once('../models/dto/Asset.class.php');
require_once('../models/mysql/AssetsMySqlDAO.class.php');
require_once('../models/mysql/ext/AssetsMySqlExtDAO.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');

//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');
//User
require_once('../models/dao/UserDAO.class.php');
require_once('../models/dto/User.class.php');
require_once('../models/mysql/UserMySqlDAO.class.php');
require_once('../models/mysql/ext/UserMySqlExtDAO.class.php');


//get the do the action
$do = $_GET['do'];

//get the SESSION
$userID = $_SESSION["userid"];

/* ======================

  Controller Name :- expensestypeCTRL

  OPERTATION in Controller

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

//here goes the instances and general variables


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

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

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

$ExpensetypeDAO = new ExpensestypeMySqlDAO();
$Expensetype = new Expensestype();
$ExpensetypeEX = new ExpensestypeMySqlExtDAO();
//Usergroup
$Usergroup = new Usergroup();
$UsergroupDAO = new UsergroupMySqlDAO();
$UsergroupEX = new UsergroupMySqlExtDAO();
//Expenses
$ExpenseDAO = new ExpensesMySqlDAO();
$Expense = new Expense();
$ExpenseEX = new ExpensesMySqlExtDAO();
//save
$Save = new Save();
$SaveDAO = new SaveMySqlDAO();
$SaveExt = new SaveMySqlExtDAO();
//savedaily
$Savedaily = new Savedaily();
$SavedailyDAO = new SavedailyMySqlDAO();

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


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

$Assetscat = new Assetscat();
$AssetscatDAO = new AssetscatMySqlDAO();
$AssetscatEX = new AssetscatMySqlExtDAO();
//Assets
$myAssets = new Asset();
$myAssetsRecord = new AssetsMySqlDAO();
$myAssetsEx = new AssetsMySqlExtDAO();

//Employeedoctor
$employeeDoctor = new Employeedoctor();
$employeeDoctorDAO = new EmployeedoctorMySqlDAO();
$employeeDoctorEX = new EmployeedoctorMySqlExtDAO();
//User
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

$Programsettingdata = $ProgramsettingDAO->load(1);
$smarty->assign("Programsettingdata", $Programsettingdata);
$userData = $myUserRecord->load($_SESSION['userid']);

//check and use the condetion that suite this action
if (empty($do)) {

} elseif ($do == "add") { //add expenses type at run time ====> NOT USED
    try {
        // here the function that do the action
        add();
        $alldata = expensesType();
        $smarty->assign("alldata", $alldata);

        $smarty->display("expensesview/expensetype.html");
    } catch (Exception $e) {

    }
} elseif ($do == "isParent") {
    $isParent = checkForChildren();
    if (!empty($isParent) && count($isParent) > 0)
        echo "isParent";
    else
        echo "isNotParent";
} elseif ($do == "hasExpenses") {
    $hasExpenses = checkForExpenses();
    if (!empty($hasExpenses) && count($hasExpenses) > 0)
        echo "yes";
    else
        echo "no";
} elseif ($do == 'checkExp') {
    $expensesname = $_POST['expensesname'];
    $expData = $ExpenseDAO->queryByExpensesname($expensesname);
    if (!empty($expData)) {
        echo -1;
    } else {
        echo 0;
    }
}

//edit  Expensetype
elseif ($do == "edit") {
    $Expensealldata = edit();
    $smarty->assign("Expensealldata", $Expensealldata);

    $alldata = expensesType();
    $smarty->assign("alldata", $alldata);

    $smarty->display("expensesview/editAjax.html");
} elseif ($do == "update") { //edit update
    print_r('aaaaaaaaaaaaaaaa');
    $saveValuebeforeValid = update();
    if ($saveValuebeforeValid == "1" || $saveValuebeforeValid == "2") {
        header("location:expensesController.php");
    } else {
        $url = "expensesController.php?do=show";
        $smarty->assign('urldirect', $url);
        $note = "لا يوجد بالخزنة الرئيسية المبلغ الكافى للمصروفات";

        $smarty->assign('msgnote', $note);
        // $smarty->display("notes.html");
    }
} elseif ($do == "addCat") {
    try {
        addCat();
        header("location:expensesController.php");
    } catch (Exception $e) {

    }
} elseif ($do == "expenseTypeAjax") {
    $assetorexpense = (int) $_GET['assetorexpense'];
    if ($assetorexpense == 0) {
        assetAjax();
    } else if ($assetorexpense == 1) {
        expenseTypeAjax();
    }
} elseif ($do == "getemployeeOrDoctorajax") {
    getemployeeOrDoctorajax();
} elseif ($do == "expensetypesave") {
    $searchTerm = $_POST['searchTerm'];
    $saveid = $_POST['saveid'];
    $queryString = " and expensestypename LIKE '%" . $searchTerm . "%' ";
    if ($saveid) {
        $queryString .= " and ( saveid = " . $saveid . " or saveid = 0 ) ";
    }
    $expensetypes = $ExpensetypeEX->expensetypesave($queryString);
    $return_arr = array();
    foreach ($expensetypes as $pro) {
        $row_array = array();
        $row_array['id'] = $pro->expensestypeid;
        $row_array['text'] = $pro->expensestypename;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "expensetypesaveuser") {
    $searchTerm = $_POST['searchTerm'];
    $saveid = $_POST['saveid'];
    $queryString = " and expensestypename LIKE '%" . $searchTerm . "%' ";

    if ($_SESSION['searchinonesave'] == 0 && $_SESSION['saveids'] != 0) {
        $queryString = ' and saveid in (' . $_SESSION['saveids'] . ') or saveid = 0 ';
    }
    $expensetypes = $ExpensetypeEX->expensetypesave($queryString);
    $return_arr = array();
    foreach ($expensetypes as $pro) {
        $row_array = array();
        $row_array['id'] = $pro->expensestypeid;
        $row_array['text'] = $pro->expensestypename;
        $row_array['withinsupervisionratio'] = $pro->withinsupervision_ratio;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}

##africano requests
elseif ($do == "triptype") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/trip-type', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "trips") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/trip', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "paths") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/path', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "dateid") {
    $tripid = (int) $_REQUEST['tripid'];
    //if send trip_id will filter by trip id
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/trip-date?trip_id=' . $tripid, 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->trip_name . "/" . $data->day_date;
        $row_array['trip_id'] = $data->trip_id;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "referrer") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/referrer', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "driver") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/driver', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
} elseif ($do == "bus") {
    $return_arr = array();
    $response = CURL_Request(array(), getenv('africanoUrl') . '/bus', 1, 0, getenv('africanoToken'));
    $receivedata = json_decode($response);
    foreach ($receivedata->data as $data) {
        $row_array = array();
        $row_array['id'] = $data->id;
        $row_array['text'] = $data->name;
        array_push($return_arr, $row_array);
    }
    echo json_encode($return_arr);
}


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

// add in Expensetype tbl
function add() {
    //to use the variable out side the funcion
    global $ExpensetypeDAO;
    global $Expensetype;

    $expenseName = $_POST['expenseName'];
    $expensecomment = $_POST['expensecomment'];

    //add in expensetype tbl
    $Expensetype->expensetypename = $expenseName;
    $Expensetype->conditions = 0;
    $Expensetype->userid = $_SESSION["userid"];
    $ExpensetypeDAO->insert($Expensetype, $expensecomment);
}

function edit() {
    global $ExpenseDAO;
    global $ExpenseEX;

    $expenseid = $_GET['id'];
    $Expensealldata = $ExpenseDAO->load($expenseid);
    return $Expensealldata;
}

//expensesType
function expensesType() {
    global $ExpensetypeEX;

    //كل التصنيفات التي ليست parent لأي تصنيف آخر
    $alldata = $ExpensetypeEX->queryAllTypesWithNoChildren();
    return $alldata;
}

function checkForChildren() {
    global $ExpensetypeEX;

    $expenseTypeId = $_GET['id'];
    $childData = $ExpensetypeEX->queryByParentExt($expenseTypeId);
    return $childData;
}

function checkForExpenses() {
    global $ExpenseEX;

    $expenseTypeId = $_GET['id'];
    $childData = $ExpenseEX->checkForExpenses($expenseTypeId);
    return $childData;
}

function update() {
    global $Expense;
    global $ExpenseEX;
    global $ExpenseDAO;
    global $today;

    global $CostcenterdetailDAO;
    global $CostcenterdetailEX;

    global $accountsTreeDAO;
    global $accountsTree;

    //save
    global $mySave;
    global $SaveDAO;

    //Dailyentry
    global $dailyEntry;
    global $dailyEntryDAO;

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

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

    global $ExpensetypeDAO;
    global $Expensetype;


    $expensesname = $_POST['expensesname'];
    $expensevalue = $_POST['expensevalue'];
    $expensetype = $_POST['expensetype'];
    $expensecomment = $_POST['expensecomment'];
    $expenseid = $_POST['expenseid'];
    $conditions = $_POST['conditions'];
    $dailyentryid = $_POST['dailyentryid'];

    print_r('<pre>');
    print_r($dailyentryid);
    print_r('<pre>');
    $saveValuebeforeValid = "1";
    $oldData = $ExpenseDAO->load($expenseid);

    print_r('<pre>old data');
    print_r($oldData);
    print_r('<pre>');

    //check conditions value in expenses tbl equal tempdele value of post variable
    //or change to change save value
    $ExpenseData = $ExpenseDAO->load($expenseid);


    if (count($ExpenseData) > 0) {
        $tempdele = $ExpenseData->conditions;
        if ($tempdele != $conditions) { //if the user changed conditions (حذف مؤقت أو إلغاء الحذف)
            if ($conditions == 0) {
                $saveValuebeforeValid = "1";

                $CostcenterdetailEX->updatedellbytypeandmodelid('6', $expenseid, 0);

                //////////////////////////check save value before expenses///////////////////////////
                //check save value
                if ($expensevalue > 0) {
                    //get save value
                    $saveValueBefore = getSaveValue();
                    $saveValuebeforeValid;
                    $saveValueafterValid = $saveValueBefore - $expensevalue;
                    if ($saveValueafterValid >= 0) {
                        $saveValuebeforeValid = "1"; //continue
                    } else {
                        $saveValuebeforeValid = "0"; //stop and return, show error
                        //return $saveValuebeforeValid;
                    }
                }

                ////////////////////////////////////////////////////////////////
                //update save
                if ($expensevalue > 0) {
                    //get saveValue before and saveValue after subtractig expensesValue
                    $valueData = getSaveValueAndMins($expensevalue);
                    $saveId = $valueData[0];
                    $saveValuebefore = $valueData[1];
                    $saveValueafter = $valueData[2];

                    //update save value after expenses
                    updateSave($saveId, $saveValueafter);

                    //insert into savedaily tbl
                    insertSavedaily($saveValuebefore, $expensevalue, 1, $saveId, "إضافة مصروف", $expenseid, $saveValueafter, "expensesController.php");
                }
            } elseif ($conditions == 1) {
                $CostcenterdetailEX->updatedellbytypeandmodelid('6', $expenseid, 1);

                $saveValuebeforeValid = "2";
                if ($expensevalue > 0) {
                    //get saveVsalue before and saveValue after adding expensesValue
                    $valueData = getSaveValueAndPlus($expensevalue);
                    if (count($valueData) > 0) {
                        $saveId = $valueData[0];
                        $saveValuebefore = $valueData[1];
                        $saveValueafter = $valueData[2];

                        //update save value after expenses
                        updateSave($saveId, $saveValueafter);

                        //insert into savedaily tbl
                        insertSavedaily($saveValuebefore, $expensevalue, 0, $saveId, "حذف مصروف", $expenseid, $saveValueafter, "expensesController.php");
                    }
                }
            }
        }


        $x = reverseEntryWithItsID($dailyentryid);

        $dailyEntry->entryComment = 'تم تعديل المصروف';

        //$dailyEntryDebtor  من نوع المصروف الجديد

        $dailyEntryDebtorArray = array();
        $dailyEntryDebtor->value = $expensevalue;

        // load expensestypename using expensestypeid

        $expenseTypeData = $ExpensetypeDAO->load($expensetype);
        $treeId = $expenseTypeData->treeId;

        $dailyEntryDebtor->accountstreeid = $treeId;


        //$dailyEntryCreditor الى نوع المصروف القديم

        $dailyEntryCreditorArray = array();
        $dailyEntryCreditor->value = $expensevalue;


//save session
        $saveid = $_SESSION['saveid'];
        $dataSave = $SaveDAO->load($saveid);

        $idTreeSave = $dataSave->treeId;
        $dailyEntryCreditor->accountstreeid = $idTreeSave;

//end  save data

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

        $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);
        print_r('<pre>new');
        print_r($returnedData);
        print_r('<pre>');
        $did = $returnedData[1];


        //update expenses table
        $Expense->expensesid = $expenseid;
        $Expense->expensesname = $expensesname;
        $Expense->expensesValue = $expensevalue;
        $Expense->conditions = $conditions;
        $Expense->expensestypeid = $expensetype;
        $Expense->expensesdate = $today;
        $Expense->userid = $_SESSION["userid"];
        $Expense->expensesdetails = $expensecomment;
        $Expense->dailyentryid = $did;



        $ExpenseDAO->update($Expense);
    }


    return $saveValuebeforeValid;
}

// get savecurrentvalue from save tbl
function getSaveValue() {
    global $Save;
    global $SaveDAO;

    $saveData = $SaveDAO->load($_SESSION['saveid']);
    $saveValuebefore = $saveData->savecurrentvalue;
    return $saveValuebefore;
}

// get savecurrentvalue and add from save tbl
function getSaveValueAndPlus($savevaluechanged) {
    global $Save;
    global $SaveDAO;

    $saveData = $SaveDAO->load($_SESSION['saveid']);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore + $savevaluechanged;

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

// get savecurrentvalue and subtract from save tbl
function getSaveValueAndMins($savevaluechanged) {
    global $Save;
    global $SaveDAO;

    $saveData = $SaveDAO->load($_SESSION['saveid']);
    $saveId = $saveData->saveid;
    $saveValuebefore = $saveData->savecurrentvalue;

    $saveValueafter = $saveValuebefore - $savevaluechanged;

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

// update Save tbl
function updateSave($saveid, $savevalueafter) {
    global $Save;
    global $SaveExt;

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

    $SaveExt->updateSaveValue($Save);
}

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

    //to use the variable out side the funcion
    global $Savedaily;
    global $SavedailyDAO;

    //insert in Savedaily tbl
    $Savedaily->savedailydate = date("Y-m-d H:i:s");
    $Savedaily->userid = $_SESSION['userid'];
    $Savedaily->savedailysavebefore = $savedailysavebefore;
    $Savedaily->savedailychangeamount = $savedailychangeamount;
    $Savedaily->savedailychangetype = $savedailychangetype;
    $Savedaily->saveid = $saveid;
    $Savedaily->processname = $processname;
    $Savedaily->savedailymodelid = $savedailymodelid;
    $Savedaily->savedailysaveafter = $savedailysaveafter;
    $Savedaily->tablename = $tablename;

    $SavedailyDAO->insert($Savedaily);
}

// add in expensetype tbl
function addCat() {
    global $ExpensetypeDAO;
    global $Expensetype;
    global $accountsTree;
    global $accountsTreeDAO;

    $userID = $_SESSION['userid'];
    $name = $_REQUEST['name'];
    $descripe = $_REQUEST['descripe'];
    $parentid = $_REQUEST['parent'];


    print_r($parentid . 'aaaaaaaaaaaaaaa');
    if (empty($descripe)) {
        $descripe = 0;
    }

    $Expensetype->expensestypename = $name;
    $Expensetype->expensestypedetails = $descripe;
    $Expensetype->conditions = 0;
    $Expensetype->expensestypedate = date("Y-m-d");
    $Expensetype->userid = $userID;
    $Expensetype->parent = $parentid;
    $Expensetype->type = 0;

    $ExpensetypeDAO->insert($Expensetype);

    $oldData = $ExpensetypeDAO->load($parentid);
    // print_r($oldData);


    $parentTreeId = $oldData->treeId;
    print_r($parentTreeId . 'aaaaaaaaaaaaaa');
    $flag = addTreeElement($name, $parentTreeId, 3, 0, 0, '', 0, 0);

    print_r($flag . 'cccccccccccc');
}

function expenseTypeAjax() {
    global $ExpensetypeEX;
    global $userData;

    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = 15; //intval($_GET['page_limit']);

    if ($userData->searchinonesave == 0) {
        $allParents = $ExpensetypeEX->queryAllExtNotParent();
    } else {
        $allParents = $ExpensetypeEX->queryAllWithSaveNotParent($_SESSION['saveid']);
    }

    foreach ($allParents as $value) {
        if ($value->parent > 0) {
            $value->expensestypename = "$value->parentexpensestypename / $value->expensestypename";
        }

        $row_array['id'] = $value->expensestypeid;
        $row_array['text'] = $value->expensestypename;

        array_push($return_arr, $row_array);
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

function assetAjax() {
    global $myAssetsRecord;

    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = 15; //intval($_GET['page_limit']);

    $alldata = $myAssetsRecord->queryAll();
    foreach ($alldata as $value) {

        $row_array['id'] = $value->assetId;
        $row_array['text'] = $value->assetsName;

        array_push($return_arr, $row_array);
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

function getemployeeOrDoctorajax() {
    global $employeeDoctorEX;


    $employeeOrDoctor = (int) $_GET['employeeOrDoctor'];

    $row_array = array();
    $return_arr = array();

    $name = $_GET['term']; //It could be product name or category name
    $limit = 15; //intval($_GET['page_limit']);

    $alldata = $employeeDoctorEX->queryAllEX(' and thetype=' . $employeeOrDoctor . ' limit ' . $limit);
    foreach ($alldata as $value) {

        $row_array['id'] = $value->id;
        $row_array['text'] = $value->name;

        array_push($return_arr, $row_array);
    }

    //echo $return_arr;
    echo json_encode($return_arr);
}

?>