<?php
if($_REQUEST['Save'])
{		
			date_default_timezone_set('Asia/Kolkata');
			$PreSql=DB_query("select prefix from locations where loccode='".$_REQUEST['branch_id']."'",$db);
			$Prefix=DB_fetch_array($PreSql);
			$chkLC=DB_query("select * from indent_so_main where site='".$_REQUEST['branch_id']."'",$db);
			if(DB_num_rows($chkLC)>0)
			{
						$GetMxSQL=DB_query("select indent_no AS MAXLC from indent_so_main
									  where site='".$_REQUEST['branch_id']."' order 
									  by id desc limit 0,1",$db);
						$GetMAXID=DB_fetch_array($GetMxSQL);
						$Explode=explode("/",$GetMAXID['MAXLC']);
						$FinalMaxId=$Explode[2]+1;
						$FinalCode=$Prefix['prefix']."/"."Indent"."/".str_pad($FinalMaxId,5,0,STR_PAD_LEFT);
			}
			else
			{
			$FinalCode=$Prefix['prefix']."/"."Indent"."/"."00001";
			}
			
			$data['site']                 =$_REQUEST['branch_id'];
			$data['indent_date']          =FormatDateForSQL($_REQUEST['indent_date']);
			$data['indent_no']            =$FinalCode;
			$data['format']               =$_REQUEST['format'];
			$data['sop']                  =$_REQUEST['sopno'];
			$data['order_type']           =$_REQUEST['order_type'];
			$data['type']                 =$_REQUEST['type_id'];
			$data['client']               =$_REQUEST['party'];
			$data['work_order_no']        =$_REQUEST['order_no'];
			$data['order_date']           =FormatDateForSQL($_REQUEST['order_date']);    
			$data['po_no']                =$_REQUEST['po_no'];
			$data['po_date']              =FormatDateForSQL($_REQUEST['po_date']);
			$data['delivery_date']        =FormatDateForSQL($_REQUEST['delivery_date']);
			$data['consignee']            =$_REQUEST['consignne_id'];
			$data['cont_person']          =$_REQUEST['contact'];
			$data['mobile_no']            =$_REQUEST['contactMobileno'];
			$data['credit_limit']         =$_REQUEST['credit_limit'];
			$data['current_bal']          =$_REQUEST['current_bal'];
			$data['credit_days']          =$_REQUEST['credit_days'];
			$data['handled_by']           =$_REQUEST['handled_by'];
			$data['basic']                =$_REQUEST['basicAmount'];
			$data['excise_id']            =$_REQUEST['EDBillRate'];
			$data['excise_amt']           =$_REQUEST['totalDiscountedPrice'];
			$data['ECessID']              =$_REQUEST['ecessid'];
			$data['ecess']                =$_REQUEST['ECessPercentage'];
			$data['ecess_amt']            =$_REQUEST['ECessValue'];
			$data['SHECessId']            =$_REQUEST['sahecessid'];
			$data['sah']                  =$_REQUEST['SAHCessPercentage'];
			$data['sah_amt']              =$_REQUEST['SAHCessValue'];
			$data['taxable_amt']          =$_REQUEST['taxableAmount'];
			$data['hardware_amt']         =$_REQUEST['hrdtotalAmt'];
			$data['vat_cst']              =$_REQUEST['VatCst'];
			$data['vat_cst_amt']          =$_REQUEST['VatCstValue'];
			$data['grand_total']          =$_REQUEST['grandTotal'];
			$data['faberication']         =$_REQUEST['faberication'];
			$data['freight']         =$_REQUEST['freight'];
			$data['mms']                  =$_REQUEST['mmsno'];
			$data['created_by']           =$_SESSION['UserID'];
			$data['created_date']         =date("Y-m-d H:i:s");
			$data['CompanyID']            =$_SESSION['ACGCompanyId'];
			$data['dis_type']           =$_REQUEST['PerAmount'];
			$data['dis_per']            =$_REQUEST['Discount_amount'];
			$data['dis_amt']           =$_REQUEST['discountperc'];
			$data['dis_amt']          =$_REQUEST['discountperc'];
			$data['sales_tax_taxabelamt']=$_REQUEST['total_taxable_amt'];
			$data['sales_tax_hrdamt']='';
			
			$data['discounted_amt']=$_REQUEST['afterdiscountprice'];
			$row= $s->RecordInsert('indent_so_main', $data);
			$lastid=DB_Last_Insert_ID($db,'indent_so_main', 'id');	

				for($i=0;$i<count($_REQUEST['itemid']);$i++)
				{
				$data_arr['mst_itemid']=$_REQUEST['itemid'][$i];
				$data_arr['size']=$_REQUEST['size'][$i];
				$data_arr['grade']=$_REQUEST['grade'][$i];
				$data_arr['qty']=$_REQUEST['pri_qty'][$i];
				$data_arr['rate']=$_REQUEST['rate'][$i];
				$data_arr['amount']=$_REQUEST['amount'][$i];
				$data_arr['parent_id']=$lastid;
				$rowinsert=$s->RecordInsert('indent_so_main_details', $data_arr);
									
					
			/*****************   Checking  Total Approve Qty From Sales  Order Details  ********************/          
					//$qty1=0;
//					
//										
//					$sql_total=DB_query("select sum(qty)  as approve 
//					 from faberication_main_child_item where parent_id='".$_REQUEST['sopno']."' 
//					 and itemid='".$_REQUEST['itemid'][$i]."'",$db);
//					$row_sales=DB_fetch_array($sql_total);
//					$qty1=$row_sales['approve'];
//					
					/************************  End *************************************/
					
					// $qty2=0;	
//					 
//					 echo "select sum(qty)  as qty 
//					from indent_so_main_details  inner join indent_so_main 
//					on  indent_so_main.id=indent_so_main_details.parent_id
//					where sop='".$_REQUEST['sopno']."' and mst_itemid='".$_REQUEST['itemid'][$i]."'";
//					 										 
//					$sql_total1=DB_query("select sum(qty)  as qty 
//					from indent_so_main_details  inner join indent_so_main 
//					on  indent_so_main.id=indent_so_main_details.parent_id
//					where sop='".$_REQUEST['sopno']."' and mst_itemid='".$_REQUEST['itemid'][$i]."'",$db);
//					$row_sales1=DB_fetch_array($sql_total1);
//					$qty2=$row_sales1['qty'];	
//					if($qty1!=0 && $qty2!=0)
//					{
//							if($qty1==$qty2)
//							{
							
						$update=DB_query("update  faberication_main_child_item   set indent_status_check=1 
						where parent_id='".$_REQUEST['sopno']."'
						and itemid='".$_REQUEST['itemid'][$i]."'",$db);
							
				//			}
//	                }
	
	        }
			
			/**************************  Adding Hardware Item Details *******************************/
			   
					for($i=0;$i<count($_REQUEST['item_code']);$i++)
					{
					$data_arr1['mst_itemid']=$_REQUEST['item_code'][$i];
					$data_arr1['qty']=$_REQUEST['stock_qty'][$i];
					$data_arr1['price']=$_REQUEST['stock_price'][$i];
					$data_arr1['perunit']=$_REQUEST['stock_perunit'][$i];
					$data_arr1['amount']=$_REQUEST['stock_value'][$i];
					$data_arr1['discounttype']=$_REQUEST['PerAmo'][$i];
					$data_arr1['Discount']=$_REQUEST['Discount'][$i];
					$data_arr1['perc_amt']=$_REQUEST['perc'][$i];
					$data_arr1['afterdis']=$_REQUEST['value_dis'][$i];
					$data_arr1['parent_id']=$lastid;
					$rowinsert=$s->RecordInsert('indent_so_main_hrditem_details', $data_arr1);
					}
			
			
			/***************************  End ***********************************************************/
			
			
			/*-----------------------------------adding terms and conditions--------------------------------------------*/
				$term=$_REQUEST['terms'];
					$condition=$_REQUEST[Terms_Condition];
					for($i=0;$i<count($_REQUEST[terms]);$i++)
					{
					if($condition[$i]=="")
					$condition_value="";
					else
					$condition_value=$condition[$i];
					
					DB_query("insert into `quotation_terms_cond`(parant_id,termsid,condid,frm_id,company_id,type) values('".$lastid."','".$term[$i]."','".$condition_value."','".$_REQUEST['frmid']."','".$_SESSION['ACGCompanyId']."','Indent')",$db);
					}

					
			/*---------------------------------------------end----------------------------------------------------------*/
			
				$count=0;
				$sql_check=DB_query("select * from faberication_main_child_item where  parent_id='".$_REQUEST['sopno']."' ",$db);
				$num1=DB_num_rows($sql_check);
				while($row_check=DB_fetch_array($sql_check))
				{
						if($row_check['indent_status_check']==1)
						{
						$count=$count+1;
						}
				}
				
	
				if($num1==$count)
				{	
					
				$update=DB_query("update faberication_main set indent_status=1 where id='".$_REQUEST['sopno']."'",$db);
				$update=DB_query("update salesorder_details_approv set indent_id='".$lastid."' where fabrication_id='".$_REQUEST['sopno']."'",$db);
				
				}
				
	$s->pageLocation('index.php?route=CRM/fabrication_index&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	
}



$SQLSales=DB_query("select * from faberication_main where id='".$_REQUEST['SID']."'",$db);
$GetSalesData=DB_fetch_array($SQLSales);
extract($GetSalesData);




 $Location=$s->GetFieldValue('locations',"loccode='".$site."'",'locationname');
$ClientName=$s->GetFieldValue('clientmaster',"id='".$client."'",'c_name');
$ContactMobile=$s->GetFieldValue('contact_information_master',"id='".$ContactPerson."'",'phone'); 
$Category=DB_query("SELECT * FROM stockmaincategory WHERE (stockmaincategoryid='SF' || stockmaincategoryid='FG') 
                  AND CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($categoryId=DB_fetch_array($Category)){
$catId[]=$categoryId['id'];
}
$FinalCatId=implode(",",$catId);?>

<script type="text/javascript" language="javascript">
function GetContactPersonNo(val)
{
	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("contactMobileno").value=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","getPhone.php?personid="+val,true);
	xmlhttp.send();
}

function calculateBasic()
{
	var count=document.getElementById("count").value;
	var total=0;
	for(var j=0; j<count; j++)
	{
		if(document.getElementById("amount_"+j).value!='')
		var amt=document.getElementById("amount_"+j).value;
		else
		amt=0;
		
		total=parseFloat(total)+parseFloat(amt);
	}
	document.getElementById("basicAmount").value=total.toFixed(2);
}


function calculateBillExcise(str)
{
	var value=document.getElementById("basicAmount").value;
	if(str.length==0)
	{
		document.getElementById("totalDiscountedPrice").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("totalDiscountedPrice").value=xmlhttp.responseText;
 	}
 }
xmlhttp.open("GET","calculateExcise.php?type=bill&id="+str+"&value="+value,true);
xmlhttp.send();	
}


function calculateECess()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var ECessPercentage=document.getElementById("ECessPercentage").value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue").value=total.toFixed(2);
}

function calculateSAHCess()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage").value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue").value=total.toFixed(2);
}


