Skip to content

Commit 668b7c3

Browse files
committed
Added Agent Sales Seeder + Fix Sold Appliance page
1 parent 78d33f5 commit 668b7c3

13 files changed

+196
-40
lines changed

src/backend/app/Listeners/SoldApplianceListener.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public function __construct(
1515

1616
public function initializeApplianceRates(SoldApplianceDataContainer $soldAppliance): void {
1717
$assetPerson = $soldAppliance->getAssetPerson();
18-
$assetType = $soldAppliance->getAssetType();
1918
$transaction = $soldAppliance->getTransaction();
20-
$asset = $soldAppliance->getAsset();
2119
$buyer = $this->personService->getById($assetPerson->person_id);
2220

2321
$this->applianceRateService->create($assetPerson);

src/backend/app/Models/AgentAssignedAppliances.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace App\Models;
44

55
use App\Models\Base\BaseModel;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Relations\BelongsTo;
78
use Illuminate\Database\Eloquent\Relations\HasMany;
89

910
class AgentAssignedAppliances extends BaseModel {
11+
use HasFactory;
1012
public const RELATION_NAME = 'agent_appliance';
1113
protected $guarded = [];
1214

src/backend/app/Models/AgentSoldAppliance.php

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

55
use App\Models\Base\BaseModel;
66
use App\Models\Person\Person;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89

910
class AgentSoldAppliance extends BaseModel {
11+
use HasFactory;
1012
protected $guarded = [];
1113

1214
public function assignedAppliance(): BelongsTo {

src/backend/app/Observers/AgentSoldApplianceObserver.php

+111-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function created(AgentSoldAppliance $agentSoldAppliance): void {
3636
}
3737
}
3838

39+
public function createdWithFactory(AgentSoldAppliance $agentSoldAppliance): void {
40+
$this->processSaleFromFactory($agentSoldAppliance);
41+
}
42+
3943
private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
4044
$assignedApplianceId = $agentSoldAppliance->agent_assigned_appliance_id;
4145
$assignedAppliance = $this->agentAssignedApplianceService->getById($assignedApplianceId);
@@ -70,7 +74,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
7074
'rate_count' => request()->input('tenure'),
7175
'total_cost' => $assignedAppliance->cost,
7276
'down_payment' => request()->input('down_payment'),
73-
'asset_type_id' => $assignedAppliance->appliance->id,
77+
'asset_id' => $assignedAppliance->appliance->id,
7478
];
7579
$appliancePerson = $this->appliancePersonService->make($appliancePersonData);
7680
$this->agentAppliancePersonService->setAssignee($agent);
@@ -81,7 +85,8 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
8185
$soldApplianceDataContainer = app()->makeWith(
8286
'App\Misc\SoldApplianceDataContainer',
8387
[
84-
'assetType' => $appliance,
88+
'asset' => $appliance,
89+
'assetType' => $appliance->assetType,
8590
'assetPerson' => $appliancePerson,
8691
'transaction' => $transaction,
8792
]
@@ -120,4 +125,108 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
120125
$this->agentCommissionHistoryBalanceService->assign();
121126
$this->agentBalanceHistoryService->save($agentBalanceHistory);
122127
}
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+
}
123232
}

src/backend/app/Services/AgentSoldApplianceService.php

+6
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,10 @@ function ($q) use ($agentId) {
9797
)->latest()
9898
->paginate();
9999
}
100+
101+
public function getAgentsByCustomerId(int $customerId): Collection {
102+
return Agent::whereHas('soldAppliances', function ($query) use ($customerId) {
103+
$query->where('person_id', $customerId);
104+
})->get();
105+
}
100106
}

