<?php
include '../connect/cms-config.php' ;
include '../requires/function.php' ;


// confirm order and check the order within 5 minutes
$from_date       = date( 'Y-m-d H:i:s', strtotime( "-5 minutes" ) ) ;
$to_date         = date( 'Y-m-d H:i:s', strtotime( "-3 minutes" ) ) ;

// confirm order and check the order within in 1 hour, 2 hours, 3 hours, 4 hours
$hours1          = date( 'Y-m-d H:i', strtotime( "-235 minutes" ) ) ;
$hours2          = date( 'Y-m-d H:i', strtotime( "-175 minutes" ) ) ;
$hours3          = date( 'Y-m-d H:i', strtotime( "-115 minutes" ) ) ;
$hours4          = date( 'Y-m-d H:i', strtotime( "-55 minutes" ) ) ;

$select_order    = $mysqli->query( "SELECT order_id, staff_id, biller_code, rms_authorizationtoken, created_at FROM staff_rms_bill_order
				   		   		    WHERE deleted_at IS NULL AND status IN ( 'pending', 'progress' ) AND ( ( created_at BETWEEN '".$from_date."' AND '".$to_date."' ) OR created_at LIKE '%".$hours1."%' OR created_at LIKE '%".$hours2."%' OR created_at LIKE '%".$hours3."%' OR created_at LIKE '%".$hours4."%' )" ) ;
if ( $select_order->num_rows > 0 ){
	while ( $row_order = $select_order->fetch_assoc() ){

		$rms_content   = [
            'authorizationToken'    => $row_order['rms_authorizationtoken'],
            'cashierId'        		=> RMSLOCATION,
            'transactionDateTime'  	=> date( 'Y-m-d\TH:i:s', strtotime( $row_order['created_at'] ) ),
            'businessDate'  		=> date( 'Y-m-d', strtotime( $row_order['created_at'] ) )
        ] ;
		$rms_call      = rmsCall( 'bill/confirm', $rms_content ) ;
        saveLog( 'rms-api', 'Confirm Bill Order *Per Minutes*', $rms_content, $rms_call ) ;

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

			if ( $mysqli->query( "UPDATE staff_rms_bill_order SET
								  rms_order_id   = '".$rms_call['orderId']."',
								  rms_receiptno  = '".$rms_call['billerReceiptNo']."',
								  status 	     = 'confirmed'
								  WHERE order_id = '".$row_order['order_id']."'" ) ){
				$status = '200' ;
			}

		}else{

			if ( $mysqli->query( "UPDATE staff_rms_bill_order SET
								  status 	     = 'progress'
								  WHERE order_id = '".$row_order['order_id']."'" ) ){
				$status = '202' ;
			}

		}
		
	}
}



// after transaction over 4 hours, auto cancel order and refund the wallet
$hours1          = date( 'Y-m-d H:i', strtotime( "-300 minutes" ) ) ;
$select_order    = $mysqli->query( "SELECT order_id, staff_id, sonumber, reference1, amount FROM staff_rms_bill_order
				   		   		    WHERE deleted_at IS NULL AND status IN ( 'pending', 'progress' ) AND ( created_at LIKE '%".$hours1."%' )" ) ;
if ( $select_order->num_rows > 0 ){
	while ( $row_order = $select_order->fetch_assoc() ){

		saveLog( 'rms-api', 'Auto Cancel Bill', [], $row_order ) ;

		if ( $mysqli->query( "UPDATE staff_rms_bill_order SET
							  status 	     = 'cancelled'
							  WHERE order_id = '".$row_order['order_id']."'" ) ){
			
			// refund the wallet
            $remark    = 'You have been refunded the wallet from bill order '.$row_order['reference1'].' (' . $row_order['sonumber'] . ')' ;
            walletMovement( 'staff_rms_bill_order', $row_order['order_id'], 'plus', 'normal', $row_order['staff_id'], $row_order['amount'], $remark ) ;

		}
		
	}
}

?>