Files
kulakpos_web/public/restaurant/Modules/Restaurant/Repositories/SupplierRepository.php

230 lines
6.6 KiB
PHP
Raw Normal View History

2026-03-15 17:08:23 +07:00
<?php
namespace Modules\Restaurant\Repositories;
use App\Abstracts\EntityRepository;
use Exception;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Modules\Restaurant\Models\Supplier;
use Symfony\Component\HttpFoundation\Response;
class SupplierRepository extends EntityRepository
{
public string $table = Supplier::TABLE_NAME;
protected array $fillableColumns = [
'restaurant_id',
'name',
'phone',
'company_name',
'contact_person',
'alternate_phone',
'email',
'website',
'image',
'address',
'city',
'state',
'country',
'postal_code',
'tax_number',
'bank_name',
'bank_account_name',
'bank_account_number',
'ifsc_code',
'opening_balance',
'opening_balance_date',
'due',
'balance',
'supply_type',
'payment_terms',
'credit_limit',
'rating',
'notes',
'contract_file',
'trade_license',
'nid_number',
'status',
'created_at',
'updated_at',
'deleted_at',
];
protected function getQuery(): Builder
{
return parent::getQuery();
}
protected function getFilterData(array $filterData = []): array
{
$defaultArgs = [
'perPage' => 10,
'search' => '',
'orderBy' => 'id',
'order' => 'desc',
'with_deleted' => false,
];
return array_merge($defaultArgs, $filterData);
}
private function getSupplierQuery(): Builder
{
return $this->getQuery()
->select(
"{$this->table}.id",
"{$this->table}.restaurant_id",
"{$this->table}.name",
"{$this->table}.phone",
"{$this->table}.company_name",
"{$this->table}.contact_person",
"{$this->table}.alternate_phone",
"{$this->table}.email",
"{$this->table}.website",
"{$this->table}.image",
"{$this->table}.address",
"{$this->table}.city",
"{$this->table}.state",
"{$this->table}.country",
"{$this->table}.postal_code",
"{$this->table}.tax_number",
"{$this->table}.bank_name",
"{$this->table}.bank_account_name",
"{$this->table}.bank_account_number",
"{$this->table}.ifsc_code",
"{$this->table}.opening_balance",
"{$this->table}.opening_balance_date",
"{$this->table}.due",
"{$this->table}.balance",
"{$this->table}.supply_type",
"{$this->table}.payment_terms",
"{$this->table}.credit_limit",
"{$this->table}.rating",
"{$this->table}.notes",
"{$this->table}.contract_file",
"{$this->table}.trade_license",
"{$this->table}.nid_number",
"{$this->table}.status",
"{$this->table}.created_at",
"{$this->table}.updated_at",
"{$this->table}.deleted_at",
);
}
protected function filterSearchQuery(Builder|EloquentBuilder $query, string $searchedText): Builder
{
$searchable = "%$searchedText%";
return $query->where("{$this->table}.name", 'LIKE', $searchable)
->orWhere("{$this->table}.status", 'LIKE', $searchable);
}
public function getAll(array $filterData = []): Paginator
{
$filter = $this->getFilterData($filterData);
$query = $this->getSupplierQuery();
if (! $filter['with_deleted']) {
$query->whereNull("{$this->table}.deleted_at");
}
if (! empty($filter['search'])) {
$query = $this->filterSearchQuery($query, $filter['search']);
}
return $query
->orderBy($filter['orderBy'], $filter['order'])
->paginate($filter['perPage']);
}
public function getCount(array $filterData = []): int
{
$filter = $this->getFilterData($filterData);
$query = $this->getQuery();
if (! $filter['with_deleted']) {
$query->whereNull("{$this->table}.deleted_at");
}
return $query->count();
}
/**
* @throws Exception
*/
public function getByColumn(string $columnName, $columnValue, array $selects = ['*']): ?object
{
$item = $this->getSupplierQuery()
->where($columnName, $columnValue)
->first($selects);
if (empty($item)) {
throw new Exception(
$this->getExceptionMessage(static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE),
Response::HTTP_NOT_FOUND
);
}
return $item;
}
/**
* @throws Exception
*/
public function create(array $data): object
{
$data = $this->prepareForDB($data);
$id = $this->getQuery()->insertGetId($data);
return Supplier::find($id);
}
/**
* @throws Exception
*/
public function update(int $id, array $data): object
{
$item = Supplier::findOrFail($id);
$data = $this->prepareForDB($data, $item);
parent::update($id, $data);
return $this->getById($id);
}
/**
* @throws Exception
*/
public function prepareForDB(array $data, ?object $item = null): array
{
$data = parent::prepareForDB($data, $item);
if (empty($item)) {
$data['created_at'] = now();
$data['restaurant_id'] = $this->getCurrentRestaurantId();
$data['status'] = 1;
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
$data['image'] = fileUploader('suppliers/', 'png', $data['image']);
}
} else {
if (! empty($data['image']) && $data['image'] instanceof \Illuminate\Http\UploadedFile) {
$data['image'] = fileUploader('suppliers/', 'png', $data['image'], $item->image);
}
$data['updated_at'] = now();
}
return $data;
}
protected function getExceptionMessages(): array
{
return [
static::MESSAGE_ITEM_DOES_NOT_EXIST_MESSAGE => 'Supplier does not exist.',
static::MESSAGE_ITEM_COULD_NOT_BE_DELETED => 'Supplier could not be deleted.',
];
}
}