Files
ladill-pos/app/Services/Pos/PosBarcodeLookupService.php
T
isaaccladandCursor 9a2d196d84
Deploy Ladill POS / deploy (push) Successful in 1m42s
Add barcode scanner, thermal receipt printing, and printer settings.
Register supports USB keyboard-wedge scanners with SKU lookup (local catalog
and CRM in retail mode). Sales get a thermal receipt print template (58/80mm)
with configurable header/footer, plus optional auto-print after cash sales.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-08 05:40:14 +00:00

79 lines
2.2 KiB
PHP

<?php
namespace App\Services\Pos;
use App\Models\PosLocation;
use App\Models\PosProduct;
use App\Services\Crm\CrmClient;
class PosBarcodeLookupService
{
/**
* Resolve a scanned barcode/SKU to a register line item.
*
* @return array{id: int|string|null, name: string, price_minor: int, sku: string|null}|null
*/
public function lookup(string $ownerRef, PosLocation $location, string $code): ?array
{
$code = trim($code);
if ($code === '') {
return null;
}
if ($location->isRestaurant()) {
return $this->lookupLocal($ownerRef, $code);
}
return $this->lookupRetail($ownerRef, $code) ?? $this->lookupLocal($ownerRef, $code);
}
/** @return array{id: int|string|null, name: string, price_minor: int, sku: string|null}|null */
private function lookupLocal(string $ownerRef, string $code): ?array
{
$product = PosProduct::owned($ownerRef)
->active()
->where('sku', $code)
->first();
if (! $product) {
return null;
}
return [
'id' => $product->id,
'name' => $product->name,
'price_minor' => $product->price_minor,
'sku' => $product->sku,
];
}
/** @return array{id: int|string|null, name: string, price_minor: int, sku: string|null}|null */
private function lookupRetail(string $ownerRef, string $code): ?array
{
try {
$rows = (array) (CrmClient::for($ownerRef)->products([
'search' => $code,
'type' => 'product',
'active' => 1,
'per_page' => 25,
])['data'] ?? []);
} catch (\Throwable) {
return null;
}
foreach ($rows as $row) {
$sku = isset($row['sku']) ? (string) $row['sku'] : '';
if ($sku !== '' && strcasecmp($sku, $code) === 0) {
return [
'id' => $row['id'] ?? null,
'name' => (string) ($row['name'] ?? ''),
'price_minor' => (int) ($row['unit_price_minor'] ?? 0),
'sku' => $sku,
];
}
}
return null;
}
}