revisedSalaryDialog.vue 9.19 KB
<!-- 修订应发工资 -->
<template>
  <baseDialog :dialog-config="dialogConfig" :is-show.sync="visible"
    @handleClick="handleAddConfirm">
    <div class="detail-dialog">
      <glsx-combi-form class="edit-form-container"
                    ref="editFormRef"
                    label-width="140px"
                    :is-block="true"
                    form-ref="editChildRef"
                    :form-obj="infoForm"
                    :form-data="formData"
                    :form-rules="formRules">
        <template slot="syncUnit"><span class="unit">元</span></template>
        <template slot="refundOffset">
          <el-input style="width: 136px;" prefix-icon="el-icon-minus" @input="handleInput($event, 'refundOffset')" placeholder="请输入" v-model="infoForm.refundOffset"></el-input>
          <span class="unit">元</span>
        </template>
        <template slot="customDeduction">
          <el-input style="width: 136px;" @input="handleInput($event, 'customDeduction')" placeholder="输入正负值" v-model="infoForm.customDeduction"></el-input>
          <span class="unit">元</span>
        </template>
        <template slot="correctSalary">
          <el-input style="width: 136px;" placeholder="" disabled v-model="computedValue"></el-input>
          <span class="unit">元</span>
        </template>
      </glsx-combi-form>
      <div class="history-box" v-if="infoForm['historyRemark'].length">
        <div class="line">
          <span class="text">历史备注以及备注时间</span>
        </div>
        <el-timeline>
          <el-timeline-item v-for="(reviewInfo, infoId) in infoForm['historyRemark']"
                            :color="infoId == 0 ? '#2A58AD' : '#999999'"
                            :key="infoId"
                            :timestamp="reviewInfo.updatedDateStr">
            {{ reviewInfo.reviewMessage }}
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
  </baseDialog>
