2026-02-06 11:56:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Modules\Integration\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
2026-02-23 10:10:03 +08:00
|
|
|
use App\Modules\Inventory\Contracts\ProductServiceInterface;
|
2026-02-06 11:56:29 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class ProductSyncController extends Controller
|
|
|
|
|
{
|
|
|
|
|
protected $productService;
|
|
|
|
|
|
2026-02-23 10:10:03 +08:00
|
|
|
public function __construct(ProductServiceInterface $productService)
|
2026-02-06 11:56:29 +08:00
|
|
|
{
|
|
|
|
|
$this->productService = $productService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function upsert(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$request->validate([
|
|
|
|
|
'external_pos_id' => 'required|string',
|
|
|
|
|
'name' => 'required|string',
|
2026-02-23 10:10:03 +08:00
|
|
|
'price' => 'nullable|numeric|min:0',
|
2026-02-06 11:56:29 +08:00
|
|
|
'barcode' => 'nullable|string',
|
|
|
|
|
'category' => 'nullable|string',
|
|
|
|
|
'unit' => 'nullable|string',
|
|
|
|
|
'updated_at' => 'nullable|date',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$product = $this->productService->upsertFromPos($request->all());
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'Product synced successfully',
|
|
|
|
|
'data' => [
|
|
|
|
|
'id' => $product->id,
|
|
|
|
|
'external_pos_id' => $product->external_pos_id,
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Product Sync Failed', ['error' => $e->getMessage(), 'payload' => $request->all()]);
|
2026-02-23 10:10:03 +08:00
|
|
|
return response()->json([
|
|
|
|
|
'message' => 'Sync failed: ' . $e->getMessage(),
|
|
|
|
|
], 500);
|
2026-02-06 11:56:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|