<?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");


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

//Shippercompanies
require_once('../models/dao/ShippercompaniesDAO.class.php');
require_once('../models/dto/Shippercompanie.class.php');
require_once('../models/mysql/ShippercompaniesMySqlDAO.class.php');
require_once('../models/mysql/ext/ShippercompaniesMySqlExtDAO.class.php');

//Representativecompany
require_once('../models/dao/RepresentativecompanyDAO.class.php');
require_once('../models/dto/Representativecompany.class.php');
require_once('../models/mysql/RepresentativecompanyMySqlDAO.class.php');
require_once('../models/mysql/ext/RepresentativecompanyMySqlExtDAO.class.php');

//Policy
require_once('../models/dao/PolicyDAO.class.php');
require_once('../models/dto/Policy.class.php');
require_once('../models/mysql/PolicyMySqlDAO.class.php');
require_once('../models/mysql/ext/PolicyMySqlExtDAO.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');
//Supplier
require_once('../models/dao/SupplierDAO.class.php');
require_once('../models/dto/Supplier.class.php');
require_once('../models/mysql/SupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierMySqlExtDAO.class.php');
//Client
require_once('../models/dao/ClientDAO.class.php');
require_once('../models/dto/Client.class.php');
require_once('../models/mysql/ClientMySqlDAO.class.php');
require_once('../models/mysql/ext/ClientMySqlExtDAO.class.php');

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


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

  Controller Name :- userCTRL

  OPERTATION in Controller

  1-display add form
  2- add in user tble
  3-display sucess or error
  4-display show form
  5-delete from user tbl
  6-edit in user data

  ======================== */
//here goes the instances and general variables
//Shippercompanies
$myShippercompanies = new Shippercompanie();
$myShippercompaniesRecord = new ShippercompaniesMySqlDAO();
$myShippercompaniesEx = new ShippercompaniesMySqlExtDAO();

//Representativecompany
$myRepresentativecompany = new Representativecompany();
$myRepresentativecompanyRecord = new RepresentativecompanyMySqlDAO();

//Policy
$myPolicy = new Policy();
$myPolicyRecord = new PolicyMySqlDAO();
$myPolicyEx = new PolicyMySqlExtDAO();


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

//savedaily
$mySavedaily = new Savedaily();
$mySavedailyRecord = new SavedailyMySqlDAO();
$mySavedailyEx = new SavedailyMySqlExtDAO();
//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();

//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();


//check and use the condetion that suite this action
if ($do == 'getSuppliers') {
    $suppliersData = getSuppliers(); //undeleted suppliers

    $data = '<option value="-1">اختر المورد</option>';
    foreach ($suppliersData as $supplier) {
        $data .= '<option value="' . $supplier->suppliername . '" data-id="' . $supplier->supplierid . '">' . $supplier->suppliername . '</option>';
    }
    print_r($data);
} elseif ($do == 'getCustomers') {
    $customersData = getCustomers(); //undeleted suppliers

    $data = '<option value="-1">اختر العميل</option>';
    foreach ($customersData as $customer) {
        $data .= '<option value="' . $customer->clientname . '" data-id="' . $customer->clientid . '">' . $customer->clientname . '</option>';
    }
    print_r($data);
} elseif ($do == 'dopay') {
    $policyId = (int) $_POST['policy'];
    $saveId = (int) $_POST['save'];
    $ret = 0;
    if ($policyId > 0 && $saveId > 0) {
        $policy = $myPolicyRecord->load($policyId);
        if ($policy->affectSave == 1 && $policy->senderId > 0) {//didnot pay in add, there is id
            $last_clientdebtchangeid = (int) R::getCell('SELECT clientdebtchangeid FROM clientdebtchange order by clientdebtchangeid desc limit 1');
            if ($policy->senderType == 1) {//client
                $clientdebt = (float) R::getCell('SELECT clientdebt from client where clientid =' . $policy->senderId);
                $dataToSend = array(
                    'clientorsupplier' => 1,
                    'clientid' => $policy->senderId,
                    'notPremium' => 'notPremium',
                    'saveorbank' => 1,
                    'saveid' => $saveId,
                    'voucher' => ($last_clientdebtchangeid + 1),
                    'oldvoucher' => ($last_clientdebtchangeid + 1),
                    'lastDate' => date('Y-m-d H:i:s'),
                    'clientnegative' => 1,
                    'currentDate' => date('Y-m-d H:i:s'),
                    'debtBefore' => $clientdebt,
                    'currencyId' => 1,
                    'cFactor' => 1,
                    'payedDept' => $policy->policyCost,
                    'payedDeptCurr' => $policy->policyCost,
                    'debtAfter' => ($clientdebt - $policy->policyCost),
                    'comment' => "دفع قيمة بوليصة رقم $policy->policyId"
                );

                $ret = CURL_Request($dataToSend, 'clientPayedDeptController.php?do=add', 0, 1);
                if ($ret > 0) {// ret here is clientdebtchangeid "paymentid"
                    $policy->affectSave = 0;
                    $myPolicyRecord->update($policy);
                }
            } elseif ($policy->senderType == 2) {//supplier
                $suppdebt = (float) R::getCell('SELECT suppliercurrentDebt from supplier where supplierid =' . $policy->senderId);
                $dataToSend = array(
                    'clientorsupplier' => 1,
                    'supplierid' => $policy->senderId,
                    'saveorbank' => 1,
                    'saveid' => $saveId,
                    'voucher' => ($last_clientdebtchangeid + 1),
                    'lastDate' => date('Y-m-d H:i:s'),
                    'suplliernegative' => 1,
                    'currentDate' => date('Y-m-d H:i:s'),
                    'debtBefore' => $suppdebt,
                    'currencyId' => 1,
                    'cFactor' => 1,
                    'otherCFactor' => 1,
                    'payedDept' => $policy->policyCost,
                    'payedDeptCurr' => $policy->policyCost,
                    'debtAfter' => ($suppdebt - $policy->policyCost),
                    'comment' => "دفع قيمة بوليصة رقم $policy->policyId",
                    'checkdate' => date('Y-m-d H:i:s')
                );

                $ret = CURL_Request($dataToSend, 'supplierPayedDeptController.php?do=add', 0, 1);
                if ($ret > 0) {// ret here is supplierDeptChangeId "paymentid"
                    $policy->affectSave = 0;
                    $myPolicyRecord->update($policy);
                }
            }
        }
    }
    echo $ret;
}
/* ===============================
  function in this CONTROLLER
  ================================ */

function getSuppliers() {
    global $supplierDAO;
    $suppliersData = $supplierDAO->queryByCondition(0);
    return $suppliersData;
}

function getCustomers() {
    global $clientDAO;
    $clientData = $clientDAO->queryByCondition(0);
    return $clientData;
}

?>