Files

169 lines
6.7 KiB
PHP

<?php
namespace App\Repositories\Order;
use App\Support\Billing\BillingDuplicateDetector;
use App\Models\Order;
use App\Repositories\BaseRepository;
use App\Repositories\Concerns\FiltersBillingRecords;
use Illuminate\Support\Carbon;
class OrderRepository extends BaseRepository implements OrderRepositoryInterface
{
use FiltersBillingRecords;
public function getModel()
{
return Order::class;
}
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'], bool $returnQuery = false)
{
$model = $this->model->with($relationships)->select($select)->marked($data);
if (isset($data['client']) && ! empty($data['client'])) {
$model = $model->where("customer_id", $data['client']);
}
if (isset($data['id']) && $data['id']) {
$model = $model->where("id", $data['id']);
}
if (isset($data['order_num']) && $data['order_num']) {
$model = $model->where("order_num", $data['order_num']);
}
if (isset($data['order_amount']) && $data['order_amount']) {
$model = $model->where("amount", $data['order_amount']);
}
if (isset($data['order_date']) && $data['order_date']) {
$date = explode("-", $data['order_date']);
$model = $model->whereBetween('date', [Carbon::parse($date[0])->timestamp, Carbon::parse($date[1])->timestamp]);
}
if (isset($data['invoice_id'])) {
if (is_array($data['invoice_id'])) {
$model = $model->where(function ($query) use ($data) {
if (isset($data['invoice_id']['condition'])) {
switch (strtoupper($data['invoice_id']['condition'])) {
case "IN":
$query->whereIn($data['invoice_id']['key'], $data['invoice_id']['value']);
break;
case "NOTIN":
$query->whereNotIn($data['invoice_id']['key'], $data['invoice_id']['value']);
break;
default:
$query->where($data['invoice_id']['key'], $data['invoice_id']['condition'], $data['invoice_id']['value']);
break;
}
} else {
$query->where($data['invoice_id']['key'], $data['invoice_id']['value']);
}
});
} else {
$model = $model->where("invoice_id", $data['invoice_id']);
}
}
$model = $this->whereStatus($model, $data['status'] ?? null, ['active' => 1, 'pending' => 0, 'processing' => 3, 'cancel' => 2]);
$model = $this->whereCreatedBetween($model, $data['date_from'] ?? null, $data['date_to'] ?? null);
$model = $this->whereIsDuplicate($model, $data['duplicates'] ?? null, BillingDuplicateDetector::orders(...));
if (! empty($data['customer_id'])) {
$model = $model->where('customer_id', $data['customer_id']);
}
if (! empty($data['payment_method'])) {
$model = $model->where('payment_method', $data['payment_method']);
}
if (isset($data['filter']) && $data['filter'] !== '') {
$search = $data['filter'];
$model = $model->where(function ($query) use ($search) {
$query->where('order_num', 'like', "%{$search}%")
->orWhereHas('customer', function ($query) use ($search) {
$query->where('fullname', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
});
$this->whereOrderItemNameLike($query, 'orderItems', $search);
$this->whereContractFileLike($query, 'invoices.attachments', $search);
});
}
$model = $this->applySort($model, $data['sort_by'] ?? null, $data['sort_order'] ?? null, [
'order_num', 'sub_total', 'discount_amount', 'total', 'status', 'created_at', 'updated_at', 'customer',
]);
if (count($withCount) > 0) {
$model = $model->withCount($withCount);
}
if ($returnQuery) {
return $model;
}
if (isset($data['limit']) && $data['limit']) {
return $model->latest('id')->take($data['limit'])->get();
}
if (isset($data['page'])) {
return $model->paginate(15);
}
return $model->get();
}
public function findWithRelationships(int $id = 0, array $relationships = [], array $withCount = [], array $withSum = [])
{
$model = $this->model;
if (count($relationships) > 0) {
foreach ($relationships as $relationship) {
if (method_exists($model, $relationship)) {
$model = $model->with($relationship);
}
}
}
// Xử lý withCount
if (count($withCount) > 0) {
foreach ($withCount as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withCount([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
]);
}
} else {
$model = $model->withCount($key);
}
}
}
// Xử lý withSum
if (count($withSum) > 0) {
foreach ($withSum as $key => $items) {
if (is_array($items)) {
foreach ($items as $alias => $item) {
$model = $model->withSum([
$key . ' as ' . $alias => function ($query) use ($item) {
$query->where($item['key'], $item['condition'], $item['value']);
}
], $item['sum']);
}
} else {
$model = $model->withSum($key);
}
}
}
return $model->find($id);
}
public function findByOrderNum(string $orderNum)
{
return Order::with([
'orderItems.product',
'orderItems.os',
'orderItems.service',
'moduleVmwares.ipAddress',
'user',
'customer',
'payment',
'discount',
'invoices.invoiceItems.product',
'invoices.attachments',
'invoices.payment',
])->where('order_num', $orderNum)->first();
}
}