Files
star-erp/app/Modules/Integration/Controllers/ProductSyncController.php
sky121113 904132e460
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Has been skipped
Koori-ERP-Deploy-System / deploy-production (push) Successful in 1m14s
feat(integration): 擴充產品同步 API 欄位與驗證強化
1. ProductSync API 新增防護機制,為既有欄位加上字串長度與金額上限限制
2. 開放並接收 ERP Product Model 實用欄位(品牌、規格、成本價、會員價、批發價)
3. 更新 ProductService 寫入邏輯以支援新增的可選欄位
4. 同步更新 api-integration.md 手冊,加入新欄位說明與 JSON 範例
2026-02-23 11:02:25 +08:00

54 lines
1.8 KiB
PHP

<?php
namespace App\Modules\Integration\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Inventory\Contracts\ProductServiceInterface;
use Illuminate\Support\Facades\Log;
class ProductSyncController extends Controller
{
protected $productService;
public function __construct(ProductServiceInterface $productService)
{
$this->productService = $productService;
}
public function upsert(Request $request)
{
$request->validate([
'external_pos_id' => 'required|string|max:255',
'name' => 'required|string|max:255',
'price' => 'nullable|numeric|min:0|max:99999999.99',
'barcode' => 'nullable|string|max:100',
'category' => 'nullable|string|max:100',
'unit' => 'nullable|string|max:100',
'brand' => 'nullable|string|max:100',
'specification' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0|max:99999999.99',
'member_price' => 'nullable|numeric|min:0|max:99999999.99',
'wholesale_price' => 'nullable|numeric|min:0|max:99999999.99',
'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()]);
return response()->json([
'message' => 'Sync failed: ' . $e->getMessage(),
], 500);
}
}
}