@@ -36,6 +36,10 @@ public function created(AgentSoldAppliance $agentSoldAppliance): void {
36
36
}
37
37
}
38
38
39
+ public function createdWithFactory (AgentSoldAppliance $ agentSoldAppliance ): void {
40
+ $ this ->processSaleFromFactory ($ agentSoldAppliance );
41
+ }
42
+
39
43
private function processSaleIfIsNotCreatedByFactory ($ agentSoldAppliance ) {
40
44
$ assignedApplianceId = $ agentSoldAppliance ->agent_assigned_appliance_id ;
41
45
$ assignedAppliance = $ this ->agentAssignedApplianceService ->getById ($ assignedApplianceId );
@@ -70,7 +74,7 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
70
74
'rate_count ' => request ()->input ('tenure ' ),
71
75
'total_cost ' => $ assignedAppliance ->cost ,
72
76
'down_payment ' => request ()->input ('down_payment ' ),
73
- 'asset_type_id ' => $ assignedAppliance ->appliance ->id ,
77
+ 'asset_id ' => $ assignedAppliance ->appliance ->id ,
74
78
];
75
79
$ appliancePerson = $ this ->appliancePersonService ->make ($ appliancePersonData );
76
80
$ this ->agentAppliancePersonService ->setAssignee ($ agent );
@@ -81,7 +85,8 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
81
85
$ soldApplianceDataContainer = app ()->makeWith (
82
86
'App\Misc\SoldApplianceDataContainer ' ,
83
87
[
84
- 'assetType ' => $ appliance ,
88
+ 'asset ' => $ appliance ,
89
+ 'assetType ' => $ appliance ->assetType ,
85
90
'assetPerson ' => $ appliancePerson ,
86
91
'transaction ' => $ transaction ,
87
92
]
@@ -120,4 +125,108 @@ private function processSaleIfIsNotCreatedByFactory($agentSoldAppliance) {
120
125
$ this ->agentCommissionHistoryBalanceService ->assign ();
121
126
$ this ->agentBalanceHistoryService ->save ($ agentBalanceHistory );
122
127
}
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
+ }
123
232
}
0 commit comments