<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 2;
include('includes/SQL_CommonFunctions.inc');
include('includes/DefinePOClass.php');
$title = _('Import Purchase Order');
$PageName ='Import Purchase Order';

if($_REQUEST['item_popup']!='')
{
	$_SESSION['site']=$_REQUEST['site'];
	$_SESSION['vendor']=$_REQUEST['vendor'];
	$_SESSION['orderPrefix']=$_REQUEST['orderPrefix'];
	$_SESSION['orderDigit']=$_REQUEST['orderDigit'];
	$_SESSION['orderDate']=$_REQUEST['orderDate'];
}
if($_REQUEST['Cancel']!='')
{
	unset($_SESSION['site']);
	unset($_SESSION['vendor']);
	unset($_SESSION['orderPrefix']);
	unset($_SESSION['orderDigit']);
	unset($_SESSION['orderDate']);
	unset($_SESSION['POItemlist']);
}
if($_REQUEST['Save']!='')
{
	$insert1="Insert into import_purchaseorder set
								site='".$_REQUEST['site1']."',    
								vendor='".$_REQUEST['vendor']."',  
								orderPrefix='".$_REQUEST['orderPrefix']."',  
								orderDigit='".$_REQUEST['orderDigit']."',  
								orderNo='".$_REQUEST['orderPrefix']."-".$_REQUEST['orderDigit']."',  
								orderDate='".FormatDateForSQL($_REQUEST['orderDate'])."',  
								basicAmount='".$_REQUEST['basicAmount']."',  
								exciseDutyPerc='".$_REQUEST['exciseDutyPerc']."',  
								exciseDutyVal='".$_REQUEST['exciseDutyVal']."',  
								eduCessPerc='".$_REQUEST['eduCessPerc']."',  
								eduCessVal='".$_REQUEST['eduCessVal']."',  
								sheCessPerc='".$_REQUEST['sheCessPerc']."',  
								sheCessVal='".$_REQUEST['sheCessVal']."',  
								taxableAmount='".$_REQUEST['taxableAmount']."',  
								taxName='".$_REQUEST['taxName']."',  
								totalTaxAmount='".$_REQUEST['totalTaxAmount']."',  
								customDuty='".$_REQUEST['customDuty']."',  
								surchargePerc='".$_REQUEST['surchargePerc']."',  
								surchargeAmt='".$_REQUEST['surchargeAmt']."',  
								totalCharges='".$_REQUEST['totalCharges']."',  
								grandTotal='".$_REQUEST['grandTotal']."',  
								createdBy='".$_SESSION['UserID']."',  
								createdDate='".FormatDateForSQL(date('d/m/Y'))."' ";
	$result1=DB_query($insert1,$db);
	$lastId=DB_Last_Insert_ID($db,'import_purchaseorder','id');
	if($lastId!='')
	{
		for($str=0;$str<$_REQUEST['count'];$str++)
		{
			$insert2="Insert into import_purchaseorder_details set
											masterId='".$lastId."', 
											itemCode='".$_REQUEST['itemCode'][$str]."',   
											make='".$_REQUEST['make'][$str]."',   
											color='".$_REQUEST['color'][$str]."',   
											grade='".$_REQUEST['grade'][$str]."',    
											size='".$_REQUEST['size'][$str]."',     
											gsm='".$_REQUEST['gsm'][$str]."',  
											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]."' ";
			$result2=DB_query($insert2,$db);
		}
	}