function calculateTaxable()

{
	var Basic=document.getElementById("basicAmount").value;
	var totalDiscountedPrice=document.getElementById("totalDiscountedPrice").value;
	var ECessValue=document.getElementById("ECessValue").value;
	var SAHCessValue=document.getElementById("SAHCessValue").value;
	if(totalDiscountedPrice==''){totalDiscountedPrice=0;}
	if(ECessValue==''){ECessValue=0;}
	if(SAHCessValue==''){SAHCessValue=0;}
	var TOTAL=parseFloat(totalDiscountedPrice)+parseFloat(ECessValue)+parseFloat(SAHCessValue)+parseFloat(Basic);
	document.getElementById("taxableAmount").value=TOTAL.toFixed(2);
}


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 calculateEditVATCST()
{
	if(document.getElementById("rate").value!='')
	{
		var rate=parseFloat(document.getElementById("rate").value);
	}
	else
	{
		rate=0;
	}
	if(document.getElementById("taxableAmount").value!='')
	{
		var taxableAmount=document.getElementById("taxableAmount").value;
	}
	else
	{
		taxableAmount=0;
	}
	
	
		hrdamt=0;
	
	
	var fintotal=parseFloat(taxableAmount)+parseFloat(hrdamt);
	//alert(fintotal);
	//alert(hrdamt);
	
	var vat_cst=fintotal*(rate/100);
	document.getElementById("VatCstValue").value=vat_cst.toFixed(2);
}


