<?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'];
    $smsstatus = remove_junk(escape($_POST['smsstatus']));
    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)); 


    $sql="INSERT INTO tbl_traledger1 (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)); 

    if($smsstatus==1){
        $sql="SELECT * FROM tbl_sms WHERE id=1 LIMIT 1";
        $smsde = mysqli_query($con,$sql);
        $sms=mysqli_fetch_array($smsde);
    
        if($sms['status']==1){
            $lang=$sms['language'];
            $appkey=$sms['appkey'];
            $senderid=$sms['senderid'];
            $type=$sms['type'];
            $url=$sms['url'];
            $htext=$sms['htext'];
            $ftext=$sms['ftext'];
            
            $hbtext=$sms['hbtext'];
            $fbtext=$sms['fbtext'];
            if($cty=='SU'){
                $cmobile=get_fild_data('tbl_supplier',$cid,'cnumber');
            }else if($cty=='CU'){
                $cmobile=get_fild_data('tbl_customer',$cid,'cnumber');
            }else{
                $cmobile=get_fild_data('tbl_employe',$cid,'mobile');
            } 
            
            
            if($lang==1){
                $url = "$url";
                $data = [
                "api_key" => "$appkey",
                "type" => "$type",
                "contacts" => "$cmobile",
                "senderid" => "$senderid",
                "msg" => "$htext $dtnow Voucher No:$recno. Withdrawal Amount: $total TK. $ftext.",
                ];
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $response = curl_exec($ch);
                curl_close($ch);
            }else{
                $url = "$url";
                $data = [
                    "api_key" => "$appkey",
                    "type" => "$type",
                    "contacts" => "$cmobile",
                    "senderid" => "$senderid",
                    "msg" => "$hbtext $dtnow রসিদ নং:$recno. উত্তোলন সর্বমোট: $total TK. $fbtext.",
                ];
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $response = curl_exec($ch);
                curl_close($ch);
            
            }
        
        }

    }

    
        
    }}}
    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!!'
        )); 
    
    }

}  


if(isset($_POST['getinvoice'])){
    if(strlen($_POST['getinvoice'])>0){    
    $eid=explode('_',$_POST['getinvoice']);
    $cty=$eid['0'];
    $cid=$eid['1'];
    }else{
    $cty='';
    $cid='';    
    }
    $output='';
    $output .= '<option value="">-Select-</option>';
    if($cty=='CU'){
        $sql="SELECT invno FROM tbl_sales WHERE  (total-rawcash) >0 AND type='$cty' AND cusid='$cid'";
        $querys=mysqli_query($con,$sql)or die(mysqli_error($con));
        if($querys->num_rows > 0) {    
            while ($row=mysqli_fetch_array($querys)){
                $output .= '<option value="'.$row['invno'].'">'.$row['invno'].'</option>';   
            }
        }  
        
    }
    echo $output;
    exit;    
}

if(isset($_POST['getinvoiceamo'])){
    if(strlen($_POST['getinvoiceamo'])>0){    
    $id=$_POST['getinvoiceamo'];
    $sql="SELECT (total-rawcash) as amount FROM tbl_sales WHERE  invno='$id'";
    $querys=mysqli_query($con,$sql)or die(mysqli_error($con));
    $row=mysqli_fetch_array($querys);

    $lnet= $row['amount'];
    echo $lnet;
    }else{
    echo '0.00';    
    }    
    }
?> 