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

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

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


  Controller Name :- clientPayedDeptControllerAjax

  OPERTATION in Controller

  1- get client dept and last operation date
  ======================== */

//here goes the instances and general variables
//Client
$client = new Client();
$clientDAO = new ClientMySqlDAO();
$clientExt = new ClientMySqlExtDAO();
//Clientdebtchange
$clientDeptChange = new Clientdebtchange();
$clientDeptChangeDAO = new ClientdebtchangeMySqlDAO();
$clientDeptChangeExt = new ClientdebtchangeMySqlExtDAO();

$today = date("y-m-d");

//check and use the condition that suite this action
if (empty($do) || $do == "show") {
    //here the permssion check


    $clientData = getClientData();
} elseif ($do == 'checkserail') {

    $serail = $_GET["serail"];

    $mydata = $clientDeptChangeExt->showbymodelid($serail);
    if (count($mydata) > 0) {
        echo '1';
    } else {
        echo '0';
    }
} elseif ($do == 'checkpayserialno') {

    $serail = $_GET["serail"];
    $count = R::getCell('select count(paySerialNo) from clientdebtchange where paySerialNo = ' . $serail);
    if ($count > 0) {
        echo '1';
    } else {
        echo '0';
    }
} elseif ($do == 'checkclient') {
    $clientid = $_POST['clientid'];
    $clientData = $clientDeptChangeExt->selectByClientId($clientid);
    if (!empty($clientData)) {
        echo -1;
    } elseif (empty($clientData)) {
        echo 0;
    }
} elseif ($do == 'select2receipts') {
    select2receipts();
} elseif ($do == 'select2paySerialNo') {
    select2paySerialNo();
}

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

function getClientData() {
    global $clientDAO;
    global $clientDeptChangeExt;
    $clientId = $_GET['id'];
    $clientData = $clientDAO->load($clientId);
    $clientdebt = $clientData->clientdebt;
    $clientData = $clientDeptChangeExt->selectByClientIdOrderByDateDESC($clientId);
    $clientdate = $clientData->clientdebtchangedate;

    $data = array('clientdebt' => $clientdebt, 'clientdate' => $clientdate);

    echo json_encode($data);
}

function select2receipts() {
    global $clientDeptChangeExt;

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

    $name = $_GET['term'];
    $limit = 15; //intval($_GET['page_limit']);

    $queryString = '';
    if ($name != "") {//first char is * search in client code only
        $queryString = ' and clientdebtchangemodelid LIKE "%' . $name . '%" ';
    }
    $allmydata = $clientDeptChangeExt->queryreceiptSimple($queryString, ' limit 15 ');
    if (count($allmydata) > 0) {
        foreach ($allmydata as $data) {
            $row_array['id'] = $data->clientdebtchangemodelid;
            $row_array['text'] = $data->clientdebtchangemodelid;
            array_push($return_arr, $row_array);
        }
    }

    echo json_encode($return_arr);
}

function select2paySerialNo() {
    global $clientDeptChangeExt;

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

    $name = $_GET['term'];
    $limit = 15; //intval($_GET['page_limit']);

    $queryString = '';
    if ($name != "") {//first char is * search in client code only
        $queryString = ' and paySerialNo LIKE "%' . $name . '%" ';
    }

    $allmydata = R::getAll('SELECT distinct paySerialNo
		FROM clientdebtchange
		WHERE clientdebtchange.tablename = "clientPayedDeptController.php" and paySerialNo > 0
		' . $queryString . ' limit 15 ');
    foreach ($allmydata as $data) {
        $row_array['id'] = $data['paySerialNo'];
        $row_array['text'] = $data['paySerialNo'];
        array_push($return_arr, $row_array);
    }

    echo json_encode($return_arr);
}

?>