<?php

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

//global varable
global $showoutside;

//to check if the page from .htacess
//$showoutside = $_GET['sn'];
// get the config file
include_once("../public/config.php");
include_once("dailyentryfun.php");

//here the db files that include in the file
include("../public/include_dao.php");


//Partnertransferbetween
require_once('../models/dao/PartnertransferbetweenDAO.class.php');
require_once('../models/dto/Partnertransferbetween.class.php');
require_once('../models/mysql/PartnertransferbetweenMySqlDAO.class.php');
require_once('../models/mysql/ext/PartnertransferbetweenMySqlExtDAO.class.php');


//Partnerwithdrawal
require_once('../models/dao/PartnerwithdrawalDAO.class.php');
require_once('../models/dto/Partnerwithdrawal.class.php');
require_once('../models/mysql/PartnerwithdrawalMySqlDAO.class.php');
require_once('../models/mysql/ext/PartnerwithdrawalMySqlExtDAO.class.php');
//Partner
require_once('../models/dao/PartnerDAO.class.php');
require_once('../models/dto/Partner.class.php');
require_once('../models/mysql/PartnerMySqlDAO.class.php');
require_once('../models/mysql/ext/PartnerMySqlExtDAO.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/BankaccountDAO.class.php');
require_once('../models/dto/Bankaccount.class.php');
require_once('../models/mysql/BankaccountMySqlDAO.class.php');
require_once('../models/mysql/ext/BankaccountMySqlExtDAO.class.php');

require_once('../models/dao/AccountmovementDAO.class.php');
require_once('../models/dto/Accountmovement.class.php');
require_once('../models/mysql/AccountmovementMySqlDAO.class.php');
require_once('../models/mysql/ext/AccountmovementMySqlExtDAO.class.php');

require_once('../models/dao/BankDAO.class.php');
require_once('../models/dto/Bank.class.php');
require_once('../models/mysql/BankMySqlDAO.class.php');
require_once('../models/mysql/ext/BankMySqlExtDAO.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');

//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 :- transfermoneyCTRL التحويل بين الخزن

  OPERTATION in Controller

  1-display add form
  2- add in transfermoney tble
  3-update savefrom value (-)
  4-insert into savedaily tbl
  5-update saveto value (+)
  6-insert into savedaily tbl
  7-display sucess or error
  8-display show and delete
  9-tempdelete from Expenses tbl
  10-returndelete save value (+)

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


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


//here goes the instances and general variables
//Partnerwithdrawal
$myPartnertransferbetween = new Partnertransferbetween();
$myPartnertransferbetweenRecord = new PartnertransferbetweenMySqlDAO();
$myPartnertransferbetweenEx = new PartnertransferbetweenMySqlExtDAO();

//Partnerwithdrawal
$myPartnerwithdrawal = new Partnerwithdrawal();
$myPartnerwithdrawalRecord = new PartnerwithdrawalMySqlDAO();
$myPartnerwithdrawalEx = new PartnerwithdrawalMySqlExtDAO();
//Partner
//save
$mySave = new Save();
$mySaveRecord = new SaveMySqlDAO();
$mySaveEx = new SaveMySqlExtDAO();



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

//Partner
$myPartner = new Partner();
$myPartnerRecord = new PartnerMySqlDAO();
$myPartnerEx = new PartnerMySqlExtDAO();

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


$account = new Bankaccount();
$accountDAO = new BankaccountMySqlDAO;
$accountExtDAO = new BankaccountMySqlExtDAO;

$accountMoveDAO = new AccountmovementMySqlDAO;
$accountMove = new Accountmovement;

$bankDAO = new BankMySqlDAO;
$bank = new Bank;
$bankExtDAO = new BankMySqlExtDAO;

//User
$myUser = new User();
$myUserRecord = new UserMySqlDAO();
$myUserEx = new UserMySqlExtDAO();

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

$today_search = date("Y-m-d");

