152 lines
5.6 KiB
PHP
152 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Order;
|
|
|
|
use App\Models\Order;
|
|
use App\Repositories\BaseRepository;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class OrderRepository extends BaseRepository implements OrderRepositoryInterface
|
|
{
|
|
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);
|
|
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']);
|
|
}
|
|
}
|
|
if (isset($data['status']) && ! empty($data['status']) && $data['status'] != 'full') {
|
|
$model = $model->where("status", match ($data['status']) {
|
|
'active' => 1,
|
|
'pending' => 0,
|
|
'processing' => 3,
|
|
'cancel' => 2,
|
|
});
|
|
}
|
|
|
|
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 updateBy(string $key = '', string|int $value = '', array $data = [])
|
|
{
|
|
if (empty($key) || empty($value)) {
|
|
return false;
|
|
}
|
|
return $this->model->where($key, $value)->update($data);
|
|
}
|
|
|
|
public function findByOrderNum(string $orderNum)
|
|
{
|
|
return Order::with([
|
|
'orderItems.product',
|
|
'orderItems.os',
|
|
'orderItems.service',
|
|
'moduleVmwares.ipAddress',
|
|
'user',
|
|
'customer',
|
|
'payment',
|
|
'discount',
|
|
'invoice',
|
|
])->where('order_num', $orderNum)->first();
|
|
}
|
|
}
|