Skip to content

Commit 6ea5040

Browse files
committed
Add down payment, tenure, and first payment date fields to agent sold appliances
1 parent 668b7c3 commit 6ea5040

File tree

5 files changed

+63
-124
lines changed

5 files changed

+63
-124
lines changed

src/backend/app/Observers/AgentSoldApplianceObserver.php

+8-118
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,10 @@ public function __construct(
3131
) {}
3232

3333
public function created(AgentSoldAppliance $agentSoldAppliance): void {
34-
if (request()->all()) {
35-
$this->processSaleIfIsNotCreatedByFactory($agentSoldAppliance);
36-
}
34+
$this->processSale($agentSoldAppliance);
3735
}
3836

39-
public function createdWithFactory(AgentSoldAppliance $agentSoldAppliance): void {
40-
$this->processSaleFromFactory($agentSoldAppliance);
41-
}
42-
43-
private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
37+
private function processSale($agentSoldAppliance) {
4438
$assignedApplianceId = $agentSoldAppliance->agent_assigned_appliance_id;
4539
$assignedAppliance = $this->agentAssignedApplianceService->getById($assignedApplianceId);
4640
$appliance = $assignedAppliance->appliance()->first();
@@ -56,7 +50,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
5650

5751
// assign agent transaction to transaction
5852
$transactionData = [
59-
'amount' => request()->input('down_payment') ?: 0,
53+
'amount' => $agentSoldAppliance->down_payment ?: 0,
6054
'sender' => $agent->device_id,
6155
'message' => '-',
6256
];
@@ -69,11 +63,11 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
6963

7064
// assign agent to appliance person
7165
$appliancePersonData = [
72-
'person_id' => request()->input('person_id'),
73-
'first_payment_date' => request()->input('first_payment_date'),
74-
'rate_count' => request()->input('tenure'),
66+
'person_id' => $agentSoldAppliance->person_id,
67+
'first_payment_date' => $agentSoldAppliance->first_payment_date,
68+
'rate_count' => $agentSoldAppliance->tenure,
7569
'total_cost' => $assignedAppliance->cost,
76-
'down_payment' => request()->input('down_payment'),
70+
'down_payment' => $agentSoldAppliance->down_payment,
7771
'asset_id' => $assignedAppliance->appliance->id,
7872
];
7973
$appliancePerson = $this->appliancePersonService->make($appliancePersonData);
@@ -97,7 +91,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
9791
// assign agent assigned appliance to agent balance history
9892
$agentBalanceHistoryData = [
9993
'agent_id' => $agent->id,
100-
'amount' => (-1 * request()->input('down_payment')),
94+
'amount' => (-1 * $agentSoldAppliance->down_payment),
10195
'transaction_id' => $transaction->id,
10296
'available_balance' => $agent->balance,
10397
'due_to_supplier' => $agent->due_to_energy_supplier,
@@ -125,108 +119,4 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
125119
$this->agentCommissionHistoryBalanceService->assign();
126120
$this->agentBalanceHistoryService->save($agentBalanceHistory);
127121
}
128-
129-
/**
130-
* Process a sale triggered by a factory instead of a user request.
131-
*
132-
* This function handles the creation of transactions, appliance assignments,
133-
* agent commissions, and balance updates when an agent sells an appliance.
134-
* Unlike the standard process, it does not rely on user input from a request
135-
* but instead generates the necessary data programmatically.
136-
*
137-
* @param AgentSoldAppliance $agentSoldAppliance the sold appliance instance created by a factory
138-
*
139-
* @return void
140-
*/
141-
private function processSaleFromFactory(AgentSoldAppliance $agentSoldAppliance) {
142-
$assignedAppliance = $this->agentAssignedApplianceService->getById($agentSoldAppliance->agent_assigned_appliance_id);
143-
$appliance = $assignedAppliance->appliance()->first();
144-
$agent = $this->agentService->getById($assignedAppliance->agent_id);
145-
146-
// Simulated factory data (instead of using request())
147-
$factoryData = [
148-
'person_id' => $agentSoldAppliance->person_id,
149-
'first_payment_date' => now(),
150-
'tenure' => rand(6, 24), // Random tenure for factory generation
151-
'down_payment' => $assignedAppliance->cost * 0.2, // 20% down payment
152-
];
153-
154-
// Create agent transaction
155-
$agentTransactionData = [
156-
'agent_id' => $agent->id,
157-
'device_id' => $agent->device_id,
158-
'status' => 1,
159-
];
160-
$agentTransaction = $this->agentTransactionService->create($agentTransactionData);
161-
162-
// Assign agent transaction to transaction
163-
$transactionData = [
164-
'amount' => $factoryData['down_payment'],
165-
'sender' => $agent->device_id,
166-
'message' => '-',
167-
];
168-
$transaction = $this->transactionService->make($transactionData);
169-
$this->agentTransactionTransactionService->setAssignee($agentTransaction);
170-
$this->agentTransactionTransactionService->setAssigned($transaction);
171-
$this->agentTransactionTransactionService->assign();
172-
$this->transactionService->save($transaction);
173-
174-
// Assign agent to appliance person
175-
$appliancePersonData = [
176-
'person_id' => $factoryData['person_id'],
177-
'first_payment_date' => $factoryData['first_payment_date'],
178-
'rate_count' => $factoryData['tenure'],
179-
'total_cost' => $assignedAppliance->cost,
180-
'down_payment' => $factoryData['down_payment'],
181-
'asset_id' => $assignedAppliance->appliance->id,
182-
];
183-
$appliancePerson = $this->appliancePersonService->make($appliancePersonData);
184-
$this->agentAppliancePersonService->setAssignee($agent);
185-
$this->agentAppliancePersonService->setAssigned($appliancePerson);
186-
$this->agentAppliancePersonService->assign();
187-
$this->appliancePersonService->save($appliancePerson);
188-
189-
$soldApplianceDataContainer = app()->makeWith(
190-
'App\Misc\SoldApplianceDataContainer',
191-
[
192-
'asset' => $appliance,
193-
'assetType' => $appliance->assetType,
194-
'assetPerson' => $appliancePerson,
195-
'transaction' => $transaction,
196-
]
197-
);
198-
199-
event('appliance.sold', $soldApplianceDataContainer);
200-
201-
// Assign agent assigned appliance to agent balance history
202-
$agentBalanceHistoryData = [
203-
'agent_id' => $agent->id,
204-
'amount' => (-1 * $factoryData['down_payment']),
205-
'transaction_id' => $transaction->id,
206-
'available_balance' => $agent->balance,
207-
'due_to_supplier' => $agent->due_to_energy_supplier,
208-
];
209-
$agentBalanceHistory = $this->agentBalanceHistoryService->make($agentBalanceHistoryData);
210-
$this->agentAssignedApplianceHistoryBalanceService->setAssignee($assignedAppliance);
211-
$this->agentAssignedApplianceHistoryBalanceService->setAssigned($agentBalanceHistory);
212-
$this->agentAssignedApplianceHistoryBalanceService->assign();
213-
$this->agentBalanceHistoryService->save($agentBalanceHistory);
214-
215-
// Create agent commission
216-
$agentCommission = $this->agentCommissionService->getById($agent->agent_commission_id);
217-
218-
// Assign agent commission to agent balance history
219-
$agentBalanceHistoryData = [
220-
'agent_id' => $agent->id,
221-
'amount' => ($assignedAppliance->cost * $agentCommission->appliance_commission),
222-
'transaction_id' => $transaction->id,
223-
'available_balance' => $agent->commission_revenue,
224-
'due_to_supplier' => $agent->due_to_energy_supplier,
225-
];
226-
$agentBalanceHistory = $this->agentBalanceHistoryService->make($agentBalanceHistoryData);
227-
$this->agentCommissionHistoryBalanceService->setAssignee($agentCommission);
228-
$this->agentCommissionHistoryBalanceService->setAssigned($agentBalanceHistory);
229-
$this->agentCommissionHistoryBalanceService->assign();
230-
$this->agentBalanceHistoryService->save($agentBalanceHistory);
231-
}
232122
}

