<?php
$must_login                 = true ;
$require_path               = '../../../' ;
$require_sub                = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){
    $status                 = '300' ;

    $bill_id                = $array['bill_id'] ;
    $amount                 = $array['amount'] ;

    if ( $bill_id != '' && $amount > 0 ){
        $status             = '250' ;

        $amount             = numberFormat( $amount, 2 ) ;

        if ( $staff_info['staff_wallet'] >= $amount ){
            $status         = '201' ;

            $select_bill    = $mysqli->query( "SELECT a.bill_id, a.reference1, a.reference2, a.reference3, a.reference4, b.biller_code, b.min_amount, b.max_amount FROM staff_rms_bill a
                                               LEFT JOIN rms_bill_item b ON ( a.item_id = b.item_id )
                                               WHERE a.deleted_at IS NULL AND b.deleted_at IS NULL AND a.staff_id = '".$staff_info['staff_id']."' AND a.bill_id = '".$bill_id."' LIMIT 1" ) ;
            if ( $select_bill->num_rows > 0 ){
                $status     = 'rms-39' ;

                $row_bill   = $select_bill->fetch_assoc() ;

                if ( $amount >= $row_bill['min_amount'] && $amount <= $row_bill['max_amount'] ){

                    $status = '203' ;

                    if ( $mysqli->query( "INSERT INTO staff_rms_bill_order
                                          ( `branch_id`, `staff_id`, `bill_id`, `biller_code`, `cashier_id`, `reference1`, `reference2`, `reference3`, `reference4`, `amount` ) VALUES
                                          ( '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$bill_id."', '".$row_bill['biller_code']."', '".RMSLOCATION."', '".$row_bill['reference1']."', '".$row_bill['reference2']."', '".$row_bill['reference3']."', '".$row_bill['reference4']."', '".$amount."' )" ) ){
                        // update sonumber
                        $order_id      = $mysqli->insert_id ;
                        $sonumber      = 'BO'.strPad( 6, $order_id ) ;

                        $mysqli->query( "UPDATE staff_rms_bill_order SET sonumber = '".$sonumber."' WHERE order_id = '".$order_id."'" ) ;

                        $rms_content   = [
                            'referenceId'       => $sonumber,
                            'billerCode'        => $row_bill['biller_code'],
                            'billReferenceNo1'  => $row_bill['reference1'],
                            'billReferenceNo2'  => $row_bill['reference2'],
                            'billReferenceNo3'  => $row_bill['reference3'],
                            'billReferenceNo4'  => $row_bill['reference4'],
                            'amount'            => $amount
                        ] ;
                        $rms_call      = rmsCall( 'bill/initiate', $rms_content ) ;
                        saveLog( 'rms-api', 'Intiate Bill Order', $rms_content, $rms_call ) ;

                        if ( $rms_call['respCode'] == '00' ){
                            $status       = '298' ;

                            // deduct the wallet first
                            $remark       = 'You have been deducted the wallet from bill order '.$row_bill['reference1'].' (' . $sonumber . ')' ;
                            $walletdeduct = walletMovement( 'staff_rms_bill_order', $order_id, 'minus', 'normal', $staff_info['staff_id'], -($amount), $remark ) ;

                            if ( $walletdeduct ){
                                $status   = '200' ;

                                // update status
                                $mysqli->query( "UPDATE staff_rms_bill_order SET 
                                                 rms_accountname        = '".$rms_call['billAccountName']."',
                                                 rms_instruction        = '".$rms_call['instruction']."',
                                                 rms_authorizationtoken = '".$rms_call['authorizationToken']."',
                                                 status                 = 'pending'
                                                 WHERE order_id         = '".$order_id."'" ) ;
                            }

                        }else{
                            $status    = 'rms-'.$rms_call['respCode'] ;
                        }

                    }
                }
            }
        }

    }
}

require( $require_sub.'footer.php' ) ;
?>