update rm deadcode

This commit is contained in:
baodev0
2026-09-27 11:42:40 +07:00
parent 82a71244ca
commit 3af59bd9c5
129 changed files with 5194 additions and 2698 deletions
+108
View File
@@ -9,6 +9,8 @@ use App\Helpers\OrderHelper;
use App\Jobs\Hosting\ProvisionHostingAccountJob;
use App\Jobs\VirtualMachine\Action\VMJobs;
use App\Jobs\VirtualMachine\Create\CreateVirtualMachineJob;
use App\Jobs\VirtualMachine\Provision\UpdateVmConfigJob;
use App\Http\Services\vCenter as ServiceVCenter;
use App\Models\Addon;
use App\Models\Customer;
use App\Models\Invoice;
@@ -19,6 +21,7 @@ use App\Models\ServiceAddon;
use App\Models\Transaction;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class InvoicePaymentService
@@ -125,6 +128,8 @@ class InvoicePaymentService
if ((int) $invoice->invoice_type === 2) {
$this->activateRenewal($invoice);
} elseif ((int) $invoice->invoice_type === 3) {
$this->activateUpgrade($invoice);
} elseif ($invoice->order) {
$this->activateOrder($invoice);
}
@@ -197,6 +202,8 @@ class InvoicePaymentService
if ((int) $invoice->invoice_type === 2) {
$this->activateRenewal($invoice);
} elseif ((int) $invoice->invoice_type === 3) {
$this->activateUpgrade($invoice);
} elseif ($invoice->order) {
$this->activateOrder($invoice);
}
@@ -385,6 +392,107 @@ class InvoicePaymentService
}
}
/**
* Merges the purchased addons into order_items.addons_meta (so RenewalPricing charges
* them on the next renewal) and applies the new absolute CPU/RAM/disk totals to the VM.
*/
private function activateUpgrade(Invoice $invoice): void
{
$service = Service::query()
->with(['orderItem.product', 'vmware'])
->find($invoice->service_id);
$orderItem = $service?->orderItem;
if (! $service || ! $orderItem) {
return;
}
$upgradeItems = $invoice->invoiceItems()->where('type', 'upgrade')->get();
if ($upgradeItems->isEmpty()) {
return;
}
$meta = collect(is_array($orderItem->addons_meta) ? $orderItem->addons_meta : []);
$purchasedCodes = [];
foreach ($upgradeItems as $item) {
$addon = Addon::find($item->addon_id);
if (! $addon) {
continue;
}
$code = strtolower((string) $addon->code);
$purchasedCodes[] = $code;
$qty = (int) $item->quantity;
$index = $meta->search(fn ($entry) => (int) ($entry['addon_id'] ?? 0) === $addon->id);
$currentQty = $index === false ? 0 : (int) ($meta->get($index)['qty'] ?? 0);
$newQty = $currentQty + $qty;
$entry = [
'addon_id' => $addon->id,
'code' => $code,
'name' => $addon->name,
'qty' => $newQty,
'price' => (float) $addon->price,
'subtotal' => $newQty * (float) $addon->price,
];
if ($index === false) {
$meta->push($entry);
} else {
$meta->put($index, $entry);
}
}
$orderItem->update(['addons_meta' => $meta->values()->all()]);
$moduleVmware = $service->vmware;
if (! $moduleVmware) {
return;
}
$addonQtyByCode = $meta->keyBy(fn ($entry) => $entry['code'] ?? '');
$product = $orderItem->product;
if (array_intersect(['cpu', 'ram'], $purchasedCodes)) {
$totalCpu = (int) ($product?->cpu ?? 0) + (int) ($addonQtyByCode->get('cpu')['qty'] ?? 0);
$totalRam = (int) ($product?->ram ?? 0) + (int) ($addonQtyByCode->get('ram')['qty'] ?? 0);
UpdateVmConfigJob::dispatch(
$moduleVmware->id,
(string) $moduleVmware->vmware_id,
'',
$totalCpu,
$totalRam,
)->afterCommit();
}
if (in_array('disk', $purchasedCodes, true)) {
$totalDisk = (int) ($product?->disk ?? 0) + (int) ($addonQtyByCode->get('disk')['qty'] ?? 0);
$vcenter = $moduleVmware->vCenter ?? $moduleVmware->ipAddress?->host?->datacenter?->vCenter;
if ($vcenter) {
DB::afterCommit(function () use ($vcenter, $moduleVmware, $totalDisk, $invoice) {
try {
$serviceVCenter = new ServiceVCenter();
if (! $serviceVCenter->loginServer($vcenter)) {
throw new \RuntimeException('vCenter login failed');
}
$serviceVCenter->UpgradeDiskVirtualMachine($moduleVmware, $totalDisk);
} catch (\Throwable $e) {
Log::error('activateUpgrade: disk upgrade failed', [
'invoice_id' => $invoice->id,
'module_vmware_id' => $moduleVmware->id,
'total_disk' => $totalDisk,
'error' => $e->getMessage(),
]);
}
});
}
}
}
private function attachServiceAddons(Service $service, OrderItem $item): void
{
if ($service->addons()->exists()) {