<?php
$PageSecurity = 1;
include('includes/DefinePaymentClass.php');
$title = _('Invoice');
include('includes/SQL_CommonFunctions.inc');
$exciserate=$s->getField('excise_tax','label','Excise','def_rate');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;

//************************Delete*********************************************** 
if( $_REQUEST['daction']=='delete')
{
	$condition="id='".$_REQUEST['pid']."'";
	$s->DeleteRecord('invoice_details',$condition);
 	$s->pageLocation('index.php?route=invoice&action=edit&id='.$_REQUEST['id'].$rurl);
}
if($_REQUEST['action']=='view')
{
	$s->pageLocation('index.php?route=invoice.php&action=addnew'.$rurl);
}
 //*******************************************
$_SESSION['editid']=$_REQUEST['id'];
function number_to_words ($x)
{
	  $nwords = array("","one","two","three","four","five","six", 
                  "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
                  "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", 
                  "nineteen", "twenty", 30 => "thirty", 40 => "fourty",
                     50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eigthy",
                     90 => "ninety" );

     if(!is_numeric($x))
     {
         $w = '#';
     }else if(fmod($x, 1) != 0)
     {
         $w = '#';
     }else{
         if($x < 0)
         {
             $w = 'minus ';
             $x = -$x;
         }else{
             $w = '';
         }
         if($x < 21)
         {
             $w .= $nwords[$x];
         }else if($x < 100)
         {
             $w .= $nwords[10 * floor($x/10)];
             $r = fmod($x, 10);
             if($r > 0)
             {
                 $w .= ' '. $nwords[$r];
             }
         } else if($x < 1000)
         {
                
                $w .= $nwords[floor($x/100)] .' hundred';
             $r = fmod($x, 100);
             if($r > 0)
             {
                 $w .= ' '. number_to_words($r);
             }
         } else if($x < 1000000)
         {
                $w .= number_to_words(floor($x/1000)) .' thousand';
             $r = fmod($x, 1000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $w .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         } else {
             $w .= number_to_words(floor($x/1000000)) .' million';
             $r = fmod($x, 1000000);
             if($r > 0)
             {
                 $w .= ' ';
                 if($r < 100)
                 {
                     $word .= ' ';
                 }
                 $w .= number_to_words($r);
             }
         }
     }
     return $w;}
if($_REQUEST['id'])
{
    $id=$_REQUEST['id'];
  $arr=array();
 for($kp=0;$kp<count($_SESSION['invoice_items']);$kp++)
 {
   if($id!=$_SESSION['invoice_items'][$kp])
   {
 		$arr[]=$_SESSION['invoice_items'][$kp];
   }
 }
 $_SESSION['invoice_items']=$arr;
 }
 ?>
<script src="js/function_invoice.js"></script><script language="javascript" type="text/javascript">
function GetURL(str)
{
	var branch_name=document.getElementById("branch_name").value;
	var client=document.getElementById("client_id").value;
	location.replace("index.php?route=invoice&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=addnew&po_no="+str+"&branch_name="+branch_name+"&client_id="+client);
}
function calculateAmount(j)
{
	var qty=document.getElementById("Quantity_"+j).value;
	var rate=document.getElementById("RatePerThousand_"+j).value;
	var rateperthousand=rate/1000;
	var total=qty*rateperthousand;
	document.getElementById("Amount_"+j).value=total.toFixed(2);
}
</script>
  <?php 
if( $_REQUEST['action'])
{
	$exciserate=$s->getField('excise_tax','label','Excise','def_rate');
	$edu_cess=$s->getField('excise_tax','label','C_Cess','def_rate');
	$she_cess=$s->getField('excise_tax','label','H_Cess','def_rate');
	//$data['invoice_type'] =	$_REQUEST['invoice_type'];
	$data['client_id'] = $_REQUEST['client_id'];
	$client_code=$s->getField('clientmaster','id',$_REQUEST['client_id'],'client_code');
	$data['client_code'] = $client_code;
	$data['ledger_id'] = $_REQUEST['ledger_id'];
	$data['our_ref_no'] = $_REQUEST['our_ref_no'];
	$data['our_ref_date'] =FormatDateForSQL($_REQUEST['our_ref_date']);
	$data['invoice_date'] = date("Y-m-d");	
	$data['branch_name'] = $_REQUEST['branch_name'];
	$data['invoice_code'] = $_REQUEST['invoice_code'];
	$data['invoice_number'] = $_REQUEST['invoice_number'];
	$data['salesmanid'] = $_REQUEST['salesmanid'];
	$data['form_type'] = $_REQUEST['form_type'];
	$data['form_no'] = $_REQUEST['form_no'];
	$data['cst_vat_no'] = $_REQUEST['cst_vat_no'];
	$data['po_date'] = FormatDateForSQL($_REQUEST['po_date']);
	$data['po_no'] = $_REQUEST['po_no'];
	$data['ecc_no'] = $_REQUEST['ecc_no'];
	$data['despatch_mode'] = $_REQUEST['despatch_mode'];
	$data['gr_no'] = $_REQUEST['gr_no'];
	$data['gr_no_date'] =FormatDateForSQL($_REQUEST['gr_no_date']);
	$data['name_of_commmodity'] = $_REQUEST['name_of_commmodity'];
	$data['destination'] = $_REQUEST['destination'];
	//$data['vendorcode'] = $_REQUEST['vendorcode'];	 
	$data['tarrif_heading'] = $_REQUEST['tarrif_heading'];	
	$data['payment_terms'] = $_REQUEST['payment_terms'];	
	$data['value_of_goods'] = $_REQUEST['value_of_goods'];
	$data['excise_duty'] = $_REQUEST['Excise'];
	
	$data['excise_perc'] = $_REQUEST['hid_Excise'];
	$data['excise_legder'] = $_REQUEST['hidtax_Excise'];
	$data['edu_cess_perc'] = $_REQUEST['hid_C_Cess'];
	$data['edu_cess_ledger'] = $_REQUEST['hidtax_C_Cess'];
	
	$data['she_cess_perc'] = $_REQUEST['hid_H_Cess'];
	$data['she_cess_ledger'] = $_REQUEST['hidtax_H_Cess'];
	$data['edu_cess'] = ($_REQUEST['Excise'])*$edu_cess *.01;
	$data['she_cess'] =($_REQUEST['Excise'])*$she_cess *.01;
	$data['total_taxable'] = $data['edu_cess'] +$data['she_cess']+$data['excise_duty']+$data['value_of_goods'];
	$data['vat_cst'] = $_REQUEST['vat_cst'];
	$data['vat_cst_ledger'] = $_REQUEST['vat_cst_ledger'];
 	$data['grand_total'] =$data['total_taxable']+($data['total_taxable']*$data['vat_cst'])*.01;	
 	$data['issue_invoice_date'] =FormatDateForSQL($_REQUEST['issue_invoice_date']);
	$data['removal_invoicedate'] = FormatDateForSQL($_REQUEST['removal_invoicedate']);
	$data['issue_invoice_time'] = $_REQUEST['issue_invoice_time'];
	$data['removal_invoicetime'] = $_REQUEST['removal_invoicetime'];
	
	
 	$data['issue_invoice_datetime'] = $_REQUEST['issue_invoice_datetime'];
	$data['removal_invoicedatetime'] = $_REQUEST['removal_invoicedatetime'];

	$data['mode_of_transport'] = $_REQUEST['mode_of_transport'];
	$data['excise_duty_inwords'] = $_REQUEST['excise_duty_inwords'];
	$data['vehicle_registration'] = $_REQUEST['vehicle_registration'];
}
if($_REQUEST['action']=='edit' && !$_REQUEST['Save_invoice'] )
{
	//unset($_SESSION['invoice_items']);
 	$cond="invoice_id='".$_REQUEST['id']."'";
    $GetRecord=$s->GetRecord("invoice",$cond);
    $result=DB_query($GetRecord,$db);
 	$row_invoice=DB_fetch_array($result);
	$_REQUEST['client_id']= $row_invoice['client_id'];
	$_REQUEST['invoice_type']= $row_invoice['invoice_type'];
 	$getclient_address=DB_query('select * from clientmaster where id="'.$row_invoice['client_id'].'"',$db);
	$rdata=DB_fetch_array($getclient_address);
	$_REQUEST['pre_address1']= $rdata['pre_address1'];
	$_REQUEST['pre_address2']= $rdata['pre_address2'];
	$_REQUEST['pre_address3']= $rdata['pre_address3'];
	$_REQUEST['fax']= $rdata['fax'];
	$_REQUEST['ledger_id']= $row_invoice['ledger_id'];
	$_REQUEST['branch_name']= $row_invoice['branch_name'];
	$_REQUEST['invoice_number']= $row_invoice['invoice_number'];
	$_REQUEST['invoice_code']= $row_invoice['invoice_code'];
	$_REQUEST['our_ref_no']= $row_invoice['our_ref_no'];
	$_REQUEST['our_ref_date']= $row_invoice['our_ref_date'];
	$_REQUEST['invoice_date']= $row_invoice['invoice_date'];
	$_REQUEST['vendorcode']= $row_invoice['vendorcode'];
	$_REQUEST['po_date']=$row_invoice['po_date'];
	$_REQUEST['po_no']=$row_invoice['po_no'];
	$_REQUEST['form_type']= $row_invoice['form_type'];
	$_REQUEST['form_no']= $row_invoice['form_no'];
	$_REQUEST['cst_vat_no']= $row_invoice['cst_vat_no'];
	$_REQUEST['ecc_no']= $row_invoice['ecc_no'];
	$_REQUEST['despatch_mode']= $row_invoice['despatch_mode'];
	$_REQUEST['gr_no']= $row_invoice['gr_no'];
	$_REQUEST['gr_no_date']= $row_invoice['gr_no_date'];
	$_REQUEST['name_of_commmodity']= $row_invoice['name_of_commmodity'];
	$_REQUEST['destination']= $row_invoice['destination'];
	$_REQUEST['salesmanid']= $row_invoice['salesmanid'];	
	$_REQUEST['tarrif_heading']= $row_invoice['tarrif_heading'];	
	$_REQUEST['payment_terms']= $row_invoice['payment_terms'];	
	$_REQUEST['value_of_goods']= $row_invoice['value_of_goods'];
	$_REQUEST['excise_duty']= $row_invoice['excise_duty'];
	$_REQUEST['edu_cess']= $row_invoice['edu_cess'];
	$_REQUEST['she_cess']= $row_invoice['she_cess'];
	

	
	
	$_REQUEST['total_taxable'] = $row_invoice['total_taxable'];
	$_REQUEST['vat_cst_ledger']= $row_invoice['vat_cst_ledger'];
	$_REQUEST['vat_cst']= $row_invoice['vat_cst'];
	$_REQUEST['calvatcst']= $row_invoice['calvatcst'];
	
	$_REQUEST['grand_total']= $row_invoice['grand_total'];	
	$_REQUEST['issue_invoice_date']= $row_invoice['issue_invoice_date'];	
	$_REQUEST['removal_invoicedate']= $row_invoice['removal_invoicedate'];
	$_REQUEST['issue_invoice_datetime']= $row_invoice['issue_invoice_datetime'];
	$_REQUEST['removal_invoicedatetime']= $row_invoice['removal_invoicedatetime'];
	$_REQUEST['issue_invoice_time']= substr($row_invoice['issue_invoice_time'],0,5);	
	$_REQUEST['removal_invoicetime']= substr($row_invoice['removal_invoicetime'],0,5);
	$_REQUEST['mode_of_transport']= $row_invoice['mode_of_transport'];	
	$_REQUEST['vehicle_registration']= $row_invoice['vehicle_registration'];	
 	$_REQUEST['excise_duty_inwords']=$row_invoice['excise_duty_inwords'];	
  }
if($_REQUEST['Save_invoice'] && $_REQUEST['action']=='edit' && isset($_REQUEST['id'])) 
{
   	$cond="invoice_id='".$_REQUEST['id']."'";	
	$update=$s->EditRecord('invoice', $data, $cond);
	$updateResult = DB_query($update,$db,$ErrMsg);
	if(!$update)
	{
			  die("Not Updated".DB_error_msg($db));
	}
 	
	for($i=0;$i<count($_REQUEST['itemids']);$i++)
			{
				    $data_arr['invoiceid']= $_REQUEST['id'];
					$data_arr['ItemCode']= $_POST['ItemCode'][$i];
					$data_arr['PackageDesc']= $_POST['PackageDesc'][$i];
					$data_arr['Quantity']= $_REQUEST['Quantity'][$i];
					$data_arr['Unit']= $_POST['Unit'][$i];
					$data_arr['Rate']= $_POST['Rate'][$i];
					$data_arr['RatePerThousand']= $_REQUEST['RatePerThousand'][$i];
					$data_arr['Amount']= $_REQUEST['Amount'][$i];
			   /*	echo "<br>****************************************************<br>";
				    echo "select * from  invoice_details where id='".$_REQUEST['itemids'][$i]."'";
					echo "<br>****************************************************<br>";*/
				$chkid=DB_query("select * from  invoice_details where id='".$_REQUEST['itemids'][$i]."'",$db);
				if(DB_num_rows($chkid)>0)
				{
				 				$cond="id='".$_REQUEST['itemids'][$i]."'";	
								$update_invoice_details=$s->EditRecord('invoice_details', $data_arr, $cond);
				 				$updateResult = DB_query($update_invoice_details,$db,$ErrMsg); 
				}else{
							    
								$data_arr['invoiceid']=$_REQUEST['id'];
								$data_arr['itemids']=$_REQUEST['itemids'][$i];
								$insert_invoice=$s->insertRecord('invoice_details', $data_arr);
 								$insertResult = DB_query($insert_invoice,$db,$ErrMsg); 
				 }	
				 			
  			 }
	 
	$s->pageLocation('index.php?route=ViewInvoice&daction=Update'.$rurl);
}
if($_REQUEST['Save_invoice'] && $_REQUEST['action']=='addnew') 
{ 
  	$insert_invoice=$s->insertRecord('invoice', $data);
 	$insertResult = DB_query($insert_invoice,$db,$ErrMsg); 
		if(!$insertResult)
			{
			  die("Not Inserted".DB_error_msg($db));
			}
			$Lastreq_id=DB_Last_Insert_ID($db,'invoice',$ErrMsg);
			for($i=0;$i<$_REQUEST['rowcount'];$i++)
			{
					$data_arr['invoiceid']= $Lastreq_id;
					$data_arr['ItemCode']= $_POST['ItemCode'][$i];
					$data_arr['PackageDesc']= $_POST['PackageDesc'][$i];
					$data_arr['Quantity']= $_REQUEST['Quantity'][$i];
					$data_arr['Unit']= $_POST['Unit'][$i];
					$data_arr['Rate']= $_POST['Rate'][$i];
					$data_arr['RatePerThousand']= $_REQUEST['RatePerThousand'][$i];
					$data_arr['Amount']= $_REQUEST['Amount'][$i];
					$insert_invoice=$s->insertRecord('invoice_details', $data_arr);
					$insertResult = DB_query($insert_invoice,$db,$ErrMsg); 
			}
	 $s->pageLocation('index.php?route=ViewInvoice&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].$rurl);
}
?>
<div style="width:800px; margin-left:auto; margin-right:auto;">
<fieldset>
<legend>Performa Invoice</legend>
<form action="" method="post">
<!-- TAble Start 1 --><div>

<table width="100%" border="0" cellspacing="1" cellpadding="3" align="center">
<tr>
<th>Branch Name </th>
<td><?php  
$SQL_client=DB_query("SELECT prefix,loccode,locationname from locations",$db);
?>
<select  name="branch_name" id="branch_name" style="width:107px;" tabindex="1"  onChange="ShowBranch(this.value)" >
<option >--Branch Name--</option>
<?php
while($row_client=DB_fetch_array($SQL_client))
{

?>
<option value="<?php  echo $row_client['prefix']?>" <?php  echo ($_REQUEST['branch_name']==$row_client['prefix']?'selected':''); ?> >
<?php  echo $row_client['locationname']?>
</option>
<?php }?>
</select></td>
<th>P Invoice No. &amp; Date</th>
<td><input name="our_ref_no" type="text" id="our_ref_no" style="width:auto;"  tabindex="4" value="<?php echo $_REQUEST['our_ref_no'];?>" />
  <?php if($_REQUEST['our_ref_date']=='')
$_REQUEST['our_ref_date']=date("d/m/Y");
?>
  <input name="our_ref_date" type="text" class="datepicker" id="our_ref_date"  tabindex="5" value="<?php echo $_REQUEST['our_ref_date'];?>" /></td>
</tr>
<tr>
<th scope="row">Client Name </th>
<td><?php
$SQL_client=DB_query("SELECT clientmaster.id, clientmaster.c_name from clientmaster Where 1=1",$db);
?>
<select  name="client_id" id="client_id" style="width:107px;" tabindex="3" onChange="showclient(this.value)">
<option >--Client--</option>
<?php
while($row_client=DB_fetch_array($SQL_client))
{
$p='';
if($row_client['id']==$_REQUEST['client_id'])
$p="selected";
?>
<option value="<?php  echo $row_client['id']?>" <?php  echo $p; ?> >
<?php  echo $row_client['c_name']?>
</option>
<?php }?>
</select></td>
<th>Against S.O.P.</th>
<td><?php	

$sql_state=DB_query("select * from tariff_master where 1=1",$db);		
if(!$sql_state)
{
   die('Error.'.DB_error_msg($db));
}
echo "<select name='tarrif_heading'   style='width:107px;' >";
echo "<option value='000'>-- Select Tarrif--</option>";
while($row_tarrif=DB_fetch_array($sql_state))
{
   $p='';
   if($row_tarrif['id']==$_REQUEST['tarrif_heading'])
   $p='selected';
  echo "<option value='".$row_tarrif['id']."' $p >".$row_tarrif['tarrif_heading']."</option>";
}
echo "</select>";
?></td>
</tr>

<tr>
<td rowspan="6" colspan="2" id="client_detials" valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<th valign="top">Address</th>
<td><input name="" type="text" id="" style="width:167px;" tabindex="6" value="" /></td>
</tr>
<tr>
<th>Contact Person</th>
<td><input name="cst_vat_no" type="text" id="cst_vat_no" style="width:167px;" tabindex="6" value="<?php echo $myrow['cst_no'];?>"  readonly="true"/></td>
</tr>
<tr>
<th>Mobile No.</th>
<td><input name="cst_vat_no" type="text" id="cst_vat_no" style="width:167px;" tabindex="6" value="<?php echo $myrow['cst_no'];?>"  readonly="true"/></td>
</tr>
<tr>
<th>CST/VAT No.</th>
<td><input name="cst_vat_no" type="text" id="cst_vat_no" style="width:167px;" tabindex="6" value="<?php echo $myrow['cst_no'];?>"  readonly="true"/></td>
</tr>
<tr>
<th>ECC No.</th>
<td><input name="ecc_no" type="text" id="ecc_no" style="width:167px;" tabindex="7" value="<?php echo $myrow['excies_no'];?>" readonly="true"  /></td>
</tr>
<tr>
<th>Party PO No.</th>
<td align="left" ><input name="input" type="text" id="input" style="width:167px;" tabindex="6" value="" /></td></tr>
</table></td>
<th>Our Reference No. &amp; Date </th>
<td><input name="our_ref_no2" type="text" id="our_ref_no2" style="width:auto;"  tabindex="4" value="<?php echo $_REQUEST['our_ref_no'];?>" />
  <?php if($_REQUEST['our_ref_date']=='')
$_REQUEST['our_ref_date']=date("d/m/Y");
?>
  <input name="our_ref_date2" type="text" class="datepicker" id="our_ref_date2"  tabindex="5" value="<?php echo $_REQUEST['our_ref_date'];?>" /></td>
</tr>
<tr>
  <th scope="row">Form No.</th>
  <td><input name="form_no" type="text"  style="width:auto;" tabindex="14" value="<?php echo $_REQUEST['form_no'];?>" /></td>
<?php 
$pref_sql=DB_query("Select prefix from locations where loccode='".$_REQUEST['branch_name']."'",$db);
$pref_row=DB_fetch_array($pref_sql);
$_REQUEST['invoice_code']=$_REQUEST['branch_name'];
?>
</tr>
<tr>
  <th scope="row">Dispatch Thru </th>
  <td><input name="despatch_mode" type="text" id="despatch_mode"  style="width:auto;" tabindex="15" value="<?php echo $_REQUEST['despatch_mode'];?>" /></td>
</tr>
<tr>
  <th>GR No. &amp; Date </th>
  <td><input name="gr_no" type="text" id="gr_no" style="width:auto;" tabindex="16"  value="<?php echo $_REQUEST['gr_no']; ?>" />
    <?php if($_REQUEST['gr_no_date']=='')
	$_REQUEST['gr_no_date']=date("d/m/Y");
	?>
    <input name="gr_no_date" type="text" class="datepicker" id="gr_no_date"  tabindex="17"  value="<?php echo $_REQUEST['gr_no_date'];?>" size="12"/></td>
</tr>
<tr>
  <th>Destination</th>
  <td><input name="destination" type="text" id="destination" style="width:auto;" tabindex="18" value="<?php echo $_REQUEST['destination'];?>" /></td>
</tr>
<tr>
  <th>Delivery Schedule</th>
  <td><input name="destination" type="text" id="destination" style="width:auto;" tabindex="18" value="<?php echo $_REQUEST['destination'];?>" /></td>
</tr>
<tr>
	<th>Party PO Date</th>
	<td  width="22%"><input name="po_date" type="text"  class="datepicker" style="width:auto;" tabindex="9"  value="<?php echo ConvertSQLDate($_REQUEST['po_date']);?>"/></td>
	<th>Validity</th>
	<td><input name="destination" type="text" id="destination" style="width:auto;" tabindex="18" value="<?php echo $_REQUEST['destination'];?>" /></td>
	</tr>
	<tr>
	<th scope="row">Salesman</th>
	<td><?php
	$SQL_salesmaster=DB_query("SELECT * from salesman_master Where 1=1",$db);
	?>
	  <select  name="salesmanid" style="width:auto;" height:20px;" tabindex="8" >
	    <option >--Select SalesMan --</option>
	    <?php
	while($row_sales_master=DB_fetch_array($SQL_salesmaster))
	{
	$p="";
	if($row_sales_master['id']==$_REQUEST['salesmanid'])
	$p="selected";
	?>
	    <option value="<?php  echo $row_sales_master['id']?>"<?php echo $p;?> >
	      <?php  echo $row_sales_master['sales_name']?>
	      </option>
	    <?php }?>
	    </select></td>
	<th scope="row">Payment Terms</th>
	<td><input name="payment_terms" type="text" id="payment_terms"  style="width:auto;" tabindex="19" value="<?php echo $_REQUEST['payment_terms'];?>" /></td>
	</tr>
    <tr>
	<th scope="row">Mode of Transport:</th>
	<td><?php
	$SQL_salesmaster=DB_query("SELECT * from salesman_master Where 1=1",$db);
	?>
	  <select  name="salesmanid" style="width:auto;" height:20px;" tabindex="8" >
	    <option >--Select SalesMan --</option>
	    <?php
	while($row_sales_master=DB_fetch_array($SQL_salesmaster))
	{
	$p="";
	if($row_sales_master['id']==$_REQUEST['salesmanid'])
	$p="selected";
	?>
	    <option value="<?php  echo $row_sales_master['id']?>"<?php echo $p;?> >
	      <?php  echo $row_sales_master['sales_name']?>
	      </option>
	    <?php }?>
	    </select></td>
	<th scope="row">Vehicle Registration No:</th>
	<td><input name="payment_terms" type="text" id="payment_terms"  style="width:auto;" tabindex="19" value="<?php echo $_REQUEST['payment_terms'];?>" /></td>
	</tr>
</table>



</div><!-- table close 1 -->

<br />
<h3 align="center">Product Details</h3>
<table width="100%" cellpadding="0" cellspacing="0"  border="0">
<tr>
<td valign="top">
<?php 
if($_REQUEST['po_no']=='')
{ $height='50px';}
else
{ $height='150px'; }
?>
 
 <div style="overflow:auto; width:auto; height:auto">
  <table width="100%" border="0" cellspacing="1" cellpadding="2" class="maintable" align="center" >
    <tr>
      <th>SNo. </th>
      <th>Item Code </th>
      <th nowrap="nowrap"> Description </th>
      <th>P.o. No. </th>
      <th>No &amp; Description of Packaging  </th>
      <th> Quantity </th>
      <th>Unit</th>
      <th>Rate per 1000 units </th>
      <th>Amount  </th>
     </tr>
	 <?php 
	 if($_REQUEST['action']=='addnew')
	 {
	 $Clientcode=$s->getField('clientmaster','id',$_REQUEST['client_id'],'client_code');
	  $detail_sql="Select * from salesorder_details where masterId ='".$_REQUEST['po_no']."'";
	 $detail_result=DB_query($detail_sql,$db);
	 $i=0;
	 while($detail_row=DB_fetch_array($detail_result))
	 {
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
	
	 $_REQUEST['Quantity'][$i]=$detail_row['Quantity'];
	 $_REQUEST['Rate'][$i]=$detail_row['Rate'];
	  $_REQUEST['Unit'][$i]=$detail_row['unit'];
	 
	 ?>
    <tr bgcolor="<?php echo $bg;?>">
      <td><?php echo $i+1;?></td>    
      <td><?php echo $detail_row['ItemCode'];?><input type="hidden" name="ItemCode[<?php echo $i;?>]" value="<?php echo $detail_row['ItemCode'];?>" /></td>
      <td><?php echo $detail_row['ItemDesc'];?></td>
      <td><?php 
	  $ponumber=$s->getField('salesorder','id',$_REQUEST['PO'],'orderNo');
	  echo $ponumber;?></td>
      <td>
 	  <input type="text" name="PackageDesc[<?php echo $i;?>]" value="<?php echo $detail_row['BillingDesc'];?>" /></td>
      <td><input type="text" name="Quantity[<?php echo $i;?>]" id="Quantity_<?php echo $i;?>" value="<?php echo $_REQUEST['Quantity'][$i]?>" readonly /></td>
      <td>
	  <?php echo $unitname=$s->getField('unitsofmeasure','unitid',$_REQUEST['Unit'][$i],'unitname');?>
	  <input type="text" name="Unit[<?php echo $i;?>]"  id="Unit_<?php echo $i;?>" value="<?php echo $_REQUEST['Unit'][$i];?>" /></td>
	  <?php
	 
	  ?>
      <td><input type="text" name="RatePerThousand[<?php echo $i;?>]"  id="RatePerThousand_<?php echo $i;?>" value="<?php echo $_REQUEST['Rate'][$i];?>" /></td>
      <td><input type="text" name="Amount[<?php echo $i;?>]" id="Amount_<?php echo $i;?>" value="<?php echo $detail_row['Amount'];?>" onfocus="calculateAmount(<?php echo $i;?>)"/></td>
    </tr>
	<?php $i++; } ?>
	
<?php } else{ 
//***************************************************************************************
 

	 $detail_sql="Select * from invoice_details where invoiceid   ='".$_REQUEST['id']."'";
	 $detail_result=DB_query($detail_sql,$db);
	 $i=0;
	 while($detail_row=DB_fetch_array($detail_result))
	 {
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
 	?>
    <tr bgcolor="<?php echo $bg;?>">
      <td><?php echo $i+1;?>
	  <input name="itemids[]" type="hidden"  value="<?php echo $detail_row['id'];?>"/>
	  
	  </td>   
      <td><?php echo $detail_row['ItemCode'];?><input type="hidden" name="ItemCode[<?php echo $i;?>]" value="<?php echo $detail_row['ItemCode'];?>" /></td>
      <td><?php echo $detail_row['PackageDesc'];?></td>
      <td><?php 
	  $ponumber=$s->getField('salesorder','id',$_REQUEST['PO'],'orderNo');
	  echo $ponumber;?></td>
      <td>
 	  <input type="text" name="PackageDesc[<?php echo $i;?>]" value="<?php echo $detail_row['PackageDesc'];?>" /></td>
      <td><input type="text" name="Quantity[<?php echo $i;?>]" id="Quantity_<?php echo $i;?>" value="<?php echo $detail_row['Quantity']?>" readonly /></td>
      <td><?php echo  $s->getField('unitsofmeasure','unitid',$detail_row['Unit'],'unitname') ?></td>
      <td><input type="text" name="RatePerThousand[<?php echo $i;?>]"  id="RatePerThousand_<?php echo $i;?>" value="<?php echo $detail_row['RatePerThousand']?>" /></td>
      <td><input type="text" name="Amount[<?php echo $i;?>]" id="Amount_<?php echo $i;?>" value="<?php echo $detail_row['Amount'];?>" onfocus="calculateAmount(<?php echo $i;?>)"/></td>
</tr>
<?php 
   $i++;
}?>



<?php 
//***************************************************************************************
} ?>	
	
  </table></div>
  <input name="rowcount" id="rowcount" 	type="hidden" value="<?php echo $i;?>" />
 </td>
</tr>
 
<tr>
<td valign="top" >
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">&nbsp;</td>
</tr>

<tr>
<td valign="top"><table width="74%" border="0" cellspacing="1" cellpadding="2">
<tr>
  <th valign="top" >Excise Duty in words Rupees.</th>
  <td valign="top" >
    <input name="excise_duty_inwords" id="excise_duty_inwords" type="text" value="<?php echo ucwords($_REQUEST['excise_duty_inwords']);?>" />	</td>
</tr>
<tr>
  <th valign="top" >Date &amp; Time of issue of Invoice:</th>
  <td valign="top" >
  <?php if($_SESSION['issue_invoice_date']=='')
$_SESSION['issue_invoice_date']=date("d/m/Y");
?>
  <input name="issue_invoice_date" type="text" value="<?php if(isset($_REQUEST['issue_invoice_date']) && $_REQUEST['issue_invoice_date']!='')echo ConvertSQLDate($_REQUEST['issue_invoice_date']); ?>" class="datepicker" /> Time: <input name="issue_invoice_time" type="text"     value="<?php  if(isset($_REQUEST['issue_invoice_time']))echo $_REQUEST['issue_invoice_time']; ?>" />
    
  <select name="issue_invoice_datetime" style="width:auto;">
  <option  value="AM" <?php echo ($_REQUEST['issue_invoice_datetime']=='AM'?'selected':'')?>  >AM</option>
  <option  value="PM" <?php echo ($_REQUEST['issue_invoice_datetime']=='PM'?'selected':'')?>>PM</option>
  </select>	  </td>
</tr>
<tr>
      <th>Remarks:</th>
      <td><textarea name="textarea" id="textarea" style="width:150px;"></textarea></td>
  </tr>
</table></td>
<td valign="top">
<div style="width:440px; float:left;">
<table width="100%" border="0"cellspacing="1" cellpadding="1">
<tr>
	<th scope="row">Value Of Goods </th>
	<td><input name="value_of_goods" type="text" id="value_of_goods" tabindex="<?php echo $tabindex++?>" value="<?php echo $_REQUEST['value_of_goods']; ?>" onfocus="cal_value_of_goods();" readonly="true"></td>
  </tr>
<?php
//echo "Select * from excise_tax where form_type='Sales'";
  $get_excisetax=DB_query("Select * from excise_tax where form_type='Sales'",$db);
while($row_excisetax=DB_fetch_array($get_excisetax))
{
?>
<tr>
<th   scope="row">Add :<?php echo $row_excisetax['caption']?></th>
<td valign="top" >
<input name="hid_<?php echo $row_excisetax['label']?>" type="hidden" id="hid_<?php echo $row_excisetax['label']?>" value="<?php echo $row_excisetax['def_rate'];?>" /><input name="hidtax_<?php echo $row_excisetax['label']?>" type="hidden" value="<?php echo $row_excisetax['ledger']?>" id="hidtax_<?php echo $row_excisetax['label']?>" />
<?php //echo $row_excisetax['def_rate']. "-->".$row_excisetax['def_rate']*$_REQUEST['value_of_goods']*.01?>
<input name="<?php echo $row_excisetax['label']?>" type="text" id="<?php echo $row_excisetax['label']?>"  value="<?php echo $row_excisetax['def_rate']*$_REQUEST['value_of_goods']*.01?>" onfocus="cal_<?php echo $row_excisetax['label'] ?>();" readonly="true"></td>

</tr>
<?php } ?>

<tr>
	<th scope="row">Total Taxable </th>
	<td valign="top" ><input name="total_taxable" type="text" id="total_taxable"  value="<?php  echo $_REQUEST['total_taxable']; ?>" onfocus="cal_total_taxable();"readonly="true" ></td>
  </tr>
<tr>
	<td scope="row" nowrap="nowrap"><span style="background-color:#cccccc; padding-top:2px; padding-bottom:5px; padding-left:10px; padding-right:10px;">VAT/CST:</span> &nbsp;&nbsp;<select name="vat_cst_ledger" onchange="ShowSalesTax(document.getElementById('total_taxable').value,this.value,<?php echo $tabindex?>)" tabindex="<?php echo $tabindex++?>" style="width:auto;" >
	      <option value="000">Select</option>
	      <?php  $selsales_tax=DB_query("select salestax_id ,tax_code,ledger_id   from sales_tax_master",$db);
		  while($rowsales_tax=DB_fetch_array($selsales_tax))
		  {
			$ps='';
			if($rowsales_tax['ledger_id']==$_REQUEST['vat_cst_ledger'])
			$ps='selected';
	?>
	      <option value="<?php echo $rowsales_tax['ledger_id'] ?>" <?php echo $ps; ?>><?php echo $rowsales_tax['tax_code'] ?></option>
	      <?php } ?>
	          </select>
        </td>
	<td valign="top" >
	
	<div id="SalesTaxRate">
	<?php $vatrate=$s->getField('sales_tax_master','ledger_id',$_REQUEST['vat_cst_ledger'],'taxrate'); ?>

	<input name="vat_cst" type="hidden" id="vat_cst" value="<?php echo $vatrate ?> "   readonly="true"> 
	  
	  <input name="calvatcst" id="calvatcst" type="text" value="<?php echo  ($_REQUEST['total_taxable']*$_REQUEST['vat_cst']*.01) ?>"  onfocus="cal_vatcst(this.value)"/>          
	  </div>		</td>
  </tr>
   <tr>
	<th scope="row">Add. Tax</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
  <tr>
	<th scope="row"> Freight Charges</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
   <tr>
	<th scope="row">Pkg & Forwarding</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
   <tr>
	<th scope="row">Misc/Other Charges</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
  <tr>
	<th scope="row">Round Off</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
  <tr>
	<th scope="row"> Grand Total</th>
	<td valign="top" ><input name="grand_total" type="text" id="grand_total"   value="<?php  echo $_REQUEST['grand_total']; ?>" onblur="cal_grand_total();"  ></td>
  </tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p style="float:left;">
  <input name="Save_invoice"  value="Save" type="submit" class="bottonsview" />
  <a href="index.php?route=ViewInvoice<?php  echo $rurl; ?>"><input name="Cancel" type="button" value="Cancel" class="bottonsview" /></a>
</p>
</form>

</fieldset>
</div>