feat: 整合門市領料日誌、API 文件存取、修改庫存與併發編號問題、供應商商品內聯編輯及日誌 UI 優化
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 1m0s
This commit is contained in:
@@ -147,4 +147,48 @@ class ProcurementService implements ProcurementServiceInterface
|
||||
->where('product_id', $productId)
|
||||
->delete();
|
||||
}
|
||||
|
||||
public function syncVendorProducts(int $vendorId, array $productsData): void
|
||||
{
|
||||
\Illuminate\Support\Facades\DB::transaction(function () use ($vendorId, $productsData) {
|
||||
$existingPivots = \Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->get();
|
||||
|
||||
$existingProductIds = $existingPivots->pluck('product_id')->toArray();
|
||||
$newProductIds = array_column($productsData, 'product_id');
|
||||
|
||||
$toDelete = array_diff($existingProductIds, $newProductIds);
|
||||
|
||||
if (!empty($toDelete)) {
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->whereIn('product_id', $toDelete)
|
||||
->delete();
|
||||
}
|
||||
|
||||
foreach ($productsData as $data) {
|
||||
$exists = in_array($data['product_id'], $existingProductIds);
|
||||
|
||||
if ($exists) {
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $data['product_id'])
|
||||
->update([
|
||||
'last_price' => $data['last_price'] ?? null,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
} else {
|
||||
\Illuminate\Support\Facades\DB::table('product_vendor')
|
||||
->insert([
|
||||
'vendor_id' => $vendorId,
|
||||
'product_id' => $data['product_id'],
|
||||
'last_price' => $data['last_price'] ?? null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user