| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- <template>
- <view>
- <cu-custom :backUrl="'/pages/index/index'" :isBack="true" bgColor="bg-gradual-blue">
- <block slot="content">报损申请</block>
- </cu-custom>
- <u--form :model="inputForm" labelWidth="100px" class="u-form" labelPosition="left" :rules="rules"
- ref="inputForm">
- <u-form-item label="报损编号" borderBottom prop="lossNo">
- <u--input placeholder="自动生成" v-model="inputForm.lossNo" disabled></u--input>
- </u-form-item>
- <u-form-item label="经办人" borderBottom prop="handledBy">
- <u--input v-model="inputForm.handledBy" disabled></u--input>
- </u-form-item>
- <u-form-item label="经办部门" borderBottom prop="handledByOfficeName">
- <u--input v-model="inputForm.handledByOfficeName" disabled></u--input>
- </u-form-item>
- <u-form-item label="报损时间" borderBottom prop="lossDate" :required="true">
- <el-date-picker v-model="inputForm.lossDate" type="date" placeholder="请选择报损时间" style="width:100%"
- placement="bottom-start" clearable :disabled="nodeFlag">
- </el-date-picker>
- </u-form-item>
- <u-form-item label="备注" borderBottom prop="remarks">
- <u--textarea placeholder="请填写备注信息" :rows="4" :maxlength="500" v-model="inputForm.remarks"
- :disabled="nodeFlag"></u--textarea>
- </u-form-item>
- <view class="section-wrap">
- <view class="section-title">报损明细</view>
- <view class="section-tip">报损数量按最小单位填写</view>
- <view v-for="(item, index) in inputForm.detailInfos" :key="item.id || index" class="detail-card">
- <view class="detail-card-title">报损详情 {{ index + 1 }}</view>
- <u-form-item label="报损人" :prop="'detailInfos[' + index + '].recipientAgent'" :required="true">
- <u--input v-model="inputForm.detailInfos[index].recipientAgent" placeholder="请选择报损人"
- :disabled="nodeFlag" @focus="openUserPullForm(index)" clearable></u--input>
- </u-form-item>
- <u-form-item label="报损人部门" :prop="'detailInfos[' + index + '].recipientOffice'">
- <u--input v-model="inputForm.detailInfos[index].recipientOffice" disabled></u--input>
- </u-form-item>
- <u-form-item label="报损类型" :prop="'detailInfos[' + index + '].collectType'" :required="true">
- <u--input v-model="inputForm.detailInfos[index].collectType" placeholder="请选择报损类型"
- :disabled="nodeFlag" @focus="openTypePicker(index)" clearable></u--input>
- </u-form-item>
- <loss-goods-selector :rowIndex="index" :inputForm="inputForm" :disabled="nodeFlag"
- @selected="handleGoodsSelected"></loss-goods-selector>
- <u-form-item label="包装规格" :prop="'detailInfos[' + index + '].spec'">
- <u--input v-model="inputForm.detailInfos[index].spec" disabled></u--input>
- </u-form-item>
- <u-form-item label="生产日期" :prop="'detailInfos[' + index + '].produceDate'">
- <u--input :value="formatDisplayDate(inputForm.detailInfos[index].produceDate)" disabled>
- </u--input>
- </u-form-item>
- <!-- <u-form-item label="库存数量" :prop="'detailInfos[' + index + '].surplusNumber'">
- <u--input v-model="inputForm.detailInfos[index].surplusNumber" disabled></u--input>
- </u-form-item> -->
- <!-- <u-form-item label="单位" :prop="'detailInfos[' + index + '].company'">
- <u--input v-model="inputForm.detailInfos[index].company" disabled></u--input>
- </u-form-item> -->
- <u-form-item label="报损数量" :prop="'detailInfos[' + index + '].lossNumber'" :required="true">
- <u--input v-model="inputForm.detailInfos[index].lossNumber" placeholder="请输入报损数量"
- :disabled="nodeFlag" @blur="handleLossNumberBlur(index)"></u--input>
- </u-form-item>
- <u-form-item label="附件上传">
- <UploadComponent :uploadUrl="`${uploadUrl}/public-modules-server/oss/file/webUpload/upload`"
- @onRemove="(file, fileList, fileIndex) => handleRemove(file, fileList, index, fileIndex, 'detail')"
- @onSuccess="(file, fileList) => handleUploadSuccess(file, fileList, index, 'detail')"
- :fileList="inputForm.detailInfos[index].fileInfoLost" :limit="3" :isDelete="nodeFlag"
- :isUpload="nodeFlag">
- </UploadComponent>
- </u-form-item>
- <u-form-item label="" v-if="!nodeFlag">
- <el-button style="width: 100%" type="danger" plain @click="removeDetail(index)">
- 删除报损详情
- </el-button>
- </u-form-item>
- </view>
- <u-form-item label="" v-if="!nodeFlag">
- <el-button style="width: 100%" type="primary" @click="addDetail()" plain>新增报损详情</el-button>
- </u-form-item>
- </view>
- <u-form-item label="附件">
- <UploadComponent :uploadUrl="`${uploadUrl}/public-modules-server/oss/file/webUpload/upload`"
- @onRemove="(file, fileList, fileIndex) => handleRemove(file, fileList, '', fileIndex, '')"
- @onSuccess="(file, fileList) => handleUploadSuccess(file, fileList, '', '')"
- :fileList="inputForm.files" :limit="3" :isDelete="nodeFlag" :isUpload="nodeFlag">
- </UploadComponent>
- </u-form-item>
- </u--form>
- <user-select ref="userPicker" @input="handleUserSelected"></user-select>
- <ba-tree-picker ref="treePicker" :multiple="false" @select-change="selectTypeChange" title="类型选择"
- :localdata="listData" valueKey="value" textKey="label" childrenKey="children" />
- </view>
- </template>
- <script>
- import { mapState } from 'vuex'
- import userSelect from '@/components/user-select/user-select-radio.vue'
- import baTreePicker from '@/components/ba-tree-picker/ba-tree-picker.vue'
- import UploadComponent from '@/pages/common/UploadComponent.vue'
- import upload from '@/utils/upload.js'
- import MaterialTypeService from '@/api/psi/MaterialTypeService'
- import WareHouseService from '@/api/psi/WareHouseService'
- import LossService from '@/api/psi/LossService'
- import CommonApi from '@/api/common/CommonApi'
- import LossGoodsSelector from './LossGoodsSelector.vue'
- export default {
- name: 'PsiLossForm',
- components: {
- userSelect,
- baTreePicker,
- UploadComponent,
- LossGoodsSelector
- },
- computed: mapState({
- userInfo: (state) => state.user.userInfo
- }),
- props: {
- businessId: {
- type: String,
- default: ''
- },
- formReadOnly: {
- type: Boolean,
- default: false
- },
- status: {
- type: String,
- default: ''
- },
- isCc: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- uploadUrl: upload.UPLOAD_URL,
- nodeFlag: false,
- loading: false,
- listData: [],
- materialList: [],
- inputForm: this.createInputForm(),
- rules: {
- lossDate: [
- {
- required: true,
- message: '报损时间不能为空',
- trigger: ['change']
- }
- ]
- }
- }
- },
- materialTypeService: null,
- wareHouseService: null,
- lossService: null,
- commonApi: null,
- created() {
- this.ensureServices()
- this.loadMaterialTypes()
- this.fillUserInfo()
- },
- watch: {
- businessId: {
- handler() {
- if (this.businessId) {
- this.init(this.businessId)
- } else {
- this.resetForm()
- this.nodeFlag = this.formReadOnly || this.status === 'taskFormDetail' || this.status === 'testSee'
- }
- },
- immediate: true,
- deep: false
- }
- },
- methods: {
- createInputForm() {
- return {
- id: '',
- procInsId: '',
- processDefinitionId: '',
- status: '',
- lossNo: '',
- lossDate: '',
- remarks: '',
- handledBy: '',
- handledById: '',
- handledByOffice: '',
- handledByOfficeName: '',
- userId: '',
- files: [],
- detailInfos: []
- }
- },
- createDetailRow() {
- return {
- id: '',
- recipientAgent: (this.userInfo && this.userInfo.name) || '',
- recipientAgentId: (this.userInfo && this.userInfo.id) || '',
- recipientOffice: (this.userInfo && this.userInfo.officeDTO && this.userInfo.officeDTO.name) || '',
- collectType: '',
- collectTypeId: '',
- goodsName: '',
- lossNumber: '',
- company: '',
- produceDate: '',
- shelfLife: '',
- shelfLifeUnit: '',
- spec: '',
- surplusNumber: '',
- currentInventory: '',
- fileInfoLost: []
- }
- },
- ensureServices() {
- if (!this.materialTypeService) {
- this.materialTypeService = new MaterialTypeService()
- }
- if (!this.wareHouseService) {
- this.wareHouseService = new WareHouseService()
- }
- if (!this.lossService) {
- this.lossService = new LossService()
- }
- if (!this.commonApi) {
- this.commonApi = new CommonApi()
- }
- },
- async loadMaterialTypes() {
- try {
- const data = await this.materialTypeService.cgList()
- this.materialList = data || []
- this.listData = this.buildTree(this.materialList)
- } catch (e) {
- this.materialList = []
- this.listData = []
- }
- },
- fillUserInfo() {
- if (!this.userInfo) {
- return
- }
- this.inputForm.handledBy = this.inputForm.handledBy || this.userInfo.name
- this.inputForm.handledById = this.inputForm.handledById || this.userInfo.id
- this.inputForm.userId = this.inputForm.userId || this.userInfo.id
- this.inputForm.handledByOffice = this.inputForm.handledByOffice || (this.userInfo.officeDTO && this.userInfo.officeDTO.id) || ''
- this.inputForm.handledByOfficeName = this.inputForm.handledByOfficeName || (this.userInfo.officeDTO && this.userInfo.officeDTO.name) || ''
- },
- resetForm() {
- this.inputForm = this.createInputForm()
- this.fillUserInfo()
- },
- async init(id) {
- this.ensureServices()
- this.resetForm()
- this.inputForm.id = id
- this.loading = true
- try {
- const data = await this.lossService.findById(id)
- this.inputForm = this.normalizeFormData(this.recover(this.createInputForm(), data))
- this.fillUserInfo()
- this.nodeFlag = await this.resolveNodeFlag(data)
- } catch (e) {
- this.nodeFlag = true
- } finally {
- this.loading = false
- }
- },
- async resolveNodeFlag(data) {
- if (this.formReadOnly || this.status === 'taskFormDetail' || this.status === 'testSee') {
- return true
- }
- try {
- const taskName = await this.commonApi.getTaskNameByProcInsId((data && data.procInsId) || this.inputForm.procInsId)
- if (this.isNotEmpty(taskName)) {
- return taskName !== '发起人重新发起申请'
- }
- } catch (e) {
- }
- return false
- },
- normalizeFormData(data) {
- const form = data || this.createInputForm()
- if (this.isNotEmpty(form.lossDate)) {
- form.lossDate = new Date(form.lossDate)
- }
- form.files = form.files || []
- form.detailInfos = (form.detailInfos || []).map(item => ({
- ...item,
- fileInfoLost: item.fileInfoLost || [],
- produceDate: this.isNotEmpty(item.produceDate) ? new Date(item.produceDate) : '',
- surplusNumber: this.isNotEmpty(item.surplusNumber) ? this.formatNumber(item.surplusNumber) : ''
- }))
- return form
- },
- buildTree(nodes, parentId = '0') {
- const tree = []
- for (const node of nodes || []) {
- if (node.parentId === parentId) {
- const children = this.buildTree(nodes, node.id)
- if (children.length) {
- node.children = children
- }
- tree.push(node)
- }
- }
- return tree
- },
- isEmpty(value) {
- if (value === null || value === undefined) {
- return true
- }
- if (typeof value === 'string' && value.trim() === '') {
- return true
- }
- if (Array.isArray(value) && value.length === 0) {
- return true
- }
- return false
- },
- isNotEmpty(value) {
- return !this.isEmpty(value)
- },
- formatDate(date) {
- if (this.isEmpty(date)) {
- return ''
- }
- const dateValue = new Date(date)
- if (Number.isNaN(dateValue.getTime())) {
- return ''
- }
- const year = dateValue.getFullYear()
- const month = `${dateValue.getMonth() + 1}`.padStart(2, '0')
- const day = `${dateValue.getDate()}`.padStart(2, '0')
- return `${year}-${month}-${day}`
- },
- formatDisplayDate(date) {
- return this.formatDate(date)
- },
- normalizeDateFieldsBeforeValidate() {
- if (this.isNotEmpty(this.inputForm.lossDate)) {
- this.inputForm.lossDate = this.formatDate(this.inputForm.lossDate)
- }
- },
- formatNumber(value) {
- const numberValue = Number(value || 0)
- if (Number.isNaN(numberValue)) {
- return ''
- }
- return numberValue % 1 === 0 ? String(numberValue) : numberValue.toFixed(2).replace(/\.?0+$/, '')
- },
- formatNumberInput(inputValue, decimalLimit = 2) {
- if (this.isEmpty(inputValue)) {
- return ''
- }
- const valueText = String(inputValue)
- if (!/^\d*\.?\d*$/.test(valueText)) {
- return ''
- }
- let value = valueText.replace(/[^\d.]/g, '')
- const dotIndex = value.indexOf('.')
- if (dotIndex !== -1) {
- const substr = value.substr(dotIndex + 1)
- if (substr.indexOf('.') !== -1) {
- value = value.substr(0, dotIndex + 1) + substr.replace(/\./g, '')
- }
- }
- if (dotIndex !== -1) {
- const integerPart = value.substring(0, dotIndex)
- const decimalPart = value.substring(dotIndex + 1, dotIndex + 1 + decimalLimit)
- value = integerPart + '.' + decimalPart
- }
- return value
- },
- addDetail() {
- if (this.inputForm.detailInfos.length > 0) {
- this.$message.warning('报损明细只能保留一条')
- return
- }
- this.inputForm.detailInfos.push(this.createDetailRow())
- },
- removeDetail(index) {
- this.inputForm.detailInfos.splice(index, 1)
- },
- patchDetail(index, patch) {
- const detail = (this.inputForm.detailInfos || [])[index] || {}
- this.$set(this.inputForm.detailInfos, index, {
- ...detail,
- ...patch
- })
- },
- openUserPullForm(index) {
- if (this.nodeFlag) {
- return
- }
- this.$refs.userPicker.open(index, 'detail')
- },
- handleUserSelected(data, index, type) {
- if (type !== 'detail') {
- return
- }
- this.patchDetail(index, {
- recipientAgentId: data.id,
- recipientAgent: data.label,
- recipientOffice: data.parentLabel
- })
- },
- openTypePicker(index) {
- if (this.nodeFlag) {
- return
- }
- this.$refs.treePicker._show(index)
- },
- selectTypeChange(ids, names, index) {
- this.patchDetail(index, {
- collectType: names,
- collectTypeId: ids[0],
- goodsName: '',
- company: '',
- produceDate: '',
- shelfLife: '',
- shelfLifeUnit: '',
- spec: '',
- surplusNumber: '',
- currentInventory: '',
- lossNumber: ''
- })
- },
- async handleGoodsSelected({ index, item }) {
- this.patchDetail(index, {
- goodsName: item.tradeName || '',
- company: item.company || '',
- produceDate: this.isNotEmpty(item.produceDate) ? new Date(item.produceDate) : '',
- shelfLife: item.shelfLife || '',
- shelfLifeUnit: item.shelfLifeUnit || '',
- spec: item.spec || '',
- currentInventory: item.currentInventory || ''
- })
- await this.syncDetailStock(index)
- },
- async syncDetailStock(index) {
- const detail = (this.inputForm.detailInfos || [])[index]
- if (!detail || this.isEmpty(detail.goodsName) || this.isEmpty(detail.collectTypeId)) {
- return
- }
- try {
- const data = await this.wareHouseService.getByProduceDateNotMerge({
- current: 1,
- size: 1000,
- tradeName: detail.goodsName,
- wareHouseType: detail.collectTypeId
- })
- const records = (data && data.records) || []
- const total = records.reduce((sum, item) => {
- const currentInventory = Number(item.currentInventory || 0)
- const spec = Number(item.spec || 1)
- return sum + currentInventory * (spec > 0 ? spec : 1)
- }, 0)
- this.$set(this.inputForm.detailInfos[index], 'surplusNumber', this.formatNumber(total))
- } catch (e) {
- this.$set(this.inputForm.detailInfos[index], 'surplusNumber', '')
- uni.showToast({
- title: '库存数量加载失败',
- icon: 'none'
- })
- }
- },
- handleUploadSuccess(file, fileList, index, type) {
- if (type === 'detail') {
- this.inputForm.detailInfos[index].fileInfoLost = fileList
- } else {
- this.inputForm.files = fileList
- }
- },
- handleRemove(file, fileList, lineIndex, fileIndex, type) {
- if (type === 'detail') {
- this.inputForm.detailInfos[lineIndex].fileInfoLost.splice(fileIndex, 1)
- } else {
- this.inputForm.files.splice(fileIndex, 1)
- }
- },
- handleLossNumberBlur(index) {
- const detail = (this.inputForm.detailInfos || [])[index]
- if (!detail) {
- return
- }
- detail.lossNumber = this.formatNumberInput(detail.lossNumber)
- if (this.isNotEmpty(detail.surplusNumber) && this.isNotEmpty(detail.lossNumber) &&
- Number(detail.lossNumber) > Number(detail.surplusNumber)) {
- this.$message.error('报损数量不能大于当前库存')
- detail.lossNumber = ''
- }
- },
- validateDetailInfos(checkStock = true) {
- if (this.isEmpty(this.inputForm.detailInfos)) {
- this.$message.error('请填写报损明细')
- return false
- }
- if (this.inputForm.detailInfos.length !== 1) {
- this.$message.error('报损明细只能保留一条')
- return false
- }
- const detail = this.inputForm.detailInfos[0]
- if (this.isEmpty(detail.recipientAgentId)) {
- this.$message.error('请选择报损人')
- return false
- }
- if (this.isEmpty(detail.collectTypeId)) {
- this.$message.error('请选择报损类型')
- return false
- }
- if (this.isEmpty(detail.goodsName)) {
- this.$message.error('请选择物品名称')
- return false
- }
- if (this.isEmpty(detail.lossNumber)) {
- this.$message.error('请输入报损数量')
- return false
- }
- if (checkStock && this.isNotEmpty(detail.surplusNumber) && Number(detail.lossNumber) > Number(detail.surplusNumber)) {
- this.$message.error('报损数量不能大于当前库存')
- return false
- }
- return true
- },
- toSubmitData() {
- const data = JSON.parse(JSON.stringify(this.inputForm))
- data.lossDate = this.formatDate(this.inputForm.lossDate)
- data.detailInfos = (this.inputForm.detailInfos || []).map(item => ({
- ...JSON.parse(JSON.stringify(item)),
- produceDate: this.formatDate(item.produceDate)
- }))
- return data
- },
- async saveForm(callback) {
- this.loading = true
- try {
- const submitData = this.toSubmitData()
- submitData.status = '1'
- this.inputForm.status = '1'
- const data = await this.lossService.save(submitData)
- callback(data.businessTable, data.businessId, submitData)
- } finally {
- this.loading = false
- }
- },
- async startForm(callback) {
- this.loading = true
- try {
- if (this.isNotEmpty(this.inputForm.id)) {
- const data = await this.lossService.findById(this.inputForm.id)
- if (this.isNotEmpty(data.status) && !['0', '1', '3'].includes(data.status)) {
- this.$message.error('任务数据已发生改变或不存在,请在待办任务中确认此任务是否存在')
- throw new Error('invalid status')
- }
- }
- await this.startFormTrue(callback)
- } finally {
- this.loading = false
- }
- },
- async startFormTrue(callback) {
- this.normalizeDateFieldsBeforeValidate()
- await this.$refs.inputForm.validate()
- if (!this.validateDetailInfos(true)) {
- return
- }
- const submitData = this.toSubmitData()
- submitData.status = '2'
- this.inputForm.status = '2'
- const data = await this.lossService.save(submitData)
- this.inputForm.id = data.businessId
- callback(data.businessTable, data.businessId, submitData)
- },
- async reapplyForm(callback) {
- this.loading = true
- try {
- const data = await this.lossService.findById(this.inputForm.id)
- if (data.status !== '4') {
- this.$message.error('任务数据已发生改变或不存在,请在待办任务中确认此任务是否存在')
- throw new Error('invalid status')
- }
- await this.startFormTrue(callback)
- } finally {
- this.loading = false
- }
- },
- async agreeForm(callback) {
- this.loading = true
- try {
- this.normalizeDateFieldsBeforeValidate()
- await this.$refs.inputForm.validate()
- if (!this.validateDetailInfos(false)) {
- return
- }
- const submitData = this.toSubmitData()
- submitData.status = '5'
- this.inputForm.status = '5'
- const data = await this.lossService.save(submitData)
- callback(data.businessTable, data.businessId, submitData)
- } finally {
- this.loading = false
- }
- },
- async updateStatusById(type, callback) {
- this.loading = true
- try {
- if (type === 'reject' || type === 'reback') {
- const data = await this.lossService.findById(this.inputForm.id)
- if (data.status !== '2') {
- this.$message.error('任务数据已发生改变或不存在,请在待办任务中确认此任务是否存在')
- throw new Error('invalid status')
- }
- const nextStatus = type === 'reject' ? '4' : '3'
- this.inputForm.status = nextStatus
- await this.lossService.updateStatusById({
- status: nextStatus,
- id: this.inputForm.id
- })
- callback()
- } else if (type === 'hold') {
- const data = await this.lossService.findById(this.inputForm.id)
- if (data.status !== '4') {
- this.$message.error('任务数据已发生改变或不存在,请在待办任务中确认此任务是否存在')
- throw new Error('invalid status')
- }
- this.inputForm.status = '1'
- await this.lossService.updateStatusById({
- status: '1',
- id: this.inputForm.id
- })
- callback()
- }
- } finally {
- this.loading = false
- }
- }
- }
- }
- </script>
- <style scoped>
- .section-wrap {
- margin-top: 24rpx;
- }
- .section-title {
- margin-bottom: 8rpx;
- padding-left: 8rpx;
- font-size: 30rpx;
- font-weight: 700;
- color: #1f2937;
- border-left: 6rpx solid #2979ff;
- }
- .section-tip {
- margin-bottom: 16rpx;
- font-size: 24rpx;
- color: #64748b;
- }
- .detail-card {
- margin-bottom: 20rpx;
- padding: 24rpx;
- background: #fff;
- border-radius: 20rpx;
- box-shadow: 0 10rpx 30rpx rgba(15, 23, 42, 0.05);
- }
- .detail-card-title {
- margin-bottom: 12rpx;
- font-size: 28rpx;
- font-weight: 600;
- color: #0f172a;
- }
- </style>
|