src/backend/database/factories/AgentSoldApplianceFactory.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function definition() {
1717
return [
1818
'person_id' => $this->faker->randomNumber(),
1919
'agent_assigned_appliance_id' => $this->faker->randomNumber(),
20+
'down_payment' => 0,
21+
'tenure' => $this->faker->randomNumber(2, 10),
22+
'first_payment_date' => date('Y-m-d', strtotime('+1 month')),
2023
];
2124
}
2225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up() {
14+
Schema::connection('tenant')->table('agent_sold_appliances', function (Blueprint $table) {
15+
if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'down_payment')) {
16+
$table->double('down_payment')->nullable();
17+
}
18+
19+
if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'tenure')) {
20+
$table->integer('tenure');
21+
}
22+
23+
if (!Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'first_payment_date')) {
24+
$table->date('first_payment_date')->nullable();
25+
}
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down() {
35+
Schema::connection('tenant')->table('agent_sold_appliances', function (Blueprint $table) {
36+
if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'down_payment')) {
37+
$table->dropColumn('down_payment');
38+
}
39+
40+
if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'tenure')) {
41+
$table->dropColumn('tenure');
42+
}
43+
44+
if (Schema::connection('tenant')->hasColumn('agent_sold_appliances', 'first_payment_date')) {
45+
$table->dropColumn('first_payment_date');
46+
}
47+
});
48+
}
49+
};

