feat: Reimplement order and invoice management with new service and repository layers, introduce compression middleware, and update database schema.

This commit is contained in:
pbao9
2026-03-12 09:30:29 +07:00
parent 7f6264c87f
commit 1e09ccf17b
87 changed files with 2348 additions and 2612 deletions
+45 -41
View File
@@ -2,84 +2,88 @@
namespace App\Repositories\Api\Order;
use App\Enums\Invoice\InvoiceStatus;
use App\Enums\Order\OrderStatus;
use App\Models\Order;
use App\Models\OrderItem;
use App\Repositories\Api\BaseApiRepository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
class OrderRepository extends BaseApiRepository implements OrderRepositoryInterface
{
public function getModel()
public function getModel(): string
{
return Order::class;
}
public function createItem($orderId, array $data)
// -------------------------------------------------------------------------
// createItem
// -------------------------------------------------------------------------
public function createItem(int $orderId, array $data): OrderItem
{
$addons = $data['addons'] ?? [];
unset($data['addons']); // Remove 'addons' from data before creating OrderItem
unset($data['addons']);
$orderItem = \App\Models\OrderItem::create(array_merge(
return OrderItem::create(array_merge(
['order_id' => $orderId],
$data
$data,
// Preserve addon selections as JSON; read again at provisioning time
// to build service_addons records.
['addons_meta' => !empty($addons) ? $addons : null],
));
if (!empty($addons)) {
foreach ($addons as $addonData) {
$addon = \App\Models\Addon::find($addonData['addon_id']);
if ($addon) {
\App\Models\OrderAddon::create([
'order_item_id' => $orderItem->id,
'addon_id' => $addon->id,
'qty' => $addonData['qty'],
'price' => $addon->price // Unit price
]);
}
}
}
return $orderItem;
}
public function processPayment($order, $method)
// -------------------------------------------------------------------------
// processPayment
// -------------------------------------------------------------------------
public function processPayment(Order $order, string $method): Order
{
// Update Order Status
$order->update([
'status' => \App\Enums\Order\OrderStatus::ACTIVE->value,
'status' => OrderStatus::ACTIVE->value,
'payment_method' => $method,
]);
// Update Invoice Status
if ($order->invoice) {
$order->invoice->update([
'status' => 2, // Paid
'date_paid' => now()
'status' => InvoiceStatus::PAID->value,
'payment_method' => $method,
'paid_at' => now(),
]);
}
return $order;
return $order->fresh(['orderItems', 'invoice', 'customer']);
}
public function hasPendingOrder($customerId)
// -------------------------------------------------------------------------
// hasPendingOrder
// -------------------------------------------------------------------------
public function hasPendingOrder(int $customerId): bool
{
return $this->model->where('customer_id', $customerId)
->where('status', \App\Enums\Order\OrderStatus::PENDING->value)
return $this->model
->where('customer_id', $customerId)
->where('status', OrderStatus::PENDING->value)
->exists();
}
public function getOrders($customerId, array $filters)
// -------------------------------------------------------------------------
// getOrders
// -------------------------------------------------------------------------
public function getOrders(int $customerId, array $filters): LengthAwarePaginator
{
$query = $this->model->where('customer_id', $customerId);
if (isset($filters['total_price'])) {
$query->where('amount', $filters['total_price']);
if (!empty($filters['total_price'])) {
$query->where('total', $filters['total_price']);
}
if (isset($filters['pending'])) {
// If pending=1 or true, filter by PENDING status
if (filter_var($filters['pending'], FILTER_VALIDATE_BOOLEAN)) {
$query->where('status', \App\Enums\Order\OrderStatus::PENDING->value);
}
if (!empty($filters['pending']) && filter_var($filters['pending'], FILTER_VALIDATE_BOOLEAN)) {
$query->where('status', OrderStatus::PENDING->value);
}
return $query->orderBy('created_at', 'desc')->paginate(10);
return $query->orderByDesc('created_at')->paginate(10);
}
}