<?php    include_once('../config/db.php');  
         include_once('../config/check.php'); 
         include_once('../functions/sale/sale.php');
       

 if (filter_has_var(INPUT_POST, "submit")) 
    try{  
             
             $transtype =         11 ; 
             $transaction =       1 ; 
             $transmode =        htmlspecialchars ( strtoupper($_POST['tmode']));
                         
             $date    =          htmlspecialchars ( strtoupper($_POST['date'])); 
             $date    =          date("Y-m-d", strtotime($date));  
             $partner =          htmlspecialchars ( strtoupper($_POST['partner']));

            ///$cr=   68;       //            htmlspecialchars ( strtoupper($_POST['from']));     //  Must define first AP    
            $to=                htmlspecialchars ( strtoupper($_POST['to']));
            $docid=        htmlspecialchars ( strtoupper($_POST['myinvoice']));
            $paymode =          htmlspecialchars ( strtoupper($_POST['paymode']));
            $amount =           htmlspecialchars ( strtoupper($_POST['amountpaid']));
            $memo =             htmlspecialchars ( strtoupper($_POST['memo']));
            $desc =                 htmlspecialchars ( strtoupper($_POST['description']));

            
            $currency =       currency($conn,$partner); 
            $erate =       erate($conn,$partner); 
            $total =  ( $erate * $amount );
         
            $sql1 = 'INSERT INTO acctransactions(date,enddate,memo,memo2,notes,transtype,transaction,transmode,transuid,storeID) VALUES (:date,:enddate,:memo,:memo2,:notes,:transtype,:transaction,:transmode,:transuid,:storeID)';     
            $stmt = $conn->prepare($sql1)->execute(['date'=>$date,'enddate'=>$date,'memo'=>$memo,'memo2'=>'','notes'=>$description,'transtype' =>$transtype,'transaction'=>$transaction,'transmode' =>$transmode,'transuid'=>$uid, 'storeID'=>$storeID  ]); 
             $lastid = $conn->lastInsertId(); 
        

            
             $sql = 'INSERT INTO acctransactionsdetails (transid,docid,branch,partner,currency,erate,coa,itemtotal,total,transmove,transclass ) 
                                            VALUES (:transid,:docid,:branch,:partner,:currency,:erate,:coa,:itemtotal,:total,:tmove,:tclass )';

            // subtotal,vat,total

             $conn->prepare($sql)->execute(['transid'=>$lastid,'docid'=>$docid ,'branch'=>0,'partner'=>$partner,'currency'=>$currency,'erate'=>$erate,'coa'=>$to,'itemtotal'=>$amount,'total'=>$total ,'tmove' =>1,'tclass'=>1  ]);
             $conn->prepare($sql)->execute(['transid'=>$lastid,'docid'=>$docid , 'branch'=>0,'partner'=>$partner, 'currency'=>$currency,'erate'=>$erate,'coa'=>2,'itemtotal'=>$amount,'total'=>$total , 'tmove' =>5,'tclass'=>2  ]);


             audittrail($conn,$lastid,$transtype,$transaction,$transmode,$uid,$storeID);
                
             header("location:payment.php"); 
        } catch(Exception $e) { echo 'Message: ' .$e->getMessage();}
       
       



