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

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

    $temp_id                = $array['temp_id'] ;
    $item_id                = $array['item_id'] ;
    $deno_id                = $array['deno_id'] ;
    $dialcode               = $array['dialcode'] ;
    $mobile                 = $array['mobile'] ;

    if ( $temp_id != '' && $item_id != '' && $deno_id != '' ){
        $dialcode           = str_replace( '+', '', $dialcode ) ;

        $select_deno        = $mysqli->query( "SELECT deno_code, amount FROM rms_prepaid_deno
                                               WHERE deleted_at IS NULL AND item_id = '".$item_id."' AND deno_id = '".$deno_id."' AND status = 'active' LIMIT 1" ) ;
        if ( $select_deno->num_rows > 0 ){

            $row_deno       = $select_deno->fetch_assoc() ;
            $amount         = numberFormat( $row_deno['amount'], 2 ) ;

            $select_temp    = $mysqli->query( "SELECT rms_authorizationtoken FROM staff_rms_prepaid_pretemp
                                               WHERE deleted_at IS NULL AND temp_id = '".$temp_id."' AND item_id = '".$item_id."' AND dialcode = '".$dialcode."' AND mobile = '".$mobile."' LIMIT 1" ) ;

            if ( $select_temp->num_rows > 0 ){
                $status     = '250' ;

                $row_temp   = $select_temp->fetch_assoc() ;

                if ( $amount > 0 && $staff_info['staff_wallet'] >= $amount ){
                    $status = '203' ;

                    if ( $mysqli->query( "INSERT INTO staff_rms_prepaid_order
                                          ( `branch_id`, `staff_id`, `temp_id`, `item_id`, `deno_id`, `deno_code`, `cashier_id`, `dialcode`, `mobile`, `amount` ) VALUES
                                          ( '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$temp_id."', '".$item_id."', '".$deno_id."', '".$row_deno['deno_code']."', '".RMSLOCATION."', '".$dialcode."', '".$mobile."', '".$amount."' )" ) ){
                        // update sonumber
                        $order_id      = $mysqli->insert_id ;
                        $sonumber      = 'TO'.strPad( 6, $order_id ) ;

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

                        $status        = '298' ;

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

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

                            $rms_content   = [
                                'referenceId'               => $sonumber,
                                'cashierId'                 => RMSLOCATION,
                                'productCode'               => $row_deno['deno_code'],
                                'transactionDateTime'       => date( "Y-m-d\TH:i:s", time() ),
                                'businessDate'              => date( "Y-m-d", time() ),
                                'countryCode'               => $dialcode,
                                'mobileNumber'              => $mobile,
                                'partnerReferenceId'        => $sonumber
                            ] ;
                            $rms_call      = rmsCall( 'pinless/requesttopup', $rms_content ) ;
                            saveLog( 'rms-api', 'Request Prepaid Order', $rms_content, $rms_call ) ;

                            // update status
                            $mysqli->query( "UPDATE staff_rms_prepaid_order SET 
                                             rms_instruction        = '".$rms_call['instructions']."',
                                             rms_order_id           = '".$rms_call['orderId']."',
                                             status                 = 'pending'
                                             WHERE order_id         = '".$order_id."'" ) ;
                            
                        }

                    }
                }

            }

        }

    }
}

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