prnMsg( _('Record Inserted Successfully'),'success');
echo "<script>location.href='index.php?route=ImportPOList&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
unset($_SESSION['site']);
unset($_SESSION['vendor']);
unset($_SESSION['orderPrefix']);
unset($_SESSION['orderDigit']);
unset($_SESSION['orderDate']);
unset($_SESSION['POItemlist']);
}
?>
<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>
<h2 align="center"><strong>New Import Purchase Order</strong></h2>
<form action="" method="post">
<table width="50%" border="0" cellspacing="1" cellpadding="2">
	  <tr>
        <th><strong>BRANCH</strong></th>
		<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",$db);
		while($site_row=DB_fetch_array($site_sql))
		{?>
		<option value="<?php echo $site_row['loccode'];?>"<?php echo ($_SESSION['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
		<?php 
		}
		?>
		  </select>
		</td>
        <th>PO NO. </th>
		<td id="POID"><input name="orderPrefix" type="text" id="orderPrefix" value="<?php echo $_SESSION['orderPrefix'];?>" size="5" />
-		  <input name="orderDigit" type="text" id="orderDigit" value="<?php echo $_SESSION['orderDigit'];?>" size="5" /></td>
      </tr>
      <tr>
        <th><strong>VENDOR</strong></th>
		<td>
		<select name="vendor" style="width:110px">
		<option value="0">Select</option>
		<?php 
		$vendor_sql=DB_query("Select vendor_code,vendor_name from suppliers",$db);
		while($vendor_row=DB_fetch_array($vendor_sql))
		{?>
		<option value="<?php echo $vendor_row['vendor_code'];?>"<?php echo ($_SESSION['vendor']==$vendor_row['vendor_code']?'selected':'')?>><?php echo $vendor_row['vendor_name'];?></option>
		<?php 
		}
		?>
        </select></td>
		<th>PO DATE </th>
		<?php if(isset($_SESSION['orderDate'])){$orderDate=$_SESSION['orderDate'];}else{$orderDate=date('d/m/Y');}?>
		<td><input type="text" name="orderDate" id="orderDate" value="<?php echo $orderDate;?>" class="datepicker" /></td>
      </tr>
      <tr></tr>
  </table>
<input type="hidden" name="site1" value="<?php echo $_SESSION['site'];?>">
<input type="hidden" name="vendor1" value="<?php echo $_SESSION['vendor'];?>">
<input type="hidden" name="orderPrefix1" value="<?php echo $_SESSION['orderPrefix'];?>">
<input type="hidden" name="orderDigit1" value="<?php echo $_SESSION['orderDigit'];?>">
<input type="hidden" name="orderDate1" value="<?php echo $_SESSION['orderDate'];?>">
<p align="left"><input type="submit" name="item_popup" value="Pick Items" id="item_popup"  onclick="open_items();" /></p>
<?php 
if($_SESSION['POItemlist']!='')
{$height="150px";}else{$height="70px";}
?>
<div style="overflow:auto; width:800px ; height:<?php echo $height;?>"  >
  <table width="99%" border="0" cellspacing="1" cellpadding="5">
    <tr>
      <th scope="col"><strong>Item Code </strong></th>
      <th scope="col"><strong>Item Name </strong></th>
      <th scope="col"><strong>Description</strong></th>
      <th scope="col"><strong>Item No. </strong></th>
      <th scope="col"><strong>Make</strong></th>
      <th scope="col"><strong>Color</strong></th>
      <th scope="col"><strong>Grade</strong></th>
	  <th scope="col"><strong>Size</strong></th>
	  <th scope="col"><strong>GSM</strong></th>
	   <th scope="col"><strong>Quantity</strong></th>
      <th scope="col"><strong>Rate</strong></th>
	   <th scope="col"><strong>Currency</strong></th>
      <th scope="col"><strong>Discount Type</strong></th>
      <th scope="col"><strong>Discount</strong></th>
      <th><strong>Discount Amount</strong></th>
      <th scope="col"><strong>Sub Total </strong></th>
      <th scope="col"><strong>Flexo Offset</strong></th>
      <th scope="col"><strong>Od</strong></th>
      <th scope="col"><strong>Length</strong></th>
      <th scope="col"><strong>Width</strong></th>
      <th scope="col"><strong>Unit</strong></th>
      <th scope="col"><strong>Serial No.</strong></th>
      <th scope="col"><strong>Orignal/Local</strong></th>
      <th scope="col"><strong>Type</strong></th>
      <th scope="col"><strong>Shade</strong></th>
      <th scope="col"><strong>Running Meter/Roll</strong></th>
      <th scope="col"><strong>Volume</strong></th>
      <th scope="col"><strong>Teeth</strong></th>
      <th scope="col"><strong>Bcm</strong></th>
      <th scope="col"><strong>Ups</strong></th>
      <th scope="col"><strong>Quality</strong></th>
      </tr>
    <?php 
	if(isset($_SESSION['POItemlist']))
	{
	$pp=explode("#",$_SESSION['POItemlist']);
  for($i=0;$i<count($pp);$i++)
  {
  	//echo "select * from item_master where StockID='".$pp[$i]."'";
	$Query=DB_query("select * from item_master where StockID='".$pp[$i]."'",$db);
  	$Query_row=DB_fetch_array($Query);
  
  ?>
    <tr bgcolor="#FFFFFF">
      <td><?php echo $pp[$i];?><input type="hidden" name="itemCode[<?php echo $i;?>]" id="itemCode_<?php echo $i;?>" value="<?php echo $pp[$i];?>" /></td>
      <td><?php echo $Query_row['item_name'];?></td>
      <td><?php echo $Query_row['item_desc'];?></td>
      <td><?php echo $Query_row['item_no'];?></td>
     <td>
	 <?php 
$makeSql=DB_query("Select manufacture from item_specification where proc_id='".$pp[$i]."' and manufacture!='000'",$db);
?>
<select name="make[<?php echo $j;?>]" id="" style="width:100px">
<?php 
while($makeRow=DB_fetch_array($makeSql))
{
	$makename=$s->getField('product_manufacture','id',$makeRow['manufacture'],'manufacturer');
?>
<option value="<?php echo $makeRow['manufacture'];?>"><?php echo $makename;?></option>
<?php } ?>
</select></td>
      <td>
	 <?php 
$colorSql=DB_query("Select color from item_specification where proc_id='".$pp[$i]."' and color!='000'",$db);
?>
<select name="color[<?php echo $j;?>]" id="" style="width:100px">
<?php 
while($colorRow=DB_fetch_array($colorSql))
{
	$colorname=$s->getField('product_color','id',$colorRow['color'],'color');
?>
<option value="<?php echo $colorRow['color'];?>"><?php echo $colorname;?></option>
<?php } ?>
</select></td>
      <td><?php
$gradeSql=DB_query("Select grade from item_specification where proc_id='".$pp[$i]."' and grade!='000'",$db);
?>
<select name="grade[<?php echo $j;?>]" id="" style="width:100px">
<?php 
while($gradeRow=DB_fetch_array($gradeSql))
{
	$gradername=$s->getField('product_grade','id',$gradeRow['grade'],'grade');
?>
<option value="<?php echo $gradeRow['grade'];?>"><?php echo $gradername;?></option>
<?php } ?>
</select></td>
<td><?php 
$sizeSql=DB_query("Select size from item_specification where proc_id='".$pp[$i]."' and size!='000'",$db);
?>
<select name="size[<?php echo $j;?>]" id="" style="width:100px">
<?php 
while($sizeRow=DB_fetch_array($sizeSql))
{
	$sizename=$s->getField('size_master','id',$sizeRow['size'],'name');
?>
<option value="<?php echo $sizeRow['size'];?>"><?php echo $sizename;?></option>
<?php } ?>
</select></td>
<td><?php

$gsmSql=DB_query("Select gsm from item_specification where proc_id='".$pp[$i]."' and gsm!='0'",$db);
?>
<select name="gsm[<?php echo $j;?>]" id="" style="width:100px">
<?php 
while($gsmRow=DB_fetch_array($gsmSql))
{
	$gsmname=$s->getField('gsm','id',$gsmRow['gsm'],'gsm');
?>
<option value="<?php echo $gsmRow['gsm'];?>"><?php echo $gsmname;?></option>
<?php } ?>
</select></td>
	 <td><input type="text" name="quantity[<?php echo $i;?>]" id="quantity_<?php echo $i;?>" value="<?php echo $_REQUEST['quantity'][$i];?>" /></td>
      <td><input type="text" 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:110px;">
	   <option value="0">Select</option>
	   <?php 
	   $currSql=DB_query("Select * from currency_master",$db);
	   while($currRow=DB_fetch_array($currSql))
	   {
	   ?>            
	   <option value="<?php echo $currRow['id'];?>"<?php echo ($_REQUEST['currency'][$i]==$currRow['id']?'selected':'')?>><?php echo $currRow['currency']." - ".$currRow['abbreviation'];?></option>
	   <?php } ?>
	   </select>
	   </td>
      <td>
	  <select name="discountType[<?php echo $i;?>]" id="discountType_<?php echo $i;?>" style="width:70px">
	  <option value="Percent"<?php echo ($_REQUEST['discountType'][$i]=='Percent'?'selected':'')?>>Percent</option>
	  <option value="Amount"<?php echo ($_REQUEST['discountType'][$i]=='Amount'?'selected':'')?>>Amount</option>
	  </select>
	  </td>
      <td><input type="text" 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" name="discountAmt[<?php echo $i;?>]" id="discountAmt_<?php echo $i;?>" value="<?php echo $_REQUEST['discountAmt'][$i];?>" /></td>
      <td><input type="text" 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 } }?>
	<input type="hidden" name="count" id="count" value="<?php echo $i;?>" />
  </table>
</div>
<br>
<table width="100%">
<tr><td>
<table width="25%" cellpadding="2" cellspacing="2" align="center">
			<tr>
			<th><strong>Basic Amount</strong></th>
			<td><input name="basicAmount" id="basicAmount" type="text" size="15" onfocus="calc_basicamt()"></td>
			</tr>
		<tr><th><strong>Grand Total</strong></th>
		<td><input name="grandTotal" id="grandTotal" type="text" size="15" onfocus="cal_grandtotal()"></td>
		</tr>
	  </table>
</td></tr>
<tr>
<td>
<table width="90%" align="center">
<tr>
<th>Remarks</th>
<td>
<textarea name="remarks" cols="80" rows="5"></textarea>
</td>	
</tr>
</table>	
</td>		
</tr>
</table>
<p align="center"><input type="submit" name="Save" value="Save" /><input type="submit" name="Cancel" value="Cancel" /></p>
</form>