- 新增 app/lib/billOwnerScope.js:仓库可访问性(主体 + 所在公司)、明细商品/组合 与仓库同货主校验、按单据仓库取货主 - 入库单/出库单 create:仓库可访问且明细商品必须与仓库同货主 (原明细校验未带 principal_id,一并修正) - 入库单/出库单 update:改仓库时校验新仓库可访问且已有明细同货主 - 明细接口(stock_items create/update):按所属单据仓库货主校验商品,堵住 单据建好后再塞入其它货主商品的路径 - 前端新增 useWarehouseGoodsScope:仓库与商品双向约束(先选仓库限制商品、 先选商品限制仓库、同单明细不得跨货主) - 入库/出库创建页与单据明细编辑区接入该约束并给出提示 - ProductSelect 新增 beforeSelect 前置校验与受控显示值,校验不通过时回滚显示
102 lines
4.6 KiB
TypeScript
102 lines
4.6 KiB
TypeScript
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<number | null>
|
||
warehouses: Ref<WarehouseOwnerLike[]>
|
||
lineOwners: () => Array<number | null | undefined>
|
||
clearMismatchedGoods: (warehouseOwnerId: number) => number
|
||
}) {
|
||
const { warehouseId, warehouses, lineOwners, clearMismatchedGoods } = options
|
||
|
||
/** 当前所选仓库的所属货主(null 表示未选仓库或仓库未关联货主) */
|
||
const warehouseOwnerId = computed<number | null>(() => {
|
||
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<number[]>(() => [
|
||
...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<number | null>(() => (goodsOwnerIds.value.length === 1 ? goodsOwnerIds.value[0] : null))
|
||
|
||
/** 商品搜索应使用的货主过滤:优先仓库货主,其次已选商品的货主 */
|
||
const scopeSubsidiaryId = computed<number | null>(() => 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 }
|
||
}
|