src/backend/database/seeders/AgentApplianceSalesSeeder.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use App\Models\AgentSoldAppliance;
88
use App\Models\Asset;
99
use App\Models\Person\Person;
10-
use App\Observers\AgentSoldApplianceObserver;
1110
use Illuminate\Database\Seeder;
1211

1312
class AgentApplianceSalesSeeder extends Seeder {
@@ -52,12 +51,10 @@ public function run() {
5251

5352
// Simulate Sales and Trigger Observer
5453
$assignedAppliances->each(function ($assignedAppliance) use ($customers) {
55-
$observer = app()->make(AgentSoldApplianceObserver::class);
56-
$agentSoldAppliance = AgentSoldAppliance::factory()->create([
54+
AgentSoldAppliance::factory()->create([
5755
'agent_assigned_appliance_id' => $assignedAppliance->id,
5856
'person_id' => $customers->random()->id,
5957
]);
60-
$observer->createdWithFactory($agentSoldAppliance);
6158
});
6259

6360
$this->command->info('Agent Appliance Sales Seeded Successfully!');

src/backend/tests/Feature/CreateEnvironments.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use App\Models\Address\Address;
66
use App\Models\GeographicalInformation;
7-
use Database\Factories\AgentAssignedApplianceFactory;
7+
use Database\Factories\AgentAssignedAppliancesFactory;
88
use Database\Factories\AgentBalanceHistoryFactory;
99
use Database\Factories\AgentCommissionFactory;
1010
use Database\Factories\AgentFactory;
@@ -499,7 +499,7 @@ protected function createAssetType($assetTypeCount = 1) {
499499
protected function createAssignedAppliances($applianceCount = 1) {
500500
$this->createAssetType($applianceCount);
501501
while ($applianceCount > 0) {
502-
$assignedAppliance = AgentAssignedApplianceFactory::new()->create([
502+
$assignedAppliance = AgentAssignedAppliancesFactory::new()->create([
503503
'agent_id' => $this->getRandomIdFromList($this->agents),
504504
'appliance_type_id' => $this->getRandomIdFromList($this->assetTypes),
505505
'user_id' => $this->user->id,

0 commit comments

Comments
 (0)