Skip to content

Commit d2c4f45

Browse files
committed
Changelog
- Fix: double entry on cash flow for procurement that has status updated from list (#1265)
1 parent 303a175 commit d2c4f45

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

app/Http/Controllers/Dashboard/ProcurementController.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ public function changePaymentStatus( Procurement $procurement, Request $request
142142
$procurement->payment_status = $request->input( 'payment_status' );
143143
$procurement->save();
144144

145-
event( new ProcurementAfterUpdateEvent( $procurement ) );
146-
147145
return [
148146
'status' => 'success',
149147
'message' => __( 'The procurement payment status has been changed successfully.' ),
@@ -166,8 +164,6 @@ public function setAsPaid( Procurement $procurement )
166164
$procurement->payment_status = Procurement::PAYMENT_PAID;
167165
$procurement->save();
168166

169-
event( new ProcurementAfterUpdateEvent( $procurement ) );
170-
171167
return [
172168
'status' => 'success',
173169
'message' => __( 'The procurement has been marked as paid.' ),

app/Services/ProcurementService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public function edit( $id, $data )
226226
}
227227

228228
/**
229-
* we want to dispatch the even
229+
* we want to dispatch the event
230230
* only when the product has been created
231231
*/
232232
event( new ProcurementAfterUpdateEvent( $procurement ) );

app/Services/TestService.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function prepareProcurement( Carbon $date, array $details = [] )
141141
$taxGroup = TaxGroup::get()->random();
142142
$margin = 25;
143143

144-
return array_merge([
144+
$config = [
145145
'name' => sprintf( __( 'Sample Procurement %s' ), Str::random(5) ),
146146
'general' => [
147147
'provider_id' => Provider::get()->random()->id,
@@ -201,6 +201,12 @@ public function prepareProcurement( Carbon $date, array $details = [] )
201201
'unit_id' => $data->unit->id,
202202
];
203203
}),
204-
], $details );
204+
];
205+
206+
foreach( $details as $key => $value ) {
207+
Arr::set( $config, $key, $value );
208+
}
209+
210+
return $config;
205211
}
206212
}

tests/Traits/WithProcurementTest.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ protected function attemptCreateAnUnpaidProcurement()
2020
$provider = Provider::get()->random();
2121
$currentExpenseValue = CashFlow::where( 'expense_category_id', ns()->option->get( 'ns_procurement_cashflow_account' ) )->sum( 'value' );
2222
$procurementsDetails = $testService->prepareProcurement( ns()->date->now(), [
23-
'general' => [
24-
'payment_status' => 'unpaid',
25-
'provider_id' => $provider->id,
26-
'delivery_status' => Procurement::DELIVERED,
27-
],
23+
'general.payment_status' => 'unpaid',
24+
'general.provider_id' => $provider->id,
25+
'general.delivery_status' => Procurement::DELIVERED
2826
]);
2927

3028
$response = $this->withSession( $this->app[ 'session' ]->all() )
3129
->json( 'POST', 'api/nexopos/v4/procurements', $procurementsDetails );
3230

33-
$response->assertOk();
3431
$decode = json_decode( $response->getContent(), true );
3532

3633
$newExpensevalue = CashFlow::where( 'expense_category_id', ns()->option->get( 'ns_procurement_cashflow_account' ) )->sum( 'value' );
@@ -56,6 +53,7 @@ protected function attemptCreateAnUnpaidProcurement()
5653
$newExpensevalue = CashFlow::where( 'expense_category_id', ns()->option->get( 'ns_procurement_cashflow_account' ) )->sum( 'value' );
5754
$existingExpense = CashFlow::where( 'procurement_id', $procurement[ 'id' ] )->first();
5855

56+
$this->assertEquals( 1, CashFlow::where( 'procurement_id', $procurement[ 'id' ] )->count(), 'There is more than 1 cash flow created for the same procurement.' );
5957
$this->assertEquals( ns()->currency->getRaw( $existingExpense->value ), ns()->currency->getRaw( $procurement[ 'cost' ] ), 'The cash flow value doesn\'t match the procurement cost.' );
6058
$this->assertTrue( $existingExpense instanceof CashFlow, 'No cash flow was created after the procurement was marked as paid.' );
6159
$this->assertTrue( (float) $currentExpenseValue !== (float) $newExpensevalue, 'The expenses hasn\'t changed for the previously unpaid procurement.' );
@@ -69,13 +67,39 @@ protected function attemptCreateProcurement()
6967
$testService = app()->make( TestService::class );
7068

7169
$currentExpenseValue = CashFlow::where( 'expense_category_id', ns()->option->get( 'ns_procurement_cashflow_account' ) )->sum( 'value' );
72-
$procurementsDetails = $testService->prepareProcurement( ns()->date->now(), [] );
70+
$procurementsDetails = $testService->prepareProcurement( ns()->date->now(), [
71+
'general.payment_status' => Procurement::PAYMENT_UNPAID,
72+
'general.delivery_status' => Procurement::PENDING
73+
]);
7374

75+
/**
76+
* Query: We store the procurement with an unpaid status.
77+
*/
7478
$response = $this->withSession( $this->app[ 'session' ]->all() )
7579
->json( 'POST', 'api/nexopos/v4/procurements', $procurementsDetails );
7680

7781
$response->assertOk();
7882

83+
$procurementId = $response->json()[ 'data' ][ 'procurement' ][ 'id' ];
84+
85+
/**
86+
* Check: at the point, there shouldn't be any expense recorded.
87+
* The procurement is not paid.
88+
*/
89+
$existingExpense = CashFlow::where( 'procurement_id', $procurementId )->first();
90+
$this->assertTrue( ! $existingExpense instanceof CashFlow, __( 'A cash flow has been created for an unpaid procurement.' ) );
91+
92+
/**
93+
* Query: we store the procurement now with a paid status
94+
*/
95+
$procurementsDetails[ 'general' ][ 'payment_status' ] = Procurement::PAYMENT_PAID;
96+
$procurementsDetails[ 'general' ][ 'delivery_status' ] = Procurement::DELIVERED;
97+
98+
$response = $this->withSession( $this->app[ 'session' ]->all() )
99+
->json( 'PUT', 'api/nexopos/v4/procurements/' . $procurementId, $procurementsDetails );
100+
101+
$response->assertOk();
102+
79103
/**
80104
* We'll proceed to the verification
81105
* and check if the accounts are valid.

0 commit comments

Comments
 (0)