revisedSalaryDialog.vue 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
<!-- 修订应发工资 -->
<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">
          <div v-for="(item, index) in customDeductionList" :key="index" class="custom_item">
            <el-input style="width: 160px; margin-right: 5px;" @input="handleInput($event.trim(), 'customDeduction', index, true)" maxlength="10" placeholder="输入项目名称" v-model.trim="item.itemName"></el-input>
            <el-input style="width: 100px;" @input="handleInput($event, 'customDeduction', index)" placeholder="输入正负值" v-model.trim="item.customDeduction"></el-input>
            <span class="unit">元</span>
            <el-button type="text" class="button_text" v-if="customDeductionList.length > 1" @click="delArr(index)">删除</el-button>
            <el-button type="text" class="button_text" @click="addArr" v-if="index == (customDeductionList.length - 1) && customDeductionList.length < 10">新增</el-button>
          </div>
          <div style="color: #2A58AD;background-color: #F8FAFF;display: inline-block;padding: 0 6px;">汇总金额:{{ computedOtherValue }}元</div>
        </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'
import { deepClone } from "@/common/util"
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: 'salary',
          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',
          placeholder: '描述补充应发和应扣项目明细',
          rows: 5,
          maxlength: 500,
          showLimit: true
        }
      ],
      infoForm: {
        correctSalary: '',
        refundOffset: '',
        remark: '',
        historyRemark: [],
        oldrefundOffset: '',
      },
      formRules: {
        correctSalary: [
          { required: true, message: '请输入修订后的应发工资', trigger: 'blur' }
        ],
        refundOffset: [
          { required: false, message: '', trigger: 'blur' },
          { validator: validateRefundOffset, trigger: ['blur', 'change'] }
        ],
        remark: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ]
      },
      customDeductionList: [{
        itemName: '默认应发应扣',
        customDeduction: 0
      }]
    }
  },
  computed: {
    visible: {
      get() {
        return this.revisedSalaryVisible
      },
      set(newVal) {
        this.$emit('dialogClose', { flag: newVal, attrName: 'revisedSalaryVisible' })
      }
    },
    computedValue: {
      get() {
        let salary = new BigNumber(this.infoForm.salary || 0)
        let refundOffset = new BigNumber(this.infoForm.refundOffset || 0)
        let dynamicItemsTotal = new BigNumber(0);
        if (this.customDeductionList && this.customDeductionList.length > 0) {
          this.customDeductionList.forEach(item => {
            if (item.customDeduction && !isNaN(item.customDeduction)) {
              dynamicItemsTotal = dynamicItemsTotal.plus(new BigNumber(item.customDeduction))
            }
          })
        }
        const correctedSalary = salary.minus(refundOffset).plus(dynamicItemsTotal)
        this.$set(this.infoForm, 'correctSalary', correctedSalary.toString())
        return correctedSalary.toString()
      }
    },
    computedOtherValue: {
      get() {
        let dynamicItemsTotal = new BigNumber(0)
        if (this.customDeductionList && this.customDeductionList.length > 0) {
          this.customDeductionList.forEach(item => {
            if (item.customDeduction && !isNaN(item.customDeduction) && item.itemName) {
              dynamicItemsTotal = dynamicItemsTotal.plus(new BigNumber(item.customDeduction))
            }
          })
        }
        return dynamicItemsTotal.toString()
      }
    }
  },
  mounted() {
    // 获取修订工资备注记录
    this.getRecord()
    // 处理记录的修改内容
    this.setDetailInfo()
  },
  methods: {
    setDetailInfo() {
      this.customDeductionList = this.revisedSalaryRow?.customDeductionList?.length ? deepClone(this.revisedSalaryRow.customDeductionList) : [{
        itemName: '默认应发应扣',
        customDeduction: 0
      }]
      this.infoForm.refundOffset = this.revisedSalaryRow?.refundOffset || ''
      // 处理备注
      this.updateDynamicItemsRemark()
    },
    // 其他应发应扣 新增一行
    addArr() {
      this.customDeductionList.push({
        itemName: '',
        customDeduction: ''
      })
    },
    delArr(index) {
      this.customDeductionList.splice(index, 1)
      this.updateDynamicItemsRemark()
    },
    handleInput(e, key, index, isNameField = false) {
      if (key == 'customDeduction') { 
        if (!isNameField) {
          this.$set(this.customDeductionList[index], key, this.limitPriceForNegative(e))
        } else {
          this.$set(this.customDeductionList[index], 'itemName', e)
        }
        // 更新备注中动态其他应发应扣部分
        this.updateDynamicItemsRemark()
      } 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']}元,` : ''
      let flag = this.infoForm.remark.includes('其他应发/应扣') && !this.infoForm.remark.includes('退单冲减')
      let existingText = this.infoForm.remark
        .replace(/退单冲减:[^,]*/g, '')
        .trim()
      if (flag) existingText = `${existingText},`
      // 添加新的输入框内容
      if (refundOffsetText) {
        existingText += refundOffsetText
      }

      // 更新备注的内容
      existingText = existingText.replace(/[,,]$/, '')
      this.infoForm.remark = existingText
    },
    updateDynamicItemsRemark() {
      console.log('123123')
      let existingText = this.infoForm.remark
        .replace(/退单冲减:[^,]*/g, '')
        .replace(/其他应发\/应扣:[^,]*/g, '')
        .trim()
      const dynamicItemsText = this.generateDynamicItemsText()
      if (dynamicItemsText) {
        existingText += dynamicItemsText
      }
      const refundOffsetText = !this.isEmpty(this.infoForm['refundOffset']) && !isNaN(this.infoForm['refundOffset']) ? `退单冲减:${this.infoForm['refundOffset']}元,` : ''
      if (refundOffsetText) {
        existingText += refundOffsetText
      }
      existingText = existingText.replace(/[,,]$/, '')
      this.infoForm.remark = existingText
    },
    generateDynamicItemsText() {
      if (!this.customDeductionList || this.customDeductionList.length === 0) {
        return ''
      }
      const validItems = this.customDeductionList.filter(item => 
        item.itemName && !((item.customDeduction ?? '') === '') && !isNaN(item.customDeduction)
      )
      if (validItems.length === 0) {
        return ''
      }
      let itemsText = '其他应发/应扣:';
      validItems.forEach((item, index) => {
        if (index > 0) itemsText += ',';
        itemsText += `${item.itemName}${item.customDeduction >= 0 ? '+' : ''}${item.customDeduction}元`
      });
      
      return itemsText + ','
    },
    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, 'salary', this.revisedSalaryRow.salary)
      })
    },
    handleAddConfirm() {
      this.$refs.editFormRef?.$refs?.editChildRef?.validate((valid) => {
        if (valid) {
          if (!this.validateDynamicItems()) return
          const cleanedItems = this.cleanDynamicItems() // 将填写不全的内容剔除掉
          if (cleanedItems?.length == 0) return this.$message.warning('其他应发应扣至少填写一项')
          let params = {
            id: this.revisedSalaryRow.id,
            refundOffset: this.infoForm.refundOffset || '',
            correctSalary: this.computedValue,
            customDeductionList: cleanedItems.map(item => ({
              itemName: item.itemName.trim(),
              customDeduction: Number(item.customDeduction)
            })),
            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' })
          })
        }
      })
    },
    validateDynamicItems() {
      let hasValidEntry = false
      let hasValid = true
      const nameSet = new Set()
      for (let i = 0; i < this.customDeductionList.length; i++) {
        const item = this.customDeductionList[i]
        const deduction = item.customDeduction ?? ""
        // 金额非空时必须是有效数字
        if (deduction !== "" && isNaN(Number(deduction))) {
          this.$message.warning(`金额必须为有效数字`)
          return false
        }
        const hasName = item.itemName && item.itemName.trim() !== ""
        const hasAmount = deduction !== ""
        if (hasName) {
          const trimmedName = item.itemName.trim()
          if (nameSet.has(trimmedName)) {
            this.$message.warning(`项目名称不能重复`)
            return false
          }
          nameSet.add(trimmedName)
        }
        // 判断是否有至少一行同时填写了项目名称和金额
        if (hasName && hasAmount) {
          hasValidEntry = true
        } else if (hasName || hasAmount) {
          hasValid = false
        }
      }
      if (!hasValidEntry) {
        this.$message.warning(`请至少填写一项完整的项目名称和金额`)
        return false
      }
      if (!hasValid) {
        this.$message.warning('请完成配置其他应发/应扣')
        return false
      }
      return true
    },
    cleanDynamicItems() {
      return this.customDeductionList.filter(item => {
        const hasName = item.itemName && item.itemName.trim() !== ''
        const hasAmount = item.customDeduction !== '' && !isNaN(item.customDeduction)
        return hasName && hasAmount
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.detail-dialog{
  max-height: calc(68vh - 130px);
  overflow: auto;
  .custom_item{
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .button_text {
      margin-left: 10px;
      font-size: 14px;
    }
  }
  .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>