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;
|
||||
|
||||
Reference in New Issue
Block a user