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

//Dated Checked
require_once('../models/dao/DatedcheckedDAO.class.php');
require_once('../models/dto/Datedchecked.class.php');
require_once('../models/mysql/DatedcheckedMySqlDAO.class.php');
require_once('../models/mysql/ext/DatedcheckedMySqlExtDAO.class.php');

//clients
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 :- datedChecked Controller
  ======================== */

//here goes the instances and general variables
//datedChecked
$datedChecked = new Datedchecked();
$datedCheckedDAO = new DatedcheckedMySqlDAO();
$datedCheckedEX = new DatedcheckedMySqlExtDAO();

/* --------------------------------------------------------------------------------------------------------- */

//Checkcontroller
//check and use the condetion that suite this action
if (empty($do)) {
} elseif ($do == "checkName") {

    checkName();
} elseif ($do == "showClientAjax") {
    showClientAjax();
} elseif ($do == "showSupplierAjax") {
    showSupplierAjax();
} elseif ($do == 'checkByAccAndChkNo') {
    $accountNo = $_POST['accountNo'];
    $checkNo = $_POST['checkNo'];

    $checkData = $datedCheckedEX->checkWithAccIdAndChkNo($accountNo, $checkNo);
    if (!empty($checkData)) {
        echo -1;
    } elseif (empty($checkData)) {
        echo 0;
    }
}


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

