<link rel="stylesheet" href="css/new_mxerp.css">
<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
include('includes/DefinePOClass.php');
$title = _('Import Purchase Order List');
$PageName ='Import Purchase Order List';
if($_REQUEST['id']!='' && $_REQUEST['act']=='delete')
{
	$delete1=DB_query("Delete from import_purchaseorder where id='".$_REQUEST['id']."'",$db);
	$delete2=DB_query("Delete from import_purchaseorder_details where masterId='".$_REQUEST['id']."'",$db);
	prnMsg( _('Record Deleted Successfully'),'success');
echo "<script>location.href='index.php?route=ImportPOList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}

if(isset($_POST['Update']))
{
$insert1="update  import_purchaseorder set
								site='".$_REQUEST['site']."',    
								vendor='".$_REQUEST['vendor']."',  
								orderPrefix='".$_REQUEST['orderPrefix']."',  
								orderDigit='".$_REQUEST['orderDigit']."',  
								orderNo='".$_REQUEST['orderPrefix']."-".$_REQUEST['orderDigit']."',  
								orderDate='".FormatDateForSQL($_REQUEST['orderDate'])."',  
								basicAmount='".$_REQUEST['basicAmount']."',  
								grandTotal='".$_REQUEST['grandTotal']."',
								remarks='".$_REQUEST['remarks']."'
								where id='".$_REQUEST['id']."'";
							
	$result1=DB_query($insert1,$db);
	//$lastId=DB_Last_Insert_ID($db,'import_purchaseorder','id');
//	if($lastId!='')

	//{
		for($str=0;$str<$_REQUEST['count'];$str++)
		{
			 $insert2="update  import_purchaseorder_details set
											quantity='".$_REQUEST['quantity'][$str]."',    
											rate='".$_REQUEST['rate'][$str]."', 
											currency='".$_REQUEST['currency'][$str]."',  
											discountType='".$_REQUEST['discountType'][$str]."',  
											discount='".$_REQUEST['discount'][$str]."',  
											discountAmt='".$_REQUEST['discountAmt'][$str]."',  
											subTotal='".$_REQUEST['subTotal'][$str]."'
											 where id='".$_REQUEST['id1'][$str]."'";
			$result2=DB_query($insert2,$db);
		}
	//}	
	
	
}



?>
<?php 
if($_REQUEST['id']!='' && $_REQUEST['act']=='edit')
{
$sql=DB_query("select * from import_purchaseorder where id='".$_REQUEST['id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$myrow=DB_fetch_array($sql);
?>




<div class="body_contain">
<div class="tab_div">
<h1>Edit Import Purchase Order</h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
	  <tr class="row1">
        <td class="tabledata1">Branch</td>
		<td><select name="site" style="width:110px" onchange="generatePO(this.value)">
		<option value="0">Select</option>
		<?php
		$site_sql=DB_query("Select loccode,locationname from locations where Companyid='".$_SESSION['ACGCompanyId']."'",$db);
		while($site_row=DB_fetch_array($site_sql))
		{
		$k='';
		if($site_row['loccode']==$myrow['site'])	
		{
		$k="selected";	
		}
		?>
		<option value="<?php echo $site_row['loccode'];?>"<?php echo $k; ?>><?php echo $site_row['locationname'];?></option>
		<?php 
		}
		?>
		  </select>	
			
			</td>
        <td class="tabledata1">Po No. </td>
		<td id="POID"><input type="text" value="<?php echo $myrow['orderPrefix'];?>" size="10"  name="orderPrefix"/>
-		  <input  type="text" value="<?php echo $myrow['orderDigit'];?>" size="5"  name="orderDigit"/></td>
      </tr>
      <tr class="row1">        
        <td class="tabledata1">Vendor</td>
		<td><select name="vendor" style="width:110px">
		<option value="0">Select</option>
		<?php 
		$vendor_sql=DB_query("Select vendor_code,vendor_name from suppliers where Companyid='".$_SESSION['ACGCompanyId']."'",$db);
		while($vendor_row=DB_fetch_array($vendor_sql))
		{
			$t1='';
			if($myrow['vendor_code']==$myrow['vendor'])
			{
			$t1="selected";	
			}
		?>
		<option value="<?php echo $vendor_row['vendor_code'];?>"<?php echo $t1; ?>><?php echo $vendor_row['vendor_name'];?></option>
		<?php 
		}
		?>
        </select>			
			</td>
		<td class="tabledata1">Po Date </td>
		<td><input type="text" value="<?php echo $myrow['orderDate'];?>" class="datepicker" /></td>
      </tr>
  </table>

  <div style="overflow:auto; width:810px ; height:<?php echo $height;?>"  >
  <table width="99%" border="1" cellspacing="0" cellpadding="3">
    <tr>
      <td class="tabledata">Item Code </td>
      <td class="tabledata">Item Name </td>
      <td class="tabledata">Description</td>
      <td class="tabledata">Item No. </td>
      <td class="tabledata">Make</td>
      <td class="tabledata">Color</td>
      <td class="tabledata">Grade</td>
	  <td class="tabledata">Size</td>
	  <td class="tabledata">GSM</td>
	   <td class="tabledata">Quantity</td>
      <td class="tabledata">Rate</td>
	   <td class="tabledata">Currency</td>
      <td class="tabledata">Discount Type</td>
      <td class="tabledata">Discount</td>
      <td class="tabledata">Discount Amount</td>
      <td class="tabledata">Sub Total </td>
      <td class="tabledata">Flexo Offset</td>
      <td class="tabledata">Od</td>
      <td class="tabledata">Length</td>
      <td class="tabledata">Width</td>
      <td class="tabledata">Unit</td>
      <td class="tabledata">Serial No.</td>
      <td class="tabledata">Orignal/Local</td>
      <td class="tabledata">Type</td>
      <td class="tabledata">Shade</td>
      <td class="tabledata">Running Meter/Roll</td>
      <td class="tabledata">Volume</td>
      <td class="tabledata">Teeth</td>
      <td class="tabledata">Bcm</td>
      <td class="tabledata">Ups</td>
      <td class="tabledata">Quality</td>
      </tr>
    <?php 
	$detailsSql=DB_query("Select * from import_purchaseorder_details where masterId='".$_REQUEST['id']."'",$db);
  $i=0;
  while($detailsRow=DB_fetch_array($detailsSql))
  {
  	//echo "select * from item_master where StockID='".$pp[$i]."'";
	$Query=DB_query("select * from item_master where StockID='".$detailsRow['itemCode']."' and Companyid='".$_SESSION['ACGCompanyId']."'",$db);
  	$Query_row=DB_fetch_array($Query);
	
	$_REQUEST['quantity'][$i]=$detailsRow['quantity'];
	$_REQUEST['rate'][$i]=$detailsRow['rate'];
	$_REQUEST['currency'][$i]=$detailsRow['currency'];
	$_REQUEST['discountType'][$i]=$detailsRow['discountType'];
	$_REQUEST['discount'][$i]=$detailsRow['discount'];
	$_REQUEST['discountAmt'][$i]=$detailsRow['discountAmt'];  
	$_REQUEST['subTotal'][$i]=$detailsRow['subTotal'];
  ?>
    <tr bgcolor="#c1c1c1">
      <td><?php echo $detailsRow['itemCode'];?><input type="hidden" name="itemCode[<?php echo $i;?>]" id="itemCode_<?php echo $i;?>" value="<?php echo $detailsRow['itemCode'];?>" /></td>
      <td style="white-space:nowrap"><?php echo $Query_row['item_name'];?></td>
      <td style="white-space:nowrap"><?php echo $Query_row['item_desc'];?></td>
      <td><?php echo $Query_row['item_no'];?></td>
     <td>
	 <?php 
	echo $makename=$s->getField('product_manufacture','id',$detailsRow['make'],'manufacturer');
?>
</td>
      <td>
	 <?php 
	echo $colorname=$s->getField('product_color','id',$detailsRow['color'],'color');
?></td>
      <td><?php
	echo $gradername=$s->getField('product_grade','id',$detailsRow['grade'],'grade');
?>
</td>
<td><?php 
	echo $sizename=$s->getField('size_master','id',$detailsRow['size'],'name');
?>
</td>
<td><?php
	echo $gsmname=$s->getField('gsm','id',$detailsRow['gsm'],'gsm');
?><input type="hidden" name="id1[<?php echo $i; ?>]"  value="<?php echo $detailsRow['id']; ?>"/>
</td>
	 <td><input type="text" style="width:40px;" name="quantity[<?php echo $i;?>]" id="quantity_<?php echo $i;?>" value="<?php echo $_REQUEST['quantity'][$i];?>" /></td>
      <td><input type="text"  style="width:40px;" name="rate[<?php echo $i;?>]" id="rate_<?php echo $i;?>" value="<?php echo $_REQUEST['rate'][$i];?>" /></td>
	   <td>
	   	<select name="currency[<?php echo $i;?>]" style="width:80px;">
	   <option value="0">Select</option>
	   <?php 
	   
	   $currSql=DB_query("Select * from currency_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	   while($currRow=DB_fetch_array($currSql))
	   {
	   	$tt='';
		if($currRow['id']==$detailsRow['currency'])
		{
		$tt="selected";	
		}
	   ?>            
	   <option value="<?php echo $currRow['id'];?>"<?php echo $tt; ?>><?php echo $currRow['currency']." - ".$currRow['abbreviation'];?></option>
	   <?php } ?>
	   </select>
	  	   </td>
      <td><select name="discountType[<?php echo $i;?>]" id="discountType_<?php echo $i;?>" style="width:80px">
	  <option value="Percent"<?php echo ($detailsRow['discountType'][$i]=='Percent'?'selected':'')?>>Percent</option>
	  <option value="Amount"<?php echo ($detailsRow['discountType'][$i]=='Amount'?'selected':'')?>>Amount</option>
	  </select>
	  
	  </td>
      <td><input type="text"  style="width:40px;" name="discount[<?php echo $i;?>]" id="discount_<?php echo $i;?>" value="<?php echo $_REQUEST['discount'][$i];?>" onblur="DiscountAmount(this.value,<?php echo $i;?>)"/></td>
      <td><input type="text"  style="width:40px;" name="discountAmt[<?php echo $i;?>]" id="discountAmt_<?php echo $i;?>" value="<?php echo $_REQUEST['discountAmt'][$i];?>" /></td>
      <td><input type="text"  style="width:40px;" name="subTotal[<?php echo $i;?>]" id="subTotal_<?php echo $i;?>" value="<?php echo $_REQUEST['subTotal'][$i];?>" /></td>
	  <td><?php echo $Query_row['flexo_offset'];?></td>
      <td><?php echo $Query_row['od'];?></td>
      <td><?php echo $Query_row['length'];?></td>
      <td><?php echo $Query_row['width'];?></td>
      <td><?php $unit=$s->getField('unitsofmeasure','unitid',$Query_row['unit'],'unitname'); echo $unit;?></td>
      <td><?php echo $Query_row['serial_no'];?></td>
      <td><?php echo $Query_row['orig_local'];?></td>
      <td><?php echo $Query_row['type_name'];?></td>
      <td><?php echo $Query_row['shade'];?></td>
      <td><?php echo $Query_row['running_roll'];?></td>
      <td><?php echo $Query_row['volume'];?></td>
      <td><?php echo $Query_row['teeth'];?></td>
      <td><?php echo $Query_row['bcm'];?></td>
      <td><?php echo $Query_row['ups'];?></td>
       <td><?php echo $Query_row['quality'];?></td>
    </tr>
    <?php $i++; } ?>
	<input type="hidden" name="count" id="count" value="<?php echo $i;?>" />
  </table>
</div>


<div align="right">
<table width="22%" cellpadding="3" cellspacing="0" border="1">
			<tr>
			<td class="tabledata">Basic Amount</td>
			<td><input name="basicAmount" id="basicAmount" type="text" size="15" value="<?php echo $myrow['basicAmount'];?>" readonly onfocus="calc_basicamt()"></td>
			</tr>
		<tr><td class="tabledata">Grand Total</td>
		<td><input name="grandTotal" id="grandTotal" type="text" size="15" value="<?php echo $myrow['grandTotal'];?>" readonly  onfocus="cal_grandtotal()"></td>
		</tr>
	  </table>
</div>


<div align="left">
<table width="100%" cellpadding="0" cellspacing="0" border="1">
<tr>
<td class="tabledata" style="width:100px;">Remarks</td>
<td>
<textarea name="remarks" style="width:100%"><?php echo $myrow['remarks'];?></textarea>
</td>	
</tr>
</table>	
</div>


<p align="center"><input type="submit" name="Update" value="Update" /></p>
</form>



</div></div>





<?php 	
}
?>
<script language="javascript" type="text/javascript">
function open_items()
{
	window.open("ImportItems.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=900,toolbar=no,menubar=no,dependent=yes,resizable=yes, scrollbars=yes");
}
function DiscountAmount(DA,i)
{
	var discountType = document.getElementById("discountType_"+i).value;
	var rate = document.getElementById("rate_"+i).value;
	var discount = document.getElementById("discount_"+i).value;
	var quantity = document.getElementById("quantity_"+i).value;
	var totalPrice = quantity*rate;
	
	if(discountType=='Percent')
	{
		if(discount==''){discount=0;}
		var percent = parseFloat(totalPrice) * parseFloat(discount)/100;
		var subTotal = parseFloat(totalPrice)-parseFloat(percent);
	}
	else if(discountType=='Amount')
	{
		var percent = parseFloat(document.getElementById("discount_"+i).value);
		if(isNaN(percent)){percent=0;}
		var subTotal = parseFloat(totalPrice)-parseFloat(percent);
	}
	
	document.getElementById("discountAmt_"+i).value=percent.toFixed(2);
	document.getElementById("subTotal_"+i).value=subTotal.toFixed(2);
}
function calc_basicamt()
{
	var total_basic=0;
	var cnt=document.getElementById("count").value
	for(var j=0;j<cnt;j++)
	{
		var netrate=document.getElementById("subTotal_"+j).value;
		if(netrate==''){netrate='0';}
		total_basic=parseFloat(total_basic)+parseFloat(netrate);
	}
	document.getElementById("basicAmount").value=total_basic.toFixed(2);
}
function cal_Excise_Duty()
{
	var excise_rate=document.getElementById("exciseDutyPerc").value;
	var basic_value=document.getElementById("basicAmount").value;	
	var excise_value=basic_value*(excise_rate/100);
	document.getElementById("exciseDutyVal").value=excise_value.toFixed(2);
	
}
function cal_Edu_Cess()
{
	var eduCessPerc=document.getElementById("eduCessPerc").value;
	var excise_val=document.getElementById("exciseDutyVal").value;
	var educess_value=excise_val*(eduCessPerc/100);
	document.getElementById("eduCessVal").value=educess_value.toFixed(2);	
}
function cal_SHE_Cess(str)
{
	var sheCessPerc=document.getElementById("sheCessPerc").value;
	var excise_val=document.getElementById("exciseDutyVal").value;
	var shecess_value=excise_val*(sheCessPerc/100);
	document.getElementById("sheCessVal").value=shecess_value.toFixed(2);	
}
function calc_taxable_amount()
{
	var basic_value=document.getElementById("basicAmount").value;
	var excise=document.getElementById("exciseDutyVal").value;
	var edu=document.getElementById("eduCessVal").value;
	var she=document.getElementById("sheCessVal").value;
	var final_taxable=parseFloat(basic_value)+parseFloat(excise)+parseFloat(edu)+parseFloat(she);
	document.getElementById("taxableAmount").value=final_taxable.toFixed(2);
}
function calc_tax_amount()
{
	var taxable=document.getElementById("taxable_amt").value;
	var taxval=document.getElementById("taxvalue").value;
	var final=taxable*(taxval/100);
	document.getElementById("tot_tax_amount").value=final.toFixed(2);
}
function get_charge(str)
{
	document.getElementById("hidden_suroct").value=str
}

function tax_rate(str)
{
var taxable=document.getElementById("taxableAmount").value;
if (str.length==0)
  {
  document.getElementById("taxVal").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("taxVal").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_tax_val.php?taxname="+str+"&taxable="+taxable,true);
xmlhttp.send();
}

function cal_suroct(str)
{
	var total_tax=document.getElementById("tot_tax_amount").value;
	var final_sur=total_tax*(str/100);
	document.getElementById("sur_oct_amt").value=final_sur.toFixed(2);
}
function cal_grandtotal()
{
	var basicAmount=document.getElementById("basicAmount").value;
	//var Totaltaxable=document.getElementById("taxableAmount").value;
	//var Totaltaxamount=document.getElementById("totalTaxAmount").value;
	//var customDuty=document.getElementById("customDuty").value;
	//var suramt=document.getElementById("surchargeAmt").value;
	//var TotalCharge=document.getElementById("totalCharges").value;
	
	var grand_tot=parseFloat(basicAmount);
	document.getElementById("grandTotal").value=grand_tot.toFixed(2); /*Math.round(*/
}
function generatePO(str)
{
	if (str.length==0)
  {
  document.getElementById("POID").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("POID").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ImportSite.php?site="+str,true);
xmlhttp.send();
}
function calculateSurcharge()
{
	var totalTaxAmount=document.getElementById("totalTaxAmount").value;
	var surchargePerc=document.getElementById("surchargePerc").value;
	var result=totalTaxAmount*(surchargePerc/100);
	document.getElementById("surchargeAmt").value=result.toFixed(2);
}
</script>