feat: Implement order and invoice management, enhance addon features, and remove the 'Type' module.

This commit is contained in:
pbao9
2026-02-11 17:02:07 +07:00
parent 58a9aa685b
commit 556b2c02e6
47 changed files with 1367 additions and 497 deletions
@@ -0,0 +1,54 @@
<?php
namespace App\Repositories\Api\Invoice;
use App\Models\Invoice;
use App\Repositories\Api\BaseApiRepository;
class InvoiceRepository extends BaseApiRepository implements InvoiceRepositoryInterface
{
public function getModel()
{
return Invoice::class;
}
/**
* Lấy danh sách sản phẩm đang hoạt động
*/
public function getInvoice(int $limit = 10, $customer = null)
{
return $this->model->where('customer_id', $customer)->paginate($limit);
}
public function createFromOrder($order)
{
$invoice = $this->model->create([
'customer_id' => $order->customer_id,
'date' => now(),
'due_date' => now()->addDays(3),
'amount' => $order->amount,
'status' => 1, // Unpaid
]);
$order->update(['invoice_id' => $invoice->id]);
$orderItems = $order->orderItems;
if ($orderItems->isEmpty()) {
$orderItems = $order->orderItems()->get();
}
foreach ($orderItems as $item) {
\App\Models\InvoiceItem::create([
'invoice_id' => $invoice->id,
'customer_id' => $invoice->customer_id,
'product_id' => $item->product_id,
'description' => optional($item->product)->name ?? 'Order Item',
'amount' => $item->price ?? $order->amount,
'type_id' => 1,
'billing' => $item->billing_cycle ?? 'monthly',
]);
}
return $invoice;
}
}
@@ -0,0 +1,12 @@
<?php
namespace App\Repositories\Api\Invoice;
use App\Repositories\BaseRepositoryInterface;
interface InvoiceRepositoryInterface extends BaseRepositoryInterface
{
public function getInvoice(int $limit = 10, $customer = null);
public function createFromOrder($order);
}
@@ -0,0 +1,31 @@
<?php
namespace App\Repositories\Api\Order;
use App\Models\Order;
use App\Repositories\Api\BaseApiRepository;
class OrderRepository extends BaseApiRepository implements OrderRepositoryInterface
{
public function getModel()
{
return Order::class;
}
public function createItem($orderId, array $data)
{
return \App\Models\OrderItem::create(array_merge(
['order_id' => $orderId],
$data
));
}
public function processPayment($order, $method)
{
return $order->update([
'status' => 'paid',
'payment_method' => $method,
'paid_at' => now()
]);
}
}
@@ -0,0 +1,12 @@
<?php
namespace App\Repositories\Api\Order;
use App\Repositories\BaseRepositoryInterface;
interface OrderRepositoryInterface extends BaseRepositoryInterface
{
public function createItem($orderId, array $data);
public function processPayment($order, $method);
}
@@ -32,4 +32,15 @@ class ServiceRepository extends BaseApiRepository implements ServiceRepositoryIn
->where('is_active', 1)
->first();
}
public function getPricing(int $productId, string $billingCycle)
{
$pricing = $this->model->find($productId)
->pricings()
->where('billing_cycle', $billingCycle)
->where('is_active', 1)
->first();
return $pricing ? $pricing->price : null;
}
}
@@ -12,4 +12,6 @@ interface ServiceRepositoryInterface extends BaseRepositoryInterface
public function getActiveServices(int $limit = 10);
public function findActiveBySlug(string $slug);
public function getPricing(int $productId, string $billingCycle);
}