64 lines
2.3 KiB
PHP
64 lines
2.3 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\Integration\Controllers;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Modules\Inventory\Contracts\InventoryServiceInterface;
|
||
use Illuminate\Http\JsonResponse;
|
||
|
||
class InventorySyncController extends Controller
|
||
{
|
||
protected $inventoryService;
|
||
|
||
public function __construct(InventoryServiceInterface $inventoryService)
|
||
{
|
||
$this->inventoryService = $inventoryService;
|
||
}
|
||
|
||
/**
|
||
* 提供外部 POS 查詢指定倉庫的商品庫存餘額
|
||
*
|
||
* @param string $warehouseCode
|
||
* @return JsonResponse
|
||
*/
|
||
public function show(\Illuminate\Http\Request $request, string $warehouseCode): JsonResponse
|
||
{
|
||
// 透過 Service 調用跨模組庫存查詢功能,傳入篩選條件
|
||
$inventoryData = $this->inventoryService->getPosInventoryByWarehouseCode(
|
||
$warehouseCode,
|
||
$request->only(['product_id', 'barcode', 'code', 'external_pos_id'])
|
||
);
|
||
|
||
// 若回傳 null,表示尋無此倉庫代碼
|
||
if (is_null($inventoryData)) {
|
||
return response()->json([
|
||
'status' => 'error',
|
||
'message' => "Warehouse with code '{$warehouseCode}' not found.",
|
||
], 404);
|
||
}
|
||
|
||
// 以 JSON 格式回傳組合好的商品庫存列表
|
||
return response()->json([
|
||
'status' => 'success',
|
||
'warehouse_code' => $warehouseCode,
|
||
'data' => $inventoryData->map(function ($item) {
|
||
return [
|
||
'product_id' => $item->product_id,
|
||
'external_pos_id' => $item->external_pos_id,
|
||
'product_code' => $item->product_code,
|
||
'product_name' => $item->product_name,
|
||
'barcode' => $item->barcode,
|
||
'category_name' => $item->category_name ?? '未分類',
|
||
'unit_name' => $item->unit_name ?? '個',
|
||
'price' => (float) $item->price,
|
||
'brand' => $item->brand,
|
||
'specification' => $item->specification,
|
||
'batch_number' => $item->batch_number,
|
||
'expiry_date' => $item->expiry_date,
|
||
'quantity' => (float) $item->total_quantity,
|
||
];
|
||
})
|
||
], 200);
|
||
}
|
||
}
|