@@ -31,16 +31,10 @@ public function __construct(
31
31
) {}
32
32
33
33
public function created (AgentSoldAppliance $ agentSoldAppliance ): void {
34
- if (request ()->all ()) {
35
- $ this ->processSaleIfIsNotCreatedByFactory ($ agentSoldAppliance );
36
- }
34
+ $ this ->processSale ($ agentSoldAppliance );
37
35
}
38
36
39
- public function createdWithFactory (AgentSoldAppliance $ agentSoldAppliance ): void {
40
- $ this ->processSaleFromFactory ($ agentSoldAppliance );
41
- }
42
-
43
- private function processSaleIfIsNotCreatedByFactory ($ agentSoldAppliance ) {
37
+ private function processSale ($ agentSoldAppliance ) {
44
38
$ assignedApplianceId = $ agentSoldAppliance ->agent_assigned_appliance_id ;
45
39
$ assignedAppliance = $ this ->agentAssignedApplianceService ->getById ($ assignedApplianceId );
46
40
$ appliance = $ assignedAppliance ->appliance ()->first ();
@@ -56,7 +50,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
56
50
57
51
// assign agent transaction to transaction
58
52
$ transactionData = [
59
- 'amount ' => request ()-> input ( ' down_payment ' ) ?: 0 ,
53
+ 'amount ' => $ agentSoldAppliance -> down_payment ?: 0 ,
60
54
'sender ' => $ agent ->device_id ,
61
55
'message ' => '- ' ,
62
56
];
@@ -69,11 +63,11 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
69
63
70
64
// assign agent to appliance person
71
65
$ 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 ,
75
69
'total_cost ' => $ assignedAppliance ->cost ,
76
- 'down_payment ' => request ()-> input ( ' down_payment ' ) ,
70
+ 'down_payment ' => $ agentSoldAppliance -> down_payment ,
77
71
'asset_id ' => $ assignedAppliance ->appliance ->id ,
78
72
];
79
73
$ appliancePerson = $ this ->appliancePersonService ->make ($ appliancePersonData );
@@ -97,7 +91,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
97
91
// assign agent assigned appliance to agent balance history
98
92
$ agentBalanceHistoryData = [
99
93
'agent_id ' => $ agent ->id ,
100
- 'amount ' => (-1 * request ()-> input ( ' down_payment ' ) ),
94
+ 'amount ' => (-1 * $ agentSoldAppliance -> down_payment ),
101
95
'transaction_id ' => $ transaction ->id ,
102
96
'available_balance ' => $ agent ->balance ,
103
97
'due_to_supplier ' => $ agent ->due_to_energy_supplier ,
@@ -125,108 +119,4 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
125
119
$ this ->agentCommissionHistoryBalanceService ->assign ();
126
120
$ this ->agentBalanceHistoryService ->save ($ agentBalanceHistory );
127
121
}
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
- }
232
122
}
0 commit comments