<?php

//the global file operation
include("../public/impOpreation.php");
include_once("../library/uploadImages.php");

include("../public/include_dao.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");


//Clientdebtchange
require_once('../models/dao/ClientdebtchangeDAO.class.php');
require_once('../models/dto/Clientdebtchange.class.php');
require_once('../models/mysql/ClientdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientdebtchangeMySqlExtDAO.class.php');

//Breadcrumb
//require_once("../library/breadcrumb.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");

$id = filter_input(INPUT_GET, 'id');
$dilEntry = filter_input(INPUT_GET, 'dailyentryid');

$ClientDAO = new ClientMySqlDAO();

$kashf = new Kashf();
$kashfDAO = new KashfMySqlDAO();
$kashfDAOEx = new KashfMySqlExtDAO();

$doctorDAO = new UserMySqlDAO ();
$doctorDAOEx = new UserMySqlExtDAO ();

//Savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();

//Clientarea
$Clientarea = new Clientarea();
$ClientareaDAO = new ClientareaMySqlDAO();

//save
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();

//Dailyentry
$dailyEntry = new Dailyentry();

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

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

//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

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

  Controller Name :- saveCTRL اضافة وتعديل وعرض الخزن

  OPERTATION in Controller

  1-display add form
  2- add in Save tbl
  3-insert into savedaily tbl
  4-display sucess or error
  5-display show form
  6-edit in Save data

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

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

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


