<?php 
error_reporting(0);
session_start();

 $lastinvoice_id = $_SESSION['lastinvoice_id']; 

 
if(!isset($_SESSION['admin']) || ($_SESSION['admin'] == "") )
{
	?>
<script language="javascript">window.location.href="index.php";</script>
<?php 
}
include "config.php";
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Generate Invoice Detail</title>
<meta name="author" content="pixelcave">
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="shortcut icon" href="img/favicon.ico">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="57x57" href="img/apple-touch-icon-57x57-precomposed.png">
<link rel="apple-touch-icon" sizes="72x72" href="img/apple-touch-icon-72x72-precomposed.png">
<link rel="apple-touch-icon" sizes="114x114" href="img/apple-touch-icon-114x114-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="img/apple-touch-icon-precomposed.png">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:400,400italic,700,700italic">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/plugins.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/themes.css">
<link href="datepicker/css/datepicker.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="datepicker/js/datepicker.js"></script>
<script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
<script type="application/javascript">
  function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
</script>

<script type="text/javascript" src="js/jsapi.js"></script>
<script type="text/javascript">
        // Load the Google Transliteration API
        google.load("elements", "1", {
          packages: "transliteration"
        });

        function onLoad() {
          var options = {
            sourceLanguage: 'en',
            destinationLanguage: ['gu'],
            shortcutKey: 'ctrl+m',
            transliterationEnabled: true
          }

          // Create an instance on TransliterationControl with the required options.
          var control = new google.elements.transliteration.TransliterationControl(options);

          // Enable transliteration in the textfields with the given ids.
          var ids = ["language"];
          control.makeTransliteratable(ids);
 
    var ids = ["language2"];
          control.makeTransliteratable(ids);
          // Show the transliteration control which can be used to toggle between English and Hindi and also choose other destination language.
          control.showControl('translControl');
        }
        google.setOnLoadCallback(onLoad);
      </script>
      
    <!-- <link rel="stylesheet" href="js/style.css">
<script src="js/jquery.js"></script>
<script src="js/choosen.js"></script>-->

</head>
<body>
<div style='display:none;'>
  <div id="send_testimonial_afb_area">
    <div id="send_testimonial_afb_note" class="afb_note" ></div>
    <div id="send_testimonial_afb_fields">
      <center>
        <form id="send_testimonial" name="send_testimonial" class="form-horizontal form-box" method="post" >
          <!--<form id="send_testimonial" name="send_testimonial" method="post" action="#">-->
          <h1 class="form-box-header">Select Invoice Format</h1>
          <table align="center" cellpadding="0" cellspacing="0" width="100%">
            <!--<tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field">Your Name</label>
                  </div></td>
                <td width="60%"><input  type="text" name="sender_name" value="" />
                </td>
              </tr>-->
            <!-- <tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field">Collage Name</label>
                  </div></td>
                <td width="60%"><input  type="text" name="sender_name" value="" />
                </td>
              </tr>-->
            <!-- <tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field">Email</label>
                  </div></td>
                <td width="60%"><input  type="text" name="sender_name" value="" />
                </td>
              </tr>-->
            <!-- <tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field">Mobile</label>
                  </div></td>
                <td width="60%"><input  type="text" name="sender_name" value="" />
                </td>
              </tr>-->
            <tr>
              <td width="40%"><div class="wrap">
                  <label class="in_field">Invoice Format</label>
                </div></td>
              <td width="60%"><select name="invoice_format">
                  <option value="">--Select Format--</option>
                  <option value="with_header">With Header</option>
                  <option value="without_header">Without Header</option>
                </select>
              </td>
            </tr>
            <tr>
              <td><br></td>
            </tr>
            <!--<tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field" >Stream</label>
                  </div></td>
                <td width="60%"><select id="send_testimonial_sender_name">
                    <option value="">--select--</option>
                  </select>
                </td>
              </tr>-->
            <!--<tr>
                <td width="40%"><div class="wrap">
                    <label class="in_field" >Message</label>
                  </div></td>
                <td width="60%"><textarea rows="5" cols="25"  name="testimonial" ></textarea>
                </td>
              </tr>-->
            <tr>
              <td width="40%">&nbsp;</td>
              <td width="60%"><button class="btn btn-success" name="generate_print"><i class="icon-print"></i> Print Invoice</button>
                <!--<input type="submit" name="generate_print" value="Print Invoice" class="btn btn-success" />-->
              </td>
            </tr>
            <tr>
              <td><br></td>
            </tr>
          </table>
        </form>
      </center>
    </div>
  </div>
</div>
<div id="page-container">
<?php include 'header.php' ?>
<div id="inner-container">
<?php include 'left_menu.php' ?>
<div id="page-content">
<ul id="nav-info" class="clearfix">
  <li><a href="#"><i class="icon-home"></i></a></li>
  <li class="active"><a href="generate_invoice_list.php">Generate Invoice Detail List</a></li>
</ul>
<?php 
	   $invoice_dtid = $_GET['invoice_dtid']; 
	    $invoiceid = $_GET['invoiceid']; 
	  
	  
	  if($invoice_dtid)
	  {
	$ure=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");		
	$sord=mysql_fetch_array($ure); 
	$total_amount = $sord['total_amount']; 
	$adv_amount= $sord['adv_amount'];  
	$pending_amount= $sord['pending_amount'];  
	
	
	} else if ($invoiceid){
    $ure=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");		
	$sord=mysql_fetch_array($ure); 
	$total_amount = $sord['total_amount']; 
	$adv_amount= $sord['adv_amount'];  
	$pending_amount= $sord['pending_amount']; 
		
	} else {
    $ure=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$lastinvoice_id'");		
	$sord=mysql_fetch_array($ure); 
	$total_amount = $sord['total_amount']; 
	$adv_amount= $sord['adv_amount'];  
	$pending_amount= $sord['pending_amount']; 
	}
	  
	   $ure0=mysql_query("select * from tbl_generate_invoice_mst order by invoice_id desc");
		
		$row0=mysql_fetch_array($ure0); 
		$invoice_no = $row0['invoice_no']; 
		
