<?php
$query="SELECT salesman_code,salesman_name FROM ".$prefix."salesman ORDER BY salesman_code ASC";
$salesType=mysqli_query($linkid,$query);
?>
<script type="text/javascript">
function newAmount(){
var mamount=$("#amount").val();
var sap=$("#sap").val();
//alert(discount);
var totalamount=parseInt(mamount) - parseInt(sap);
//alert(totalamount);
$("#amount").val(totalamount);
}

function selectMode(){
var bank=$("#mode").val();
//alert(bank);
if(bank==2 || bank==3)
$("#cardDisplay").show();
else
$("#cardDisplay").hide();
}
</script>

<div id="contentwrapper">
  <div class="main_content">
    <nav>
      <div id="jCrumbs" class="breadCrumb module">
        <ul>
          <li> <a href="#"><i class="icon-home"></i></a> </li>
          <li> <a href="#">Customer Payment Entry</a> </li>
        </ul>
      </div>
    </nav>
    <div class="row-fluid">
      <div class="span12">
        <h3 class="heading"><?php echo ucwords($getbilling['name']);?> |&nbsp;Address: <?php echo $getbilling['address'];?>&nbsp; Phone: <?php echo $getbilling['contact_phone'];?></h3>
      </div>
      <div class="span10">
        <?php
		          	if(isset($_POST['submit']))
					{
						$order=$_POST['orderno'];
						$debtor=$_POST['debtor'];
						$mydate=$_POST['mydate'];
						$reference=$_POST['reference'];
						$bank_account=$_POST['bank_account'];
						$discount=$_POST['discount'];
						$sap=$_POST['sap'];
						$amount=$_POST['amount'];
						$cash=$_POST['cash'];
						$cheque=$_POST['cheque'];
						$chequenumber=$_POST['chequenumber'];
						$card=$_POST['card'];
						$cardnumber=$_POST['cardnumber'];
						
						$paid=$cash+$cheque+$card;
												
						$notes=$_POST['notes'];	
						$person=$_POST['person'];

                                                $date1 = strtotime($mydate);
						$date2 = time();
//echo $date2."Hello".$date2;

						$subTime =$date2 - $date1;
						$dayval = ($subTime/(60*60*24))%365;
											
																			
if($dayval==0){								
//update Money receipt
						$query="SELECT id,money_receipt_no FROM ".$prefix2."order_payment ORDER BY id DESC limit 0,1";
						$vourcherType=mysqli_query($linkid,$query);
						$vrow=mysqli_fetch_row($vourcherType);
						
						if($vrow[1]=="")
						$mrno=1;
						else
						$mrno=$vrow[1]+1;
						
						$query = "INSERT INTO ".$prefix2."order_payment values(null,'$order','$mrno','$sap','$discount','$paid','$debtor','$mydate','$cash','$cheque','$chequenumber','$card','$cardnumber','$name','$person','$notes')";
						$resultLast=mysqli_query($linkid,$query) or die(mysqli_error());
						
						
						if($result){
						?>
        <div class="alert alert-success"> <a data-dismiss="alert" class="close">x</a> <strong>Payment </strong> is saved. </div>
		
		 <script type="text/javascript" language="javascript">

							  setTimeout(function () {

								 window.location.href= 'money-receipt-payment.php?mid=<?php echo $mrno;?>'; // the redirect goes here

								},3000);

							  </script>
        <?php 
						
						 }
}
						   else if($dayval > 0)	{
							echo ' <div class="alert alert-error"> <a data-dismiss="alert" class="close">x</a>Previous Date Entry is </strong> not permitted.</div>';
							}
						}
							
					 //unlink($filename1);
					 
						 unset($_POST);
						 $_POST['submit']='';
						 $_POST='';
					?>
        <h3 class="heading">Customer Payment Collection<small> &nbsp;Amount and discount are in customer's currency. </small></h3>
        <form class="form_validation_reg" id="form_validation_reg" action=""  method="post">
          <input type="hidden" value="<?php echo $getbilling['debtor_no'];?>" class="span12" name="debtor" id="debtor">
          <input type="hidden" value="<?php echo $pagenew;?>" class="span12" name="orderno" id="orderno">
          <div class="row-fluid">
            <div class="span3">
              <label>Date of Deposit<span class="f_req">*</span></label>
              <input type="text" name="mydate" id="mydate" class="span12"  value="<?php echo date('Y-m-d');?>"/>
            </div>
            <div class="span3">
              <label>Reference<span class="f_req">*</span></label>
              <input type="text" value="CP-<?php echo $countRef;?>" class="span12" name="reference">
            </div>
          </div>
          <div class="row-fluid">
		    <div class="span3">
               <label>Advance (Prev. Balance)</label>
              <input type="text" name="sap" id="sap" class="span12" value="<?php echo $advanceNew;?>"/>
            </div>
            <div class="span3">
                <label>Sales Person</label>
                <SELECT class="input_text" name="person" id="person">
                  <OPTION SELECTED VALUE="">Please select</OPTION>
                  <?php
												if($salesType)
												{
												   while($mydata=mysqli_fetch_row($salesType))
													{
													 echo "<option value='$mydata[0]'>$mydata[1]</option>";
													}
												}	
													?>
                </SELECT>
              </div>
          
          </div>
           <div class="row-fluid">
              <div class="span3">
                <label>Net Payable<span class="f_req">*</span></label>
                <input type="text" name="amount" id="amount" class="span12"  value="<?php echo $pendingBalance-$advanceNew;?>" />
              </div>
              
            </div>
            
			<div class="row-fluid">
              <div class="span3">
                <label>Mode of Payment:<span class="f_req">*</span></label>
                <input type="text" name="cash" id="cash" class="span12"  placeholder="Cash" />
              </div>           
            </div>
			<div class="row-fluid">
              <div class="span3">
                <input type="text" name="cheque" id="cheque" class="span12"  placeholder="Cheque" />
              </div>
              <div class="span3">
                <input type="text" name="chequenumber" id="chequenumber" class="span12" placeholder="Number"  />
              </div>
            </div>
			<div class="row-fluid">
              <div class="span3">
                <input type="text" name="card" id="card" class="span12" placeholder="Card" />
              </div>
              <div class="span3">
                <input type="text" name="cardnumber" id="cardnumber" class="span12" placeholder="Number"  />
              </div>
            </div>
            <div class="row-fluid">
              <div class="span10">
                <label>Notes</label>
                <textarea type="text" name="notes" id="notes" class="span10"  value=""></textarea>
              </div>
            </div>
          <div class="form-actions">
            <input class="btn btn-inverse" name="submit" type="submit" value="Process Payment" />
            <button class="btn">Cancel</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