$userData = $myUserRecord->load($_SESSION['userid']);
$smarty->assign("userData", $userData);
//check and use the condetion that suite this action
if (empty($do)) {
    //here the permssion check
    include_once("../public/authentication.php");

    // here the function that do the action
    $partnerData = loadPartner();
    $smarty->assign("partnerData", $partnerData);



    $smarty->display("partnerTransferBetweenview/add.html");
} elseif ($do == "add") {

    //here the permssion check
    include_once("../public/authentication.php");
    try {
        // here the function that do the action
        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);

    $partnerData = loadPartner();
    $smarty->assign("partnerData", $partnerData);

    $start_date = $_REQUEST['from'];
    $end_date = $_REQUEST['to'];
    $partnerid = $_REQUEST['partnerid'];
    $queryString = " ";


    if ($start_date && $end_date) {
        $queryString .= ' and  partnertransferbetween.partnerdate  >= "' . $start_date . '" and partnertransferbetween.partnerdate <= "' . $end_date . '" ';
    }

    if ($partnerid) {
        $queryString .= ' AND (partnertransferbetween.partneridfrom = ' . $partnerid . '
        or partnertransferbetween.partneridto = ' . $partnerid . ')';
    }





    $partnertransferbetweenData = $myPartnertransferbetweenEx->queryStringSearch($queryString);
    $smarty->assign('partnertransferbetweenData', $partnertransferbetweenData);
    $smarty->display("partnerTransferBetweenview/show.html");
}

