<?php 
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/cartfunctions.php');
include ('../inc/repfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){  
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$dtnow = date("Y-m-d h:i:s", time());



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


	if(!isset($_SESSION['axes_payitem'])){
    echo json_encode(array(
    'status' => 'info',
    'message'=> 'Sorry! No Payment Head found!!'
    )); 
    exit;    
    }

    
    $payno = gen_newinvno('tbl_payvoucher','PAV');
    $apdate = remove_junk(escape($_POST['apdate']));
    $note = remove_junk(escape($_POST['note']));
    $total = total_payvalue();
    
    $sql="INSERT INTO tbl_payvoucher (invno,note,amount,apdate,brid,uid,date) VALUES ('$payno','$note','$total','$apdate','$brid','$aid','$dtnow')";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $sid=$con->insert_id;
    $efid=mysqli_affected_rows($con);
    if($efid>0){
            if(isset($_SESSION['axes_payitem'])){
                if(is_array($_SESSION['axes_payitem'])){
                    $max=count($_SESSION['axes_payitem']);
                    for($i=0;$i<$max;$i++){
                        
                            $dty = $_SESSION['axes_payitem'][$i]['dty'];
                            $did=$_SESSION['axes_payitem'][$i]['did'];    
                            $amo=$_SESSION['axes_payitem'][$i]['amo'];
                            $cid=$_SESSION['axes_payitem'][$i]['cid'];
                            $cty=$_SESSION['axes_payitem'][$i]['cty'];   
                            $chkno=$_SESSION['axes_payitem'][$i]['chkno'];
                            $chkdt=$_SESSION['axes_payitem'][$i]['chkdt'];
                            if($chkno=='' || strlen($chkno)<2){$chkdt='NULL';$chkno='NULL';}else{$chkdt="'".$chkdt."'";$chkno="'".$chkno."'";}    
                            $ref=$_SESSION['axes_payitem'][$i]['ref'];
                                
                            $sql="INSERT INTO tbl_payvoucherde (seid,cid,cty,amount,did,dty,cheno,chedt,chest,ref ) VALUES ('$sid','$cid','$cty','$amo','$did','$dty',$chkno,$chkdt,'0','$ref')";
                            mysqli_query($con,$sql) or die(mysqli_error($con));

                            
                            if(get_fild_data('tbl_setting','12','sval')==0){
                                if($chkno != 'NULL'){
                                    $sql="INSERT INTO tbl_cheqman(clientid,type,chkno,chkdt,amount,acid,refinv,ref,paymod,status,apdate,brid,uid,date) VALUES ('$did','$dty',$chkno,$chkdt,'$amo','$cid','$payno','$ref','PAY','1','$apdate','$brid','$aid','$dtnow')";
                                    mysqli_query($con,$sql) or die(mysqli_error($con)); 
                                    $amo=0;
                                } 
                            } 

                            $sql="INSERT INTO tbl_trarecord (invno,dty,did,amo,cid,cty,chkno,chkdt,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$payno','$dty','$did','$amo','$cid','$cty',$chkno,$chkdt,'$ref','0','0','$apdate','$brid','$aid','$dtnow')";
                            mysqli_query($con,$sql) or die(mysqli_error($con)); 
                        
                
                        
                    }
                }
            }
            unset($_SESSION['axes_paydata']);
            unset($_SESSION['axes_payitem']);    
            $act =remove_junk(escape('Payment No: '.$payno));    
            write_activity($aid,'PAY','Payment has been Added',$act);    
                echo json_encode(array(
                'status' => 'success',
                'message'=> 'Data Save Successfully!!',
                'printid'=> $sid    
                ));
                exit; 
    }else{
        echo json_encode(array(
        'status' => 'error',
        'message'=> 'Sorry! Data Fail to Saved!!'
        )); 
    }
}  
?> 