Deploy Ladill Mini / deploy (push) Successful in 23s
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>
94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Pos;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Controllers\Pos\Concerns\ScopesToAccount;
|
|
use App\Models\PosProduct;
|
|
use App\Models\PosSale;
|
|
use App\Services\Pos\PosLocationService;
|
|
use App\Services\Pos\PosSaleService;
|
|
use App\Services\Crm\CrmClient;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use RuntimeException;
|
|
|
|
class RegisterController extends Controller
|
|
{
|
|
use ScopesToAccount;
|
|
|
|
public function __construct(private PosSaleService $sales, private PosLocationService $locations) {}
|
|
|
|
public function index(Request $request): View
|
|
{
|
|
$owner = $this->ownerRef($request);
|
|
$location = $this->locations->ensureDefault($owner);
|
|
|
|
$products = PosProduct::owned($owner)
|
|
->active()
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return view('pos.register', [
|
|
'products' => $products,
|
|
'location' => $location,
|
|
'crmCustomers' => $this->crmCustomers($owner),
|
|
]);
|
|
}
|
|
|
|
/** @return list<array<string, mixed>> */
|
|
private function crmCustomers(string $owner): array
|
|
{
|
|
try {
|
|
return (array) ((CrmClient::for($owner)->customers(['per_page' => 200])['data']) ?? []);
|
|
} catch (\Throwable) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
public function charge(Request $request): RedirectResponse
|
|
{
|
|
$data = $request->validate([
|
|
'lines' => ['required', 'array', 'min:1'],
|
|
'lines.*.product_id' => ['nullable', 'integer'],
|
|
'lines.*.name' => ['required', 'string', 'max:200'],
|
|
'lines.*.unit_price_minor' => ['required', 'integer', 'min:1'],
|
|
'lines.*.quantity' => ['required', 'integer', 'min:1', 'max:999'],
|
|
'customer_name' => ['nullable', 'string', 'max:120'],
|
|
'customer_email' => ['nullable', 'email', 'max:255'],
|
|
'customer_phone' => ['nullable', 'string', 'max:40'],
|
|
'crm_customer_id' => ['nullable', 'integer'],
|
|
'payment_method' => ['required', 'in:pay,cash'],
|
|
]);
|
|
|
|
$merchant = ladill_account() ?? $request->user();
|
|
$location = $this->locations->ensureDefault($this->ownerRef($request));
|
|
|
|
try {
|
|
$sale = $this->sales->createSale($merchant, $data['lines'], [
|
|
'location_id' => $location->id,
|
|
'currency' => $location->currency,
|
|
'customer_name' => $data['customer_name'] ?? null,
|
|
'customer_email' => $data['customer_email'] ?? null,
|
|
'customer_phone' => $data['customer_phone'] ?? null,
|
|
'crm_customer_id' => $data['crm_customer_id'] ?? null,
|
|
]);
|
|
|
|
if ($data['payment_method'] === 'cash') {
|
|
$this->sales->recordCashPayment($sale);
|
|
|
|
return redirect()
|
|
->route('pos.sales.show', $sale)
|
|
->with('success', 'Cash sale recorded.');
|
|
}
|
|
|
|
$result = $this->sales->initiatePayCheckout($sale, $merchant);
|
|
|
|
return redirect()->away($result['checkout_url']);
|
|
} catch (RuntimeException $e) {
|
|
return back()->withInput()->with('error', $e->getMessage());
|
|
}
|
|
}
|
|
}
|