71 lines
2.0 KiB
PHP
71 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin\Master;
|
|
|
|
use App\Models\Customer;
|
|
use App\Services\Concerns\LogsFormHistory;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class CustomerService
|
|
{
|
|
use LogsFormHistory;
|
|
|
|
public function paginated(int $perPage = 25, string $search = '', string $sort = 'created_at', string $direction = 'desc', array $filters = []): LengthAwarePaginator
|
|
{
|
|
return Customer::query()
|
|
->select(['id', 'name', 'phone_number', 'address'])
|
|
->when($search, fn ($q) => $q->where('name', 'like', "%{$search}%"))
|
|
->orderBy($sort, $direction)
|
|
->paginate($perPage);
|
|
}
|
|
|
|
public function getAll(): Collection
|
|
{
|
|
return Customer::select(['id', 'name'])->orderBy('name')->get();
|
|
}
|
|
|
|
public function store(array $data): Customer
|
|
{
|
|
$customer = Customer::create($data);
|
|
|
|
$this->logCreated($customer, 'Customer', [
|
|
'Nama' => $customer->name,
|
|
'No. Telepon' => $customer->phone_number,
|
|
'Alamat' => $customer->address,
|
|
]);
|
|
|
|
return $customer;
|
|
}
|
|
|
|
public function update(Customer $customer, array $data): Customer
|
|
{
|
|
$oldValues = [
|
|
'Nama' => $customer->name,
|
|
'No. Telepon' => $customer->phone_number,
|
|
'Alamat' => $customer->address,
|
|
];
|
|
|
|
$customer->update($data);
|
|
|
|
$this->logUpdated($customer, 'Customer', $oldValues, [
|
|
'Nama' => $customer->name,
|
|
'No. Telepon' => $customer->phone_number,
|
|
'Alamat' => $customer->address,
|
|
]);
|
|
|
|
return $customer;
|
|
}
|
|
|
|
public function destroy(Customer $customer): bool
|
|
{
|
|
$this->logDeleted($customer, 'Customer', [
|
|
'Nama' => $customer->name,
|
|
'No. Telepon' => $customer->phone_number,
|
|
'Alamat' => $customer->address,
|
|
]);
|
|
|
|
return $customer->delete();
|
|
}
|
|
}
|