if($invoiceid){

if ($_POST['add1']) 
{
	
	//$cat_id=$_REQUEST['cat_id'];
//	$ure222=mysql_query("select * from   tbl_category_mst where cat_id='$cat_id'"); 
//	$row222=mysql_fetch_array($ure222); 
// 	$category_name = $row222['category_name'];
	
	//$mat_id=$_REQUEST['mat_id'];
//	$ure333=mysql_query("select * from   tbl_material_mst where mat_id='$mat_id'"); 
//	$row333=mysql_fetch_array($ure333); 
// 	$material_name = $row333['material_name'];
	
	$prd_id=$_REQUEST['prd_id']; 
	$ure444=mysql_query("select * from  tbl_product_master where prd_id='$prd_id'"); 
	$row444=mysql_fetch_array($ure444); 
 	$productname = $row444['productname'];
	
	$title=$_REQUEST['title'];
	$amount=$_REQUEST['amount'];
	$qty=$_REQUEST['qty'];
	$price=$_REQUEST['price'];
	$duration=$_REQUEST['duration'];
	$remark=$_REQUEST['remark'];
	
	$total_amount = $price * $qty;
//	$driver=$_REQUEST['driver']; 
//	$vehicle_exp=$_REQUEST['vehicle_exp'];
//	$liter=$_REQUEST['liter'];
//	$amount=$_REQUEST['amount'];
//	$paymentby=$_REQUEST['paymentby'];
//	$remark=$_REQUEST['remark'];
	$status=$_REQUEST['status'];
	
  $sql=mysql_query("INSERT INTO 
		tbl_generate_invoice_detail
		(invoice_id,prd_id,productname,title,amount,qty,price,duration,remark,total_amount,status) 
		VALUES ('$invoiceid','$prd_id','$productname','$title','$amount','$qty','$price','$duration','$remark','$total_amount','$status')");
		
		 $lastinvoice_detail_id =  mysql_insert_id();
	  	 $_SESSION['lastinvoice_detail_id'] = $lastinvoice_detail_id;
		 $lastinvoice_detail_id = $_SESSION['lastinvoice_detail_id'];

} 


 $query = "SELECT SUM(qty),SUM(total_amount) FROM tbl_generate_invoice_detail where  invoice_id = '$invoiceid'"; 
 $result = mysql_query($query) or die(mysql_error());

// Print out result
    while($row = mysql_fetch_array($result))
	{
 	$qty =  $row['SUM(qty)'];
 	$rate =  $row['SUM(total_amount)'];		
	}

	$uupQry="UPDATE tbl_generate_invoice_mst SET totalqty='$qty',total_amount='$rate' WHERE invoice_id='$invoiceid'";
	$uuresult=mysql_query($uupQry);

if(isset($_REQUEST['generate_print']))
{
	$invoice_format=$_REQUEST['invoice_format'];
?>
<script>window.location="generate_invoice_list.php";</script>
<?php	
}
} else {		
	  
if ($_POST['add']) 
{
	
//$v_id=$_REQUEST['v_id'];
//	$ure111=mysql_query("select * from  tbl_vehicle_mst where v_id='$v_id'"); 
//	$row111=mysql_fetch_array($ure111); 
// 	$vehicle_no = $row111['vehicle_no'];
	
	
	//$cat_id=$_REQUEST['cat_id'];
//	$ure222=mysql_query("select * from   tbl_category_mst where cat_id='$cat_id'"); 
//	$row222=mysql_fetch_array($ure222); 
// 	$category_name = $row222['category_name'];
	
	//$mat_id=$_REQUEST['mat_id'];
//	$ure333=mysql_query("select * from   tbl_material_mst where mat_id='$mat_id'"); 
//	$row333=mysql_fetch_array($ure333); 
// 	$material_name = $row333['material_name'];
	
	$prd_id=$_REQUEST['prd_id']; 
	$ure444=mysql_query("select * from  tbl_product_master where prd_id='$prd_id'"); 
	$row444=mysql_fetch_array($ure444); 
 	$productname = $row444['productname'];
	
	$title=$_REQUEST['title'];
	$amount=$_REQUEST['amount'];
	$qty=$_REQUEST['qty'];
	$price=$_REQUEST['price'];
	$duration=$_REQUEST['duration'];
	$remark=$_REQUEST['remark'];
	
	$total_amount = $price * $qty;
	$pendingamount = $total_amount - $adv_amount; 
//	$driver=$_REQUEST['driver']; 
//	$vehicle_exp=$_REQUEST['vehicle_exp'];
//	$liter=$_REQUEST['liter'];
//	$amount=$_REQUEST['amount'];
//	$paymentby=$_REQUEST['paymentby'];
//	$remark=$_REQUEST['remark'];
	$status=$_REQUEST['status'];
	
  $sql=mysql_query("INSERT INTO 
		tbl_generate_invoice_detail
		(invoice_id,prd_id,productname,title,amount,qty,price,duration,remark,total_amount,status) 
		VALUES ('$lastinvoice_id','$prd_id','$productname','$title','$amount','$qty','$price','$duration','$remark','$total_amount','$status')");
		
		 $lastinvoice_detail_id =  mysql_insert_id();
	  	 $_SESSION['lastinvoice_detail_id'] = $lastinvoice_detail_id;
		 $lastinvoice_detail_id = $_SESSION['lastinvoice_detail_id'];

} 


 $query = "SELECT SUM(qty),SUM(total_amount) FROM tbl_generate_invoice_detail where  invoice_id = '$lastinvoice_id'";  
 $result = mysql_query($query) or die(mysql_error());

// Print out result
    while($row = mysql_fetch_array($result))
	{
 	$qty =  $row['SUM(qty)'];
 	$rate =  $row['SUM(total_amount)'];	
		
	}

	$uupQry="UPDATE tbl_generate_invoice_mst SET totalqty='$qty',total_amount='$rate' WHERE invoice_id='$lastinvoice_id'";
	$uuresult=mysql_query($uupQry);

if(isset($_REQUEST['generate_print']))
{
	$invoice_format=$_REQUEST['invoice_format'];
?>
<script>window.location="print_invoice_with_logo.php?lastinvoce_id=<?php echo $lastinvoice_id; ?>&&invoice_format=<?php echo $invoice_format; ?>";</script>
<?php	
}
}
				
 if(isset($_REQUEST['up'])) 
      {  	  
	//$cat_id=$_REQUEST['cat_id'];
	//	$ure222=mysql_query("select * from   tbl_category_mst where cat_id='$cat_id'"); 
	//	$row222=mysql_fetch_array($ure222); 
	// 	$category_name = $row222['category_name'];
		
	//$mat_id=$_REQUEST['mat_id'];
	//	$ure333=mysql_query("select * from   tbl_material_mst where mat_id='$mat_id'"); 
	//	$row333=mysql_fetch_array($ure333); 
	// 	$material_name = $row333['material_name'];

	$prd_id=$_REQUEST['prd_id']; 
	$ure444=mysql_query("select * from  tbl_product_master where prd_id='$prd_id'"); 
	$row444=mysql_fetch_array($ure444); 
 	$productname = $row444['productname'];
	
	$title=$_REQUEST['title'];
	$amount=$_REQUEST['amount'];
	$qty=$_REQUEST['qty'];
	$price=$_REQUEST['price'];
	$duration=$_REQUEST['duration'];
	$remark=$_REQUEST['remark'];
	//$adv_amount=$_REQUEST['adv_amount'];
	
	$total_amount = $price * $qty;
//	$driver=$_REQUEST['driver']; 
//	$vehicle_exp=$_REQUEST['vehicle_exp'];
//	$liter=$_REQUEST['liter'];
//	$amount=$_REQUEST['amount'];
//	$paymentby=$_REQUEST['paymentby'];
//	$remark=$_REQUEST['remark'];
	$status=$_REQUEST['status'];
	
	$uupQry="UPDATE tbl_generate_invoice_detail SET invoice_id='$invoiceid',prd_id='$prd_id',productname='$productname',title='$title',amount='$amount',qty='$qty',price='$price',duration='$duration',remark='$remark',total_amount='$total_amount',status='$status' where quotation_in_id = '$invoice_dtid'"; 
	$uuresult=mysql_query($uupQry);
	$lastinvoice_detail_id =  mysql_insert_id();
	$_SESSION['lastinvoice_detail_id'] = $lastinvoice_detail_id;
	$lastinvoice_detail_id = $_SESSION['lastinvoice_detail_id'];
	
	//$quotation_id = $_GET['reg'];
	$query = "SELECT SUM(qty),SUM(total_amount) FROM tbl_generate_invoice_detail where  invoice_id = '$invoiceid'";  
    $result = mysql_query($query) or die(mysql_error());

// Print out result
    while($row = mysql_fetch_array($result))
	{
 	$qty =  $row['SUM(qty)'];
	$rate =  $row['SUM(total_amount)'];		
	}
	
	$uupQry="UPDATE  tbl_generate_invoice_mst SET totalqty='$qty',total_amount='$rate' WHERE invoice_id	='$invoiceid'";
	$uuresult=mysql_query($uupQry);
  }
  
   if(isset($_REQUEST['generate_update']))
    {
?>
<script>window.location="generate_invoice_list.php";</script>
<?php	
}?>
<?php 						
		$ure=mysql_query("select * from tbl_generate_invoice_detail where quotation_in_id='$invoice_dtid'");
		$urow=mysql_fetch_array($ure); ?>
<form id="form-validation"  method="post" name="form" class="form-horizontal form-box remove-margin">
  <div class="form-box-content">
    <div class="control-group">
      <label class="control-label" for="val_username">પ્રોડક્ટ નામ : &nbsp;&nbsp;</label>
      <div class="controls">
        <div class="input-prepend">
         <!-- <input type="text" required id="language" name="title" value="<?php //echo $urow['title']; ?>">-->     
       <select id="select_page"  data-placeholder="Select Product Name..." class="operator" name="prd_id"  tabindex="1" >
            <option value="">Select Product Name....</option>
            <?php 
			$ure33=mysql_query("select * from  tbl_product_master");
			while($urow33=mysql_fetch_array($ure33)) {?>
            <option value="<?php echo $prd_id=$urow33['prd_id']; ?>" <?php if($urow['prd_id'] == $urow33['prd_id']) echo 'selected="selected"'?>>
            <?php  echo $productname=$urow33['productname']; ?>
            </option>
            <?php } ?>
          </select>
        </div>
        <div class="input-prepend">
          <label class="control-label" for="val_username">જથ્થો : &nbsp;&nbsp;</label>
          <input type="text" tabindex="2" required id="val_username" name="qty" value="<?php echo $urow['qty']; ?>">
        </div>
        <!-- <div class="input-prepend">
          <label class="control-label" for="val_username">Price : &nbsp;&nbsp;</label>
          <input type="text" required id="val_username" name="price" value="<?php //echo $urow['price']; ?>">
        </div>-->
      </div>
    </div>
    <div class="control-group">
      <label class="control-label" for="val_username">કિંમત : &nbsp;&nbsp;</label>
      <div class="controls">
        <div class="input-prepend">
          <input type="text" required id="val_username" tabindex="3" name="price" value="<?php echo $urow['price']; ?>">
        </div>
        <!-- <div class="input-prepend">
          <label class="control-label" for="val_username">Remark : &nbsp;&nbsp;</label>
          <textarea id="" style="width:253px;" name="remark"><?php //echo $urow['remark ']; ?></textarea>
        </div>-->
      </div>
    </div>
    <div class="form-actions">
      <?php
					 
                        if($invoice_dtid)
						{
						?>
      <input type="submit" name="up" value="પ્રોડક્ટ સુધારો " class="btn btn-success">
      <?php 
}
else if($invoiceid)
{

?>
      <input type="submit" name="add1" value="પ્રોડક્ટ ઉમેરો" class="btn btn-success">
      <?php } else { ?>
      <input type="submit" name="add" value="પ્રોડક્ટ ઉમેરો" class="btn btn-success">
      <?php } ?>
      <!--<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Reset</button>-->
    </div>
  </div>
</form>
<table id="example-datatables" class="table table-striped table-bordered table-hover">
  <thead>
    <tr>
      <th class="span1">Action</th>
      <th>Product Name </th>
      <!--  <th>Title / Domain Name </th>-->
      <!--<th>Approx. Amount </th>  -->
      <th>Qty.</th>
      <th>Price</th>
      <th>Amount</th>
      <!--<th>Remarks </th>-->
      <!--<th>Weight Date</th>
  <th>Charges</th>-->
      <!-- <th> Weight Detail </th>-->
      <!--<th class="hidden-phone hidden-tablet">Status</th>-->
    </tr>
  </thead>
  <tbody>
    <?php if($invoice_dtid) {
	$ure= mysql_query("SELECT * FROM  tbl_generate_invoice_detail where invoice_id = '$invoiceid'") ;	
		while($urow=mysql_fetch_array($ure)) {
		?>
    <tr>
      <td class="span1"><div class="btn-group">
          <?php /*?><a href="weight_add.php?reg=<?php echo $urow[0]; ?>" data-toggle="tooltip" title="Edit" class="btn btn-mini btn-success" style="margin-right:10px;"><i class="icon-pencil"></i></a><?php */?>
          <?php echo "<a href='#' data-toggle='tooltip' title='Delete' class='btn btn-mini btn-danger' onclick='del(this,$urow[0])'>" ?><i class="icon-remove"></i></a></div></td>
      <td><?php echo $urow['productname']; ?></td>
      <!--<td><?php //echo $urow['title']; ?></td>-->
      <td><?php echo $qty = $urow['qty']; ?></td>
      <td><?php echo $price = $urow['price']; ?></td>
      <td><div align="right">
          <?php 	echo $amount = $qty * $price; ?>
          .00</div></td>
      <!-- <td><?php //echo $urow['remark']; ?></td>-->
    </tr>
    <?php } ?>
    <tr>
      <td colspan="5"><br />
      </td>
    </tr>
    <tr>
      <td colspan="4" align="right"><div align="right"><strong>Total Amount:</strong></div></td>
      <td colspan="1"><div align="right"><strong>
          <?php /*?> <?php
                       		$query1223 = "SELECT SUM(total_amount) FROM tbl_sale_invoice_detail where salein_id='$salein_id'"; 
	  						   	  $result1223 = mysql_query($query1223) or die(mysql_error());
							      $row1223 = mysql_fetch_array($result1223);
								echo  $total_sum =  $row1223['SUM(total_amount)']; 
								 ?>.00/-<?php */?>
          <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$invoiceid'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalamount = $sord111['total_amount']; 
						?>
          .00 </strong> </div></td>
    </tr>
    <tr>
      <td colspan="4" align="right"><div align="right"><strong>Adv Amount:</strong></div></td>
      <td colspan="1"><div align="right"><strong>
          <?php
					 	echo $adv_amount;   ?>
          </strong> </div></td>
    </tr>
    <tr>
      <td colspan="4" align="right"><div align="right"><strong>Final Amount:</strong></div></td>
      <td colspan="1"><div align="right"><strong>
          <?php 
							echo $totalfinalamount = $totalamount - $adv_amount;
						
						?>
          .00</strong>
          <?php /*?>  <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$invoiceid'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalfinalamount = $sord111['total_amount']; 
						?>.00 </strong>			<?php */?>
        </div></td>
    </tr>
    <tr>
      <div align="right">
      <form id="select_page"   method="post" name="form" class="form-horizontal form-box remove-margin">
        <?php 
					$ure=mysql_query("select * from   tbl_customer_payment order by ABS(receipt_no) desc");
					$srow = mysql_fetch_array($ure);
					 $code = $srow['receipt_no'] + 1; 
					//$dr_dep_id = $srow['dr_dep_id'];
					
   				 ?>
        <input name="receipt_no" type="hidden" value="<?php echo $code; ?>"  />
        <?php 						
		$ure=mysql_query("select * from tbl_customer_payment where invoice_id='$invoiceid'");
		$urow111=mysql_fetch_array($ure); ?>
        <td colspan="2">Adv Payment Mode:</td>
        <td colspan="9"> Cash
          <input type="radio" id="val_username" name="payment_through" value="cash" checked="checked" onClick="displayForm(this)">
          Cheque
          <input type="radio" id="val_username" name="payment_through" value="cheque"  onClick="displayForm(this)">
          <div style="visibility:visible;position:relative;" id="paypalformContainer">
            <label class="control-label" for="val_username">Cash Amount&nbsp;&nbsp;&nbsp;</label>
            <dd>
              <input type="text" id="paypal" name="adv_amount" value="<?php echo $urow111['adv_amount']; ?>" class="form-control" >
            </dd>
          </div>
          <div style="visibility:hidden; position:relative" id="ccformContainer">
            <dd> Cheque Amount :&nbsp;&nbsp;&nbsp;
              <input type="text" name="adv_amount1" value="<?php echo $urow111['adv_amount1']; ?>" class="form-control">
              &nbsp;&nbsp;&nbsp;  Bank Name: &nbsp;&nbsp;&nbsp;
              <input type="text" name="bank_name" value="<?php echo $urow111['bank_name']; ?>" class="form-control">
              <br>
              Account Number : &nbsp;
              <input type="text" name="acc_no" value="<?php echo $urow111['acc_no']; ?>" class="form-control">
              &nbsp;&nbsp;&nbsp;Cheque No. : &nbsp;&nbsp;&nbsp;
              <input type="text" name="cheque_no" value="<?php echo $urow111['cheque_no']; ?>" class="form-control">
              <br>
              Cheque Date :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input name="cheque_date"  class="form-control"   type="text" value="<?php echo $urow111['cheque_date']; ?>"  tabindex="1" placeholder='dd-mm-yyyy' />
            </dd>
          </div>
          <?php
					 
                        if($invoice_dtid)
						{
						?>
          <input type="submit" name="update_advance_payment" value="Update Adv.Payment" class="btn btn-success">
          <?php } else { ?>
          <input type="submit" name="add_advance_payment" value="Add Adv.Payment" class="btn btn-success">
          <?php } ?>
        </td>
        <!--<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Reset</button>-->
        </div>
      </form>
  </div>
  
  </tr>
  
  <?php
  if(isset($_REQUEST['update_advance_payment']))
   {
   			
			$receipt_no=$_REQUEST['receipt_no'];
   			$payment_through=$_REQUEST['payment_through'];
 		    $cheque_cash_amount=$_REQUEST['adv_amount'];
			$cheque_cash_amount1=$_REQUEST['adv_amount1']; 
 		    $bank_name = $_REQUEST['bank_name'];
			$cheque_no = $_REQUEST['cheque_no'];
			$date1=$_REQUEST['cheque_date'];
			$cheque_date=date("Y-m-d", strtotime($date1));  
			$acc_no = $_REQUEST['acc_no'];
			
		
	    $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		$urow11=mysql_fetch_array($ure11); 
		
		$invoice_no	 = $urow11['invoice_no'];  
		$customername	 = $urow11['customername'];  
    	$invoice_date	 = $urow11['invoice_date']; 
		$total_amount	 = $urow11['total_amount']; 
		
		$payment_date = $invoice_date;
		 
		
		if($payment_through == 'cheque'){
		$new_adv_amount =  $cheque_cash_amount1;
		}else{
		$new_adv_amount =  $cheque_cash_amount;
		}
	    $new_pending_amount =  $total_amount - $new_adv_amount;
			
	
 if($payment_through == 'cheque')	
		{
		
		if($cheque_cash_amount1 == '')
		{
		 $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		 $urow11=mysql_fetch_array($ure11); 
			 $adv_amount1 = $urow11['adv_amount'];
			 $total_amount1 = $urow11['total_amount'];
			 $new_pending_amount1 = $total_amount1 - $adv_amount1; 
			 
	$uupQry="UPDATE  tbl_customer_payment SET invoice_id='$invoice_id',invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";
  $uuresult=mysql_query($uupQry);
  
    
     $uupQry="UPDATE  tbl_generate_invoice_mst SET 	pending_amount='$new_pending_amount1',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
		}
		else
		{
$uupQry="UPDATE  tbl_customer_payment SET invoice_id='$invoice_id',invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',cheque_cash_amount='$cheque_cash_amount1',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";
  $uuresult=mysql_query($uupQry);
  
    
     $uupQry="UPDATE  tbl_generate_invoice_mst SET 	adv_amount='$cheque_cash_amount1',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
 		}
 
		}
	  
	   else
	   {
		   
		if($cheque_cash_amount == '')
		{
		$ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		 $urow11=mysql_fetch_array($ure11); 
			 $adv_amount1 = $urow11['adv_amount'];
			 $total_amount1 = $urow11['total_amount'];
			 $new_pending_amount1 = $total_amount1 - $adv_amount1; 
			 
		$uupQry="UPDATE  tbl_customer_payment SET invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'"; 
  $uuresult=mysql_query($uupQry);
  
   $uupQry="UPDATE  tbl_generate_invoice_mst SET pending_amount='$new_pending_amount1',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
		}
		else
		{
 $uupQry="UPDATE  tbl_customer_payment SET invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',cheque_cash_amount='$cheque_cash_amount',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";   $uuresult=mysql_query($uupQry);
 
  $uupQry="UPDATE  tbl_generate_invoice_mst SET adv_amount='$cheque_cash_amount',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
   		}
   
	   }
	  
   ?>
  <script language="javascript">window.location.href="generate_extra_invoice_detail_add.php?invoice_dtid=<?php echo $invoice_dtid; ?>&&invoiceid=<?php echo $invoiceid; ?>";</script>
  <?php
 	}
?>
  <tr>
    <td colspan="5" align="center"><input type="submit" name="generate_update" value="Update & Close Invoice" class="btn btn-success" id='contact_form_link'>
      <!-- <a href="#" id='contact_form_link'>Request for Quote</a> -->
    </td>
  </tr>
  <?php } else if($invoiceid) { ?>
  <?php 						
		

	$ure= mysql_query("SELECT * FROM  tbl_generate_invoice_detail where invoice_id = '$invoiceid'") ;	
		while($urow=mysql_fetch_array($ure)) {
		?>
  <tr>
    <td class="span1"><div class="btn-group">
        <?php /*?><a href="weight_add.php?reg=<?php echo $urow[0]; ?>" data-toggle="tooltip" title="Edit" class="btn btn-mini btn-success" style="margin-right:10px;"><i class="icon-pencil"></i></a><?php */?>
        <?php echo "<a href='#' data-toggle='tooltip' title='Delete' class='btn btn-mini btn-danger' onclick='del(this,$urow[0])'>" ?><i class="icon-remove"></i></a></div></td>
   <td><?php echo $urow['productname']; ?></td>
   <!-- <td><?php //echo $urow['title']; ?></td>-->
    <td><?php echo $qty = $urow['qty']; ?></td>
    <td><?php echo $price = $urow['price']; ?></td>
    <td><div align="right">
        <?php 	echo $amount = $qty * $price; ?>
        .00</div></td>
    <!--<td><?php //echo $urow['remark']; ?></td>-->
  </tr>
  <?php } ?>
  <tr>
    <td colspan="5"><br />
    </td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Total Amount:</strong></div></td>
    <td colspan="1"><div align="right"><strong>
        <?php /*?> <?php
                       		$query1223 = "SELECT SUM(total_amount) FROM tbl_sale_invoice_detail where salein_id='$salein_id'"; 
	  						   	  $result1223 = mysql_query($query1223) or die(mysql_error());
							      $row1223 = mysql_fetch_array($result1223);
								echo  $total_sum =  $row1223['SUM(total_amount)']; 
								 ?>.00/-<?php */?>
        <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$invoiceid'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalamount = $sord111['total_amount']; 
						?>
        .00 </strong> </div></td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Adv Amount:</strong></div></td>
    <td colspan="1">
      <div align="right">
        <?php /*?> <?php
                       		$query1223 = "SELECT SUM(total_amount) FROM tbl_sale_invoice_detail where salein_id='$salein_id'"; 
	  						   	  $result1223 = mysql_query($query1223) or die(mysql_error());
							      $row1223 = mysql_fetch_array($result1223);
								echo  $total_sum =  $row1223['SUM(total_amount)']; 
								 ?>.00/-<?php */?>
        <?php
					 	echo $adv_amount;   ?>
      </div></td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Final Amount:</strong></div></td>
    <td colspan="1"><div align="right"><strong>
        <?php 
							echo $totalfinalamount = $totalamount - $adv_amount;
						
						?>
        .00</strong>
        <?php /*?>  <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$lastinvoice_id'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalfinalamount = $sord111['total_amount']; 
						?>.00 </strong>			<?php */?>
      </div></td>
  </tr>
  <tr>
    <div align="right">
    <form id="form-validation"  method="post" name="form" class="form-horizontal form-box remove-margin">
      <?php 
					$ure=mysql_query("select * from   tbl_customer_payment order by ABS(receipt_no) desc");
					$srow = mysql_fetch_array($ure);
					 $code = $srow['receipt_no'] + 1; 
					//$dr_dep_id = $srow['dr_dep_id'];
					
   				 ?>
      <input name="receipt_no" type="hidden" value="<?php echo $code; ?>"  />
      <td colspan="2">Adv Payment Mode:</td>
      <td colspan="9"> Cash
        <input type="radio" id="val_username" name="payment_through" value="cash" checked="checked" onClick="displayForm(this)">
        Cheque
        <input type="radio" id="val_username" name="payment_through" value="cheque"  onClick="displayForm(this)">
        <div style="visibility:visible;position:relative;" id="paypalformContainer">
          <label class="control-label" for="val_username">Cash Amount&nbsp;&nbsp;&nbsp;</label>
          <dd>
            <input type="text" id="paypal" name="adv_amount" class="form-control" >
          </dd>
        </div>
        <div style="visibility:hidden; position:relative" id="ccformContainer">
          <dd> Cheque Amount :&nbsp;&nbsp;&nbsp;
            <input type="text" name="adv_amount1" class="form-control">
            &nbsp;&nbsp;&nbsp;  Bank Name: &nbsp;&nbsp;&nbsp;
            <input type="text" name="bank_name" class="form-control">
            <br>
            Account Number : &nbsp;
            <input type="text" name="acc_no" class="form-control">
            &nbsp;&nbsp;&nbsp;Cheque No. : &nbsp;&nbsp;&nbsp;
            <input type="text" name="cheque_no" class="form-control">
            <br>
            Cheque Date :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="cheque_date"  class="form-control"   type="text" value="<?php echo $urow['cheque_date']; ?>"  tabindex="1" placeholder='dd-mm-yyyy' />
          </dd>
        </div>
        <input type="submit" name="advance_payment1" value="Add Adv.Payment" class="btn btn-success">
      </td>
      <!--<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Reset</button>-->
      </div>
    </form>
  </div>
  
  </tr>
  
  <?php
  if(isset($_REQUEST['advance_payment1']))
   {
   			
			$receipt_no=$_REQUEST['receipt_no'];
   			$payment_through=$_REQUEST['payment_through'];
 		    $cheque_cash_amount=$_REQUEST['adv_amount'];
			$cheque_cash_amount1=$_REQUEST['adv_amount1']; 
 		    $bank_name = $_REQUEST['bank_name'];
			$cheque_no = $_REQUEST['cheque_no'];
			$date1=$_REQUEST['cheque_date'];
			$cheque_date=date("Y-m-d", strtotime($date1));  
			$acc_no = $_REQUEST['acc_no'];
			
		
	    $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		$urow11=mysql_fetch_array($ure11); 
		
		$invoice_no	 = $urow11['invoice_no'];  
		$customername	 = $urow11['customername'];  
    	$invoice_date	 = $urow11['invoice_date'];  
		$total_amount	 = $urow11['total_amount']; 
		
		$payment_date = $invoice_date;
		 
		
		if($payment_through == 'cheque'){
		$new_adv_amount =  $cheque_cash_amount1;
		}else{
		$new_adv_amount =  $cheque_cash_amount;
		}
	    $new_pending_amount =  $total_amount - $new_adv_amount;
			
	
 if($payment_through == 'cheque')	
		{
		
		if($cheque_cash_amount1 == '')
		{
		$ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		 $urow11=mysql_fetch_array($ure11); 
			 $adv_amount1 = $urow11['adv_amount'];
			 $total_amount1 = $urow11['total_amount'];
			 $new_pending_amount1 = $total_amount1 - $adv_amount1; 
		
	$uupQry="UPDATE  tbl_customer_payment SET invoice_id='$invoice_id',invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";
  $uuresult=mysql_query($uupQry);
  
    
     $uupQry="UPDATE  tbl_generate_invoice_mst SET 	pending_amount='$new_pending_amount1',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
		}
		else
		{
$uupQry="UPDATE  tbl_customer_payment SET invoice_id='$invoice_id',invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',cheque_cash_amount='$cheque_cash_amount1',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";
  $uuresult=mysql_query($uupQry);
  
    
     $uupQry="UPDATE  tbl_generate_invoice_mst SET 	adv_amount='$cheque_cash_amount1',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
 		}
 
		}
	  
	   else
	   {
		   
		if($cheque_cash_amount == '')
		{
		
		 $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoiceid'");
		 $urow11=mysql_fetch_array($ure11); 
			 $adv_amount1 = $urow11['adv_amount'];
			 $total_amount1 = $urow11['total_amount'];
			 $new_pending_amount1 = $total_amount1 - $adv_amount1; 
		
		$uupQry="UPDATE  tbl_customer_payment SET invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'"; 
  $uuresult=mysql_query($uupQry);
  
  $uupQry="UPDATE  tbl_generate_invoice_mst SET pending_amount='$new_pending_amount1',status = 'Running' WHERE invoice_id='$invoiceid'";  
   $uuresult=mysql_query($uupQry);
		}
		else
		{
 $uupQry="UPDATE  tbl_customer_payment SET invoice_no='$invoice_no',invoice_date='$invoice_date',payment_date='$payment_date',customername='$customername',amount='$amount',payment_through='$payment_through',payment_by='$payment_by',cheque_cash_amount='$cheque_cash_amount',bank_name='$bank_name',cheque_no='$cheque_no',cheque_date='$cheque_date',acc_no='$acc_no',total_amount='$total_amount' WHERE invoice_id='$invoiceid'";   $uuresult=mysql_query($uupQry);
 
  $uupQry="UPDATE  tbl_generate_invoice_mst SET adv_amount='$cheque_cash_amount',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$invoiceid'";
   $uuresult=mysql_query($uupQry);
   		}
   
	   }
	   
   ?>
  <script language="javascript">window.location.href="generate_extra_invoice_detail_add.php?invoiceid=<?php echo $invoiceid; ?>";</script>
  <?php
 	}
?>
  <tr>
    <td colspan="5" align="center"><input type="submit" name="generate_print" value="Confirm & Print Invoice" class="btn btn-success" id='contact_form_link'>
      <!-- <a href="#" id='contact_form_link'>Request for Quote</a> -->
    </td>
  </tr>
  <?php } else { ?>
  <?php 
	$ure= mysql_query("SELECT * FROM  tbl_generate_invoice_detail where invoice_id = '$lastinvoice_id'");	
	while($urow=mysql_fetch_array($ure)) {
		?>
  <tr>
    <td class="span1"><div class="btn-group">
        <?php /*?><a href="weight_add.php?reg=<?php echo $urow[0]; ?>" data-toggle="tooltip" title="Edit" class="btn btn-mini btn-success" style="margin-right:10px;"><i class="icon-pencil"></i></a><?php */?>
        <?php echo "<a href='#' data-toggle='tooltip' title='Delete' class='btn btn-mini btn-danger' onclick='del(this,$urow[0])'>" ?><i class="icon-remove"></i></a></div></td>
    <!-- <td><?php //echo $urow['productname']; ?></td>-->
    <td><?php echo $urow['productname']; ?></td>
    <td><?php echo $qty = $urow['qty']; ?></td>
    <td><?php echo $price = $urow['price']; ?></td>
    <td><div align="right">
        <?php 	echo $amount = $qty * $price; ?>
        .00</div></td>
    <!-- <td><?php //echo $urow['remark']; ?></td>-->
  </tr>
  <?php } ?>
  <tr>
    <td colspan="5"><br />
    </td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Total Amount:</strong></div></td>
    <td colspan="1"><div align="right"><strong>
        <?php /*?> <?php
                       		$query1223 = "SELECT SUM(total_amount) FROM tbl_sale_invoice_detail where salein_id='$salein_id'"; 
	  						   	  $result1223 = mysql_query($query1223) or die(mysql_error());
							      $row1223 = mysql_fetch_array($result1223);
								echo  $total_sum =  $row1223['SUM(total_amount)']; 
								 ?>.00/-<?php */?>
        <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$lastinvoice_id'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalamount = $sord111['total_amount']; 
						?>
        .00 </strong> </div></td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Adv. Amount:</strong></div></td>
    <td colspan="1"><strong>
      <div align="right">
        <?php /*?> <?php
                       		$query1223 = "SELECT SUM(total_amount) FROM tbl_sale_invoice_detail where salein_id='$salein_id'"; 
	  						   	  $result1223 = mysql_query($query1223) or die(mysql_error());
							      $row1223 = mysql_fetch_array($result1223);
								echo  $total_sum =  $row1223['SUM(total_amount)']; 
								 ?>.00/-<?php */?>
        <?php
					 	echo $adv_amount;   ?>
      </div></td>
  </tr>
  <tr>
    <td colspan="4" align="right"><div align="right"><strong>Final Amount:</strong></div></td>
    <td colspan="1"><div align="right"><strong>
        <?php 
							echo $totalfinalamount = $totalamount - $adv_amount;
						
						?>
        .00</strong>
        <?php /*?>  <?php
                      	 $ure111=mysql_query("select * from  tbl_generate_invoice_mst where invoice_id = '$lastinvoice_id'"); 
 						 $sord111=mysql_fetch_array($ure111);
 						 echo  $totalfinalamount = $sord111['total_amount']; 
						?>.00 </strong>			<?php */?>
      </div></td>
  </tr>
  <tr>
    <div align="right">
    <form id="form-validation"  method="post" name="form" class="form-horizontal form-box remove-margin">
      <?php 
					$ure=mysql_query("select * from   tbl_customer_payment order by ABS(receipt_no) desc");
					$srow = mysql_fetch_array($ure);
					 $code = $srow['receipt_no'] + 1; 
					//$dr_dep_id = $srow['dr_dep_id'];
					
   				 ?>
      <input name="receipt_no" type="hidden" value="<?php echo $code; ?>"  />
      <td colspan="2">Adv Payment Mode:</td>
      <td colspan="9"> Cash
        <input type="radio" id="val_username" name="payment_through" value="cash" checked="checked" onClick="displayForm(this)">
        Cheque
        <input type="radio" id="val_username" name="payment_through" value="cheque"  onClick="displayForm(this)">
        <div style="visibility:visible;position:relative;" id="paypalformContainer">
          <label class="control-label" for="val_username">Cash Amount&nbsp;&nbsp;&nbsp;</label>
          <dd>
            <input type="text" id="paypal" name="adv_amount" class="form-control" >
          </dd>
        </div>
        <div style="visibility:hidden; position:relative" id="ccformContainer">
          <dd> Cheque Amount :&nbsp;&nbsp;&nbsp;
            <input type="text" name="adv_amount1" class="form-control">
            &nbsp;&nbsp;&nbsp;  Bank Name: &nbsp;&nbsp;&nbsp;
            <input type="text" name="bank_name" class="form-control">
            <br>
            Account Number : &nbsp;
            <input type="text" name="acc_no" class="form-control">
            &nbsp;&nbsp;&nbsp;Cheque No. : &nbsp;&nbsp;&nbsp;
            <input type="text" name="cheque_no" class="form-control">
            <br>
            Cheque Date :  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input name="cheque_date"  class="form-control"   type="text" value="<?php echo $urow['cheque_date']; ?>"  tabindex="1" placeholder='dd-mm-yyyy' />
          </dd>
        </div>
        <input type="submit" name="advance_payment" value="Add Adv.Payment" class="btn btn-success">
      </td>
      <!--<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Reset</button>-->
      </div>
    </form>
  </div>
  
  </tr>
  
  <?php
  if(isset($_REQUEST['advance_payment']))
   {
   			
			$receipt_no=$_REQUEST['receipt_no'];
   			$payment_through=$_REQUEST['payment_through'];
 		    $cheque_cash_amount=$_REQUEST['adv_amount']; 
			$cheque_cash_amount1=$_REQUEST['adv_amount1']; 
 		    $bank_name = $_REQUEST['bank_name'];
			$cheque_no = $_REQUEST['cheque_no'];
			$date1=$_REQUEST['cheque_date'];
			$cheque_date=date("Y-m-d", strtotime($date1));  
			$acc_no = $_REQUEST['acc_no'];
			
		
	    $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$lastinvoice_id'");
		$urow11=mysql_fetch_array($ure11); 
		
		$invoice_no	 = $urow11['invoice_no'];  
		$customername	 = $urow11['customername'];  
    	$invoice_date	 = $urow11['invoice_date'];  
		$total_amount	 = $urow11['total_amount']; 
		
		$payment_date = $invoice_date;
		 
		
		if($payment_through == 'cheque'){
		$new_adv_amount =  $cheque_cash_amount1;
		}else{
		$new_adv_amount =  $cheque_cash_amount;
		}
	    $new_pending_amount =  $total_amount - $new_adv_amount;
			
	
 if($payment_through == 'cheque')	
		{
		
		if($cheque_cash_amount1 == '')
		{
		}
		
		else {
 $sql=mysql_query("INSERT 
		INTO 
		 tbl_customer_payment	(invoice_id,invoice_no,invoice_date,receipt_no,payment_date,customername,amount,payment_through,payment_by,cheque_cash_amount,bank_name,cheque_no,cheque_date,acc_no,total_amount) 
		VALUES       ('$lastinvoice_id','$invoice_no','$invoice_date','$receipt_no','$payment_date','$customername','$amount','$payment_through','$payment_by','$cheque_cash_amount1','$bank_name','$cheque_no','$cheque_date','$acc_no','$total_amount')");
	    $paymentid =  mysql_insert_id();
		}
   
     $uupQry="UPDATE  tbl_generate_invoice_mst SET 	adv_amount='$cheque_cash_amount1',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$lastinvoice_id'";
   $uuresult=mysql_query($uupQry);
		}
	  
	   else
	   {
		   
		if($cheque_cash_amount == '')
		{
		}
		
		else {
$sql=mysql_query("INSERT 
		INTO tbl_customer_payment(invoice_id,invoice_no,invoice_date,receipt_no,payment_date,customername,amount,payment_through,payment_by,cheque_cash_amount,bank_name,cheque_no,cheque_date,acc_no,total_amount) 
		VALUES  ('$lastinvoice_id','$invoice_no','$invoice_date','$receipt_no','$payment_date','$customername','$amount','$payment_through','$payment_by','$cheque_cash_amount','$bank_name','$cheque_no','$cheque_date','$acc_no','$total_amount')");
	   $paymentid =  mysql_insert_id();
   		}
    $uupQry="UPDATE  tbl_generate_invoice_mst SET adv_amount='$cheque_cash_amount',pending_amount='$new_pending_amount',status = 'Running' WHERE invoice_id='$lastinvoice_id'";
   $uuresult=mysql_query($uupQry);
	   }
	   
	  
	   
  
   ?>
  <script language="javascript">window.location.href="generate_extra_invoice_detail_add.php";</script>
  <?php
 	}
?>
  <form id="send_testimonial" name="send_testimonial" class="form-horizontal form-box" method="post" >
    <tr>
      <td colspan="5" align="center"><input type="submit" name="generate_print" value="Confirm & Print Invoice" class="btn btn-success" >
        <!-- <a href="#" id='contact_form_link'>Request for Quote</a> -->
      </td>
    </tr>
    <?php }?>
    </tbody>
    
  </form>
</table>
<script language="javascript">
function del(obj,id)
{
        if(confirm("Are You Sure To Delete ?"))
        {
            obj.href="generate_extra_invoice_detail_add.php?id="+id;
        }
    
}
</script>
<?php
if(isset($_GET['id']))
	{
	
		if($invoice_dtid){
		$delquery="select * from tbl_generate_invoice_detail where quotation_in_id=".$_GET['id'];
		$delquery11=mysql_query($delquery);
		$delquery11=mysql_fetch_array($delquery111);
		$del_amount = $delquery11['total_amount'];
		$quotation_in_id = $delquery11['quotation_in_id'];
		$invoice_id = $delquery11['invoice_id'];

		$delqueryold="select * from tbl_generate_invoice_mst where invoice_id=$invoiceid";
		$delqueryold11=mysql_query($delqueryold);
		$old_amount = $delqueryold11['total_amount'];		
		$new_fnal_amount = $del_amount - $old_amount;
		
		$dusr="delete from   tbl_generate_invoice_detail where quotation_in_id=$_GET[id]";
		$dre=mysql_query($dusr);
		
		

	$qry111 = "update tbl_generate_invoice_mst set total_amount='$new_fnal_amount' where invoice_id=$invoiceid";
	mysql_query($qry111) or die(mysql_error());
	
		
		?>
<script language="javascript">window.location.href="generate_extra_invoice_detail_add.php?invoice_dtid=<?php echo $quotation_in_id; ?>&&invoiceid=<?php echo $invoice_id; ?>"</script>
<?php	
		} else If ($invoiceid){
		
		 $delquery="select * from tbl_generate_invoice_detail where quotation_in_id=".$_GET['id'];
		$delquery11=mysql_query($delquery);
		$delquery11=mysql_fetch_array($delquery111);
		$del_amount = $delquery11['total_amount'];
		$quotation_in_id = $delquery11['quotation_in_id'];
		$invoice_id = $delquery11['invoice_id'];

		$delqueryold="select * from tbl_generate_invoice_mst where invoice_id=$invoiceid";
		$delqueryold11=mysql_query($delqueryold);
		$old_amount = $delqueryold11['total_amount'];		
		$new_fnal_amount = $del_amount - $old_amount;
		
		$dusr="delete from   tbl_generate_invoice_detail where quotation_in_id=$_GET[id]";
		$dre=mysql_query($dusr);
		
		

	$qry111 = "update tbl_generate_invoice_mst set total_amount='$new_fnal_amount' where invoice_id=$invoiceid";
	mysql_query($qry111) or die(mysql_error());
	
		
		?>
<script language="javascript">window.location.href="generate_extra_invoice_detail_add.php?invoiceid=<?php echo $invoice_id; ?>"</script>
<?php	
		  } else {
		  
		$delquery="select * from tbl_generate_invoice_detail where quotation_in_id=".$_GET['id'];
		$delquery11=mysql_query($delquery);
		$del_amount = $delquery11['total_amount'];

		$delqueryold="select * from tbl_generate_invoice_mst where invoice_id=$lastinvoice_id";
		$delqueryold11=mysql_query($delqueryold);
		$old_amount = $delqueryold11['total_amount'];		
		$new_fnal_amount = $del_amount - $old_amount;
		
		$dusr="delete from   tbl_generate_invoice_detail where quotation_in_id=$_GET[id]";
		$dre=mysql_query($dusr);
		
		

	$qry111 = "update tbl_generate_invoice_mst set total_amount='$new_fnal_amount' where invoice_id=$lastinvoice_id";
	mysql_query($qry111) or die(mysql_error());
	
		
		?>
<script language="javascript">window.location.href="generate_extra_invoice_detail_add.php"</script>
<?php	
		}
	}
	?>
</div>
<?php include 'footer.php' ?>
</div>
</div>
<?php //include 'profile_popup.php' ?>

<!--[if lte IE 8]><script src="js/helpers/excanvas.min.js"></script><![endif]-->
<!--<script src="js/jquery.min.js"></script>-->
<script>!window.jQuery && document.write(unescape('%3Cscript src="js/vendor/jquery-1.9.1.min.js"%3E%3C/script%3E'));</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<script>$(function(){$("#form-validation").validate({errorClass:"help-block text-error",errorElement:"span",errorPlacement:function(t,e){e.parents(".controls").append(t)},highlight:function(t){$(t).closest(".control-group").removeClass("success error").addClass("error")},success:function(t){t.addClass("valid").closest(".control-group").removeClass("success error").addClass("success"),t.closest(".help-inline").remove(),t.closest(".help-block").remove()},rules:{val_username:{required:!0,minlength:2},val_password:{required:!0,minlength:5},val_confirm_password:{required:!0,minlength:5,equalTo:"#val_password"},val_email:{required:!0,email:!0},val_website:{required:!0,url:!0},val_date:{required:!0,date:!0},val_range:{required:!0,range:[1,100]},val_number:{required:!0,number:!0},val_digits:{required:!0,digits:!0},val_skill:{required:!0},val_credit_card:{required:!0,creditcard:!0},val_terms:{required:!0}},messages:{val_username:{required:"Please enter a username",minlength:"Your username must consist of at least 2 characters"},val_password:{required:"Please provide a password",minlength:"Your password must be at least 5 characters long"},val_confirm_password:{required:"Please provide a password",minlength:"Your password must be at least 5 characters long",equalTo:"Please enter the same password as above"},val_email:"Please enter a valid email address",val_website:"Please enter your website!",val_date:"Please select a date!",val_range:"Please enter a number between 1 and 100!",val_number:"Please enter a number!",val_digits:"Please enter digits!",val_credit_card:"Please enter a valid credit card!",val_skill:"Please select a skill!",val_terms:"You must agree to the terms!"}})});</script>
<script language="javascript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
    }
	
		function getBoard(countryId) {		
		var strURL="find_vehicle_name.php?v_id="+countryId;
		var req = getXMLHTTP();
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('v_id').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}
	
	function getState(countryId,stateId) {		
		
		var strURL="findSubject.php?streamid="+countryId+"&subject_id="+stateId;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('subject_id').innerHTML=req.responseText;
												
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}
	
</script>
</body>
</html>