172 lines
6.7 KiB
PHP
172 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\InvoiceItem;
|
|
|
|
use App\Models\InvoiceItem;
|
|
use App\Repositories\BaseRepository;
|
|
use App\Repositories\Module\Hosting\HostingRepositoryInterface;
|
|
use App\Repositories\Module\Vmware\VmwareRepositoryInterface;
|
|
|
|
class InvoiceItemRepository extends BaseRepository implements InvoiceItemRepositoryInterface
|
|
{
|
|
public function __construct(
|
|
public VmwareRepositoryInterface $vmwareRepository,
|
|
public HostingRepositoryInterface $hostingRepository
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
public function getModel()
|
|
{
|
|
return InvoiceItem::class;
|
|
}
|
|
public function getItemBy(array $type_in = [], int $invoice_type_id = 0)
|
|
{
|
|
return $this->model->with(['hosting', 'vps', 'os'])->where(function ($query) use ($type_in, $invoice_type_id) {
|
|
$query->whereIn('product_type', $type_in);
|
|
$query->where('invoice_type', $invoice_type_id);
|
|
})->get();
|
|
}
|
|
public function findBy(int $userId = 0, array $data = [], array $relationships = [])
|
|
{
|
|
$query = $this->model->with($relationships);
|
|
|
|
// Handle customer_id filtering through invoice relationship
|
|
if ($userId > 0) {
|
|
$query = $query->whereHas('invoice', function ($subQuery) use ($userId) {
|
|
$subQuery->where('customer_id', $userId);
|
|
});
|
|
}
|
|
|
|
// Handle additional data conditions, but skip customer_id if it's trying to query invoice_item directly
|
|
if (!empty($data) && isset($data['key']) && $data['key'] !== 'customer_id') {
|
|
$query = $query->where($data['key'], $data['condition'], $data['value']);
|
|
}
|
|
|
|
return $query->first();
|
|
}
|
|
public function filter(array $data = [], array $relationships = [], array $withCount = [], array $select = ['*'])
|
|
{
|
|
$model = $this->model->with($relationships)->select($select);
|
|
|
|
if (isset($data['invoice_type'])) {
|
|
$model = $model->where("invoice_type", $data['invoice_type']);
|
|
}
|
|
if (isset($data['parent_id'])) {
|
|
$model = $model->where("parent_id", $data['parent_id']);
|
|
}
|
|
// if (isset($data['customer_id'])) {
|
|
// $model = $model->where("customer_id", $data['customer_id']);
|
|
// }
|
|
if (isset($data['invoice_id'])) {
|
|
$model = $model->where("invoice_id", $data['invoice_id']);
|
|
}
|
|
if (count($withCount) > 0) {
|
|
$model = $model->withCount($withCount);
|
|
}
|
|
// if (isset($data['type_id'])) {
|
|
// $model = $model->where(function ($query) use ($data) {
|
|
// if (isset($data['type_id']['condition'])) {
|
|
// switch (strtoupper($data['type_id']['condition'])) {
|
|
// case "IN":
|
|
// $query->whereIn($data['type_id']['key'], $data['type_id']['value']);
|
|
// break;
|
|
// case "NotIn":
|
|
// $query->whereNotIn($data['type_id']['key'], $data['type_id']['value']);
|
|
// break;
|
|
// default:
|
|
// $query->where($data['type_id']['key'], $data['type_id']['condition'], $data['type_id']['value']);
|
|
// break;
|
|
// }
|
|
// } else {
|
|
// $query->where($data['type_id']['key'], $data['type_id']['value']);
|
|
// }
|
|
// });
|
|
// }
|
|
if (isset($data['relations'])) {
|
|
foreach ($data['relations'] as $key => $value) {
|
|
$model = $model->whereHas($key, function ($query) use ($value) {
|
|
if (isset($value['condition'])) {
|
|
switch (strtoupper($value['condition'])) {
|
|
case "IN":
|
|
$query->whereIn($value['key'], $value['value']);
|
|
break;
|
|
case "NotIn":
|
|
$query->whereNotIn($value['key'], $value['value']);
|
|
break;
|
|
default:
|
|
$query->where($value['key'], $value['condition'], $value['value']);
|
|
break;
|
|
}
|
|
} else {
|
|
$query->where($value['key'], $value['value']);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
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 findServiceStatus(int $relid = 0, int $type_id = 0)
|
|
{
|
|
return match ($type_id) {
|
|
3 => $this->hostingRepository->find($relid),
|
|
4 => $this->vmwareRepository->find($relid),
|
|
default => null
|
|
};
|
|
}
|
|
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);
|
|
}
|
|
}
|