?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('../inc/slink.php');   ?>  
</head>
<body <?php include_once('../inc/snav.php');  ?> 
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('../inc/sbrand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('../inc/snavbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('../inc/smenu.php'); ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">


    <section class="content-header">
        <p class="listheader" > <i class="fa fa-plus-circle" ></i> Receive Payment   </p>
    
        <!-- <h1>   <i class="fa fa-user-plus"></i> Make Purchases Payment    </h1> -->
        <ol class="breadcrumb">
        
            </ol>
    </section>


  <!-- Main content -->
  <section class="content container-fluid" style="padding-top:0px;">
    
      <div class="box box-default box-xs">
              <div class="box-body">
                  <div class="row">
                    <div class="col-md-12">
                  <form class="form-horizontal "  action="" method="post" >
                        <div class="row">
                                               
                            <div class="">   
                                    <div class="col-md-2">
                                        <label > Date </label>
                                            <div class="input-group ">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                                <input type="text" class="form-control datepicker " autocomplete="off" name="date">
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        <label> Customer  </label>
                                        <select class="form-control  js-example-basic-single "  required="" name="partner" id="customer" style="" >
                                        <?php $stmt = $conn->prepare("SELECT * FROM (
                                                                       SELECT a.*,b.code bcode  FROM bppartner a
                                                                      LEFT JOIN acccurrency b ON a.currency=b.id 
                                                                      WHERE a.type=1 )d WHERE d.branch='$storeID' or d.branch=0  ");
                                            $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option> </option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name'].'  '. $row['bcode'] ; ?></option> <?php } ?>
                                        </select>                                   
                                    </div>
                                    
                        
                                    <div class="col-md-3">
                                        <label>  Reference/Invoice NO   </label> 
                                        <select class="form-control  js-example-basic-single form-group "  required="" name="myinvoice"  id="myinvoice"  ></select>              
                                    </div>

                                    <div class="col-md-4">
                                        <label>  Invoice Amount </label>
                                        <input type="text" class="form-control" name="myamount" required='' autocomplete="off" readonly="true" >
                                    </div> 
                                    </div>
                                
                                    <div class="">                                                                  
                                    <!-- DESCRIPTIONS --->
                                    <div class="col-md-2">
                                        <label>  Payment Mode   </label>                                       
                                        <select class="form-control js-example-basic-single  "  required="" name="paymode"  >
                                        <?php $stmt = $conn->prepare("SELECT * FROM  admpaymode   ");
                                            $stmt->execute(); foreach ($stmt as $row) {?>
                                                <option> </option>
                                                <option value="<?php echo $row['id']; ?>"><?php
                                                    echo $row['name']; ?></option> <?php } ?>
                                        </select>      
                                    </div>
                                    <!-- INVOICE NUM  --->
                                    <div class="col-md-3">
                                        <label>   Amount Paid   </label>
                                        <input type="text" class="form-control" name="amountpaid" required='' autocomplete="off">
                                    </div>
                                    <div class="col-md-3">
                                        <label>  Invoice Balance   </label>
                                       
                                        <input type="text" class="form-control" name="amountbalance" required='' readonly="true" autocomplete="off">
                                    </div>
                                    
                                    <div class="col-md-4">
                                            <label>   Transaction Ref  </label>
                                            <input type="text" name="memo" class="form-control" autocomplete="off">

                                    </div>
                                    <div class="col-md-2">
                                        <label>  Payment Status   </label>
                                            <select class="form-control js-example-basic-single  "  required="" name="tmode" >
                                            <?php $stmt = $conn->prepare("SELECT * FROM adtransactionmode where type=2     ");
                                                $stmt->execute(); foreach ($stmt as $row) {?>
                                                    <option> </option>
                                                    <option value="<?php echo $row['id']; ?>"><?php
                                                        echo $row['name']; ?></option> <?php } ?>
                                        </select>                                   
                                    </div> 
                                    <div class="col-md-3">
                                        <label>  Send received  payment to   </label>
                                            <select class="form-control js-example-basic-single  "  required="" name="to"  >
                                            <?php $stmt = $conn->prepare("SELECT * FROM coa where payaccount=1     ");
                                                $stmt->execute(); foreach ($stmt as $row) {?>
                                                    <option> </option>
                                                    <option value="<?php echo $row['id']; ?>"><?php
                                                        echo $row['name']; ?></option> <?php } ?>
                                        </select>                                   
                                    </div> 
                                    <!-- <div class="col-md-3">
                                            <label>   payments status  </label>
                                            <input type="text" name="memo" class="form-control" autocomplete="off">

                                    </div> -->
                                    <div class="col-md-7">
                                            <label>   Description  </label>
                                            <input type="text" name="description" class="form-control" autocomplete="off">
                                          
                                            
                                    </div>
                                    </div>                                                                                                                                               
                                        <!-- submit button -->
                                        <div class="form-group " >
                                        <div class="col-sm-offset-4 col-sm-10" style="padding-top:20px;">                             
                                            <button type="submit"name="submit" class="btn btn-default col-md-4 subbtn ">
                                                <i class="fa fa-plus-circle "></i>  Receive payments   </button>
                                                </div>
                                </div>     
                            
                            </div>
                                              
                    </form>
                </div>
                <!-- <div class="col-md-2">
                 
                    <label> Currency  </label>
                     <input type="text" name="memo" class="form-control"  autocomplete="off">
                   
                   <label> Total Invoice  </label>
                     <input type="text" name="memo" class="form-control" autocomplete="off">
                    <label>  Total Paid : </label> 
                     <input type="text" name="memo" class="form-control" autocomplete="off">
                    <label> Total Balance  </label> 
                  <input type="text" name="memo" class="form-control" autocomplete="off">
                </div> -->

                   
                        
                    </div>
                </div>
            </div>
   </section>
</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('../inc/sfooter.php'); ?>
  </footer>
  
</div>
<?php include_once('../inc/slinkjs.php') ?>


<script>
// CHOOSE INVOICE  FOR PAYMENTS
$(document).ready(function() {                    
                      $("select[name='partner']") .change(function(){        
                        var partner = $("select[name='partner']").val(); 
                       // alert (partner) ;                     
                          $.ajax({    type: "POST",
                                      url: "../functions/ajax/sale/customerinv.php",
                                      data: {partner:partner},
                                      cache: false,
                                      success: function(html) {                                                                 
                                                                $('select[name="myinvoice"]' ).html(html);
                                                                    $('select[name="myinvoice"]') .change(function(){                   
                                                                          var invoice = $('select[name="myinvoice"]').val();
                                                                          // alert(invoice);
                                                                                  $.ajax({      
                                                                                           type: "POST",
                                                                                           url: "../functions/ajax/sale/customerinv.php",
                                                                                           data:{invoice:invoice},
                                                                                           cache: false,
                                                                                           success: function(data)
                                                                                              {         
                                                                                                var obj=$.parseJSON(data);
                                                                                                $("input[name='myamount']").val(obj.tt);
                                                                                             }
                                                                                        })
                                                                        });
                                                              }                 
                                 })
                          });
                 });


                    // Calculating balance 
                    $("input[name='amountpaid']") .change(function(){ 
                    var ap = $("input[name='amountpaid']").val(); 
                    var amount = $("input[name='myamount']").val(); 
                    var bal = amount - ap ;
                    $("input[name='amountbalance']").val(bal);
                   

                });
</script>


</body>
</html>

