-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAppliancePersonController.php
More file actions
168 lines (147 loc) · 6.93 KB
/
AppliancePersonController.php
File metadata and controls
168 lines (147 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace App\Http\Controllers;
use App\Events\PaymentSuccessEvent;
use App\Events\TransactionSuccessfulEvent;
use App\Http\Resources\ApiResource;
use App\Models\Appliance;
use App\Models\AppliancePerson;
use App\Models\Person\Person;
use App\Services\AddressesService;
use App\Services\AddressGeographicalInformationService;
use App\Services\AppliancePersonService;
use App\Services\ApplianceRateService;
use App\Services\ApplianceService;
use App\Services\CashTransactionService;
use App\Services\DeviceService;
use App\Services\GeographicalInformationService;
use App\Services\UserAppliancePersonService;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AppliancePersonController extends Controller {
public function __construct(
private AppliancePerson $appliancePerson,
private AppliancePersonService $appliancePersonService,
private UserAppliancePersonService $userAppliancePersonService,
private UserService $userService,
private DeviceService $deviceService,
private AddressesService $addressesService,
private GeographicalInformationService $geographicalInformationService,
private AddressGeographicalInformationService $addressGeographicalInformationService,
private CashTransactionService $cashTransactionService,
private ApplianceService $applianceService,
private ApplianceRateService $applianceRateService,
) {}
/**
* Store a newly created resource in storage.
*/
public function store(
Appliance $appliance,
Request $request,
): ApiResource {
try {
$userId = $request->input('user_id');
$applianceId = $request->input('id');
$personId = $request->input('person_id');
$cost = (int) $request->input('cost');
$installmentCount = (int) $request->input('rate');
$downPayment = (float) $request->input('down_payment');
$deviceSerial = $request->input('device_serial');
$addressData = $request->input('address');
$user = $this->userService->getById($userId);
$appliance = $this->applianceService->getById($applianceId);
$installmentType = $request->input('rate_type');
$points = $request->input('points');
DB::connection('tenant')->beginTransaction();
$appliancePerson = $this->appliancePersonService->make([
'appliance_id' => $applianceId,
'person_id' => $personId,
'total_cost' => $cost,
'rate_count' => $installmentCount,
'down_payment' => $downPayment,
'device_serial' => $deviceSerial,
]);
$this->userAppliancePersonService->setAssigned($appliancePerson);
$this->userAppliancePersonService->setAssignee($user);
$this->userAppliancePersonService->assign();
$this->appliancePersonService->save($appliancePerson);
$preferredPrice = $appliance->price;
if ($cost !== $preferredPrice) {
$this->appliancePersonService->createLogForSoldAppliance($appliancePerson, $cost, $preferredPrice);
}
$this->applianceRateService->create($appliancePerson, $installmentType);
if ($deviceSerial) {
$device = $this->deviceService->getBySerialNumber($deviceSerial);
$this->deviceService->update($device, ['person_id' => $personId]);
$appliancePerson->device_serial = $deviceSerial;
$address = $this->addressesService->make([
'street' => $addressData['street'],
'village_id' => $addressData['village_id'],
]);
// Attach the new address to the person rather than the device.
$this->addressesService->assignAddressToOwner($appliancePerson->person, $address);
$geographicalInformation = $this->geographicalInformationService->make([
'points' => $points,
]);
$this->addressGeographicalInformationService->setAssigned($geographicalInformation);
$this->addressGeographicalInformationService->setAssignee($address);
$this->addressGeographicalInformationService->assign();
$this->geographicalInformationService->save($geographicalInformation);
}
if ($downPayment > 0) {
$sender = isset($addressData) ? $addressData['phone'] : '-';
$transaction = $this->cashTransactionService->createCashTransaction(
$user->id,
$downPayment,
$sender,
$deviceSerial
);
$applianceRate = $this->applianceRateService->getDownPaymentAsApplianceRate($appliancePerson);
event(new PaymentSuccessEvent(
amount: (int) $transaction->amount,
paymentService: 'web',
paymentType: 'down payment',
sender: $transaction->sender,
paidFor: $applianceRate,
payer: $appliancePerson->person,
transaction: $transaction,
));
event(new TransactionSuccessfulEvent($transaction));
}
DB::connection('tenant')->commit();
return ApiResource::make($appliancePerson);
} catch (\Exception $e) {
DB::connection('tenant')->rollBack();
throw new \Exception($e->getMessage(), (int) $e->getCode(), $e);
}
}
/**
* Display the specified resource.
*/
public function index(Person $person, Request $request): ApiResource {
$appliances = $this->appliancePerson::with('appliance.applianceType', 'rates.logs', 'logs.owner')
->where('person_id', $person->id)
->get();
return ApiResource::make($appliances);
}
public function show(int $applianceId): ApiResource {
$appliance = $this->appliancePersonService->getApplianceDetails($applianceId);
return ApiResource::make($appliance);
}
public function getRates(int $appliancePersonId, Request $request): ApiResource {
$perPage = $request->get('per_page', 15);
$appliancePerson = $this->appliancePerson::findOrFail($appliancePersonId);
return ApiResource::make($appliancePerson->rates()
->with('logs.owner')
->orderBy('due_date', 'asc')
->paginate($perPage));
}
public function getLogs(int $appliancePersonId, Request $request): ApiResource {
$perPage = $request->get('per_page', 10);
$appliancePerson = $this->appliancePerson::findOrFail($appliancePersonId);
return ApiResource::make($appliancePerson->logs()
->with('owner')
->orderBy('created_at', 'desc')
->paginate($perPage));
}
}