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


//MMaintenancereceipt
require_once('../models/dao/MMaintenancereceiptDAO.class.php');
require_once('../models/dto/MMaintenancereceipt.class.php');
require_once('../models/mysql/MMaintenancereceiptMySqlDAO.class.php');
require_once('../models/mysql/ext/MMaintenancereceiptMySqlExtDAO.class.php');
//MFinishingpayed
require_once('../models/dao/MFinishingpayedDAO.class.php');
require_once('../models/dto/MFinishingpayed.class.php');
require_once('../models/mysql/MFinishingpayedMySqlDAO.class.php');
require_once('../models/mysql/ext/MFinishingpayedMySqlExtDAO.class.php');



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

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

  Controller Name :- productController

  OPERTATION in Controller

  1- display add form, add supplier cat and supplier with ajax
  2- add in supplier tbl
  2.1- insert into supplier unit tbl
  2.2- insert into storedetail tbl
  2.3- insert new row into storereport tbl
  3- display show and tempdelete
  4- edit supplier
  5- update supplier
  5.1- update supplier tbl with supplierid
  5.2- delete supplier units associated to this supplier from productunit tbl, then insert it again

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

//here goes the instances and general variables
$mMaintenanceReceiptEX = new MMaintenancereceiptMySqlExtDAO();
$mMaintenanceReceiptDAO = new MMaintenancereceiptMySqlDAO();

$mFinishingPayedEX = new MFinishingpayedMySqlExtDAO();


//check and use the condition that suite this action
if (empty($do)) {
    //here the permssion check
    try {
        $ReceiptsData = getMaintenanceReceipts();
        $smarty->assign("ReceiptsData", $ReceiptsData);

        //here the smarty templates
        $smarty->display("maintennanceSettlementView/receipts.html");
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "getPayments") {

    try {
        $paymentsData = getPayments();
        $itr = $_REQUEST["itr"];

        $smarty->assign("itr", $itr);
        $smarty->assign("paymentsData", $paymentsData);
        $smarty->display("maintennanceSettlementView/payments.html");
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "getTotalCost") {
    try {
        $id = $_REQUEST["id"];
        $result = $mMaintenanceReceiptEX->getTotalCostEX($id);
        echo $result;
    } catch (Exception $e) {
        //header("location:?do=error");
    }
} elseif ($do == "calculateRemain") {
    /* try
      { */
    $id = $_REQUEST["id"];
    $result = $mMaintenanceReceiptEX->getTotalCostEX($id);
    echo $result;

    /* }
      catch(Exception $e)
      {
      //header("location:?do=error");
      } */
}

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

function getMaintenanceReceipts() {
    global $mMaintenanceReceiptEX;
    $clientId = $_REQUEST["clientId"];

    $ReceiptsData = $mMaintenanceReceiptEX->queryallByclientIdEX($clientId);
    /* print_r('<pre>');
      print_r($ReceiptsData);
      print_r('</pre>'); */
    return $ReceiptsData;
}

function getPayments() {
    global $mFinishingPayedEX;
    $clientId = $_REQUEST["clientId"];

    $Ids = $_REQUEST["Ids"];
    $Ids.='0';
    //$Ids=substr($Ids,0,strlen($Ids)-1);
    //$Ids=rtrim($Ids, ',');
    //print_r($Ids);
    $paymentsData = $mFinishingPayedEX->getPaymentsDataEX($clientId, $Ids);

    foreach ($paymentsData as $mypaymentsData) {
        if ($mypaymentsData->finishingType == 0) {
            $mypaymentsData->userId = $mypaymentsData->payed;
        } elseif ($mypaymentsData->finishingType == 2) {
            $mypaymentsData->userId = $mypaymentsData->remain;
        }
    }
    return $paymentsData;
}

?>