function showClientAjax()
{

    $columns = array('datedchecked.datedCheckedID', 'client.clientname', 'datedchecked.bankName', 'checkdate', 'accountNo', 'checkValue', 'checkNo', 'dueDate', 'datedchecked.done', 'datedchecked.done', 'paperType');
    $ordercol = $_POST["ordercol"];
    $orderdir = $_POST["orderdir"];
    $search = $_POST["searchval"];
    $start_date = $_POST["start_date"];
    $end_date = $_POST["end_date"];
    $clientid = $_POST["clientid"];
    $bankid = $_POST["bankid"];
    $due_start_date = $_POST["due_start_date"];
    $due_end_date = $_POST["due_end_date"];
    
    $searchQuery = " ";
    if ($start_date != '' && $end_date != '') {
        $searchQuery .= ' and  datedchecked.operationDate  >= "' . $start_date . '" and datedchecked.operationDate <= "' . $end_date . '" ';
    }

    if ($due_start_date != '') {
        $searchQuery .= ' and datedchecked.dueDate >= "' . $due_start_date . '" ';
    }
    if ($due_end_date != '') {
        $searchQuery .= ' and datedchecked.dueDate <= "' . $due_end_date . '" ';
    }

    if ($bankid > 0) {
        $searchQuery .=  " and datedchecked.bankid = " . $bankid . " ";
    }

    if ($clientid > 0) {
        $searchQuery .= " and datedchecked.clientNum = " . $clientid . " ";
    }




    if (isset($search) && $search != "") {
        $searchQuery .= " and ( ";
        for ($i = 1; $i < count($columns); $i++) {
            $searchQuery .= $columns[$i] . " LIKE '%" . trim($search) . "%' OR ";
        }
        $searchQuery .= "  datedchecked.datedCheckedID LIKE '%" . trim($search) . "%' ) ";
    }


    if ($ordercol) {
        $searchQuery .= 'ORDER BY ' . $columns[$ordercol] . ' ' . $orderdir . '  ';
    } else {
        $searchQuery .= "ORDER BY  datedchecked.datedCheckedID DESC ";
    }


    $totals = R::getCell("SELECT COUNT(*)  FROM datedchecked INNER JOIN client on datedchecked.clientNum = client.clientid  WHERE datedchecked.addType = 0 $searchQuery");


    if (isset($start) && $length != '-1') {
        $searchQuery .= "LIMIT " . intval($start) . ", " .
            intval($length);
    }



    $rResult = R::getAll("SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $columns)) . "
            FROM datedchecked INNER JOIN client on datedchecked.clientNum = client.clientid  WHERE datedchecked.addType = 0 $searchQuery");
    $output = array(
        "draw" => intval(filter_input(INPUT_POST, 'draw')),
        "recordsTotal" => intval($totals),
        "recordsFiltered" => count($rResult),
        "data" => array()
    );
    $notPayVal = 0;
    foreach ($rResult as $row) {
        $sub_array = array();
        $sub_array[] = $row["clientname"];
        $sub_array[] = $row["bankName"];
        $sub_array[] = $row["checkdate"];
        $sub_array[] = $row["accountNo"];
        $sub_array[] = $row["checkValue"];
        $sub_array[] = $row["checkNo"];
        $sub_array[] = $row["dueDate"];
        if ($row["done"] == 1 && $row["paperType"] == 1) {
            $sub_array[] = '<a href="?do=payCheckForSup&id=' . $row["datedCheckedID"] . '&clientid=1" type="button"   class="btn btn-default btn-lg editicon"></a>';
            $sub_array[] = 'دفع شيك';
        } else if ($row["done"] == 0) {
            $sub_array[] = '<a href="?do=payCheck&id=' . $row["datedCheckedID"] . '" type="button"   class="btn btn-default btn-lg editicon"></a>';
            $sub_array[] = ' <a href="?do=endorse&id=' . $row["datedCheckedID"] . '" type="button"   class="btn btn-default btn-lg moneyicon"></a>';
            $sub_array[] = '<a  href="#" id="delbtn" data-id="' . $row["datedCheckedID"] . '" data-paper="' . $row["paperType"] . '" type="button" class="btn btn-default btn-lg deleteicon conformdel1"></a>';
            $notPayVal += $row["checkValue"];
        } else if ($row["done"] == 1) {
            $sub_array[] = '   لقد تم تسديد الشيك ';
            $sub_array[] = 'لقد تم تسديد الشيك';
            $sub_array[] = ' لقد تم تسديد الشيك';
        } else if ($row["done"] == 3) {
            $sub_array[] = ' لقد تم تظهير الشيك';
            $sub_array[] = 'لقد تم تظهير الشيك';
            $sub_array[] = ' لقد تم تظهير الشيك';
        } else {
            $sub_array[] = ' لقد تم الغاء الشيك';
            $sub_array[] = '    لقد تم الغاء الشيك';
            $sub_array[] = 'لقد تم الغاء الشيك';
        }
        $sub_array[] = $row["done"];
        $sub_array[] = $row["paperType"];


        $output['data'][] = $sub_array;
    }
    $sub_array2 = array();
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '<h2>  إجمالي القيم :     </h2> ';
    $sub_array2[] = $notPayVal;
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $output['data'][] = $sub_array2;
    echo json_encode($output, JSON_UNESCAPED_SLASHES);
}


function showSupplierAjax()
{

    $columns = array('datedchecked.datedCheckedID', 'supplier.suppliername', 'datedchecked.bankName', 'checkdate', 'accountNo', 'checkValue', 'checkNo', 'dueDate', 'datedchecked.done', 'datedchecked.done', 'paperType');
    $ordercol = $_POST["ordercol"];
    $orderdir = $_POST["orderdir"];
    $search = $_POST["searchval"];
    $start_date = $_POST["start_date"];
    $end_date = $_POST["end_date"];
    $supplierid = $_POST["supplierid"];
    $bankid = $_POST["bankid"];
    $due_start_date = $_POST["due_start_date"];
    $due_end_date = $_POST["due_end_date"];
    $searchQuery = " ";
    if ($start_date != '' && $end_date != '') {
        $searchQuery .= ' and  datedchecked.operationDate  >= "' . $start_date . '" and datedchecked.operationDate <= "' . $end_date . '" ';
    }

    if ($due_start_date != '') {
        $searchQuery .= ' and datedchecked.dueDate >= "' . $due_start_date . '" ';
    }
    if ($due_end_date != '') {
        $searchQuery .= ' and datedchecked.dueDate <= "' . $due_end_date . '" ';
    }

    if ($bankid > 0) {
        $searchQuery .=  " and datedchecked.bankid = " . $bankid . " ";
    }

    if ($supplierid > 0) {
        $searchQuery .= " and datedchecked.clientNum = " . $supplierid . " ";
    }




    if (isset($search) && $search != "") {
        $searchQuery .= " and ( ";
        for ($i = 1; $i < count($columns); $i++) {
            $searchQuery .= $columns[$i] . " LIKE '%" . trim($search) . "%' OR ";
        }
        $searchQuery .= "  datedchecked.datedCheckedID LIKE '%" . trim($search) . "%' ) ";
    }


    if ($ordercol) {
        $searchQuery .= 'ORDER BY ' . $columns[$ordercol] . ' ' . $orderdir . '  ';
    } else {
        $searchQuery .= "ORDER BY  datedchecked.datedCheckedID DESC ";
    }


    $totals = R::getCell("SELECT COUNT(*)  FROM datedchecked INNER JOIN supplier on datedchecked.clientNum = supplier.supplierid  WHERE datedchecked.addType = 1 $searchQuery");


    if (isset($start) && $length != '-1') {
        $searchQuery .= "LIMIT " . intval($start) . ", " .
            intval($length);
    }



    $rResult = R::getAll("SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $columns)) . "
            FROM datedchecked INNER JOIN supplier on datedchecked.clientNum = supplier.supplierid  WHERE datedchecked.addType = 1 $searchQuery");
    $output = array(
        "draw" => intval(filter_input(INPUT_POST, 'draw')),
        "recordsTotal" => intval($totals),
        "recordsFiltered" => count($rResult),
        "data" => array()
    );
    $notPayVal = 0;
    foreach ($rResult as $row) {
        $sub_array = array();
        $sub_array[] = $row["suppliername"];
        $sub_array[] = $row["bankName"];
        $sub_array[] = $row["checkdate"];
        $sub_array[] = $row["accountNo"];
        $sub_array[] = $row["checkValue"];
        $sub_array[] = $row["checkNo"];
        $sub_array[] = $row["dueDate"];
        if ($row["done"] == 1 && $row["paperType"] == 1) {
            $sub_array[] = '<a href="?do=payCheckForSup&id=' . $row["datedCheckedID"] . '&supplierid=1" type="button"   class="btn btn-default btn-lg editicon"></a>';

            $sub_array[] = 'دفع شيك';
        } else if ($row["done"] == 0) {
            $sub_array[] = '<a href="?do=payCheckForSup&id=' . $row["datedCheckedID"] . '" type="button"   class="btn btn-default btn-lg editicon"></a>';
            $sub_array[] = '<a  href="#" id="delbtn" data-id="' . $row["datedCheckedID"] . '" data-paper="' . $row["paperType"] . '" type="button" class="btn btn-default btn-lg deleteicon conformdel1"></a>';
            $notPayVal += $row["checkValue"];
        } else if ($row["done"] == 1) {
            $sub_array[] = '   لقد تم تسديد الشيك ';
            $sub_array[] = ' لقد تم تسديد الشيك';
        } else {
            $sub_array[] = ' لقد تم الغاء الشيك';
            $sub_array[] = 'لقد تم الغاء الشيك';
        }
        $sub_array[] = $row["done"];
        $sub_array[] = $row["paperType"];


        $output['data'][] = $sub_array;
    }
    $sub_array2 = array();
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '<h2>  إجمالي القيم :     </h2> ';
    $sub_array2[] = $notPayVal;
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $sub_array2[] = '';
    $output['data'][] = $sub_array2;
    echo json_encode($output, JSON_UNESCAPED_SLASHES);
}

function checkName()
{
    global $datedCheckedDAO;

    $flag = '0';
    $assetname = $_GET['assetname'];
    if (!empty($assetname)) {
        if (count($myAssetsRecord->queryByAssetsName($assetname)) > 0) {
            $flag = '1';
        }
    }
    echo $flag;
}
