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


  $invoice_id = $_GET['reg'];
 
  
 $_SESSION['id'] = $id;
 $paymentid = $_SESSION['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>Invoice Payment</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;
      }
	  
	  
	   function onlyAlphabets(e, t) {
            try {
                if (window.event) {
                    var charCode = window.event.keyCode;
                }
                else if (e) {
                    var charCode = e.which;
                }
                else { return true; }
                if ((charCode > 64 && charCode < 91 ) || (charCode > 96 && charCode < 123) || (charCode = ''))
                    return true;
                else
                    return false;
            }
            catch (err) {
                alert(err.Description);
            }
        }
	  
	  
</script>
<script type="text/javascript"> 
    function displayForm(c) {
        if (c.value == "cheque") {

            document.getElementById("ccformContainer").style.visibility = 'visible';
            document.getElementById("paypalformContainer").style.visibility = 'hidden';
        } else if (c.value == "cash") {
            document.getElementById("ccformContainer").style.visibility = 'hidden';

            document.getElementById("paypalformContainer").style.visibility = 'visible';
        } else {}
    }

    </script>
</head>
<body>
<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 List</a></li>
      </ul>
      <?php 
	  
		if ($_POST['add']) 
			{
	
 	        $payment_by = $_REQUEST['payment_by'];
			$payment_through = $_REQUEST['payment_through'];
		
			 $receipt_no=$_REQUEST['receipt_no']; 
			$total_amount=$_REQUEST['total_amount'];
			$cheque_cash_amount=$_REQUEST['cheque_cash_amount'];
			$cheque_cash_amount1=$_REQUEST['cheque_cash_amount1']; 
 		    $bank_name = $_REQUEST['bank_name'];
			$cheque_no = $_REQUEST['cheque_no'];
			
			$date1=$_REQUEST['cheque_date'];
			$cheque_date=date("Y-m-d", strtotime($date1));  
			
			$date11=$_REQUEST['payment_date'];
			$payment_date=date("Y-m-d", strtotime($date11));  
		//	$payment_date=$_REQUEST['payment_date'];
			$acc_no = $_REQUEST['acc_no'];
	
	    $ure11=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoice_id'");
		$urow11=mysql_fetch_array($ure11); 
		
		$invoice_no	 = $urow11['invoice_no'];  
		$customername	 = $urow11['customername'];  
    	$invoice_date	 = $urow11['invoice_date'];  
		 $total_amount	 = $urow11['total_amount'];  
		$old_adv_amount	 = $urow11['adv_amount']; 
		$old_pending_amount = $urow11['pending_amount']; 
		$cur_payment = $cheque_cash_amount; 
	
		$new_adv_amount = $old_adv_amount + $cur_payment; 
	    $new_pending_amount =  $total_amount - $new_adv_amount;

	if($new_adv_amount > $total_amount )
	{
		echo "<script type='text/javascript'>alert('The Amount Is More Than The Total Amount')</script>";
		
	}
	else{
 
	if($payment_through == 'cheque')	
		{
	 $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       ('$invoice_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();
		if($new_pending_amount == '0'){
		$qry1 = "update  tbl_generate_invoice_mst set adv_amount='$new_adv_amount',pending_amount='$new_pending_amount',status='Confirm' where invoice_id='$invoice_id'";
		mysql_query($qry1) or die(mysql_error());
		} else {
		$qry1 = "update  tbl_generate_invoice_mst set adv_amount='$new_adv_amount',pending_amount='$new_pending_amount',status='Running' where invoice_id='$invoice_id'";
		mysql_query($qry1) or die(mysql_error());
		}
	   }
	   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  ('$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')");
	   $paymentid =  mysql_insert_id();
	   if($new_pending_amount == '0'){
	   $qry2 = "update tbl_generate_invoice_mst set adv_amount='$new_adv_amount',pending_amount='$new_pending_amount' ,status='Confirm' where invoice_id='$invoice_id'";
		mysql_query($qry2) or die(mysql_error());
		} else {
		 $qry2 = "update tbl_generate_invoice_mst set adv_amount='$new_adv_amount',pending_amount='$new_pending_amount',status='Running' where invoice_id='$invoice_id'";
		mysql_query($qry2) or die(mysql_error());
		}
	   }
	   
	   

	   
 	?>
      <script language="javascript">window.location.href="generate_invoice_list.php";</script>
      <?php
//	}
}
}







 
?>
      <button id="addRow" class="btn btn-success" onClick="location.href = 'generate_invoice_list.php';">&nbsp;View Generate Invoice List</button>
      <?php 

	   						
		$ure1=mysql_query("select * from tbl_generate_invoice_mst where invoice_id='$invoice_id'");
		$urow11111=mysql_fetch_array($ure1); 
		$invoice_no1 = $urow11111['invoice_no'];
		$invoice_date1 = $urow11111['invoice_date'];
		$customername1 = $urow11111['customername'];
		$pending_amount1 = $urow11111['pending_amount'];
		
		
	//	$_SESSION['invoice_no'] = $invoice_no;
	//	$invoice_no1 = $_SESSION['invoice_no'];
		
	//	$_SESSION['invoice_date'] = $invoice_date;
	//	$invoice_date1 = $_SESSION['invoice_date'];
		
	//	$_SESSION['customername'] = $customername;
	//	$customername1 = $_SESSION['customername'];
		
		?>
      <form id="form-validation"  method="post" name="frm11" class="form-horizontal form-box remove-margin">
      
        <div class="form-box-content">
        
          <?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; ?>"  />
          <div class="control-group">
            <label class="control-label" for="val_username">Invoice No. : &nbsp;&nbsp;</label>
            <div class="controls">
              <div class="input-prepend">
                <input type="text"  id="val_username" name="invoice_no" value="<?php echo $invoice_no1; ?>" readonly>
              </div>
              <div class="input-prepend">
                <label class="control-label" for="val_username">Invoice Date : &nbsp;&nbsp;</label>
                <input type="text"  id="val_username" name="invoice_date" value="<?php echo $invoice_date1; ?>" readonly>
              </div>
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="val_username">Customer Name : &nbsp;&nbsp;</label>
            <div class="controls">
              <div class="input-prepend">
                <input type="text"  id="val_username" name="customername" value="<?php echo $customername1; ?>" readonly>
              </div>
              <div class="input-prepend">
                <label class="control-label" for="val_username">Pending Amount : &nbsp;&nbsp;</label>
                <input type="text"  id="val_username"  value="<?php echo $pending_amount1; ?>" readonly>
              </div>
            </div>
          </div>
          <div class="control-group">
            <div class="input-prepend">
              <label class="control-label" for="val_username">Payment Date : &nbsp;&nbsp;</label>
              <input type="text" id="val_password" required class="w8em format-d-m-y" name="payment_date" >
            </div>
            <div class="input-prepend">
              <label class="control-label" for="val_username">Payment By : &nbsp;&nbsp;</label>
              <input type="text"  id="val_username" name="payment_by" value="<?php echo $urow['payment_by']; ?>" required onKeyPress="return onlyAlphabets(event,this);">
            </div>
          </div>
          <div class="control-group">
            <label class="control-label" for="val_password">Payment Through :&nbsp;&nbsp;&nbsp;&nbsp;</label>
            <div class="form_input" style="padding-left:50px;">
              <input value="cash" type="radio" name="payment_through"  checked="checked" onClick="displayForm(this)">
              </input>
              &nbsp;By Cash &nbsp;
              <input value="cheque" type="radio" name="payment_through" onClick="displayForm(this)" >
              </input>
              &nbsp;By Cheque </div>
          </div>
          <div style="visibility:visible;position:relative;" id="paypalformContainer" >
            <div class="control-group">
              <label class="control-label" for="val_username"> Cash Amount : &nbsp;&nbsp;</label>
              <div class="controls">
                <div class="input-prepend">
                  <input type="text"  id="val_username" name="cheque_cash_amount" value="<?php echo $urow['cheque_cash_amount']; ?>" onKeyPress="return isNumberKey(event)">
                </div>
              </div>
            </div>
          </div>
          <div style="visibility:hidden; position:relative" id="ccformContainer" >
            <div class="control-group">
              <label class="control-label" for="val_username"> Cheque Amount : &nbsp;&nbsp;</label>
              <div class="controls">
                <div class="input-prepend">
                  <input type="text"  id="val_username" name="cheque_cash_amount1" value="<?php echo $urow['cheque_cash_amount1']; ?>" onKeyPress="return isNumberKey(event)">
                </div>
                <div class="input-prepend">
                  <label class="control-label" for="val_username">Bank Name : &nbsp;&nbsp;</label>
                  <input type="text"  id="val_username" name="bank_name" value="<?php echo $urow['bank_name']; ?>" >
                </div>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="val_username"> Cheque No : &nbsp;&nbsp;</label>
              <div class="controls">
                <div class="input-prepend">
                  <input type="text"  id="val_username1" name="cheque_no" >
                </div>
                <div class="input-prepend date input-datepicker" data-date="04-30-2013" data-date-format="mm-dd-yyyy">
                  <label class="control-label" for="val_Cheque_Date">Cheque Date : &nbsp;&nbsp;</label>
                  <input type="text" id="val_Cheque_Date" class="w8em format-d-m-y" name="cheque_date" >
                </div>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="val_username">Account Number : &nbsp;&nbsp;</label>
              <div class="controls">
                <div class="input-prepend">
                  <input type="text"  id="val_username" name="acc_no" value="<?php echo $urow['acc_no']; ?>">
                </div>
              </div>
            </div>
          </div>
          <div class="form-actions">
            <input type="submit" name="add" value="Add Payment Invoice" class="btn btn-success">
            <!--<button type="reset" class="btn btn-danger"><i class="icon-repeat"></i> Reset</button>-->
          </div>
        </div>
      </form>
    
      <script language="javascript">
function del(obj,id)
{
        if(confirm("Are You Sure To Delete ?"))
        {
            obj.href="generate_quotation_invoice_add.php?id="+id;
        }
    
}
</script>
      <?php
if(isset($_GET['id']))
	{
		 $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=$lastquotation_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=$lastquotation_id";
		mysql_query($qry111) or die(mysql_error());
			
		
	//	header('location:purchase_order_product_add.php');
		
		?>
      <script language="javascript">window.location.href="generate_quotation_invoice_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>
</body>
</html>