<?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");
//Sparepart
require_once('../models/dao/SparepartDAO.class.php');
require_once('../models/dto/Sparepart.class.php');
require_once('../models/mysql/SparepartMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartMySqlExtDAO.class.php');
//Sparepartstore
require_once('../models/dao/SparepartstoreDAO.class.php');
require_once('../models/dto/Sparepartstore.class.php');
require_once('../models/mysql/SparepartstoreMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartstoreMySqlExtDAO.class.php');
//Sparepartstoredetail
require_once('../models/dao/SparepartstoredetailsDAO.class.php');
require_once('../models/dto/Sparepartstoredetail.class.php');
require_once('../models/mysql/SparepartstoredetailsMySqlDAO.class.php');
require_once('../models/mysql/ext/SparepartstoredetailsMySqlExtDAO.class.php');
//Productrecieve
require_once('../models/dao/ProductrecieveDAO.class.php');
require_once('../models/dto/Productrecieve.class.php');
require_once('../models/mysql/ProductrecieveMySqlDAO.class.php');
require_once('../models/mysql/ext/ProductrecieveMySqlExtDAO.class.php');


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

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

  Controller Name :-sparePartController

  OPERTATION in Controller

  1-display add form
  2- add in sparepartstoredetail table
  3-display sucess or error
  4-display show and delete
  5-display edit
  ======================== */

//Sparepart
$sparePart = new Sparepart();
$sparePartDAO = new SparepartMySqlDAO();
$sparePartExt = new SparepartMySqlExtDAO();
//Sparepartstore
$sparePartStore = new Sparepartstore();
$sparePartStoreDAO = new SparepartstoreMySqlDAO();
$sparePartStoreExt = new SparepartstoreMySqlExtDAO();
//Sparepartstoredetail
$sparePartDetail = new Sparepartstoredetail();
$sparePartDetailDAO = new SparepartstoredetailsMySqlDAO();
$sparePartDetailExt = new SparepartstoredetailsMySqlExtDAO();
//Productrecieve
$productRecieve = new Productrecieve();
$productRecieveDAO = new ProductrecieveMySqlDAO();
$productRecieveExt = new ProductrecieveMySqlExtDAO();

//check and use the condition that suits this action
if (empty($do)) {
    //here the permission check


    getQuantity();
    //check for available quantity in sparepartstore
    //checkForQuantity();
}

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

function getQuantity() {
    global $sparePartDetailExt;

    $sparePartId = $_GET['partId'];
    if (isset($sparePartId) && $sparePartId != '-1') {
        $sparePartDetails = $sparePartDetailExt->queryBySparepartidExt($sparePartId);
        if (count($sparePartDetails)) {
            echo $sparePartDetails->partquantity;
        }
    } else {
        echo "error";
    }
}

function checkForQuantity() {
    global $sparePartDetailExt;

    $quantity = $_GET['qty'];
    $sparePartId = $_GET['partId'];
    if (isset($quantity) && isset($sparePartId) && $sparePartId != '-1') {
        $sparePartDetails = $sparePartDetailExt->queryBySparepartidExt($sparePartId);
        if (count($sparePartDetails)) {
            if ($sparePartDetails->partquantity < $quantity) {
                $quantity = $sparePartDetails->partquantity;
                echo $quantity;
            }
        }
    } else {
        echo "error";
    }
}

?>