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

if(isset($_POST['editrec'])){

    unset($_SESSION['axes_edrecdata']);
    unset($_SESSION['axes_edrecitem']);   

    $ids = $_POST['editrec'];
    $sql="SELECT * FROM tbl_recvoucher WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
    $adm=mysqli_query($con,$sql) or die(mysqli_error($con));    
    $rows=mysqli_fetch_array($adm);    

    $invno=$rows['invno'];

    $_SESSION['axes_edrecdata']['invno']=$rows['invno'];
    $_SESSION['axes_edrecdata']['note']=$rows['note'];    
    $_SESSION['axes_edrecdata']['date']=$rows['apdate'];   

    $sql="SELECT * FROM tbl_trarecord WHERE invno='$invno' ORDER BY id ASC";
    $result=mysqli_query($con,$sql) or die(mysqli_error($con));
    while ($row=mysqli_fetch_array($result)){
    addtoedrec($row['dty'],$row['did'],$row['amo'],$row['cid'],$row['cty'],$row['chkno'],$row['chkdt'],$row['ref'],$row['refinv'],$row['xrate']);	
    }


}



?>

<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">Edit Receipt</h3>
</div>
<div class="box-body">
<div class="col-md-12 addpayment">
<div class="row">    
<center>
<h3 class="page-title">RECEIPT VOUCHER EDIT</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 echo $rows['invno'];?>" placeholder="e.g. REV121119101" 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 echo $rows['apdate'];?>" placeholder="Date:" autocomplete="off">
</div>
</div>    
</div>
</div>
<div class="row">    
<div class="col-md-4">    
<div class="form-group">
<label>Received To</label>    
<select class="form-control select2" name="did" id="did">
<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>	<?php echo get_fild_data('tbl_currency','1','symbol'); ?> </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-4">
<div class="form-group" >
<label>Received From</label>
<div class="input-group">
<select class="form-control select2" name="cid" id="cid">
<option value="">-Select-</option>
<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="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="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>	<?php echo get_fild_data('tbl_currency','1','symbol'); ?> </span><span id="balance">0.00</span></div>    
</div>    
</div>   
<div class="col-md-2">
<div class="form-group" >
<label>Invoice</label>
<select class="form-control select2" name="refinv" id="refinv">

</select>
</div>    
</div> 
<div class="col-md-1">
<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="addrec" 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>Received</th>
<th>Received From</th>    
<th>Amount</th>
<th>Cheque</th>
<th>Date</th> 
<th>Inv</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 echo $rows['note'];?></textarea>
<input type="hidden" name="save_edreceived" 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="recreset" 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_reclist.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,'REC','A');}else{echo read_activity($aid,'REC','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', '#did', 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_edrecview.php",
method: "POST",
data:{ 
body:1
},
success: function(data){
$('#itemdata').html(data);
}
})

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

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

if(sdid == '-Select-' || sdid == ''){
toastr.info('Please Select Received To!');
return;
};    

edid = sdid.split("_");
dty=edid[0];
did=edid[1];

/*if(dty=='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(scid == '-Select-' || scid == ''){
toastr.info('Please Select Received From!');
return;
}
    
ecid = scid.split("_");
cty=ecid[0];
cid=ecid[1];
    
if(isNaN(amo) != false || amo <= 0){
toastr.info('Please Enter Amount!');
return;
}    

$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {getedreceived:1,did:did,dty:dty,amo:amo,cty:cty,cid:cid,chkno:chkno,chkdt:chkdt,ref:ref,refinv:refinv,xrate:xrate},
success:function(data) {
$("#cid").val("").trigger("change");    
$("#did").val("").trigger("change");
$('#amo').val("");
$('#chkno').val(""); 
$('#chkdt').val("");     
$('#ref').val("");
$('#refinv').val("");
ReadData();
}
});    
    
e.preventDefault();     
});
    
$(document).on('blur change', '#apdate', function() {
apdate=$(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
edrecdate: 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:{ 
edrecnote: note
},
dataType: 'json',
success: function(data){
$('#note').val(data[0]);
}
});     
});    

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

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

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

$(document).on('keydown', '#dinvno', function() {
$('#dinvno' ).autocomplete({
source: function( request, response ) {
$.ajax({
url: 'axe_cart.php',
type: 'post',
dataType: 'json',
data: {
search: request.term, invre:1
},
success: function(data) {
response(data);
}
});
},
select: function (event, ui) {
var te=ui.item.label;
$(this).val(te); // display the selected text
var invno = ui.item.info;
einf = invno.split("_");   
$("#cid").val('SU_'+einf[0]).trigger("change");    
$("#symbol").html(einf[1]+' '); 
$("#dueamo").html(einf[2]);    
return false;
}
});    
});
    
$(document).on('change keyup blur', '#xrate', function() {
var due = parseFloat($('#dueamo').html());
var xrate = parseFloat($(this).val());
var cash = 0;

cash = (due*xrate);
if(cash != '' && typeof(cash) != "undefined" && !isNaN(cash)){    
$("#amo").val(cash.toFixed(2));
}else{
$("#amo").val('');    
}
});

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

$.ajax({
url: "acc_reccart.php",
method: "POST",
data: {
getinvoice: id 
},
success: function(data) {	
$('#refinv').html(data);
}
})
});  


$(document).on('click change', '#refinv', function () {
id = $(this).val();

$.ajax({
url: "acc_reccart.php",
method: "POST",
data: {
getinvoiceamo: id 
},
success: function(data) {	
$('#amo').val(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_reccart.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_reccart.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_recprint.php",
data:{print:id}, 
type: 'post',    
success: function(data){
$("#invdata").html(data);    
$("div.printableArea").printArea(options);
$("#invdata").html('');    
}
})    
};    
</script>    
<!-- /page script -->
</html>