56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Writers;
|
|
|
|
use App\Integrations\Whmcs\DTOs\ProductData;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductWriter
|
|
{
|
|
/**
|
|
* Skips products whose category couldn't be resolved — products.product_categories_id is NOT NULL.
|
|
*/
|
|
public function write(ProductData $data): bool
|
|
{
|
|
if ($data->categoryId === null) {
|
|
return false;
|
|
}
|
|
|
|
DB::transaction(function () use ($data) {
|
|
$productType = DB::table('product_categories')->where('id', $data->categoryId)->value('type') ?? 'other';
|
|
|
|
DB::table('products')->updateOrInsert(
|
|
['legacy_id' => $data->legacyId],
|
|
[
|
|
'uuid' => DB::table('products')->where('legacy_id', $data->legacyId)->value('uuid') ?? (string) Str::uuid(),
|
|
'product_type' => $productType,
|
|
'product_categories_id' => $data->categoryId,
|
|
'name' => $data->name,
|
|
'slug' => $data->slug,
|
|
'short_description' => $data->shortDescription,
|
|
'description' => $data->description,
|
|
'stock' => $data->stock,
|
|
'is_unlimited' => $data->isUnlimited,
|
|
'setup_fee' => $data->setupFee,
|
|
'is_active' => $data->isActive,
|
|
'sort_order' => $data->sortOrder,
|
|
'created_at' => $data->createdAt,
|
|
'updated_at' => $data->updatedAt,
|
|
]
|
|
);
|
|
|
|
$productId = DB::table('products')->where('legacy_id', $data->legacyId)->value('id');
|
|
|
|
foreach ($data->pricings as $pricing) {
|
|
DB::table('product_pricings')->updateOrInsert(
|
|
['product_id' => $productId, 'billing_cycle' => $pricing['billing_cycle']],
|
|
['price' => $pricing['price'], 'currency' => 'VND']
|
|
);
|
|
}
|
|
});
|
|
|
|
return true;
|
|
}
|
|
}
|