<?php

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

// get the config file 
include_once("../public/config.php");

//here the db files that include in the file
include("../public/include_dao.php");
	
	//Client
	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');
	
	
	//MMaintenancereceiptfinish
	require_once('../models/dao/MMaintenancereceiptfinishDAO.class.php');
	require_once('../models/dto/MMaintenancereceiptfinish.class.php');
	require_once('../models/mysql/MMaintenancereceiptfinishMySqlDAO.class.php');
	require_once('../models/mysql/ext/MMaintenancereceiptfinishMySqlExtDAO.class.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'];

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

//here goes the instances and general variables
$clientEX = new ClientMySqlExtDAO();

$mMaintenanceReceiptFinish = new MMaintenancereceiptfinish();
$mMaintenanceReceiptFinishDAO = new MMaintenancereceiptfinishMySqlDAO();


$mMaintenanceReceiptEX = new MMaintenancereceiptMySqlExtDAO();
$mFinishingPayedEX = new MFinishingpayedMySqlExtDAO();

//check and use the condetion that suite this action
if (empty($do)||$do=="all") {
   
	$clients=$clientEX->queryClientsEX($_SESSION['branchId']);
	$smarty->assign("clients",$clients);
	/*print_r('<pre>');
	print_r($clients);
	print_r('</pre>');*/
	
	 $smarty->assign("title1",'الصيانة');
	 $smarty->assign("title2",'ادارة الفواتير');
	 $smarty->assign("title3",' تسوية');
	 $smarty->assign("link",'MaintennanceSettlementController.php?do=all');
	 $smarty->display("maintennanceHeader.html");
	 
	 
    $smarty->display("maintennanceSettlementView/maintennanceSettlement.html");
}

elseif($do=="closeout")
{
		try
		{
			$flag=closeout();
			if($flag == 0)
			{
				$note =" تمت العملية بنجاح";
				$url = "MaintennanceSettlementController.php?do=all";
				$smarty->assign('urldirect', $url);
				$smarty->assign('msgnote', $note);
				$smarty->display("notes2.html");
			}
			else if($flag == 1)
			{
				$note =" الدفعات المدخلة غير كافية";
				$url = "MaintennanceSettlementController.php?do=all";
				$smarty->assign('urldirect', $url);
				$smarty->assign('msgnote', $note);
				$smarty->display("notes2.html");
			}
			
		}
		catch(Exception $e)
		{
			//echo $e;
			header("location:?do=error");
		}	
}

//here the global templates
$smarty->assign("customValidation",1);
$smarty->assign("customMaintennanceSettlement",1);
$smarty->assign("GeneralSearch",1);
$smarty->display("footer.html");

////****************************  Functions  ********************///

function closeout()
{
	global $mMaintenanceReceiptFinish;
	global $mMaintenanceReceiptFinishDAO;
	
	global $mMaintenanceReceiptEX;
	global $mFinishingPayedEX;
	
	$clientId=$_POST["clientId"];
	$mMaintenanceReceiptId=$_POST["mMaintenanceReceiptId"];
	
	if(isset($clientId)&&$clientId!=-1&&isset($mMaintenanceReceiptId)&&$mMaintenanceReceiptId!=-1)
	{
		$itr=$_POST["hidden_itr"];
		$selectedReciptTotalCost_hidden=$_POST["selectedReciptTotalCostLbl1_hidden"];
		$totalOfPayments=0;
		for($i=1;$i<$itr;$i++)
		{
			$thisPayed=$_POST["payed".$i];
			if(isset($thisPayed)&&!empty($thisPayed))
			{
				//print_r("payment".$i."<br/>");
				$myArray=explode("/",$thisPayed);
				$money = $myArray[0];
				//print_r("money".$money."<br/>");
				$totalOfPayments=$totalOfPayments+$money;
			}
		}
		$remain=$selectedReciptTotalCost_hidden-$totalOfPayments;
		//print_r("remain=".$remain);
		
		//case of enough money
		if($remain<=0)
		{
			for($i=1;$i<$itr;$i++)
			{
				$thisPayed=$_POST["payed".$i];
				if(isset($thisPayed)&&!empty($thisPayed))
				{
					$myArray=explode("/",$thisPayed);
					$money = $myArray[0];
					$finishingType = $myArray[1];
					
					/*******************************/
					/*******************************/
					$mMaintenanceReceiptFinish->maintenanceReceiptId = $mMaintenanceReceiptId;
					$mMaintenanceReceiptFinish->finishingPayedId = $_POST["payment".$i];
					$mMaintenanceReceiptFinish->payed = $money;///////////////
					$mMaintenanceReceiptFinish->remain = 0;////////////////
					if($i == ($itr-1))
					{
						/*print_r('last one'.$i);*/
						$mMaintenanceReceiptFinish->remain = ($remain*-1);
					}
					
					$mMaintenanceReceiptFinish->maintenanceReceiptFinishDate = date("Y-m-d");
					$mMaintenanceReceiptFinish->userId = $_SESSION['userid'];
					$mMaintenanceReceiptFinish->branchId = $_SESSION['branchId'];
					$mMaintenanceReceiptFinish->del = 0;
					
					$mMaintenanceReceiptFinishDAO->insert($mMaintenanceReceiptFinish);
					
					$mMaintenanceReceiptEX->updateMaintenanceReceiptType(0,$mMaintenanceReceiptId);
					$mytype = 1;
					if($mMaintenanceReceiptFinish->remain>0)
					{$mytype = 2;}
					$mFinishingPayedEX->updateFinishingPayedType($mytype,$mMaintenanceReceiptFinish->remain,$_POST["payment".$i]);
					
				}
				
			}
			return 0;
		}
		else
		{
			return 1;
		}
		
	}
}

?>