WEB/src/views/storage/stockcheck/pages/CheckFormDialog.vue
2026-10-01 18:02:41 +08:00

267 lines
8.0 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!--
CheckFormDialog — 新建盘点单(stock.check.create)
字段说明(接口文档):
- title 盘点名称,非必填
- type 盘点范围:all 按库位(缺省,未传库位时后端自动拉取全仓启用库位)
/ location 按库位(指定库位)/ store 按货主按商品 / goods 按商品
—— all/location 生成「库位明细行」,store/goods 生成「商品明细行」
- mode 盘点方式:open 明盘 / blind 盲盘
—— 库位类(all / location)固定为 blind 盲盘且不可更改;商品类(store / goods)默认明盘
- store_id 货主ID(type=store 时必填)
- remark 备注
盘点时间不在本表单维护:主表已无 check_date,start_time(首个盘点明细写入时间)/
end_time(完成盘点时间)由盘点作业流程自动回填,前端不传。
warehouse_id 由服务端注入,前端不传。
-->
<template>
<el-dialog
:model-value="visible"
title="新建盘点单"
width="min(560px, 94vw)"
align-center
destroy-on-close
:close-on-click-modal="false"
@update:model-value="handleVisibleChange"
@closed="handleClosed"
>
<el-form
ref="formRef"
:model="form"
:rules="rules"
label-width="90px"
size="small"
>
<el-form-item label="盘点名称" prop="title">
<el-input v-model="form.title" maxlength="50" show-word-limit placeholder="如:9月底全盘(可留空)" />
</el-form-item>
<el-form-item label="盘点范围" prop="type">
<el-radio-group v-model="form.type">
<el-radio value="all">按库位(默认全仓)</el-radio>
<el-radio value="location">按库位(指定库位)</el-radio>
<el-radio value="store">按货主按商品</el-radio>
<el-radio value="goods">按商品</el-radio>
</el-radio-group>
<div class="type-tip">
{{ isLocationType
? '库位盘点:明细行为「库位」(一行一个库位,行内按商品聚合),创建后添加盘点库位。'
: '商品盘点:明细行为「商品」(一行一个库存商品),创建后从库存中挑选商品。' }}
</div>
</el-form-item>
<el-form-item v-if="form.type === 'store'" label="货主" prop="store_id">
<el-select
v-model="form.store_id"
placeholder="请选择货主"
filterable
clearable
style="width: 100%"
>
<el-option
v-for="opt in storeOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
</el-form-item>
<el-form-item label="盘点方式" prop="mode">
<el-radio-group v-model="form.mode" :disabled="isLocationType">
<el-radio value="open">明盘</el-radio>
<el-radio value="blind">盲盘</el-radio>
</el-radio-group>
<span v-if="isLocationType" class="form-tip form-tip--lock">
库位盘点固定为盲盘(不可更改):盘点作业时前端不显示账面数量
</span>
<span v-else-if="form.mode === 'blind'" class="form-tip">盲盘:盘点作业时前端不显示账面数量</span>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" type="textarea" :rows="3" maxlength="200" show-word-limit placeholder="可留空" />
</el-form-item>
<el-alert
type="info"
:closable="false"
show-icon
:title="createAlertText"
/>
</el-form>
<template #footer>
<el-button size="small" @click="handleVisibleChange(false)">取消</el-button>
<el-button size="small" type="primary" :loading="submitting" @click="handleSubmit">
确认创建
</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import {
isCheckApiSuccess,
isLocationScopeType,
loadStoreOptions,
storeOptions
} from '../config/template-config'
import {
stockCheckCreate,
type StockCheckMode,
type StockCheckType
} from '@/api/stock.check/stock.check'
defineOptions({ name: 'CheckFormDialog' })
const props = defineProps<{ visible: boolean }>()
const emit = defineEmits<{
(e: 'update:visible', value: boolean): void
/** 创建成功(携创建出的盘点单关键信息;type 用于决定后续添加「商品」还是「库位」) */
(e: 'success', task: { id: number; check_no: string; title: string; type: string }): void
}>()
const formRef = ref<FormInstance>()
const submitting = ref(false)
const form = reactive<{
title: string
type: StockCheckType
store_id: number | undefined
mode: StockCheckMode
remark: string
}>({
title: '',
type: 'all',
store_id: undefined,
mode: 'blind',
remark: ''
})
const rules: FormRules = {
store_id: [
{
validator: (_rule, value, callback) => {
if (form.type === 'store' && !value) callback(new Error('按货主盘点时必须选择货主'))
else callback()
},
trigger: 'change'
}
]
}
const isLocationType = computed(() => isLocationScopeType(form.type))
/** 库位类盘点(all/location)固定盲盘、不可更改;切回商品类恢复默认明盘 */
watch(
() => form.type,
(type) => {
form.mode = isLocationScopeType(type) ? 'blind' : 'open'
}
)
/** 库位类任务创建后要添加的是「库位」而不是「商品」 */
const createAlertText = computed(() => {
if (!isLocationType.value) {
return '创建后为「空盘点单」,需再添加盘点商品并提交审核。'
}
return form.type === 'all'
? '创建后为「空盘点单」,需再添加盘点库位并提交审核;不选库位时由后端自动拉取全仓库启用库位生成盘点范围。'
: '创建后为「空盘点单」,需再添加盘点库位并提交审核。'
})
function handleVisibleChange(value: boolean) {
emit('update:visible', value)
}
function handleClosed() {
formRef.value?.clearValidate()
submitting.value = false
}
function resetForm() {
form.title = ''
form.type = 'all'
form.store_id = undefined
form.mode = 'blind'
form.remark = ''
}
watch(
() => props.visible,
(visible) => {
if (!visible) return
resetForm()
formRef.value?.clearValidate()
// 货主下拉(按货主盘点 / 后续筛选共用)
void loadStoreOptions()
}
)
async function handleSubmit() {
if (!formRef.value) return
try {
await formRef.value.validate()
} catch {
return
}
submitting.value = true
try {
const res: any = await stockCheckCreate({
title: form.title.trim() || undefined,
type: form.type,
mode: form.mode,
store_id: form.type === 'store' ? form.store_id : undefined,
remark: form.remark.trim() || undefined
})
if (!isCheckApiSuccess(res)) {
throw new Error(res?.message || '创建盘点单失败')
}
const payload: any =
res.data && (res.data.id || res.data.check_no) ? res.data : res
const task = {
id: Number(payload.id) || 0,
check_no: String(payload.check_no ?? ''),
title: String(payload.title ?? form.title ?? ''),
type: String(payload.type ?? form.type ?? '')
}
ElMessage.success(task.check_no ? `盘点单 ${task.check_no} 创建成功` : '盘点单创建成功')
emit('update:visible', false)
emit('success', task)
} catch (e: any) {
console.error('[stockcheck] 创建盘点单失败', e)
ElMessage.error(e?.message || '创建盘点单失败')
} finally {
submitting.value = false
}
}
</script>
<style lang="scss" scoped>
.form-tip {
margin-left: 10px;
font-size: 12px;
color: #e6a23c;
}
.form-tip--lock {
margin-left: 0;
width: 100%;
line-height: 1.5;
color: #909399;
}
.type-tip {
width: 100%;
margin-top: 2px;
font-size: 12px;
line-height: 1.5;
color: #909399;
}
</style>