<?php session_name('CAKEPHP'); session_start();	
$PageSecurity = 1;
$tabindex=0;
include('includes/DefinePaymentClass.php');
$title = _('Telephonic Order Memo');
include('includes/SQL_CommonFunctions.inc');
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<style>
#showdata {
	display: block;
	background: url(Default.png) no-repeat;
	top: 0px;
	left: 0px;
	width: auto;
	min-height: 60px;
	z-index:0;
}
#back {
	display: none;
	top: 0px;
	left: 0px;
	width: auto;
	min-height: 60px;
	z-index:0;
}
</style>
<?php 
if(($_REQUEST['Save']))
{
if($_REQUEST['branch']==''){
	echo "Please select Branch";
}
	$prefix_sql=DB_query("Select prefix from locations where loccode='".$_REQUEST['branch']."' ",$db);
    $prefix_row=DB_fetch_array($prefix_sql);
    $branchPrefix=$prefix_row['prefix'];

    $orderNo_sql="Select id,ticket_number from trouble_ticket_creation where branch='".$_REQUEST['branch']."' order by id desc limit 1";
   $orderResult=DB_query($orderNo_sql,$db);

   if(DB_num_rows($orderResult)>0)
    {
	$orderNo_row=DB_fetch_array($orderResult);	
 	$OrderNo=$orderNo_row['ticket_number'];
	$OrderNo_brk1=substr($OrderNo,0,7);
	$OrderNo_brk2=substr($OrderNo,-4);
	$next=$OrderNo_brk2+1;
	$sql_orderNo=$OrderNo_brk1.str_pad($next,4,"0",STR_PAD_LEFT);
	
  }
  else
  {
	$sql_orderNo=$branchPrefix."/TT/0001";
  }	
 	$sql1="Insert into trouble_ticket_creation set
			branch='".$_REQUEST['branch']."', 
			ticket_number='".$sql_orderNo."',
			part_name='".$_REQUEST['part_name']."', 
			product_name='".$_REQUEST['product_name']."',
			invoiceNo='".$_REQUEST['invoiceNo']."',
			invoice_date='".$_REQUEST['invoice_date']."',
			expire_date='".$_REQUEST['expire_date']."',
			closer_date='".FormatDateForSQL($_REQUEST['closer_date'])."',
			prority='".$_REQUEST['prority']."',  
			category='".$_REQUEST['category']."',  
			scope_compaint='".$_REQUEST['scope_compaint']."',  
			mode_compaint='".$_REQUEST['mode_compaint']."',
			mode_contract='".$_REQUEST['mode_compaint']."',
			prob_desc='".$_REQUEST['prob_desc']."',
			materialAmount='".$_REQUEST['materialAmount']."',
			VatCst='".$_REQUEST['VatCst']."',
			VatCstValue='".$_REQUEST['VatCstValue']."',
			serviceAmount='".$_REQUEST['serviceAmount']."',
			serviceTax='".$_REQUEST['serviceTax']."',
			serviceTaxValue='".$_REQUEST['serviceTaxValue']."',
			otherAmount='".$_REQUEST['otherAmount']."',
			grandTotal='".$_REQUEST['grandTotal']."',
			createdBy='".$_SESSION['UserID']."',
			CompanyId='".$_SESSION['ACGCompanyId']."',
			ttDate       = NOW(),
			comt       = '".$_REQUEST['comt']."'";
					
	$result1=DB_query($sql1,$db);
	$TicketId=DB_Last_Insert_ID($db,'trouble_ticket_creation','id');

if(isset($TicketId))
{	
	for($i=0;$i<count($_REQUEST['itemname']);$i++)
	{
		if($_REQUEST['itemname'][$i]!='')
		{
			$data_arr['tt_id']=$TicketId;
			$data_arr['itemname']=$_REQUEST['itemname'][$i];
			$data_arr['rqty']=$_REQUEST['rqty'][$i];
			$data_arr['price']=$_REQUEST['price'][$i];
			$data_arr['amt']=$_REQUEST['amt'][$i];
			$rowinsert= $s->RecordInsert('trouble_ticket_creation_details', $data_arr);
			
			if(!$rowinsert)
			{
			   die('Error'.DB_error());
			}
		}
	}
	for($i=0;$i<count($_REQUEST['service_type']);$i++)
	{
		if($_REQUEST['service_type'][$i]!=''){
				$data_arr1['tt_id']=$TicketId;
				$data_arr1['service']=$_REQUEST['service'][$i];
				$data_arr1['service_type']=$_REQUEST['service_type'][$i];
				$data_arr1['service_desc']=$_REQUEST['service_desc'][$i];
				$data_arr1['billable']=$_REQUEST['billable'][$i];
				$data_arr1['service_amt']=$_REQUEST['service_amt'][$i];
				$rowinsert1= $s->RecordInsert('add_service', $data_arr1);
		if(!$rowinsert1)
		{
		   die('Error'.DB_error());
		}
		}
		
	}
}

$_SESSION['session_msg']="Trouble Ticket inserted successfully";
echo "<script>location.href='index.php?route=service/view_trouble_ticket_creation&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

unset($_POST);
?>
<script language="JavaScript">
function addRow(tableID) 
{
//alert(tableID);
	if(document)
		//document.getElementById("back").style.display = "none";
    	//document.getElementById("showdata").style.display = "block";
		var table = document.getElementById(tableID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
			var newcell = row.insertCell(i);
           	//newcell.style.background='#e7e7e7';
			 // newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
				switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	}
function deleteRowOrder(tableID) 
{
	try 
	{
		var table = document.getElementById(tableID);
		//alert(table);
		var rowCount = table.rows.length;
		//alert(rowCount);
		for(var i=0; i<rowCount; i++) 
			{
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) 
				{
					if(rowCount <= 1) 
					{
						alert("Cannot delete all the rows.");
						break;
					}
				table.deleteRow(i);
				rowCount--;
				i--;
				}
				
			}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
		alert(e);
	}

}
	
	
function open_items()
{

	window.open("open_item.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
	
/*function open_items()
{
window.open("open_item.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=700,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function open_items(i)
{ 
	window.open("open_item.php?rowcount="+i,"profiles_popup_window","height=425,width=640,toolbar=no,menubar=no,dependent=yes,resizable=yes");
}*/


function GoToBack(serviceID){
if(document.getElementById("back").style.display != 'block') {
    document.getElementById("back").style.display = "block";
}else{
	if(document)
		
		var table = document.getElementById(serviceID);
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		document.getElementById("hiditemcount1").value=rowCount;
		var colCount = table.rows[0].cells.length;
		for(var i=0; i<colCount; i++) {	 
            
			var newcell = row.insertCell(i);
           	//newcell.style.background='#e7e7e7';
			 // newcell.className='tabledata';
			   newcell.innerHTML = table.rows[0].cells[i].innerHTML;
				switch(newcell.childNodes[0].type) 
				{
				case "text":
					newcell.childNodes[0].value = "";
					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
					
				break;

				case "hidden":

					newcell.childNodes[0].value = "";

					var nod_id_hidden = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "button":

					var nod_id = newcell.childNodes[0].id;

					newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;

				break;

				case "checkbox":

				newcell.childNodes[0].checked = false;
				var nod_id_chk = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				

				break;

				case "select-one":
				
				newcell.childNodes[0].selectedIndex = 0;
				var nod_id = newcell.childNodes[0].id;
				newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
				break;	
			}
		}
	
}	
}
function deleteRowService(serviceID) 
{
	try 
	{
		var table = document.getElementById(serviceID);
		//alert(table);
		var rowCount = table.rows.length;
		//alert(rowCount);
		for(var i=0; i<rowCount; i++) 
			{
				var row = table.rows[i];
				var chkbox = row.cells[0].childNodes[0];
				if(null != chkbox && true == chkbox.checked) 
				{
					if(rowCount <= 1) 
					{
						alert("Cannot delete all the rows.");
						break;
					}
				table.deleteRow(i);
				rowCount--;
				i--;
				}
				
			}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
	}
	catch(e) 
	{
		alert(e);
	}

}

function getInvoice(str)
{
	var clientId=document.getElementById("partyId").value;
	var prodId = str;
	if (clientId.length==0)
	{
		alert("Please select party Name.");
		return;
	}
	window.open("getInvoice.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>&clientID="+clientId+"&itemId="+prodId,"itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}

function showInvoiceDetails(str)
{
	
	var invoiceId = str;
	window.open("getInvoiceDetail.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>&invoiceId="+invoiceId,"itempopup_window", "height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");

}

function getInvoiceNumber(str)
{

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
	xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	data = xmlhttp.responseText.split("__");
    document.getElementById("invoiceNo").innerHTML=data[0];
	document.getElementById("productId").innerHTML=data[1];
    }
  }
xmlhttp.open("GET","getInvoiceList.php?partyid="+str,true);
xmlhttp.send();
}




</script>
<div class="body_contain">
  <div class="tab_div">
    <form name="troubleTicket" action="" method="post">
      <h1>Trouble Ticket Creation</h1>
      <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
        <tr class="row1">
          <td class="tabledata1">Branch<label><font color="#FF0000"> *</font></label></td>
          <td><?php $location = DB_query("SELECT * FROM locations where CompanyId = '".$_SESSION['ACGCompanyId']."'",$db)?>
            <select name="branch" style="width:150px;">
              <option selected Value="">Select your option</option>
              <?php while($loc = DB_fetch_array($location)){?>
              <option Value="<?php echo $loc['loccode']?>"><?php echo  $loc['locationname']?></option>
              <?php }
			?>
            </select>          </td>
          <td class="tabledata1">Ticket No.<label><font color="#FF0000"> *</font></label></td>
          <td><input type="text" style="width:150px;" name="ticket_number" readonly="readonly" value="<?php  if($_REQUEST['ticket_number']=='') echo 'Auto Generated'; else $_REQUEST['ticket_number'] ;?>" />          </td>
        </tr>
        <tr class="row1">
          <td class="tabledata1">Party Name<label><font color="#FF0000"> *</font></label></td>
          <td><?php 
	  $ClientSQL="Select * from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site in('".$_SESSION['AssignedBranch']."') and Zone in(".$_SESSION['AssignedSites'].")";
		$Client_Name = DB_query($ClientSQL,$db);
	?>
            <select name="part_name" id="partyId" style="width:150px;" onchange="getInvoiceNumber(this.value)">
              <option selected Value="">Select your option</option>
              <?php while($Client = DB_fetch_array($Client_Name)){?>
              <option Value="<?php echo $Client['id']?>"><?php echo $Client['c_name']?></option>
              <?php } ?>
            </select>          </td>
          <td class="tabledata1">Product Name<label><font color="#FF0000"> *</font></label></td>
          <td>
		  <div id="productIddiv" >
		  <?php $item = DB_query("SELECT * FROM item_master order by item_name",$db);?>
            <select name="product_name" id="productId" style="width:150px;" onchange="getInvoice(this.value)">
              <option selected Value="">Selection your option</option>
              <?php while($Item_name = DB_fetch_array($item)){?>
              <option Value="<?php echo $Item_name['id']?>"><?php echo $Item_name['item_name']?></option>
              <?php } ?>
            </select> 
			</div>
			<input name="product_name1" id="productId1" style="display:none;" value="" readonly="readonly" />
			<input name="product_name" id="productId2" style="visibility:hidden; height:1px;" value="" />

			         </td>
        </tr>
		<tr class="row1">
          <td class="tabledata1">Invoice No<label><font color="#FF0000"> *</font></label></td>
          <td><div id="invoiceNodiv" >
		  <?php 
	  	$InvoiceSQL="Select invoice_id,invoiceNo from invoice";
		$Invoice_No = DB_query($InvoiceSQL,$db);
	?>
            <select name="invoiceNo" id="invoiceNo" style="width:150px;"  onchange="showInvoiceDetails(this.value)" >
              <option selected Value="">Select your option</option>
              <?php while($Invoice = DB_fetch_array($Invoice_No)){?>
              <option Value="<?php echo $Invoice['invoice_id']?>"><?php echo $Invoice['invoiceNo']?></option>
              <?php } ?>
            </select>         
			
		</div>	
		<input name="invoiceNo1" id="invoiceNo1" style="display:none;" value="" readonly="readonly" />
		<input name="invoiceNo" id="invoiceNo2" style="visibility:hidden; height:1px;" value="" readonly="readonly" />
			 </td>
			 <td class="tabledata1">Invoice Date<label><font color="#FF0000"> *</font></label></td>
          	 <td><input name="invoice_date" id="invoiceDate" value="" readonly="readonly" style="width:150px;" /></td>
        </tr>
		<tr class="row1">
			<td class="tabledata1">Warrantee Expiry Date<label><font color="#FF0000"> *</font></label></td>
			<td><input name="expire_date" id="expireDate" value="" readonly="readonly" style="width:150px;" /></td>
			<td class="tabledata1">Expected Closing Date<label><font color="#FF0000"> *</font></label></td>
			<td><input name="closer_date" id="closerDate" value="" readonly="readonly" class="datepicker" style="width:150px;" /></td>
		</tr>
        <tr class="row1">
          <td class="tabledata1">Priority<label><font color="#FF0000"> *</font></label></td>
          <td><select name="prority" style="width:150px;">
              <option Value="">Select your option</option>
              <option Value="1">Low</option>
              <option Value="2">Intermediate</option>
              <option Value="3">High</option>
              <option Value="4">Urgent</option>
            </select>          </td>
          <td class="tabledata1">Category</td>
          <td><select name="category" style="width:150px;">
              <option Value="">Select your option</option>
              <option Value="1">Big Fault</option>
              <option Value="2">Small Fault</option>
              <option Value="3">Other Fault</option>
            </select>          </td>
        </tr>
        <tr class="row1">
          <td class="tabledata1">Scope of Service<label><font color="#FF0000"> *</font></label></td>
          <td><select name="scope_compaint" style="width:150px;">
              <option value="">Select your option</option>
              <option value="1">Complaint</option>
              <option value="2">Installation</option>
              <option value="3">De-Installation</option>
              <option value="4">Re-Installation</option>
              <option value="5">Preventive</option>
              <option value="6">Service</option>
              <option value="7">Site Survey</option>
            </select></td>
          <td class="tabledata1">Mode of complaint</td>
          <td><select name="mode_compaint" style="width:150px;">
              <option Value="">Select your option</option>
              <option Value="1">By Call</option>
              <option Value="2">By Mail</option>
              <option Value="3">Internal Mail Request</option>
              <option Value="4">Internal Request</option>
            </select>          </td>
        </tr>
        <tr class="row1">
          <td class="tabledata1">Mode of Contract<label><font color="#FF0000"> *</font></label></td>
          <td colspan="3"><select name="mode_contract" style="width:150px;">
              <option Value="">Select your option</option>
              <option Value="1">Lease</option>
              <option Value="2">NA</option'>
              <option Value="3">Sold</option>
            </select>          </td>
        </tr>
        <tr class="row1">
          <td class="tabledata1" scope="row">Fault Details<label><font color="#FF0000"> *</font></label></td>
          <td colspan="3"><input name="prob_desc" type="text" id="prob_desc" value="" style="width:80%; height:50px;"></td>
        </tr>
        <!--<tr>
          <td class="tabledata1" scope="row">&nbsp;</td>
          <td colspan="3">
		  	<select name="z" id="order_type" style="width:206px;"   onchange="getorder(this.value)">
			  <option value="Consolidate">Spare Parts</option>
			  <option value="MRP_Item">Add Service</option>
			</select>
		  </td>
        </tr>-->
      </table>
      <br />
      <div style="width:auto; overflow:auto; border:#006699 solid 2px;">
        <p align="left" style="padding-top:5px; padding-bottom:5px;">
          <input type="button" onclick="addRow('dataTable')" value="Add Rows for Spare Part" class="search" />
          &nbsp;&nbsp;
          <input type="button" class="search"   onclick="deleteRowOrder('dataTable')" value="Delete" />
          &nbsp;&nbsp;
          <input type="button" name="profile_popup" value="Add Spare Part" id="profile_popup"  onclick="open_items()" class="search" />
		  &nbsp;&nbsp;
		  <input type="button"  value="Add Service" name="add_service" class="search" onclick="GoToBack('serviceID');" />
          <input name="button" type="button" class="search"   onclick="deleteRowService('serviceID')" value="Delete" />
        </p>
        <!-- <p style="padding-left:0px; margin:0; text-align:left;">
          <input type="button" value="Add Spare Part" name="item_popup" class="heading_button"  onclick="open_items(this.id)">
          &nbsp;&nbsp;
          <input type="button" value="Add Service" class="heading_button" />
        </p>-->
		
        <div style="width:auto; overflow:auto;">
          <div id="showdata">
            <table width="100%" border="1" cellspacing="0" cellpadding="3">
              <tr>
                <td class="tabledata">S. No.</td>
				<td class="tabledata" width="2px;"></td>
                <td class="tabledata">Item Name</td>
                <td class="tabledata">Qty in Stock </td>
                <td class="tabledata">Req. Qty.</td>
                <td class="tabledata">Price</td>
                <td class="tabledata">Total amount</td>
              </tr>
              <tr bgcolor="#c1c1c1">
              <tbody id="dataTable">
                <tr bgcolor="#c1c1c1">
                  <td><input name="delitem[]" type="checkbox" value="<?php echo $item_row['id'];?>" />
                 </td>
				 <td><input type="hidden" name="itemid[]" id="itemid" ></td>
                  <td><input type="text" name="itemname[]"  id="itemname" size="40"/></td>
                  <td>&nbsp;</td>
                  <td><input type="text" name="rqty[]"  id="rqty" size="20"/>                  </td>
                  <td><input type="text" name="price[]" id="price" size="20" />                  </td>
                  <td><input type="text" name="amt[]" id="amt" size="20" onkeyup="calculateTotalAmt(this.id)"/></td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
		<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
        <input type="hidden" name="" value="" id="edit_cnt" />
		
		<?php 
			 $basic = DB_query("Select * from trouble_ticket_creation",$db);
 			$basicAmount=DB_fetch_array($basic);
		?>
		<div style="width:806px; overflow:auto;">
			<div id="back">
				<table width="806" border="1" cellspacing="0" cellpadding="0" style="border:#666666 solid 1px;">
               <tr>
                <td width="76" class="tabledata">S. No.</td>
				<td class="tabledata">&nbsp;</td>
                <td width="229" class="tabledata">Service Type</td>
                <td width="251" class="tabledata">Service Description</td>
				<td width="240" class="tabledata">Billable?</td>
                <td width="240" class="tabledata">Service Amount</td>
              </tr>
			   <tbody id="serviceID">
				<tr bgcolor="#c1c1c1">
                  <td><input name="delitem[]" type="checkbox" value="<?php echo $item_row['id'];?>" />
				  </td>
				  <td> <input type="text" name="service[]" id="service" size="40"/></td>
                  <td><?php $serv= DB_query("SELECT * FROM services_type",$db);?>
				  	<select name="service_type[]" style="width:50%" id="service_type">
						<option value="">--Select--</option>
						<?php 
							   while($getServe_type= DB_fetch_array($serv)){?>
							   			<option value="<?php echo $getServe_type['id']?>"><?php echo $getServe_type['service_type']?></option>
							   <?php }
						?>
					</select>
				  </td>
                  <td>
				  	<?php $stype= DB_query("SELECT * FROM services_description",$db);?>
				  	<select name="service_desc[]" id="service_desc" style="width:50%">
						<option value="">--Select--</option>
						<?php 
						   while($getStype= DB_fetch_array($stype)){?>
							<option value="<?php echo $getStype['id']?>"><?php echo $getStype['service_description']?></option>
						   <?php }
						?>
					</select>
				  </td>
				   <td>
				  <select name="billable[]" id="billable" onchange="isbillable(this.value, this.id)">
				  	<option value="Y">Yes</option>
					<option value="N">No</option>
				  </select>
				  </td>
                  <td><input type="text" name="service_amt[]" id="service_amt" size="20" value=""/></td>
                </tr>
  </tbody>
</table>
			</div>
		</div>
        <input type="hidden" name="hiditemcount1" id="hiditemcount1" value="" />
        <input type="hidden" name="" value="" id="edit_cnt1" />
        <!--<p style="padding-left:5px;padding-top:5px; margin:0; text-align:left;">
          <input type="button" value="Add" class="submit_button" />
          &nbsp;&nbsp;
          <input type="button" value="Delete" class="submit_button" />
        </p>-->
        <div style="width:32%; float:right;">
          <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:#999999 solid 1px;">
            <tr class="row1">
              <td class="tabledata">Material Total Amt.</td>
              <td><input name="materialAmount" type="text"  style="width:150px;" onfocus="calculateMaterial()" id="materialAmount"  readonly="readonly"/></td>
            </tr>
			<tr class="row1">
              <td class="tabledata">Vat/Cst</td>
              <td><select name="VatCst" id="value3" onchange="calculateVATCST(this.value)" style="width:150px;">
                  <option value="" >Select</option>
                  <?php 
					$salesSql=DB_query("Select * from sales_tax_master",$db);
					while($salesRow=DB_fetch_array($salesSql))
					{?>
                  <option value="<?php echo $salesRow['salestax_id'];?>" <?php echo $VatCst==$salesRow['salestax_id']?'selected':''?>><?php echo $salesRow['tax_code'];?></option>
                  <?php 	
					}
					?>
                </select>
                <div id="VATId">
                  <input name="VatCstValue" id="VatCstValue" type="text" value="<?php echo $VatCstValue?>" style="width:150px;" readonly  onfocus="calculateEditVATCST()" />
                </div>
                <input type="hidden" name="" id="rate" value="<?php echo $rate;?>" />
              </td>
            </tr>
            <tr class="row1">
              <td class="tabledata" style="width:120px;">Service Total Amt.</td>
              <td><input name="serviceAmount" type="text"  style="width:150px;" onfocus="calculateService()" id="serviceAmount"  readonly="readonly"/></td>
            </tr>
			<tr class="row1">
              <td class="tabledata">Service Tax</td>
              <td><select name="serviceTax" id="value3" onchange="calculateServiceTax(this.value)" style="width:150px;">
                  <option value="" >Select</option>
                  <?php 
					$salesSql=DB_query("Select * from service_tax_master",$db);
					while($salesRow=DB_fetch_array($salesSql))
					{?>
                  <option value="<?php echo $salesRow['id'];?>" <?php echo $serviceTax==$salesRow['id']?'selected':''?>><?php echo $salesRow['tax_code'];?></option>
                  <?php 	
					}
					?>
                </select>
                <div id="VATId">
                  <input name="serviceTaxValue" id="serviceTaxValue" type="text" value="<?php echo $serviceTaxValue?>" style="width:150px;" readonly  onfocus="calculateEditServiceTax()" />
                </div>
                <input type="hidden" name="" id="servicerate" value="<?php echo $servicerate;?>" />
              </td>
            </tr>
            <tr class="row1">
              <td class="tabledata">Other Amount</td>
              <td><input name="otherAmount" id="otherAmount" type="text" style="width:150px;" onfocus=""/></td>
            </tr>
            <tr class="row1">
              <td class="tabledata">Grand Total</td>
              <td><input name="grandTotal" id="grandTotal" type="text"  style="width:150px;" onfocus="calculateGrand()" readonly="readonly"  /></td>
            </tr>
          </table>
        </div>
        <div class="cl"></div>
      </div>
      <p align="center">
        <input type="submit" name="Save" value="Save" onclick="return validate_tt()"/>
        <input type="submit" name="cancel" value="Cancel" />
      </p>
    </form>
  </div>
</div>
<script>
function getorder(str)
{
	if(str!='Consolidate')
	document.getElementById("EDRate").setAttribute('disabled',true);
	else
	document.getElementById("EDRate").removeAttribute('disabled');
	if(str.length==0)
	{
		document.getElementById("showdata").innerHTML="";
		return;
	}
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("showdata").innerHTML=xmlhttp.responseText;
 	}
 }
xmlhttp.open("GET","getservicedata.php?id="+str,true);
xmlhttp.send();
}

function calculateTotalAmt(id)
{ 
		var id_brk=id.split('_');
		var i=id_brk[1];
		if(id_brk.length>1)
		{
			var qty=parseFloat(document.getElementById("rqty_"+i).value);
			var val=parseFloat(document.getElementById("price_"+i).value);
			var tot_amt=qty*val;
			document.getElementById("amt_"+i).value=tot_amt.toFixed(2);
		}
		else
		{
			var qty=parseFloat(document.getElementById("rqty").value);
			var val=parseFloat(document.getElementById("price").value);
			var tot_amt=qty*val;
			document.getElementById("amt").value=tot_amt.toFixed(2);
		}
		
		
}

function calculateMaterial()
{
	var totalMat=0;
	var first_row=document.getElementById("amt").value;
	if(first_row==''){first_row=0;}
	var cnt=document.getElementById("hiditemcount").value;
	var newcount=parseInt(cnt)+1;
	for(var i=1;i<newcount;i++)
	{
	 	var totalMat1=document.getElementById("amt_"+i).value;
	 	totalMat=(parseFloat(totalMat)+parseFloat(totalMat1)).toFixed(2);
	}
	
	  document.getElementById("materialAmount").value=parseFloat(totalMat)+parseFloat(first_row);
}


function calculateService()
{
	var totalBasic=0;
	var cnt1=document.getElementById("hiditemcount1").value;
	var service_amt=document.getElementById("service_amt").value;
	if(service_amt==''){service_amt=0;}
	var newcount1=parseInt(cnt1)+1;
	for(var j=1;j<newcount1;j++)
	{
	 	var service_amt1=document.getElementById("service_amt_"+j).value;
	 	var service_amt=(parseFloat(service_amt)+parseFloat(service_amt1)).toFixed(2);
	}
	  document.getElementById("serviceAmount").value=parseFloat(service_amt);
}

function calculateVATCST(str)
{
	
	if (str.length==0)
  		{
  			document.getElementById("rate").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("rate").value=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise.php?salesId="+str,true);
		xmlhttp.send();
}

function calculateServiceTax(str)
{
	
	if (str.length==0)
  		{
  			document.getElementById("servicerate").innerHTML="";
  			return;
  		}
		if (window.XMLHttpRequest)
  		{// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
		else
  		{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
		xmlhttp.onreadystatechange=function()
  		{
  			if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("servicerate").value=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","calculateExcise.php?servicesId="+str,true);
		xmlhttp.send();
}



function calculateEditVATCST()
{
	var rate=document.getElementById("rate").value;
	var taxableAmount=document.getElementById("materialAmount").value;
	var vat_cst=taxableAmount*(rate/100);
	document.getElementById("VatCstValue").value=vat_cst.toFixed(2);
}
function calculateEditServiceTax()
{
	var rate=document.getElementById("servicerate").value;
	var taxableAmount=document.getElementById("serviceAmount").value;
	var vat_cst=taxableAmount*(rate/100);
	document.getElementById("serviceTaxValue").value=vat_cst.toFixed(2);
}

function calculateGrand()
{
	
	
	var a=document.getElementById("materialAmount").value;
	var b=document.getElementById("VatCstValue").value;
	var c=document.getElementById("serviceAmount").value;
	var d=document.getElementById("serviceTaxValue").value;
	var e=document.getElementById("otherAmount").value;
	if(a==''){a=0;}
	if(b==''){b=0;}
	if(c==''){c=0;}
	if(d==''){d=0;}
	if(e==''){e=0;}
	
	var GRAND=parseFloat(a)+parseFloat(b)+parseFloat(c)+parseFloat(d)+parseFloat(e);
	document.getElementById("grandTotal").value=GRAND.toFixed(2);
	
}



function validate_tt()
 	{
		if(document.troubleTicket.branch.value=='')
			{
				alert("Please Select Branch First");
				return false;
			}
			else if(document.troubleTicket.ticket_number.value=='')
			{
				alert("Please Select Ticket Number");
				return false;
			}
			else if(document.troubleTicket.part_name.value=='')
			{
				alert("Please Select Party Name");
				return false;
			}
			else if(document.troubleTicket.product_name.value=='')
			{
				alert("Please Select Product Name");
				return false;
			}
			else if(document.troubleTicket.invoiceNo.value=='')
			{
				alert("Please Select Invoice No.");
				return false;
			}
			else if(document.troubleTicket.invoice_date.value=='')
			{
				alert("Please Enter The Invoice Date");
				return false;
			}
			else if(document.troubleTicket.expire_date.value=='')
			{
				alert("Please Enter The Warrantee Expiry Date");
				return false;
			}
			else if(document.troubleTicket.closer_date.value=='')
			{
				alert("Please Enter The Expected Closing Date");
				return false;
			}
			else if(document.troubleTicket.prority.value=='')
			{
				alert("Please Select The Prority");
				return false;
			}
			else if(document.troubleTicket.scope_compaint.value=='')
			{
				alert("Please Select The Scope of Service");
				return false;
			}
			else if(document.troubleTicket.mode_contract.value=='')
			{
				alert("Please Select The Mode of Contract");
				return false;
			}
			else if(document.troubleTicket.prob_desc.value=='')
			{
				alert("Please Enter The Problem Description");
				return false;
			}
			var counter=document.getElementById("rowcount").value;
			for(var x=0;x<counter;x++)
				{
					  if(document.getElementById("itemname").value=='')
						{
							alert("Please Enter The Item Name");
							return false;
						}
					 else if(document.getElementById("rqty").value=='')
						{
							alert("Please Enter The Quantity");
							return false;
						}	
					 else if(document.getElementById("itemname_"+x).value=='')
						{
							alert("Please Enter The Item Name");
							return false;
						}
						else if(document.getElementById("rqty_"+x).value=='')
						{
							alert("Please Enter The Quantity");
							return false;
						}
				}
}


</script>