src/backend/app/Services/ApplianceRateService.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function create($assetPerson, $installmentType = 'monthly'): void {
9393
$this->applianceRate->newQuery()->create(
9494
[
9595
'asset_person_id' => $assetPerson->id,
96-
'rate_cost' => $assetPerson->down_payment,
96+
'rate_cost' => round($assetPerson->down_payment),
9797
'remaining' => 0,
9898
'due_date' => Carbon::parse(date('Y-m-d'))->toDateTimeString(),
9999
'remind' => 0,
@@ -141,7 +141,7 @@ public function getDownPaymentAsAssetRate($assetPerson): ?AssetRate {
141141
/** @var ?AssetRate $result */
142142
$result = $this->applianceRate->newQuery()
143143
->where('asset_person_id', $assetPerson->id)
144-
->where('rate_cost', $assetPerson->down_payment)
144+
->where('rate_cost', round($assetPerson->down_payment))
145145
->where('remaining', 0)
146146
->first();
147147

src/backend/database/factories/AgentAssignedApplianceFactory.php src/backend/database/factories/AgentAssignedAppliancesFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use App\Models\AgentAssignedAppliances;
66
use Illuminate\Database\Eloquent\Factories\Factory;
77

8-
class AgentAssignedApplianceFactory extends Factory {
8+
class AgentAssignedAppliancesFactory extends Factory {
99
protected $model = AgentAssignedAppliances::class;
1010

1111
/**
@@ -16,7 +16,7 @@ class AgentAssignedApplianceFactory extends Factory {
1616
public function definition() {
1717
return [
1818
'agent_id' => $this->faker->numberBetween(1, 10),
19-
'appliance_type_id' => $this->faker->numberBetween(1, 10),
19+
'appliance_id' => $this->faker->numberBetween(1, 10),
2020
'user_id' => $this->faker->numberBetween(1, 10),
2121
'cost' => $this->faker->randomFloat(2, 1, 100),
2222
];

src/backend/database/factories/AgentSoldApplianceFactory.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Database\Factories;
44

5+
use App\Models\AgentSoldAppliance;
56
use Illuminate\Database\Eloquent\Factories\Factory;
67

78
class AgentSoldApplianceFactory extends Factory {
9+
protected $model = AgentSoldAppliance::class;
10+
811
/**
912
* Define the model's default state.
1013
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Agent;
6+
use App\Models\AgentAssignedAppliances;
7+
use App\Models\AgentSoldAppliance;
8+
use App\Models\Asset;
9+
use App\Models\Person\Person;
10+
use App\Observers\AgentSoldApplianceObserver;
11+
use Illuminate\Database\Seeder;
12+
13+
class AgentApplianceSalesSeeder extends Seeder {
14+
/**
15+
* Run the database seeds.
16+
*
17+
* @return void
18+
*/
19+
public function run() {
20+
// Fetch Existing Agents
21+
$agents = Agent::all();
22+
if ($agents->isEmpty()) {
23+
$this->command->warn('No existing agents found. Skipping seeder.');
24+
25+
return;
26+
}
27+
28+
// Fetch Existing Customers
29+
$customers = Person::where('is_customer', true)->get();
30+
if ($customers->isEmpty()) {
31+
$this->command->warn('No existing customers found. Skipping seeder.');
32+
33+
return;
34+
}
35+
36+
// Fetch Existing Assets
37+
$assets = Asset::all();
38+
if ($assets->isEmpty()) {
39+
$this->command->warn('No existing assets found. Skipping seeder.');
40+
41+
return;
42+
}
43+
44+
// Assign Assets to Agents
45+
$assignedAppliances = $agents->flatMap(function ($agent) use ($assets, $customers) {
46+
return AgentAssignedAppliances::factory()->count(3)->create([
47+
'agent_id' => $agent->id,
48+
'appliance_id' => $assets->random()->id, // Use existing assets
49+
'user_id' => $customers->random()->id, // Use existing customers
50+
]);
51+
});
52+
53+
// Simulate Sales and Trigger Observer
54+
$assignedAppliances->each(function ($assignedAppliance) use ($customers) {
55+
$observer = app()->make(AgentSoldApplianceObserver::class);
56+
$agentSoldAppliance = AgentSoldAppliance::factory()->create([
57+
'agent_assigned_appliance_id' => $assignedAppliance->id,
58+
'person_id' => $customers->random()->id,
59+
]);
60+
$observer->createdWithFactory($agentSoldAppliance);
61+
});
62+
63+
$this->command->info('Agent Appliance Sales Seeded Successfully!');
64+
}
65+
}

src/backend/database/seeders/AgentBalanceHistorySeeder.php

-29
This file was deleted.

src/backend/database/seeders/DatabaseSeeder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function run() {
3232
CountrySeeder::class,
3333
SubConnectionTypeSeeder::class,
3434
SmsSeeder::class,
35-
AgentBalanceHistorySeeder::class,
3635
TargetSeeder::class,
3736
PluginsSeeder::class,
37+
AgentApplianceSalesSeeder::class,
3838
]);
3939
} else {
4040
// If the database already includes the Demo data we don't throw an error,

src/frontend/src/modules/Agent/AgentTransactionList.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default {
4949
this.$tc("words.id"),
5050
this.$tc("words.amount"),
5151
this.$tc("words.device"),
52-
this.$tc("words.person"),
52+
this.$tc("words.customer"),
5353
this.$tc("words.date"),
5454
],
5555
}

src/frontend/src/services/AgentSoldApplianceService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class AgentSoldApplianceService {
2323
fromJson(data) {
2424
let soldAppliance = {
2525
id: data.id,
26-
applianceName: data.assigned_appliance.appliance_type.name,
26+
applianceName: data.assigned_appliance.appliance.name,
2727
amount: data.assigned_appliance.cost,
2828
customerName: data.person.name + " " + data.person.surname,
2929
createdAt: data.created_at

0 commit comments

Comments
 (0)