update rm deadcode
This commit is contained in:
@@ -17,6 +17,7 @@ use App\Models\Service;
|
||||
use App\Models\VirtualMachines;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Repositories\Api\Service\ServiceRepositoryInterface;
|
||||
use App\Support\RenewalPricing;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -204,6 +205,19 @@ class OrderQueryService
|
||||
return ['ok' => false, 'message' => 'Không thể xác định giá gia hạn dịch vụ.', 'code' => 400];
|
||||
}
|
||||
|
||||
$cycleMonths = OrderHelper::cycleToMonths($orderItem->billing_cycle);
|
||||
$validMonths = RenewalPricing::validMonths($orderItem->billing_cycle);
|
||||
|
||||
if (! in_array($months, $validMonths, true)) {
|
||||
return [
|
||||
'ok' => false,
|
||||
'message' => 'Số tháng gia hạn không hợp lệ với chu kỳ của dịch vụ này. Vui lòng chọn: ' . implode(', ', $validMonths) . ' tháng.',
|
||||
'code' => 422,
|
||||
];
|
||||
}
|
||||
|
||||
$cycles = intdiv($months, $cycleMonths);
|
||||
|
||||
// Gia hạn luôn dùng giá sản phẩm hiện tại trong bảng giá, không dùng giá đã
|
||||
// chốt lúc đặt mua ban đầu — giá cũ chỉ để hiển thị trên đơn hàng/hoá đơn gốc.
|
||||
$currentPrice = $this->productRepo->getPricing((int) $service->product_id, $orderItem->billing_cycle);
|
||||
@@ -211,7 +225,9 @@ class OrderQueryService
|
||||
return ['ok' => false, 'message' => 'Sản phẩm hiện không còn bán, không thể gia hạn.', 'code' => 400];
|
||||
}
|
||||
|
||||
$price = (int) $currentPrice * $months;
|
||||
$renewalPrice = (int) $currentPrice * $cycles;
|
||||
$addonTotal = RenewalPricing::addonTotal($service, $cycles);
|
||||
$price = $renewalPrice + $addonTotal;
|
||||
|
||||
if ($service->invoice_id && Invoice::where('id', $service->invoice_id)
|
||||
->where('invoice_type', 2)
|
||||
@@ -238,7 +254,7 @@ class OrderQueryService
|
||||
$periodStart = $service->expires_at && $service->expires_at->isFuture() ? $service->expires_at->copy() : now();
|
||||
$periodEnd = $periodStart->copy()->addMonths($months);
|
||||
|
||||
$invoice = DB::transaction(function () use ($service, $orderItem, $price, $months, $periodStart, $periodEnd) {
|
||||
$invoice = DB::transaction(function () use ($service, $orderItem, $price, $renewalPrice, $cycles, $months, $periodStart, $periodEnd) {
|
||||
$invoice = Invoice::create([
|
||||
'invoice_number' => OrderHelper::generateOrderNumber('INV'),
|
||||
'customer_id' => $service->customer_id,
|
||||
@@ -261,10 +277,14 @@ class OrderQueryService
|
||||
'description' => 'Gia hạn dịch vụ: ' . ($orderItem->product?->name ?? $service->server_name ?? ''),
|
||||
'months' => $months,
|
||||
'quantity' => 1,
|
||||
'unit_price' => $price,
|
||||
'total_price' => $price,
|
||||
'unit_price' => $renewalPrice,
|
||||
'total_price' => $renewalPrice,
|
||||
]);
|
||||
|
||||
foreach (RenewalPricing::addonInvoiceItems($service, $cycles, $months) as $addonItem) {
|
||||
\App\Models\InvoiceItem::create($addonItem + ['invoice_id' => $invoice->id]);
|
||||
}
|
||||
|
||||
$service->update(['invoice_id' => $invoice->id]);
|
||||
|
||||
return $invoice;
|
||||
|
||||
@@ -5,12 +5,13 @@ namespace App\Services\Api\Order;
|
||||
use App\Enums\Order\OrderStatus;
|
||||
use App\Helpers\OrderHelper;
|
||||
use App\Jobs\Mail\Admin\AdminOrderNotificationJob;
|
||||
use App\Models\Addon;
|
||||
use App\Models\Order;
|
||||
use App\Models\Product;
|
||||
use App\Repositories\Api\Invoice\InvoiceRepository;
|
||||
use App\Repositories\Api\Order\OrderRepository;
|
||||
use App\Repositories\Api\Service\ServiceRepository;
|
||||
use App\Services\InvoicePaymentService;
|
||||
use App\Support\AddonQuantityValidator;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -32,31 +33,31 @@ class OrderService
|
||||
* Validate → calculate price → persist Order + OrderItem + Invoice.
|
||||
*
|
||||
* @return array{order: Order, invoice: \App\Models\Invoice}
|
||||
* @throws Exception
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function create(array $data, $customer): array
|
||||
{
|
||||
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.');
|
||||
throw new \InvalidArgumentException('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.');
|
||||
}
|
||||
|
||||
$product = $this->productRepo->find($data['product_id']);
|
||||
if (!$product) {
|
||||
throw new Exception('Sản phẩm không tồn tại.');
|
||||
throw new \InvalidArgumentException('Sản phẩm không tồn tại.');
|
||||
}
|
||||
|
||||
$basePrice = $this->productRepo->getPricing($product->id, $data['billing_cycle']);
|
||||
if (is_null($basePrice)) {
|
||||
throw new Exception('Không tìm thấy giá cho chu kỳ thanh toán này.');
|
||||
throw new \InvalidArgumentException('Không tìm thấy giá cho chu kỳ thanh toán này.');
|
||||
}
|
||||
|
||||
// Resolve addons → enriched meta with price & subtotal
|
||||
$addonsMeta = $this->resolveAddons($data['addons'] ?? []);
|
||||
$addonsMeta = $this->resolveAddons($data['addons'] ?? [], $product);
|
||||
$addonsPrice = array_sum(array_column($addonsMeta, 'subtotal'));
|
||||
$total = $basePrice + $addonsPrice;
|
||||
|
||||
if ($customer->total < $total) {
|
||||
throw new Exception(sprintf(
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Số dư không đủ. Hiện có: %s VNĐ, cần: %s VNĐ.',
|
||||
number_format($customer->total),
|
||||
number_format($total)
|
||||
@@ -101,19 +102,19 @@ class OrderService
|
||||
/**
|
||||
* Debit customer balance → mark Order+Invoice paid → provision Service records.
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function pay(Order $order, string $method): Order
|
||||
{
|
||||
if (in_array($order->status, [OrderStatus::ACTIVE->value, OrderStatus::PROCESSING->value], true)) {
|
||||
throw new Exception('Đơn hàng đã được thanh toán hoặc đang xử lý.');
|
||||
throw new \InvalidArgumentException('Đơn hàng đã được thanh toán hoặc đang xử lý.');
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($order, $method) {
|
||||
$customer = $order->customer;
|
||||
|
||||
if ($customer->total < $order->total) {
|
||||
throw new Exception(sprintf(
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Số dư không đủ. Hiện có: %s VNĐ, cần: %s VNĐ.',
|
||||
number_format($customer->total),
|
||||
number_format($order->total)
|
||||
@@ -126,7 +127,7 @@ class OrderService
|
||||
}
|
||||
|
||||
if (! $order->invoice) {
|
||||
throw new Exception('Không tìm thấy hóa đơn của đơn hàng.');
|
||||
throw new \InvalidArgumentException('Không tìm thấy hóa đơn của đơn hàng.');
|
||||
}
|
||||
|
||||
$this->invoicePaymentService->payWithBalance($order->invoice, $customer, $method);
|
||||
@@ -175,25 +176,13 @@ class OrderService
|
||||
*
|
||||
* Input: [['addon_id' => 1, 'qty' => 2], ...]
|
||||
* Output: [['addon_id' => 1, 'qty' => 2, 'price' => 50000, 'subtotal' => 100000], ...]
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
private function resolveAddons(array $raw): array
|
||||
private function resolveAddons(array $raw, Product $product): array
|
||||
{
|
||||
$resolved = [];
|
||||
foreach ($raw as $entry) {
|
||||
$addon = Addon::find($entry['addon_id'] ?? null);
|
||||
if (!$addon) {
|
||||
continue;
|
||||
}
|
||||
$qty = max(1, (int) ($entry['qty'] ?? 1));
|
||||
$resolved[] = [
|
||||
'addon_id' => $addon->id,
|
||||
'code' => strtolower((string) $addon->code),
|
||||
'name' => $addon->name,
|
||||
'qty' => $qty,
|
||||
'price' => $addon->price,
|
||||
'subtotal' => $addon->price * $qty,
|
||||
];
|
||||
}
|
||||
return $resolved;
|
||||
$activeAddons = $product->addons()->where('status', true)->get()->keyBy('id');
|
||||
|
||||
return AddonQuantityValidator::resolve($raw, $activeAddons);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user