<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('26','creates','R');    
$_SESSION['cuPages']='acc_paycreate.php';   
$cuPage='acc_paycreate.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='account';
$menuh='Account';
$phead='paycre';
$page='Payment Create';
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
$dtnow = date("Y-m-d h:i:s", time());
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Add Payment</h3>
</div>
<div class="box-body">
<div class="col-md-12 addpayment">
<div class="row">    
<center>
<h3 class="page-title">PAYMENT VOUCHER</h3>
</center>
</div>
<div class="row">    
<div class="col-md-4 col-md-offset-8">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><b>Voucher No:</b></span>
<input type="text" class="form-control" maxlength="15" name="invno" id="invno" value="<?php if(isset($_SESSION['axes_paydata']['invno'])){$_SESSION['axes_paydata']['invno']=gen_newinvno('tbl_payvoucher','PAV');echo $_SESSION['axes_paydata']['invno'];}else{echo gen_newinvno('tbl_payvoucher','PAV');}?>" placeholder="e.g. PAV121119101" readonly>
</div>
</div>
<div class="form-group" >
<div class="input-group">
<span class="input-group-addon"><b>Date:</b></span>
<input type="text" class="form-control datetimepicker" name="apdate" id="apdate" value="<?php if(isset($_SESSION['axes_paydata']['date'])){echo $_SESSION['axes_paydata']['date'];}else{ echo date('Y-m-d');}?>" placeholder="Date:" autocomplete="off" readonly>
</div>
</div>    
</div>
</div>
<div class="row">    
<div class="col-md-4">    
<div class="form-group">
<label>Payment Sourch</label>    
<select class="form-control select2" name="cid" id="cid">
<option value="">-Select-</option>
<option value="LE_2">Cash</option>    
<?php
$sql="SELECT tbl_bacount.id,CONCAT(tbl_bank.sort,' - ',tbl_bacount.acno) AS name FROM tbl_bacount LEFT JOIN tbl_bank ON tbl_bank.id=tbl_bacount.bid ORDER BY tbl_bank.sort ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'BA_'.$rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php
$sql="SELECT id,CONCAT(name,' - ',mobile) AS cname,name FROM tbl_acmobile ORDER BY name ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'MO_'.$rows['id'];?>"><?php echo $rows['cname'];?></option>
<?php } ?>     
</select>
</div>    
</div>
<div class="col-md-2">    
<div class="form-group">
<label>Balance</label>
<div style="font-size: 20px;color: red;"><span>	৳ </span><span id="cbalance">0.00</span></div>    
</div>    
</div>    
<div class="col-md-2">    
<div class="form-group">
<label>Cheque No</label>
<input type="text" maxlength="20" class="form-control" name="chkno" id="chkno" value="" placeholder="e.g. KA7865467" autocomplete="off" disabled="disabled">
</div>    
</div>
<div class="col-md-2">    
<div class="form-group" >
<label>Cheque Date</label>
<input type="text" class="form-control datetimepicker" name="chkdt" id="chkdt" value="" placeholder="Cheque Date" autocomplete="off" disabled="disabled">
</div>    
</div>    
<div class="col-md-2">
    <div class="form-group">
        <label>SMS</label><br>
        <input type="checkbox" name="smsstatus" value="1">
    </div>

