<?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_received'])){

    
	if(!isset($_SESSION['axes_recitem'])){
    echo json_encode(array(
    'status' => 'info',
    'message'=> 'Sorry! No Received Head found!!'
    )); 
    exit;   
    }
    
    $recno = gen_newinvno('tbl_recvoucher','REV');
    $apdate = remove_junk(escape($_POST['apdate']));
    $note = remove_junk(escape($_POST['note']));
    $total = total_recvalue();
    
    $sql="INSERT INTO tbl_recvoucher (invno,note,amount,apdate,brid,uid,date) VALUES ('$recno','$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_recitem'])){
    if(is_array($_SESSION['axes_recitem'])){
    $max=count($_SESSION['axes_recitem']);
    for($i=0;$i<$max;$i++){
        
    $dty = $_SESSION['axes_recitem'][$i]['dty'];
    $did=$_SESSION['axes_recitem'][$i]['did'];    
    $amo=$_SESSION['axes_recitem'][$i]['amo'];
    $cid=$_SESSION['axes_recitem'][$i]['cid'];
    $cty=$_SESSION['axes_recitem'][$i]['cty'];   
    $chkno=$_SESSION['axes_recitem'][$i]['chkno'];
    $chkdt=$_SESSION['axes_recitem'][$i]['chkdt'];
    if($chkno=='' || strlen($chkno)<2){$chkdt='NULL';$chkno='NULL';}else{$chkdt="'".$chkdt."'";$chkno="'".$chkno."'";}    
    $ref=$_SESSION['axes_recitem'][$i]['ref'];
    $refinv=$_SESSION['axes_recitem'][$i]['refinv'];
    if($refinv=='' || strlen($refinv)<1){$refinv='NULL';}else{$refinv="'".$refinv."'";}    
    $xrate=$_SESSION['axes_recitem'][$i]['xrate'];    
    if($xrate=='' || strlen($xrate)<1){$xrate=0;} 
    
    $sql="INSERT INTO tbl_recvoucherde (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 ('$cid','$cty',$chkno,$chkdt,'$amo','$did','$recno','$ref','REC','1','$apdate','$brid','$aid','$dtnow')";
            mysqli_query($con,$sql) or die(mysqli_error($con)); 
            $amo=0;
        }  
    }  
    $sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,ref,curid,xrate,apdate,uid,brid,date) VALUES ('$recno',$refinv,'$dty','$did','$amo','$cid','$cty',$chkno,$chkdt,'$ref','0','$xrate','$apdate','$aid','$brid','$dtnow')";
    mysqli_query($con,$sql) or die(mysqli_error($con)); 

    
        
    }}}
    unset($_SESSION['axes_recdata']);
    unset($_SESSION['axes_recitem']);    
    $act =remove_junk(escape('Receipt No: '.$recno));    
    write_activity($aid,'REC','Receipt 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!!'
        )); 
    
    }

}  
?> 