</template>
<script>
import { reviseSalaryApi, getSalaryRecordApi } from '@api/resident'
import { Util} from '@/common/util'
import { limitPrice, limitPriceForNegative } from '@/common/format'
import BigNumber from 'bignumber.js'
import { isEmpty } from '../util.js'
export default {
  name: 'RevisedSalaryDialog',
  props: {
    revisedSalaryVisible: {
      type: Boolean,
      default: false
    },
    revisedSalaryRow: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    var validateRefundOffset = (rule, value, callback) => {
      if (!this.isEmpty(value) && Number(value) <= 0) {
        callback(new Error('输入值必须大于0'))
      } else {
        callback()
      }
    }
    var validateCustomDeduction = (rule, value, callback) => {
      if (!this.isEmpty(value) && Number(value) == 0) {
        callback(new Error('输入值不能为0'))
      } else {
        callback()
      }
    }
    return {
      isEmpty,
      dialogConfig: {
        title: '修订合计应发工资',
        width: '720px'
      },
      formData: [
        {
          label: '当前月份合计应发',
          name: 'currentSalary',
          type: 'input',
          isDisabled: true
        },
        {
          label: '退单冲减',
          name: 'refundOffset',
          addHtml: 'refundOffset'
        },
        {
          label: '正负冲减',
          name: 'customDeduction',
          addHtml: 'customDeduction'
        },
        {
          label: '实发工资',
          name: 'correctSalary',
          addHtml: 'correctSalary'
        },
        {
          label: '备注',
          name: 'remark',
          type: 'input',
          inputType: 'textarea',
          rows: 5,
          maxlength: 500,
          showLimit: true
        }
      ],
      infoForm: {
        correctSalary: '',
        refundOffset: '',
        customDeduction: '',
        remark: '',
        historyRemark: [],
        oldrefundOffset: '',
        oldcustomDeduction: ''
      },
      formRules: {
        correctSalary: [
          { required: true, message: '请输入修订后的应发工资', trigger: 'blur' }
        ],
        refundOffset: [
          { required: false, message: '', trigger: 'blur' },
          { validator: validateRefundOffset, trigger: ['blur', 'change'] }
        ],
        customDeduction: [
          { required: false, message: '', trigger: 'blur' },
          { validator: validateCustomDeduction, trigger: ['blur']}
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.revisedSalaryVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'revisedSalaryVisible' })
      }
    },
    computedValue: {
      get() {
        let currentSalary = new BigNumber(this.infoForm.currentSalary || 0)
        let refundOffset = new BigNumber(this.infoForm.refundOffset || 0)
        let customDeduction = new BigNumber(this.infoForm.customDeduction != '-' ? (this.infoForm.customDeduction || 0) : 0)
        this.$set(this.infoForm, 'correctSalary', `${currentSalary.minus(refundOffset).plus(customDeduction)}`)
        return `${currentSalary.minus(refundOffset).plus(customDeduction)}`
      }
    }
  },
  mounted() {
    // 获取修订工资备注记录
    this.getRecord()
  },
  methods: {
    handleInput(e, key) {
      if (key == 'customDeduction') { 
        this.$set(this.infoForm, key, limitPriceForNegative(e))
      } else {
        let oldValue = `${this.infoForm['old' + key]}`
        let maxlength = key == 'customDeduction' ? 7 : 6
        this.$set(this.infoForm, key, limitPrice(e, oldValue, maxlength, 2))
        this.$set(this.infoForm, 'old' + key, this.infoForm[key])
      }
      // 清除与输入框1和输入框2相关的内容
      const refundOffsetText = !this.isEmpty(this.infoForm['refundOffset']) && !isNaN(this.infoForm['refundOffset']) ? `退单冲减${this.infoForm['refundOffset']},` : ''
      const customDeductionText = !this.isEmpty(this.infoForm['customDeduction']) && !isNaN(this.infoForm['customDeduction']) ? `正负冲减${this.infoForm['customDeduction']},` : ''
      let existingText = this.infoForm.remark
        .replace(/退单冲减[^,]*/g, '')
        .replace(/正负冲减[^,]*/g, '')
        .trim()

      // 添加新的输入框内容
      if (refundOffsetText) {
        existingText += refundOffsetText
      }
      if (customDeductionText) {
        existingText += customDeductionText
      }

      // 更新备注的内容
      this.infoForm.remark = existingText
    },
    limitPriceForNegative(newValue) {
      // 用来检索整数且不超过6位数
      const limitDigit = /[0-9]{1,6}/
      // 用来检索小数且小数点后不超过两位数
      const limitDigitdot = /[0-9]{1,6}.[\d]{0,2}/
      if (newValue) {
        newValue = newValue + ""
        let nn = ""
        // 正负判断
        if (newValue.match("-")) {
          nn = newValue.match("-")[0]
        }
        // 小数判断
        if (newValue.indexOf(".") != -1) {
          if (newValue.match(limitDigitdot)) {
            nn = nn + newValue.match(limitDigitdot)[0] || ""
          }
        } else {
          if (newValue.match(limitDigit)) {
            nn = nn + newValue.match(limitDigit)[0] || ""
          }
        }
        return nn
      }
    },
    getRecord() {
      getSalaryRecordApi({ staffPhone: this.revisedSalaryRow.staffPhone, salaryMonth: this.revisedSalaryRow.salaryMonth, pageSize: 10, currentPage: 1 }).then(res => {
        res?.list?.forEach(item => {
          item.reviewMessage = item.remark
          item.updatedDateStr = Util.dateFormate(item.createdDate, 'YYYY-MM-DD HH:mm:ss')
        })
        this.infoForm.historyRemark = res?.list || []
        this.$set(this.infoForm, 'currentSalary', this.revisedSalaryRow.correctSalary || this.revisedSalaryRow.salary)
      })
    },
    handleAddConfirm() {
      this.$refs.editFormRef?.$refs?.editChildRef?.validate((valid) => {
        if (valid) {
          let params = {
            id: this.revisedSalaryRow.id,
            refundOffset: this.infoForm.refundOffset || '',
            customDeduction: this.infoForm.customDeduction || '',
            correctSalary: this.computedValue,
            remark: this.infoForm.remark
          }
          if (this.computedValue - 0 < 0) return this.$message.warning('实发工资不能小于0')
          if (isNaN(params.customDeduction)) return this.$message.warning('正负冲减值需为数字')
          reviseSalaryApi(params).then(res => {
            this.$message.success('修订成功')
            this.$emit('dialogClose', { flag: true, attrName: 'revisedSalaryVisible' })
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-dialog{
  .unit{
    margin-left: 4px;
  }
  .line{
    width: 100%;
    height: 1px;
    background: #E6E6E6;
    position: relative;
    margin: 32px 0;
    .text{
      position: absolute;
      left: 50%;
      top: -10px;
      transform: translateX(-50%);
      background: #fff;
      padding: 0 16px;
      font-size: 14px;
      color: #00000066;
    }
  }
  ::v-deep .el-timeline-item__node--normal{
    width: 8px;
    height: 8px;
  }
  ::v-deep .el-timeline-item__tail{
    left: 2px
  }
}
</style>