import { computed, watch, type Ref } from 'vue' import { ElMessage } from 'element-plus' /** 仓库下拉项中本组合式函数关心的最小字段集 */ export interface WarehouseOwnerLike { id: number subsidiary_id?: number | null } /** * 出入库单「仓库 ↔ 商品」货主双向约束 * * 单据货主由仓库决定,明细商品必须与仓库同货主: * - 先选仓库:商品搜索只保留同货主商品(由 ProductSelect 的 subsidiaryId 生效) * - 先选商品:仓库下拉只保留同货主仓库;两者冲突时自动清空并提示 * @param options 配置 * @param options.warehouseId 单据头仓库(可写,冲突时会被清空) * @param options.warehouses 全部仓库选项 * @param options.lineOwners 明细当前已选商品的所属货主(未选商品给 null) * @param options.clearMismatchedGoods 清空与仓库货主不一致的明细商品,返回清空行数 * @return 派生状态:仓库货主 / 商品货主 / 是否跨货主 / 可选仓库 */ export function useWarehouseGoodsScope(options: { warehouseId: Ref warehouses: Ref lineOwners: () => Array clearMismatchedGoods: (warehouseOwnerId: number) => number }) { const { warehouseId, warehouses, lineOwners, clearMismatchedGoods } = options /** 当前所选仓库的所属货主(null 表示未选仓库或仓库未关联货主) */ const warehouseOwnerId = computed(() => { if (warehouseId.value == null) return null const hit = warehouses.value.find(item => Number(item.id) === Number(warehouseId.value)) const sid = hit?.subsidiary_id return sid === null || sid === undefined ? null : Number(sid) }) /** 明细商品的货主集合(忽略未选商品与未关联货主的商品) */ const goodsOwnerIds = computed(() => [ ...new Set(lineOwners().filter((id): id is number => id !== null && id !== undefined).map(Number)), ]) /** 明细商品是否已跨货主(选择了两个不同货主的商品) */ const goodsOwnerConflict = computed(() => goodsOwnerIds.value.length > 1) /** 明细商品的统一货主(未选 / 跨货主时为 null) */ const goodsOwnerId = computed(() => (goodsOwnerIds.value.length === 1 ? goodsOwnerIds.value[0] : null)) /** 商品搜索应使用的货主过滤:优先仓库货主,其次已选商品的货主 */ const scopeSubsidiaryId = computed(() => warehouseOwnerId.value ?? goodsOwnerId.value) /** 仓库下拉:已选商品有货主时只保留同货主仓库 */ const selectableWarehouses = computed(() => { const sid = goodsOwnerId.value if (sid === null) return warehouses.value return warehouses.value.filter(item => item.subsidiary_id == null || Number(item.subsidiary_id) === sid) }) /** 校验待选商品是否能加入单据(同一单据明细必须同货主) */ function canPickGoods(goodsOwnerIdOfProduct: number | null | undefined, exceptIndex?: number): boolean { const sid = goodsOwnerIdOfProduct === null || goodsOwnerIdOfProduct === undefined ? null : Number(goodsOwnerIdOfProduct) if (sid === null) return true // 同一行改选商品时排除该行原有货主,避免自己挡住自己 const otherOwners = lineOwners() .filter((_, index) => index !== exceptIndex) .filter((id): id is number => id !== null && id !== undefined) .map(Number) if (otherOwners.some(id => id !== sid)) { ElMessage.warning('同一单据的明细商品必须属于同一货主(所属公司)') return false } const current = warehouseOwnerId.value if (current !== null && current !== sid) { ElMessage.warning('该商品不属于所选仓库的货主(所属公司)') return false } return true } // 换仓库:清空与仓库货主不一致的已选商品 watch(warehouseOwnerId, sid => { if (sid === null) return const cleared = clearMismatchedGoods(sid) if (cleared > 0) ElMessage.warning(`已清空 ${cleared} 行与所选仓库货主不一致的商品,请重新选择`) }) // 先选商品后选仓库:仓库货主与商品不一致时清空仓库,避免提交后才发现 watch(goodsOwnerId, sid => { if (sid === null) return const current = warehouseOwnerId.value if (current !== null && current !== sid) { warehouseId.value = null ElMessage.warning('已清空仓库:与所选商品的货主(所属公司)不一致') } }) return { warehouseOwnerId, goodsOwnerId, goodsOwnerConflict, scopeSubsidiaryId, selectableWarehouses, canPickGoods } }