<?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");
//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');
//Supplierdebtchange
require_once('../models/dao/SupplierdebtchangeDAO.class.php');
require_once('../models/dto/Supplierdebtchange.class.php');
require_once('../models/mysql/SupplierdebtchangeMySqlDAO.class.php');
require_once('../models/mysql/ext/SupplierdebtchangeMySqlExtDAO.class.php');

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

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

  Controller Name :- supplierPayedDeptController

  OPERTATION in Controller

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

//here goes the instances and general variables
//Supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierExt = new SupplierMySqlExtDAO();
//Supplierdebtchange
$supplierDeptChange = new Supplierdebtchange();
$supplierDeptChangeDAO = new SupplierdebtchangeMySqlDAO();
$supplierDeptChangeExt = new SupplierdebtchangeMySqlExtDAO();

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

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


    $supplierData = getSupplierData();
}elseif ($do == 'checkserail') {

    $serail = $_GET["serail"];

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

    $serail = $_GET["serail"];
    $count = R::getCell('select count(paySerialNo) from supplierdebtchange where paySerialNo = ' . $serail);
    if ($count > 0) {
        echo '1';
    } else {
        echo '0';
    }
} elseif ($do == 'select2paySerialNo') {
    select2paySerialNo();
}

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

function getSupplierData() {
    global $supplierDAO;
    $supplierId = $_GET['id'];
    $supplierData = $supplierDAO->load($supplierId);
    $suppliercurrentDebt = $supplierData->suppliercurrentDebt;
    $supplierdate = $supplierData->supplierdate;

    $data = array('suppliercurrentDebt' => $suppliercurrentDebt, 'supplierdate' => $supplierdate);

    echo json_encode($data);
}
function select2paySerialNo() {
    global $supplierDeptChangeExt;

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

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

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

    $allmydata = R::getAll('SELECT distinct paySerialNo
		FROM supplierdebtchange
		WHERE supplierdebtchange.tablename = "supplierPayedDeptController.php" and paySerialNo > 0
		' . $queryString . ' order by paySerialNo desc 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);
}
?>