Files
ladill-pos/app/Services/Import/CrmProductImportService.php
T
isaaccladandCursor e5d2b84388
Deploy Ladill Mini / deploy (push) Successful in 23s
Add Ladill POS v1 — register, Pay checkout, and commerce links.
Staff-facing counter register at pos.ladill.com with catalog cart, cash and
MoMo/card checkout via Ladill Pay, CRM timeline/import, invoice prefill, and
Merchant catalog import.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-23 22:52:24 +00:00

59 lines
1.7 KiB
PHP

<?php
namespace App\Services\Import;
use App\Models\PosProduct;
use App\Services\Crm\CrmClient;
use RuntimeException;
class CrmProductImportService
{
/**
* @return array{imported: int, updated: int}
*/
public function import(string $ownerRef): array
{
$response = CrmClient::for($ownerRef)->products(['active' => 1, 'per_page' => 500]);
$rows = (array) ($response['data'] ?? []);
if ($rows === []) {
throw new RuntimeException('No active products found in CRM.');
}
$imported = 0;
$updated = 0;
foreach ($rows as $row) {
$name = trim((string) ($row['name'] ?? ''));
if ($name === '') {
continue;
}
$sku = isset($row['sku']) && $row['sku'] !== '' ? (string) $row['sku'] : null;
$matchQuery = PosProduct::owned($ownerRef)->where('name', $name);
if ($sku) {
$matchQuery = PosProduct::owned($ownerRef)->where(fn ($q) => $q->where('sku', $sku)->orWhere('name', $name));
}
$existing = $matchQuery->first();
$attrs = [
'name' => $name,
'sku' => $sku,
'price_minor' => max(0, (int) ($row['unit_price_minor'] ?? 0)),
'currency' => strtoupper((string) ($row['currency'] ?? config('pos.default_currency', 'GHS'))),
'is_active' => (bool) ($row['active'] ?? true),
];
if ($existing) {
$existing->update($attrs);
$updated++;
} else {
PosProduct::create([...$attrs, 'owner_ref' => $ownerRef]);
$imported++;
}
}
return compact('imported', 'updated');
}
}