//tempdelete
elseif ($do == "tempdelete") {
    include_once("../public/authentication.php");

    $partnertransferbetweenid = $_GET['partnertransferbetweenid'];

    try {
        // here the function that do the action
        tempdelete($partnertransferbetweenid);
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "edit") {
    include_once("../public/authentication.php");

    $loadData = edit();
    $smarty->assign("loadData", $loadData);
    $partnerData = loadPartner();
    $smarty->assign("partnerData", $partnerData);

    $smarty->display("partnerTransferBetweenview/edit.html");
} elseif ($do == "update") {
    include_once("../public/authentication.php");

    try {
        $partnertransferbetweenid = $_REQUEST['oldpartnertransferbetweenid'];
        tempdelete($partnertransferbetweenid);
        add();
        header("location:?do=sucess");
    } catch (Exception $e) {
        header("location:?do=error");
    }
} elseif ($do == "sucess") {
    //here the smarty templates
    $smarty->display("succes.html");
} elseif ($do == "error") {
    //here the smarty templates
    $smarty->display("error.html");
}
// $smarty->assign("customValidation", 1);
// $smarty->assign("customPartnerwithdrawal", 1);
// $smarty->assign("customAssets", 1);
//here the global templates
$smarty->display("footer.html");

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

//select all Partner data
function loadPartner() {
    //to use the variable out side the funcion
    global $myPartnerRecord;

    //load all Partner data
    $partnerData = $myPartnerRecord->queryByConditions(0);

    return $partnerData;
}

// add in Partnerwithdrawal tbl
function add() {
    //to use the variable out side the funcion
    global $myPartnertransferbetween;
    global $myPartnertransferbetweenRecord;
    global $userData;

    $partneridfrom = $_POST['partneridfrom'];
    $partneridto = $_POST['partneridto'];
    $partnervalue = $_POST['partnervalue'];
    $comment = $_POST['comment'];


    $partnerfromdata = R::findOne('partner', 'partnerid = ? ', [$partneridfrom]);
    $partnertodata = R::findOne('partner', 'partnerid = ? ', [$partneridto]);



    $myPartnertransferbetween->conditions = 0;
    $myPartnertransferbetween->partneridfrom = $partneridfrom;
    $myPartnertransferbetween->partneridto = $partneridto;
    $myPartnertransferbetween->partnerdate = date("Y-m-d");
    $myPartnertransferbetween->partnervalue = $partnervalue;
    $myPartnertransferbetween->partnerfrombefore = $partnerfromdata->partnermoney;
    $myPartnertransferbetween->partnerfromafter = $partnerfromdata->partnermoney - $partnervalue;
    $myPartnertransferbetween->partnertobefore = $partnertodata->partnermoney;
    $myPartnertransferbetween->partnertoafter = $partnertodata->partnermoney + $partnervalue;
    $myPartnertransferbetween->userid = $_SESSION["userid"];
    $myPartnertransferbetween->comment = $comment;
    $did = makePartnerTransferDailyEntry($partneridfrom, $partneridto, $partnervalue);
    $myPartnertransferbetween->dailyentryid = $did;
    $Partnertransferbetweenid = $myPartnertransferbetweenRecord->insert($myPartnertransferbetween);

    R::exec("UPDATE `partner` SET `partnermoney`= partnermoney - " . $partnervalue . "  WHERE partnerid = '" . $partneridfrom . "' ");
    R::exec("UPDATE `partner` SET `partnermoney`= partnermoney + " . $partnervalue . "  WHERE partnerid = '" . $partneridto . "' ");


    return $Partnertransferbetweenid;
}

function tempdelete($partnertransferbetweenid) {
    //to use the variable out side the funcion
    global $myPartnertransferbetween;
    global $myPartnertransferbetweenRecord;


    global $userData;
    $Partnertransferbetween = $myPartnertransferbetweenRecord->load($partnertransferbetweenid);






    R::exec("UPDATE `partnertransferbetween` SET `conditions`= 1 WHERE partnertransferbetweenid = '" . $partnertransferbetweenid . "' ");

    $partnerfromdata = R::findOne('partner', 'partnerid = ? ', [$Partnertransferbetween->partneridto]);
    $partnertodata = R::findOne('partner', 'partnerid = ? ', [$Partnertransferbetween->partneridfrom]);

    $myPartnertransferbetween->conditions = 1;
    $myPartnertransferbetween->partneridfrom = $Partnertransferbetween->partneridto;
    $myPartnertransferbetween->partneridto = $Partnertransferbetween->partneridfrom;
    $myPartnertransferbetween->partnerdate = date("Y-m-d");
    $myPartnertransferbetween->partnervalue = $Partnertransferbetween->partnervalue;
    $myPartnertransferbetween->partnerfrombefore = $partnerfromdata->partnermoney;
    $myPartnertransferbetween->partnerfromafter = $partnerfromdata->partnermoney + $Partnertransferbetween->partnervalue;
    $myPartnertransferbetween->partnertobefore = $partnertodata->partnermoney;
    $myPartnertransferbetween->partnertoafter = $partnertodata->partnermoney - $Partnertransferbetween->partnervalue;
    $myPartnertransferbetween->userid = $_SESSION["userid"];
    $myPartnertransferbetween->comment = $Partnertransferbetween->comment;
    $Partnertransferbetweenid = $myPartnertransferbetweenRecord->insert($myPartnertransferbetween);
    R::exec("UPDATE `partner` SET `partnermoney`= partnermoney + '" . $Partnertransferbetween->partnervalue . "'  WHERE partnerid = '" . $Partnertransferbetween->partneridfrom . "' ");
    R::exec("UPDATE `partner` SET `partnermoney`= partnermoney - '" . $Partnertransferbetween->partnervalue . "'  WHERE partnerid = '" . $Partnertransferbetween->partneridto . "' ");
    reverseEntryWithItsID($Partnertransferbetween->dailyentryid);
    return $Partnertransferbetweenid;
}

//edit from Partnerwithdrawal tbl
function edit() {
    global $myPartnertransferbetweenEx;

    $partnertransferbetweenid = $_GET['partnertransferbetweenid'];

    $loadData = $myPartnertransferbetweenEx->loadPartnerwithdrawalById($partnertransferbetweenid);
    return $loadData;
}

// update data in Partnerwithdrawal tbl
function update() {
    //to use the variable out side the funcion
    global $myPartnerwithdrawal;
    global $myPartnerwithdrawalRecord;
    global $myPartnerwithdrawalEx;

    //save
    global $mySave;
    global $mySaveRecord;
    //bank
    global $bank;
    global $bankDAO;
    global $accountDAO;
    //partner
    global $myPartner;
    global $myPartnerRecord;

    //tree
    global $accountsTreeDAO;
    global $accountsTree;

    //Dailyentry
    global $dailyEntry;
    global $dailyEntryDAO;

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

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

    global $userData;

    $partnerId = $_POST['partnerid'];
    $partnerwithdrawalValue = $_POST['partnerwithdrawalvalue'];
    $partnerwithdrawalId = $_POST['partnerwithdrawalid'];
    $partnerwithdrawalDate = $_POST['partnerwithdrawaldate'];
    $partenrwithdrawaltype = $_POST['partenrwithdrawaltype'];
    $conditions = $_POST['conditions'];
    //$saveId = $_POST['saveid'];
    $comment = $_POST['comment'];
    $partnertype = $_POST['partnertype'];
    $partnervalue = $_POST['partnervalue'];
    $dailyentryid = $_POST['dailyentryid'];

    $paymethod = (int) $_POST["paymethod"];
    if ($userData->searchinonesave == 0) {
        $saveid = (int) $_POST["saveid"];
    } else {
        $saveid = (int) $_SESSION['saveid'];
    }
    //  print_r('aaaaaaaaaaaaaaaaaaaaa' . $dailyentryid);

    $myPartnerwithdrawal = $myPartnerwithdrawalRecord->load($partnerwithdrawalId);
    $bankid = $myPartnerwithdrawal->bankid;
    $bankaccountid = $myPartnerwithdrawal->bankaccountid;
    $partnervalueOld = $myPartnerwithdrawal->partnerwithdrawalvalue;
    $saveValuebeforeValid = 1;
    /////////////////check save value before withdrawal//////////////////////

    if ($paymethod == 0 && $partenrwithdrawaltype == 1 && $partnerwithdrawalValue > 0) {
        //get save value
        //$saveValueBefore = getSaveValueBefore($_SESSION['saveid']);
        $saveValueBefore = getSaveValueBefore($saveid);


        $saveValueafterValid = $saveValueBefore - $partnerwithdrawalValue;
        if ($saveValueafterValid >= 0) {
            $saveValuebeforeValid = 1;
        } else {
            $saveValuebeforeValid = 0;
            return $saveValuebeforeValid;
        }
    }
    if ($paymethod == 0) {
        if ($partnertype == 0 && $partnervalueOld > 0) {
            $saveData = getSaveValueAndMins($saveid, $partnervalueOld);
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];


            //update saveFrom value after add transfer
            updateSave($saveId, $saveValueafter);
            insertSavedaily($saveValuebefore, $partnervalueOld, 1, $saveId, "تعديل شريك", $partnerwithdrawalId, $saveValueafter, "partnerwithdrawalController.php");
        } elseif ($partnertype == 1 && $partnervalueOld > 0) {
            $saveData = getSaveValueAndPlus($saveid, $partnervalueOld);
            $saveId = $saveData[0];
            $saveValuebefore = $saveData[1];
            $saveValueafter = $saveData[2];

            //update saveFrom value after add transfer
            updateSave($saveId, $saveValueafter);
            insertSavedaily($saveValuebefore, $partnervalueOld, 0, $saveId, "تعديل شريك", $partnerwithdrawalId, $saveValueafter, "partnerwithdrawalController.php");
        }
    } elseif ($paymethod == 1) {
        $dataBankAccount = $accountDAO->load($bankaccountid);
        //tree account
        $treeIdBankAccount = $dataBankAccount->treeId;

        if ($partnertype == 0 && $partnervalueOld > 0) {
            //mins transfermoneyValue
            //decrease account value and write in history
            $accValOld = $dataBankAccount->accountbeginingbalance;
            $dataBankAccount->accountbeginingbalance = $dataBankAccount->accountbeginingbalance - $partnervalueOld;
            $accountDAO->update($dataBankAccount);
            insertAccountmovement($accValOld, $partnervalueOld, 1, $dataBankAccount->accountbeginingbalance, "تعديل شريك", $partnerwithdrawalId, $dataBankAccount->accountid, $dataBankAccount->bankid, "partnerwithdrawalController.php");
        } elseif ($partnertype == 1 && $partnervalueOld > 0) {
            //plus transfermoneyValue
            $accValOld = $dataBankAccount->accountbeginingbalance;
            $dataBankAccount->accountbeginingbalance = $dataBankAccount->accountbeginingbalance + $partnervalueOld;
            $accountDAO->update($dataBankAccount);
            insertAccountmovement($accValOld, $partnervalueOld, 0, $dataBankAccount->accountbeginingbalance, "تعديل شريك", $partnerwithdrawalId, $dataBankAccount->accountid, $dataBankAccount->bankid, "partnerwithdrawalController.php");
        }
    }
    try {
        if (isset($partnerId) && $partnerId > 0) {

            //setcondition eq 1



            $myPartnerwithdrawal->conditions = 1;
            $myPartnerwithdrawal->userid = $_SESSION['userid'];
            $myPartnerwithdrawal->partnerwithdrawaldate = date("Y-m-d");
            $myPartnerwithdrawal->partnerwithdrawalid = $partnerwithdrawalId;

            $myPartnerwithdrawalEx->updateConditions($myPartnerwithdrawal);

            //insert new partnerwithdrawal
            //save and bank data coming with load
            $myPartnerwithdrawal->conditions = $conditions;
            $myPartnerwithdrawal->partnerid = $partnerId;
            $myPartnerwithdrawal->partnerwithdrawaldate = $partnerwithdrawalDate;
            $myPartnerwithdrawal->partnerwithdrawalvalue = $partnerwithdrawalValue;
            $myPartnerwithdrawal->saveid = $saveid;
            $myPartnerwithdrawal->userid = $_SESSION["userid"];
            $myPartnerwithdrawal->partenrwithdrawaltype = $partenrwithdrawaltype;
            $myPartnerwithdrawal->comment = $comment;
            $myPartnerwithdrawal->dailyentryid = $dailyentryid;
            $partnerwithdrawalId = $myPartnerwithdrawalRecord->insert($myPartnerwithdrawal);

            ///affect
            $treeIdBankAccount = 0;
            if ($paymethod == 0) {
                if ($partenrwithdrawaltype == 1 && $partnerwithdrawalValue > 0) {
                    //get saveFromValue before and saveFromValue after mins transfermoneyValue
                    $saveData = getSaveValueAndMins($saveid, $partnerwithdrawalValue);
                    $saveId = $saveData[0];
                    $saveValuebefore = $saveData[1];
                    $saveValueafter = $saveData[2];

                    //update saveFrom value after add transfer
                    updateSave($saveId, $saveValueafter);

                    //insert into savedaily tbl from savefrom
                    insertSavedaily($saveValuebefore, $partnerwithdrawalValue, 1, $saveId, "اضافة مسحوبات شركاء", $partnerwithdrawalId, $saveValueafter, "partnerwithdrawalController.php");
                } elseif ($partenrwithdrawaltype == 0 && $partnerwithdrawalValue > 0) {
                    //get saveFromValue before and saveFromValue after mins transfermoneyValue
                    $saveData = getSaveValueAndPlus($saveid, $partnerwithdrawalValue);
                    $saveId = $saveData[0];
                    $saveValuebefore = $saveData[1];
                    $saveValueafter = $saveData[2];


                    //update saveFrom value after add transfer
                    updateSave($saveId, $saveValueafter);

                    //insert into savedaily tbl from savefrom
                    insertSavedaily($saveValuebefore, $partnerwithdrawalValue, 0, $saveId, "اضافة ايداع شركاء", $partnerwithdrawalId, $saveValueafter, "partnerwithdrawalController.php");
                }
            } elseif ($paymethod == 1) {
                $dataBankAccount = $accountDAO->load($bankaccountid);
                //tree account
                $treeIdBankAccount = $dataBankAccount->treeId;




                $dailyEntryCreditor->accountstreeid = $treeIdBankAccount;



                if ($partenrwithdrawaltype == 1 && $partnerwithdrawalValue > 0) {
                    //mins transfermoneyValue
                    //decrease account value and write in history
                    $accValOld = $dataBankAccount->accountbeginingbalance;
                    $dataBankAccount->accountbeginingbalance = $dataBankAccount->accountbeginingbalance - $partnerwithdrawalValue;
                    $accountDAO->update($dataBankAccount);
                    insertAccountmovement($accValOld, $partnerwithdrawalValue, 1, $dataBankAccount->accountbeginingbalance, "اضافة مسحوبات شركاء", $partnerwithdrawalId, $dataBankAccount->accountid, $dataBankAccount->bankid, "partnerwithdrawalController.php");
                } elseif ($partenrwithdrawaltype == 0 && $partnerwithdrawalValue > 0) {
                    //plus transfermoneyValue
                    $accValOld = $dataBankAccount->accountbeginingbalance;
                    $dataBankAccount->accountbeginingbalance = $dataBankAccount->accountbeginingbalance + $partnerwithdrawalValue;
                    $accountDAO->update($dataBankAccount);
                    insertAccountmovement($accValOld, $partnerwithdrawalValue, 0, $dataBankAccount->accountbeginingbalance, "اضافة ايداع شركاء", $partnerwithdrawalId, $dataBankAccount->accountid, $dataBankAccount->bankid, "partnerwithdrawalController.php");
                }
            }




            $flag = reverseEntryWithItsID($dailyentryid);

            $dailyEntry->entryComment = 'تم تعديل الشريك';

            //////////////// start partner data

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

            $dataPartner = $myPartnerRecord->load($partnerId);

            $idTreePartner = $dataPartner->treeId;




            //end  partner data
            //
            //////////////// start save data

            $dailyEntryCreditorArray = array();
            $dailyEntryCreditor->value = $partnerwithdrawalValue;
            //save session
            //$saveid = $_SESSION['saveid'];
            $saveid = $saveid;
            $dataSave = $mySaveRecord->load($saveid);

            $idTreeSave = $dataSave->treeId;
            //
            $treeIdBankAccount = $treeIdBankAccount;


            //end  partner data

            if ($partenrwithdrawaltype == 0) {
                $dailyEntryCreditor->accountstreeid = $idTreePartner;
                $dailyEntryDebtor->accountstreeid = $idTreeSave;
                if ($paymethod == 1) {
                    $dailyEntryDebtor->accountstreeid = $treeIdBankAccount;
                }
            } else {
                $dailyEntryCreditor->accountstreeid = $idTreeSave;
                if ($paymethod == 1) {
                    $dailyEntryCreditor->accountstreeid = $treeIdBankAccount;
                }
                $dailyEntryDebtor->accountstreeid = $idTreePartner;
            }
            array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
            array_push($dailyEntryDebtorArray, $dailyEntryDebtor);

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

            $myPartnerwithdrawal->dailyentryid = $did;
            $myPartnerwithdrawalRecord->update($myPartnerwithdrawal);
        }
    } catch (Exception $e) {
        $note = "حدث خطأ أثناء اعادة المسحوبات";
        $saveValuebeforeValid = 2;
    }


    return $saveValuebeforeValid;
}

function makePartnerTransferDailyEntry($partneridfrom, $partneridto, $partnerMoney) {
    $partnerFrom = R::getRow('select * from partner where partnerid=' . $partneridfrom);
    $partnerTo = R::getRow('select * from partner where partnerid=' . $partneridto);
    $dailyEntry->entryComment = 'تحويل من الشريك ' . $partnerFrom['partnername'] . ' الى الشريك ' . $partnerTo['partnername'] . '';

    //  //الي حساب الخزينة
    $dailyEntryDebtorArray = array();
    $dailyEntryDebtor->value = $partnerMoney;
    $dailyEntryDebtor->accountstreeid = $partnerFrom['treeId'];


    //من حساب جاري الشركاء (اسم الشريك)
    $dailyEntryCreditorArray = array();
    $dailyEntryCreditor->value = $partnerMoney;
    $dailyEntryCreditor->accountstreeid = $partnerTo['treeId'];
    array_push($dailyEntryCreditorArray, $dailyEntryCreditor);
    array_push($dailyEntryDebtorArray, $dailyEntryDebtor);
    $returnedData = insertEntery($dailyEntry, $dailyEntryDebtorArray, $dailyEntryCreditorArray);

    $did = $returnedData[1];
    return $did;
}

?>