//function calculateGrand()
//{
//	var a=document.getElementById("totalDiscountedPrice").value;
//	var b=document.getElementById("taxableAmount").value;
//	var c=document.getElementById("VatCstValue").value;
//	if(b==''){b=0;}
//	if(c==''){c=0;}
//	var GRAND=parseFloat(b)+parseFloat(c);
//	document.getElementById("grandTotal").value=GRAND.toFixed(2);
//}


function calculateGrand()
{
	//var a=document.getElementById("basicAmount").value;
	//var b=document.getElementById("totalDiscountedPrice").value;
	//var c=document.getElementById("ECessValue").value;
	//var d=document.getElementById("SAHCessValue").value;
	//var e=document.getElementById("taxableAmount").value;
	//var f=document.getElementById("VatCstValue").value;
	var g=document.getElementById("freight").value;
	var h=document.getElementById("total_taxable_amt").value;
	//var i=document.getElementById("total_taxable_hrd").value;
	//if(a==''){a=0;}
	//if(b==''){b=0;}
	//if(c==''){c=0;}
	//if(d==''){d=0;}
	//if(e==''){e=0;}
	//if(f==''){f=0;}
	if(g==''){g=0;}
	if(h==''){h=0;}
	//if(i==''){i=0;}
	//var GRAND=parseFloat(a)+parseFloat(b)+parseFloat(c)+parseFloat(d)+parseFloat(e)+parseFloat(f)+parseFloat(g);
	var GRAND=parseFloat(g)+parseFloat(h);
	//grandBrk=GRAND.toFixed(2).split('.');
	//grandA=grandBrk[0];
	//grandB=grandBrk[1];
	//alert(grandA);
	//alert(grandB);
	//if(grandB>'50' || grandB=='50')
	//{
		//GRAND11=parseInt(GRAND)+1;
	//}
	//else
	//if(grandB<'50')
	//{
		//GRAND11=parseInt(GRAND);
	//}	
	document.getElementById("grandTotal").value=GRAND.toFixed(2);
	//document.getElementById("roundOff").value="."+grandB;
}
 

