<?php



function drawRMADt($connection, $RMA_ID, $list_pending_items){
	$list_pending_items = explode(",", $list_pending_items);
	echo "<table style='border-collapse: collapse; width: 4250px;'   cellpadding='0' cellspacing='1'>
	<tr>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Item Code</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Item Description</th>
	<th style='font-size: 12px; width: 100px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Quantity</th>
	<th style='font-size: 12px; width: 100px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Outside Item</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Remark</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Charge</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Labor Cost</th>
	<th style='font-size: 12px; width: 350px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Serial No.</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Inv. Date</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Inv. No.</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Warranty</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Picked Up By</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Notes</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Received</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Problem</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Solution</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Physical Damage</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>DOA</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Informed by</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Informed on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Replied on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Back from srv on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Service Center</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Problem Category</th>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	</tr>";
	$curr_date = date("d/m/Y");
	
	$query_rmadt  = "select rmadt_id, rmadt.item_code as ItemCode, items.Item_Description as ItemName, rmadt.RMADt_Remark as RmaDtRemark,
	                 rmadt_price as RMADtCharge, rmadt_price2 as RMADtCharge2, RMADt_Remark2 as RMADtPickedUpBy, rmadt_notes as RmaDtNotes,
					 rmadt_received as RmaDtReceived, rmadt_problem as RmaDtProblem, rmadt_solution as RmaDtSolution, rmadt_physicalDamage as RmaDtPhysicalDamage,
					 rmadt_DOA as RmaDtDOA, date_format(rmadt_informedon, '%d/%m/%Y') as RmaDtInformedOn, date_format(rmadt_repliedon, '%d/%m/%Y') as RmaDtRepliedOn, date_format(rmadt_backfromsrvon, '%d/%m/%Y') as RmaDtBackFromSRVOn,
					 rmadt_servicecenter as RmaDtServiceCenter, RMADT_PROBLEMCATEGORY as RmaDtProblemCategory, Outside_Item as OutsideItem, RMADt_Quantity as RMADtQuantity, Serial_Number, date_format(Invoice_Date, '%d/%m/%Y') as Invoice_Date, Invoice_Number, Rmadt_Warranty, rmadt_informedby as rmadtinformedby
					 from temp_rmadt as rmadt inner join temp_rma as rma on rmadt.RMA_Id = rma.RMA_Id
					 left join items on items.item_code = rmadt.item_code
					 where 1=1 and rmadt.rma_id = '$RMA_ID' order by rmadt_id desc";
	$result_rmadt = mysql_query($query_rmadt, $connection);
	$number_rmadt = mysql_num_rows($result_rmadt);
	echo "<input type='hidden' name='number_of_items' id='number_of_items' value='$number_rmadt' />";
	$count = 1;
	while($row_rmadt = mysql_fetch_array($result_rmadt)){
		extract($row_rmadt);
		$count++;
		if($count %2 != 0){
			$color = "#f7f7f7";
		}
		else{
			$color = "#e6e6e6";
		}
		
		if($OutsideItem == 1){
			$checked = "checked";
		}
		else{
			$checked = "";
		}
		if(in_array($rmadt_id, $list_pending_items)){
			$color = "";
		}
		//$RmaDtRemark = ereg_replace("'", "\'", $RmaDtRemark);
		echo"<tr id='row_$rmadt_id' style='background: $color;'>
	<th  style='padding-left: 5px; padding-right: 5px;  padding-top: 5px; '><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteRMADt('$rmadt_id')\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='item_code_$rmadt_id' id='item_code_$rmadt_id' onkeyup=\"AutoCompleteData(event,this.id,'item_code_$rmadt_id','item_description_$rmadt_id','items');\" style='width: 100px' value='$ItemCode' onkeydown=\"moveNextField2('1', event, '$rmadt_id'); searchItem2(event, '$rmadt_id'); addPending('$rmadt_id', event)\" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems2('13','$rmadt_id')\"/></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='item_description_$rmadt_id' id='item_description_$rmadt_id' style='width: 150px' value='$ItemName' onkeydown=\"moveNextField2('2', event, '$rmadt_id'); addPending('$rmadt_id')\" /></th>
	<th style='font-size: 12px; width: 100px; padding-top: 5px;'><input type='text' name='RMADt_Quantity_$rmadt_id' id='RMADt_Quantity_$rmadt_id' style='width: 75px' value='$RMADtQuantity' onkeydown=\"searchItem2(event, '$rmadt_id'); moveNextField2('3', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 100px; padding-top: 5px;'><input type='hidden' id='Outside_Item_$rmadt_id' name='Outside_Item_$rmadt_id' value='$OutsideItem' /><input type='checkbox' name='chk_Outside_Item_$rmadt_id' id='chk_Outside_Item_$rmadt_id' $checked onclick=\"chkOutsideItem2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Remark_$rmadt_id' id='RMADt_Remark_$rmadt_id' style='width: 200px' value=\"$RmaDtRemark\" onkeydown=\"moveNextField2('4', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Charge_$rmadt_id' id='RMADt_Charge_$rmadt_id' style='width: 150px' value='$RMADtCharge' onkeydown=\"moveNextField2('5', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Charge2_$rmadt_id' id='RMADt_Charge2_$rmadt_id' style='width: 150px' value='$RMADtCharge2' onkeydown=\"moveNextField2('6', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Serial_Number_$rmadt_id' id='Serial_Number_$rmadt_id' style='width: 150px' value='$Serial_Number' onkeydown=\"moveNextField2('7', event, '$rmadt_id'); addPending('$rmadt_id', event)\" />&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListSerial2('$rmadt_id')\"/>&nbsp.&nbsp;<button onclick=\"openSerialHistory2('$rmadt_id')\"><span><span>H</span></span></button></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Invoice_Date_$rmadt_id' id='Invoice_Date_$rmadt_id' style='width: 150px' value='$Invoice_Date' onkeydown=\"moveNextField2('8', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Invoice_Number_$rmadt_id' id='Invoice_Number_$rmadt_id' style='width: 150px' value='$Invoice_Number' onkeydown=\"moveNextField2('9', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($Rmadt_Warranty == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='Rmadt_Warranty_$rmadt_id' id='Rmadt_Warranty_$rmadt_id' style='width: 150px' value='$Rmadt_Warranty' /><input type='checkbox' name='chk_Rmadt_Warranty_$rmadt_id' $checked id='chk_Rmadt_Warranty_$rmadt_id' onclick=\"chkRmadtWarranty2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_PickedUpBy_$rmadt_id' id='RMADt_PickedUpBy_$rmadt_id' style='width: 150px' value='$RMADtPickedUpBy' onkeydown=\"moveNextField2('10', event, '$rmadt_id'); addPending('$rmadt_id', event)\" />  <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory2(7, '$rmadt_id')\"/></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_notes_$rmadt_id' id='rmadt_notes_$rmadt_id' style='width: 150px' value='$RmaDtNotes' onkeydown=\"moveNextField2('11', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_received_$rmadt_id' id='rmadt_received_$rmadt_id' style='width: 150px' value='$RmaDtReceived'  onkeydown=\"moveNextField2('12', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /> <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory2(4, '$rmadt_id')\"/></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_problem_$rmadt_id' id='rmadt_problem_$rmadt_id' style='width: 150px' value='$RmaDtProblem' onkeydown=\"moveNextField2('13', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_solution_$rmadt_id' id='rmadt_solution_$rmadt_id' style='width: 150px' value='$RmaDtSolution' onkeydown=\"moveNextField2('14', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($RmaDtPhysicalDamage == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='rmadt_physicalDamage_$rmadt_id' id='rmadt_physicalDamage_$rmadt_id' value='$RmaDtPhysicalDamage' /><input type='checkbox' name='chk_rmadt_physicalDamage_$rmadt_id' id='chk_rmadt_physicalDamage_$rmadt_id' $checked onclick=\"chkRmadtPhysicalDamage2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($RmaDtDOA == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='rmadt_DOA_$rmadt_id' id='rmadt_DOA_$rmadt_id' value='$RmaDtDOA' /><input type='checkbox' name='chk_rmadt_DOA_$rmadt_id' id='chk_rmadt_DOA_$rmadt_id' $checked onclick=\"chkRmadtDOA2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_informedby_$rmadt_id' id='rmadt_informedby_$rmadt_id' style='width: 150px' value='$rmadtinformedby' onkeydown=\"moveNextField2('15', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_informedon_$rmadt_id' id='rmadt_informedon_$rmadt_id' onfocus='getDate(this.id)' style='width: 150px' value='$RmaDtInformedOn' onkeydown=\"moveNextField2('16', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_repliedon_$rmadt_id' id='rmadt_repliedon_$rmadt_id' onfocus='getDate(this.id)' style='width: 150px' value='$RmaDtRepliedOn' onkeydown=\"moveNextField2('17', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_backfromsrvon_$rmadt_id' id='rmadt_backfromsrvon_$rmadt_id' onfocus='getDate(this.id)' style='width: 150px' value='$RmaDtBackFromSRVOn' onkeydown=\"moveNextField2('18', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_servicecenter_$rmadt_id' id='rmadt_servicecenter_$rmadt_id' style='width: 150px' value='$RmaDtServiceCenter' onkeydown=\"moveNextField2('19', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADT_PROBLEMCATEGORY_$rmadt_id' id='RMADT_PROBLEMCATEGORY_$rmadt_id' style='width: 150px' value=\"$RmaDtProblemCategory\" onkeydown=\"searchproblem2(event, '$rmadt_id'); addPending('$rmadt_id', event)\" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory2(2, '$rmadt_id')\"/></th>
	<th style='padding-left: 5px; padding-right: 5px;padding-top: 5px; '><img src='img/confirm-1.png' width=20 style='cursor: pointer;' onclick=\"updateRMADt('$rmadt_id')\"/></th>
	<th style='padding-left: 5px; padding-right: 5px; padding: 5px; width: 50px'></th>
	</tr>";
	}
	echo "<input type='hidden' id='new_item_added' value='' />";
	echo"<tr style='border: solid 1px #00000'>
	<th style='padding-left: 5px; padding-right: 5px; background: #f7f7f7; padding-top: 5px; '><img src='img/undo-1.png' style='cursor: pointer' width=20 onclick=\"clearGrid()\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='item_code' id='item_code' autofocus='autofocus' style='width: 100px' onkeydown=\"searchItem(event); moveNextField('1', event); changeNewItem(event)\" onkeyup=\"AutoCompleteData(event,this.id,'item_code','item_description','items');\" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems(12)\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='item_description' id='item_description' style='width: 150px' onkeydown=\"moveNextField('2', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 100px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Quantity' id='RMADt_Quantity' value=1 style='width: 75px' onkeydown=\"searchItem(event);  moveNextField('3', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 100px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' id='Outside_Item' name='Outside_Item' value=1 /><input type='checkbox' name='chk_Outside_Item' id='chk_Outside_Item' onclick=\"chkOutsideItem(); changeNewItem(event)\" checked /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Remark' id='RMADt_Remark' style='width: 200px' onkeydown=\"moveNextField('4', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Charge' id='RMADt_Charge' style='width: 150px' onkeydown=\"moveNextField('5', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Charge2' id='RMADt_Charge2' style='width: 150px' onkeydown=\"moveNextField('6', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Serial_Number' id='Serial_Number' style='width: 150px' onkeydown=\"moveNextField('7', event); changeNewItem(event); getInvoice(this.value, event); \" />&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListSerial()\"/>&nbsp.&nbsp;<button onclick=\"openSerialHistory()\"><span><span>H</span></span></button></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Invoice_Date' value='' id='Invoice_Date' style='width: 150px' onkeydown=\"moveNextField('8', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Invoice_Number' id='Invoice_Number' style='width: 150px' onkeydown=\"moveNextField('9', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='Rmadt_Warranty' id='Rmadt_Warranty' style='width: 150px' /><input type='checkbox' name='chk_Rmadt_Warranty' id='chk_Rmadt_Warranty' onclick=\"chkRmadtWarranty(); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_PickedUpBy' id='RMADt_PickedUpBy' style='width: 150px' onkeydown=\"moveNextField('10', event); changeNewItem(event)\" /> <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory(6)\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_notes' id='rmadt_notes' style='width: 150px' onkeydown=\"moveNextField('11', event); changeNewItem(event);\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_received' id='rmadt_received' style='width: 150px' onkeydown=\"moveNextField('12', event); changeNewItem(event)\" /> <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory(3)\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_problem' id='rmadt_problem' style='width: 150px' onkeydown=\"moveNextField('13', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_solution' id='rmadt_solution' style='width: 150px' onkeydown=\"moveNextField('14', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='rmadt_physicalDamage' id='rmadt_physicalDamage' value=0 /><input type='checkbox' name='chk_rmadt_physicalDamage' id='chk_rmadt_physicalDamage' onclick=\"chkRmadtPhysicalDamage(); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='rmadt_DOA' id='rmadt_DOA' value=0 /><input type='checkbox' name='chk_rmadt_DOA' id='chk_rmadt_DOA' onclick=\"chkRmadtDOA(); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_informedby' id='rmadt_informedby' value='' style='width: 150px' onkeydown=\"moveNextField('15', event); changeNewItem(event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_informedon' id='rmadt_informedon' onfocus='getDate(this.id)' value='' style='width: 150px' onkeydown=\"moveNextField('16', event); changeNewItem(event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_repliedon' id='rmadt_repliedon' onfocus='getDate(this.id)' value='' style='width: 150px' onkeydown=\"moveNextField('17', event); changeNewItem(event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_backfromsrvon' id='rmadt_backfromsrvon' onfocus='getDate(this.id)' value='' style='width: 150px'  onkeydown=\"moveNextField('18', event); changeNewItem(event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_servicecenter' id='rmadt_servicecenter' style='width: 150px' onkeydown=\"moveNextField('19', event); changeNewItem(event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADT_PROBLEMCATEGORY' id='RMADT_PROBLEMCATEGORY' value='To Be Filled' style='width: 150px' onkeydown=\"searchproblem(event); \" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListProblemCategory(1)\"/></th>
	<th style='padding-left: 5px; padding-right: 5px; background: #f7f7f7; padding-top: 5px; '><img src='img/confirm-1.png' width=20 style='cursor: pointer' onclick=\"saveRmaDtInsert()\"/></th>
	</tr>";
	echo"</table>";
}
function drawReturnRMADt($connection, $RMA_ID){
	$query_module  = "select Module_RMATabSheet from module";
	$result_module = mysql_query($query_module, $connection);
	$row_module    = mysql_fetch_array($result_module);
	extract($row_module);
	echo "<table style='border-collapse: collapse; width: 4150px;'   cellpadding='0' cellspacing='1'>
	<tr>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Item Code</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Item Description</th>
	<th style='font-size: 12px; width: 75px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Repair</th>
	<th style='font-size: 12px; width: 100px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Quantity</th>
	<th style='font-size: 12px; width: 100px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Outside Item</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Remark</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Charge</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Charge 2</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Serial No.</th>";
	if($Module_RMATabSheet != 1){
	echo"<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>New Serial No.</th>";
	}
	echo"<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Inv. Date</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Inv. No.</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Warranty</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Picked Up By</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Notes</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Received</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Problem</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Solution</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Physical Damage</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>DOA</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Return</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Informed on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Replied on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Back from srv on</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Service Center</th>
	<th style='font-size: 12px; width: 250px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'>Problem Category</th>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	<th style='padding-left: 5px; padding-right: 5px; background: #d8d8d8; border-bottom: solid 1px #000000; padding-top: 8px; padding-bottom: 8px'></th>
	</tr>";
	$curr_date = date("d/m/Y");
	
	$query_rmadt  = "select rmadt_id, rmadt.rma_id, rmadt.item_code as ItemCode, items.Item_Description as ItemName, rmadt.RMADt_Remark as RmaDtRemark,
	                 rmadt_price as RMADtCharge, rmadt_price2 as RMADtCharge2, RMADt_Remark2 as RMADtPickedUpBy, rmadt_notes as RmaDtNotes,
					 rmadt_received as RmaDtReceived, rmadt_problem as RmaDtProblem, rmadt_solution as RmaDtSolution, rmadt_physicalDamage as RmaDtPhysicalDamage,
					 rmadt_DOA as RmaDtDOA, date_format(rmadt_informedon, '%d/%m/%Y') as RmaDtInformedOn, date_format(rmadt_repliedon, '%d/%m/%Y') as RmaDtRepliedOn, date_format(rmadt_backfromsrvon, '%d/%m/%Y') as RmaDtBackFromSRVOn,
					 rmadt_servicecenter as RmaDtServiceCenter, RMADT_PROBLEMCATEGORY as RmaDtProblemCategory, Outside_Item as OutsideItem, RMADt_Quantity as RMADtQuantity, Serial_Number, date_format(Invoice_Date, '%d/%m/%Y') as Invoice_Date, Invoice_Number, Rmadt_Warranty, RMADt_Repaire, NewSerial_Number, ifNull(rmadt.RmaDt_Return, 0) as RmaDtReturn
					 from temp_rmadt as rmadt inner join temp_rma as rma on rmadt.RMA_Id = rma.RMA_Id
					 left join items on items.item_code = rmadt.item_code
					 where 1=1 and rmadt.rma_id = '$RMA_ID' order by rmadt_id desc";
	//echo $query_rmadt;
	$result_rmadt = mysql_query($query_rmadt, $connection);
	$number_rmadt = mysql_num_rows($result_rmadt);
	echo "<input type='hidden' name='number_of_items' id='number_of_items' value='$number_rmadt' />";
	$count = 1;
	while($row_rmadt = mysql_fetch_array($result_rmadt)){
		extract($row_rmadt);
		$count++;
		if($count %2 != 0){
			$color = "#f7f7f7";
		}
		else{
			$color = "#e6e6e6";
		}
		
		if($OutsideItem == 1){
			$checked = "checked";
		}
		else{
			$checked = "";
		}
		
		echo"<tr id='row_$rmadt_id' style='background: $color;'>
	<th style='padding-left: 5px; padding-right: 5px; padding-top: 5px; '><img src='img/btn_details.png' width=20 style='cursor: pointer' onclick=\"modifyRMADt('$rmadt_id', '$rma_id')\" /></th>
	<th style='padding-left: 5px; padding-right: 5px; padding-top: 5px; '><img src='img/delete.png' width=20 style='cursor: pointer' onclick=\"deleteRMADt('$rmadt_id')\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='item_code_$rmadt_id' id='item_code_$rmadt_id' style='width: 100px' value='$ItemCode' onkeydown=\"moveNextField2('1', event, '$rmadt_id'); searchItem2(event, '$rmadt_id'); addPending('$rmadt_id', event)\" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems2('13','$rmadt_id')\"/></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='item_description_$rmadt_id' id='item_description_$rmadt_id' style='width: 150px' value='$ItemName' onkeydown=\"moveNextField2('2', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 75px; padding-top: 5px;'><input type='text' name='RMADt_Repaire_$rmadt_id' id='RMADt_Repaire_$rmadt_id' style='width: 50px' value='$RMADt_Repaire' onkeydown=\"moveNextField2('3', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 100px; padding-top: 5px;'><input type='text' name='RMADt_Quantity_$rmadt_id' id='RMADt_Quantity_$rmadt_id' style='width: 75px' value='$RMADtQuantity' onkeydown=\"searchItem2(event, '$rmadt_id'); moveNextField2('4', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 100px; padding-top: 5px;'><input type='hidden' id='Outside_Item_$rmadt_id' name='Outside_Item_$rmadt_id' value='$OutsideItem' /><input type='checkbox' name='chk_Outside_Item_$rmadt_id' id='chk_Outside_Item_$rmadt_id' $checked onclick=\"chkOutsideItem2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Remark_$rmadt_id' id='RMADt_Remark_$rmadt_id' style='width: 200px' value='$RmaDtRemark' onkeydown=\"moveNextField2('5', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Charge_$rmadt_id' id='RMADt_Charge_$rmadt_id' style='width: 150px' value='$RMADtCharge' onkeydown=\"moveNextField2('5', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_Charge2_$rmadt_id' id='RMADt_Charge2_$rmadt_id' style='width: 150px' value='$RMADtCharge2' onkeydown=\"moveNextField2('7', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Serial_Number_$rmadt_id' id='Serial_Number_$rmadt_id' style='width: 150px' value='$Serial_Number' onkeydown=\"moveNextField2('8', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($Module_RMATabSheet != 1){
		echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='NewSerial_Number_$rmadt_id' id='NewSerial_Number_$rmadt_id' style='width: 150px' value='$NewSerial_Number' /></th>";
	}
	else{
		echo"<input type='hidden' name='NewSerial_Number_$rmadt_id' id='NewSerial_Number_$rmadt_id' style='width: 150px' value='$NewSerial_Number' />";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Invoice_Date_$rmadt_id' id='Invoice_Date_$rmadt_id' style='width: 150px' value='$Invoice_Date' onkeydown=\"moveNextField2('9', event, '$rmadt_id') ; addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='Invoice_Number_$rmadt_id' id='Invoice_Number_$rmadt_id' style='width: 150px' value='$Invoice_Number' onkeydown=\"moveNextField2('10', event, '$rmadt_id') ; addPending('$rmadt_id', event)\" /></th>";
	if($Rmadt_Warranty == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='Rmadt_Warranty_$rmadt_id' id='Rmadt_Warranty_$rmadt_id' style='width: 150px' value='$Rmadt_Warranty' /><input type='checkbox' name='chk_Rmadt_Warranty_$rmadt_id' $checked id='chk_Rmadt_Warranty_$rmadt_id' onclick=\"chkRmadtWarranty2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADt_PickedUpBy_$rmadt_id' id='RMADt_PickedUpBy_$rmadt_id' style='width: 150px' value='$RMADtPickedUpBy' onkeydown=\"moveNextField2('11', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_notes_$rmadt_id' id='rmadt_notes_$rmadt_id' style='width: 150px' value='$RmaDtNotes' onkeydown=\"moveNextField2('12', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_received_$rmadt_id' id='rmadt_received_$rmadt_id' style='width: 150px' value='$RmaDtReceived'  onkeydown=\"moveNextField2('13', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_problem_$rmadt_id' id='rmadt_problem_$rmadt_id' style='width: 150px' value='$RmaDtProblem' onkeydown=\"moveNextField2('14', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_solution_$rmadt_id' id='rmadt_solution_$rmadt_id' style='width: 150px' value='$RmaDtSolution' onkeydown=\"moveNextField2('15', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($RmaDtPhysicalDamage == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='rmadt_physicalDamage_$rmadt_id' id='rmadt_physicalDamage_$rmadt_id' value='$RmaDtPhysicalDamage' /><input type='checkbox' name='chk_rmadt_physicalDamage_$rmadt_id' id='chk_rmadt_physicalDamage_$rmadt_id' $checked onclick=\"chkRmadtPhysicalDamage2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>";
	if($RmaDtDOA == 1){
		$checked = "checked";
	}
	else{
		$checked = "";
	}
	if($RmaDtReturn==1){
		$ch="checked";
	}
	else{
		$ch="";
	}
	echo"<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='rmadt_DOA_$rmadt_id' id='rmadt_DOA_$rmadt_id' value='$RmaDtDOA' /><input type='checkbox' name='chk_rmadt_DOA_$rmadt_id' id='chk_rmadt_DOA_$rmadt_id' $checked onclick=\"chkRmadtDOA2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='hidden' name='rmadt_return_$rmadt_id' id='rmadt_return_$rmadt_id' value='$RmaDtReturn' /><input type='checkbox' name='chk_rmadt_return_$rmadt_id' id='chk_return_DOA_$rmadt_id' $ch onclick=\"chkRmadtReturn2('$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_informedon_$rmadt_id' id='rmadt_informedon_$rmadt_id' style='width: 150px' value='$RmaDtInformedOn' onkeydown=\"moveNextField2('16', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_repliedon_$rmadt_id' id='rmadt_repliedon_$rmadt_id' style='width: 150px' value='$RmaDtRepliedOn' onkeydown=\"moveNextField2('17', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_backfromsrvon_$rmadt_id' id='rmadt_backfromsrvon_$rmadt_id' style='width: 150px' value='$RmaDtBackFromSRVOn' onkeydown=\"moveNextField2('18', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='rmadt_servicecenter_$rmadt_id' id='rmadt_servicecenter_$rmadt_id' style='width: 150px' value='$RmaDtServiceCenter' onkeydown=\"moveNextField2('19', event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='font-size: 12px; width: 250px; padding-top: 5px;'><input type='text' name='RMADT_PROBLEMCATEGORY_$rmadt_id' id='RMADT_PROBLEMCATEGORY_$rmadt_id' style='width: 150px' value='$RmaDtProblemCategory' onkeydown=\"searchproblem2(event, '$rmadt_id'); addPending('$rmadt_id', event)\" /></th>
	<th style='padding-left: 5px; padding-right: 5px; padding-top: 5px; '><img src='img/confirm-1.png' width=20 style='cursor: pointer;' onclick=\"updateRMADt('$rmadt_id')\"/></th>
	<th style='padding-left: 5px; padding-right: 5px; padding: 5px; width: 50px'></th>
	</tr>";
	}
	/*
	echo"<tr style='border: solid 1px #00000'>
	<th style='padding-left: 5px; padding-right: 5px; background: #f7f7f7; padding-top: 5px; '><img src='img/undo-1.png' style='cursor: pointer' width=20 onclick=\"clearGrid()\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='item_code' id='item_code' autofocus='autofocus' style='width: 100px' onkeydown=\"searchItem(event); moveNextField('1', event)\" />&nbsp;&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems(12)\"/></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='item_description' id='item_description' style='width: 150px' onkeydown=\"moveNextField('2', event)\" /></th>
	<th style='font-size: 12px; width: 100px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Quantity' id='RMADt_Quantity' style='width: 75px' onkeydown=\"searchItem(event);  moveNextField('3', event)\" /></th>
	<th style='font-size: 12px; width: 100px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' id='Outside_Item' name='Outside_Item' value=0 /><input type='checkbox' name='chk_Outside_Item' id='chk_Outside_Item' onclick=\"chkOutsideItem()\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Remark' id='RMADt_Remark' style='width: 200px' onkeydown=\"moveNextField('4', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Charge' id='RMADt_Charge' style='width: 150px' onkeydown=\"moveNextField('5', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_Charge2' id='RMADt_Charge2' style='width: 150px' onkeydown=\"moveNextField('6', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Serial_Number' id='Serial_Number' style='width: 150px' onkeydown=\"moveNextField('7', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Invoice_Date' value='$curr_date' id='Invoice_Date' style='width: 150px' onkeydown=\"moveNextField('8', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='Invoice_Number' id='Invoice_Number' style='width: 150px' onkeydown=\"moveNextField('9', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='Rmadt_Warranty' id='Rmadt_Warranty' style='width: 150px' /><input type='checkbox' name='chk_Rmadt_Warranty' id='chk_Rmadt_Warranty' onclick=chkRmadtWarranty() /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADt_PickedUpBy' id='RMADt_PickedUpBy' style='width: 150px' onkeydown=\"moveNextField('10', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_notes' id='rmadt_notes' style='width: 150px' onkeydown=\"moveNextField('11', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_received' id='rmadt_received' style='width: 150px' onkeydown=\"moveNextField('12', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_problem' id='rmadt_problem' style='width: 150px' onkeydown=\"moveNextField('13', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_solution' id='rmadt_solution' style='width: 150px' onkeydown=\"moveNextField('14', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='rmadt_physicalDamage' id='rmadt_physicalDamage' value=0 /><input type='checkbox' name='chk_rmadt_physicalDamage' id='chk_rmadt_physicalDamage' onclick=\"chkRmadtPhysicalDamage()\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='hidden' name='rmadt_DOA' id='rmadt_DOA' value=0 /><input type='checkbox' name='chk_rmadt_DOA' id='chk_rmadt_DOA' onclick=\"chkRmadtDOA()\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_informedon' id='rmadt_informedon' value='$curr_date' style='width: 150px' onkeydown=\"moveNextField('15', event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_repliedon' id='rmadt_repliedon' value='$curr_date' style='width: 150px' onkeydown=\"moveNextField('16', event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_backfromsrvon' id='rmadt_backfromsrvon' value='$curr_date' style='width: 150px'  onkeydown=\"moveNextField('17', event)\" /> </th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='rmadt_servicecenter' id='rmadt_servicecenter' style='width: 150px' onkeydown=\"moveNextField('18', event)\" /></th>
	<th style='font-size: 12px; width: 250px; background: #f7f7f7; padding-top: 5px;'><input type='text' name='RMADT_PROBLEMCATEGORY' id='RMADT_PROBLEMCATEGORY' style='width: 150px' onkeydown=\"moveNextField('19', event)\" /></th>
	<th style='padding-left: 5px; padding-right: 5px; background: #f7f7f7; padding-top: 5px; '><img src='img/confirm-1.png' width=20 style='cursor: pointer' onclick=\"saveRmaDtInsert()\"/></th>
	</tr>";
	*/
	echo"</table>";
}
function drawListRMA($connection, $FI_RMA, $FI_RMAApp, $FI_RMAMod, $FI_RMADel, $ref, $date, $client_code, $start, $pagenumber){
	if($start ==""){
		$start = 0;
	}
	if($pagenumber == ""){
		$pagenumber = 1;
	}
	echo "<table width=1090>
<tr>
<th style='width: 15px; background: #1e87a5; padding: 5px; '></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 60px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Reference</th>
<th style='width: 100px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Date</th>
<th style='width: 590px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Client</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Charge Amount</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Currency</th>
</tr>";
$query_rma = "select rma.rma_id, rma.RMA_reference as RMARef, date_format(RMA_Date, '%d/%m/%Y') as RMADate, rma.client_code as ClientCode, clients.ledger_name as ClientName,
             format(rma.Rmacharge_amount, 2) as RMAAmount, rma.Currency_code
			 from rma left join clients on clients.ledger_number = rma.client_code
			 where 1=1 and rma_type = 'MI'";
if($ref != ""){
	$query_rma = $query_rma." and RMA_reference = '$ref'";
}
if($client_code != ""){
	$query_rma = $query_rma." and rma.client_code = '$client_code'";
}
if($date != ""){
$date = explode("/", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
$query_rma = $query_rma." and rma.RMA_Date between '$date' and '$date 23:59:59'";
}
$result_rma2 = mysql_query($query_rma, $connection);
$number_rma = mysql_num_rows($result_rma2);
$total_num_pages = ceil($number_rma/10);
$query_rma  = $query_rma." order by rma.RMA_Date desc LIMIT $start, 10";
$result_rma = mysql_query($query_rma, $connection);
$count = 0; 
while($row_rma = mysql_fetch_array($result_rma)){
	extract($row_rma);
	$count++;
	if($count %2 != 0){
		$background = "#e6e6e6";
	}
	else{
		$background = "#f7f7f7";
	}
	$query_pending  = "select count(*) as PendingRMA from temp_rma where rma_id = '$rma_id'";
	$result_pending = mysql_query($query_pending, $connection);
	$row_pending = mysql_fetch_array($result_pending);
	extract($row_pending);
	echo "<tr>
<td style='width: 15px; background: $background; padding: 5px; '><img src='img/printpreview.png' style='cursor: pointer' width=20 onclick=\"printPreview('$rma_id')\" /></td>
<td style='width: 15px; background: $background; padding: 5px'>";
if($FI_RMADel == 1){
echo"<img src='img/delete.png' style='cursor: pointer' width=20 onclick=\"deleteRMA('$rma_id')\" />";
}
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'>";
if($PendingRMA == 0){
	if($FI_RMAMod == 1){
echo"<img src='img/edit.png' style='cursor: pointer' width=20 onclick=\"editRMA('$rma_id')\" />";
	}
}
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/details.gif' style='cursor: pointer' width=15 id='show_$rma_id' onclick=\"displayDetails('$rma_id')\"/></td>
<td style='width: 60px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMARef</td>
<td style='width: 100px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMADate</td>
<td style='width: 590px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$ClientCode - $ClientName</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold '>$RMAAmount</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$Currency_code</td>
</tr>";
/*
echo "<script>
$(document).ready(function(){
    $(\"#show_$rma_id\").click(function(){
        $(\"#details_$rma_id\").toggle(\"medium\");
    });
});
</script>";
*/
echo "<tr>
<th colspan=9 style='background: $background; min-height: 0px;'><div style='display: none; width: 900px; margin: auto; background: #ccffff; border: solid 1px #33ccff; padding: 5px;' id='details_$rma_id'>";

echo"</div></th>
</tr>";
}
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo "<tr>
<th colspan=9>
<div style='display: inline-block; text-align: right; width: 48%'>";
if($start > 0){
echo"<img src='img/arrowleft.png' style='cursor: pointer' onclick=\"movePrev()\" />";
}
//echo $pagenumber;
echo"</div>
<div style='display: inline-block; text-align: left; width: 48%'>";
if($pagenumber != $total_num_pages && $number_rma != 0){
echo"<img src='img/arrowright.png' style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</tr>";
echo"</table>";
}
function drawListRMAToLoad($connection, $ref, $date, $client_code, $start, $pagenumber){
	if($start ==""){
		$start = 0;
	}
	if($pagenumber == ""){
		$pagenumber = 1;
	}
	echo "<table width=1090>
<tr>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 60px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Reference</th>
<th style='width: 100px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Date</th>
<th style='width: 590px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Client</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Charge Amount</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Currency</th>
</tr>";
$query_rma = "select rma.rma_id, rma.RMA_reference as RMARef, date_format(rma.RMA_Date, '%d/%m/%Y') as RMADate,
rma.client_code as ClientCode, clients.ledger_name as ClientName,
format(rma.Rmacharge_amount, 2) as RMAAmount, rma.Currency_code, count(rmadt.rmadt_id)
from rma
left join clients on clients.ledger_number = rma.client_code
inner join rmadt on rmadt.rma_id = rma.rma_id
left join rmadt rmadt2 on rmadt2.MI_ID=rmadt.rmadt_id
where 1=1 and rma.rma_type = 'MI'
and rmadt2.MI_ID is null ";
//$query_rma = $query_rma." and (rmadt.rmadt_qcapproved = 1 or rmadt.rmadt_approved = 0)";
if($ref != ""){
	$query_rma = $query_rma." and rma.RMA_reference = '$ref'";
}
if($client_code != ""){
	$query_rma = $query_rma." and rma.client_code = '$client_code'";
}
if($date != ""){
$date = explode("/", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
$query_rma = $query_rma." and rma.RMA_Date between '$date' and '$date 23:59:59'";
}
$query_rma = $query_rma." group by rmadt.rma_id order by rma.RMA_Date desc ";
//echo $query_rma;
$result_rma = mysql_query($query_rma, $connection);
$count = 0; 
while($row_rma = mysql_fetch_array($result_rma)){
	extract($row_rma);
	$count++;
	if($count %2 != 0){
		$background = "#e6e6e6";
	}
	else{
		$background = "#f7f7f7";
	}
	$query_pending  = "select count(*) as PendingRMA from temp_rma where rma_id = '$rma_id'";
	$result_pending = mysql_query($query_pending, $connection);
	$row_pending = mysql_fetch_array($result_pending);
	extract($row_pending);
	echo "<tr>
	<td style='width: 15px; background: $background; padding: 5px'><input type='radio' name='rma_id' id='rma_id_$rma_id' onclick=\"fillRmaId('$rma_id')\" /></td>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/details.gif' style='cursor: pointer' width=15 id='show_$rma_id' onclick=\"displayDetails('$rma_id')\"/></td>
<td style='width: 60px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMARef</td>
<td style='width: 100px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMADate</td>
<td style='width: 590px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$ClientCode - $ClientName</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold '>$RMAAmount</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$Currency_code</td>
</tr>";

/*
echo "<script>
$(document).ready(function(){
    $(\"#show_$rma_id\").click(function(){
        $(\"#details_$rma_id\").toggle(\"medium\");
    });
});
</script>";
*/
echo "<tr>
<th colspan=9 style='background: $background; min-height: 0px;'><div style='display: none; width: 900px; margin: auto; background: #ccffff; border: solid 1px #33ccff; padding: 5px;' id='details_$rma_id'>";

echo"</div></th>
</tr>";
}
echo "<input type='hidden' id='rma_id2' value='' />";
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo "<tr>
<th colspan=9>
<div style='display: inline-block; text-align: right; width: 48%'>";
if($start > 0){
echo"<img src='img/arrowleft.png' style='cursor: pointer' onclick=\"movePrev()\" />";
}
//echo $pagenumber;
echo"</div>
<div style='display: inline-block; text-align: left; width: 48%'>";
if($pagenumber != $total_num_pages && $number_rma != 0){
echo"<img src='img/arrowright.png' style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</tr>";
echo"</table>";
}
function drawListReturningRMA($connection, $FI_ReturnRMAApp, $FI_ReturnRMAMod, $FI_ReturnRMADel, $ref, $date, $client_code, $start, $pagenumber){
	//echo $FI_ReturnRMAMod;
	if($start ==""){
		$start = 0;
	}
	if($pagenumber == ""){
		$pagenumber = 1;
	}
	echo "<table width=1090>
<tr>
<th style='width: 15px; background: #1e87a5; padding: 5px; '></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 60px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Reference</th>
<th style='width: 100px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Date</th>
<th style='width: 590px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Client</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Charge Amount</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Currency</th>
</tr>";
$query_rma = "select rma.rma_id, rma.RMA_reference as RMARef, date_format(RMA_Date, '%d/%m/%Y') as RMADate, rma.client_code as ClientCode, clients.ledger_name as ClientName,
             format(rma.Rmacharge_amount, 2) as RMAAmount, rma.Currency_code
			 from rma left join clients on clients.ledger_number = rma.client_code
			 where 1=1 and rma_type = 'MR'";
if($ref != ""){
	$query_rma = $query_rma." and RMA_reference = '$ref'";
}
if($client_code != ""){
	$query_rma = $query_rma." and rma.client_code = '$client_code'";
}
if($date != ""){
$date = explode("/", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
$query_rma = $query_rma." and rma.RMA_Date between '$date' and '$date 23:59:59'";
}
$result_rma2 = mysql_query($query_rma, $connection);
$number_rma = mysql_num_rows($result_rma2);
$total_num_pages = ceil($number_rma/10);
$query_rma  = $query_rma." order by RMA_reference desc, RMA_Date desc LIMIT $start, 10";
$result_rma = mysql_query($query_rma, $connection);
$count = 0; 
while($row_rma = mysql_fetch_array($result_rma)){
	extract($row_rma);
	$count++;
	if($count %2 != 0){
		$background = "#e6e6e6";
	}
	else{
		$background = "#f7f7f7";
	}
	$query_pending  = "select count(*) as PendingRMA from temp_rma where rma_id = '$rma_id'";
	$result_pending = mysql_query($query_pending, $connection);
	$row_pending = mysql_fetch_array($result_pending);
	extract($row_pending);
	echo "<tr>
<td style='width: 15px; background: $background; padding: 5px; '><img src='img/printpreview.png' style='cursor: pointer' width=20 onclick=\"printPreview('$rma_id')\" /></td>
<td style='width: 15px; background: $background; padding: 5px'>";
if($FI_ReturnRMADel == 1){
echo"<img src='img/delete.png' style='cursor: pointer' width=20 onclick=\"deleteRMA('$rma_id')\" />";
}
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'>";
if($PendingRMA == 0){
	if($FI_ReturnRMAMod == 1){
echo"<img src='img/edit.png' style='cursor: pointer' width=20 onclick=\"editRMA('$rma_id')\" />";
	}
}
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/details.gif' style='cursor: pointer' width=15 id='show_$rma_id' onclick=\"displayDetails('$rma_id')\"/></td>
<td style='width: 60px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMARef</td>
<td style='width: 100px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMADate</td>
<td style='width: 590px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$ClientCode - $ClientName</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold '>$RMAAmount</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$Currency_code</td>
</tr>";
/*
echo "<script>
$(document).ready(function(){
    $(\"#show_$rma_id\").click(function(){
        $(\"#details_$rma_id\").toggle(\"medium\");
    });
});
</script>";
*/
echo "<tr>
<th colspan=9 style='background: $background; min-height: 0px;'><div style='display: none; width: 900px; margin: auto; background: #ccffff; border: solid 1px #33ccff; padding: 5px;' id='details_$rma_id'>";

echo"</div></th>
</tr>";
}
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo "<tr>
<th colspan=9>
<div style='display: inline-block; text-align: right; width: 48%'>";
if($start > 0){
echo"<img src='img/arrowleft.png' style='cursor: pointer' onclick=\"movePrev()\" />";
}
//echo $pagenumber;
echo"</div>
<div style='display: inline-block; text-align: left; width: 48%'>";
if($pagenumber != $total_num_pages && $number_rma != 0){
echo"<img src='img/arrowright.png' style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</tr>";
echo"</table>";
}
function drawListRMAPending($connection, $ref, $date, $client_code, $start, $pagenumber){
	if($start ==""){
		$start = 0;
	}
	if($pagenumber == ""){
		$pagenumber = 1;
	}
	echo "<table width=1090>
<tr>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 60px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Reference</th>
<th style='width: 100px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Date</th>
<th style='width: 590px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Client</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Charge Amount</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Currency</th>
</tr>";
$query_rma = "select rma.rma_id, rma.RMA_reference as RMARef, date_format(RMA_Date, '%d/%m/%Y') as RMADate, rma.client_code as ClientCode, clients.ledger_name as ClientName,
             format(rma.Rmacharge_amount, 2) as RMAAmount, rma.Currency_code
			 from temp_rma as rma left join clients on clients.ledger_number = rma.client_code
			 where 1=1 and rma_type = 'MI'";
if($ref != ""){
	$query_rma = $query_rma." and RMA_reference = '$ref'";
}
if($client_code != ""){
	$query_rma = $query_rma." and rma.client_code = '$client_code'";
}
//echo $query_rma;
if($date != ""){
$date = explode("/", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
$query_rma = $query_rma." and rma.RMA_Date between '$date' and '$date 23:59:59'";
}
$result_rma2 = mysql_query($query_rma, $connection);
$number_rma = mysql_num_rows($result_rma2);
$total_num_pages = ceil($number_rma/10);
$query_rma  = $query_rma." order by RMA_Date desc , RMA_Id desc LIMIT $start, 10";
$result_rma = mysql_query($query_rma, $connection);
$count = 0; 
while($row_rma = mysql_fetch_array($result_rma)){
	extract($row_rma);
	$count++;
	if($count %2 != 0){
		$background = "#e6e6e6";
	}
	else{
		$background = "#f7f7f7";
	}
	$query_pending  = "select count(*) as PendingRMA from temp_rma where rma_id = '$rma_id'";
	$result_pending = mysql_query($query_pending, $connection);
	$row_pending = mysql_fetch_array($result_pending);
	extract($row_pending);
	echo "<tr>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/delete.png' style='cursor: pointer' width=20 onclick=\"deleteRMA('$rma_id')\" /></td>
<td style='width: 15px; background: $background; padding: 5px'>";
echo"<img src='img/edit.png' style='cursor: pointer' width=20 onclick=\"editRMA('$rma_id')\" />";
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/details.gif' style='cursor: pointer' width=15 id='show_$rma_id' onclick=\"displayDetails('$rma_id')\"/></td>
<td style='width: 60px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMARef</td>
<td style='width: 100px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMADate</td>
<td style='width: 590px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$ClientCode - $ClientName</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold '>$RMAAmount</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$Currency_code</td>
</tr>";
/*
echo "<script>
$(document).ready(function(){
    $(\"#show_$rma_id\").click(function(){
        $(\"#details_$rma_id\").toggle(\"medium\");
    });
});
</script>";
*/
echo "<tr>
<th colspan=8 style='background: $background; min-height: 0px;'><div style='display: none; width: 900px; margin: auto; background: #ccffff; border: solid 1px #33ccff; padding: 5px;' id='details_$rma_id'>";

echo"</div></th>
</tr>";
}
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo "<tr>
<th colspan=9>
<div style='display: inline-block; text-align: right; width: 48%'>";
if($start > 0){
echo"<img src='img/arrowleft.png' style='cursor: pointer' onclick=\"movePrev()\" />";
}
//echo $pagenumber;
echo"</div>
<div style='display: inline-block; text-align: left; width: 48%'>";
if($pagenumber != $total_num_pages && $number_rma != 0){
echo"<img src='img/arrowright.png' style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</tr>";
echo"</table>";
}
function drawListReturnRMAPending($connection, $ref, $date, $client_code, $start, $pagenumber){
	if($start ==""){
		$start = 0;
	}
	if($pagenumber == ""){
		$pagenumber = 1;
	}
	echo "<table width=1090>
<tr>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 15px; background: #1e87a5; padding: 5px'></th>
<th style='width: 60px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Reference</th>
<th style='width: 100px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Date</th>
<th style='width: 590px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Client</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Charge Amount</th>
<th style='width: 140px; background: #1e87a5; padding: 5px; font-size: 14px; font-family: Arial; color: #FFFFFF;'>Currency</th>
</tr>";
$query_rma = "select rma.rma_id, rma.RMA_reference as RMARef, date_format(RMA_Date, '%d/%m/%Y') as RMADate, rma.client_code as ClientCode, clients.ledger_name as ClientName,
             format(rma.Rmacharge_amount, 2) as RMAAmount, rma.Currency_code
			 from temp_rma as rma left join clients on clients.ledger_number = rma.client_code
			 where 1=1 and rma_type = 'MR'";
if($ref != ""){
	$query_rma = $query_rma." and RMA_reference = '$ref'";
}
if($client_code != ""){
	$query_rma = $query_rma." and rma.client_code = '$client_code'";
}
//echo $query_rma;
if($date != ""){
$date = explode("/", $date);
$date = $date[2]."-".$date[1]."-".$date[0];
$query_rma = $query_rma." and rma.RMA_Date between '$date' and '$date 23:59:59'";
}
$result_rma2 = mysql_query($query_rma, $connection);
$number_rma = mysql_num_rows($result_rma2);
$total_num_pages = ceil($number_rma/10);
$query_rma  = $query_rma." order by RMA_Date desc , RMA_Id desc LIMIT $start, 10";
$result_rma = mysql_query($query_rma, $connection);
$count = 0; 
while($row_rma = mysql_fetch_array($result_rma)){
	extract($row_rma);
	$count++;
	if($count %2 != 0){
		$background = "#e6e6e6";
	}
	else{
		$background = "#f7f7f7";
	}
	$query_pending  = "select count(*) as PendingRMA from temp_rma where rma_id = '$rma_id'";
	$result_pending = mysql_query($query_pending, $connection);
	$row_pending = mysql_fetch_array($result_pending);
	extract($row_pending);
	echo "<tr>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/delete.png' style='cursor: pointer' width=20 onclick=\"deleteRMA('$rma_id')\" /></td>
<td style='width: 15px; background: $background; padding: 5px'>";
echo"<img src='img/edit.png' style='cursor: pointer' width=20 onclick=\"editRMA('$rma_id')\" />";
echo"</td>
<td style='width: 15px; background: $background; padding: 5px'><img src='img/details.gif' style='cursor: pointer' width=15 id='show_$rma_id' onclick=\"displayDetails('$rma_id')\"/></td>
<td style='width: 60px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMARef</td>
<td style='width: 100px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$RMADate</td>
<td style='width: 590px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$ClientCode - $ClientName</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold '>$RMAAmount</td>
<td style='width: 156px; background: $background; padding: 5px; font-size: 13px; font-family: Arial; font-weight: bold'>$Currency_code</td>
</tr>";
/*
echo "<script>
$(document).ready(function(){
    $(\"#show_$rma_id\").click(function(){
        $(\"#details_$rma_id\").toggle(\"medium\");
    });
});
</script>";
*/
echo "<tr>
<th colspan=8 style='background: $background; min-height: 0px;'><div style='display: none; width: 900px; margin: auto; background: #ccffff; border: solid 1px #33ccff; padding: 5px;' id='details_$rma_id'>";

echo"</div></th>
</tr>";
}
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo "<tr>
<th colspan=9>
<div style='display: inline-block; text-align: right; width: 48%'>";
if($start > 0){
echo"<img src='img/arrowleft.png' style='cursor: pointer' onclick=\"movePrev()\" />";
}
//echo $pagenumber;
echo"</div>
<div style='display: inline-block; text-align: left; width: 48%'>";
if($pagenumber != $total_num_pages && $number_rma != 0){
echo"<img src='img/arrowright.png' style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</tr>";
echo"</table>";
}
function drawRMADtSearch($connection, $rma_id){
	$query_details  = "select rmadt.item_code as ItemCode, items.item_description as ItemName, rmadt.RMADt_Quantity as RMADtQuantity, if(rmadt.Outside_Item = 1, 'Yes', 'No') as Outside_Item, RMADT_PROBLEMCATEGORY as ProblemCategory, format(ifnull(rmadt.rmadt_price, 0), 2) as rmadt_price, format(ifnull(rmadt.rmadt_price2, 0), 2) as rmadt_price2
                    from rmadt left join items on items.item_code = rmadt.item_code
				    where rmadt.rma_id = '$rma_id'";
					//echo $query_details;
$result_details = mysql_query($query_details, $connection);
echo "<table width=100% style='border-collapse: collapse'>
<tr>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Item Code</th>
<th style='width: 450px; font-size: 12px; font-family: Arial;'>Item Description</th>
<th style='width: 50px; font-size: 12px; font-family: Arial;'>Quantity</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Outside Item</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Charge 1</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Charge 2</th>
<th style='width: 200px; font-size: 12px; font-family: Arial;'>Problem Category</th>
</tr>";
while($row_details = mysql_fetch_array($result_details)){
	extract($row_details);
	echo "<tr>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemCode</td>
<td style='width: 450px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemName</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$RMADtQuantity</td>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$Outside_Item</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$rmadt_price</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$rmadt_price2</td>
<td style='width: 200px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ProblemCategory</td>
</tr>";
}
echo"</table>";
}
function drawRMADtSearch2($connection, $rma_id){
	$query_details  = "select rmadt_id, rma_id, rmadt.item_code as ItemCode, items.item_description as ItemName, rmadt.RMADt_Quantity as RMADtQuantity, if(rmadt.Outside_Item = 1, 'Yes', 'No') as Outside_Item, RMADT_PROBLEMCATEGORY as ProblemCategory, format(ifnull(rmadt.rmadt_price, 0), 2) as rmadt_price, format(ifnull(rmadt.rmadt_price2, 0), 2) as rmadt_price2
                    from rmadt left join items on items.item_code = rmadt.item_code
				    where rmadt.rma_id = '$rma_id' and rmadt_id not in (select ifNull(MI_ID, 0) as MI_ID from rmadt)";
					//echo $query_details;
$result_details = mysql_query($query_details, $connection);
echo "<table width=100% style='border-collapse: collapse'>
<tr>
<th></th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Item Code</th>
<th style='width: 450px; font-size: 12px; font-family: Arial;'>Item Description</th>
<th style='width: 50px; font-size: 12px; font-family: Arial;'>Quantity</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Outside Item</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Charge 1</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Charge 2</th>
<th style='width: 200px; font-size: 12px; font-family: Arial;'>Problem Category</th>
</tr>";
$list_dt = "";
while($row_details = mysql_fetch_array($result_details)){
	extract($row_details);
	$list_dt = $list_dt."$rmadt_id,";
	echo "<tr>
	<td><input type='checkbox' id='chk_dt_$rmadt_id' checked onclick=\"chkDt('$rmadt_id', '$rma_id')\" /></td>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemCode</td>
<td style='width: 450px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemName</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$RMADtQuantity</td>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$Outside_Item</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$rmadt_price</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$rmadt_price2</td>
<td style='width: 200px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ProblemCategory</td>
</tr>";
}

echo"</table>";
}
function insertInArrayDetails($connection, $rma_id){
	$query_details  = "select rmadt_id, rma_id, rmadt.item_code as ItemCode, items.item_description as ItemName, rmadt.RMADt_Quantity as RMADtQuantity, if(rmadt.Outside_Item = 1, 'Yes', 'No') as Outside_Item, RMADT_PROBLEMCATEGORY as ProblemCategory, format(ifnull(rmadt.rmadt_price, 0), 2) as rmadt_price, format(ifnull(rmadt.rmadt_price2, 0), 2) as rmadt_price2
                    from rmadt left join items on items.item_code = rmadt.item_code
				    where rmadt.rma_id = '$rma_id' and rmadt_id not in (select ifNull(MI_ID, 0) as MI_ID from rmadt)";
					//echo $query_details;
    $result_details = mysql_query($query_details, $connection);
    while($row_details = mysql_fetch_array($result_details)){
	    extract($row_details);
	    $list_dt = $list_dt."$rmadt_id,";
    }
	echo "<input type='hidden' id='list_dt_$rma_id' value='$list_dt' />";
}
function drawPendingRMADtSearch($connection, $rma_id){
	$query_details  = "select rmadt.item_code as ItemCode, items.item_description as ItemName, rmadt.RMADt_Quantity as RMADtQuantity, if(rmadt.Outside_Item = 1, 'Yes', 'No') as Outside_Item, RMADT_PROBLEMCATEGORY as ProblemCategory
                    from temp_rmadt as rmadt left join items on items.item_code = rmadt.item_code
				    where rmadt.rma_id = '$rma_id'";
					//echo $query_details;
$result_details = mysql_query($query_details, $connection);
echo "<table width=100% style='border-collapse: collapse'>
<tr>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Item Code</th>
<th style='width: 450px; font-size: 12px; font-family: Arial;'>Item Description</th>
<th style='width: 50px; font-size: 12px; font-family: Arial;'>Quantity</th>
<th style='width: 100px; font-size: 12px; font-family: Arial;'>Outside Item</th>
<th style='width: 200px; font-size: 12px; font-family: Arial;'>Problem Category</th>
</tr>";
while($row_details = mysql_fetch_array($result_details)){
	extract($row_details);
	echo "<tr>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemCode</td>
<td style='width: 450px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ItemName</td>
<td style='width: 50px; font-size: 11px; font-family: Arial; text-align: right; color: #186194;'>$RMADtQuantity</td>
<td style='width: 100px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$Outside_Item</td>
<td style='width: 200px; font-size: 11px; font-family: Arial; color: #186194;'>&nbsp;$ProblemCategory</td>
</tr>";
}
echo"</table>";
}
function drawRMADtRPC($connection, $rma_id, $rmadt_id){
	echo "<table width=100%>
	<tr>
	<th width=150 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Item Code</th>
	<th width=395 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Item Description</th>
	<th width=170 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Serial</th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>WH</th>
	<th width=75 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Quantity</th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Price</th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'>Total Price</th>
	<th width=30 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'></th>
	<th width=30 style='font-family: Arial; padding: 2px; background: #016a84; color: #ffffff; font-size: 15px;'></th>
	</tr>";
	echo "<tr>
	<th width=150 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='item_code' style='width: 90px' onkeyup=\"moveNextField(event, '1'); searchItem(event)\" autofocus = 'autofocus' /> <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems(16)\"/></th>
	<th width=395 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='item_description' onkeyup=\"moveNextField(event, '2')\" style='width: 380px' /></th>
	<th width=170 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='serial_number' onkeyup=\"moveNextField(event, '3')\" style='width: 165px' /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='warehouse_code' onkeyup=\"moveNextField(event, '4'); searchWarehouse(event)\" style='width: 50px' />&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListWarehouse(12)\"/></th>
	<th width=75 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_quantity' onkeyup=\"moveNextField(event, '5'); searchItem(event)\" style='width: 60px' /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_price' onkeyup=\"moveNextField(event, '6')\" style='width: 60px' /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_totalprice' onkeyup=\"moveNextField(event, '7')\" style='width: 60px' readonly /></th>
	<th width=30 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'><img src='img/confirm-1.png' width=20 style='cursor: pointer;' onclick=\"insertRMADt2()\"/></th>
	<th width=30 style='font-family: Arial; padding: 2px; background: #e6e6e6; color: #000000; font-size: 15px;'></th>
	</tr>";
	$query_rmadtrpc = "select rmadtrpc_id, rmadtrpc.item_code, items.Item_Description, rmadtrpc.serial_number, rmadtrpc.warehouse_code, rmadtrpc_quantity, rmadtrpc_price, (rmadtrpc_quantity * rmadtrpc_price) as rmadtrpc_totalprice
	                   from temp_rmadtrpc as rmadtrpc inner join items on items.item_code = rmadtrpc.item_code
					   where rmadt_id = '$rmadt_id'
					   order by rmadtrpc_id desc";
	//echo $query_rmadtrpc;
	$result_rmadtrpc = mysql_query($query_rmadtrpc, $connection);
	$count = 1;
	while($row_rmadtrpc = mysql_fetch_array($result_rmadtrpc)){
		extract($row_rmadtrpc);
		$count++;
		if($count %2 != 0){
			$color = "#e6e6e6";
		}
		else{
			$color = "#f7f7f7";
		}
		echo "<tr>
	<th width=150 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='item_code_$rmadtrpc_id' style='width: 90px' value='$item_code' onkeyup=\"moveNextField2(event, '1', '$rmadtrpc_id'); searchItem2(event, '$rmadtrpc_id')\" /> <img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListItems2(17, '$rmadtrpc_id')\" value=''/></th>
	<th width=395 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='item_description_$rmadtrpc_id' style='width: 380px' value='$Item_Description' onkeyup=\"moveNextField2(event, '2', '$rmadtrpc_id')\" /></th>
	<th width=170 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='serial_number_$rmadtrpc_id' style='width: 165px' value='$serial_number' onkeyup=\"moveNextField2(event, '3', '$rmadtrpc_id')\" /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='warehouse_code_$rmadtrpc_id' style='width: 50px' value='$warehouse_code' onkeyup=\"moveNextField2(event, '4', '$rmadtrpc_id'); ; searchWarehouse2(event, '$rmadtrpc_id')\" />&nbsp;&nbsp;<img src='img/openlist.png' width=20 style='cursor: pointer; vertical-align: middle' onclick=\"openListWarehouse2(13, '$rmadtrpc_id')\"/></th>
	<th width=75 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_quantity_$rmadtrpc_id' style='width: 60px' value='$rmadtrpc_quantity' onkeyup=\"moveNextField2(event, '5', '$rmadtrpc_id'); searchItem2(event, '$rmadtrpc_id')\" /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_price_$rmadtrpc_id' style='width: 60px' value='$rmadtrpc_price' onkeyup=\"moveNextField2(event, '6', '$rmadtrpc_id')\" /></th>
	<th width=120 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><input type='text' id='rmadtrpc_totalprice_$rmadtrpc_id' style='width: 60px' value='$rmadtrpc_totalprice' onkeyup=\"moveNextField2(event, '7', '$rmadtrpc_id')\" readonly /></th>
	<th width=30 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><img src='img/confirm-1.png' height=20 style='cursor: pointer;' onclick=\"updateRMADt2('$rmadtrpc_id')\"/></th>
	<th width=30 style='font-family: Arial; padding: 2px; background: $color; color: #000000; font-size: 15px;'><img src='img/delete2.png' height=20 style='cursor: pointer;' onclick=\"deleteRMADt('$rmadtrpc_id')\"/></th>
	</tr>";
	}
	echo"</table>";
}
function drawListProblemCategory($connection, $searchproblem, $start, $pagenumber, $type){
	include('config.inc.php');
	if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
	echo"<table width=700>";
$query_problemcategory = "select PROBLEMCATEGORY as ProblemId, PROBLEMCATEGORY_NAME as ProblemDesc, user_code as UserCode
                          from problemcategory
			              where 1=1 and PROBLEMCATEGORY_TYPE = '$type'";
if($searchproblem != ""){
	$query_problemcategory = $query_problemcategory." and PROBLEMCATEGORY_NAME like '%$searchproblem%'";
}
$result_problemcategory2 = mysql_query($query_problemcategory, $connection);
$num_problemcategory = mysql_num_rows($result_problemcategory2);
$query_problemcategory  = $query_problemcategory." LIMIT $start, ".$config['num_rows_per_page'];
$result_problemcategory = mysql_query($query_problemcategory, $connection);
$count = 0;
//echo $query_problemcategory;
$total_num_pages = ceil($num_problemcategory/$config['num_rows_per_page']);
while($row_problemcategory = mysql_fetch_array($result_problemcategory)){
	extract($row_problemcategory);
	$count++;
    if($count %2 != 0){
		$background = "background-color: #e6e6e6;";
	}
	else{
		$background = "background-color: #f7f7f7;";
	}
	echo "<tr>";
	if($type!='E' && $type!='P'){
	echo"<th style='text-align: left; font-size: 14px; font-family: Arial; $background padding: 6px; text-decoration: underline; color: #0000ee; cursor: pointer; width: 150px'>";
	
	echo"<input type='checkbox' id='$ProblemId' onclick=\"fillProblem('$ProblemId', '$ProblemDesc')\" />";
	echo"</th>";
	$colspan=1;
	}
	else{
		$colspan=2;
	}
	echo"<th colspan=$colspan style='text-align: left; font-size: 14px; font-family: Arial; $background padding: 6px; text-decoration: underline; color: #0000ee; cursor: pointer' onclick=\"selectProblem('$ProblemDesc', '$UserCode')\">$ProblemDesc</th>
	</tr>";
}
echo "<tr>
<th colspan=2>
<div style='width: 48%; text-align: right; display: inline-block'>";
if($start != 0){
echo"<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
}
echo"</div>
<div style='width: 48%; text-align: left; display: inline-block'>";
if($total_num_pages != $pagenumber && $num_problemcategory != 0){
echo"<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</th>
</tr>";
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo"</table>";
}
function drawListSerialNo($connection, $searchserial, $start, $pagenumber){
	include('config.inc.php');
	if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
	$query_module  = "select module_delivery from module";
    $result_module = mysql_query($query_module, $connection); 
	$row_module    = mysql_fetch_array($result_module);
	extract($row_module);
	if($module_delivery == 1){
		$query_serial = "select SerialNo_Code, PurchaseDt_ID, sales.sale_reference as InvRef
                         from purchasesserialno inner join salesdt on salesdt.saledt_id = purchasesserialno.PurchaseDt_ID
                         inner join sales on sales.sale_id = salesdt.sale_id
                         where purchasesserialno.journal_type = 'SI'";
		if($searchserial != ""){
		$query_serial = $query_serial." and SerialNo_Code like '%$searchserial%'";
		}
$query_serial = $query_serial." order by PurchaseDt_ID desc";
	}
	else{
		$query_serial = "select SerialNo_Code, PurchaseDt_ID, deliveries.delivery_reference as InvRef
                         from purchasesserialno inner join deliverydt on deliverydt.deliverydt_id = purchasesserialno.PurchaseDt_ID
                         inner join deliveries on deliveries.delivery_id = deliverydt.delivery_id
                         where purchasesserialno.journal_type = 'DI'";
		if($searchserial != ""){
		$query_serial = $query_serial." and SerialNo_Code like '%$searchserial%'";
		}
$query_serial = $query_serial." order by PurchaseDt_ID desc";
	}
	echo"<table width=700>";
$result_serial2 = mysql_query($query_serial, $connection);
$num_serial = mysql_num_rows($result_serial2);
$query_serial  = $query_serial." LIMIT $start, ".$config['num_rows_per_page'];
//echo $query_serial;
$result_serial = mysql_query($query_serial, $connection);
$count = 0;
//echo $query_problemcategory;
$total_num_pages = ceil($num_serial/$config['num_rows_per_page']);
while($row_serial = mysql_fetch_array($result_serial)){
	extract($row_serial);
	$count++;
    if($count %2 != 0){
		$background = "background-color: #e6e6e6;";
	}
	else{
		$background = "background-color: #f7f7f7;";
	}
	echo "<tr>
	<th width=50%  style='text-align: left; font-size: 14px; font-family: Arial; $background padding: 6px; text-decoration: underline; color: #0000ee; cursor: pointer' onclick=\"selectSerial('$SerialNo_Code')\">$SerialNo_Code</th>
	<th width=50%  style='text-align: left; font-size: 14px; font-family: Arial; $background padding: 6px; text-decoration: underline; color: #0000ee; cursor: pointer' onclick=\"selectSerial('$SerialNo_Code')\">$InvRef</th>
	</tr>";
}
echo "<tr>
<th colspan=2>
<div style='width: 48%; text-align: right; display: inline-block'>";
if($start != 0){
echo"<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
}
echo"</div>
<div style='width: 48%; text-align: left; display: inline-block'>";
if($total_num_pages != $pagenumber && $num_serial != 0){
echo"<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
}
echo"</div>
</th>
</tr>";
echo "<input type='hidden' id='start' value='$start' />";
echo "<input type='hidden' id='pagenumber' value='$pagenumber' />";
echo"</table>";
}
function drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box){
	
	echo "<div style='height: 500px; overflow-y: scroll; overflow-x: scoll; width: 1106px; margin-left: 43px'>";
	echo "<table style='width: 4100px; border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; background: #d8d8d8; width: 20px; border: solid 1px #000000; padding: 7px'></th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>Return RMA Ref.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>RMA Ref.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; text-align: left; border: solid 1px #000000; padding: 7px'>Status</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; border: solid 1px #000000; padding: 7px'>RMA Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 250px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 250px; text-align: left; border: solid 1px #000000; padding: 7px'>Remark</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px; text-align: left; border: solid 1px #000000; padding: 7px'>Engineer Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Part No.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 250px; text-align: left; border: solid 1px #000000; padding: 7px'>Description</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Serial No.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 75px; border: solid 1px #000000; padding: 7px' >Repair</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px; text-align: left; border: solid 1px #000000; padding: 7px'>Remark</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px;text-align: left; border: solid 1px #000000; padding: 7px'>Invoice Number</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; border: solid 1px #000000; padding: 7px'>Invoice Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 50px;text-align: right;border: solid 1px #000000; padding: 7px'>Quantity</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 120px; text-align: right; border: solid 1px #000000; padding: 7px'>Price</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 50px; text-align: left; border: solid 1px #000000; padding: 7px'>Warranty</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Notes</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Problem</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Solution</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; border: solid 1px #000000; padding: 7px'>Date Received</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; border: solid 1px #000000'; padding: 7px>Date Delivered</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>User Delivered</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>User Received</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>Transfer No.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 127px; border: solid 1px #000000; padding: 7px'>Return RMA Date Received</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 127px; text-align: left; border: solid 1px #000000; padding: 7px'>Return RMA User Received</th>
	</tr>";
	$query_isengineer = "select user_engineer from ".$_SESSION['dbLabel'].".users where user_account = '".$_SESSION['useraccount']."'";
	$result_isengineer = mysql_query($query_isengineer, $connection);
	$row_isengineer = mysql_fetch_array($result_isengineer);
	extract();
	$query_pending = "SELECT RmaDTRet.rmadt_id,
       RmaRet.rma_reference AS RMA_Ret_Ref,
       Rma.rma_reference AS RMA_Ref,
       RmaDTRet.rmadt_status,
       date_format(RmaDTRet.rma_date, '%d/%m/%Y %H:%i:%s') as rma_date,
       RmaDTRet.Client_Code,
       clients.ledger_name,
       RmaDTRet.project_code,
       RmaDTRet.costcent_code,
       items.item_code,
       item_description,
       RmaDTRet.RMADt_Repaire,
       RmaDTRet.rmadt_remark,
       RmaDTRet.Invoice_Number,
       date_format(RmaDTRet.Invoice_Date, '%d/%m/%Y %H:%i:%s') as Invoice_Date,
       RmaDTRet.Serial_Number,
       RmaDTRet.RMADt_Quantity,
       if(RmaDTRet.Rmadt_Warranty = 1, 'True', 'False') as Rmadt_Warranty,
       format(RmaDTRet.rmadt_price, 2) as rmadt_price,
       RmaDTRet.rmadt_notes,
       RmaDTRet.rmadt_problem,
       RmaDTRet.rmadt_solution,
       RmaDTRet.serial_numberRep,
       RmaDTRet.item_codeRep,
       date_format(RmaDTRet.rmadt_DateReceived, '%d/%m/%Y %H:%i:%s') as rmadt_DateReceived,
       date_format(RmaDTRet.rmadt_DateDelivered, '%d/%m/%Y %H:%i:%s') as rmadt_DateDelivered,
       RmaDTRet.rmadt_UserRecieved,
       RmaDTRet.rmadt_UserDelivered,
       RmaDTRet.rmadt_transferno,
       date_format(RmaDTRet.rmadt_RetRMADateRecieved, '%d/%m/%Y %H:%i:%s') as rmadt_RetRMADateRecieved,
       RmaDTRet.rmadt_RetRMAUserRecieved,
       rma.rma_remark,
       problemcategory.PROBLEMCATEGORY_NAME as Rma_EngineerName, rma.rma_id
FROM rmadt AS RmaDTRet,
     rma AS RmaRet,
     items,
     clients,
     RmaDT,
     rma left join problemcategory on problemcategory.user_code = rma.Rma_EngineerName
WHERE RmaDTRet.rma_id=RmaRet.rma_id
  AND RmaDT.rma_id=Rma.rma_id
  AND RmaDT.rmadt_id=RmadtRet.MI_ID
  AND RmaDTRet.rma_type='MR'
  AND ((RmaDTRet.RMADt_Repaire ='C'
        AND RmaDTRet.rmadt_status IS NOT NULL
        AND RmaDTRet.rmadt_status<>'')
       OR (RmaDTRet.RMADt_Repaire ='P')
       OR (RmaDTRet.RMADt_Repaire ='X'))
  AND RmaDTRet.item_code=items.item_code
  AND clients.ledger_number=RmaDTRet.client_code";
  if($filter_by == "not_received"){
	  $query_pending = $query_pending." and RmaDTRet.rmadt_DateReceived is null";
  }
  else
	  if($filter_by == "not_delivered"){
		  $query_pending = $query_pending." and RmaDTRet.rmadt_DateDelivered is null";
	  }
	  else
		  if($filter_by == "not_delivered_not_received"){
			  $query_pending = $query_pending." and RmaDTRet.rmadt_DateDelivered is null and RmaDTRet.rmadt_DateReceived is null";
		  }
		  
		  else{
			  $query_pending = $query_pending." ";
		  }
		  
		  if($search_type == "return_rma_ref"){
			  $query_pending = $query_pending." and RmaRet.rma_reference = '$search_box'";
		  }
		  else
			  if($search_type == "rma_ref"){
				  $query_pending = $query_pending." and Rma.rma_reference = '$search_box'";
			  }
			  else
			    if($search_type == "status"){
				  $query_pending = $query_pending." and RmaDTRet.rmadt_status = '$search_box'";
			  }
			  else
			    if($search_type == "rma_date"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.rma_date between '$search_box' and '$search_box 23:59:59'";
			  }
			  else
			    if($search_type == "client_code"){
				  $query_pending = $query_pending." and RmaDTRet.Client_Code like '%$search_box%'";
			  }
			  else
			    if($search_type == "client_name"){
				  $query_pending = $query_pending." and clients.ledger_name like '%$search_box%'";
			  }
			  else
			    if($search_type == "engineer_name"){
				  $query_pending = $query_pending." and problemcategory.PROBLEMCATEGORY_NAME like '%$search_box%'";
			  }
			  else
			    if($search_type == "part_no"){
				  $query_pending = $query_pending." and items.item_code like '%$search_box%'";
			  }
			  else
			    if($search_type == "description"){
				  $query_pending = $query_pending." and item_description like '%$search_box%'";
			  }
			  else
			    if($search_type == "serial_no"){
				  $query_pending = $query_pending." and RmaDTRet.Serial_Number like '%$search_box%'";
			  }
			  else
			    if($search_type == "repair"){
				  $query_pending = $query_pending." and RmaDTRet.RMADt_Repaire = '$search_box'";
			  }
			  else
			    if($search_type == "invoice_number"){
				  $query_pending = $query_pending." and RmaDTRet.Invoice_Number = '$search_box'";
			  }
			  else
			    if($search_type == "invoice_date"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.Invoice_Date between '$search_box' and '$search_box 23:59:59'";
			  }
			  else
			    if($search_type == "date_received"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.rmadt_DateReceived between '$search_box' and '$search_box 23:59:59'";
			  }
			  else
			    if($search_type == "date_delivered"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.rmadt_DateDelivered between '$search_box' and '$search_box 23:59:59'";
			  }
			  else
			    if($search_type == "transfer_no"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.rmadt_transferno between '$search_box' and '$search_box 23:59:59'";
			  }
			  else
			    if($search_type == "return_rma_date_received"){
				  $search_box = explode("/", $search_box);
				  $search_box = $search_box[2]."-".$search_box[1]."-".$search_box[0];
				  $query_pending = $query_pending." and RmaDTRet.rmadt_RetRMADateRecieved between '$search_box' and '$search_box 23:59:59'";
			  }
		 // echo $query_pending;
		  $query_pending = $query_pending." order by rma_date desc ";
    if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
    $result_pending2 = mysql_query($query_pending, $connection);
	$number_pending = mysql_num_rows($result_pending2);
	$query_pending = $query_pending." limit $start, 20";
	$totalpages = ceil($number_pending / 20);
	
    $result_pending = mysql_query($query_pending, $connection);
	$count = 0;
	while($row_pending = mysql_fetch_array($result_pending)){
		extract($row_pending);
		$count++;
		if($count %2 != 0){
			$background = "#e6e6e6";
		}
		else{
			$background = "#f7f7f7";
		}
		echo"<tr>
	<td style='font-size: 13px; background: $background; width: 20px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='chk_rmadt_id' id='chk_rmadt_id_$rmadt_id' value='$rmadt_id' style='cursor: pointer' onclick=\"changeValue('$rmadt_id', '$rma_id'); displayDate('$rmadt_id');\" /></td>
	<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ret_Ref</td>
	<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
	<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$Status</td>
	<td style='font-size: 14px; background: $background; width: 150px; text-align: center; border: solid 1px #000000; padding: 7px'>$rma_date</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$Client_Code</td>
	<td style='font-size: 13px; background: $background; width: 250px; border: solid 1px #000000; padding: 7px'>$ledger_name</td>
	<td style='font-size: 13px; background: $background; width: 250px; border: solid 1px #000000; padding: 7px'>$rma_remark</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$Rma_EngineerName</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$item_code</td>
	<td style='font-size: 13px; background: $background; width: 250px; border: solid 1px #000000; padding: 7px'>$item_description</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
	<td style='font-size: 13px; background: $background; width: 75px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMADt_Repaire</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$rmadt_remark</td>
	<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Invoice_Number</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$Invoice_Date</td>
	<td style='font-size: 13px; background: $background; width: 50px; text-align: right; border: solid 1px #000000; padding: 7px'>$RMADt_Quantity</td>
	<td style='font-size: 13px; background: $background; width: 120px; text-align: right; border: solid 1px #000000; padding: 7px'>$rmadt_price</td>
	<td style='font-size: 13px; background: $background; width: 50px; border: solid 1px #000000; padding: 7px'>$Rmadt_Warranty</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$RmaDtNotes</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$rmadt_problem</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$rmadt_solution</td>
	<td style='font-size: 13px; background: $background; width: 150px; text-align: center; border: solid 1px #000000; padding: 7px'>$rmadt_DateReceived</td>
	<td style='font-size: 13px; background: $background; width: 150px; text-align: center; border: solid 1px #000000; padding: 7px'>$rmadt_DateDelivered</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$rmadt_UserDelivered</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$rmadt_UserRecieved</td>
	<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$rmadt_transferno</td>
	<td style='font-size: 13px; background: $background; width: 127px; text-align: center; border: solid 1px #000000; padding: 7px'>$rmadt_RetRMADateRecieved</td>
	<td style='font-size: 13px; background: $background; width: 127px; border: solid 1px #000000; padding: 7px'>$rmadt_RetRMAUserRecieved</td>
	</tr>";
	}
	echo"</table>";
	echo "</div>";
	echo "<div style='text-align: center; margin-top: 10px'>";
	if($start != 0){
	echo "<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
	}
	if($pagenumber != $totalpages && $number_pending != 0){
	echo "<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
	}
	echo "</div>";
	echo "<input type='hidden' name='start_par' id='start_par' value='$start' />";
	echo "<input type='hidden' name='pagenumber_par' id='pagenumber_par' value='$pagenumber' />";
}

function drawRMALocation($connection, $search_type, $search_location, $start, $pagenumber, $from_date, $to_date, $curr_rmadt_id, $search_status, $engineerchecked, $qcchecked, $operatorchecked){
	include('config.inc.php');
	
	//echo $search_type; exit;
	//echo $search_location;  exit;
	//echo $start; exit;
	//echo $pagenumber; exit;
	//echo $from_date; exit;
	//echo $to_date; exit;
	//echo $search_status;exit;
	//echo $engineerchecked;exit;
	//echo $qcchecked;exit;
	//echo $operatorchecked;exit;
	//echo "eng:".$engineerchecked." ,qc:".$qcchecked." ,op:".$operatorchecked." ,ss:".$search_status;
	$query_isengineer  = "select ifNull(user_engineer, 0) as user_engineer from ".$_SESSION['dbLabel'].".users where User_account='".$_SESSION['useraccount']."'";
	//echo $query_isengineer;
	//echo $engineerchecked;
	$result_isengineer = mysql_query($query_isengineer, $connection);
    $row_isengineer    = mysql_fetch_array($result_isengineer);
    extract($row_isengineer);
	//echo $user_engineer;exit;
	
	$query_check  = "select rmadt_techquotation, date_format(ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as ramdt_techquotationdate, date_format(rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as checkStartDate, date_format(rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as checkEndDate, date_format(rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as checkStartDate2, date_format(rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as checkEndDate2, rmadt_techquotation2, date_format(rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2   
						                 from rmadt 
										 where rmadt_id = '$rmadt_id'";
	$result_check = mysql_query($query_check, $connection);
	$row_check    = mysql_fetch_array($result_check);
	extract($row_check);
	
	echo "<div style='height: 500px; overflow-y: scroll; overflow-x: scoll; width: 1106px; margin-left: 43px'>";
	echo "<input type='hidden' id='rmadt_id' value='' />";
	echo "<form action='Form_ListRMALocation.php' method='post'>";
	echo "<table style=' border-collapse: collapse'>";
	echo"<tr>";
	if($user_engineer==1){
		
		if($engineerchecked==0 && $search_status==0)
			echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
		else 
			echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'></th>";
		echo "	<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>SN</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Ref</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Status</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Group</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Item Code</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>IMEI</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Serial Number</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Client Name</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Notes</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Date</th>";
	}
	/*if ($user_engineer==1){
		//if($checkEndDate != ""){
			//echo "HELLO";exit;
			if($engineerchecked==0 && $search_status==0)
				echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
			else 
				echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'></th>";
			
			echo "	<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>SN</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Ref</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Group</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Item Code</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>IMEI</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Serial Number</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Client Name</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Status</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Notes</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Date</th>";
		//}
	}*/
	else{
		if ($user_engineer==3){
			if($qcchecked == 0)
				echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
			else 
				echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'></th>";
			
			echo "	<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>SN</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Ref</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Group</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Item Code</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>IMEI</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Serial Number</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Client Name</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Status</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Notes</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Date</th>";
				
		}else{
		/*if($user_engineer==3)
			echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
		else
			echo"<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'></th>";
		*/
			/*echo"<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'></th>";
			echo "<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>RMA Ref.</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Expected Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; border: solid 1px #000000; padding: 7px'>Received Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; border: solid 1px #000000; padding: 7px'>Checked Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Location</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Received Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 170px; text-align: left; border: solid 1px #000000; padding: 7px'>Project</th>";
		*/
			if ($user_engineer==2){
				/*if($operatorchecked==0)
					echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
				else */
					echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'></th>";
				
				echo "	<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>SN</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Ref</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Group</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Item Code</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>IMEI</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Serial Number</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Client Name</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Problem</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Quotation</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Status</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>On Hand</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>Notes</th>
				<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>RMA Date</th>";
			}
		}
	}
	if($user_engineer == 0 /*|| $user_engineer == 2*/){
		echo"<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'></th>";
			echo "<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>RMA Ref.</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Expected Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; border: solid 1px #000000; padding: 7px'>Received Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 153px; border: solid 1px #000000; padding: 7px'>Checked Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Location</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Received Date</th>
			<th style='font-size: 14px; background: #d8d8d8; width: 170px; text-align: left; border: solid 1px #000000; padding: 7px'>Project</th>";
		echo"<th style='font-size: 14px; background: #d8d8d8; width: 231px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Code</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Name</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Telephone</th>
		<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Client E-mail</th>";
	}
	if($user_engineer != 1 && $user_engineer !=3 && $user_engineer !=2){
		echo"<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Remark</th>";
	
	echo"<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Engineer Name</th>";
	echo"<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>User Onhand</th>";
	echo"<th style='font-size: 14px; background: #d8d8d8; width: 100px; text-align: left; border: solid 1px #000000; padding: 7px'>Repair</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px' >Status</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Item Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Item Description</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Serial Number</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Approval Status</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Spare Parts</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Quotation</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Quotation Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Eng. Start Date and Time</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Eng. End Date and Time</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC Start Date and Time</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC End Date and Time</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC Duration</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px;text-align: left; border: solid 1px #000000; padding: 7px'>Quotation (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px;text-align: left; border: solid 1px #000000; padding: 7px'>Quotation Date (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Eng. Start Date and Time (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Eng. End Date and Time (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC Start Date and Time (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC End Date and Time (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC Duration (2nd Process)</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>QC Remarks</th>";
	
	}
	echo"</tr>";
	$query_pending = "SELECT *, problemcategory.PROBLEMCATEGORY_NAME as EngName from (
select 'Service Center' as Service_Center,date_format(rmadt.rmadt_expectedDate,'%d/%m/%Y') as Expected_Date,date_format(rmadt.rmadt_DateReceived,'%d/%m/%Y')  as Received_Date,rma_reference as RMA_Ref,
rma.client_code as Client_Code,clients.ledger_name as Client_Name,
if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) as Repair,
if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales ,  Repair','Pending'))) as Status,
items.item_code as Item_Code,items.item_description as Item_Description,rma.project_code
,rma.rma_remark ,rma.Rma_EngineerName, rma.rma_date, rmadt.Serial_Number, rmadt.rmadt_id, date_format(rmadt.rmadt_techcheckupdatedate, '%d/%m/%Y %H:%i:%s') as CheckUpdateDate, rmadt.rmadt_techquotation as QuotationText, date_format(rmadt.ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as QuotationDate, date_format(rmadt.rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as StartDate, date_format(rmadt.rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as EndDate, clients.Client_Telephone1 as ClientTelephone, clients.client_Email as ClientEmail, if(rmadt.rmadt_approved = 1, concat('Approved at ', date_format(rmadt.rmadt_approveddate, '%d/%m/%Y %H:%i:%s')), if(rmadt.rmadt_approved = 0, 'Refused', 'Waiting for reply')) as ApprovalStatus, rmadt.rmadt_operatorSpareParts as SpareParts, rmadt.rmadt_qcremark as QCRemarks, rmadt.rmadt_qcduration as QCDuration, date_format(rmadt.rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repStartDateTime2, date_format(rmadt.rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repEndDateTime2, date_format(rmadt.rmadt_qcStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime2, date_format(rmadt.rmadt_qcEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime2, rmadt.rmadt_qcduration2, date_format(rmadt.rmadt_qcStartDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime, date_format(rmadt.rmadt_qcEndDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime, date_format(rmadt.rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2, rmadt.rmadt_techquotation2, date_format(rmadt.rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, items.Group_Code, rmadt.user_onhand, rmadt.rmadt_qcapproved, rmadt.rmadt_notes, rmadt.rmadt_problem 
   from rma,rmadt,items,clients
where rma.rma_id=rmadt.rma_id
  and rma.client_code=clients.ledger_number
  and rmadt.item_code=items.item_code
  and rma.rma_type='MI'
  and rmadt.rmadt_DateReceived is not null
  and rmadt.rmadt_id not in (select mi_id from rmadt as rmadtret where rma_type='MR' and rmadtret.mi_id=rmadt.rmadt_id)";
  if($user_engineer == 1 || $user_engineer == -1){
  $query_pending = $query_pending." and rma.Rma_EngineerName = '".$_SESSION['useraccount']."'";
  }
  else
	  if($user_engineer == 2){
		  $query_pending = $query_pending." and rmadt.ramdt_techquotationdate is not null";
	  }
	  else
		  if($user_engineer == 3){
			  $query_pending = $query_pending." and rmadt.rmadt_approved = 1 and rmadt.rmadt_repStartDateTime is not null and rmadt.rmadt_repEndDateTime is not null and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
		  }
  if($search_status != ""){
	   if($search_status == 2){
		   $query_pending = $query_pending." and rmadt.rmadt_qcapproved = '0'";
	   }
	   else
	   if($search_status == -1){
		   $query_pending = $query_pending." ";
	   }
	   else{
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '$search_status' and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
	   }
  }
  if($search_status == "" && $user_engineer == 1){
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '0'";
  }
$query_pending = $query_pending." union
select 'Service Center' as Service_Center,date_format(rmadt.rmadt_expectedDate,'%d/%m/%Y') as Expected_Date,date_format(rma.rma_Date,'%d/%m/%Y') as Received_Date,rmama.rma_reference as RMA_Ref,
   rma.client_code as Client_Code,clients.ledger_name as Client_Name,
  if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) as Repair,
  if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales ,  Repair','Pending'))) as Status,
  items.item_code as Item_Code,items.item_description as Item_Description,rma.project_code
,rma.rma_remark ,rma.Rma_EngineerName, rma.rma_date, rmadt.Serial_Number, rmadt.rmadt_id, date_format(rmadt.rmadt_techcheckupdatedate, '%d/%m/%Y %H:%i:%s') as CheckUpdateDate, rmadt.rmadt_techquotation as QuotationText, date_format(rmadt.ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as QuotationDate, date_format(rmadt.rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as StartDate, date_format(rmadt.rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as EndDate, clients.Client_Telephone1 as ClientTelephone, clients.client_Email as ClientEmail, if(rmadt.rmadt_approved = 1, concat('Approved at ', date_format(rmadt.rmadt_approveddate, '%d/%m/%Y %H:%i:%s')), if(rmadt.rmadt_approved = 0, 'Refused', 'Waiting for reply')) as ApprovalStatus, rmadt.rmadt_operatorSpareParts as SpareParts, rmadt.rmadt_qcremark as QCRemarks, rmadt.rmadt_qcduration as QCDuration, date_format(rmadt.rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repStartDateTime2, date_format(rmadt.rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repEndDateTime2, date_format(rmadt.rmadt_qcStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime2, date_format(rmadt.rmadt_qcEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime2, rmadt.rmadt_qcduration2, date_format(rmadt.rmadt_qcStartDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime, date_format(rmadt.rmadt_qcEndDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime, date_format(rmadt.rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2, rmadt.rmadt_techquotation2, date_format(rmadt.rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, items.Group_Code, rmadt.user_onhand, rmadt.rmadt_qcapproved, rmadt.rmadt_notes, rmadt.rmadt_problem 
   from rma,rmadt,items,clients,rma as rmaMA,rmadt as rmaMAdt
where rma.rma_id=rmadt.rma_id
  and rmaMa.rma_id=rmaMadt.rma_id
  and rmaMAdt.rmadt_id=rmadt.mi_id
  and rma.client_code=clients.ledger_number
  and rmadt.item_code=items.item_code
  and rmadt.rmadt_status is null
  and rmadt.rmadt_RetRMADateRecieved is null
  AND RMADT.rmadt_DateReceived IS NULL
  and rma.rma_type='MR'";
  if($user_engineer == 1 || $user_engineer == -1){
  $query_pending = $query_pending." and rmaMA.Rma_EngineerName = '".$_SESSION['useraccount']."'";
  }
   else
	  if($user_engineer == 2){
		  $query_pending = $query_pending." and rmadt.ramdt_techquotationdate is not null";
	  }
	  else
		  if($user_engineer == 3){
			  $query_pending = $query_pending." and rmadt.rmadt_approved = 1 and rmadt.rmadt_repStartDateTime is not null and rmadt.rmadt_repEndDateTime is not null and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
		  }
   if($search_status != ""){
	   if($search_status == 2){
		   $query_pending = $query_pending." and rmadt.rmadt_qcapproved = '0'";
	   }
	   else
	   if($search_status == -1){
		   $query_pending = $query_pending." ";
	   }
	   else{
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '$search_status' and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
	   }
  }
  
   if($search_status == "" && $user_engineer == 1){
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '0'";
  }
$query_pending = $query_pending." union

select 'Warehouse' as Service_Center,date_format(rmadt.rmadt_expectedDate,'%d/%m/%Y') as Expected_Date,date_format(rmadt.rmadt_RetRMADateRecieved ,'%d/%m/%Y') as Received_Date,rmama.rma_reference as RMA_Ref,
rma.client_code as Client_Code,clients.ledger_name as Client_Name,
if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) as Repair,
if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales ,  Repair','Pending'))) as Status,
   items.item_code as Item_Code,items.item_description as Item_Description,rma.project_code
,rma.rma_remark ,rma.Rma_EngineerName, rma.rma_date, rmadt.Serial_Number, rmadt.rmadt_id, date_format(rmadt.rmadt_techcheckupdatedate, '%d/%m/%Y %H:%i:%s') as CheckUpdateDate, rmadt.rmadt_techquotation as QuotationText, date_format(rmadt.ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as QuotationDate, date_format(rmadt.rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as StartDate, date_format(rmadt.rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as EndDate, clients.Client_Telephone1 as ClientTelephone, clients.client_Email as ClientEmail, if(rmadt.rmadt_approved = 1, concat('Approved at ', date_format(rmadt.rmadt_approveddate, '%d/%m/%Y %H:%i:%s')), if(rmadt.rmadt_approved = 0, 'Refused', 'Waiting for reply')) as ApprovalStatus, rmadt.rmadt_operatorSpareParts as SpareParts, rmadt.rmadt_qcremark as QCRemarks, rmadt.rmadt_qcduration as QCDuration, date_format(rmadt.rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repStartDateTime2, date_format(rmadt.rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repEndDateTime2, date_format(rmadt.rmadt_qcStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime2, date_format(rmadt.rmadt_qcEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime2, rmadt.rmadt_qcduration2, date_format(rmadt.rmadt_qcStartDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime, date_format(rmadt.rmadt_qcEndDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime, date_format(rmadt.rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2, rmadt.rmadt_techquotation2, date_format(rmadt.rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, items.Group_Code, rmadt.user_onhand, rmadt.rmadt_qcapproved, rmadt.rmadt_notes, rmadt.rmadt_problem 
   from rma,rmadt,items,clients,rma as rmaMA,rmadt as rmaMAdt
where rma.rma_id=rmadt.rma_id
  and rmaMa.rma_id=rmaMadt.rma_id
  and rmaMAdt.rmadt_id=rmadt.mi_id
  and rma.client_code=clients.ledger_number
  and rmadt.item_code=items.item_code
  and (rmadt.rmadt_RetRMADateRecieved is not null
  or rmadt.rmadt_status is not null)
  and rmadt.rmadt_DateReceived is  null
  and rma.rma_type='MR'";
  if($user_engineer == 1 || $user_engineer == -1){
  $query_pending = $query_pending." and rmaMA.Rma_EngineerName = '".$_SESSION['useraccount']."'";
  }
   else
	  if($user_engineer == 2){
		  $query_pending = $query_pending." and rmadt.ramdt_techquotationdate is not null";
	  }
	  else
		  if($user_engineer == 3){
			  $query_pending = $query_pending." and rmadt.rmadt_approved = 1 and rmadt.rmadt_repStartDateTime is not null and rmadt.rmadt_repEndDateTime is not null and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
		  }
   if($search_status != ""){
	   if($search_status == 2){
		   $query_pending = $query_pending." and rmadt.rmadt_qcapproved = '0'";
	   }
	   else
	   if($search_status == -1){
		   $query_pending = $query_pending." ";
	   }
	   else{
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '$search_status' and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
	   }
  }
   if($search_status == "" && $user_engineer == 1){
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '0'";
  }
$query_pending = $query_pending." union

select 'Reception' as Service_Center,date_format(rmadt.rmadt_expectedDate,'%d/%m/%Y') as Expected_Date,date_format(ifnull(rmadt.rmadt_DateReceived,rmama.rma_date),'%d/%m/%Y') as Received_Date,rmama.rma_reference as RMA_Ref,
rma.client_code as Client_Code,clients.ledger_name as Client_Name,
if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) as Repair,
if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales ,  Repair','Pending'))) as Status,
items.item_code as Item_Code,items.item_description as Item_Description,rma.project_code
,rma.rma_remark ,rma.Rma_EngineerName, rma.rma_date, rmadt.Serial_Number, rmadt.rmadt_id, date_format(rmadt.rmadt_techcheckupdatedate, '%d/%m/%Y %H:%i:%s') as CheckUpdateDate, rmadt.rmadt_techquotation as QuotationText, date_format(rmadt.ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as QuotationDate, date_format(rmadt.rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as StartDate, date_format(rmadt.rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as EndDate, clients.Client_Telephone1 as ClientTelephone, clients.client_Email as ClientEmail, if(rmadt.rmadt_approved = 1, concat('Approved at ', date_format(rmadt.rmadt_approveddate, '%d/%m/%Y %H:%i:%s')), if(rmadt.rmadt_approved = 0, 'Refused', 'Waiting for reply')) as ApprovalStatus, rmadt.rmadt_operatorSpareParts as SpareParts, rmadt.rmadt_qcremark as QCRemarks, rmadt.rmadt_qcduration as QCDuration, date_format(rmadt.rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repStartDateTime2, date_format(rmadt.rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repEndDateTime2, date_format(rmadt.rmadt_qcStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime2, date_format(rmadt.rmadt_qcEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime2, rmadt.rmadt_qcduration2, date_format(rmadt.rmadt_qcStartDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime, date_format(rmadt.rmadt_qcEndDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime, date_format(rmadt.rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2, rmadt.rmadt_techquotation2, date_format(rmadt.rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, items.Group_Code, rmadt.user_onhand, rmadt.rmadt_qcapproved, rmadt.rmadt_notes, rmadt.rmadt_problem 
   from rma,rmadt,items,clients,rma as rmaMA,rmadt as rmaMAdt
where rma.rma_id=rmadt.rma_id
  and rmaMa.rma_id=rmaMadt.rma_id
  and rmaMAdt.rmadt_id=rmadt.mi_id
  and rma.client_code=clients.ledger_number
  and rmadt.item_code=items.item_code
  and ((rmadt.rmadt_repaire='C'  and rmadt.rmadt_DateReceived is not null  and  rmadt.rmadt_status is not null)
      or ((rmadt.rmadt_repaire='X' or rmadt.rmadt_repaire='P') AND rmadt.rmadt_DateReceived is NOT null))
  and rma.rma_type='MR'
  and rmadt.rmadt_DateDelivered is null";
  if($user_engineer == 1 || $user_engineer == -1){
  $query_pending = $query_pending." and rmaMA.Rma_EngineerName = '".$_SESSION['useraccount']."'";
  }
   else
	  if($user_engineer == 2){
		  $query_pending = $query_pending." and rmadt.ramdt_techquotationdate is not null";
	  }
	  else
		  if($user_engineer == 3){
			  $query_pending = $query_pending." and rmadt.rmadt_approved = 1 and rmadt.rmadt_repStartDateTime is not null and rmadt.rmadt_repEndDateTime is not null and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
		  }
   if($search_status != ""){
	   if($search_status == 2){
		   $query_pending = $query_pending." and rmadt.rmadt_qcapproved = '0'";
	   }
	   else
	   if($search_status == -1){
		   $query_pending = $query_pending." ";
	   }
	   else{
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '$search_status' and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
	   }
  }
   if($search_status == "" && $user_engineer == 1){
	  $query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '0'";
  }
$query_pending = $query_pending." union
select 'Reception' as Service_Center,date_format(rmadt.rmadt_expectedDate,'%d/%m/%Y') as Expected_Date,
date_format(rma.rma_date,'%d/%m/%Y')  as Received_Date,
rma_reference as RMA_Ref,
rma.client_code as Client_Code,clients.ledger_name as Client_Name,
if(rmadt.rmadt_repaire='C','Credit note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) as Repair,
if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales ,  Repair','Pending'))) as Status,
items.item_code as Item_Code,items.item_description as Item_Description,rma.project_code
,rma.rma_remark ,rma.Rma_EngineerName, rma.rma_date, rmadt.Serial_Number, rmadt.rmadt_id, date_format(rmadt.rmadt_techcheckupdatedate, '%d/%m/%Y %H:%i:%s') as CheckUpdateDate, 
rmadt.rmadt_techquotation as QuotationText, 
date_format(rmadt.ramdt_techquotationdate, '%d/%m/%Y %H:%i:%s') as QuotationDate, 
date_format(rmadt.rmadt_repStartDateTime, '%d/%m/%Y %H:%i:%s') as StartDate, 
date_format(rmadt.rmadt_repEndDateTime, '%d/%m/%Y %H:%i:%s') as EndDate, clients.Client_Telephone1 as ClientTelephone, 
clients.client_Email as ClientEmail, 
if(rmadt.rmadt_approved = 1, concat('Approved at ', date_format(rmadt.rmadt_approveddate, '%d/%m/%Y %H:%i:%s')), 
if(rmadt.rmadt_approved = 0, 'Refused', 'Waiting for reply')) as ApprovalStatus, rmadt.rmadt_operatorSpareParts as SpareParts, 
rmadt.rmadt_qcremark as QCRemarks, rmadt.rmadt_qcduration as QCDuration, 
date_format(rmadt.rmadt_repStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repStartDateTime2, 
date_format(rmadt.rmadt_repEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_repEndDateTime2, 
date_format(rmadt.rmadt_qcStartDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime2, 
date_format(rmadt.rmadt_qcEndDateTime2, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime2, rmadt.rmadt_qcduration2, 
date_format(rmadt.rmadt_qcStartDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcStartDateTime, 
date_format(rmadt.rmadt_qcEndDateTime, '%d/%m/%Y %H:%i:%s') as rmadt_qcEndDateTime, 
date_format(rmadt.rmadt_techquotationdate2, '%d/%m/%Y %H:%i:%s') as rmadt_techquotationdate2, rmadt_techquotation2, 
date_format(rmadt.rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, items.Group_Code, rmadt.user_onhand, rmadt.rmadt_qcapproved, rmadt.rmadt_notes, rmadt.rmadt_problem 
   from rma,rmadt,items,clients
where rma.rma_id=rmadt.rma_id
  and rma.client_code=clients.ledger_number
  and rmadt.item_code=items.item_code
  and rma.rma_type='MI'
  and rmadt.rmadt_DateReceived is null
  and rmadt.rmadt_id not in (select mi_id from rmadt as rmadtret where rma_type='MR' and rmadtret.mi_id=rmadt.rmadt_id)";
  	if($user_engineer == 1 || $user_engineer == -1){
  		$query_pending = $query_pending." and rma.Rma_EngineerName = '".$_SESSION['useraccount']."'";
  	}
   	else
	  	if($user_engineer == 2){
		  	$query_pending = $query_pending." and rmadt.ramdt_techquotationdate is not null";
	  	}
	  	else
		  	if($user_engineer == 3){
			  	$query_pending = $query_pending." and rmadt.rmadt_approved = 1 and rmadt.rmadt_repStartDateTime is not null and rmadt.rmadt_repEndDateTime is not null and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
		  	}
   	if($search_status != ""){
	   	if($search_status == 2){
		   $query_pending = $query_pending." and rmadt.rmadt_qcapproved = '0'";
	   	}
	   	else
	   	if($search_status == -1){
		   $query_pending = $query_pending." ";
	   	}
	   	else{
	  		$query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '$search_status' and ifNull(rmadt.rmadt_qcapproved, 0) = 0";
	   	}
 	}
   	if($search_status == "" && $user_engineer == 1){
	  	$query_pending = $query_pending." and ifNull(rmadt.rmadt_approved, 0) = '0'";
  	}
$query_pending = $query_pending." ) as rma 
left join problemcategory on problemcategory.user_code = rma.Rma_EngineerName where 1=1";
//echo $search_status;
//exit;
//echo $query_pending;exit;
if($search_type == "RMA_Ref" && $search_location != ""){
	$query_pending = $query_pending." and RMA_Ref = '$search_location'";
}
else
	if($search_type == "Expected_Date" && $search_location != ""){
	   //	$search_location = explode("/", $search_location);
		//$search_location = $search_location[2]."-".$search_location[1]."-".$search_location[0];
	    $query_pending = $query_pending." and Expected_Date between '$search_location' and '$search_location 23:59:59'";
    }
	else
	if($search_type == "Received_Date" && $search_location != ""){
	   	//$search_location = explode("/", $search_location);
		//$search_location = $search_location[2]."-".$search_location[1]."-".$search_location[0];
	    $query_pending = $query_pending." and Received_Date between '$search_location' and '$search_location 23:59:59'";
    }
	else
		if($search_type == "Project" && $search_location != ""){
	       $query_pending = $query_pending." and project_code = '$search_location'";
    }
	else
		if($search_type == "Client_Code" && $search_location != ""){
	       $query_pending = $query_pending." and Client_Code like '%$search_location%'";
    }
	else
		if($search_type == "Client_Name" && $search_location != ""){
	       $query_pending = $query_pending." and Client_Name like '%$search_location%'";
    }
	else
		if($search_type == "Remark" && $search_location != ""){
	       $query_pending = $query_pending." and rma_remark like '%$search_location%'";
    }
	else
		if($search_type == "Engineer_Name" && $search_location != ""){
	       $query_pending = $query_pending." and Rma_EngineerName like '%$search_location%'";
    }
	else
		if($search_type == "Item_code" && $search_location != ""){
	       $query_pending = $query_pending." and Item_code like '%$search_location%'";
    }
	else
		if($search_type == "Item_Description" && $search_location != ""){
	       $query_pending = $query_pending." and Item_Description like '%$search_location%'";
    }
	else
		if($search_type == "Service_Center" && $search_location != ""){
	       $query_pending = $query_pending." and Service_Center like '%$search_location%'";
    }
	else
		if($search_type == "Repair" && $search_location != ""){
	       $query_pending = $query_pending." and Repair like '%$search_location%'";
    }
	else
		if($search_type == "Status" && $search_location != ""){
	       $query_pending = $query_pending." and Status like '%$search_location%'";
    }
	else
		if($search_type == "Serial_Number" && $search_location != ""){
	       $query_pending = $query_pending." and Serial_Number like '%$search_location%'";
	}
	else
		if($search_type == "Problem" && $search_location != ""){
	       $query_pending = $query_pending." and rmadt_problem like '%$search_location%'";
	}
	
	//echo $rmadt_qcrecieved;
	if($user_engineer==1){
		if($engineerchecked==0 && $search_status==0){
			if($from_date != "" && $to_date != ""){
				$from_date = explode("/", $from_date);
				$from_date = $from_date[2]."-".$from_date[1]."-".$from_date[0];
				$to_date = explode("/", $to_date);
				$to_date = $to_date[2]."-".$to_date[1]."-".$to_date[0];
				$query_pending = $query_pending." and rma_date between '$from_date' and '$to_date 23:59:59'";
			}
			else{
				$query_dates  = "select date_format(now(), '%Y-%m-%d') as fromdate, date_format(now(), '%Y-%m-%d') as todate from dual";
				$result_dates = mysql_query($query_dates, $connection); 
				$row_dates    = mysql_fetch_array($result_dates);
				extract($row_dates);
				$query_pending = $query_pending." and rma_date between '$fromdate' and '$todate 23:59:59'";
			}
		}
	}
	else if($user_engineer==3){
		if($qcchecked == 0){
			if($from_date != "" && $to_date != ""){
				$from_date = explode("/", $from_date);
				$from_date = $from_date[2]."-".$from_date[1]."-".$from_date[0];
				$to_date = explode("/", $to_date);
				$to_date = $to_date[2]."-".$to_date[1]."-".$to_date[0];
				$query_pending = $query_pending." and rma_date between '$from_date' and '$to_date 23:59:59'";
			}
			else{
				$query_dates  = "select date_format(now(), '%Y-%m-%d') as fromdate, date_format(now(), '%Y-%m-%d') as todate from dual";
				$result_dates = mysql_query($query_dates, $connection); 
				$row_dates    = mysql_fetch_array($result_dates);
				extract($row_dates);
				$query_pending = $query_pending." and rma_date between '$fromdate' and '$todate 23:59:59'";
			}
		}
	}
	else if($user_engineer==2){
		/*if($operatorchecked == 0){
			if($from_date != "" && $to_date != ""){
				$from_date = explode("/", $from_date);
				$from_date = $from_date[2]."-".$from_date[1]."-".$from_date[0];
				$to_date = explode("/", $to_date);
				$to_date = $to_date[2]."-".$to_date[1]."-".$to_date[0];
				$query_pending = $query_pending." and rma_date between '$from_date' and '$to_date 23:59:59'";
			}
			else{
				$query_dates  = "select date_format(now(), '%Y-%m-%d') as fromdate, date_format(now(), '%Y-%m-%d') as todate from dual";
				$result_dates = mysql_query($query_dates, $connection); 
				$row_dates    = mysql_fetch_array($result_dates);
				extract($row_dates);
				$query_pending = $query_pending." and rma_date between '$fromdate' and '$todate 23:59:59'";
			}
		}*/
	}
	else{
		if($from_date != "" && $to_date != ""){
			$from_date = explode("/", $from_date);
			$from_date = $from_date[2]."-".$from_date[1]."-".$from_date[0];
			$to_date = explode("/", $to_date);
			$to_date = $to_date[2]."-".$to_date[1]."-".$to_date[0];
			$query_pending = $query_pending." and rma_date between '$from_date' and '$to_date 23:59:59'";
		}
		else{
			$query_dates  = "select date_format(now(), '%Y-%m-%d') as fromdate, date_format(now(), '%Y-%m-%d') as todate from dual";
			$result_dates = mysql_query($query_dates, $connection); 
			$row_dates    = mysql_fetch_array($result_dates);
			extract($row_dates);
			$query_pending = $query_pending." and rma_date between '$fromdate' and '$todate 23:59:59'";
		}
	}

	$query_pending = $query_pending." order by rma_date desc, Expected_Date,received_date desc,RMA_Ref,Client_Code,Item_Code";
	//echo $query_pending;
		//echo $from_date;exit;
	if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
	$result_pending2 = mysql_query($query_pending, $connection);
	$number_pending = mysql_num_rows($result_pending2);
	$query_pending = $query_pending." LIMIT $start, ".$config['num_rows_per_page'];
	$totalpages = ceil($number_pending/$config['num_rows_per_page']);
	//echo $query_pending;exit;
    $result_pending = mysql_query($query_pending, $connection);
	$count = 0;
	$countRows = 0;
	while($row_pending = mysql_fetch_array($result_pending)){
		extract($row_pending);
		
		$query_engrecv="select rmadt_engrecieved from rmadt where rmadt_id = $rmadt_id";
		$rmadt_engrecieved_row= mysql_query($query_engrecv, $connection);
		$rmadt_engrecieved = mysql_fetch_array($rmadt_engrecieved_row);


		$query_qcrecv="select rmadt_qcrecieved from rmadt where rmadt_id = $rmadt_id";
		$rmadt_qcrecieved_row= mysql_query($query_qcrecv, $connection);
		$rmadt_qcrecieved = mysql_fetch_array($rmadt_qcrecieved_row);

		$query_operatorrecv="select rmadt_operatorrecieved from rmadt where rmadt_id = $rmadt_id";
		$rmadt_operatorrecieved_row= mysql_query($query_operatorrecv, $connection);
		$rmadt_operatorrecieved = mysql_fetch_array($rmadt_operatorrecieved_row);
		//echo $rmadt_engrecieved[0];exit;
		$count++;
		if($count %2 != 0){
			$background = "#e6e6e6";
		}
		else{
			$background = "#f7f7f7";
		}
		if($curr_rmadt_id == $rmadt_id){
			$checked = "checked";
		}
		else{
			$checked = "";
		}
		echo"<tr>";
		//echo $user_engineer;exit;
		//echo $user_engineer;exit;
		
		$State="";

		if($user_engineer==1){
			if($rmadt_engrecieved[0] == 0)
				$State="Not Checked";
			else if(empty($QuotationText))
				$State="Checked";
			else if(!isset($StartDate))
				$State="Awaiting Repair";
			else if(!isset($EndDate))
				$State="Under Repair";
			else if($ApprovalStatus == "Waiting for reply")
				$State="Awaiting Approval";
			else if($rmadt_qcapproved == "")
				$State="At QC";
			else if($rmadt_qcapproved == 0)
				$State="Refused by QC";

			echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0], $State)'></td>";

			if($engineerchecked == 0 && $rmadt_engrecieved[0] == 0)
				echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='checkbox' name='rmadt_engrecieved[]' value='$rmadt_id' id='rmadt_enrecieved'/></td>";
			else if($engineerchecked == 1 && $rmadt_engrecieved[0] == 1)
				echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>";

			echo"<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
			<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>";
			echo"<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$State</td>";
			
			echo"<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
			/*
			if($search_status==1 && !isset($EndDate)){
				$countRows++;
				echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, 1, $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
					<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
				
			}else{
				if($search_status==0){
					if($engineerchecked == 0 && $rmadt_engrecieved[0] == 0 && $RmaDt_Return==""){
						$countRows++;
						//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
						echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
						<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='checkbox' name='rmadt_engrecieved[]' value='$rmadt_id' id='rmadt_enrecieved'/></td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
					}
					else 
						if($engineerchecked == 1 && $rmadt_engrecieved[0] == 1 && empty($QuotationText)){
							$countRows++;
							//echo $QuotationText;
							//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
							echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
							<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
						}
				}
				else {
					if($search_status==2 && $rmadt_repEndDateTime2==""){
						$countRows++;
						echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
							<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
							<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
							<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
							<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
					}
				}
			}
		}
		*/
		//echo $qcchecked;echo $rmadt_qcrecieved[0]; exit;
		if($user_engineer==3){
			//echo $QCDuration."  ".$rmadt_techquotation2." ".$rmadt_qcduration2."--";
				if(($qcchecked == 0 && $rmadt_qcrecieved[0] == 0)){
					$countRows++;
					//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
					<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='checkbox' name='rmadt_qcrecieved[]' value='$rmadt_id' id='rmadt_qcrecieved'/></td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
				}
				else 
					if(($qcchecked == 1 && $rmadt_qcrecieved[0] == 1 && !isset($QCDuration)) || ($qcchecked == 1 && $rmadt_qcrecieved[0] == 1 && isset($rmadt_techquotation2) && !isset($rmadt_qcduration2))){
						$countRows++;
						//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
						echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
						<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
					}
			}
		
			if($user_engineer==2){
				/*if($operatorchecked == 0 && $rmadt_operatorrecieved[0] == 0){
					//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
					<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='checkbox' name='rmadt_operatorrecieved[]' value='$rmadt_id' id='rmadt_operatorrecieved'/></td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$count</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status.", ".$ApprovalStatus</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
				}
				else 
					if($operatorchecked == 1 && $rmadt_operatorrecieved[0] == 1 && $ApprovalStatus == "Waiting for reply"){
						//echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>
						echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
						<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$count</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
						<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
						<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
						<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
					}*/
				if($operatorchecked == 0)	{	
					$countRows++;
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
					
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_problem</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$QuotationText</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status.", ".$ApprovalStatus</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$user_onhand</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
				}
				if($operatorchecked == 1 && $ApprovalStatus == "Waiting for reply"){
					$countRows++;
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0], $operatorchecked, $rmadt_operatorrecieved[0])'></td>
					
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$countRows</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
					<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$Group_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$imei</td>
					<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_problem</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$QuotationText</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Status.", ".$ApprovalStatus</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$user_onhand</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_notes</td>
					<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rma_date</td>";
				}
			}

		//echo $rmadt_engrecieved_date;$exit;
		if($user_engineer!=1 && $user_engineer!=3 && $user_engineer!=2){
			/*if($user_engineer==3){
				echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><img src='img/btnopen.jpg' width='20' style='cursor: pointer; vertical-align: top' onclick='openRMADesc($rmadt_id, $engineerchecked, $rmadt_engrecieved[0], $qcchecked, $rmadt_qcrecieved[0])'></td>";
				if($qcchecked==0)
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='checkbox' name='rmadt_qcrecieved[]' value='$rmadt_id' id='rmadt_qcrecieved'/></td>";
				else
					echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'></td>";
			}*/
			
			echo"<td style='font-size: 13px; background: $background; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='rmadt_id_' id='rmadt_id_$rmadt_id' style='cursor: pointer' onclick=\"selectRmaDt('$rmadt_id')\" $checked /></td>";
			
			echo "<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Expected_Date</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Received_Date</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$CheckUpdateDate</td>
			<td style='font-size: 14px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Service_Center</td>
			<td style='font-size: 14px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$rmadt_DateReceived</td>
			<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$project_code</td>";
			}
		if($user_engineer == 0/* || $user_engineer == 2*/){
			echo"<td style='font-size: 13px; background: $background; width: 170px; border: solid 1px #000000; padding: 7px'>$Client_Code</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$ClientTelephone</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$ClientEmail</td>";
		}
		if($user_engineer != 1 && $user_engineer != 3 && $user_engineer != 2){
			echo"<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$rma_remark</td>";
			
			echo"<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$EngName</td>";
			
			echo"<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$user_onhand</td>";
			echo"<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$Repair</td>
			<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$Status</td>
			<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Item_Description</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Serial_Number</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$ApprovalStatus</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$SpareParts</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$QuotationText</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$QuotationDate</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$StartDate</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$EndDate</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_qcStartDateTime</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_qcEndDateTime</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>";
			if($QCDuration != ""){
				echo "$QCDuration minute(s)";
			}
			echo "</td>
			<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$rmadt_techquotation2</td>
			<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$rmadt_techquotationdate2</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_repStartDateTime2</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_repEndDateTime2</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_qcStartDateTime2</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$rmadt_qcEndDateTime2</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>";
			if($rmadt_qcduration2 != ""){
				echo $rmadt_qcduration2;
				
				echo " minute(s)";
			}
			echo"</td>
			<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$QCRemarks</td>
			</tr>";
		}
	}
	echo "</form>";
	echo"</table>";
	echo "</div>";
	echo "<div style='text-align: center; margin-top: 10px'>";
	if($start != 0){
		echo "<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
	}
	if($pagenumber != $totalpages && $number_pending != 0){
		echo "<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
	}
	echo "</div>";
	echo "<input type='hidden' name='start_par' id='start_par' value='$start' />";
	echo "<input type='hidden' name='pagenumber_par' id='pagenumber_par' value='$pagenumber' />";
}

function drawReturnRMALocation($connection, $search_type, $search_location, $start, $pagenumber){
	echo "<div style='height: 500px; overflow-y: scroll; overflow-x: scoll; width: 1106px; margin-left: 43px'>";
	echo "<input type='hidden' id='rmadt_id' value='' />";
	echo "<table style='width: 2000px; border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>RMA Ref.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Expected Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; border: solid 1px #000000; padding: 7px'>Received Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Location</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 170px; text-align: left; border: solid 1px #000000; padding: 7px'>Project</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Remark</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Engineer Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; text-align: left; border: solid 1px #000000; padding: 7px'>Repair</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px' >Status</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 153px; text-align: left; border: solid 1px #000000; padding: 7px'>Item Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 231px;text-align: left; border: solid 1px #000000; padding: 7px'>Item Description</th>
	</tr>";
	$query_pending = "SELECT Service_Center,
	date_format(Expected_Date, '%d/%m/%Y %h:%i:%s %p') as Expected_Date,
	date_format(Received_Date, '%d/%m/%Y %h:%i:%s %p') as Received_Date,
	RMA_Ref,
	Client_Code,
	Client_Name,
	Repair,Status,
	Item_Code,
	Item_Description,
	project_code,
	rma_remark,
	Rma_EngineerName
FROM
(SELECT 'Service Center' AS Service_Center,
		rmadt.rmadt_expectedDate AS Expected_Date,
		rmadt.rmadt_DateReceived AS Received_Date,
		rma_reference AS RMA_Ref,
		rma.client_code AS Client_Code,
		clients.ledger_name AS Client_Name,
		if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) AS Repair,
		if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales , Repair','Pending'))) AS Status,
		items.item_code AS Item_Code,
		items.item_description AS Item_Description,
		rma.project_code,
		rma.rma_remark,
		rma.Rma_EngineerName
 FROM rma,
	  rmadt,
	  items,
	  clients
 WHERE rma.rma_id=rmadt.rma_id
   AND rma.client_code=clients.ledger_number
   AND rmadt.item_code=items.item_code
   AND rma.rma_type='MI'
   AND rmadt.rmadt_DateReceived IS NOT NULL
   AND rmadt.rmadt_id NOT IN
	 (SELECT mi_id
	  FROM rmadt AS rmadtret
	  WHERE rma_type='MR'
		AND rmadtret.mi_id=rmadt.rmadt_id)
 UNION SELECT 'Service Center' AS Service_Center,
			  rmadt.rmadt_expectedDate AS Expected_Date,
			  rma.rma_Date AS Received_Date,
			  rmama.rma_reference AS RMA_Ref,
			  rma.client_code AS Client_Code,
			  clients.ledger_name AS Client_Name,
			  if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) AS Repair,
			  if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales , Repair','Pending'))) AS Status,
			  items.item_code AS Item_Code,
			  items.item_description AS Item_Description,
			  rma.project_code,
			  rma.rma_remark,
			  rma.Rma_EngineerName
 FROM rma,
	  rmadt,
	  items,
	  clients,
	  rma AS rmaMA,
	  rmadt AS rmaMAdt
 WHERE rma.rma_id=rmadt.rma_id
   AND rmaMa.rma_id=rmaMadt.rma_id
   AND rmaMAdt.rmadt_id=rmadt.mi_id
   AND rma.client_code=clients.ledger_number
   AND rmadt.item_code=items.item_code
   AND rmadt.rmadt_status IS NULL
   AND rmadt.rmadt_RetRMADateRecieved IS NULL
   AND RMADT.rmadt_DateReceived IS NULL
   AND rma.rma_type='MR'
 UNION SELECT 'Warehouse' AS Service_Center,
			  rmadt.rmadt_expectedDate AS Expected_Date,
			  rmadt.rmadt_RetRMADateRecieved AS Received_Date,
			  rmama.rma_reference AS RMA_Ref,
			  rma.client_code AS Client_Code,
			  clients.ledger_name AS Client_Name,
			  if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) AS Repair,
			  if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales , Repair','Pending'))) AS Status,
			  items.item_code AS Item_Code,
			  items.item_description AS Item_Description,
			  rma.project_code,
			  rma.rma_remark,
			  rma.Rma_EngineerName
 FROM rma,
	  rmadt,
	  items,
	  clients,
	  rma AS rmaMA,
	  rmadt AS rmaMAdt
 WHERE rma.rma_id=rmadt.rma_id
   AND rmaMa.rma_id=rmaMadt.rma_id
   AND rmaMAdt.rmadt_id=rmadt.mi_id
   AND rma.client_code=clients.ledger_number
   AND rmadt.item_code=items.item_code
   AND (rmadt.rmadt_RetRMADateRecieved IS NOT NULL
		OR rmadt.rmadt_status IS NOT NULL)
   AND rmadt.rmadt_DateReceived IS NULL
   AND rma.rma_type='MR'
 UNION SELECT 'Reception' AS Service_Center,
			  rmadt.rmadt_expectedDate AS Expected_Date,
			  ifnull(rmadt.rmadt_DateReceived,rmama.rma_date) AS Received_Date,
			  rmama.rma_reference AS RMA_Ref,
			  rma.client_code AS Client_Code,
			  clients.ledger_name AS Client_Name,
			  if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) AS Repair,
			  if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales , Repair','Pending'))) AS Status,
			  items.item_code AS Item_Code,
			  items.item_description AS Item_Description,
			  rma.project_code,
			  rma.rma_remark,
			  rma.Rma_EngineerName
 FROM rma,
	  rmadt,
	  items,
	  clients,
	  rma AS rmaMA,
	  rmadt AS rmaMAdt
 WHERE rma.rma_id=rmadt.rma_id
   AND rmaMa.rma_id=rmaMadt.rma_id
   AND rmaMAdt.rmadt_id=rmadt.mi_id
   AND rma.client_code=clients.ledger_number
   AND rmadt.item_code=items.item_code
   AND ((rmadt.rmadt_repaire='C'
		 AND rmadt.rmadt_DateReceived IS NOT NULL
		 AND rmadt.rmadt_status IS NOT NULL)
		OR ((rmadt.rmadt_repaire='X'
			 OR rmadt.rmadt_repaire='P')
			AND rmadt.rmadt_DateReceived IS NOT NULL))
   AND rma.rma_type='MR'
   AND rmadt.rmadt_DateDelivered IS NULL
 UNION SELECT 'Reception' AS Service_Center,
			  rmadt.rmadt_expectedDate AS Expected_Date,
			  rma.rma_date AS Received_Date,
			  rma_reference AS RMA_Ref,
			  rma.client_code AS Client_Code,
			  clients.ledger_name AS Client_Name,
			  if(rmadt.rmadt_repaire='C','Credit Note',if(rmadt.rmadt_repaire='X','Rejected',if(rmadt.rmadt_repaire='P','Replacement','Pending'))) AS Repair,
			  if(rmadt.rmadt_status='X','Rejected',if(rmadt.rmadt_status='R','Repaired wihtout Spare Parts',if(rmadt.rmadt_status='U','Exchange, Upgrade, Return Sales , Repair','Pending'))) AS Status,
			  items.item_code AS Item_Code,
			  items.item_description AS Item_Description,
			  rma.project_code,
			  rma.rma_remark,
			  rma.Rma_EngineerName
 FROM rma,
	  rmadt,
	  items,
	  clients
 WHERE rma.rma_id=rmadt.rma_id
   AND rma.client_code=clients.ledger_number
   AND rmadt.item_code=items.item_code
   AND rma.rma_type='MI'
   AND rmadt.rmadt_DateReceived IS NULL
   AND rmadt.rmadt_id NOT IN
	 (SELECT mi_id
	  FROM rmadt AS rmadtret
	  WHERE rma_type='MR'
		AND rmadtret.mi_id=rmadt.rmadt_id) ) AS rma
WHERE 1=1";
if($search_type == "RMA_Ref" && $search_location != ""){
	$query_pending = $query_pending." and RMA_Ref = '$search_location'";
}
else
	if($search_type == "Expected_Date" && $search_location != ""){
	   //	$search_location = explode("/", $search_location);
		//$search_location = $search_location[2]."-".$search_location[1]."-".$search_location[0];
	    $query_pending = $query_pending." and Expected_Date between '$search_location' and '$search_location 23:59:59'";
    }
	else
	if($search_type == "Received_Date" && $search_location != ""){
	   	//$search_location = explode("/", $search_location);
		//$search_location = $search_location[2]."-".$search_location[1]."-".$search_location[0];
	    $query_pending = $query_pending." and Received_Date between '$search_location' and '$search_location 23:59:59'";
    }
	else
		if($search_type == "Project" && $search_location != ""){
	       $query_pending = $query_pending." and project_code = '$search_location'";
    }
	else
		if($search_type == "Client_Code" && $search_location != ""){
	       $query_pending = $query_pending." and Client_Code like '%$search_location%'";
    }
	else
		if($search_type == "Client_Name" && $search_location != ""){
	       $query_pending = $query_pending." and Client_Name like '%$search_location%'";
    }
	else
		if($search_type == "Remark" && $search_location != ""){
	       $query_pending = $query_pending." and rma_remark like '%$search_location%'";
    }
	else
		if($search_type == "Engineer_Name" && $search_location != ""){
	       $query_pending = $query_pending." and Rma_EngineerName like '%$search_location%'";
    }
	else
		if($search_type == "Item_code" && $search_location != ""){
	       $query_pending = $query_pending." and Item_code like '%$search_location%'";
    }
	else
		if($search_type == "Item_Description" && $search_location != ""){
	       $query_pending = $query_pending." and Item_Description like '%$search_location%'";
    }
	else
		if($search_type == "Service_Center" && $search_location != ""){
	       $query_pending = $query_pending." and Service_Center like '%$search_location%'";
    }
	else
		if($search_type == "Repair" && $search_location != ""){
	       $query_pending = $query_pending." and Repair like '%$search_location%'";
    }
	else
		if($search_type == "Status" && $search_location != ""){
	       $query_pending = $query_pending." and Status like '%$search_location%'";
    }
$query_pending = $query_pending." ORDER BY date_format(Expected_Date, '%Y-%m-%d') DESC,
									date_format(Received_date, '%Y-%m-%d') DESC,
									RMA_Ref desc,
									Client_Code desc,
									Item_Code desc";
 
		  //echo $query_pending;exit;
	if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
	$result_pending2 = mysql_query($query_pending, $connection);
	$number_pending = mysql_num_rows($result_pending2);
	$query_pending = $query_pending." LIMIT $start, 20";
	$totalpages = ceil($number_pending/20);
	//echo $query_pending;
    $result_pending = mysql_query($query_pending, $connection);
	$count = 0;
	while($row_pending = mysql_fetch_array($result_pending)){
		extract($row_pending);
		$count++;
		if($count %2 != 0){
			$background = "#e6e6e6";
		}
		else{
			$background = "#f7f7f7";
		}
		echo"<tr>
	<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
	<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Expected_Date</td>
	<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Received_Date</td>
	<td style='font-size: 14px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Service_Center</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$project_code</td>
	<td style='font-size: 13px; background: $background; width: 170px; border: solid 1px #000000; padding: 7px'>$Client_Code</td>
	<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
	<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$rma_remark</td>
	<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Rma_EngineerName</td>
	<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$Repair</td>
	<td style='font-size: 13px; background: $background; width: 100px; border: solid 1px #000000; padding: 7px'>$Status</td>
	<td style='font-size: 13px; background: $background; width: 153px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
	<td style='font-size: 13px; background: $background; width: 231px; border: solid 1px #000000; padding: 7px'>$Item_Description</td>
	</tr>";
	}
	echo"</table>";
	echo "</div>";
	echo "<div style='text-align: center; margin-top: 10px'>";
	if($start != 0){
	echo "<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
	}
	if($pagenumber != $totalpages && $number_pending != 0){
	echo "<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
	}
	echo "</div>";
	echo "<input type='hidden' name='start_par' id='start_par' value='$start' />";
	echo "<input type='hidden' name='pagenumber_par' id='pagenumber_par' value='$pagenumber' />";
}
function drawReturnRMAReception($connection, $search_type, $search_reception, $start, $pagenumber){
	echo "<div style='height: 500px; overflow-y: scroll; overflow-x: scoll; width: 1106px; margin-left: 43px'>";
	echo "<input type='hidden' id='rmadt_id' value='' />";
	echo "<table style='width: 1500px; border-collapse: collapse'>
	<tr>
	<th style='font-size: 14px; background: #d8d8d8; width: 20px; border: solid 1px #000000; padding: 7px'></th>
	<th style='font-size: 14px; background: #d8d8d8; width: 100px; border: solid 1px #000000; padding: 7px'>RMA Ref.</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 120px; border: solid 1px #000000; padding: 7px'>Received Date</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 110px; text-align: left; border: solid 1px #000000; padding: 7px'>Location</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 120px; text-align: left; border: solid 1px #000000; padding: 7px'>Project</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 140px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px; text-align: left; border: solid 1px #000000; padding: 7px'>Client Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px; text-align: left; border: solid 1px #000000; padding: 7px'>Remark</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 150px; text-align: left; border: solid 1px #000000; padding: 7px'>Engineer Name</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 140px; text-align: left; border: solid 1px #000000; padding: 7px'>Item Code</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px;text-align: left; border: solid 1px #000000; padding: 7px'>Item Description</th>
	<th style='font-size: 14px; background: #d8d8d8; width: 200px;text-align: left; border: solid 1px #000000; padding: 7px'>Service Center</th>
	</tr>";
	$query_user  = "select ifNull(user_engineer, 0) as IsEngineer from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
	$result_user = mysql_query($query_user, $connection); 
	$row_user    = mysql_fetch_array($result_user);
	extract($row_user);
	$query_pending = "SELECT RmaDtID,
	Service_Center,
	date_format(Received_Date, '%d/%m/%Y %h:%i:%s %p') AS Received_Date,
	RMA_Ref,
	Client_Code,
	Client_Name,
	Repair,
	Status,
	Item_Code,
	Item_Description,
	project_code,
	rmadt_id,
	rma_remark,
	Rma_EngineerName,
	ServiceCenter
FROM
(SELECT rmadt.rmadt_id AS RmaDtID,
	   'Reception' AS Service_Center,
	   rma.rma_date AS Received_Date,
	   rma_reference AS RMA_Ref,
	   rma.client_code AS Client_Code,
	   clients.ledger_name AS Client_Name,
	   if(rmadt.rmadt_repaire='C', 'Credit Note', if(rmadt.rmadt_repaire='X', 'Rejected', if(rmadt.rmadt_repaire='P', 'Replacement', 'Pending'))) AS Repair,
	   if(rmadt.rmadt_status='X', 'Rejected', if(rmadt.rmadt_status='R', 'Repaired wihtout Spare Parts', if(rmadt.rmadt_status='U', 'Exchange, Upgrade, Return Sales , Repair', 'Pending'))) AS Status,
	   items.item_code AS Item_Code,
	   items.item_description AS Item_Description,
	   rma.project_code,
	   rmadt.rmadt_id,
	   rma.rma_remark,
	   rma.Rma_EngineerName,
	   rmadt.rmadt_servicecenter AS ServiceCenter
FROM rma,
	 rmadt,
	 items,
	 clients
WHERE rma.rma_id=rmadt.rma_id
  AND rma.client_code=clients.ledger_number
  AND rmadt.item_code=items.item_code
  AND rma.rma_type='MI'
  AND rmadt.rmadt_DateReceived IS NULL
  AND rmadt.rmadt_id NOT IN
	(SELECT mi_id
	 FROM rmadt AS rmadtret
	 WHERE rma_type='MR'
	   AND rmadtret.mi_id=rmadt.rmadt_id) ) AS rma
LEFT JOIN problemcategory ON problemcategory.user_code = rma.Rma_EngineerName
WHERE 1=1";
if($search_type == "RMA_Ref" && $search_reception != ""){
	$query_pending = $query_pending." and RMA_Ref = '$search_reception'";
}
	else
	if($search_type == "Received_Date" && $search_reception != ""){
	   	//$search_location = explode("/", $search_location);
		//$search_location = $search_location[2]."-".$search_location[1]."-".$search_location[0];
	    $query_pending = $query_pending." and Received_Date between '$search_reception' and '$search_reception 23:59:59'";
    }
	else
		if($search_type == "Project" && $search_reception != ""){
	       $query_pending = $query_pending." and project_code = '$search_reception'";
    }
	else
		if($search_type == "Client_Code" && $search_reception != ""){
	       $query_pending = $query_pending." and Client_Code like '%$search_reception%'";
    }
	else
		if($search_type == "Client_Name" && $search_reception != ""){
	       $query_pending = $query_pending." and Client_Name like '%$search_reception%'";
    }
	else
		if($search_type == "Remark" && $search_reception != ""){
	       $query_pending = $query_pending." and rma_remark like '%$search_reception%'";
    }
	else
		if($search_type == "Engineer_Name" && $search_reception != ""){
	       $query_pending = $query_pending." and Rma_EngineerName like '%$search_reception%'";
    }
	else
		if($search_type == "Item_code" && $search_reception != ""){
	       $query_pending = $query_pending." and Item_code like '%$search_reception%'";
    }
	else
		if($search_type == "Item_Description" && $search_reception != ""){
	       $query_pending = $query_pending." and Item_Description like '%$search_reception%'";
    }
	else
		if($search_type == "Service_Center" && $search_reception != ""){
	       $query_pending = $query_pending." and Service_Center like '%$search_reception%'";
    }
	
	if($IsEngineer == 1){
		$query_pending = $query_pending." and rma.Rma_EngineerName = '".$_SESSION['useraccount']."'";
	}
$query_pending = $query_pending." ORDER BY date_format(Received_date, '%Y-%m-%d') DESC,RMA_Ref,
									Client_Code,
									Item_Code";
 
	//echo $query_pending;exit;
	if($start == "") $start = 0;
	if($pagenumber == "") $pagenumber = 1;
	$result_pending2 = mysql_query($query_pending, $connection);
	$number_pending = mysql_num_rows($result_pending2);
	$query_pending = $query_pending." LIMIT $start, 20";
	$totalpages = ceil($number_pending/20);
	//echo $query_pending;
    $result_pending = mysql_query($query_pending, $connection);
	$count = 0;
	while($row_pending = mysql_fetch_array($result_pending)){
		extract($row_pending);
		$count++;
		if($count %2 != 0){
			$background = "#e6e6e6";
		}
		else{
			$background = "#f7f7f7";
		}
		echo"<tr>
	<td style='font-size: 13px; background: $background; width: 20px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' id='$RmaDtID' name='chk_rmadt_id' onclick=\"fillRmaDt('$RmaDtID')\" style='cursor: pointer'></td>
	<td style='font-size: 13px; background: $background; width: 100px; text-align: center; border: solid 1px #000000; padding: 7px'>$RMA_Ref</td>
	<td style='font-size: 13px; background: $background; width: 120px; border: solid 1px #000000; padding: 7px'>$Received_Date</td>
	<td style='font-size: 14px; background: $background; width: 110px; border: solid 1px #000000; padding: 7px'>$Service_Center</td>
	<td style='font-size: 13px; background: $background; width: 120px; border: solid 1px #000000; padding: 7px'>$project_code</td>
	<td style='font-size: 13px; background: $background; width: 140px; border: solid 1px #000000; padding: 7px'>$Client_Code</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$Client_Name</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$rma_remark</td>
	<td style='font-size: 13px; background: $background; width: 150px; border: solid 1px #000000; padding: 7px'>$PROBLEMCATEGORY_NAME</td>
	<td style='font-size: 13px; background: $background; width: 140px; border: solid 1px #000000; padding: 7px'>$Item_Code</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$Item_Description</td>
	<td style='font-size: 13px; background: $background; width: 200px; border: solid 1px #000000; padding: 7px'>$ServiceCenter</td>
	</tr>";
	}
	echo"</table>";
	echo "</div>";
	echo "<div style='text-align: center; margin-top: 10px'>";
	if($start != 0){
	echo "<img src='img/arrowleft.png' width=20 style='cursor: pointer' onclick=\"movePrev()\" />";
	}
	if($pagenumber != $totalpages && $number_pending != 0){
	echo "<img src='img/arrowright.png' width=20 style='cursor: pointer' onclick=\"moveNext()\" />";
	}
	echo "</div>";
	echo "<input type='hidden' name='start_par' id='start_par' value='$start' />";
	echo "<input type='hidden' name='pagenumber_par' id='pagenumber_par' value='$pagenumber' />";
}
function addToDelivery($connection, $RMA_Id){
	
	$query_module  = "select MODULE_PROVEOFDELIVERY as Moduleproveofdelivery, module_returnRMATOSales as ModuleReturnRMATOSales, Module_Project as ModuleProject, Module_Costcenter as ModuleCostcenter, module_returnRMATOSales as ModuleReturnRMATOSales, Module_Delivery as moduledelivery, Module_PostToJournal as ModulePostToJournal
                      from module";
	$result_module = mysql_query($query_module, $connection); 
	$row_module    = mysql_fetch_array($result_module);
	extract($row_module);
	
	$query_returnrma  = "select rma_id as returnrmarma_id, rma_deliveryid as returnrmarma_deliveryid, rma_dreturnid as returnrmarma_dreturnid, Project_code as ReturnRmaProject_code, Client_Code as ReturnRmaClient_code, Currency_code as returnrmacurrency_Code, RMA_Reference as ReturnRmaRMA_Reference, rma_sreturnid as returnrmarma_sreturnid, rma_saleid as returnrmarma_saleid
	                     from rma 
						 where rma_id = '$RMA_Id'";
	$result_returnrma = mysql_query($query_returnrma, $connection); 
	$row_returnrma    = mysql_fetch_array($result_returnrma);
	extract($row_returnrma);
	
	$query_miid  = "select mi_id as ReturnRmadtMI_ID, RMADt_Repaire as ReturnRmadtRMADt_Repaire, RmaDt_Return as ReturnRmadtRMATDT_RETURN 
	                from rmadt 
					where rma_id = '$RMA_Id'";
	//echo $query_miid;exit;
	$result_miid = mysql_query($query_miid, $connection);
	$row_miid    = mysql_fetch_array($result_miid);
	extract($row_miid);
	
	$query_rmaid  = "select rma_id as rmarma_id from rmadt where rmadt_id = '$ReturnRmadtMI_ID'";
	$result_rmaid = mysql_query($query_rmaid, $connection); 
	$row_rmaid    = mysql_fetch_array($result_rmaid);
	extract($row_rmaid);

	if($Moduleproveofdelivery == 1){
		
	$query_returnRMA  = "select status_id from InvoiceStatus where  journal_type='MR' and sale_id = '$returnrmarma_id'";
	$result_returnRMA = mysql_query($query_returnRMA, $connection); 
	$number_returnRMA = mysql_num_rows($result_returnRMA);
	$row_returnRMA    = mysql_fetch_array($result_returnRMA);
	extract($row_returnRMA);
	  if($number_returnRMA == 0 || $status_id == 3){
		  $query_delete  = "delete from InvoiceStatus where journal_type='MR' and sale_id='$returnrmarma_id'";
		  $result_delete = mysql_query($query_delete, $connection); 
		  
		  $query_insert = "insert into InvoiceStatus (sale_id,sale_reference ,journal_type,InvoiceStatus_DateRead,Status_id,
		                   InvoiceStatus_charges,client_code,InvoiceStatus_date)
						   select rma_id,rma_reference,rma_type,now(),3,0,client_code,rma_date
						   from rma
						   where rma_id='$returnrmarma_id'";
			//echo "in". $query_insert;	exit;
		  $result_insert = mysql_query($query_insert, $connection);
	  }
	}
	//	echo "in". $Moduleproveofdelivery;	exit;
	if($ModuleReturnRMATOSales == 1 && $ReturnRmadtRMADt_Repaire == "U"){
		if($returnrmarma_deliveryid != "" && $returnrmarma_deliveryid != 0){
		$query_update = "update deliveries Delivery_Total = 0,
		                                   Delivery_TotalBase1 = 0,
										   Delivery_TotalBase2 = 0,
										   Delivery_VAT = 0,
										   Delivery_VATBase1 = 0,
										   Delivery_VATBase2 = 0,
										   Delivery_Taxable = 0,
										   Delivery_TaxableBase1 = 0,
										   Delivery_TaxableBase2 = 0,
										   Delivery_NoneTaxable = 0,
										   Delivery_NoneTaxableBase1 = 0,
										   Delivery_NoneTaxableBase2 = 0,
										   Delivery_Cash = 0,
										   Delivery_CashBase1 = 0,
										   Delivery_CashBase2 = 0,
										   Delivery_CashInBase1 = 0,
										   Delivery_CashInBase2 = 0,
			              where delivery_id = '$returnrmarma_deliveryid'";
		$result_update = mysql_query($query_update, $connection);
		
		$query_delete  = "DELETE FROM DELIVERYDT WHERE DELIVERY_ID='$returnrmarma_deliveryid'";
		$result_delete = mysql_query($query_delete, $connection); 
	}
	if($returnrmarma_dreturnid != "" && $returnrmarma_dreturnid != 0){
		$query_update = "update deliveries Delivery_Total = 0,
		                                   Delivery_TotalBase1 = 0,
										   Delivery_TotalBase2 = 0,
										   Delivery_VAT = 0,
										   Delivery_VATBase1 = 0,
										   Delivery_VATBase2 = 0,
										   Delivery_Taxable = 0,
										   Delivery_TaxableBase1 = 0,
										   Delivery_TaxableBase2 = 0,
										   Delivery_NoneTaxable = 0,
										   Delivery_NoneTaxableBase1 = 0,
										   Delivery_NoneTaxableBase2 = 0,
										   Delivery_Cash = 0,
										   Delivery_CashBase1 = 0,
										   Delivery_CashBase2 = 0,
										   Delivery_CashInBase1 = 0,
										   Delivery_CashInBase2 = 0,
			              where delivery_id = '$returnrmarma_dreturnid'";
		$result_update = mysql_query($query_update, $connection);
		
		$query_delete  = "DELETE FROM DELIVERYDT WHERE DELIVERY_ID='$returnrmarma_dreturnid'";
		$result_delete = mysql_query($query_delete, $connection); 
	}
  }
  $ParSreturnid = $returnrmarma_dreturnid;
  $ParSaleid    = $returnrmarma_deliveryid;
  
  $query_delivery1  = "select count(*) as AvailableDelivery1 from deliveries where delivery_id= '$returnrmarma_dreturnid'";
  $result_delivery1 = mysql_query($query_delivery1, $connection); 
  $row_delivery1    = mysql_fetch_array($result_delivery1);
  extract($row_delivery1);
  
  if($AvailableDelivery1 == "" || $AvailableDelivery1 == 0){
	  $ParSreturnid = 0;
  }
  
  $query_delivery2  = "select count(*) as AvailableDelivery2 from deliveries where delivery_id = '$returnrmarma_deliveryid'";
  $result_delivery2 = mysql_query($query_delivery2, $connection); 
  $row_delivery2    = mysql_fetch_array($result_delivery2);
  extract($row_delivery2);
  
  if($AvailableDelivery2 == "" || $AvailableDelivery2 == 0){
	  $ParSaleid = 0;
  }
  
  $query_returnrmadt  = "select rmadt_id as returnrmarmadt_id, Project_code as ReturnRmaDtProject_Code, Costcent_code as ReturnRmaDtCostcent_code, RMADt_Repaire as ReturnRmadtRMADt_Repaire, Item_code as returnrmadtitem_Code, Warehouse_code as ReturnRmadtRMADT_SRETURNWAREHOUSE, rmadt_price as returnrmadtRMADT_SRETURNPRICE
                        from rmadt
						where rma_id = '$returnrmarma_id'";
 // echo $query_returnrmadt;exit;
  $result_returnrmadt = mysql_query($query_returnrmadt, $connection);
  while($row_returnrmadt = mysql_fetch_array($result_returnrmadt)){
	  extract($row_returnrmadt);
	 // echo $ReturnRmadtRMADT_SRETURNWAREHOUSE;exit;
	  if($ModuleProject == 1 && $ReturnRmaDtProject_Code == "" && $ModuleCostcenter == 1 && $ReturnRmaDtCostcent_code == ""){
		  $query_updatermadt = "update rmadt set Project_code = '$ReturnRmaDtProject_Code',
		                                         Costcent_code = '$ReturnRmaDtCostcent_code',
												 Client_Code = '$ReturnRmaClient_code'
								where rmadt_id = '$returnrmarmadt_id'";
	  }
	  else
	  if($ModuleProject == 1 && $ReturnRmaDtProject_Code == ""){
		  $query_updatermadt = "update rmadt set Project_code = '$ReturnRmaProject_code', Client_Code = '$ReturnRmaClient_code' where rmadt_id = '$returnrmarmadt_id'";
	  }
	  else
	  if($ModuleCostcenter == 1 && $ReturnRmaDtCostcent_code == ""){
		  $query_updatermadt = "update rmadt set Costcent_code = '$ReturnRmaDtCostcent_code', Client_Code = '$ReturnRmaClient_code' where rmadt_id = '$returnrmarmadt_id'";
	  }
	  
	  $result_updatermadt = mysql_query($query_updatermadt, $connection);
	//ECHO $ModuleReturnRMATOSales." ".$ReturnRmadtRMADt_Repaire." ".$returnrmadtitem_Code." ".$ReturnRmadtRMADT_SRETURNWAREHOUSE;
	//EXIT;
	  if($ModuleReturnRMATOSales == 1 && $ReturnRmadtRMADt_Repaire == "U"){
		  // echo "test";exit;
		 // echo $ReturnRmadtRMADT_SRETURNWAREHOUSE;exit;
		  if($returnrmadtitem_Code != "" && $ReturnRmadtRMADT_SRETURNWAREHOUSE != ""){
			  //echo "test";exit;
			  $VarVat = "1";
			  $query_rmadtrpc  = "select rmadtrpc_id, item_code as RMADTRCPitem_code
			                      from rmadtrpc
								  where rmadt_id = '$returnrmarmadt_id'";
			//echo $query_rmadtrpc;exit;
			  $result_rmadtrpc = mysql_query($query_rmadtrpc, $connection);
			  while($row_rmadtrpc = mysql_fetch_array($result_rmadtrpc)){
				  extract($row_rmadtrpc);
				 // echo $RMADTRCPitem_code."-".$ReturnRmadtRMADT_SRETURNWAREHOUSE;exit;
				  if($RMADTRCPitem_code != "" && $ReturnRmadtRMADT_SRETURNWAREHOUSE != ""){
					  //echo "test";exit;
					  $query_createdelivery  = "SELECT Create_Auto_Delivery_DI_DR('$ParSreturnid','DR',now(), '$ReturnRmaClient_code', '$returnrmacurrency_Code', 
					                            '$returnrmadtitem_Code', '', '$returnrmadtRMADT_SRETURNPRICE', '0', 
											    '$ReturnRmadtRMADT_SRETURNWAREHOUSE', '1', 
											    'AUTO RETURN RMA No. $ReturnRmaRMA_Reference', '', '0', '$VarVat') as id FROM DUAL";
					  //echo $query_createdelivery;exit;
					  $result_createdelivery = mysql_query($query_createdelivery, $connection);
					  $row_createdelivery    = mysql_fetch_array($result_createdelivery);
					  extract($row_createdelivery);
					  //echo $id; exit;

					  $ParSreturnid = $id;
				  }
				  
				  
				  
			  }
			  $query_update  = "update rma set rma_dreturnid = '$id' where rma_id='$returnrmarma_id'";
		      $result_update = mysql_query($query_update, $connection); 
			  
			  $ParSreturnid = $id;
			 // echo $ParSreturnid;exit;
		  }
		      $query_rmadtrpc  = "select rmadtrpc_id, item_code as RMADTRCPitem_code, rmadtrpc_price as RMADTRCPrmadtrpc_price, warehouse_code as RMADTRCPwarehouse_code, rmadtrpc_quantity as RMADTRCPrmadtrpc_quantity
			                      from rmadtrpc
								  where rmadt_id = '$returnrmarmadt_id'";
			  $result_rmadtrpc = mysql_query($query_rmadtrpc, $connection);
			  $VarVat = "1";
			  while($row_rmadtrpc = mysql_fetch_array($result_rmadtrpc)){
				  extract($row_rmadtrpc);
				  $query_create  = "SELECT Create_Auto_Delivery_DI_DR('$ParSaleid','DI',now(),'$ReturnRmaClient_code','$returnrmacurrency_Code',
				                    '$RMADTRCPitem_code', '', '$RMADTRCPrmadtrpc_price', '0', '$RMADTRCPwarehouse_code', '$RMADTRCPrmadtrpc_quantity',
								    'AUTO RETURN RMA No. $ReturnRmaRMA_Reference', '', '0', '$VarVat') as id from dual";
				 // echo $query_create;exit;
				  $result_create = mysql_query($query_create, $connection);
				  $row_create    = mysql_fetch_array($result_create);
				  extract($row_create);
				  $ParSaleid = $id;
			  }
		      $query_update  = "update rma set rma_deliveryid='$id' where rma_id='$returnrmarma_id'";
			  $result_update = mysql_query($query_update, $connection); 
			  $ParSaleid = $id;
			  
			  
	  }
  
  }
  if($moduledelivery == 1){
	  $query_selectref  = "SELECT RMA_REFERENCE FROM RMadt,RMA  WHERE RMadt.RMA_ID=RMA.RMA_ID  AND  RMAdt_ID='$ReturnRmadtMI_ID'";
	  //echo $query_selectref;exit;
	  $result_selectref = mysql_query($query_selectref, $connection);
	  $row_selectref    = mysql_fetch_array($result_selectref);
	  extract($row_selectref);
	  
	  $Description = "RMA Reference: $RMA_REFERENCE";
	 // echo $ParSreturnid."-".$ReturnRmadtRMATDT_RETURN;exit;
	 //echo $ReturnRmadtRMATDT_RETURN;exit;
	  if(($ParSreturnid != "" && $ParSreturnid != 0) && $ReturnRmadtRMATDT_RETURN == 1){
		  $query_sales  = "select sale_id,sale_reference,journal_id from sales where journal_type='SR' and sale_id='$returnrmarma_sreturnid'";
		  $result_sales = mysql_query($query_sales, $connection); 
		  $number_sales = mysql_num_rows($result_sales);
		  $row_sales    = mysql_fetch_array($result_sales);
		  extract($row_sales);
		 // echo $number_sales."-".$returnrmarma_saleid;exit;
		  if($number_sales == 0 || ($returnrmarma_saleid != "" && $returnrmarma_saleid != 0)){
			  $query_saleid  = "select nextsequence('sale_id','') sales_id from dual";
			  $result_saleid = mysql_query($query_saleid, $connection);
			  $row_saleid    = mysql_fetch_array($result_saleid);
			  extract($row_saleid);
			  
			  $SaleId = $sales_id;
			  
			  $query_saleref  = "select nextsequence('sales_reference','') sale_reference from dual";
			  //echo $query_saleref;exit;
			  $result_saleref = mysql_query($query_saleref, $connection);
              $row_saleref    = mysql_fetch_array($result_saleref);
              extract($row_saleref);
              
              $saleReference = 	$sale_reference;			  
		  }
		  else{
			  $SaleId = $sale_id;
			  $saleReference = $sale_reference;
			  if($journal_id == "")
				  $JVID = 'null';
			  else
				  $JVID = $journal_id;
			  
			  $query_delete  = "delete from sales where journal_type='SR'  and sale_id='$returnrmarma_sreturnid'";
			  $result_delete = mysql_query($query_delete, $connection); 
		  }
		  
		  $query_insert = "insert into sales (Sale_id,Sale_Reference,Sale_Date,Sale_Remark,Client_code,Salesman_Code,Currency_code, 
              project_code,costcenter_code, Sale_CreditCur,Sale_CreditAccount,Sale_CashCur,Sale_CashAccount,journal_type,
              JournalDt_ValueDate,User_Account,Sale_Vataccount,Sale_creditaccountvat,Sale_RelatedId,sale_rate,
              sale_ratebase1,sale_ratebase2,Sales_BelongsToGroup,sorder_reference,sale_chkvat,sale_discountper,
              sale_mode
              ,sale_discountamount,sale_discountbase1,sale_discountbase2,sale_total,sale_totalbase1,sale_totalbase2,
              sale_cash,sale_cashinbase1,sale_cashinbase2,sale_cashoutbase1,sale_cashoutbase2,sale_tipsaccount,
              sale_vat,sale_vatbase1,sale_vatbase2,sale_taxable,sale_taxablebase1,sale_taxablebase2,
              sale_nonetaxable,sale_nonetaxablebase1,sale_nonetaxablebase2,journal_id)
			  select '$SaleId','$saleReference',delivery_date,'$Description',client_code,salesman_code,currency_code,
              project_code,costcent_code, delivery_CreditCur,delivery_CreditAccount,delivery_CashCur,delivery_CashAccount,
              if(journal_type='DI','SI','SR'),
              JournalDt_ValueDate,User_Account,delivery_Vataccount,delivery_creditaccountvat,delivery_Id,delivery_rate,
              delivery_ratebase1,delivery_ratebase2,delivery_BelongsToGroup,null,Delivery_VatChkbx,delivery_discountper,
              delivery_mode
              ,delivery_discountamount,delivery_discountbase1,delivery_discountbase2,delivery_total,delivery_totalbase1,
              delivery_totalbase2,
              delivery_cash,delivery_cashinbase1,delivery_cashinbase2,delivery_cashoutbase1,delivery_cashoutbase2,delivery_tipsaccount,
              delivery_vat,delivery_vatbase1,delivery_vatbase2,delivery_taxable,delivery_taxablebase1,delivery_taxablebase2,
              delivery_nonetaxable,delivery_nonetaxablebase1,delivery_nonetaxablebase2,'".$JVID."'
               from deliveries where delivery_id='$ParSreturnid'";
			//echo $query_insert."<br>";
			$result_insert = mysql_query($query_insert, $connection);
			
			$query_deletedt  = "delete from salesdt where sale_id='$SaleId'";
			$result_deletedt = mysql_query($query_deletedt, $connection); 
		    
			$query_insertdt  = "insert into salesdt (saledt_id,Sale_id,relatedDt_id,Client_Code,sale_Date,Journal_Type, 
                                                  Project_Code,CostCenter_Code,Warehouse_Code,Item_code,saleDt_Quantity,
                                                  Currency_code,SaleDt_Price,SaleDt_PriceBase1,SaleDt_PriceBase2, 
                                                  Item_Vat,
                                                  saleDt_DiscountPer,saledt_DirectStkDbt,saledt_DirectStkCrd, 
                                                  saledt_DirectStkCur,saledt_ItemAccount,saledt_discountacc,saledt_BelongsToGroup,
                                                  Salesdt_Remark,saledt_vataccount)
							    select nextsequence('saledt_id',''),'$SaleId',DeliveryDt_id,Client_Code,Delivery_Date,if(journal_type='DI','SI','SR'), 
                                                  Project_Code,CostCent_Code,Warehouse_Code,Item_code,DeliveryDt_Quantity,
                                                  Currency_code,DeliveryDt_Price,DeliveryDt_PriceBase1,DeliveryDt_PriceBase2, 
                                                  Item_Vat,
                                                  DeliveryDt_DiscountPer,Deliverydt_DirectStkDbt,Deliverydt_DirectStkCrd, 
                                                  Deliverydt_DirectStkCur,Deliverydt_ItemAccount,deliverydt_discountacc,Deliverydt_BelongsToGroup,
                                                  Deliverydt_remark,deliverydt_vataccount from deliverydt where delivery_id='$ParSreturnid'";
		   $result_inserdt = mysql_query($query_insertdt, $connection);
		   
		   if($SaleId != 0 && $SaleId != ""){
			   $query_update  = "update rma set  rma_sreturnid='$SaleId' where rma_id='$returnrmarma_id'";
			   $result_update = mysql_query($query_update, $connection); 
		   }
		   
		   if($ModulePostToJournal == 1){
			   
			   
			   $query_companyYear  = "select DefaultYear as PCompYear, user_name as UserName from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
			   $result_companyYear = mysql_query($query_companyYear, $connection); 
			   $row_companyYear    = mysql_fetch_array($result_companyYear);
			   extract($row_companyYear);
			   
			   $query_belongstogroupgrp  = "select login_belongstogrp as BelongsToGroupGrp from ".$_SESSION['dbLabel'].".login where User_account='".$_SESSION['useraccount']."'";
			   $result_belongstogroupgrp = mysql_query($query_belongstogroupgrp, $connection); 
			   $row_belongstogroupgrp    = mysql_fetch_array($result_belongstogroupgrp);
			   extract($row_belongstogroupgrp);
			   fbegin();
			   //$query_starttransaction  = "start transaction;";
			  // $result_starttransaction = mysql_query($query_starttransaction, $connection);
			   $resultvars = mysql_query("set @JvId :=0 ", $connection);
			   $query_createAutoJv  = "call Create_SalesAutoJv('$SaleId', false, '$PCompYear', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."',
			                           '$UserName', '$BelongsToGroupGrp', @JvId)";
			   //echo $query_createAutoJv."<br>";
			   $result_createAutoJv = mysql_query($query_createAutoJv, $connection);//echo $result_createAutoJv;exit;
			    fcommit();
			   $query_select_jv  = "select @JvId as JvID from dual";
			   $result_select_jv = mysql_query($query_select_jv, $connection); 
			  $row_select_jv    = mysql_fetch_array($result_select_jv);
			   extract($row_select_jv);
			   //echo $JvID;exit;
			   $query_update  = "update sales set journal_id='$JvID' where sale_id='$SaleId'";
			  // echo $query_select_jv."<br>";
			   $result_update = mysql_query($query_update, $connection); 
			   
			   //$query_commit  = "commit;";
			   //$result_commit = mysql_query($query_commit, $connection);
			   fcommit();
			   //exit;
		   }
	  }
	  if($ParSaleid != 0 && $ParSaleid != ""){
		  $query_selectsale  = "select sale_id,sale_reference,journal_id from sales where  journal_type='SI'  and sale_id='$returnrmarma_saleid'";
		  $result_selectsale = mysql_query($query_selectsale, $connection); 
		  $number_selectsale = mysql_num_rows($result_selectsale);
		  $row_selectsale    = mysql_fetch_array($result_selectsale);
		  extract($row_selectsale);
		  
		  if($number_selectsale == 0 || $returnrmarma_saleid == 0){
			  $query_nextsequence  = "select nextsequence('sale_id','') sales_id from dual";
			  $result_nextsequence = mysql_query($query_nextsequence, $connection); 
			  $row_nextsequence    = mysql_fetch_array($result_nextsequence);
			  extract($row_nextsequence);
			  
			  $query_saleref  = "select nextsequence('Sreturn_Reference','') sale_reference from dual";
			  //echo $query_saleref;exit;
			  $result_saleref = mysql_query($query_saleref, $connection);
              $row_saleref    = mysql_fetch_array($result_saleref);
              extract($row_saleref);
              
              $saleReference = 	$sale_reference;
			  
			  $SaleId = $sales_id;
		  }
		  else{
			 $SaleId = $sale_id;
			 $saleReference = $sale_reference;
			 if($journal_id != ""){
				 $JVID = 'null';
			 }
			 else{
				 $JVID = $journal_id;
			 }
			 $query_delete  = "delete from sales where journal_type='SI'  and sale_id='$returnrmarma_saleid'";
			 $result_delete = mysql_query($query_delete, $connection); 
		  }
		  $query_insert  = "insert into sales (Sale_id,Sale_Reference,Sale_Date,Sale_Remark,Client_code,Salesman_Code,Currency_code,
		                    project_code,costcenter_code, Sale_CreditCur,Sale_CreditAccount,Sale_CashCur,Sale_CashAccount,journal_type,
						    JournalDt_ValueDate,User_Account,Sale_Vataccount,Sale_creditaccountvat,Sale_RelatedId,sale_rate,
						    sale_ratebase1,sale_ratebase2,Sales_BelongsToGroup,sorder_reference,sale_chkvat,sale_discountper,
						    sale_mode,sale_discountamount,sale_discountbase1,sale_discountbase2,sale_total,sale_totalbase1,sale_totalbase2,
						    sale_cash,sale_cashinbase1,sale_cashinbase2,sale_cashoutbase1,sale_cashoutbase2,sale_tipsaccount,
						    sale_vat,sale_vatbase1,sale_vatbase2,sale_taxable,sale_taxablebase1,sale_taxablebase2,
						    sale_nonetaxable,sale_nonetaxablebase1,sale_nonetaxablebase2,journal_id)
						    select '$SaleId', '$saleReference', delivery_date, '$Description', client_code,salesman_code,currency_code,
						    project_code,costcent_code, delivery_CreditCur,delivery_CreditAccount,delivery_CashCur,delivery_CashAccount,
						    if(journal_type='DI','SI','SR'), JournalDt_ValueDate,User_Account,delivery_Vataccount,delivery_creditaccountvat,delivery_Id,delivery_rate,
						    delivery_ratebase1,delivery_ratebase2,delivery_BelongsToGroup,null,Delivery_VatChkbx,delivery_discountper, delivery_mode,
						    delivery_discountamount,delivery_discountbase1,delivery_discountbase2,delivery_total,delivery_totalbase1,
						    delivery_totalbase2, delivery_cash,delivery_cashinbase1,delivery_cashinbase2,delivery_cashoutbase1,delivery_cashoutbase2,delivery_tipsaccount,
						    delivery_vat,delivery_vatbase1,delivery_vatbase2,delivery_taxable,delivery_taxablebase1,delivery_taxablebase2,
						    delivery_nonetaxable,delivery_nonetaxablebase1,delivery_nonetaxablebase2, '$JVID'
						    from deliveries 
							where delivery_id='$ParSaleid'";
			//echo $query_insert."<br>";
			$result_insert = mysql_query($query_insert, $connection);
			
			$query_delete  = "delete from salesdt where sale_id= '$SaleId'";
			$result_delete = mysql_query($query_delete, $connection);

            $query_insertdt  = "insert into salesdt (saledt_id,Sale_id,relatedDt_id,Client_Code,sale_Date,Journal_Type,
			                    Project_Code,CostCenter_Code,Warehouse_Code,Item_code,saleDt_Quantity,
							    Currency_code,SaleDt_Price,SaleDt_PriceBase1,SaleDt_PriceBase2, 
							    Item_Vat, saleDt_DiscountPer,saledt_DirectStkDbt,saledt_DirectStkCrd,
							    saledt_DirectStkCur,saledt_ItemAccount,saledt_discountacc,saledt_BelongsToGroup, Salesdt_Remark,saledt_vataccount)
							    select nextsequence('saledt_id',''), '$SaleId',DeliveryDt_id,Client_Code,Delivery_Date,if(journal_type='DI','SI','SR'),
							    Project_Code,CostCent_Code,Warehouse_Code,Item_code,DeliveryDt_Quantity, Currency_code,DeliveryDt_Price,DeliveryDt_PriceBase1,DeliveryDt_PriceBase2,
							    Item_Vat, DeliveryDt_DiscountPer,Deliverydt_DirectStkDbt,Deliverydt_DirectStkCrd, Deliverydt_DirectStkCur,Deliverydt_ItemAccount,deliverydt_discountacc,Deliverydt_BelongsToGroup,
							    Deliverydt_remark,deliverydt_vataccount  from deliverydt where delivery_id='$ParSaleid'";	
           $result_insertdt = mysql_query($query_insertdt, $connection);

           if($SaleId != 0 || $SaleId != ""){
			   $query_update  = "update rma set  rma_saleid='$SaleId' where rma_id='$returnrmarma_id'";
			   $result_update = mysql_query($query_update, $connection); 
		   }

           if($ModulePostToJournal == 1){
			   
			   $query_companyYear  = "select DefaultYear as PCompYear, user_name as UserName from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
			   $result_companyYear = mysql_query($query_companyYear, $connection); 
			   $row_companyYear    = mysql_fetch_array($result_companyYear);
			   extract($row_companyYear);
			   
			   $query_belongstogroupgrp  = "select login_belongstogrp as BelongsToGroupGrp from ".$_SESSION['dbLabel'].".login where User_account='".$_SESSION['useraccount']."'";
			   $result_belongstogroupgrp = mysql_query($query_belongstogroupgrp, $connection); 
			   $row_belongstogroupgrp    = mysql_fetch_array($result_belongstogroupgrp);
			   extract($row_belongstogroupgrp);
			   
			   
			   $query_starttransaction  = "start transaction";
			   $result_starttransaction = mysql_query($query_starttransaction, $connection);
			   
			   $query_createautojv  = "call Create_SalesAutoJv('$SaleId', false, '$PCompYear', '".$_SESSION['dbLabel']."',
			                           '".$_SESSION['useraccount']."', '".$UserName."', '$BelongsToGroupGrp', @JvID)";
			   $result_createautojv = mysql_query($query_createautojv, $connection);
			   
			   $query_select_jv  = "select @JvID as JvID from dual";
			   $result_select_jv = mysql_query($query_select_jv, $connection); 
			   $row_select_jv    = mysql_fetch_array($result_select_jv);
			   extract($row_select_jv);
			   
			   $query_update  = "update sales set journal_id='$JvID' where sale_id='$SaleId'";
			   $result_update = mysql_query($query_update, $connection);
		   }			   
	  }
  }
  
  if($returnrmarma_dreturnid != 0 && $returnrmarma_dreturnid != "" && $moduledelivery != 1){
	  $query_companyYear  = "select DefaultYear as PCompYear, user_name as UserName from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
	  $result_companyYear = mysql_query($query_companyYear, $connection); 
	  $row_companyYear    = mysql_fetch_array($result_companyYear);
	  extract($row_companyYear);
			   
	  $query_belongstogroupgrp  = "select login_belongstogrp as BelongsToGroupGrp from ".$_SESSION['dbLabel'].".login where User_account='".$_SESSION['useraccount']."'";
	  $result_belongstogroupgrp = mysql_query($query_belongstogroupgrp, $connection); 
	  $row_belongstogroupgrp    = mysql_fetch_array($result_belongstogroupgrp);
	  extract($row_belongstogroupgrp);
	  $query_createdeliveryjv  = "call Create_DeliveryAutoJv('$returnrmarma_dreturnid',1,'$PCompYear','".$_SESSION['dbLabel']."','".$_SESSION['useraccount']."','$UserName','$BelongsToGroupGrp',@PJvId)";
	  $result_createdeliveryjv = mysql_query($query_createdeliveryjv, $connection);
  }
  
  if($returnrmarma_deliveryid != 0 && $returnrmarma_deliveryid != "" && $moduledelivery != 1){
	  $query_companyYear  = "select DefaultYear as PCompYear, user_name as UserName from ".$_SESSION['dbLabel'].".users where User_account = '".$_SESSION['useraccount']."'";
	  $result_companyYear = mysql_query($query_companyYear, $connection); 
	  $row_companyYear    = mysql_fetch_array($result_companyYear);
	  extract($row_companyYear);
			   
	  $query_belongstogroupgrp  = "select login_belongstogrp as BelongsToGroupGrp from ".$_SESSION['dbLabel'].".login where User_account='".$_SESSION['useraccount']."'";
	  $result_belongstogroupgrp = mysql_query($query_belongstogroupgrp, $connection); 
	  $row_belongstogroupgrp    = mysql_fetch_array($result_belongstogroupgrp);
	  extract($row_belongstogroupgrp);
	  $query_createdeliveryjv  = "call Create_DeliveryAutoJv('$returnrmarma_deliveryid',1,'$PCompYear','".$_SESSION['dbLabel']."','".$_SESSION['useraccount']."','$UserName','$BelongsToGroupGrp',@PJvId)";
	  $result_createdeliveryjv = mysql_query($query_createdeliveryjv, $connection);
  }
  
}
function addToAdjustment($connection, $RMA_Id){
	$query_sequence  = "select nextsequence('ADJUSTMENT_ID', '') as NewAdjustmentId, nextsequence('ADJUSTMENT_REFERENCE', '') as NewAdjustmentReference
	                   from dual";
    $result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	
	$query_adjustmentsetup  = "select CurrencyBase from adjustmentsetup";
	$result_adjustmentsetup = mysql_query($query_adjustmentsetup, $connection);
	$row_adjustmentsetup    = mysql_fetch_array($result_adjustmentsetup);
	extract($row_adjustmentsetup);
	
	$query_setup  = "select base1, base2 from setup";
	$result_setup = mysql_query($query_setup, $connection);
	$row_setup    = mysql_fetch_array($result_setup);
	extract($row_setup);
	
	if($CurrencyBase == 1){
		$Currency_Code = $base1;
	}
	else{
		$Currency_Code = $base2;
	}
	
	$query_rma  = "select RMA_Reference, rma.Project_Code as RMAProjectCode, rma.Costcent_code as RMACostcentCode, RMA_Date as RMADate, Adjustment_Id as RMAAdjustmentId, clients.ledger_name as ClientName, RMA_BelongsToGroup as RMABelongsToGroup
	               from rma inner join clients on clients.ledger_number = rma.client_code
				   where rma_id = '$RMA_Id'";
	//echo $query_rma;exit;
	$result_rma = mysql_query($query_rma, $connection);
	$row_rma    = mysql_fetch_array($result_rma);
	extract($row_rma);
	if($RMAProjectCode == ""){
		$RMAProjectCode = 'null';
	}
	if($RMACostcentCode == ""){
		$RMACostcentCode = 'null';
	}
	if($RMAAdjustmentId == "" || $RMAAdjustmentId == 0){
    $Adjustment_Remak = "RMA Reference: $RMA_Reference - Client Name: $ClientName";
	$query_insert  = "insert into adjustment (Adjustment_Id, Adjustment_Reference, Adjustment_Date, Adjustment_posted,
	                  Currency_Code, Project_Code, Costcent_Code, Adjustment_Remak, Adjustment_BelongsToGroup, Adjustment_Type, Adjustment_EntryType, User_account) values ('$NewAdjustmentId', '$NewAdjustmentReference',
					 '$RMADate', '0', '$Currency_Code', ".$RMAProjectCode.", ".$RMACostcentCode.", '$Adjustment_Remak', '$RMABelongsToGroup', 'AD', 'A', '".$_SESSION['useraccount']."')";
	//echo $query_insert;exit;
	$result_insert = mysql_query($query_insert, $connection);
	
	$query_updaterma  = "update rma set Adjustment_Id = '$NewAdjustmentId' where rma_id = '$RMA_Id'";
	$result_updaterma = mysql_query($query_updaterma, $connection);
	
	$MainAdjustmentId = $NewAdjustmentId;
	}
	else{
		$query_update  = "update adjustment set Adjustment_posted = 0,
		                                        Adjustment_Date = '$RMADate',
		                                        Currency_Code = '$Currency_Code',
											    Project_Code = ".$RMAProjectCode.",
											    Costcent_Code = ".$RMACostcentCode."
		                 where adjustment_id = '$RMAAdjustmentId'";
		$result_update = mysql_query($query_update, $connection);
		
		$query_adjustmentdt  = "select adjustmentdt_id, journal_type from adjustmentdt where adjustment_id = '$RMAAdjustmentId'";
		$result_adjustmentdt = mysql_query($query_adjustmentdt, $connection);
		while($row_adjustmentdt = mysql_fetch_array($result_adjustmentdt)){
			extract($row_adjustmentdt);
			$query_deletepurchase  = "delete from purchasesserialno where PurchaseDt_ID = '$adjustmentdt_id' and journal_type = '$journal_type'";
		    $result_deletepurchase = mysql_query($query_deletepurchase, $connection);
		}
		
		//$query_deletepurchase  = "delete from purchasesserialno where PurchaseDt_ID in (select adjustmentdt_id from adjustmentdt where adjustment_id = '$RMAAdjustmentId')";
		//$result_deletepurchase = mysql_query($query_deletepurchase, $connection);
		
		$query_delete = "delete from adjustmentdt where adjustment_id = '$RMAAdjustmentId'";
		$result_delete = mysql_query($query_delete, $connection);
		
		$MainAdjustmentId = $RMAAdjustmentId;
	}
	
	$query_rmadt  = "select rmadt.item_code, rmadt.RMADt_Quantity, rmadt.Warehouse_code, rmadt.Project_code,
	                 rmadt.Costcent_code, rma.rma_date, Serial_Number, rmadt.warehouse_code, rmadt.RMADt_Quantity, rma.currency_code
					 from rmadt inner join rma on rma.rma_id = rmadt.rma_id
					 where rmadt.rma_id = '$RMA_Id'";
	$result_rmadt = mysql_query($query_rmadt, $connection);
	
	$query_setup  = "select base1, base2 from setup";
	$result_setup = mysql_query($query_setup, $connection);
    $row_setup    = mysql_fetch_array($result_setup);
    extract($row_setup);

    $query_module  = "select ifNull(Module_rawmaterial, 0) as Module_rawmaterial, ifNull(Module_directstock, 0) as Module_directstock, ifNull(Module_DirectStockSO, 0) as Module_DirectStockSO from module";
    $result_module = mysql_query($query_module, $connection); 
    $row_module    = mysql_fetch_array($result_module);
    extract($row_module);	
	
	while($row_rmadt = mysql_fetch_array($result_rmadt)){
		extract($row_rmadt);
		$query_cost  = "select round(MeanCostCur('$item_code', '$rma_date', 1, '$base1', '$currency_code'), 2) as AdjustmentDt_Price, 
		                       round(MeanCostCur('$item_code', '$rma_date', 1, '$base1', '$base1'), 2) as AdjustmentDtBase1, 
						 	   round(MeanCostCur('$item_code', '$rma_date', 1, '$base1', '$base2'), 2) as AdjustmentDtBase2 
						   	   from dual";
		//echo $query_cost;exit;
		$result_cost = mysql_query($query_cost, $connection);
	    $row_cost    = mysql_fetch_array($result_cost);
		extract($row_cost);
		if($Project_code == ""){
			$Project_code = 'null';
		}
		else{
			$Project_code = "'$Project_code'";
		}
		if($Costcent_code == ""){
			$Costcent_code = 'null';
		}
		else{
			$Costcent_code = "'$Costcent_code'";
		}
		if($Warehouse_code == ""){
			$Warehouse_code = 'null';
		}
		else{
			$Warehouse_code = "'$Warehouse_code'";
		}
		$query_sequence  = "select nextsequence('adjustmentdt_id', '') as adjustmentdt_id from dual";
		$result_sequence = mysql_query($query_sequence, $connection);
		$row_sequence    = mysql_fetch_array($result_sequence);
		extract($row_sequence);
		if($RMADt_Quantity > 0){
			$Journal_Type = "AI";
		}
		else{
			$Journal_Type = "AR";
		}
		if($Module_rawmaterial == 0){
			$query_adjustmentsetup  = "select DebitAccount as DbAccount, CreditAccount as CdAccount, CurrencyBase from adjustmentsetup";
			$result_adjustmentsetup = mysql_query($query_adjustmentsetup, $connection); 
			$row_adjustmentsetup    = mysql_fetch_array($result_adjustmentsetup); 
			extract($row_adjustmentsetup);
			
			if($CurrencyBase == 1){
				$CurrencyCode = $base1;
			}
			else{
				$CurrencyCode = $base2;
			}
     			
		}
		else{
			$query_groupcode  = "select group_code as GroupCode from items where item_code = '$item_code'";
			$result_groupcode = mysql_query($query_groupcode, $connection); 
			$row_groupcode    = mysql_fetch_array($result_groupcode);
			extract($row_groupcode);
			
			$query_ledgers  = "select Trans_DirectStkDbt as DbAccount, Trans_DirectStkCrd as CdAccount, Trans_DirectStkCur as CurrencyCode
			                   from transactionsetup
							   where group_code = '$GroupCode'";
			$result_ledgers = mysql_query($query_ledgers, $connection);
			$row_ledgers    = mysql_fetch_array($result_ledgers);
			extract($row_ledgers);
		}
		$query_insertdt  = "insert into adjustmentdt (adjustmentdt_id, adjustment_id, item_code, AdjustmentDt_Quantity, AdjustmentDt_PhysicalQty, AdjustmentDt_ExistingQty,
		                    Warehouse_Code, Project_code, Costcent_Code, Adjustment_Date, Journal_Type, AdjustmentDt_Price, AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2, AdjustmentDt_DirectStkDbt, AdjustmentDt_DirectStkCrd, AdjustmentDt_DirectStkCur) 
							values ('$adjustmentdt_id', '$MainAdjustmentId', '$item_code', '$RMADt_Quantity', '$RMADt_Quantity',
						    '0', ".$Warehouse_code.", ".$Project_code.", ".$Costcent_code.", '$rma_date', '$Journal_Type', '$AdjustmentDt_Price', '$AdjustmentDtBase1', '$AdjustmentDtBase2', '$DbAccount', '$CdAccount', '$CurrencyCode')";
		//echo $query_insertdt;exit;
		$result_insertdt = mysql_query($query_insertdt, $connection);
		//echo $Serial_Number;exit;
		if($Serial_Number != ""){
			$query_sequence  = "select serial_id from sequence";
			$result_sequence = mysql_query($query_sequence, $connection);
			$row_sequence    = mysql_fetch_array($result_sequence);
			extract($row_sequence);
			$query_insertserial  = "insert into purchasesserialno (Serial_Id, SerialNo_Code, PurchaseDt_ID, Item_Code, SerialNo_Used, Journal_type) 
			                        values ('$serial_id', '$Serial_Number', '$adjustmentdt_id', '$item_code', '0', '$Journal_Type')";
			//echo $query_insertserial;exit;
			$result_insertserial = mysql_query($query_insertserial, $connection);
			$query_updateseq  = "update sequence set serial_id = serial_id + 1";
			$result_updateseq = mysql_query($query_updateseq, $connection); 
		}
	}
	
	$query_total  = "select sum(AdjustmentDt_Price) as TotalPrice, 
	                 sum(AdjustmentDt_PriceBase1) as TotalPriceBase1, 
					 sum(AdjustmentDt_PriceBase2) as TotalPriceBase2
					 from adjustmentdt
					 where adjustment_id = '$MainAdjustmentId'";
	$result_total = mysql_query($query_total, $connection);
	$row_total    = mysql_fetch_array($result_total);
	extract($row_total);
	
	$query_update = "update adjustment set adjustment_amount = '$TotalPrice',
	                                       adjustment_amountbase1 = '$TotalPriceBase1',
										   adjustment_amountbase2 = '$TotalPriceBase2'
					 where adjustment_id = '$MainAdjustmentId'";
	$result_update = mysql_query($query_update, $connection);
	
	$query_user  = "select User_Name, login_belongstogrp from ".$_SESSION['dbLabel'].".login where User_account = '".$_SESSION['useraccount']."' and Login_Schema = '".strtoupper($_SESSION['dbName'])."'";
	//echo $query_user;exit;
	$result_user = mysql_query($query_user, $connection); 
	$row_user    = mysql_fetch_array($result_user);
	extract($row_user);
	
	if($Module_directstock == 1 || $Module_DirectStockSO == 1){
	
	$query_startTransaction  = "start transaction";
	$result_startTransaction = mysql_query($query_startTransaction, $connection); 
	
	$query_startJv  = "SET @JvId:=''";
	$result_startJv = mysql_query($query_startJv, $connection); 
	
	$query_callprocedure  = "call Create_AdjustmentAutoJv('$MainAdjustmentId', 1, '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '$User_Name', '$login_belongstogrp', @JvId)";
	//echo $query_callprocedure;exit;
	$result_callprocedure = mysql_query($query_callprocedure, $connection); 
	
	$query_jvid  = "select @JvId as JvID from dual";
	$result_jvid = mysql_query($query_jvid, $connection); 
	$row_jvid    = mysql_fetch_array($result_jvid);
	extract($row_jvid);
	
	$query_updateadjustment  = "update adjustment set Journal_id = '$JvID' where adjustment_id = '$MainAdjustmentId'";
	$result_updateadjustment = mysql_query($query_updateadjustment, $connection); 
	
	}
	
}
function addToAdjustment2($connection, $RMA_Id){
	$query_module  = "select ifNull(Module_rawmaterial, 0) as Module_rawmaterial, ifNull(Module_directstock, 0) as Module_directstock, ifNull(Module_DirectStockSO, 0) as Module_DirectStockSO from module";
    $result_module = mysql_query($query_module, $connection); 
    $row_module    = mysql_fetch_array($result_module);
    extract($row_module);
	
	$query_rma  = "select adjustment_id as AdjustmentId from rma where rma_id = '$RMA_Id'";
	$result_rma = mysql_query($query_rma, $connection);
	$row_rma    = mysql_fetch_array($result_rma);
	extract($row_rma);
	
	if($AdjustmentId == "" || $AdjustmentId == 0){
		$query_sequence  = "select nextsequence('ADJUSTMENT_ID', '') as NewAdjustmentId, nextsequence('ADJUSTMENT_REFERENCE', '') as NewAdjustmentReference
	                        from dual";
        $result_sequence = mysql_query($query_sequence, $connection);
	    $row_sequence    = mysql_fetch_array($result_sequence);
	    extract($row_sequence);
	
	    $query_adjustmentsetup  = "select CurrencyBase from adjustmentsetup";
	    $result_adjustmentsetup = mysql_query($query_adjustmentsetup, $connection);
	    $row_adjustmentsetup    = mysql_fetch_array($result_adjustmentsetup);
	    extract($row_adjustmentsetup);
	
	    $query_setup  = "select base1, base2 from setup";
	    $result_setup = mysql_query($query_setup, $connection);
	    $row_setup    = mysql_fetch_array($result_setup);
	    extract($row_setup);
	
	    if($CurrencyBase == 1){
		   $Currency_Code = $base1;
	    }
	    else{
		   $Currency_Code = $base2;
	    }
	
	    $query_rma  = "select RMA_Reference, rma.Project_Code as RMAProjectCode, rma.Costcent_code as RMACostcentCode, RMA_Date as RMADate, Adjustment_Id as RMAAdjustmentId, clients.ledger_name as ClientName, RMA_BelongsToGroup as RMABelongsToGroup
	                   from rma inner join clients on clients.ledger_number = rma.client_code
				       where rma_id = '$RMA_Id'";
	   //echo $query_rma;exit;
	   $result_rma = mysql_query($query_rma, $connection);
	   $row_rma    = mysql_fetch_array($result_rma);
	   extract($row_rma);
	   if($RMAProjectCode == ""){
		  $RMAProjectCode = 'null';
	   }
	   if($RMACostcentCode == ""){
		   $RMACostcentCode = 'null';
	   }
	   
         $Adjustment_Remak = "Return RMA Reference: $RMA_Reference - Client Name: $ClientName";
	     $query_insert  = "insert into adjustment (Adjustment_Id, Adjustment_Reference, Adjustment_Date, Adjustment_posted,
	                       Currency_Code, Project_Code, Costcent_Code, Adjustment_Remak, Adjustment_BelongsToGroup, Adjustment_Type, Adjustment_EntryType, User_account) values ('$NewAdjustmentId', '$NewAdjustmentReference',
					       '$RMADate', '0', '$Currency_Code', ".$RMAProjectCode.", ".$RMACostcentCode.", '$Adjustment_Remak', '$RMABelongsToGroup', 'AD', 'A', '".$_SESSION['useraccount']."')";
	      //echo $query_insert;exit;
	     $result_insert = mysql_query($query_insert, $connection);
	
	     $query_updaterma  = "update rma set Adjustment_Id = '$NewAdjustmentId' where rma_id = '$RMA_Id'";
	     $result_updaterma = mysql_query($query_updaterma, $connection);
	
	     $MainAdjustmentId = $NewAdjustmentId;
	}
	else{
		$query_rma  = "select RMA_Reference, rma.Project_Code as RMAProjectCode, rma.Costcent_code as RMACostcentCode, RMA_Date as RMADate, Adjustment_Id as RMAAdjustmentId, clients.ledger_name as ClientName, RMA_BelongsToGroup as RMABelongsToGroup
	                   from rma inner join clients on clients.ledger_number = rma.client_code
				       where rma_id = '$RMA_Id'";
	   //echo $query_rma;exit;
	   $result_rma = mysql_query($query_rma, $connection);
	   $row_rma    = mysql_fetch_array($result_rma);
	   extract($row_rma);
	   if($RMAProjectCode == ""){
		  $RMAProjectCode = 'null';
	   }
	   if($RMACostcentCode == ""){
		   $RMACostcentCode = 'null';
	   }
	   
	    $query_adjustmentsetup  = "select CurrencyBase from adjustmentsetup";
	    $result_adjustmentsetup = mysql_query($query_adjustmentsetup, $connection);
	    $row_adjustmentsetup    = mysql_fetch_array($result_adjustmentsetup);
	    extract($row_adjustmentsetup);
	
	    $query_setup  = "select base1, base2 from setup";
	    $result_setup = mysql_query($query_setup, $connection);
	    $row_setup    = mysql_fetch_array($result_setup);
	    extract($row_setup);
	
	    if($CurrencyBase == 1){
		   $Currency_Code = $base1;
	    }
	    else{
		   $Currency_Code = $base2;
	    }
		$query_update  = "update adjustment set Adjustment_posted = 0,
		                                        Adjustment_Date = '$RMADate',
		                                        Currency_Code = '$Currency_Code',
											    Project_Code = ".$RMAProjectCode.",
											    Costcent_Code = ".$RMACostcentCode."
		                 where adjustment_id = '$AdjustmentId'";
		$result_update = mysql_query($query_update, $connection);
		$MainAdjustmentId = $AdjustmentId;
	}
	    $query_setup  = "select base1, bas2 from setup";
		$result_setup = mysql_query($query_setup, $connection); 
		$row_setup    = mysql_fetch_array($result_setup);
		extract($row_setup);
	    $query_adjustmentdt  = "select adjustmentdt_id, journal_type from adjustmentdt where adjustment_id = '$AdjustmentId'";
		$result_adjustmentdt = mysql_query($query_adjustmentdt, $connection);
		while($row_adjustmentdt = mysql_fetch_array($result_adjustmentdt)){
			extract($row_adjustmentdt);
			$query_deletepurchase  = "delete from purchasesserialno where PurchaseDt_ID = '$adjustmentdt_id' and journal_type = '$journal_type'";
		    $result_deletepurchase = mysql_query($query_deletepurchase, $connection);
		}
		$query_deletedt  = "delete from adjustmentdt where adjustment_id= '$AdjustmentId'";
		$result_deletedt = mysql_query($query_deletedt, $connection);
		
		$query_rmadt  = "select rmadt.rmadt_id as RMADtID, rmadt.item_code as ItemCode, rmadt.RMADt_Quantity, rmadt.Warehouse_code, rma.rma_date as RMADate, rmadt.Serial_Number, rmadt.RMADt_Repaire, rma.currency_code
		                 from rma inner join rmadt on rmadt.rma_id = rma.rma_id
						 where rmadt.rma_id = '$RMA_Id'";
		$result_rmadt = mysql_query($query_rmadt, $connection);
		while($row_rmadt = mysql_fetch_array($result_rmadt)){
			extract($row_rmadt);
			$query_cost  = "select round(MeanCostCur('$ItemCode', '$RMADate', 1, '$base1', '$currency_code'), 2) as AdjustmentDt_Price, 
		                       round(MeanCostCur('$ItemCode', '$RMADate', 1, '$base1', '$base1'), 2) as AdjustmentDtBase1, 
						 	   round(MeanCostCur('$ItemCode', '$RMADate', 1, '$base1', '$base2'), 2) as AdjustmentDtBase2 
						   	   from dual";
		//echo $query_cost;exit;
		$result_cost = mysql_query($query_cost, $connection);
	    $row_cost    = mysql_fetch_array($result_cost);
		extract($row_cost);
			if($Module_rawmaterial == 0){
			$query_adjustmentsetup  = "select DebitAccount as DbAccount, CreditAccount as CdAccount, CurrencyBase from adjustmentsetup";
			$result_adjustmentsetup = mysql_query($query_adjustmentsetup, $connection); 
			$row_adjustmentsetup    = mysql_fetch_array($result_adjustmentsetup); 
			extract($row_adjustmentsetup);
			
			if($CurrencyBase == 1){
				$CurrencyCode = $base1;
			}
			else{
				$CurrencyCode = $base2;
			}
     			
		}
		else{
			$query_groupcode  = "select group_code as GroupCode from items where item_code = '$ItemCode'";
			$result_groupcode = mysql_query($query_groupcode, $connection); 
			$row_groupcode    = mysql_fetch_array($result_groupcode);
			extract($row_groupcode);
			
			$query_ledgers  = "select Trans_DirectStkDbt as DbAccount, Trans_DirectStkCrd as CdAccount, Trans_DirectStkCur as CurrencyCode
			                   from transactionsetup
							   where group_code = '$GroupCode'";
			$result_ledgers = mysql_query($query_ledgers, $connection);
			$row_ledgers    = mysql_fetch_array($result_ledgers);
			extract($row_ledgers);
		}
			$AdjustmentdtItem_code = $ItemCode;
			$AdjustmentDtAdjustmentDt_Quantity = $RMADt_Quantity;
			$AdjustmentDtAdjustmentdt_physicalQty = 0;
			$AdjustmentDtAdjustmentdt_existingQty = abs($RMADt_Quantity);
			$AdjustmentdtWarehouse_Code = $Warehouse_code;
			$AdjustmentdtAdjustment_Date = $RMADate;
			if($RMADt_Repaire == "X" || $RMADt_Repaire == "R"){
			$query_sequence2  = "select nextsequence('adjustmentdt_id', '') as adjustmentdt_id from dual";
		    $result_sequence2 = mysql_query($query_sequence2, $connection);
		    $row_sequence2    = mysql_fetch_array($result_sequence2);
		    extract($row_sequence2);
			$query_insertadjustmentdt  = "insert into adjustmentdt(adjustmentdt_id, Adjustment_Id, Item_Code, AdjustmentDt_Quantity, AdjustmentDt_PhysicalQty, AdjustmentDt_ExistingQty, Warehouse_Code, Adjustment_Date, AdjustmentDt_DirectStkDbt, AdjustmentDt_DirectStkCrd, AdjustmentDt_DirectStkCur, AdjustmentDt_Price, AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2)
			                              values ('$adjustmentdt_id', '$MainAdjustmentId', '$AdjustmentdtItem_code', '$AdjustmentDtAdjustmentDt_Quantity', '$AdjustmentDtAdjustmentdt_physicalQty', '$AdjustmentDtAdjustmentdt_existingQty', '$AdjustmentdtWarehouse_Code', '$AdjustmentdtAdjustment_Date', '$DbAccount', '$CdAccount', '$CurrencyCode', '$AdjustmentDt_Price', '$AdjustmentDtBase1', '$AdjustmentDtBase2')";
			$result_insertadjustmentdt = mysql_query($query_insertadjustmentdt, $connection);
			
			 $query_updatermadt  = "update rmadt set AdjustmentDt_Id = '$adjustmentdt_id'
			                                where rmadt_id = '$RMADtID'";
			         $result_updatermadt = mysql_query($query_updatermadt, $connection);
			
			if($Serial_Number != ""){
				if($AdjustmentDtAdjustmentDt_Quantity > 0){
					$journal_type= "AI";
				}
				else{
					$journal_type= "AR";
				}
				
				$query_sequence  = "select (serial_id + 1) as SerialID from sequence";
			    $result_sequence = mysql_query($query_sequence, $connection);
			    $row_sequence    = mysql_fetch_array($result_sequence);
			    extract($row_sequence);
				$query_insertpurchaseserialno  = "insert into purchasesserialno (Serial_ID, SerialNo_Code, PurchaseDt_ID, Item_Code, Journal_Type) 
				                                  values ('$SerialID', '$Serial_Number', '$adjustmentdt_id', '$AdjustmentdtItem_code', '$journal_type')";
				$result_insertpurchaseserialno = mysql_query($query_insertpurchaseserialno, $connection);
				
				$query_updateseq  = "update sequence set serial_id = serial_id + 1";
			    $result_updateseq = mysql_query($query_updateseq, $connection);
			  }
			}
			else
				if($RMADt_Repaire == "U"){
					$query_sequence2  = "select nextsequence('adjustmentdt_id', '') as adjustmentdt_id from dual";
		    $result_sequence2 = mysql_query($query_sequence2, $connection);
		    $row_sequence2    = mysql_fetch_array($result_sequence2);
		    extract($row_sequence2);
			$query_insertadjustmentdt  = "insert into adjustmentdt(adjustmentdt_id, Adjustment_Id, Item_Code, AdjustmentDt_Quantity, AdjustmentDt_PhysicalQty, AdjustmentDt_ExistingQty, Warehouse_Code, Adjustment_Date, AdjustmentDt_DirectStkDbt, AdjustmentDt_DirectStkCrd, AdjustmentDt_DirectStkCur, AdjustmentDt_Price, AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2)
			                              values ('$adjustmentdt_id', '$MainAdjustmentId', '$AdjustmentdtItem_code', '$AdjustmentDtAdjustmentDt_Quantity', '$AdjustmentDtAdjustmentdt_physicalQty', '$AdjustmentDtAdjustmentdt_existingQty', '$AdjustmentdtWarehouse_Code', '$AdjustmentdtAdjustment_Date', '$DbAccount', '$CdAccount', '$CurrencyCode', '$AdjustmentDt_Price', '$AdjustmentDtBase1', '$AdjustmentDtBase2')";
			//echo $query_insertadjustmentdt;exit;
			$result_insertadjustmentdt = mysql_query($query_insertadjustmentdt, $connection);
			
			$query_updatermadt  = "update rmadt set AdjustmentDt_Id = '$adjustmentdt_id'
			                       where rmadt_id = '$RMADtID'";
			$result_updatermadt = mysql_query($query_updatermadt, $connection);
			
			if($Serial_Number != ""){
				if($AdjustmentDtAdjustmentDt_Quantity > 0){
					$journal_type= "AI";
				}
				else{
					$journal_type= "AR";
				}
				
				$query_sequence  = "select (serial_id + 1) as SerialID from sequence";
			    $result_sequence = mysql_query($query_sequence, $connection);
			    $row_sequence    = mysql_fetch_array($result_sequence);
			    extract($row_sequence);
				$query_insertpurchaseserialno  = "insert into purchasesserialno (Serial_ID, SerialNo_Code, PurchaseDt_ID, Item_Code, Journal_Type) 
				                                  values ('$SerialID', '$Serial_Number', '$adjustmentdt_id', '$AdjustmentdtItem_code', '$journal_type')";
				$result_insertpurchaseserialno = mysql_query($query_insertpurchaseserialno, $connection);
				
				$query_updateseq  = "update sequence set serial_id = serial_id + 1";
			    $result_updateseq = mysql_query($query_updateseq, $connection);
			     }
				 //echo $AdjustmentdtWarehouse_Code;exit;
				 if($AdjustmentdtWarehouse_Code != ""){
					 //echo "test"; exit;
					 $query_warehouseout  = "select RMA_Out as WarehouseRMAOut 
					                         from ".$_SESSION['dbLabel'].".login
											 where user_account = '".$_SESSION['useraccount']."' and Login_Schema = '".strtoupper($_SESSION['dbName'])."'";
					 $result_warehouseout = mysql_query($query_warehouseout, $connection);
					 $row_warehouseout    = mysql_fetch_array($result_warehouseout);
					 extract($row_warehouseout);
					 $AdjustmentdtItem_code = $ItemCode;
			         $AdjustmentDtAdjustmentDt_Quantity = $RMADt_Quantity;
			         $AdjustmentDtAdjustmentdt_physicalQty = abs($RMADt_Quantity);
			         $AdjustmentDtAdjustmentdt_existingQty = 0;
			         $AdjustmentdtWarehouse_Code = $WarehouseRMAOut;
			         $AdjustmentdtAdjustment_Date = $RMADate;
					 
					 $query_sequence2  = "select nextsequence('adjustmentdt_id', '') as adjustmentdt_id from dual";
		             $result_sequence2 = mysql_query($query_sequence2, $connection);
		             $row_sequence2    = mysql_fetch_array($result_sequence2);
		             extract($row_sequence2);
					 
					 $query_insertadjustmentdt  = "insert into adjustmentdt(adjustmentdt_id, Adjustment_Id, Item_Code, AdjustmentDt_Quantity, AdjustmentDt_PhysicalQty, AdjustmentDt_ExistingQty, Warehouse_Code, Adjustment_Date, AdjustmentDt_DirectStkDbt, AdjustmentDt_DirectStkCrd, AdjustmentDt_DirectStkCur, AdjustmentDt_Price, AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2)
			                                       values ('$adjustmentdt_id', '$MainAdjustmentId', '$AdjustmentdtItem_code', '$AdjustmentDtAdjustmentDt_Quantity', '$AdjustmentDtAdjustmentdt_physicalQty', '$AdjustmentDtAdjustmentdt_existingQty', '$AdjustmentdtWarehouse_Code', '$AdjustmentdtAdjustment_Date', '$DbAccount', '$CdAccount', '$CurrencyCode', '$AdjustmentDt_Price', '$AdjustmentDtBase1', '$AdjustmentDtBase2')";
			         //echo $query_insertadjustmentdt;exit;
					 $result_insertadjustmentdt = mysql_query($query_insertadjustmentdt, $connection);
					 
					 $query_updatermadt  = "update rmadt set AdjustmentDt_Id = '$adjustmentdt_id'
			                                where rmadt_id = '$RMADtID'";
			         $result_updatermadt = mysql_query($query_updatermadt, $connection);
					
					 if($Serial_Number != ""){
				         if($AdjustmentDtAdjustmentDt_Quantity > 0){
					        $journal_type= "AI";
				         }
				         else{
					        $journal_type= "AR";
				          }
				
				     $query_sequence  = "select (serial_id + 1) as SerialID from sequence";
			         $result_sequence = mysql_query($query_sequence, $connection);
			         $row_sequence    = mysql_fetch_array($result_sequence);
			         extract($row_sequence);
				     $query_insertpurchaseserialno  = "insert into purchasesserialno (Serial_ID, SerialNo_Code, PurchaseDt_ID, Item_Code, Journal_Type) 
				                                       values ('$SerialID', '$Serial_Number', '$adjustmentdt_id', '$AdjustmentdtItem_code', '$journal_type')";
				     $result_insertpurchaseserialno = mysql_query($query_insertpurchaseserialno, $connection);
				
				     $query_updateseq  = "update sequence set serial_id = serial_id + 1";
			         $result_updateseq = mysql_query($query_updateseq, $connection);
			        }
					
					 $AdjustmentdtItem_code = $ItemCode;
			         $AdjustmentDtAdjustmentDt_Quantity = $RMADt_Quantity;
			         $AdjustmentDtAdjustmentdt_physicalQty = 0;
			         $AdjustmentDtAdjustmentdt_existingQty = abs($RMADt_Quantity);
			         $AdjustmentdtWarehouse_Code = $Warehouse_code;
			         $AdjustmentdtAdjustment_Date = $RMADate;
					 
					 $query_sequence2  = "select nextsequence('adjustmentdt_id', '') as adjustmentdt_id from dual";
		             $result_sequence2 = mysql_query($query_sequence2, $connection);
		             $row_sequence2    = mysql_fetch_array($result_sequence2);
		             extract($row_sequence2);
					 
					 $query_insertadjustmentdt  = "insert into adjustmentdt(adjustmentdt_id, Adjustment_Id, Item_Code, AdjustmentDt_Quantity, AdjustmentDt_PhysicalQty, AdjustmentDt_ExistingQty, Warehouse_Code, Adjustment_Date, AdjustmentDt_DirectStkDbt, AdjustmentDt_DirectStkCrd, AdjustmentDt_DirectStkCur, AdjustmentDt_Price, AdjustmentDt_PriceBase1, AdjustmentDt_PriceBase2)
			                                       values ('$adjustmentdt_id', '$MainAdjustmentId', '$AdjustmentdtItem_code', '$AdjustmentDtAdjustmentDt_Quantity', '$AdjustmentDtAdjustmentdt_physicalQty', '$AdjustmentDtAdjustmentdt_existingQty', '$AdjustmentdtWarehouse_Code', '$AdjustmentdtAdjustment_Date', '$DbAccount', '$CdAccount', '$CurrencyCode', '$AdjustmentDt_Price', '$AdjustmentDtBase1', '$AdjustmentDtBase2')";
			         $result_insertadjustmentdt = mysql_query($query_insertadjustmentdt, $connection);
					 
					  $query_updatermadt  = "update rmadt set AdjustmentDt_Id = '$adjustmentdt_id'
			                                where rmadt_id = '$RMADtID'";
			         $result_updatermadt = mysql_query($query_updatermadt, $connection);
					 
					 if($Serial_Number != ""){
				         if($AdjustmentDtAdjustmentDt_Quantity > 0){
					        $journal_type= "AI";
				         }
				         else{
					        $journal_type= "AR";
				          }
				
				     $query_sequence  = "select (serial_id + 1) as SerialID from sequence";
			         $result_sequence = mysql_query($query_sequence, $connection);
			         $row_sequence    = mysql_fetch_array($result_sequence);
			         extract($row_sequence);
				     $query_insertpurchaseserialno  = "insert into purchasesserialno (Serial_ID, SerialNo_Code, PurchaseDt_ID, Item_Code, Journal_Type) 
				                                       values ('$SerialID', '$Serial_Number', '$adjustmentdt_id', '$AdjustmentdtItem_code', '$journal_type')";
				     $result_insertpurchaseserialno = mysql_query($query_insertpurchaseserialno, $connection);
				
				     $query_updateseq  = "update sequence set serial_id = serial_id + 1";
			         $result_updateseq = mysql_query($query_updateseq, $connection);
			        }
				 }
				 
			}
		}
		if($Module_directstock == 1 || $Module_DirectStockSO == 1){
		$query_login  = "select login_belongstogrp, user_name from ".$_SESSION['dbLabel'].".login where user_account = '".$_SESSION['useraccount']."' and Login_Schema='".strtoupper($_SESSION['dbName'])."'";
		$result_login = mysql_query($query_login, $connection); 
		$row_login    = mysql_fetch_array($result_login);
		extract($row_login);
		$query_starttransaction  = "start transaction";
		$result_starttransaction = mysql_query($query_starttransaction, $connection); 
		
		$query_setjvid  = "set @JvId:=''";
		$result_setjvid = mysql_query($query_setjvid, $connection); 
		
		$query_calladjustmentjv  = "call Create_AdjustmentAutoJv('$MainAdjustmentId', 1, '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '$user_name', '$login_belongstogrp', @JvId)";
		$result_calladjustmentjv = mysql_query($query_calladjustmentjv, $connection);
		
		$query_getjvid  = "select @JvId as JvId from dual";
		$result_getjvid = mysql_query($query_getjvid, $connection); 
		$row_getjvid    = mysql_fetch_array($result_getjvid);
		extract($row_getjvid);
		
		$query_commit  = "COMMIT";
		$result_commit = mysql_query($query_commit, $connection); 
		
		$query_updateadjustment  = "update adjustment set journal_id = '$JvId' where adjustment_id = '$MainAdjustmentId'";
		$result_updateadjustment = mysql_query($query_updateadjustment, $connection); 
		}
		//return $query_calladjustmentjv;
}
function chkQtyExists($connection, $item_code, $warehouse_code, $rmadt_id, $rmadtrpc_quantity, $rma_id){
	    if($rmadt_id == "") $rmadt_id = 0;
	    $query_date  = "select RMA_Date from rma where rma_id = '$rma_id'";
		$result_date = mysql_query($query_date, $connection); 
		$row_date    = mysql_fetch_array($result_date);
		extract($row_date);
		//echo $query_date;exit;
		$type = "MR";
		
		$query_warehouse  = "select RMA_In, RMA_Out from ".$_SESSION['dbLabel'].".login where User_account = '".$_SESSION['useraccount']."'";
		$result_warehouse = mysql_query($query_warehouse, $connection); 
		$row_warehouse    = mysql_fetch_array($result_warehouse);
		extract($row_warehouse);
		
		$query_qtyexist  = "select QtyExists('$item_code', '$warehouse_code', '$RMA_Date', '$rmadt_id', '$type', '$RMA_In', '$RMA_Out') as NumberQty from dual";
		//echo $query_qtyexist;
		$result_qtyexist = mysql_query($query_qtyexist, $connection); 
		$row_qtyexist    = mysql_fetch_array($result_qtyexist);
		extract($row_qtyexist);
		
		if($NumberQty > $rmadtrpc_quantity){
			$exists = 1;
		}
		else{
			$exists = 0;
		}
		return $exists;
}

function drawListOfUsers($connection, $userType){
	echo"<form method='post' action=''>";
	echo "<div style='height: 150px; overflow-y: scroll; width: 300px; margin-left: 43px'>";
	echo "<table>";
	if($userType=='op'){
		$query_alloc  = "SELECT User_Name as UserName, User_account as UserAccount from ".$_SESSION['dbLabel'].".users where user_engineer = 0";
	}
	elseif($userType=='qc'){
		$query_alloc  = "SELECT User_Name as UserName, User_account as UserAccount from ".$_SESSION['dbLabel'].".users where user_engineer = 3";
	}
	elseif($userType=='cc'){
		$query_alloc  = "SELECT User_Name as UserName, User_account as UserAccount from ".$_SESSION['dbLabel'].".users where user_engineer = NULL";
	}
	
	echo "<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'></th>
		<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px'>User Name</th>";
	//echo $query_alloc;exit;
	$result_alloc = mysql_query($query_alloc, $connection);
	while($row_alloc = mysql_fetch_array($result_alloc)){
		extract($row_alloc);
		
		echo "<tr>
				<td style='font-size: 13px; background: #d3d3d3; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'><input type='radio' name='listUser_id' id='listUser_id' value='$UserAccount' style='cursor: pointer'/></td>
				<td style='font-size: 13px; background: #d3d3d3; width: 30px; text-align: center; border: solid 1px #000000; padding: 7px'>$UserName</td>
				</tr>";
	}
	echo"</table>";
	echo"</div>";
	echo"<th style='font-size: 14px; background: #d8d8d8; width: 30px; border: solid 1px #000000; padding: 7px' colspan='2'><input type='submit'></th>";
	echo"</form>";
}
?>