feat: Implement comprehensive product management, including categories, pricing, and integration into the order system.
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Repositories\Api\Service\ServiceRepository;
|
||||
use App\Repositories\Api\Invoice\InvoiceRepository;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use Exception;
|
||||
|
||||
class OrderService
|
||||
@@ -18,6 +19,11 @@ class OrderService
|
||||
|
||||
public function create(array $data, $customer)
|
||||
{
|
||||
// Check for existing pending orders
|
||||
if ($this->orderRepo->hasPendingOrder($customer->id)) {
|
||||
throw new Exception('Bạn có đơn hàng chưa thanh toán. Vui lòng thanh toán trước khi tạo đơn mới.');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($data, $customer) {
|
||||
|
||||
$product = $this->productRepo->find($data['product_id']);
|
||||
@@ -34,9 +40,10 @@ class OrderService
|
||||
}
|
||||
|
||||
$order = $this->orderRepo->create([
|
||||
'order_num' => \App\Helpers\OrderHelper::generateOrderNumber(),
|
||||
'customer_id' => $customer->id,
|
||||
'amount' => $price,
|
||||
'status' => 'pending'
|
||||
'status' => OrderStatus::PENDING->value
|
||||
]);
|
||||
|
||||
$this->orderRepo->createItem($order->id, [
|
||||
@@ -44,7 +51,8 @@ class OrderService
|
||||
'billing_cycle' => $data['billing_cycle'],
|
||||
'server_name' => $data['server_name'] ?? null,
|
||||
'os_id' => $data['os_id'] ?? null,
|
||||
'price' => $price
|
||||
'price' => $price,
|
||||
'addons' => $data['addons'] ?? []
|
||||
]);
|
||||
|
||||
$invoice = $this->invoiceRepo->createFromOrder($order);
|
||||
@@ -58,8 +66,8 @@ class OrderService
|
||||
|
||||
public function pay($order, string $method)
|
||||
{
|
||||
if ($order->status === 'paid') {
|
||||
throw new Exception('Đơn hàng đã thanh toán');
|
||||
if ($order->status !== OrderStatus::PENDING->value) {
|
||||
throw new Exception('Đơn hàng đã được xử lý hoặc hủy bỏ.');
|
||||
}
|
||||
|
||||
return $this->orderRepo->processPayment($order, $method);
|
||||
|
||||
Reference in New Issue
Block a user