</script>
<link rel="stylesheet" href="css/new_mxerp.css"><div class="body_contain">
<div class="tab_div">
<h1>Indent</h1>
<form method="post">
<table width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td class="tabledata1"><strong>Branch</strong></td>
	<td><input type="hidden" style="width:150px;" name="branch_id" id="branch_id" value="<?php echo $site; ?>"  />
		<input type="text" style="width:150px;" name="branch1" readonly="" id="branch1" value="<?php echo $Location; ?>"  /></td>
	<td class="tabledata1"><strong>Indent No.</strong></td>
	<td><input name="indent_no" id="indent_no" type="text" value="Auto Generated" readonly="" style="width:150px;" /></td>
	<td class="tabledata1"><strong>Indent Date<font size="3" color="#FF0000">*</font></strong></td>
	<td><input name="indent_date" id="indent_date" type="text" class="datepicker" style="width:150px;" value="<?php echo date('d/m/Y'); ?>" /></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Format</strong></td>
	<td><input type="text" name="format" id="format" value="Standard" readonly style="width:150px;" /></td>
	<td class="tabledata1"><strong>Type of Order</strong></td>
	<td><input type="text" style="width:150px;" name="order_type" id="order_type" readonly value="<?php echo $type_of_sheet; ?>"  /></td>
	<td class="tabledata1"><strong>Type<font size="3" color="#FF0000">*</font></strong></td>
	<td>
	<?php $typ_nm=DB_fetch_array(DB_query("SELECT * FROM type_master WHERE id='".$type."'",$db));?>
		<input type="text"  style="width:150px;"  value="<?php echo $typ_nm['name']; ?>"   name="type_name"  readonly />
		<input type="hidden" value="<?php echo $typ_nm['id']; ?>" name="type_id"   />
	</td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Client</strong></td>
	<td><input type="hidden" style="width:150px;" name="party" id="party" value="<?php echo $client; ?>"  />
		<input type="text" style="width:150px;" name="party1" id="party1" readonly value="<?php echo $ClientName; ?>"  /></td>
	<td class="tabledata1"><strong>Work Order No</strong></td>
	<td><input type="text" style="width:150px;" name="order_no" id="order_no" readonly value="<?php
	 echo $s->getField('measurment_sheet_main','id',$against_indent,'order_no'); ?>"  /></td>
	<td class="tabledata1"><strong>Order Date</strong></td>
	<td><input name="order_date" id="order_date" type="text" readonly value="<?php echo ConvertSQLDate($s->getField('measurment_sheet_main','id',$against_indent,'order_date')); ?>" style="width:150px;" /></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>P.O. No</strong></td>
	<td><input name="po_no" id="po_no" type="text" style="width:150px;" readonly value="<?php echo $s->getField('measurment_sheet_main','id',$against_indent,'po_no'); ?>" /></td>
	<td class="tabledata1"><strong>Po. Date</strong></td>
	<td><input name="po_date" id="po_date" type="text" readonly value="<?php echo ConvertSQLDate($s->getField('measurment_sheet_main','id',$against_indent,'po_date')); ?>" style="width:150px;" /></td>
	<td class="tabledata1"><strong>Delivery Date</strong></td>
	<td><input name="delivery_date" id="delivery_date" type="text" readonly value="<?php echo ConvertSQLDate($s->getField('measurment_sheet_main','id',$against_indent,'delivery_date')); ?>" style="width:150px;" /></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Consignee<font size="3" color="#FF0000">*</font></strong></td>
	<td>
		<?php $sqlConsi=DB_fetch_array(DB_query("SELECT * FROM clientmaster_consignee_address where id='".$consignee."'",$db)); ?>
		<input type="text" value="<?php echo  $sqlConsi['consignee_name']  ?>" readonly  style="width:150px;" />
		<input type="hidden" value="<?php echo $consignee ; ?>" name="consignne_id" />
			</td>
	<td class="tabledata1"><strong>Cont. Person</strong></td>
	<td>
	<?php $contact= DB_fetch_array(DB_query("SELECT ci.id,ci.person, ci.phone FROM contact_information_master ci,salesorder s WHERE ci.id=s.ContactPerson AND s.id=(SELECT soid FROM measurment_sheet_main WHERE id=(SELECT against_indent  FROM`faberication_main` WHERE id='".$_REQUEST['SID']."'))",$db)); ?>
<input type="text" value="<?php echo  $contact['person']; ?>" readonly  style="width:150px;" />

<input type="hidden"  name="contact" value="<?php echo  $contact['id']; ?>" readonly  style="width:150px;" />	
	</td>
	<td class="tabledata1"><strong>Mobile No.</strong></td>
	<td><input name="contactMobileno" id="contactMobileno" type="text" value="<?php echo $contact['phone'];  ?>" readonly style="width:150px;" /></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Credit Limit </strong></td>
	<?php $cli_det=DB_fetch_array(DB_query("select credit_limit,credit_days from clientmaster where id='" .$client."' ",$db))?>
	<td><input name="credit_limit" readonly="true" id="credit_limit" type="text" value="<?php echo $cli_det['credit_limit']; ?>" class="" style="width:150px;" /></td>
	<td class="tabledata1"><strong>Current Bal.</strong></td>
	<td><input name="current_bal" id="current_bal" type="text" value=""  class="" style="width:150px;" /></td>
	<td class="tabledata1"><strong>Credit Days</strong></td>
	<td><input name="credit_days" readonly="true" 	 id="credit_days" type="text"  value="<?php echo $cli_det['credit_days']; ?>" class="" style="width:150px;" /></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Handled By<font size="3" color="#FF0000">*</font></strong></td>
	<td><select style="width:150px;" name="handled_by" id="handled_by">
			<option value="0">Select</option>
			<?php $sqlEmplo=DB_query("SELECT * FROM empmaster WHERE CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			while($Handles=DB_fetch_array($sqlEmplo)){?>
				<option value="<?php echo $Handles['id']; ?>"><?php echo $Handles['emp_name']; ?></option>
			<?php }?>
	</select></td>
	<td class="tabledata1"><strong>Fabrication No</strong></td>
	<td><input type="text" name="" value="<?php echo $sheet_no?>" readonly="readonly" style="width:150px;"/>
	<input type="hidden" name="faberication"  value="<?php echo $id; ?>" />
	</td>
	 <td class="tabledata1"><strong>MMS No</strong></td>
	<td ><input type="text" name="" value="<?php echo $s->getField('measurment_sheet_main','id',$against_indent,'mms_no'); ?>" readonly="readonly" style="width:150px;"/>
	<input type="hidden" name="mmsno"  value="<?php echo $against_indent; ?>" />
	<input type="hidden" name="sopno" value="<?php echo $_REQUEST['SID']; ?> " /></td>
