Skip to content

Commit c06aefc

Browse files
committed
Update
- Fixed: orders not recorded on register while making a sale
1 parent b6ad504 commit c06aefc

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

app/Listeners/CashRegisterEventsSubscriber.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Events\OrderAfterCreatedEvent;
77
use App\Events\OrderAfterPaymentCreatedEvent;
88
use App\Events\OrderAfterPaymentStatusChangedEvent;
9+
use App\Events\OrderAfterUpdatedEvent;
910
use App\Services\CashRegistersService;
1011

1112
class CashRegisterEventsSubscriber
@@ -57,6 +58,11 @@ public function subscribe( $event )
5758
[ $this->registerService, 'increaseFromOrderCreatedEvent' ]
5859
);
5960

61+
$event->listen(
62+
OrderAfterUpdatedEvent::class,
63+
[ $this->registerService, 'increaseFromOrderCreatedEvent' ]
64+
);
65+
6066
$event->listen(
6167
OrderRefundPaymentAfterCreatedEvent::class,
6268
[ $this->registerService, 'afterOrderRefunded' ]

app/Services/CashRegistersService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ public function increaseFromPaidOrder( OrderAfterPaymentStatusChangedEvent $even
249249
* Listen to order created and
250250
* will update the cash register if any order
251251
* is marked as paid.
252-
* @param OrderAfterCreatedEvent $event
252+
* @param OrderAfterCreatedEvent|OrderAfterUpdatedEvent $event
253253
* @return void
254254
*/
255-
public function increaseFromOrderCreatedEvent( OrderAfterCreatedEvent $event )
255+
public function increaseFromOrderCreatedEvent( $event )
256256
{
257257
/**
258258
* If the payment status changed from

config/nexopos.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
return [
3-
'version' => '4.6.16',
3+
'version' => '4.6.17',
44
'languages' => [
55
'en' => 'English',
66
'fr' => 'Français',

tests/Feature/CreateOrderOnRegister.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ public function test_create_order_on_register()
3131
$this->attemptAuthenticate();
3232
$this->attemptCreateOrderOnRegister();
3333
}
34+
35+
public function test_update_order_on_register()
36+
{
37+
$this->attemptAuthenticate();
38+
$this->attemptUpdateOrderOnRegister();
39+
}
3440
}

tests/Traits/WithOrderTest.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function attemptPostOrder( $callback )
6363
return $responses;
6464
}
6565

66-
protected function attemptCreateOrderOnRegister()
66+
protected function attemptCreateOrderOnRegister( $data = [] )
6767
{
6868
RegisterHistory::truncate();
6969

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

101101
/**
102102
* between each operation
103103
* we need to refresh the cash register
104104
*/
105105
$cashRegister->refresh();
106106

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

110115
/**
111116
* Step 2 : disburse (cash-out) some cash
@@ -171,19 +176,60 @@ protected function attemptCreateOrderOnRegister()
171176

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

174-
$this->assertEquals( $cashRegister->balance, $totalTransactions, __( 'The transaction aren\'t reflected on the register balance' ) );
179+
$this->assertEquals(
180+
ns()->currency->getRaw( $cashRegister->balance ),
181+
ns()->currency->getRaw( $totalTransactions ),
182+
__( 'The transaction aren\'t reflected on the register balance' )
183+
);
184+
185+
return compact( 'response', 'cashRegister' );
186+
}
187+
188+
public function attemptUpdateOrderOnRegister()
189+
{
190+
/**
191+
* @var OrdersService $orderService
192+
*/
193+
$orderService = app()->make( OrdersService::class );
194+
195+
$result = $this->attemptCreateOrderOnRegister([
196+
'orderData' => [
197+
'payments' => [], // we'll disable payments.
198+
]
199+
]);
200+
201+
extract( $result );
202+
/**
203+
* @var array $response
204+
* @var Register $cashRegister
205+
*/
206+
$order = Order::find( $response[ 'data' ][ 'order' ][ 'id' ] );
207+
$orderService->makeOrderSinglePayment([
208+
'identifier' => OrderPayment::PAYMENT_CASH,
209+
'value' => $response[ 'data' ][ 'order' ][ 'total' ],
210+
], $order );
211+
212+
/**
213+
* Making assertions
214+
*/
215+
$cashRegisterHistory = RegisterHistory::where( 'register_id', $cashRegister->id )->orderBy( 'id', 'desc' )->first();
216+
217+
$this->assertTrue(
218+
ns()->currency->getRaw( $cashRegisterHistory->value ) === $order->total,
219+
__( 'The payment wasn\'t added to the cash register history' )
220+
);
175221
}
176222

177-
private function registerOrderForCashRegister( Register $cashRegister )
223+
private function registerOrderForCashRegister( Register $cashRegister, $data )
178224
{
179225
/**
180226
* @var TestService
181227
*/
182228
$testService = app()->make( TestService::class );
183229

184-
$orderDetails = $testService->prepareOrder( ns()->date->now(), [
230+
$orderDetails = $testService->prepareOrder( ns()->date->now(), array_merge([
185231
'register_id' => $cashRegister->id
186-
]);
232+
], $data ) );
187233

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

0 commit comments

Comments
 (0)