<?php

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

//global varable
global $showoutside;



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

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


require_once('../models/dao/TypesupplierDAO.class.php');
require_once('../models/dto/Typesupplier.class.php');
require_once('../models/mysql/TypesupplierMySqlDAO.class.php');
require_once('../models/mysql/ext/TypesupplierMySqlExtDAO.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');

$ProgramsettingDAO = new ProgramsettingsMySqlDAO();


$type = new Typesupplier();
$TypeSupplierDAO = new TypesupplierMySqlDAO();
$TypeSupplierEX = new TypesupplierMySqlExtDAO();

//supplier
$supplier = new Supplier();
$supplierDAO = new SupplierMySqlDAO();
$supplierEX = new SupplierMySqlExtDAO();


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

  Controller Name :- clientsAndProductsReportController تقرير اقساط عملاء

  OPERTATION in Controller

  1- show report
  2- search with clients and products and date

  ======================== */
if ($do == "show" || empty($do)) {
    //here the permission check
    include_once("../public/authentication.php");

    $Programsetting = $ProgramsettingDAO->load(1);
    $smarty->assign("Programsettingdata", $Programsetting);


    $TypeSupplierData = getTypeclient();
    $smarty->assign("TypeSupplierData", $TypeSupplierData);


    $typesupplierId = $_REQUEST['typesupplierId'];


    $message = '';

    if ($typesupplierId != '' && $typesupplierId != '-1') {
        if($typesupplierId == 0){
          $message .= " نوع الموردين: " . 'الكل';
          $allData = $supplierEX->getSupplierByQuery();
        } else {
          $TypeClientData = $TypeSupplierDAO->load($typesupplierId);
          $TypeClientName = $TypeClientData->typeName;
          $message .= " نوع الموردين: " . $TypeClientName;
          $allData = $supplierEX->getSupplierByType($typesupplierId);
        }
    }


  if(count($allData) > 0){
      foreach ($allData as $data) {
        $lastPayed = R::getRow("SELECT supplierdebtchangeamount, supplierdebtchangedate FROM supplierdebtchange 
          WHERE supplierid = ? 
          AND tablename = 'supplierPayedDeptController.php'
          ORDER BY supplierdebtchangedate DESC LIMIT 1", [$data->supplierid]);
        if($lastPayed){
          $data->supplierdate = $lastPayed['supplierdebtchangedate'] ? $lastPayed['supplierdebtchangedate'] : '-';
          $data->currencyName = $lastPayed['supplierdebtchangeamount'] ? $lastPayed['supplierdebtchangeamount'] : 0;
        }
        $lastDeficit = R::getRow("SELECT supplierdebtchangedate FROM supplierdebtchange 
          WHERE supplierid = ? 
          AND tablename = 'supplierDeficitController.php'
          ORDER BY supplierdebtchangedate DESC LIMIT 1", [$data->supplierid]);
          $data->typesupplier_id = $lastDeficit['supplierdebtchangedate'] ? $lastDeficit['supplierdebtchangedate'] : '-';
        
          $lastBuybill = R::getRow('select * from buybill where buybillsupplierid = ? order by buybillid desc limit 1', [$data->supplierid]);
          $data->sullpierCurrencyid = $lastBuybill['buybilldate'] ? $lastBuybill['buybilldate'] : '-';
          $data->suppliercurrentDebtInCurrency = $lastBuybill['buybilltotalbill'] ? $lastBuybill['buybilltotalbill'] : 0;

          $lastBuybillRet = R::getRow('select * from returnbuybill where returnbuybillsupplierid = ? order by returnbuybillid desc limit 1', [$data->supplierid]);
          $data->webApiId = $lastBuybillRet['returnbuybilldate'] ? $lastBuybillRet['returnbuybilldate'] : '-';
          $data->treeId = $lastBuybillRet['returnbuybilltotalbill'] ? $lastBuybillRet['returnbuybilltotalbill'] : 0;
    }

  }
    $smarty->assign("allData", $allData);
    $smarty->assign("message", $message);

    //here the smarty templates
    $smarty->assign("clientPremium", 1);
    $smarty->display("typesupplierReportview/showNew.html");

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

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

function getTypeclient() {
    global $TypeSupplierEX;
    $TypeSupplierData = $TypeSupplierEX->queryAllsup();
    return $TypeSupplierData;
}





?>