</div>    
</div>    
<div class="row">
<div class="col-md-5">
<div class="form-group" >
<label>Payment To</label>
<div class="input-group">
<select class="form-control select2" name="did" id="did">
<option value="">-Select-</option>
<optgroup label="Supplier">
<?php
$sql="SELECT * FROM tbl_supplier ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'SU_'.$rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'].' - '.$rows['cnumber'];?></option>
<?php } ?>
</optgroup>
<optgroup label="Customer">
<?php
$sql="SELECT * FROM tbl_customer ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'CU_'.$rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'].' - '.$rows['cnumber'];?></option>
<?php } ?>
</optgroup>
<optgroup label="Employee">
<?php
$sql="SELECT * FROM tbl_employe ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'EM_'.$rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>
<optgroup label="Loan">
<?php
$sql="SELECT * FROM tbl_loanid ORDER BY name ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'LO_'.$rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>     
<optgroup label="Other">    
<?php
$sql="SELECT * FROM tbl_acledger WHERE sgrid NOT IN (1,2,3,4,13) ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="<?php echo 'LE_'.$rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>    
</select>
<span class="input-group-addon"><a href=""><span class="fa fa-plus"></span></a></span>     
</div>   
</div>
</div>
<div class="col-md-2">    
<div class="form-group">
<label>Balance</label>
<div style="font-size: 20px;color: red;"><span>	৳ </span><span id="balance">0.00</span></div>    
</div>    
</div>    
<div class="col-md-2">
<div class="form-group" >
<label>Ref</label>
<input type="text" maxlength="25" class="form-control" name="ref" id="ref" placeholder="e.g. Sumon" autocomplete="off">    
</div>    
</div>
<div class="col-md-2">
<div class="form-group">
<label>Amount</label>    
<input type="text" maxlength="10" class="form-control" name="amo" id="amo"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div> 
</div>
<div class="col-md-1">
<div class="form-group">
<label>&nbsp;</label>    
<input type="button" id="addpay" class="btn btn-flat bg-red" value="Add"/>    
</div>
</div>    
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-striped">
<thead>    
<th style="width:40px; text-align:center">SN</th>
<th>Sourch</th>
<th>Pay To</th>    
<th>Amount</th>
<th>Cheque</th>
<th>Date</th>    
<th>Ref</th>
<th style="width:40px; text-align:center"><a class="empty" style="cursor: pointer;"><i class="fa fa-trash"></i></a></th>    
</thead>
<tbody id="itemdata">

</tbody>
<tfoot id="itemfoot">

</tfoot>    
</table> 
</div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Note</label>
<textarea class="form-control" name="note" id="note" maxlength="250" rows="3" placeholder="e.g. Note"><?php if(isset($_SESSION['axes_paydata']['note'])){echo $_SESSION['axes_paydata']['note'];}?></textarea>
<input type="hidden" name="save_payment" readonly="">
</div>    
</div>   
</div>    
    
</div>    
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="button" id="payreset" class="btn btn-flat bg-red btn-sm " value="Reset">
<input type="button" id="print_payment" class="btn btn-flat bg-purple btn-sm" value="Save &amp; Print">
<input type="button" id="save_payment" class="btn btn-flat bg-purple btn-sm" value="Save">    
 <a href="acc_paylist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'PAY','A');}else{echo read_activity($aid,'PAY','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->   
<?php include('../layout/rside.php'); ?>
<div id="invdata" style="display: none;"></div>    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var apdate = new LiveValidation('apdate');
apdate.add(Validate.Presence);
});
    
$(document).on('change', '#cid', function() {
id_arr = $(this).val();
id = id_arr.split("_");
type=id[0];
if(type=='BA'){
document.getElementById("chkno").disabled=false; 
document.getElementById("chkdt").disabled=false;     
}else{
$('#chkno').val('');
$('#chkdt').val('');    
document.getElementById("chkno").disabled=true; 
document.getElementById("chkdt").disabled=true;    
}    
});
    
