<?
include_once('baseModelPMS.php');

class mroModel extends baseModelPMS {


	/* QUERYING FUNCTIONS */


	/* ********************************************************************************
	Select list of mro - budgeted material (get from CMS!)
	******************************************************************************** */

	function getlist_mro_bm($projectpkey, $mrostatus="", $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		if ($mrostatus!="") {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRO.mro_status = ".$mrostatus." order by MRO.".$sortby." ".$sortdir;
		} else {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 order by MRO.".$sortby." ".$sortdir;
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro - budgeted material exceeded, where status >= specified level
	******************************************************************************** */

	function getlist_mro_bme($projectpkey, $mrostatus="", $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		// TODO: link with mro_items!!

		if ($mrostatus!="") {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=2 and MRO.mro_cancel=0 and MRO.mro_status >= ".$mrostatus." order by MRO.".$sortby." ".$sortdir;
		} else {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=2 and MRO.mro_cancel=0 order by MRO.".$sortby." ".$sortdir;
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro - non-budgeted material - vo, where status >= specified level
	******************************************************************************** */

	function getlist_mro_nbmvo($projectpkey, $mrostatus="", $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		// TODO: link with mro_items!!

		if ($mrostatus!="") {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=3 and MRO.mro_cancel=0 and MRO.mro_status >= ".$mrostatus." order by MRO.".$sortby." ".$sortdir;
		} else {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=3 and MRO.mro_cancel=0 order by MRO.".$sortby." ".$sortdir;
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro - non-budgeted material - subcon, where status >= specified level
	******************************************************************************** */

	function getlist_mro_nbmsc($projectpkey, $mrostatus="", $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		// TODO: link with mro_items!!

		if ($mrostatus!="") {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=4 and MRO.mro_cancel=0 and MRO.mro_status >= ".$mrostatus." order by MRO.".$sortby." ".$sortdir;
		} else {
			$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=4 and MRO.mro_cancel=0 order by MRO.".$sortby." ".$sortdir;
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro - any
	******************************************************************************** */

	function getlist_mro($projectpkey, $mrotype, $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=".$mrotype." order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro by status
	******************************************************************************** */

	function getlist_mrobystatus($projectpkey, $mrostatus, $sortby="mro_requestdate", $sortdir="ASC") {

		$listdata=array();

		$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_status=".$mrostatus." order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro by stage (status range)
	Sort by MRO No, descending.. for use by the main MRO list page
	NOTE: superceded by following function :P
	******************************************************************************** */

	function getlist_mrobystage($projectpkey, $minstatus, $maxstatus, $mrono="", $mrodate="", $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		if ($mrodate!="") { $mrodate=converttounix($mrodate); }

		$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and (MRO.mro_id like '%".$mrono."%' and MRO.mro_requestdate like '%".$mrodate."%') and MRO.mro_status>=".$minstatus." and MRO.mro_status<=".$maxstatus." order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro by stage (status range) and cancel/reject status
	Sort by MRO No, descending.. for use by the main MRO list page (1st tab)
	******************************************************************************** */

	function getlist_mrolist($projectpkey, $mrono="", $mrodate="", $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		if ($mrodate!="") { $mrodate=converttounix($mrodate); }

		$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and (MRO.mro_id like '%".$mrono."%' and MRO.mro_requestdate like '%".$mrodate."%') and MRO.mro_status>=0 and MRO.mro_status<=2 and MRO.mro_cancel<>1 order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro by stage (status range) and cancel/reject status
	Sort by MRO No, descending.. for use by the main MRO list page (2nd tab: history)
	******************************************************************************** */

	function getlist_mrohistory($projectpkey, $mrono="", $mrodate="", $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		if ($mrodate!="") { $mrodate=converttounix($mrodate); }

		$sql="Select MRO.* from mro_details MRO where MRO.mro_projectpkey = '".$projectpkey."' and (MRO.mro_id like '%".$mrono."%' and MRO.mro_requestdate like '%".$mrodate."%') and ((MRO.mro_status>=3 and MRO.mro_status<=5) or MRO.mro_status=9) order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro by ITQ Evaluation status
	******************************************************************************** */

	function getlist_mrobyitqstatus($projectpkey, $itqstatus, $sortby="mro_id", $sortdir="ASC") {

		$listdata=array();

		$sql="Select MRO.*, ITQ.* from mro_details MRO inner join itq_details ITQ on MRO.mro_pkey=ITQ.itq_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and ITQ.itq_evalstatus=".$itqstatus." order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro items
	******************************************************************************** */

	function getlist_mroitems($mropkey, $sortby="item_no", $sortdir="ASC") {

		$listdata=array();

		$sql="Select MRI.* from mro_items MRI where MRI.item_mropkey = '".$mropkey."' order by MRI.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro items by project pkey and subtrade (mro type 1 only!)
	******************************************************************************** */

	//function getlist_mroitemsbysubtrade($projectpkey, $subtradepkey, $sortby="item_code", $sortdir="ASC") {
	function getlist_mroitemsbysubtrade($projectpkey, $subtradepkey, $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		//$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRI.item_subtradepkey = '".$subtradepkey."' order by MRI.".$sortby." ".$sortdir;
		$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRO.mro_cancel=0 and MRI.item_subtradepkey = '".$subtradepkey."' order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}

	
	/* ********************************************************************************
	Select list of mro items by project pkey and subtrade (mro type 1 only!) with date from & date to
	******************************************************************************** */

	//function getlist_mroitemsbysubtrade($projectpkey, $subtradepkey, $sortby="item_code", $sortdir="ASC") {
	// myapit 2010-07-28
	function getlist_mroitemsbysubtrade_withdate($projectpkey, $subtradepkey, $date_start, $date_end, $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		//$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRI.item_subtradepkey = '".$subtradepkey."' order by MRI.".$sortby." ".$sortdir;
		$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where 
		MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and 
		MRO.mro_cancel=0 and MRI.item_subtradepkey = '".$subtradepkey."' and 
		MRO.mro_requestdate >='".$date_start."' and MRO.mro_requestdate <= '".$date_end."'  
		order by MRO.".$sortby." ".$sortdir;
		///echo $sql; die();
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}

	/* ********************************************************************************
	Select list of mro items by project pkey and subsubtrade (mro type 1 only!)
	******************************************************************************** */

	//function getlist_mroitemsbysubsubtrade($projectpkey, $subsubtradepkey, $sortby="item_code", $sortdir="ASC") {
	function getlist_mroitemsbysubsubtrade($projectpkey, $subsubtradepkey, $sortby="mro_id", $sortdir="DESC") {

		$listdata=array();

		//$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRI.item_subsubtradepkey = '".$subsubtradepkey."' order by MRI.".$sortby." ".$sortdir;
		$sql="Select MRI.* from mro_items MRI inner join mro_details MRO on MRI.item_mropkey=MRO.mro_pkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=1 and MRO.mro_cancel=0 and MRI.item_subsubtradepkey = '".$subsubtradepkey."' order by MRO.".$sortby." ".$sortdir;
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro and more items, vo or subcon, if approved
	******************************************************************************** */

	function getlist_mroitems_nbm($projectpkey,$mrostatus) {

		$listdata=array();

		// TODO: link with mro_items!!

		$sql="Select MRO.*, MROI.*, ST.tradedescription from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey inner join settings_trades ST on MRO.mro_subtradepkey = ST.tradepkey where MRO.mro_projectpkey = '".$projectpkey."' and (MRO.mro_type=3 or MRO.mro_type=4) and MRO.mro_status>=".$mrostatus." order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro items based on subsubtradepkey
	******************************************************************************** */

	function getlist_mroitems_bme($projectpkey, $mrostatus="") {

		$listdata=array();

		if ($mrostatus!="") {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=2 and MRO.mro_status >= ".$mrostatus." order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		} else {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=2 order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro items based on subsubtradepkey
	******************************************************************************** */

	function getlist_mroitems_nbmvo($projectpkey, $mrostatus="") {

		$listdata=array();

		if ($mrostatus!="") {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=3 and MRO.mro_status >= ".$mrostatus." order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		} else {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=3 order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select list of mro items based on subsubtradepkey
	******************************************************************************** */

	function getlist_mroitems_nbmsc($projectpkey, $mrostatus="") {

		$listdata=array();

		if ($mrostatus!="") {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=4 and MRO.mro_status >= ".$mrostatus." order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		} else {
			$sql="Select MRO.*, MROI.* from mro_details MRO inner join mro_items MROI on MRO.mro_pkey = MROI.item_mropkey where MRO.mro_projectpkey = '".$projectpkey."' and MRO.mro_type=4 order by MRO.mro_subtradepkey, MROI.item_subsubtradepkey";
		}
		$listdata=$this->getextendedrows($sql);
		return $listdata;
	}


	/* ********************************************************************************
	Select count of mro items
	******************************************************************************** */

	function getcount_mroitems($mropkey) {

		$data=array();

		$sql="Select count(MRI.item_pkey) as item_count from mro_items MRI where MRI.item_mropkey = '".$mropkey."'";
		$data=$this->getextendedrow($sql);
		return $data['item_count'];
	}


	/* ********************************************************************************
	Select a particular mro
	******************************************************************************** */

	function getdata_mro($mropkey){

		$data=array();

		$sql="Select MRO.* from mro_details MRO where MRO.mro_pkey='".$mropkey."'";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	Select a particular mro by exceedpkey
	******************************************************************************** */

	function getdata_mrobyexceed($exceedpkey){

		$data=array();

		$sql="Select MRO.* from mro_details MRO where MRO.mro_exceedpkey='".$exceedpkey."'";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	Select a particular mro item
	******************************************************************************** */

	function getdata_mroitem($itempkey){

		$data=array();

		$sql="Select MRI.* from mro_items MRI where MRI.item_pkey='".$itempkey."'";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	Select a particular mro item by mropkey and subsubtradepkey
	******************************************************************************** */

	function getdata_mroitembysubsubtrade($mropkey, $subsubtradepkey){

		$data=array();

		$sql="Select MRI.* from mro_items MRI where MRI.item_mropkey='".$mropkey."' and MRI.item_subsubtradepkey='".$subsubtradepkey."'";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	get total amount in an mro
	******************************************************************************** */

	function gettotal_mroitems($mropkey){

		$data=array();

		$sql="Select sum(MRI.item_quantity*MRI.item_rate) as mro_total from mro_items MRI where MRI.item_mropkey='".$mropkey."'";
		$data=$this->getextendedrow($sql);
		return $data['mro_total'];
	}


	/* ********************************************************************************
	Select a particular mro's latest approval
	******************************************************************************** */

	function getdata_lastapproval($mropkey) {

		$data=array();

		$sql="Select MRA.* from mro_approvals MRA where MRA.approval_mropkey='".$mropkey."' order by MRA.approval_date DESC limit 0,1";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	Select a particular mro's rejection data
	******************************************************************************** */

	function getdata_lastrejection($mropkey) {

		$data=array();

		$sql="Select MRA.* from mro_approvals MRA where MRA.approval_mropkey='".$mropkey."' and MRA.approval_status=3 order by MRA.approval_date DESC limit 0,1";
		$data=$this->getextendedrow($sql);
		return $data;
	}


	/* ********************************************************************************
	get total quantity for subtrade in all mros
	******************************************************************************** */

	function getquantity_mrosubtrade($projectpkey, $subtradepkey) {

		$data=array();

		$sql="Select sum(MRI.item_quantity) as total_quantity from mro_items MRI inner join mro_details MRO on MRI.item_mropkey = MRO.mro_pkey where MRO.mro_projectpkey='".$projectpkey."' and MRI.item_subtradepkey='".$subtradepkey."' and MRO.mro_cancel<>1";
		$data=$this->getextendedrow($sql);
		return $data['total_quantity'];
	}


	/* ********************************************************************************
	get total quantity for subsubtrade in all mros
	******************************************************************************** */

	function getquantity_mrosubsubtrade($projectpkey, $subsubtradepkey) {

		$data=array();

		$sql="Select sum(MRI.item_quantity) as total_quantity from mro_items MRI inner join mro_details MRO on MRI.item_mropkey = MRO.mro_pkey where MRO.mro_projectpkey='".$projectpkey."' and MRI.item_subsubtradepkey='".$subsubtradepkey."' and MRO.mro_cancel<>1";
		$data=$this->getextendedrow($sql);
		return $data['total_quantity'];
	}


	/* ********************************************************************************
	get last item no in mro_items
	******************************************************************************** */

	function getlast_itemno($mropkey) {

		$data=array();

		$sql="Select max(MRI.item_no) as last_no from mro_items MRI where MRI.item_mropkey='".$mropkey."'";
		$data=$this->getextendedrow($sql);
		return $data['last_no'];
	}


	/* ********************************************************************************
	Get last mro id (ignore cancelled mros <-- nope!)
	******************************************************************************** */

	function getdata_lastmroid(){

		$data=array();

		$sql="Select max(MRO.mro_id) as lastid from mro_details MRO";	// where MRO.mro_cancel=0";
		$data=$this->getextendedrow($sql);
		return $data['lastid'];
	}


	/* ********************************************************************************
	Get last mro rev no
	******************************************************************************** */

	function getdata_revno($mropkey){

		$data=array();

		$sql="Select MRO.mro_revno as revno from mro_details MRO where MRO.mro_pkey='".$mropkey."'";
		$data=$this->getextendedrow($sql);
		return $data['revno'];
	}


	/* FUNCTIONS TO INSERT RECORDS */


	/* ********************************************************************************
	Insert/Create a new mro (any type)
	Duplicate issue?
	******************************************************************************** */

	function insertdata_mro($p_mro_pkey, $p_mro_projectpkey, $p_mro_type, $p_mro_id, $p_mro_subtradepkey, $p_mro_subconref, $p_mro_clientref, $p_mro_requestdate, $p_mro_deliverydate, $p_mro_debitaccount, $p_mro_remarks, $mro_subcons, $p_mro_status) {

		// clean/convert/add params..
		$p_mro_requestdate=converttounix($p_mro_requestdate);
		$p_mro_deliverydate=converttounix($p_mro_deliverydate);
		if ($p_mro_subconref=="") $p_mro_subconref="-NA-";
		if ($p_mro_clientref=="") $p_mro_clientref="-NA-";
		//if ($p_mro_subtradepkey1!="") { $p_mro_subtradepkey=$p_mro_subtradepkey1; } else { $p_mro_subtradepkey=$p_mro_subtradepkey2; }
		if ($p_mro_type=="") $p_mro_type="0";

		// to check: this
		if (($p_mro_debitaccount!="")||($mro_subcons!="")) $p_mro_type="4";

		//include_once('userModel.php');
		//$myuserModel=new userModel;
		//$myuser = $myuserModel->getdata_user($_SESSION['Xuserpkey']);
		//$myusername = $myuser['staff_name'];
		$myusername = $_SESSION['Xstaffname'];

		// update..
		$new_mro_pkey=md5(microtime());

		$lastid=$this->getdata_lastmroid();
		$lastid++;

		$sa = $this->add_data("MRO [Add]", "mro_details", "mro_pkey, mro_projectpkey, mro_type, mro_id, mro_subtradepkey, mro_subconref, mro_clientref, mro_requestdate, mro_deliverydate, mro_debitaccount, mro_remarks, mro_subcons, mro_requestor, mro_status",
			"'".$new_mro_pkey."','".$p_mro_projectpkey."',".$p_mro_type.
			",".$lastid.",'".$p_mro_subtradepkey."','".$p_mro_subconref.
			"','".$p_mro_clientref.
			"','".$p_mro_requestdate."','".$p_mro_deliverydate."','".$p_mro_debitaccount.
			"','".$p_mro_remarks."','".$mro_subcons."','".$myusername."',0");

		// return result..
		if ($sa==1) {
			return $new_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Insert/Create a new mro item
	******************************************************************************** */

	function insertdata_mroitem($p_item_pkey, $p_item_mropkey, $p_item_no, $p_item_code, $p_item_maintradepkey, $p_item_submaintradepkey, $p_item_subtradepkey, $p_item_subsubtradepkey, $p_item_trisubtradepkey, $p_item_description, $p_item_unit, $p_item_quantity, $p_item_exceeded, $p_item_purpose, $p_item_remarks, $p_projectpkey, $p_mrotype, $p_mroexceedpkey) {

		// clean/convert/add params..
		$p_item_quantity=str_replace(",","",$p_item_quantity);
		$p_item_exceeded=str_replace(",","",$p_item_exceeded);
		//$p_item_rate=str_replace(",","",$p_item_rate);

		// if no other items already exist, set subtrade and set mro type (unless subcon!)
		if ($this->getcount_mroitems($p_item_mropkey)==0) {

			if ($p_mrotype!="4") {

				$sa1 = $this->updatetypesubtrade_mro($p_item_mropkey, $p_mrotype, $p_item_subtradepkey);

				// if exceeded and no balance left, add item to this exceeded mro

				if (($p_item_exceeded!="0.0000")&&($p_item_quantity=="0.0000")) {

					// update..
					$new_item_pkey=md5(microtime());

					$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
						"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
						",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
						"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
						"','".$p_item_description.
						"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
						",'".$p_item_purpose."','".$p_item_remarks."'");

					// return result..
					if ($sa==1) {
						return $new_item_pkey;
					} else {
						return false;
					}

				} else {

					// else if quantity >0 (normal/vo/subcon - NOT SUBCON), add item to this mro..

					// update..
					$new_item_pkey=md5(microtime());

					$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
						"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
						",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
						"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
						"','".$p_item_description.
						"','".$p_item_unit."',".$p_item_quantity.",".$p_item_exceeded.
						",'".$p_item_purpose."','".$p_item_remarks."'");

					if ($p_item_exceeded!="0.0000") {

						// get existing mro data
						$mro_data = $this->getdata_mro($p_item_mropkey);

						// add new mro and item then link original mro to this one..
						$newmropkey = $this->insertdata_mro("", $p_projectpkey, "2", "", $p_item_subtradepkey, "", "", "", unconverttounixslash($mro_data['mro_deliverydate']), $mro_data['mro_debitaccount'], $mro_data['mro_remarks'], $mro_data['mro_subcons'], "0");

						$new_item_pkey=md5(microtime());
						$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
							"'".$new_item_pkey."','".$newmropkey."',"."1".
							",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
							"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
							"','".$p_item_description.
							"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
							",'".$p_item_purpose."','".$p_item_remarks."'");

						// update exceedpkey link..
						$sa3 = $this->update_data("MRO [Edit]", "mro_details", "mro_exceedpkey = '".$newmropkey."'",
							"where mro_pkey = '".$p_item_mropkey."'");

						// return result..
						if ($sa2==1) {
							return $new_item_pkey;
						} else {
							return false;
						}

					}

					// return result..
					if ($sa==1) {
						return $new_item_pkey;
					} else {
						return false;
					}
				}

			} else {

				// if subcon update subtrade too
				$sa1 = $this->updatetypesubtrade_mro($p_item_mropkey, "4", $p_item_subtradepkey);

				// update..
				$new_item_pkey=md5(microtime());

				$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
					"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
					",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
					"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
					"','".$p_item_description.
					"','".$p_item_unit."',".$p_item_quantity.",".$p_item_exceeded.
					",'".$p_item_purpose."','".$p_item_remarks."'");

				// return result..
				if ($sa==1) {
					return $new_item_pkey;
				} else {
					return false;
				}

			}

		} else {

			// else if qty>0
			// get mro details (mostly to get mro type)
			$currentmro = $this->getdata_mro($p_item_mropkey);

			// do not add if other items exists and qty=0 and exceeded>0 (pure exceeded)

			if ($p_mrotype!="4") {

				if ($p_item_quantity!="0.0000") {

					// update..
					$new_item_pkey=md5(microtime());

					$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
						"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
						",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
						"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
						"','".$p_item_description.
						"','".$p_item_unit."',".$p_item_quantity.",".$p_item_exceeded.
						",'".$p_item_purpose."','".$p_item_remarks."'");

					// return result..
					if ($sa==1) {
						return $new_item_pkey;
					} else {
						return false;
					}

				} else if (($p_item_exceeded!="0.0000")&&($p_item_quantity=="0.0000")&&($p_mrotype=="2")) {

					if ($currentmro['mro_type']=="1") {

						/*
						// if pure exceeded and mro is normal type

						// update..
						$new_item_pkey=md5(microtime());

						$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
							"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
							",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
							"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
							"','".$p_item_description.
							"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
							",'".$p_item_purpose."','".$p_item_remarks."'");

						// return result..
						if ($sa==1) {
							return $new_item_pkey;
						} else {
							return false;
						}
						*/


						// proceed depending on if we have link to exceeded mro already..
						if ($p_mroexceedpkey!="") {

							// add/update item depending on if subsub already exists in exceeded mro
							$exc_data = $this->getdata_mroitembysubsubtrade($p_mroexceedpkey, $p_item_subsubtradepkey);

							if ($exc_data['item_pkey']!="") {

								$new_amount = 0;
								$new_amount = $p_item_exceeded + $exc_data['item_quantity'];

								$sa2 = $this->update_data("MRO Item [Edit]", "mro_items", "item_quantity = ".$new_amount,
									"where item_pkey = '".$exc_data['item_pkey']."'");

								// return result..
								if ($sa2==1) {
									return $exc_data['item_pkey'];
								} else {
									return false;
								}

							} else {

								$autono = $this->getlast_itemno($p_mroexceedpkey);
								$autono++;

								$new_item_pkey=md5(microtime());
								$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
									"'".$new_item_pkey."','".$p_mroexceedpkey."',".$autono.
									",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
									"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
									"','".$p_item_description.
									"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
									",'".$p_item_purpose."','".$p_item_remarks."'");

								// return result..
								if ($sa2==1) {
									return $new_item_pkey;
								} else {
									return false;
								}

							}

						} else {

							// get existing mro data
							$mro_data = $this->getdata_mro($p_item_mropkey);

							// add new mro and item then link original mro to this one..
							$newmropkey = $this->insertdata_mro("", $p_projectpkey, "2", "", $p_item_subtradepkey, "", "", "", unconverttounixslash($mro_data['mro_deliverydate']), $mro_data['mro_debitaccount'], $mro_data['mro_remarks'], $mro_data['mro_subcons'], "0");

							$new_item_pkey=md5(microtime());
							$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
								"'".$new_item_pkey."','".$newmropkey."',"."1".
								",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
								"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
								"','".$p_item_description.
								"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
								",'".$p_item_purpose."','".$p_item_remarks."'");

							// update exceedpkey link..
							$sa3 = $this->update_data("MRO [Edit]", "mro_details", "mro_exceedpkey = '".$newmropkey."'",
								"where mro_pkey = '".$p_item_mropkey."'");

							// return result..
							if ($sa2==1) {
								return $new_item_pkey;
							} else {
								return false;
							}

						}


					} else if ($currentmro['mro_type']=="2") {

						// if pure exceeded and mro is already of exceeded type

						// update..
						$new_item_pkey=md5(microtime());

						$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
							"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
							",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
							"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
							"','".$p_item_description.
							"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
							",'".$p_item_purpose."','".$p_item_remarks."'");

						// return result..
						if ($sa==1) {
							return $new_item_pkey;
						} else {
							return false;
						}
					}
				}

			} else {

				// update..
				$new_item_pkey=md5(microtime());

				$sa = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
					"'".$new_item_pkey."','".$p_item_mropkey."',".$p_item_no.
					",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
					"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
					"','".$p_item_description.
					"','".$p_item_unit."',".$p_item_quantity.",".$p_item_exceeded.
					",'".$p_item_purpose."','".$p_item_remarks."'");

				// return result..
				if ($sa==1) {
					return $new_item_pkey;
				} else {
					return false;
				}

			}

		}

		// tukar: if qty=0 and exceeded>0 (i.e. this is pure mro exceeded), do not add item to
		// normal mro

		// if mro type not exceeded but quantity exceeded kena buat lg 1 mro
		//if ($p_mrotype=="2") {

		/*
		if ($p_item_exceeded!="0.0000") {

			// proceed depending on if we have link to exceeded mro already..
			if ($p_mroexceedpkey!="") {

				// add/update item depending on if subsub already exists in exceeded mro
				$exc_data = $this->getdata_mroitembysubsubtrade($p_mroexceedpkey, $p_item_subsubtradepkey);

				if ($exc_data['item_pkey']!="") {

					$new_amount = 0;
					$new_amount = $p_item_exceeded + $exc_data['item_quantity'];

					$sa2 = $this->update_data("MRO Item [Edit]", "mro_items", "item_quantity = ".$new_amount,
						"where item_pkey = '".$exc_data['item_pkey']."'");

				} else {

					$autono = $this->getlast_itemno($p_mroexceedpkey);
					$autono++;

					$new_item_pkey=md5(microtime());
					$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
						"'".$new_item_pkey."','".$p_mroexceedpkey."',".$autono.
						",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
						"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
						"','".$p_item_description.
						"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
						",'".$p_item_purpose."','".$p_item_remarks."'");
				}

			} else {

				// get existing mro data
				$mro_data = $this->getdata_mro($p_item_mropkey);

				// add new mro and item then link original mro to this one..
				$newmropkey = $this->insertdata_mro("", $p_projectpkey, "2", "", $p_item_subtradepkey, "", "", "", unconverttounixslash($mro_data['mro_deliverydate']), $mro_data['mro_debitaccount'], $mro_data['mro_remarks'], $mro_data['mro_subcons'], "0");

				$new_item_pkey=md5(microtime());
				$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_purpose, item_remarks",
					"'".$new_item_pkey."','".$newmropkey."',"."1".
					",'".$p_item_code."','".$p_item_maintradepkey."','".$p_item_submaintradepkey.
					"','".$p_item_subtradepkey."','".$p_item_subsubtradepkey."','".$p_item_trisubtradepkey.
					"','".$p_item_description.
					"','".$p_item_unit."',".$p_item_exceeded.","."0.0000".
					",'".$p_item_purpose."','".$p_item_remarks."'");

				// update exceedpkey link..
				$sa3 = $this->update_data("MRO [Edit]", "mro_details", "mro_exceedpkey = '".$newmropkey."'",
					"where mro_pkey = '".$p_item_mropkey."'");

			}
		}
		*/

		// return result..
		if ($sa==1) {
			return $new_item_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Insert/Create a new mro approval (cumulative)
	Type: 1 = Submission, 2 = Recommendation, 3 = Approval
	Status: 0 = Unprocessed/Unknown, 1 = Recommended/Approved, 2 = Cancelled, 3 = Rejected
	NOTE: At the same, must update status field in table mro_details!
	******************************************************************************** */

	function insertdata_approval($p_approval_pkey, $p_approval_mropkey, $p_approval_type, $p_approval_userpkey, $p_approval_officername, $p_approval_date, $p_approval_status, $p_approval_remarks) {

		// clean/convert/add params..
		$p_approval_date=converttounixfull($p_approval_date." ".date('H:i:s'));	// use convertfull coz timestamp;
		//$p_approval_date=gettimestamp();

		$p_mro_approved="0";
		$p_mro_cancel="0";
		if ($p_approval_status=="1") $p_mro_approved = $p_approval_type;

		// update..
		$new_approval_pkey=md5(microtime());

		$sa = $this->add_data("MRO -> Approval [Add]", "mro_approvals", "approval_pkey, approval_mropkey, approval_type, approval_userpkey, approval_officername, approval_date, approval_status, approval_remarks",
			"'".$new_approval_pkey."','".$p_approval_mropkey."',".$p_approval_type.
			",'".$p_approval_userpkey."','".$p_approval_officername."','".$p_approval_date.
			"',".$p_approval_status.",'".$p_approval_remarks."'");

		if ($p_mro_approved=="3") {

			// if approval (3) and approved, update mro with status and approver info 
			// (and request date) - also set cancel to 0 (in case rejected earlier)..

			$myusername = $_SESSION['Xstaffname'];

			$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_status=".$p_mro_approved.
				", mro_cancel=0".
				", mro_approver='".$myusername.
				"', mro_requestdate='".$p_approval_date."'",
				"where mro_pkey='".$p_approval_mropkey."'");
		} else {

			if ($p_approval_status=="1") {

				// if submission or recommendation (accepted), update mro status..
				// also remove cancel tag!!

				//$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_status=".$p_mro_approved,
				//	"where mro_pkey='".$p_approval_mropkey."'");
				$pubuserfullname = $_SESSION['Xstaffname'];
				$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_status=".$p_mro_approved.
					", mro_cancel=0".
					", mro_recommended='".$pubuserfullname."'",
					"where mro_pkey='".$p_approval_mropkey."'");
				


			} else if ($p_approval_status=="2") {

				// if cancelled, update cancel status but not mro status (cannot do anything)..
				// now change status to 9 (dead)

				//$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_cancel=1",
				//	"where mro_pkey='".$p_approval_mropkey."'");
				
				
				$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_cancel=1".
					", mro_status=9",
					"where mro_pkey='".$p_approval_mropkey."'");
				
			} else if ($p_approval_status=="3") {

				// if rejected, update cancel status and set mro status to 0 (new)..
				// and increment rev no
				// actually, set status to 0 (dead) but create new duplicate mro

				$myrev = $this->getdata_revno($p_approval_mropkey);
				$myrev++;

				//$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_cancel=2".
				//	", mro_revno=".$myrev.
				//	", mro_status=0",
				//	"where mro_pkey='".$p_approval_mropkey."'");

				$sa2 = $this->update_data("MRO [Edit]", "mro_details", "mro_cancel=2".
					", mro_status=9",
					"where mro_pkey='".$p_approval_mropkey."'");

				$ca = $this->copy_mro($p_approval_mropkey, $myrev);

			}
		}

		// return result..
		if ($sa==1) {
			return $new_approval_pkey;
		} else {
			return false;
		}
	}


	/* FUNCTIONS TO UPDATE RECORDS */


	/* ********************************************************************************
	Update/Override an existing mro
	> 
	******************************************************************************** */

	function updatedata_mro($p_mro_pkey, $p_mro_projectpkey, $p_mro_type, $p_mro_id, $p_mro_subtradepkey, $p_mro_subconref, $p_mro_clientref, $p_mro_requestdate, $p_mro_deliverydate, $p_mro_debitaccount, $p_mro_remarks, $mro_subcons, $p_mro_status) {

		// clean/convert/add params..
		$p_mro_requestdate=converttounix($p_mro_requestdate);
		$p_mro_deliverydate=converttounix($p_mro_deliverydate);
		if ($p_mro_subconref=="") $p_mro_subconref="-NA-";
		if ($p_mro_clientref=="") $p_mro_clientref="-NA-";
		//if ($p_mro_subtradepkey1!="") { $p_mro_subtradepkey=$p_mro_subtradepkey1; } else { $p_mro_subtradepkey=$p_mro_subtradepkey2; }
		if ($p_mro_type=="") $p_mro_type="0";

		// to check: this
		if (($p_mro_debitaccount!="")||($mro_subcons!="")) $p_mro_type="4";

		$myusername = $_SESSION['Xstaffname'];

		// update (except status)..
		$sa = $this->update_data("MRO [Edit]", "mro_details", "mro_projectpkey = '".$p_mro_projectpkey.
			"', mro_type = ".$p_mro_type.
			", mro_subtradepkey = '".$p_mro_subtradepkey.
			"', mro_subconref = '".$p_mro_subconref.
			"', mro_clientref = '".$p_mro_clientref.
			"', mro_requestdate = '".$p_mro_requestdate.
			"', mro_deliverydate = '".$p_mro_deliverydate.
			"', mro_debitaccount = '".$p_mro_debitaccount.
			"', mro_remarks = '".$p_mro_remarks.
			"', mro_subcons = '".$mro_subcons.
			"', mro_requestor = '".$myusername."'",
			"where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Update/Override an existing mro's status
	******************************************************************************** */

	function updatestatus_mro($p_mro_pkey, $p_mro_status) {

		// clean/convert/add params..

		// update (except status)..
		$sa = $this->update_data("MRO [Edit]", "mro_details", "mro_status = ".$p_mro_status,
			"where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Update/Override an existing mro's type and subtrade
	******************************************************************************** */

	function updatetypesubtrade_mro($p_mro_pkey, $p_mro_type, $p_mro_subtradepkey) {

		// clean/convert/add params..

		// update (except status)..
		$sa = $this->update_data("MRO [Edit]", "mro_details", "mro_type = ".$p_mro_type.
			", mro_subtradepkey = '".$p_mro_subtradepkey."'",
			"where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Cancel an existing mro
	******************************************************************************** */

	function cancel_mro($p_mro_pkey) {

		// clean/convert/add params..

		// update..
		$sa = $this->update_data("MRO [Cancel]", "mro_details", "mro_cancel = 1",
			"where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Reject an existing mro
	******************************************************************************** */

	function reject_mro($p_mro_pkey) {

		// clean/convert/add params..

		// update..
		$sa = $this->update_data("MRO [Reject]", "mro_details", "mro_cancel = 2",
			"where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Copy rejected MRO into new MRO!!
	******************************************************************************** */

	function copy_mro($p_mro_pkey, $p_revno) {

		// first, copy the mro..
		$mymro = $this->getdata_mro($p_mro_pkey);

		// add new mro with status=0 and cancel status = 2..
		$new_mro_pkey=md5(microtime());

		$sa = $this->add_data("MRO [Add]", "mro_details", "mro_pkey, mro_projectpkey, mro_exceedpkey, mro_type, mro_id, mro_revno, mro_subtradepkey, mro_subconref, mro_clientref, mro_requestdate, mro_deliverydate, mro_debitaccount, mro_remarks, mro_subcons, mro_requestor, mro_status, mro_cancel",
			"'".$new_mro_pkey."','".$mymro['mro_projectpkey'].
			"','".$mymro['mro_exceedpkey'].
			"',".$mymro['mro_type'].
			",".$mymro['mro_id'].",".$p_revno.
			",'".$mymro['mro_subtradepkey']."','".$mymro['mro_subconref'].
			"','".$mymro['mro_clientref'].
			"','".$mymro['mro_requestdate']."','".$mymro['mro_deliverydate'].
			"','".$mymro['mro_debitaccount'].
			"','".$mymro['mro_remarks']."','".$mymro['mro_subcons'].
			"','".$mymro['mro_requestor']."',0,2");

		// now, copy all items..
		$mro_items = $this->getlist_mroitems($p_mro_pkey);

		foreach($mro_items as $myitem) {

			// update..
			$new_item_pkey=md5(microtime());

			$sa2 = $this->add_data("MRO Item [Add]", "mro_items", "item_pkey, item_mropkey, item_no, item_code, item_maintradepkey, item_submaintradepkey, item_subtradepkey, item_subsubtradepkey, item_trisubtradepkey, item_description, item_unit, item_quantity, item_exceeded, item_rate, item_purpose, item_remarks",
				"'".$new_item_pkey."','".$new_mro_pkey."',".$myitem['item_no'].
				",'".$myitem['item_code']."','".$myitem['item_maintradepkey'].
				"','".$myitem['item_submaintradepkey'].
				"','".$myitem['item_subtradepkey']."','".$myitem['item_subsubtradepkey'].
				"','".$myitem['item_trisubtradepkey'].
				"','".$myitem['item_description'].
				"','".$myitem['item_unit'].
				"',".$myitem['item_quantity'].
				",".$myitem['item_exceeded'].
				",".$myitem['item_rate'].
				",'".$myitem['item_purpose']."','".$myitem['item_remarks']."'");
		}

		// return result..
		if ($sa==1) {
			return $new_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Update/Override an existing mro item
	> NOTE: For now not used (editing existing mro item can make everything go crazy,
	especially programmer!!
	******************************************************************************** */

	function updatedata_mroitem($p_item_pkey, $p_item_mropkey, $p_item_no, $p_item_code, $p_item_maintradepkey, $p_item_submaintradepkey, $p_item_subtradepkey, $p_item_subsubtradepkey, $p_item_trisubtradepkey, $p_item_description, $p_item_unit, $p_item_quantity, $p_item_exceeded, $p_item_purpose, $p_item_remarks, $p_projectpkey, $p_mrotype, $p_mroexceedpkey) {

		// clean/convert/add params..
		$p_item_quantity=str_replace(",","",$p_item_quantity);
		$p_item_exceeded=str_replace(",","",$p_item_exceeded);
		//$p_item_rate=str_replace(",","",$p_item_rate);

		// if only this item exists, set subtrade and set mro type
		if ($this->getcount_mroitems($p_item_mropkey)==1) {
			$sa1 = $this->updatetypesubtrade_mro($p_item_mropkey, $p_mrotype, $p_item_subtradepkey);
		}

		// update..
		$sa = $this->update_data("MRO Item [Edit]", "mro_items", "item_mropkey = '".$p_item_mropkey.
			"', item_no = ".$p_item_no.
			", item_code = '".$p_item_code.
			"', item_maintradepkey = '".$p_item_maintradepkey.
			"', item_submaintradepkey = '".$p_item_submaintradepkey.
			"', item_subtradepkey = '".$p_item_subtradepkey.
			"', item_subsubtradepkey = '".$p_item_subsubtradepkey.
			"', item_trisubtradepkey = '".$p_item_trisubtradepkey.
			"', item_description = '".$p_item_description.
			"', item_unit = '".$p_item_unit.
			"', item_quantity = ".$p_item_quantity.
			", item_exceeded = ".$p_item_exceeded.
			", item_purpose = '".$p_item_purpose.
			"', item_remarks = '".$p_item_remarks."'",
			"where item_pkey = '".$p_item_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_item_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Update/Override an existing mro item's rate
	******************************************************************************** */

	function updaterate_mroitem($p_item_pkey, $p_item_mropkey, $p_item_rate) {

		// clean/convert/add params..

		// update (except status)..
		$sa = $this->update_data("MRO Item [Edit]", "mro_items", "item_rate = ".$p_item_rate,
			"where item_pkey = '".$p_item_pkey."' and item_mropkey = '".$p_item_mropkey."'");

		// return result..
		if ($sa==1) {
			return $p_item_pkey;
		} else {
			return false;
		}
	}


	/* FUNCTIONS TO DELETE RECORDS */


	/* ********************************************************************************
	Delete an existing mro
	> NOTE: forbid delete if record is used in any transaction!!
	> (forbid before we even reach here)
        > THIS IS IMPORTANT!!!
	******************************************************************************** */

	function deletedata_mro($p_mro_pkey) {

		// delete items then mro..
		$sa = $this->delete_data("MRO Items [Delete]", "mro_items", "where item_mropkey = '".$p_mro_pkey."'");
		$sa = $this->delete_data("MRO [Delete]", "mro_details", "where mro_pkey = '".$p_mro_pkey."'");

		// return result..
		if ($sa==1) {
			return $p_mro_pkey;
		} else {
			return false;
		}
	}


	/* ********************************************************************************
	Delete an existing mro item
	******************************************************************************** */

	function deletedata_mroitem($p_item_pkey, $p_item_mropkey, $p_projectpkey) {

		// get mro and item data
		$mro_data = $this->getdata_mro($p_item_mropkey);
		$item_data = $this->getdata_mroitem($p_item_pkey);

		// delete item..
		$sa = $this->delete_data("MRO Item [Delete]", "mro_items", "where item_pkey = '".$p_item_pkey."' and item_mropkey = '".$p_item_mropkey."'");

		// delete corresponding item (by subsubtrade) in partner exceeded mro..
		if ($mro_data['mro_exceedpkey']!="") {
			$sa1 = $this->delete_data("MRO Item [Delete]", "mro_items", "where item_subsubtradepkey = '".$item_data['item_subsubtradepkey']."' and item_mropkey = '".$mro_data['mro_exceedpkey']."'");
		}

		// if last item in mro deleted, reset mro type and trade so user can select other items!
		// reset type unless subcon!!
		$newcount = $this->getcount_mroitems($p_item_mropkey);
		if ($newcount==0) {
			if ($mro_data['mro_type']!="4") {
				$sa2 = $this->updatetypesubtrade_mro($p_item_mropkey, "0", "");
			}
		// otherwise re-sort numbers
		} else {
			$ncount=1;
			$item_list = $this->getlist_mroitems($p_item_mropkey, $sortby="item_no", $sortdir="ASC");
			foreach ($item_list as $myitem) {
				// update..
				$sa = $this->update_data("MRO Item [Edit]", "mro_items", "item_no = ".$ncount,
					"where item_pkey = '".$myitem['item_pkey']."'");
				$ncount++;
			}
		}

		// return result..
		if ($sa==1) {
			return $p_item_pkey;
		} else {
			return false;
		}
	}

}
?>