1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests\Customer; |
4
|
|
|
|
5
|
|
|
use App\Enum\Gender; |
6
|
|
|
use App\Infrastructure\Foundation\Http\FormRequest; |
7
|
|
|
use App\Models\Customer; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
10
|
|
|
use Illuminate\Validation\Rule; |
11
|
|
|
use Propaganistas\LaravelPhone\PhoneNumber; |
12
|
|
|
|
13
|
|
|
class UpdateRequest extends FormRequest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritDoc} |
17
|
|
|
*/ |
18
|
|
|
public static function getRules() |
19
|
|
|
{ |
20
|
|
|
$phoneRule = function () { |
21
|
|
|
return ['required', 'string', 'phone:ID', function ($attribute, $phone, $fail) { |
22
|
|
|
$country = $attribute === 'phone' |
23
|
|
|
? request()->input('phone_country', env('PHONE_COUNTRY', 'ID')) |
24
|
|
|
: request()->input('whatsapp_phone_country', env('PHONE_COUNTRY', 'ID')); |
25
|
|
|
|
26
|
|
|
$user = Customer::where($attribute, PhoneNumber::make($phone, $country)->formatE164()) |
27
|
|
|
->where(request()->route('customer')->getKeyName(), '!=', request()->route('customer')->getKey()) |
28
|
|
|
->count(); |
29
|
|
|
|
30
|
|
|
if ($user > 0) { |
31
|
|
|
$fail(trans('validation.unique', ['attribute' => static::getAttributes()[$attribute]])); |
32
|
|
|
} |
33
|
|
|
}]; |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
return [ |
37
|
|
|
'username' => 'required|string|max:255', |
38
|
|
|
'fullname' => 'required|string|max:255', |
39
|
|
|
'gender' => 'sometimes|nullable|enum:' . Gender::class, |
40
|
|
|
'email' => ['required', 'string', 'email', 'max:255', function ($attribute, $email, $fail) { |
41
|
|
|
$validator = Validator::make(compact('email'), [ |
42
|
|
|
$attribute => Rule::unique(Customer::class)->ignore($email, $attribute), |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
if ($validator->fails()) { |
46
|
|
|
$fail(trans('validation.unique', compact('attribute'))); |
47
|
|
|
} |
48
|
|
|
}], |
49
|
|
|
'phone_country' => 'sometimes|in:ID', |
50
|
|
|
'phone' => value($phoneRule), |
51
|
|
|
'whatsapp_phone_country' => 'sometimes|in:ID', |
52
|
|
|
'whatsapp_phone' => value($phoneRule), |
53
|
|
|
'account_number' => ['required_without:identitycard_number'] + (request()->filled('account_number') ? ['numeric'] : []), |
54
|
|
|
'identitycard_number' => ['required_without:account_number'] + (request()->filled('identitycard_number') ? ['numeric'] : []), |
55
|
|
|
'identitycard_image' => 'sometimes|nullable|image', |
56
|
|
|
'location_latitude' => 'required|numeric', |
57
|
|
|
'location_longitude' => 'required|numeric', |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public static function getAttributes() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'username' => trans('Username'), |
68
|
|
|
'fullname' => trans('Full name'), |
69
|
|
|
'gender' => trans('Gender'), |
70
|
|
|
'email' => trans('Email'), |
71
|
|
|
'phone_country' => trans('Phone Country'), |
72
|
|
|
'phone' => trans('Phone Number'), |
73
|
|
|
'whatsapp_phone_country' => trans('Whatsapp Phone Country'), |
74
|
|
|
'whatsapp_phone' => trans('Whatsapp Phone Number'), |
75
|
|
|
'account_number' => trans('Account Number'), |
76
|
|
|
'identitycard_number' => trans('Identity Card Number'), |
77
|
|
|
'identitycard_image' => trans('Identity Card Image'), |
78
|
|
|
'location_latitude' => trans('Latitude'), |
79
|
|
|
'location_longitude' => trans('Longitude'), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update the image file from the incoming request. |
85
|
|
|
* |
86
|
|
|
* @param string $key |
87
|
|
|
* @return string|null |
88
|
|
|
*/ |
89
|
|
|
public function updateImage(string $key = 'identitycard_image'): ?string |
90
|
|
|
{ |
91
|
|
|
if (!$this->hasFile($key)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @var \App\Models\Customer $model */ |
96
|
|
|
$model = $this->route('customer'); |
97
|
|
|
|
98
|
|
|
if ($model->getRawOriginal('identitycard_image') && $this->hasFile($key)) { |
99
|
|
|
Storage::delete(Customer::IDENTITYCARD_IMAGE_PATH . '/' . $model->getRawOriginal('identitycard_image')); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$file = $this->file($key); |
103
|
|
|
|
104
|
|
|
$file->storeAs( |
105
|
|
|
Customer::IDENTITYCARD_IMAGE_PATH, |
106
|
|
|
$filename = ($this->input('value') . '.' . $file->getClientOriginalExtension()) |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
return $filename; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|