Files
portal-api/app/Repositories/Invoice/InvoiceRepository.php
T

162 lines
6.1 KiB
PHP

<?php
namespace App\Repositories\Invoice;
use App\Models\Invoice;
use App\Repositories\BaseRepository;
use Carbon\Carbon;
class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInterface
{
public function getModel()
{
return Invoice::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['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['payment_method']) && $data['payment_method'] !== 'full') {
$model = $model->where("payment_id", $data['payment_method']);
}
if (isset($data['invoice_id'])) {
$model = $model->where("invoice_id", $data['invoice_id']);
}
if (isset($data['date'])) {
$data = explode("-", $data['date']);
$model = $model->whereBetween(
'date',
[Carbon::parse($data[0])->timestamp, Carbon::parse($data[1])->timestamp]
);
}
if (isset($data['due_date'])) {
$data = explode("-", $data['due_date']);
$model = $model->whereBetween(
'due_date',
[Carbon::parse($data[0])->timestamp, Carbon::parse($data[1])->timestamp]
);
}
if (isset($data['date_paid'])) {
$date = explode("-", $data['date_paid']);
$model = $model->where(function ($query) use ($date) {
$query->wherNotNull();
$query->whereBetween(
'date_paid',
[Carbon::parse($date[0])->timestamp, Carbon::parse($date[1])->timestamp]
);
});
}
if (isset($data['status']) && $data['status'] !=='full') {
$model = $model->where('status', match ($data['status']) {
'active' => 1,
'cancel' => 2,
default => 0
});
}
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 (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 get_billing_year(int $year = 0){
return $this->model->whereYear('created_at',$year)->get()->sum('total');
}
public function get_billing_month(int $month = 0){
return $this->model->whereMonth('created_at',$month)->get()->sum('total');
}
public function get_billing_day(int|string $day = ''){
return $this->model->whereDate('created_at',$day)->get()->sum('total');
}
public function findWithRelationships(string $ordernum = '', 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->where('order_num', $ordernum)->first();
}
}