Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Fixed: orders not recorded on register while making a sale
  • Loading branch information
Blair2004 committed Mar 15, 2022
1 parent b6ad504 commit c06aefc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
6 changes: 6 additions & 0 deletions app/Listeners/CashRegisterEventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Events\OrderAfterCreatedEvent;
use App\Events\OrderAfterPaymentCreatedEvent;
use App\Events\OrderAfterPaymentStatusChangedEvent;
use App\Events\OrderAfterUpdatedEvent;
use App\Services\CashRegistersService;

class CashRegisterEventsSubscriber
Expand Down Expand Up @@ -57,6 +58,11 @@ public function subscribe( $event )
[ $this->registerService, 'increaseFromOrderCreatedEvent' ]
);

$event->listen(
OrderAfterUpdatedEvent::class,
[ $this->registerService, 'increaseFromOrderCreatedEvent' ]
);

$event->listen(
OrderRefundPaymentAfterCreatedEvent::class,
[ $this->registerService, 'afterOrderRefunded' ]
Expand Down
4 changes: 2 additions & 2 deletions app/Services/CashRegistersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ public function increaseFromPaidOrder( OrderAfterPaymentStatusChangedEvent $even
* Listen to order created and
* will update the cash register if any order
* is marked as paid.
* @param OrderAfterCreatedEvent $event
* @param OrderAfterCreatedEvent|OrderAfterUpdatedEvent $event
* @return void
*/
public function increaseFromOrderCreatedEvent( OrderAfterCreatedEvent $event )
public function increaseFromOrderCreatedEvent( $event )
{
/**
* If the payment status changed from
Expand Down
2 changes: 1 addition & 1 deletion config/nexopos.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
return [
'version' => '4.6.16',
'version' => '4.6.17',
'languages' => [
'en' => 'English',
'fr' => 'Français',
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/CreateOrderOnRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public function test_create_order_on_register()
$this->attemptAuthenticate();
$this->attemptCreateOrderOnRegister();
}

public function test_update_order_on_register()
{
$this->attemptAuthenticate();
$this->attemptUpdateOrderOnRegister();
}
}
62 changes: 54 additions & 8 deletions tests/Traits/WithOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function attemptPostOrder( $callback )
return $responses;
}

protected function attemptCreateOrderOnRegister()
protected function attemptCreateOrderOnRegister( $data = [] )
{
RegisterHistory::truncate();

Expand Down Expand Up @@ -96,16 +96,21 @@ protected function attemptCreateOrderOnRegister()
* Step 1 : let's prepare the order
* before submitting that.
*/
$response = $this->registerOrderForCashRegister( $cashRegister );
$response = $this->registerOrderForCashRegister( $cashRegister, $data[ 'orderData' ] ?? [] );

/**
* between each operation
* we need to refresh the cash register
*/
$cashRegister->refresh();

$this->assertNotEquals( $cashRegister->balance, $previousValue, __( 'There hasn\'t been any change during the transaction on the cash register balance.' ) );
$this->assertEquals( ( float ) $cashRegister->balance, ( float ) ( $previousValue + $response[ 'data' ][ 'order' ][ 'total' ] ), __( 'The cash register balance hasn\'t been updated correctly.' ) );
/**
* only if the order total is greater than 0
*/
if( (float) $response[ 'data' ][ 'order' ][ 'tendered' ] > 0 ) {
$this->assertNotEquals( $cashRegister->balance, $previousValue, __( 'There hasn\'t been any change during the transaction on the cash register balance.' ) );
$this->assertEquals( ( float ) $cashRegister->balance, ( float ) ( $previousValue + $response[ 'data' ][ 'order' ][ 'total' ] ), __( 'The cash register balance hasn\'t been updated correctly.' ) );
}

/**
* Step 2 : disburse (cash-out) some cash
Expand Down Expand Up @@ -171,19 +176,60 @@ protected function attemptCreateOrderOnRegister()

$totalTransactions = ( $openingBalance + $totalCashing + $totalSales ) - ( $totalClosing + $totalRefunds + $totalCashOut );

$this->assertEquals( $cashRegister->balance, $totalTransactions, __( 'The transaction aren\'t reflected on the register balance' ) );
$this->assertEquals(
ns()->currency->getRaw( $cashRegister->balance ),
ns()->currency->getRaw( $totalTransactions ),
__( 'The transaction aren\'t reflected on the register balance' )
);

return compact( 'response', 'cashRegister' );
}

public function attemptUpdateOrderOnRegister()
{
/**
* @var OrdersService $orderService
*/
$orderService = app()->make( OrdersService::class );

$result = $this->attemptCreateOrderOnRegister([
'orderData' => [
'payments' => [], // we'll disable payments.
]
]);

extract( $result );
/**
* @var array $response
* @var Register $cashRegister
*/
$order = Order::find( $response[ 'data' ][ 'order' ][ 'id' ] );
$orderService->makeOrderSinglePayment([
'identifier' => OrderPayment::PAYMENT_CASH,
'value' => $response[ 'data' ][ 'order' ][ 'total' ],
], $order );

/**
* Making assertions
*/
$cashRegisterHistory = RegisterHistory::where( 'register_id', $cashRegister->id )->orderBy( 'id', 'desc' )->first();

$this->assertTrue(
ns()->currency->getRaw( $cashRegisterHistory->value ) === $order->total,
__( 'The payment wasn\'t added to the cash register history' )
);
}

private function registerOrderForCashRegister( Register $cashRegister )
private function registerOrderForCashRegister( Register $cashRegister, $data )
{
/**
* @var TestService
*/
$testService = app()->make( TestService::class );

$orderDetails = $testService->prepareOrder( ns()->date->now(), [
$orderDetails = $testService->prepareOrder( ns()->date->now(), array_merge([
'register_id' => $cashRegister->id
]);
], $data ) );

$response = $this->withSession( $this->app[ 'session' ]->all() )
->json( 'POST', 'api/nexopos/v4/orders', $orderDetails );
Expand Down

0 comments on commit c06aefc

Please sign in to comment.