ReadData();
function ReadData(){
$.ajax({
url: "acc_payview.php",
method: "POST",
data:{ 
body:1
},
success: function(data){
$('#itemdata').html(data);
}
})

$.ajax({
url: "acc_payview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})    
};    

function ReadFoot(){
$.ajax({
url: "acc_payview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})	
}
    
$(document).on('click', '#addpay', function(e) {
scid = $('#cid').val();
chkno = $('#chkno').val();
chkdt = $('#chkdt').val();    
sdid = $('#did').val();
amo = parseFloat($('#amo').val());    
ref = $('#ref').val();    
toastr.options = {'positionClass': 'toast-top-center'};

if(scid == '-Select-' || scid == ''){
toastr.info('Please Select Payment Sourch!');
return;
};    

ecid = scid.split("_");
cty=ecid[0];
cid=ecid[1];    

if(cty=='BA'){    
if(chkno.length>=0 && chkno.length<5){
toastr.info('Please Enter Valid Cheque No!');
return;    
}    

if(chkno.length>0){
if(chkdt.length<=0){
toastr.info('Please Enter Cheque Date!');
return;    
}    
}    
}
    
if(sdid == '-Select-' || sdid == ''){
toastr.info('Please Select Payment To!');
return;
}
    
edid = sdid.split("_");
dty=edid[0];
did=edid[1];
    
if(isNaN(amo) != false || amo <= 0){
toastr.info('Please Enter Amount!');
return;
}    

$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {getpayment:1,did:did,dty:dty,amo:amo,cty:cty,cid:cid,chkno:chkno,chkdt:chkdt,ref:ref},
success:function(data) {
$("#cid").val("").trigger("change");    
$("#did").val("").trigger("change");
$('#amo').val("");
$('#chkno').val(""); 
$('#chkdt').val("");     
$('#ref').val("");
ReadData();
}
});    
    
e.preventDefault();     
});
    
$(document).on('blur change', '#apdate', function() {
apdate=$(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
paydate: apdate
},
dataType: 'json',
success: function(data){
$('#apdate').val(data[0]);
}
});     
});    

$(document).on('blur', '#note', function() {
note = $(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
paynote: note
},
dataType: 'json',
success: function(data){
$('#note').val(data[0]);
}
});     
});    

$(document).on('click','.empty',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
paydata: 1
},
success: function(data){
ReadData();    
}
});    
});    

$(document).on('click', '.remove', function () {
var id = $(this).attr('id');
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
payremove: id
},
success: function(data){
ReadData();
}
});    
});    

$(document).on('click','#payreset',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
payclear: 1
},
success: function(data){
$("#note").val("");   
ReadData();    
}
});    
});

$(document).on('change', '#cid', function () {
id = $(this).val();
$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {checkbales:id},
success:function(data){
$('#cbalance').html(data);
}
});   
});    
    
$(document).on('change', '#did', function () {
id = $(this).val();
$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {checkbal:id},
success:function(data){
$('#balance').html(data);
}
});   
}); 

$(document).on('click', '#save_payment', function() {
var pay_data = $('.addpayment input, .addpayment select, .addpayment textarea');    
toastr.options = {'positionClass': 'toast-top-center'};
    
$.ajax({
url: "acc_paycart.php",
data: pay_data,
type: 'post',
dataType: 'json',    
success: function(data){
if(data.status === "success"){
$("#invno").val(data.nexvour);
$("apdate").val(data.date);    
$("#note").val("");    
ReadData();    
toastr.success(data.message);    
}else if(data.status === "info"){
toastr.info(data.message);    
}else{
toastr.error(data.message);    
}         
}
})    
});
   
$(document).on('click', '#print_payment', function() {
var pay_data = $('.addpayment input, .addpayment select, .addpayment textarea');    
toastr.options = {'positionClass': 'toast-top-center'};

$.ajax({
url: "acc_paycart.php",
data: pay_data,
type: 'post',
dataType: 'json',    
success: function(data){
if(data.status === "success"){
$("#invno").val(data.nexvour);
$("apdate").val(data.date);    
$("#note").val("");    
ReadData();    
toastr.success(data.message);
print_inv(data.printid);    
}else if(data.status === "info"){
toastr.info(data.message);    
}else{
toastr.error(data.message);    
}         
}
})    
});
    
function print_inv(id){
var mode = 'iframe'; //popup
var close = mode == "popup";
var options = {
mode: mode,
popClose: close
};    
$.ajax({
url: "acc_payprint.php",
data:{print:id}, 
type: 'post',    
success: function(data){
$("#invdata").html(data);    
$("div.printableArea").printArea(options);
$("#invdata").html('');    
}
})    
};    
</script>    
<!-- /page script -->
</html>    