<?php $PageSecurity = 11;//echo $_SERVER['QUERY_STRING'];exit;
include('includes/DefineSuppTransClass.php');
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');
include('includes/session.inc');
$sql="Select ReceivedAgainst,PO_WO_MTO,BillNo,Type,DocumentType from addmrn where id='".$_GET['mrnno']."'";
$result=DB_query($sql,$db);
$mrnrow=DB_fetch_array($result);
$data1='';

if($mrnrow['DocumentType']=='Bill')
{
$data1.="<input name='bill_no' type='text' value='".$mrnrow['BillNo']."' style='width:200px;' readonly='true'/>";
}
else
{
$data1.="<input name='bill_no' type='text' value='' style='width:200px;'/>";
}


$data2="";

if($mrnrow['DocumentType']=='Challan')
{
$data2.="<input name='challan_no' type='text' value='".$mrnrow['BillNo']."' style='width:200px;' readonly='true'/>";
}
else
{
$data2.="<input name='challan_no' type='text' value='' style='width:200px;' />";
}




$data3="";
$data3.="<table width='100%' border='1' cellspacing='2' cellpadding='3' style='border:solid 1px #999999;'>";
  $data3.="<tr class='row1'>";
    $data3.="<td class='tabledata'><strong>ITEM CODE</strong></td>";
    $data3.="<td class='tabledata'><strong>DESCRIPTION</strong></td>";
    $data3.="<td class='tabledata'><strong>UOM</strong></td>";
	if($mrnrow['ReceivedAgainst']=='Without PO')
	{$data3.="<td class='tabledata'><strong>PR QTY </strong></td>";}
	else
    {$data3.="<td class='tabledata'><strong>PO QTY </strong></td>";}
    $data3.="<td class='tabledata'><strong>QTY AS PER BILL </strong></td>";
    $data3.="<td class='tabledata'><strong>QTY ACCEPTED </strong></td>";
    $data3.="<td class='tabledata'><strong>SHORT QUANTITY </strong></td>";
    $data3.="<td class='tabledata'><strong>RATE</strong></td>";
	$data3.="<td class='tabledata'><strong>Total Price</strong></td>";	
	$data3.="<td class='tabledata'><strong>Discount Percent</strong></td>";
	$data3.="<td class='tabledata'><strong>Discount</strong></td>";
	$data3.="<td class='tabledata'><strong>Discount Amount</strong></td>";
	$data3.="<td class='tabledata'><strong>Sub Total</strong></td>";
	$data3.="<td class='tabledata'><strong>SPECIAL DISCOUNT</strong></td>";
	$data3.="<td class='tabledata'><strong>SPECIAL DISCOUNT AMOUNT</strong></td>";
	$data3.="<td class='tabledata'><strong>BASIC AMOUNT</strong></td>";
	$data3.="<td class='tabledata'><strong>Tax Type</strong></td>";
	$data3.="<td class='tabledata'><strong>Excise</strong></td>";
	$data3.="<td class='tabledata'><strong>E-Cess</strong></td>";
	$data3.="<td class='tabledata'><strong>SAH-E Cess</strong></td>";
	$data3.="<td class='tabledata'><strong>Taxable Amount </strong></td>";
	$data3.="<td class='tabledata'><div style='width:150px;'><strong>VAT/CST</strong></div></td>";
	$data3.="<td class='tabledata'><strong>VAT/CST Amount</strong></td>";
	$data3.="<td class='tabledata'><strong>TOTAL AMOUNT</strong></td>";    
	$data3.="<td class='tabledata'><strong>REMARKS</strong></td>";
  $data3.="</tr>";
  $i=1;
  $totalbasic=0;$totalexcise=0;$totalEcess=0;$totalsahcess=0;$totaltaxable=0;$totalvatamt=0;
  $sql2=DB_query("select * from addmrnqty where issue_id='".$_GET['mrnno']."'",$db);
  while($rowdata=DB_fetch_array($sql2))
  {  			
  	
 	$makename=$s->getField('product_manufacture','id',$rowdata['Make'],'manufacturer');
 	$gradename=$s->getField('product_grade','id',$rowdata['Gread'],'grade');
 	$colorname=$s->getField('product_color','id',$rowdata['Color'],'color'); 
 	$unitname=$s->getField('unitsofmeasure','unitid',$rowdata['UOM'],'unitname');
 	$poid=$s->getField('addmrn','id',$_GET['mrnno'],'PO_WO_MTO');
 	
	if($mrnrow['PO_WO_MTO']!='')
	{
		$get_po_item_details=DB_query("Select * from purchorderdetails where orderno='".$mrnrow['PO_WO_MTO']."' and mst_itemid='".$rowdata['mst_itemid']."'",$db);
		$details_row=DB_fetch_array($get_po_item_details);
		if(isset($details_row)){
		extract($details_row);}
		$total_price=$rate*$rowdata['reciveqty'];
		
		if(discount_type=='Amount')
		{$disc_amt=$discount;}
		elseif(discount_type=='PercentAmt')
		{$disc_amt=$total_price*($discount/100);}
		else{$disc_amt='0';}
		
		$subtotal=$total_price-$disc_amt;
		$special_disc_amt=$subtotal*($specialdiscount/100);
		$basic_amt=$subtotal-$special_disc_amt;
		
		$excise_val=$basic_amt*($ExcisePercentage/100);
	}
	else
	{
		$rate='';
		$total_price='';
		$discount='';
		$disc_amt='';
		$subtotal='';
		$specialdiscount='';
		$special_disc_amt='';
		$excise_val='';
		$e_cess_val='';
		$sah_val='';
	}
	
	$item_code=$s->getField('item_master','id',$rowdata['mst_itemid'],'StockID');
   $item_name=$s->getField('item_master','id',$rowdata['mst_itemid'],'item_name');
  
  $data3.="<tr bgcolor='#FFFFFF'><td>
  <input type='hidden' name='ProductID[$i]' value='".$item_code."'> 
  <input type='hidden' name='item_id[$i]' value='".$rowdata['mst_itemid']."'>".$item_code."</td>";
  $data3.="<td><input type='hidden' name='ProductDes[$i]' value='".$item_name."'>".$item_name."</td>";
      $data3.="<td><input type='hidden' name='UOM[$i]' value='".$rowdata['UOM']."'>".$unitname."</td>";
       $data3.="<td>".$rowdata['POQty']."</td>";
        $data3.="<td>".$rowdata['qty_as_per_bill']."<input type='hidden' name='qty_as_per_bill[".$i."]' id='qty_as_per_bill_".$i."' value='".$rowdata['qty_as_per_bill']."'></td>";
         $data3.="<td><input type='hidden' name='quantity[$i]' id='Quantity_$i' value='".$rowdata['reciveqty']."'>".$rowdata['reciveqty']."</td>";
          $data3.="<td>".$rowdata['short_accessid']."<input type='hidden' name='short_qty[".$i."]' id='short_qty_".$i."' value='".$rowdata['short_accessid']."' style='width:100%;'></td>";
           $data3.="<td><input type='text' name='rate[$i]' id='Rate_$i' value='".$rate."' onchange='calamt($i)'></td>";
           
            $data3.="<td><input type='text' name='total_price[$i]' id='totalValue_$i' value='".$total_price."'  onfocus='calculateTotalPrice($i)'></td>";
            $data3.="<td><select style='width:100%;' name='discount_type[$i]' id='PerAmo_$i'>";
			if($discount_type=='Amount')
			{$c="selected";}
			else if($discount_type=='PercentAmt')
			{$p="selected";}
            $data3.="<option value='Amount' ".$c.">Amount</option>";
            $data3.="<option value='PercentAmt' ".$p." >Percent Amt</option>";
          $data3.="</select></td>";
           $data3.="<td><input type='text' name='discount[$i]' id='Discount_$i' onblur='DiscountAmount(this.value,$i)' style='width:100%;' value='".$discount."'  /></td>";
            $data3.="<td><input type='text' name='discount_amount[$i]' id='perc_$i'  readonly='true' style='width:100%;' onblur='checkamount(this.value,$i)'
            value='".$disc_amt."' /></td>";
            $data3.="<td><input type='text' id='NetRate_$i' name='sub_total[$i]' style='width:100%;' readonly value='".$subtotal."' /></td>";
            $data3.="<td><input type='text' name='specialdiscount[$i]' id='SpecialDiscount_$i' size='10' style='width:100%;' onblur='DiscountAmountspe(this.value,$i)' value='".$specialdiscount."' /></td>";
             $data3.="<td><input type='text' name='specialdiscount_amount[$i]' id='Specialperc_$i'  style='width:100%;' size='10' onblur='checkspecialamt(this.value,$i)' value='".$special_disc_amt."' /></td>";
             $data3.="<td><input type='text' id='basic_$i' name='basic_amt[$i]' size='8' style='width:100%;' readonly value='".$basic_amt."' /></td>";
            $data3.="<td><select name='tax_type[$i]' style='width:70px' id='hid_billing_type_$i'  onchange='checkExcisable(this.value,$i)' onkeyup='checkExcisable(this.value,$i)'>";
	          $data3.="<option value=''>Select</option>";
	          if($tax_type=='NonExcisable')
			  {$d="selected";}
           	  else if($tax_type=='Excisable')
		   	  {$e="selected";}
			  else if($tax_type=='IncludeTax')
			  {$f="selected";}
				 $data3.="<option value='NonExcisable'".$d.">NonExcisable</option>";
				 $data3.="<option value='Excisable' ".$e.">Excisable</option>";
				 $data3.="<option value='IncludeTax' ".$f.">IncludeTax</option>";
				 $data3.="</select></td>";
			 $data3.="<td><select name='Excise_Duty[$i]' id='exciseDuty_$i' style='width:100%;' onchange='claculateExcise($i,this.value)' onfocus='checkexiseflag($i)' onkeyup='claculateExcise($i,this.value)'   >";
    	   $data3.="<option value=''>Select</option>";   	    
    	    $excise_sql=DB_query("Select * from excise_tax where caption like '%excise%' and form_type='Purchase'",$db);
			while($excise_row=DB_fetch_array($excise_sql))
			{
			$g='';
			if($Excise_Duty==$excise_row['id'])
			{$g='selected';}	
				 $data3.="<option value=".$excise_row['id']." ".$g.">".$excise_row['caption']."</option>";
			}
    	    
  	       $data3.="</select>";
  	      $data3.="<div id='exciseId_$i' style='width: 100px;'>";
  	      	$data3.="<input name='ExcisePercentage[$i]' id='ExcisePercentage_$i' type='text' value='".$ExcisePercentage."'  style='width:80%;' readonly />"; 
  	      	$data3.="<input name='exciseValue[$i]' id='exciseValue_$i' type='text' value='".$excise_val."' style='width:70%;' onfocus='calculateExciseDuty($i)'  onkeyup='calculateExciseDuty($i)'  readonly /></div>";
  	       $data3.="</td>";
    	     $cessSql=DB_query("Select caption from excise_tax where caption like '%cess%'",$db);
			 $k=1;
			 while($cessRow=DB_fetch_array($cessSql))
			 {
			 	$caption[$k]=$cessRow['caption']; 
				
				$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++;
			 } 
			 
			 $cessLedgerSql1=DB_query("Select * from excise_tax where ledgertype='cess' LIMIT 0,1",$db);	 
		$cessLedgerSql2=DB_query("Select * from excise_tax where ledgertype='cess' LIMIT 1,1",$db);	 	 
			 
			 if($mrnrow['PO_WO_MTO']!=''){
			 $e_cess_val=$excise_val*($rate[1]/100); 
			 $sah_val=$excise_val*($rate[2]/100);
			 }
			 else
			 {
			 $e_cess_val='';
			 $sah_val='';
			 }
$data3.="<td><select name='cess_ID1[".$i."]' style='width:70px;' id='cess_ID1_$i'   >";
	while($cessLedger1=DB_fetch_array($cessLedgerSql1)){
		$data3.="<option value='".$cessLedger1['id']."'>".$cessLedger1['caption']."</option>";
	}
	$data3.="</select><input name='Edu_Cess_perc[$i]' id='ECessPercentage_$i' type='text' value=".$rate[1]." style='width:100%;' readonly /><input name='Edu_Cess[$i]' id='ECessValue_$i' type='text' value='".$e_cess_val."' size='25' style='width:100%;' readonly onfocus='calculateECess($i)' onkeyup='calculateECess($i)'    /></td>";
	
$data3.="<td><select name='cess_ID2[".$i."]'  style='width:70px;'  id='cess_ID2_$i'    >";
	while($cessLedger2=DB_fetch_array($cessLedgerSql2)){
		$data3.="<option value='".$cessLedger2['id']."'>".$cessLedger2['caption']."</option>";
	}
	$data3.="</select>
<input name='SHE_Cess_perc[$i]' id='SAHCessPercentage_$i' type='text' value=".$rate[2]." style='width:100%;' readonly/><input name='SHE_Cess[$i]' id='SAHCessValue_$i' type='text' value='".$sah_val."' size='25' style='width:100%;' readonly onfocus='calculateSAHCess($i)' onkeyup='calculateSAHCess($i)'    /></td>";

$taxable_amt=$basic_amt+$excise_val+$e_cess_val+$sah_val;
$get_vat_rate=DB_query("Select taxrate from sales_tax_master where salestax_id='".$vat_cst."'",$db);
$vat_row=DB_fetch_array($get_vat_rate);
$vat_sct_amt=$taxable_amt*($vat_row['taxrate']/100);

$data3.="<td><input type='text' name='taxable_amt[$i]' id='taxable_amt_$i' value='".$taxable_amt."' style='width:100%;' onfocus='calculateTaxable($i)'      readonly/></td>";
	$Taxsql='SELECT salestax_id,tax_code, taxrate FROM sales_tax_master';
	$Taxresult=DB_query($Taxsql,$db);
    $data3.="<td>";
	$data3.="<select id='taxname_$i' name='vat_cst[$i]' onchange='tax_rate(this.value,$i)' onkeyup='tax_rate(this.value,$i)'    style='width:100%;'>";
	$data3.="<option value='0'>---Select---</option>";
    
			while($Taxmyrow=DB_fetch_array($Taxresult)){
                $data3.="<option  value=".$Taxmyrow['salestax_id']." ".($vat_cst==$Taxmyrow['salestax_id']?'selected':'').">".$Taxmyrow['tax_code']." --- ".$Taxmyrow['taxrate']."%"."</option>";
                  } 
             $data3.="</select></td>";
 
				$Taxsql='SELECT salestax_id,tax_code, taxrate FROM sales_tax_master';
				$Taxresult=DB_query($Taxsql,$db);
              $data3.="<td><div id='taxval_$i'>";
                  $data3.="<input type='text' name='vat_cst_amt[$i]' id='taxvalue_$i' style='width:100%;' value='".$vat_sct_amt."' readonly='true'/>";
              $data3.="</div>";
			$data3.="</td>";
			
			  if($tax_type=='NonExcisable')
			  {$total_amount=$basic_amt+$vat_sct_amt;}
           	  else if($tax_type=='Excisable')
		   	  {$total_amount=$taxable_amt+$vat_sct_amt;}
			  else if($tax_type=='IncludeTax')
			  {$total_amount=$basic_amt;}
			  
			$total_amount=$taxable_amt+$vat_sct_amt;
			
			$data3.="<td><input type='text' id='TotalSpecial_$i' name='total_amount[$i]' size='8' style='width:100%;' readonly  onfocus='cal_grandtotal($i);' value='".$total_amount."' /></td>";
		   		    $data3.="<td>".$rowdata['remarks']."<input type='hidden' name='item_remarks[$i]' value='".$rowdata['remarks']."' /></td>";
			    $data3.="</tr>";
			   $i++; }
  $data3.="<input type='hidden' name='count' id='count' value=".$i.">";
  $data3.="</table>";
  
  $venderdata1=DB_query("select * from venderquotations where id='".$venderqtyid."' ",$db);
 $venderrow1=DB_fetch_array($venderdata1);

echo $data=$data1."__".$data2."__".$data3;
//$sql2=DB_query("select * from addmrnqty where issue_id='".$_GET['mrnno']."'",$db);
?>
