feat(billing): renewals, coupons, VAT numbering, billing cycles and invoice payment tracking
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
ea9a53f742
commit
25f23d541e
@@ -8,6 +8,7 @@ use App\Models\Invoice;
|
||||
use App\Models\InvoiceItem;
|
||||
use App\Models\Order;
|
||||
use App\Repositories\Api\BaseApiRepository;
|
||||
use App\Support\VatNumbering;
|
||||
|
||||
class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryInterface
|
||||
{
|
||||
@@ -37,6 +38,7 @@ class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryIn
|
||||
// sub_total là tổng TRƯỚC giảm giá, để sub_total - discount_amount = total.
|
||||
'sub_total' => (int) $order->total + $discount,
|
||||
'discount_amount' => $discount,
|
||||
'coupon_code' => $order->coupon_code,
|
||||
'tax' => 0,
|
||||
'total' => $order->total,
|
||||
'status' => InvoiceStatus::UNPAID->value,
|
||||
@@ -78,7 +80,7 @@ class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryIn
|
||||
}
|
||||
}
|
||||
|
||||
return $invoice;
|
||||
return VatNumbering::settle($invoice);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repositories\Api\Service;
|
||||
|
||||
use App\Support\ProductBillingCycles;
|
||||
use App\Models\Product;
|
||||
use App\Repositories\Api\BaseApiRepository;
|
||||
|
||||
@@ -35,8 +36,13 @@ class ServiceRepository extends BaseApiRepository implements ServiceRepositoryIn
|
||||
|
||||
public function getPricing(int $productId, string $billingCycle)
|
||||
{
|
||||
$pricing = $this->model->find($productId)
|
||||
->pricings()
|
||||
$product = $this->model->find($productId);
|
||||
|
||||
if (!ProductBillingCycles::allows($product->product_type, $billingCycle)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pricing = $product->pricings()
|
||||
->where('billing_cycle', $billingCycle)
|
||||
->where('is_active', 1)
|
||||
->first();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repositories\Api\ServiceClient;
|
||||
|
||||
use App\Support\ProductBillingCycles;
|
||||
use App\Models\Service;
|
||||
use App\Repositories\Api\BaseApiRepository;
|
||||
|
||||
@@ -46,8 +47,13 @@ class ServiceClientRepository extends BaseApiRepository implements ServiceClient
|
||||
|
||||
public function getPricing(int $productId, string $billingCycle)
|
||||
{
|
||||
$pricing = $this->model->find($productId)
|
||||
->pricings()
|
||||
$product = $this->model->find($productId);
|
||||
|
||||
if (!ProductBillingCycles::allows($product->product_type, $billingCycle)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$pricing = $product->pricings()
|
||||
->where('billing_cycle', $billingCycle)
|
||||
->where('is_active', 1)
|
||||
->first();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repositories\Concerns;
|
||||
|
||||
use App\Models\InvoiceAttachment;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
@@ -28,6 +29,53 @@ trait FiltersBillingRecords
|
||||
->when($to, fn (Builder $q) => $q->where('created_at', '<=', Carbon::parse($to)->endOfDay()));
|
||||
}
|
||||
|
||||
/** @param callable(): array<int, mixed> $duplicates */
|
||||
private function whereIsDuplicate(Builder $query, mixed $flag, callable $duplicates): Builder
|
||||
{
|
||||
if (!filter_var($flag, FILTER_VALIDATE_BOOLEAN)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->whereIn($query->getModel()->getQualifiedKeyName(), array_keys($duplicates()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $columns
|
||||
*/
|
||||
private function applySort(Builder $query, mixed $sortBy, mixed $sortOrder, array $columns): Builder
|
||||
{
|
||||
$direction = strtolower((string) $sortOrder) === 'asc' ? 'asc' : 'desc';
|
||||
$table = $query->getModel()->getTable();
|
||||
|
||||
if ($sortBy === 'customer') {
|
||||
return $query
|
||||
->orderBy(
|
||||
\App\Models\Customer::query()->select('fullname')->whereColumn('customers.id', "{$table}.customer_id")->limit(1),
|
||||
$direction
|
||||
)
|
||||
->orderByDesc("{$table}.id");
|
||||
}
|
||||
|
||||
if ($sortBy === 'outstanding' && in_array($sortBy, $columns, true)) {
|
||||
return $query
|
||||
->orderByRaw("GREATEST({$table}.total - COALESCE({$table}.amount_paid, 0), 0) {$direction}")
|
||||
->orderByDesc("{$table}.id");
|
||||
}
|
||||
|
||||
if (!in_array($sortBy, $columns, true)) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query->orderBy("{$table}.{$sortBy}", $direction)->orderByDesc("{$table}.id");
|
||||
}
|
||||
|
||||
private function whereContractFileLike(Builder $query, string $attachmentsRelation, string $search): Builder
|
||||
{
|
||||
return $query->orWhereHas($attachmentsRelation, fn (Builder $files) => $files
|
||||
->where('type', InvoiceAttachment::TYPE_CONTRACT)
|
||||
->where('filename', 'like', "%{$search}%"));
|
||||
}
|
||||
|
||||
private function whereOrderItemNameLike(Builder $query, string $orderItemsRelation, string $search): Builder
|
||||
{
|
||||
return $query->orWhereHas($orderItemsRelation, fn (Builder $items) => $items
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repositories\Invoice;
|
||||
|
||||
use App\Support\Billing\BillingDuplicateDetector;
|
||||
use App\Models\Invoice;
|
||||
use App\Repositories\BaseRepository;
|
||||
use App\Repositories\Concerns\FiltersBillingRecords;
|
||||
@@ -63,6 +64,7 @@ class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInter
|
||||
}
|
||||
$model = $this->whereStatus($model, $data['status'] ?? null, ['unpaid' => 1, 'paid' => 2, 'cancel' => 3, 'overdue' => 4, 'refunded' => 5]);
|
||||
$model = $this->whereCreatedBetween($model, $data['date_from'] ?? null, $data['date_to'] ?? null);
|
||||
$model = $this->whereIsDuplicate($model, $data['duplicates'] ?? null, BillingDuplicateDetector::invoices(...));
|
||||
if (isset($data['filter']) && $data['filter'] !== '') {
|
||||
$search = $data['filter'];
|
||||
$model = $model->where(function ($query) use ($search) {
|
||||
@@ -73,6 +75,7 @@ class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInter
|
||||
->orWhere('email', 'like', "%{$search}%");
|
||||
});
|
||||
$this->whereOrderItemNameLike($query, 'order.orderItems', $search);
|
||||
$this->whereContractFileLike($query, 'attachments', $search);
|
||||
});
|
||||
}
|
||||
if (isset($data['relations'])) {
|
||||
@@ -96,6 +99,10 @@ class InvoiceRepository extends BaseRepository implements InvoiceRepositoryInter
|
||||
});
|
||||
}
|
||||
}
|
||||
$model = $this->applySort($model, $data['sort_by'] ?? null, $data['sort_order'] ?? null, [
|
||||
'invoice_number', 'sub_total', 'tax', 'total', 'status', 'created_at', 'due_date', 'paid_at', 'customer', 'outstanding',
|
||||
]);
|
||||
|
||||
if (count($withCount) > 0) {
|
||||
$model = $model->withCount($withCount);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Repositories\Order;
|
||||
|
||||
use App\Support\Billing\BillingDuplicateDetector;
|
||||
use App\Models\Order;
|
||||
use App\Repositories\BaseRepository;
|
||||
use App\Repositories\Concerns\FiltersBillingRecords;
|
||||
@@ -59,6 +60,7 @@ class OrderRepository extends BaseRepository implements OrderRepositoryInterface
|
||||
}
|
||||
$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']);
|
||||
}
|
||||
@@ -75,9 +77,14 @@ class OrderRepository extends BaseRepository implements OrderRepositoryInterface
|
||||
->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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user