</tr>

<tr class="row1">
	<td class="tabledata1" colspan="6">
		<table width="100%" border="1" cellpadding="2" cellspacing="1">
		<tr class="row1">
			<td class="tabledata">Sr. No.</td>
			<td class="tabledata">Part Code</td>
			<td class="tabledata">Part Name</td>
			<td class="tabledata">Size</td>
			<td class="tabledata">Unit</td>
			<td class="tabledata">Qty.</td>
			<td class="tabledata">Rate</td>
			<td class="tabledata">Amount</td>
		</tr>
		<?php $i=0; 
		$totalamount1=0;
		$sqlDetails=DB_query("select * from faberication_main_child_item where parent_id='".$_REQUEST['SID']."'",$db);
		while($salesDetails=DB_fetch_array($sqlDetails)) {
		$itemCode=$s->GetFieldValue('item_master',"id='".$salesDetails['itemid']."'",'StockID');
		$itemName=$s->GetFieldValue('item_master',"id='".$salesDetails['itemid']."'",'item_name');
		$Unitid=$s->GetFieldValue('item_master',"id='".$salesDetails['itemid']."'",'mst_unitid');
		$unitname=$s->GetFieldValue('unitsofmeasure',"unitid='".$Unitid."'",'unitname');
		
			
		$privious_qty_show=DB_query("select sum(qty) as qty from indent_so_main_details 
		        inner join indent_so_main on indent_so_main.id=indent_so_main_details.parent_id
				where mst_itemid='".$salesDetails['ItemCode']."' and sop='".$_REQUEST['SID']."'",$db);
        		
		$privious_qty_show1=DB_fetch_array($privious_qty_show);
	    
		$privious_qty=$privious_qty_show1['qty'];
		
		if($privious_qty=='')
		$privious_qty=0;		
			
		 $totalqty=$salesDetails['qty']-$privious_qty;
		
		 $orderno=$s->getField('measurment_sheet_main','id',$against_indent,'order_no');
         $masterid=$s->getField('salesorder','orderNo',$orderno,'id');		
		$sqlDetails1=DB_query("select * from salesorder_details where masterId='".$masterid."' 
		and ItemCode='".$salesDetails['itemid']."' ",$db);
		while($salesDetails1=DB_fetch_array($sqlDetails1)) 
		{
		  $rate=$salesDetails1['Rate'];
		}
		?>
		<tr class="row1">
			<td><?php echo $i+1; ?></td>
			<td><?php echo $itemCode; ?><input type="hidden" name="itemid[]" value="<?php echo $salesDetails['itemid']; ?>" id="" style="width:50px;" /></td>
			<td><?php echo $itemName; ?></td>
			<td><select style="width:120px;" name="size[]" id="size">
					<option>--Select--</option>
				<?php $SQLSize=DB_query("SELECT * FROM size_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
				while($sizeid=DB_fetch_array($SQLSize)){?>
					<option value="<?php echo $sizeid['id']; ?>"<?php echo($salesDetails['size']==$sizeid['id']?'selected':''); ?>><?php echo $sizeid['size']; ?></option>
				<?php } ?>
			</select></td>
			<td><?php echo $unitname; ?></td>
			<td><input type="text" name="pri_qty[]" id="pri_qty_<?php echo $i;?>" readonly value="<?php echo $totalqty; ?>" 
			style="width:80px;" /></td>
			
			<td><input type="text" name="rate[]" readonly id="rate_<?php echo $i;?>" value="<?php echo $rate; ?>"  
			style="width:80px;" /></td>
			<td>
			<?php
			$itemamount=($totalqty*$rate); 
			$totalamount1=$$totalamount1+$itemamount; ?>			
			<input type="text" name="amount[]" readonly id="amount_<?php echo $i;?>" 
			value="<?php echo $itemamount; ?>"  style="width:80px;" /></td>
		</tr>
		<?php $i++;} ?>
		<input type="hidden" name="count" id="count" value="<?php echo $i; ?>" />
</table>
	</td>
</tr>
</table>
<p align="left"><b>Assembly Item details</b></p>
<table align='center' width='100%;' border='1' cellpadding='3' cellspacing='0'>
</tr>
<tr class='row1'>
<td class='tabledata'>Sr. No.</td>
<td class='tabledata'>&nbsp;</td>
<td class='tabledata'>Item Code</td>
<td class='tabledata'>Item Name</td>
<td class='tabledata'>Qty.</td>
<td class='tabledata'>Price</td>
<td class='tabledata'>Per Unit</td>
<td class='tabledata'>Value</td>
<td class='tabledata'>Discount Type</td>
<td class='tabledata'>Discount Per</td>
<td class='tabledata'>Discount Price</td>
<td class='tabledata'>Value After Discount</td>

</tr>
<?php 
$sql_hrd=DB_query("select * from salesorder_hardware_items where parant_id='".$masterid."'",$db);
while($row=DB_fetch_array($sql_hrd))
{

$item_Codeas=$s->GetFieldValue('item_master',"id='".$row['itemCode']."'",'StockID');
$item_names=$s->GetFieldValue('item_master',"id='".$row['itemCode']."'",'item_name');
?>
<tr class='row1'>
<td style='white-space:nowrap;' class='tabledata'><?php echo $i; ?></td>	
<td class='tabledata'><input type='hidden' name='item_code[]' id="itemid1_<?php echo $i; ?>" style='width:60px;' 
value="<?php echo $row['itemCode'] ?>" /></td>
<td class='tabledata'><input type='text' style='width:60px;' value="<?php echo $item_Codeas; ?>"  />
<input type='hidden'   id="stockqty1_<?php echo $i ?>"   value="<?php echo $row['stock_qty']; ?>"  /></td>
<td class='tabledata' style='white-space:nowrap'><input type='text' style='width:90px;'   id=''  value="<?php echo $item_names; ?>"  /></td>


<td class='tabledata'><input type='text' name='stock_qty[]'  id="stockqty_<?php echo $i; ?>" style='width:50px;'
 value="<?php echo $row['quantity']; ?>"  onkeyup='calcValue1(this.value,this.id)'/></td>
<td class='tabledata'>
<input type='text' name='stock_price[]' value="<?php echo $row['price']; ?>" id="stock_price_<?php echo $i; ?>"  style='width:50px;'/></td>
<td class='tabledata'><input type='text' name='stock_perunit[]' onfocus='filldefaultAmt(this.value,this.id)' id="stock_perunit_<?php echo $i; ?>" style='width:30px;' value="<?php echo $row['PerUnits']; ?>"/></td>
<td class='tabledata'><input type='text' name='stock_value[]' value="<?php echo $row['totalValue']; ?>" id="stock_value_<?php echo $i; ?>" style='width:60px;' onfocus='CalculateTotalLeadAmt(this.value,this.id)'/></td>
<td class='tabledata'><select style='width:80px;' name='PerAmo[]' id="PerAmo_<?php echo $i; ?>"  >
            <option value='Amount' >Amount</option>
            <option value='PercentAmt'>Percent Amt</option>
          </select></td>
<td class='tabledata'><input type='text' style='width:50px' name='Discount[]' id="Discount_<?php echo $i; ?>" onkeyup='DiscountAmount1(this.value,this.id)' value="<?php echo $row['discount_per']; ?>"/></td>
<td class='tabledata'><input type='text' style='width:50px' name='perc[]' id="perc_<?php echo $i; ?>"  readonly='true' 
value="<?php echo $row['after_dis']; ?>" /></td>
<td class='tabledata'><input type='text' style='width:70px' name='value_dis[]' id="value_dis_<?php echo $i; ?>"  readonly='true'
value="<?php echo $row['stock_value']; ?>" /></td>
</tr>
<?php 
$i++;}
?>
<input type='hidden' name='count1' id='count1' value="<?php echo $i; ?>">
</table>

</table>



<table width="100%" cellpadding="3" cellspacing="0" border="1">
<tr class="row1">
	<td width="50%" valign="top">
		<table width="100%" border="0" cellspacing="0" cellpadding="3" align="left">
			<tr class="row1">
				<td class="tabledata1" style="text-align:center;" colspan="2">Terms And Conditions</td>
			</tr>
			<?php $i=0;
			$SQLTerms=DB_query("SELECT * FROM terms_condition where parant_id=0 and CompanyId='".$_SESSION['ACGCompanyId']."' and frm_id='".$_REQUEST['frmid']."'",$db); 
			while($TermsAndCond=DB_fetch_array($SQLTerms)){?>
			<tr class="row1">
				<td class="tabledata1"><?php echo $TermsAndCond['terms_conditions']; ?>
				<input type="hidden" name="terms[]" id="terms" value="<?php echo $TermsAndCond['id']; ?>" />
				</td>
				<td><select name="Terms_Condition[]" id="Terms_Condition">
						<option value="">Select</option>
						<?php $SQLCond=DB_query("SELECT * FROM terms_condition where parant_id='".$TermsAndCond['id']."' and CompanyId='".$_SESSION['ACGCompanyId']."' and frm_id='".$_REQUEST['frmid']."'",$db);
						while($GetCond=DB_fetch_array($SQLCond)){?>
						<option value="<?php echo $GetCond['id']; ?>"><?php echo $GetCond['terms_conditions']; ?></option>
						<?php }?>
				</select></td>
			</tr>
<?php $i++;}?>
		</table>
	</td>
	<td width="50%">
		<table width="100%" border="0" cellspacing="0" cellpadding="3" align="right">
			<tr class="row1">
				<td class="tabledata1" style="text-align:center;" colspan="4">Amount And Taxes</td>
			</tr>
			
			
			
			
			<tr class="row1">
					<td class="tabledata1">Basic Amount<label><font color="#FF0000">*</font></label></td>
					<td colspan="3"><input name="basicAmount" type="text" value="<?php echo $basicAmount;?>" style="width:150px;" onfocus="calculateBasic()" id="basicAmount"   /></td>
			  </tr>
			<!--	  <tr class="row1">
					<td class="tabledata1" >Discounted Price<label><font color="#FF0000">*</font></label></td>
					<td><input name="totalDiscountedPrice" id="totalDiscountedPrice" type="text" value="<?php echo $totalDiscountedPrice;?>" style="width:150px;" onfocus ="calculateDiscounted()"/>
					
					
					</td>
					
				</tr>-->
			<tr class="row1">
			<td>Discount Type</td>
			<td><select  name='PerAmount' id='PerAmount' style="width:150px;" >
            <option value='Amount' >Amount</option>
            <option value='PercentAmt'>Percent Amt</option>
          </select></td>
			<td><input type="text" style="width:35px" name="Discount_amount" id="Discount_amount" 
onkeyup="DiscountAmountvalue(this.value)" value="0" /></td>
			<td>&nbsp;</td>
			</tr>
		  <tr class="row1">
<td>Discount Amt</td><td><input type="text" style="width:150px" name="discountperc" id="discountperc"  readonly="true"  /></td>
			<td>Discounted Amt</td>
			<td><input type="text" style="width:150px" name="afterdiscountprice" id="afterdiscountprice"  readonly="true"  /></td>
		  </tr>	
			
			<tr class="row1">
					<td class="tabledata1">Excise Duty</td>
					<td>
				  <select name="EDBillRate" id="EDRate" style="width:150px" onchange="calculateBillExcise(this.value)"  >
				<option value="0">--Select--</option>
				<?php
				$ServiceSql="select * from  excise_tax WHERE caption LIKE '%excise%'" ;
				$ServiceRes=DB_query($ServiceSql,$db);
				while($ServiceRow=DB_fetch_array($ServiceRes))
				{
				?>
				<option value="<?php echo $ServiceRow['id']?>"><?php echo $ServiceRow['caption']?></option>
				<?php	
				}
				
				?>
				</select>							</td>
				    <td>Excise Amt </td>
				    <td><input name="totalDiscountedPrice" id="totalDiscountedPrice" 
				type="text"  style="width:150px;" onfocus="calculateTotalExcise()"/>	</td>
			</tr>
			<?php 
			$ecess_id=array();
			$cessSql=DB_query("Select caption,id from excise_tax where caption like '%cess%'",$db);
			$k=1;
			while($cessRow=DB_fetch_array($cessSql))
			{
			$caption[$k]=$cessRow['caption']; 
			$ecess_id[$k]=$cessRow['id'];
			$rate_sql=DB_query("Select def_rate from excise_tax where caption='".$caption[$k]."'",$db);
			while($rate_row=DB_fetch_array($rate_sql))
			{
			$rate[$k]=$rate_row['def_rate'];
			}
			$k++;
			} 
			?>
			<tr class="row1">
					<td class="tabledata1">E-Cess</td>
					<td><input type="hidden" name="ecessid" value="<?php echo $ecess_id[1];?>" />
					
					<input name="ECessPercentage" id="ECessPercentage" type="text" value="<?php echo $rate[1];?>" style="width:150px;" readonly />						</td>
					<td><span class="tabledata1">E-Cess</span> Amt </td>
					<td><input name="ECessValue" id="ECessValue" type="text" value="<?php echo $ECessValue?>" style="width:150px;"readonly onfocus="calculateECess()"/></td>
			</tr>
			<tr class="row1">
					<td class="tabledata1" >SAH-E Cess </td>
					<td><input type="hidden" name="sahecessid" value="<?php echo $ecess_id[2];?>" />
					<input name="SAHCessPercentage" id="SAHCessPercentage" type="text" value="<?php echo $rate[2];?>" style="width:150px;" readonly/>						</td>
					<td><span class="tabledata1">SAH-E Cess</span> Amt </td>
					<td><input name="SAHCessValue" id="SAHCessValue" type="text" value="<?php echo $SAHCessValue?>" style="width:150px;" readonly onfocus="calculateSAHCess()"/></td>
			</tr>
			<tr class="row1">
			<td class="tabledata1">Taxable Amount<label><font color="#FF0000">*</font></label></td>
			<td><input name="taxableAmount" id="taxableAmount" type="text" value="<?php echo $taxableAmount?>" style="width:150px;" onfocus="calculateTaxable()"/>			</td>
		    <td class="tabledata1">Hardware Amount</td>
			<td><input name="hardwareamount" id="hardwareamount" type="text" style="width:150px;" onfocus=""/>			</td>
		    </tr>
		 
			<tr class="row1">
					<td class="tabledata1">Sales Tax (Taxable Amt) </td>
					<td>
					<select name="VatCst" id="value3" style="width:150px;" onchange="calculateVATCST(this.value)">
				<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="" /></td>
				    <td class="tabledata1">Hardware taxable Amount</td>
					<td><input name="hardware_tax_amount" id="hardware_tax_amount" type="text" style="width:150px;" onfocus=""/>			</td>
		      </tr>
				
				<tr class="row1">
					<td class="tabledata1">Total Amt I </td>
					<td ><input type="text" name="total_taxable_amt" id="total_taxable_amt" style="width:150px;" onfocus="calculatetaxable_tax()"  /></td>
					<td colspan="2" class="tabledata1">&nbsp;</td>
				</tr>
				
				<tr class="row1">
					<td class="tabledata1">Freight Amt </td>
					<td colspan="3"><input type="text" name="freight" id="freight"  style="width:150px;" /></td>
				</tr>
				
				
		 <!--   	  <tr class="row1">
					<td class="tabledata1">Freight</td>
					<td><input name="freightValue" id="value2" type="text" value="<?php echo $freightValue?>" style="width:150px;" /></td>
					
				</tr>
				  <tr class="row1">
					<td class="tabledata1">Pkg Charges</td>
					<td><input name="packagingCharges" id="value1" type="text" value="<?php echo $packagingCharges;?>" style="width:150px;" /></td>
					
				</tr>
				<tr class="row1">
					<td class="tabledata1">Additional Charges</td>
					<td><input name="additionalCharges" id="value4" type="text" value="<?php  echo $additionalCharges;?>" style="width:150px;" /></td>
					
				</tr>
				  <tr class="row1">
					<td class="tabledata1">Round Off</td>
					<td><input name="roundOff" id="roundOff" type="text" value="<?php echo $roundOff;?>" readonly="true" style="width:150px;" /></td>
					
				</tr>-->
			<tr class="row1">
					<td class="tabledata1">Grand Total<label><font color="#FF0000">*</font></label></td>
					<td colspan="3"><input name="grandTotal" id="grandTotal" type="text"  style="width:150px;" onfocus="calculateGrand()" readonly="readonly" value="<?php echo $grandTotal  ;?>" /></td>
			  </tr>
			</table>
	</td>
</tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save" onclick="return validate()" class="search" />&nbsp;<input type="submit" name="Cancel" value="Cancel" class="search" /></p>
</form>
</div>
</div>
<script>
function DiscountAmount1(DA,str)
{
  var k=str.split("_");
  
  if(k[1]==undefined)
  {
   
	var discount_percent = document.getElementById("PerAmo").value;
	var discount = document.getElementById("Discount").value;
	var total = document.getElementById("stock_value").value;
	}
	else
	{
	var i=k[1];
	
	var discount_percent = document.getElementById("PerAmo_"+i).value;
	var discount = document.getElementById("Discount_"+i).value;
	var total = document.getElementById("stock_value_"+i).value;
	}
	if(discount_percent=='PercentAmt')
	{
		if(discount==''){discount=0; var percent=0;}
		else
		{
		var percent = parseFloat(DA) * parseFloat(total)/100;
		}
		var subTotal = parseFloat(total)-parseFloat(percent);
	}
	else if(discount_percent=='Amount')
	{ 
	
	if(discount==''){discount=0; var percent=0;}
	else
	{
	var percent = parseFloat(DA);
	}
	var subTotal = parseFloat(total)-percent;
	
	}	
	if(k[1]==undefined)
	{
	document.getElementById("perc").value=percent.toFixed(2);
	document.getElementById("value_dis").value=subTotal.toFixed(2);
	}
	else
	{
	document.getElementById("perc_"+i).value=percent.toFixed(2);
	document.getElementById("value_dis_"+i).value=subTotal.toFixed(2);
	}
	
}

function DiscountAmountvalue(DA)
{

var discount_percent = document.getElementById("PerAmount").value;
	var discount = document.getElementById("Discount_amount").value;
	var total = document.getElementById("basicAmount").value;
	
	if(discount_percent=='PercentAmt')
	{
		if(discount==''){discount=0;var percent=0;}
		else
		{
		var percent = parseFloat(DA) * parseFloat(total)/100;
		}
		var subTotal = parseFloat(total)-parseFloat(percent);
	}
	else if(discount_percent=='Amount')
	{ 
	
	if(discount==''){discount=0;
	var percent=0;
	}
	else
	{
	 percent = parseFloat(DA);
	 }
	var subTotal = parseFloat(total)-percent;
	
	}		
	document.getElementById("discountperc").value=percent.toFixed(2);
	document.getElementById("afterdiscountprice").value=subTotal.toFixed(2);
	
}


function calculateVATCST_hrd(str)
{
	if (str=='')
	{
	document.getElementById("rate1").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("rate1").value=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","calculateExcise_hrd.php?salesId="+str,true);
	xmlhttp.send();
}


function calculatetaxable_tax()
{
var salestaxamt=document.getElementById("VatCstValue").value;
var taxableamt=document.getElementById("taxableAmount").value;
	if(salestaxamt==''){salestaxamt=0;}
	if(taxableamt==''){taxableamt=0;}
	
var totalamt=parseFloat(salestaxamt)+parseFloat(taxableamt);	
document.getElementById("total_taxable_amt").value=totalamt.toFixed(2);
}

function calculatetaxable_hrd()
{
var salestaxamt_hrd=document.getElementById("VatCstValue1").value;
var taxableamt_hrd=document.getElementById("hrdtotalAmt").value;
	if(salestaxamt_hrd==''){salestaxamt_hrd=0;}
	if(taxableamt_hrd==''){taxableamt_hrd=0;}
	
var totalamt=parseFloat(salestaxamt_hrd)+parseFloat(taxableamt_hrd);	
document.getElementById("total_taxable_hrd").value=totalamt.toFixed(2);

}




</script>