<?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();
    $smsstatus = remove_junk(escape($_POST['smsstatus']));
    
    $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'];
                            $refinv=$_SESSION['axes_payitem'][$i]['refinv'];
                                
                            $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;
                                } 
                            } 
                            // new code for balance payemnt
                            $sql="INSERT INTO tbl_traledger1 (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$payno','$refinv','$dty','$did','$amo','$cid','$cty',$chkno,$chkdt,'$ref','0','0','$apdate','$brid','$aid','$dtnow')";
                            mysqli_query($con,$sql) or die(mysqli_error($con));


                            $sql="INSERT INTO tbl_trarecord (invno,refinv,dty,did,amo,cid,cty,chkno,chkdt,ref,curid,xrate,apdate,brid,uid,date) VALUES ('$payno','$refinv','$dty','$did','$amo','$cid','$cty',$chkno,$chkdt,'$ref','0','0','$apdate','$brid','$aid','$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($dty=='SU'){
                                        $cmobile=get_fild_data('tbl_supplier',$did,'cnumber');
                                    }else if($dty=='CU'){
                                        $cmobile=get_fild_data('tbl_customer',$did,'cnumber');
                                    }else{
                                        $cmobile=get_fild_data('tbl_employe',$did,'mobile');
                                    } 
                                    
                                    
                                    if($lang==1){
                                        $url = "$url";
                                        $data = [
                                        "api_key" => "$appkey",
                                        "type" => "$type",
                                        "contacts" => "$cmobile",
                                        "senderid" => "$senderid",
                                        "msg" => "$htext $dtnow Voucher No:$payno. Deposite 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 রসিদ নং:$payno. জমা সর্বমোট: $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_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!!'
        )); 
    }
} 

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=='SU'){
        $sql="SELECT invno FROM tbl_purchase WHERE  (total-rawcash) >0 AND type='$cty' AND supid='$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_purchase 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';    
    }    
    }
?> 