Files
ladill-pos/app/Services/Crm/CrmClient.php
T
isaaccladandClaude Opus 4.8 5d8e185223
Deploy Ladill POS / deploy (push) Successful in 28s
Link retail POS products to the CRM products API (restaurant stays local)
Products are now mode-aware:
- Retail: the catalog lives in Ladill CRM. The Products page proxies CRUD to the
  CRM products API (CrmClient gains product/create/update/delete), and the
  register reads CRM products live; a sold line is stored as a name/price
  snapshot (product_id null, since CRM products aren't local rows). Resilient —
  the register shows an empty catalog if CRM is unreachable. CRM import is hidden
  in Settings (not needed when reading live).
- Restaurant: unchanged — the local pos_products catalog keeps its POS-only depth
  (category, kitchen station, course, modifier groups).

ProductController routes take a raw {product} id (CRM id in retail, local id in
restaurant). Suite green (17), covering both modes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-25 12:27:19 +00:00

96 lines
2.5 KiB
PHP

<?php
namespace App\Services\Crm;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
class CrmClient
{
public function __construct(private readonly string $owner) {}
public static function for(string $owner): self
{
return new self($owner);
}
public function customers(array $filters = []): array
{
return $this->get('customers', $filters);
}
public function products(array $filters = []): array
{
return $this->get('products', $filters);
}
public function product(int|string $id): array
{
return $this->get("products/{$id}");
}
public function createProduct(array $data): array
{
return $this->send('post', 'products', $data);
}
public function updateProduct(int|string $id, array $data): array
{
return $this->send('patch', "products/{$id}", $data);
}
public function deleteProduct(int|string $id): array
{
return $this->send('delete', "products/{$id}", []);
}
public function pushTimeline(array $data): array
{
return $this->post('timeline', $data);
}
private function client(): PendingRequest
{
return Http::baseUrl((string) config('crm.url'))
->withToken((string) config('crm.key'))
->acceptJson()
->asJson()
->connectTimeout(10)
->timeout(20);
}
private function get(string $path, array $query = []): array
{
return $this->handle(fn () => $this->client()->get($path, [...$query, 'owner' => $this->owner]));
}
private function post(string $path, array $data): array
{
return $this->handle(fn () => $this->client()->post($path, [...$data, 'owner' => $this->owner]));
}
private function send(string $method, string $path, array $data): array
{
return $this->handle(fn () => $this->client()->{$method}($path, [...$data, 'owner' => $this->owner]));
}
private function handle(callable $request): array
{
try {
$response = $request();
} catch (ConnectionException) {
throw ValidationException::withMessages([
'crm' => ['Could not reach the CRM service. Please try again in a moment.'],
]);
}
if ($response->failed()) {
abort($response->status() === 404 ? 404 : 502, 'CRM service error.');
}
return (array) $response->json();
}
}