修复BUG

This commit is contained in:
liu
2026-08-27 14:20:09 +08:00
parent 513cc568a9
commit 52dc6efadf
203 changed files with 576 additions and 1130 deletions
+55
View File
@@ -4,6 +4,7 @@ namespace App\Services;
use App\Models\ProductModel;
use App\Models\PurchaseOrderModel;
use App\Models\StoreModel;
use App\Models\StoreOrderItemModel;
use App\Models\StoreOrderModel;
use Illuminate\Database\Eloquent\Collection;
@@ -138,6 +139,60 @@ class PurchaseItemService
return $this->sortRows($rows);
}
/**
* 供应商采购明细矩阵(导出用):该供应商在采购单内的商品行按市场分组,
* 行附「汇总数量 + 各门店数量」;门店列=本采购单内有该供应商明细的门店(按ID排序)
*
* @return array{
* stores: array<int, string>,
* markets: array<string, array<int, array<string, mixed>>>
* }
*/
public function supplierMarketMatrix(int $purchaseId, int $supplierId): array
{
$items = $this->purchaseItems($purchaseId)
->where('supplier_id', $supplierId)
->sortBy('id');
$storeIds = $items->pluck('store_id')
->map(static fn ($id): int => (int) $id)
->unique()->sort()->values()->all();
$stores = StoreModel::withTrashed()
->whereIn('id', $storeIds)
->orderBy('id')
->pluck('name', 'id')
->toArray();
$rows = [];
foreach ($items->groupBy('product_id') as $productId => $group) {
$first = $group->first();
$quantity = 0;
$storeQuantities = array_fill_keys(array_map('intval', array_keys($stores)), 0);
foreach ($group as $item) {
$quantity += (int) $item->quantity;
$storeQuantities[(int) $item->store_id] += (int) $item->quantity;
}
$rows[] = [
'product_id' => (int) $productId,
'product_name' => $first->product_name,
'market' => (string) data_get($first, 'market', ''),
'quantity' => $quantity,
'store_quantities' => $storeQuantities,
'category_sort' => (int) data_get($first, 'category_sort', 9999),
'product_sort' => (int) data_get($first, 'product_sort', 9999),
];
}
// 排序后按市场分组(组间/组内均保持 分类sort → 商品sort 顺序)
$markets = [];
foreach ($this->sortRows($rows) as $row) {
$markets[$row['market']][] = $row;
}
return ['stores' => $stores, 'markets' => $markets];
}
/**
* 采购单订货明细(关联订单过滤软删、成本价可见、附分类/商品排序键)
*