//here goes the instances and general variables
//Breadcrumb
//$breadcrumbObject = new Breadcrumb();
//check and use the condetion that suite this action
if (empty($do)) {


    $today = date('Y-m-d');
    $smarty->assign('today', $today);
    $customer = $ClientDAO->queryAll();
    $client = $ClientDAO->queryAll();
    $smarty->assign('client', $client);

    $smarty->assign('customer', $customer);

    $Doctor = $doctorDAOEx->loadDocotr(4);



    $smarty->assign('doctor', $Doctor);

    $clientarea = $ClientareaDAO->queryAll();
    $smarty->assign('clientarea', $clientarea);

    $kashfDetails = $kashfDAO->queryAll();

    //# get last row was inserted in DB .
    $last_obj = end($kashfDetails);

    //# get id .
    $last_id = $last_obj->id;

    $new_id = $last_id + 1;

    $smarty->assign('new_id', $new_id);
    $smarty->display("rb_kashf/kashf_add.html");
} elseif ($do == 'add') {
    $customerid = filter_input(INPUT_POST, 'client');

    $datetoday = filter_input(INPUT_POST, 'kashfdate');
    $docselect = filter_input(INPUT_POST, 'docselect');
    $kashf_value = (float) filter_input(INPUT_POST, 'kashf_value');
    $kasf_type = filter_input(INPUT_POST, 'kasf_type');
    // $paytype = filter_input(INPUT_POST, 'paytype');
    $paytype = 2; //# تم الدفع
    $kasfh_serial = filter_input(INPUT_POST, 'kasfh_serial');
    $bill_id = filter_input(INPUT_POST, 'bill_id');

    if ($bill_id == '') {
        $bill_id = 0;
    }
    if ($kashf_value == '') {
        $kashf_value = 0;
    }

    $date = date('Y-m-d');


    $loadkashfdata = $kashfDAOEx->loadKashf($customerid, $datetoday);

    if ($customerid == '-1') {

        $today = date('Y-m-d');
        $smarty->assign('today', $today);
        $client = $ClientDAO->queryAll();
        $smarty->assign('client', $client);

        $Doctor = $doctorDAOEx->loadDocotr(4);


        $smarty->assign('doctor', $Doctor);

        $smarty->assign('msg', 'راجع اختيار العميلً');
        $smarty->display("rb_kashf/kashf_add.html");
    } elseif ($docselect == '-1') {

        $today = date('Y-m-d');
        $smarty->assign('today', $today);
        $client = $ClientDAO->queryAll();
        $smarty->assign('client', $client);

        $Doctor = $doctorDAOEx->loadDocotr(4);


        $smarty->assign('doctor', $Doctor);

        $smarty->assign('doc', 'راجع اختيار الطبيب');
        $smarty->display("rb_kashf/kashf_add.html");
    } elseif ($datetoday < $date) {

        $today = date('Y-m-d');
        $smarty->assign('today', $today);
        $client = $ClientDAO->queryAll();
        $smarty->assign('client', $client);

        $Doctor = $doctorDAOEx->loadDocotr(4);


        $smarty->assign('doctor', $Doctor);

        $smarty->assign('date', 'راجع اختيار التاريخً');
        $smarty->display("rb_kashf/kashf_add.html");
    } elseif ($kasf_type == '') {

        $today = date('Y-m-d');
        $smarty->assign('today', $today);
        $client = $ClientDAO->queryAll();
        $smarty->assign('client', $client);

        $Doctor = $doctorDAOEx->loadDocotr(4);


        $smarty->assign('doctor', $Doctor);

        $smarty->assign('katype', 'ادخل نوع الكشف');

        $smarty->display("rb_kashf/kashf_add.html");
    } elseif ($kashf_value == '' && $kashf_value != 0) {

        $today = date('Y-m-d');
        $smarty->assign('today', $today);
        $client = $ClientDAO->queryAll();
        $smarty->assign('client', $client);

        $Doctor = $doctorDAOEx->loadDocotr(4);


        $smarty->assign('doctor', $Doctor);

        $smarty->assign('salary', 'راجع سعر الكشفً');
        $smarty->display("rb_kashf/kashf_add.html");
    } else {

        $kashf->kashfvalue = $kashf_value;
        $kashf->kashftype = $kasf_type;
        $kashf->customerid = $customerid;
        $kashf->doctorid = $docselect;
        $kashf->kashfdate = $datetoday;
        $kashf->paystatus = $paytype;
        $kashf->entered = 0;
        $kashf->dailyentryid = 0;
        $kashf->kasfhserial = $kasfh_serial;
        $kashf->bill_id = $bill_id;

        $kashId = $kashfDAO->insert($kashf);

        //# m7md .
        //# load last client dept change .
        $last_client_deptchaneg = $clientDeptChangeExt->selectLastClientDept($customerid);

        if (!empty($customerid)) {
            $today = date('Y-m-d');
            //insert into clientdeptchange
            $clientDeptChange->clientdebtchangeafter = $last_client_deptchaneg[0]->clientdebtchangeafter;
            //$clientDeptChange->clientdebtchangeamount =  $last_client_deptchaneg[0]->clientdebtchangeamount;
            $clientDeptChange->clientdebtchangeamount = 0;
            $clientDeptChange->clientdebtchangebefore = $last_client_deptchaneg[0]->clientdebtchangebefore;
            $clientDeptChange->clientdebtchangedate = $today;
            $clientDeptChange->clientdebtchangemodelid = $kashId;
            $clientDeptChange->clientdebtchangetype = 0;
            $clientDeptChange->clientid = $customerid;
            $clientDeptChange->comment = "";
            $clientDeptChange->processname = "إضافة كشف جديد";
            $clientDeptChange->tablename = "rb_kashf.php";
            $clientDeptChange->userid = $_SESSION['userid'];
            $clientDeptChange->clientareaid = $last_client_deptchaneg[0]->clientareaid;
            $clientDeptChange->dailyentryid = 0;

            $clientDeptChangeDAO->insert($clientDeptChange);
        }


        $dailyEntry->entryComment = 'تم حجز كشف بصريات';

        // start من حساب الخزنة
        //save session
        $dailyEntryDebtorArray = array();
        $dailyEntryDebtor->value = $kashf_value;
        $saveid = $_SESSION['saveid'];
        $dataSave = $mySaveRecord->load($saveid);
        $idTreeSave = $dataSave->treeId;
        $dailyEntryDebtor->accountstreeid = $idTreeSave;

        // الى حساب خدمات الكشف
        ///الي حساب مردودات المشتريات
        $dailyEntryCreditorArray = array();
        $dailyEntryCreditor->value = $kashf_value;
        $dailyEntryCreditor->accountstreeid = 273;

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

        // load daily entry id and insert it
        $returnDailyId = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);
        $dailyId = $returnDailyId[1];

        $rowData = $kashfDAO->load($kashId);

        $kashf->kashfvalue = $kashf_value;
        $kashf->kashftype = $rowData->kashftype;
        $kashf->customerid = $rowData->customerid;
        $kashf->doctorid = $rowData->doctorid;
        $kashf->kashfdate = $rowData->kashfdate;
        $kashf->paystatus = $rowData->paystatus;
        $kashf->entered = $rowData->entered;
        $kashf->clientname = $rowData->clientname;
        $kashf->docname = $rowData->docname;
        $kashf->rondomtxt = $rowData->rondomtxt;
        $kashf->branchid = $rowData->branchid;
        $kashf->offline = $rowData->offline;
        $kashf->uploaddate = $rowData->uploaddate;
        $kashf->dailyentryid = $dailyId;
        $kashf->kasfhserial = $rowData->kasfhserial;


        $x = $kashfDAO->update($kashf);

        // update save
        //insert data in Save tbl
        $saveDatat = $mySaveRecord->load($saveid);
        $savedetails = '';
        $mySave->savedate = $saveDatat->savedate;
        $mySave->conditions = $saveDatat->conditions;
        $mySave->savecurrentvalue = ($saveDatat->savecurrentvalue + $kashf_value);
        $mySave->savename = $saveDatat->savename;
        $mySave->userid = $saveDatat->userid;
        $mySave->saveid = $saveid;

        $mySaveRecord->update($mySave, $savedetails);



        //insert in Savedaily tbl
        $mySavedaily->savedailydate = date('Y-m-d H:i:s');
        $mySavedaily->userid = $_SESSION['userid'];
        $mySavedaily->savedailysavebefore = 0;
        $mySavedaily->savedailychangeamount = $kashf_value;
        $mySavedaily->savedailychangetype = 0;
        $mySavedaily->saveid = $saveid;
        $mySavedaily->processname = 'تم حجز كشف بصريات';
        $mySavedaily->savedailymodelid = $kashId;
        $mySavedaily->savedailysaveafter = ($saveDatat->savecurrentvalue + $kashf_value);
        $mySavedaily->savedailysavebefore = $saveDatat->savecurrentvalue;
        $mySavedaily->tablename = 'rb_kashf.php';
        $mySavedaily->clientid = $customerid;

        $mySavedailyRecord->insert($mySavedaily);

        header('location:?do=show');
    }
} elseif ($do == 'show') {


    $showkashf = $kashfDAOEx->queryAllDESC();

    foreach ($showkashf as $kdata) {
        $custid = $kdata->customerid;
        $docid = $kdata->doctorid;


        $custData = $ClientDAO->load($custid);

        $kdata->custname = $custData->clientname;

        $docdata = $doctorDAOEx->loadDocotr2($docid, 4);

        $kdata->DocData = $docdata->username;
    }

    $smarty->assign('kashfshow', $showkashf);



    $smarty->display('rb_kashf/kashf_show.html');
} elseif ($do == 'edit') {

    $editkasf = $kashfDAO->load($id);
    //load customer

    $custid = $editkasf->customerid;
    // print_r($custid);
    $custData = $ClientDAO->load($custid);

    // print_r($custData);
    $smarty->assign('custData', $custData);
    $editkasf->custname = $custdata . clientname;


    $allcustmoer = $ClientDAO->queryAll();
    $smarty->assign('allcust', $allcustmoer);

    $clientarea = $ClientareaDAO->queryAll();
    $smarty->assign('clientarea', $clientarea);

    //doctor
    $userId = $editkasf->doctorid;

    $docdata = $doctorDAOEx->loadDocotr2($userId, 4);

    $docName = $docdata->username;
    $smarty->assign('docName', $docName);
    $alldoctors = $doctorDAOEx->loadDocotr(4);
    $smarty->assign('alldoc', $alldoctors);


    $smarty->assign('editkasf', $editkasf);

    $smarty->display('rb_kashf/kashf_edit.html');
} elseif ($do == 'editprint') {

    $editkasf = $kashfDAO->load($id);
    //load customer

    $custid = $editkasf->customerid;
    $custData = $ClientDAO->load($custid);
    $smarty->assign('custdata', $custdata);
    $editkasf->custname = $custdata . clientname;


    $allcustmoer = $ClientDAO->queryAll();
    $smarty->assign('allcust', $allcustmoer);


    //doctor
    $userId = $editkasf->doctorid;

    $docdata = $doctorDAOEx->loadDocotr2($userId, 4);

    $docName = $docdata->username;
    $smarty->assign('docName', $docName);
    $alldoctors = $doctorDAOEx->loadDocotr(4);
    $smarty->assign('alldoc', $alldoctors);


    $smarty->assign('editkasf', $editkasf);

    $smarty->display('rb_kashf/kashf_editprint.html');
} elseif ($do == 'update') {
    $custselect = filter_input(INPUT_POST, 'custselect');
    $docselect = filter_input(INPUT_POST, 'docselect');
    $kashf_value = filter_input(INPUT_POST, 'kashf_value');
    $kasf_type = filter_input(INPUT_POST, 'kasf_type');
    $kashfid = filter_input(INPUT_POST, 'kashfid');
    $kashfdate = filter_input(INPUT_POST, 'kashfdate');
    //$paytype = filter_input(INPUT_POST, 'paytype');
    $paytype = 2; //# تم الدفع

    $oldCustomer = filter_input(INPUT_POST, 'client');
    $bill_id = filter_input(INPUT_POST, 'bill_id');

    if ($bill_id == '') {
        $bill_id = 0;
    }

    $loadkashf = $kashfDAOEx->loadKashf($custselect, $kashfdate);
    $now = new DateTime();
    $date = $now->format('Y-m-d');

    if ($oldCustomer == $custselect) {

        $loadkashf2 = $kashfDAO->load($kashfid);
        $loadkashf2->kashfvalue = $kashf_value;
        $loadkashf2->kashftype = $kasf_type;
        $loadkashf2->customerid = $oldCustomer;
        $loadkashf2->doctorid = $docselect;
        $loadkashf2->paystatus = $paytype;
        $now = new DateTime();
        $date = $now->format('Y-m-d');
        if ($kashfdate < $date) {

            $editkasf = $kashfDAO->load($kashfid);

            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;



            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('date', 'راجع اختيار التاريخً');
            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($kasf_type == '') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('katype', 'ادخل نوع الكشف');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($docselect == '-1') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('doc', 'راجع اختيار الطبيب');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($kashf_value < 0) {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('value', 'راجع سعر الكشف');

            $smarty->display("rb_kashf/kashf_edit.html");
        } else {

            $loadkashf2->kashfdate = $kashfdate;
            $loadkashf2->id = $kashfid;
            $loadkashf2->dailyentryid = $loadkashf2->dailyentryid;

            $kashfDAO->update($loadkashf2);

            $dailyId1 = $loadkashf2->dailyentryid;
            reverseEntryWithItsID($dailyId1);

            $dailyEntry->entryComment = 'تم تعديل الكشف ';
            $dailyEntry->reverseofid = 0;
            // start من حساب الخزنة
            //save session
            $dailyEntryDebtorArray = array();
            $dailyEntryDebtor->value = $kashf_value;
            $saveid = $_SESSION['saveid'];
            $dataSave = $mySaveRecord->load($saveid);
            $idTreeSave = $dataSave->treeId;
            $dailyEntryDebtor->accountstreeid = $idTreeSave;

            // الى حساب خدمات الكشف
            ///الي حساب مردودات المشتريات
            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $kashf_value;
            $dailyEntryCreditor->accountstreeid = 273;

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

            // load daily entry id and insert it
            $returnDailyId = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);
            $dailyId2 = $returnDailyId[1];

            $rowData = $kashfDAO->load($kashfid);

            $loadkashf2->kashfvalue = $kashf_value;
            $loadkashf2->kashftype = $rowData->kashftype;
            $loadkashf2->customerid = $rowData->customerid;
            $loadkashf2->doctorid = $rowData->doctorid;
            $loadkashf2->kashfdate = $rowData->kashfdate;
            $loadkashf2->paystatus = $rowData->paystatus;
            $loadkashf2->entered = $rowData->entered;
            $loadkashf2->clientname = $rowData->clientname;
            $loadkashf2->docname = $rowData->docname;
            $loadkashf2->rondomtxt = $rowData->rondomtxt;
            $loadkashf2->branchid = $rowData->branchid;
            $loadkashf2->offline = $rowData->offline;
            $loadkashf2->uploaddate = $rowData->uploaddate;
            $loadkashf2->dailyentryid = $dailyId2;
            $loadkashf2->kasfhserial = $rowData->kasfhserial;

            $kashfDAO->update($loadkashf2);


            header('location:?do=show');
        }
    } elseif ($oldCustomer != $custselect) {


        if ($custselect == '-1') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('client', 'راجع اختيار العميل');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($kasf_type == '') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('katype', 'ادخل نوع الكشف');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($kashf_value < 0 || $kashf_value == '') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('value', 'راجع سعر الكشف');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($docselect == '-1') {

            $editkasf = $kashfDAO->load($kashfid);


            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;

            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);

            $smarty->assign('doc', 'راجع اختيار الطبيب');

            $smarty->display("rb_kashf/kashf_edit.html");
        } elseif ($kashfdate < $date) {




            $editkasf = $kashfDAO->load($kashfid);

            $custid = $editkasf->customerid;
            $custData = $ClientDAO->load($custid);
            $smarty->assign('custdata', $custdata);
            $editkasf->custname = $custdata->customername;

            $allcustmoer = $ClientDAO->queryAll();
            $smarty->assign('allcust', $allcustmoer);


            $docid = $editkasf->doctorid;
            $docdata = $doctorDAOEx->loadDocotr2($docid, 4);


            $smarty->assign('docdata', $docdata);
            $editkasf->docname = $docdata->username;



            $alldoctors = $doctorDAOEx->loadDocotr(4);
            $smarty->assign('alldoc', $alldoctors);


            $smarty->assign('editkasf', $editkasf);


            $smarty->assign('date', 'راجع اختيار التاريخً');
            $smarty->display("rb_kashf/kashf_edit.html");
        } else {
            $loadkashf2 = $kashfDAO->load($kashfid);

            $loadkashf2->kashfvalue = $kashf_value;
            $loadkashf2->kashftype = $kasf_type;
            $loadkashf2->customerid = $oldCustomer;
            $loadkashf2->doctorid = $docselect;
            $loadkashf2->paystatus = $paytype;
            $loadkashf2->kashfdate = $kashfdate;
            $loadkashf2->id = $kashfid;
            $loadkashf2->entered = 0;
            $loadkashf2->dailyentryid = 0;
            $loadkashf2->bill_id = $bill_id;

            print_r('<pre>');
            print_r($loadkashf2);
            print_r('<pre>');

            $kashfDAO->update($loadkashf2);


            $dailyId1 = $loadkashf2->dailyentryid;
            reverseEntryWithItsID($dailyId1);

            $dailyEntry->entryComment = 'تم تعديل الكشف ';
            $dailyEntry->reverseofid = 0;
            // start من حساب الخزنة
            //save session
            $dailyEntryDebtorArray = array();
            $dailyEntryDebtor->value = $kashf_value;
            $saveid = $_SESSION['saveid'];
            $dataSave = $mySaveRecord->load($saveid);
            $idTreeSave = $dataSave->treeId;
            $dailyEntryDebtor->accountstreeid = $idTreeSave;

            // الى حساب خدمات الكشف
            ///الي حساب مردودات المشتريات
            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $kashf_value;
            $dailyEntryCreditor->accountstreeid = 273;

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

            // load daily entry id and insert it
            $returnDailyId = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);
            $dailyId2 = $returnDailyId[1];

            $rowData = $kashfDAO->load($kashfid);

            $loadkashf2->kashfvalue = $kashf_value;
            $loadkashf2->kashftype = $rowData->kashftype;
            $loadkashf2->customerid = $rowData->customerid;
            $loadkashf2->doctorid = $rowData->doctorid;
            $loadkashf2->kashfdate = $rowData->kashfdate;
            $loadkashf2->paystatus = $rowData->paystatus;
            $loadkashf2->entered = $rowData->entered;
            $loadkashf2->clientname = $rowData->clientname;
            $loadkashf2->docname = $rowData->docname;
            $loadkashf2->rondomtxt = $rowData->rondomtxt;
            $loadkashf2->branchid = $rowData->branchid;
            $loadkashf2->offline = $rowData->offline;
            $loadkashf2->uploaddate = $rowData->uploaddate;
            $loadkashf2->dailyentryid = $dailyId2;

            $kashfDAO->update($loadkashf2);

            header('location:?do=show');
        }
    }
} elseif ($do == 'delete') {

    $kashfDAO->delete($id);

    reverseEntryWithItsID($dilEntry);


    header('location:?do=show');
} elseif ($do == "sucess") {

    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {

    //here the smarty templates
    $smarty->display("error.html");
}




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

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