modify.vue 18.6 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 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
<template>
  <view class="page">
    <scroll-view class="scroll" scroll-y>
      <!-- 基础信息(详情回显,只读) -->
      <uni-list>
        <uni-list-item title="码单编号">
          <template v-slot:footer>
            <text class="readonly-text">{{ form.weighingTicketCode || '-' }}</text>
          </template>
        </uni-list-item>
        <uni-list-item title="来料单位">
          <template v-slot:footer>
            <uni-easyinput :value="form.incomingUnit" :inputBorder="false" disabled />
          </template>
        </uni-list-item>
        <uni-list-item title="计划单编号">
          <template v-slot:footer>
            <uni-easyinput :value="form.planCodes" :inputBorder="false" disabled />
          </template>
        </uni-list-item>
        <uni-list-item title="采购处">
          <template v-slot:footer>
            <uni-easyinput :value="form.purchaseDepartmentName" :inputBorder="false" disabled />
          </template>
        </uni-list-item>
        <uni-list-item title="区域">
          <template v-slot:footer>
            <uni-easyinput :value="form.regionName" :inputBorder="false" disabled />
          </template>
        </uni-list-item>
      </uni-list>

      <!-- 明细数据(详情回显,入库数量/入库单价/入库金额/实际吨损耗可编辑) -->
      <view class="sub-table">
        <view class="header">
          <image class="opCollapse" src="/static/images/title.png" />
          <text class="title">明细数据</text>
        </view>
        <view class="add-list">
          <view v-if="!lineList.length" class="empty-tip">暂无明细</view>
          <view v-for="(item, idx) in lineList" :key="'l-' + (item.rowId || item.id || idx)" class="block">
            <uni-list>
              <uni-list-item title="买断品种">
                <template v-slot:footer>
                  <uni-easyinput :value="item.productName" :inputBorder="false" disabled />
                </template>
              </uni-list-item>
              <uni-list-item title="买断数量(kg)">
                <template v-slot:footer>
                  <uni-easyinput :value="item.buyoutQuantity" :inputBorder="false" disabled />
                </template>
              </uni-list-item>
              <uni-list-item title="买断单价">
                <template v-slot:footer>
                  <uni-easyinput :value="item.buyoutPrice" :inputBorder="false" disabled />
                </template>
              </uni-list-item>
              <uni-list-item title="买断金额">
                <template v-slot:footer>
                  <uni-easyinput :value="item.buyoutAmount" :inputBorder="false" disabled />
                </template>
              </uni-list-item>
              <uni-list-item>
                <template v-slot:body>
                  <view class="item-title"><text class="required">*</text><text>入库数量(kg)</text></view>
                </template>
                <template v-slot:footer>
                  <uni-easyinput v-model="item.instockQuantity" type="digit" :inputBorder="false" placeholder="请输入入库数量" @input="recalcDiffAmount(item)" />
                </template>
              </uni-list-item>
              <uni-list-item>
                <template v-slot:body>
                  <view class="item-title"><text class="required">*</text><text>入库单价</text></view>
                </template>
                <template v-slot:footer>
                  <uni-easyinput v-model="item.instockPrice" type="digit" :inputBorder="false" placeholder="请输入入库单价" />
                </template>
              </uni-list-item>
              <uni-list-item>
                <template v-slot:body>
                  <view class="item-title"><text class="required">*</text><text>入库金额</text></view>
                </template>
                <template v-slot:footer>
                  <uni-easyinput v-model="item.instockAmount" type="digit" :inputBorder="false" placeholder="请输入入库金额" />
                </template>
              </uni-list-item>
              <uni-list-item title="预计吨损耗(元/吨)">
                <template v-slot:footer>
                  <uni-easyinput :value="item.expectTonLoss" :inputBorder="false" disabled />
                </template>
              </uni-list-item>
              <uni-list-item>
                <template v-slot:body>
                  <view class="item-title"><text class="required">*</text><text>实际吨损耗(元/吨)</text></view>
                </template>
                <template v-slot:footer>
                  <uni-easyinput v-model="item.actualTonLoss" type="digit" :inputBorder="false" placeholder="请输入实际吨损耗" @input="recalcTonLossDiff(item)" />
                </template>
              </uni-list-item>
              <uni-list-item title="吨损耗差异(元/吨)">
                <template v-slot:footer>
                  <uni-easyinput :value="item.tonLossDiff" :inputBorder="false" disabled placeholder="预计-实际自动计算" />
                </template>
              </uni-list-item>
              <uni-list-item title="差异金额(元)">
                <template v-slot:footer>
                  <uni-easyinput :value="item.diffAmount" :inputBorder="false" disabled placeholder="入库数量×吨损耗差异/1000" />
                </template>
              </uni-list-item>
            </uni-list>
          </view>
        </view>
      </view>

      <!-- 合计 -->
      <view class="total-card">
        <view class="total-title">合计</view>
        <view class="total-body">
          <view class="total-item">
            <text class="total-label">买断数量(kg)</text>
            <text class="total-value">{{ totalSummary.totalBuyoutQuantity || '-' }}</text>
          </view>
          <view class="total-item">
            <text class="total-label">买断金额</text>
            <text class="total-value">{{ totalSummary.totalBuyoutAmount || '-' }}</text>
          </view>
          <view class="total-item">
            <text class="total-label">入库数量(kg)</text>
            <text class="total-value">{{ totalSummary.totalInstockQuantity || '-' }}</text>
          </view>
          <view class="total-item">
            <text class="total-label">入库金额</text>
            <text class="total-value">{{ totalSummary.totalInstockAmount || '-' }}</text>
          </view>
          <view class="total-item">
            <text class="total-label">差异金额</text>
            <text class="total-value">{{ totalSummary.totalDiffAmount || '-' }}</text>
          </view>
        </view>
      </view>

      <!-- 其他信息 -->
      <view class="remark-section">
        <view class="header">
          <image class="opCollapse" src="/static/images/title.png" />
          <text class="title">其他信息</text>
        </view>
        <view class="section-content">
          <uni-list>
            <uni-list-item>
              <template v-slot:body>
                <view class="item-title"><text class="required">*</text><text>磅差情况</text></view>
              </template>
              <template v-slot:footer>
                <uni-easyinput v-model="form.weighDiff" type="digit" :inputBorder="false" placeholder="请输入磅差情况" />
              </template>
            </uni-list-item>
          </uni-list>
        </view>
      </view>

      <view class="footer">
        <button class="btn submit" type="primary" @click="onSubmit">保存</button>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import {
  outrightPurchaseDetailApi,
  outrightPurchaseUpdateApi
} from '@/api/procure-manage/outrightPurchase.js'

export default {
  name: 'ModifyOutrightPurchase',
  data() {
    return {
      id: '',
      form: {
        id: '',
        settleDetailId: '',
        weighingTicketCode: '',
        incomingUnit: '',
        planCodes: '',
        purchaseDepartmentName: '',
        regionName: '',
        weighDiff: ''
      },
      // 明细数据(详情回显,保留行主键 id,提交时随行携带)
      lineList: []
    }
  },
  computed: {
    // 明细各数值列合计(保留两位)
    totalSummary() {
      const list = this.lineList || []
      return {
        totalBuyoutQuantity: this.calcTotal(list, 'buyoutQuantity'),
        totalBuyoutAmount: this.calcTotal(list, 'buyoutAmount'),
        totalInstockQuantity: this.calcTotal(list, 'instockQuantity'),
        totalInstockAmount: this.calcTotal(list, 'instockAmount'),
        totalDiffAmount: this.calcTotal(list, 'diffAmount')
      }
    }
  },
  onLoad(query) {
    this.id = (query && query.id) || ''
    this.loadDetail()
  },
  methods: {
    loadDetail() {
      if (!this.id) {
        uni.showToast({ title: '缺少单据ID', icon: 'none' })
        return
      }
      uni.showLoading({ title: '加载中...', mask: true })
      outrightPurchaseDetailApi({ id: this.id })
        .then(res => {
          const data = res && res.data ? (res.data.data || res.data) : {}
          this.form = {
            id: data.id || this.id,
            settleDetailId: data.settleDetailId || '',
            weighingTicketCode: data.weighingTicketCode || '',
            incomingUnit: data.incomingUnit || '',
            planCodes: data.planCodes || '',
            purchaseDepartmentName: data.purchaseDepartmentName || '',
            regionName: data.regionName || '',
            weighDiff: data.weighDiff == null ? '' : data.weighDiff
          }
          // 明细回显(保留行主键 id,提交时随行携带)
          this.lineList = (Array.isArray(data.lines) ? data.lines : []).map(item => ({
            rowId: genRowId(),
            id: item.id || '',
            itemId: item.id || item.settleLineId || '',
            settleLineId: item.settleLineId || '',
            procurementPlanId: item.procurementPlanId || '',
            productId: item.productId || '',
            productName: item.productName || '',
            buyoutQuantity: item.buyoutQuantity == null ? '' : item.buyoutQuantity,
            buyoutPrice: item.buyoutPrice == null ? '' : item.buyoutPrice,
            buyoutAmount: item.buyoutAmount == null ? '' : item.buyoutAmount,
            instockQuantity: item.instockQuantity == null ? '' : item.instockQuantity,
            instockPrice: item.instockPrice == null ? '' : item.instockPrice,
            instockAmount: item.instockAmount == null ? '' : item.instockAmount,
            expectTonLoss: item.expectTonLoss == null ? '' : item.expectTonLoss,
            actualTonLoss: item.actualTonLoss == null ? '' : item.actualTonLoss,
            tonLossDiff: item.tonLossDiff == null ? '' : item.tonLossDiff,
            diffAmount: item.diffAmount == null ? '' : item.diffAmount
          }))
        })
        .catch(() => {
          uni.showToast({ title: '详情加载失败', icon: 'none' })
        })
        .finally(() => {
          uni.hideLoading()
        })
    },
    // 吨损耗差异 = 预计吨损耗 - 实际吨损耗(预计或实际为空则差异为空),并联动差异金额
    recalcTonLossDiff(row) {
      const expect = row.expectTonLoss
      const actual = row.actualTonLoss
      const hasExpect = expect !== null && expect !== undefined && expect !== ''
      const hasActual = actual !== null && actual !== undefined && actual !== ''
      if (hasExpect && hasActual) {
        row.tonLossDiff = Math.round((Number(expect) - Number(actual)) * 100) / 100
      } else {
        row.tonLossDiff = ''
      }
      this.recalcDiffAmount(row)
    },
    // 差异金额 = 入库数量 * 吨损耗差异 / 1000(入库数量或吨损耗差异为空则金额为空)
    recalcDiffAmount(row) {
      const qty = row.instockQuantity
      const diff = row.tonLossDiff
      const hasQty = qty !== null && qty !== undefined && qty !== ''
      const hasDiff = diff !== null && diff !== undefined && diff !== ''
      if (hasQty && hasDiff) {
        row.diffAmount = Math.round(((Number(qty) * Number(diff)) / 1000) * 100) / 100
      } else {
        row.diffAmount = ''
      }
    },
    // 计算合计(保留两位,空值按0处理,无数据返回 '-')
    calcTotal(list, field) {
      if (!list.length) return ''
      const total = Number(list.reduce((sum, row) => sum + Number(row[field] || 0), 0).toFixed(2))
      return String(total)
    },
    validate() {
      if (this.form.weighDiff === '' || this.form.weighDiff == null) return this.tip('请输入磅差情况')
      return true
    },
    tip(msg) {
      uni.showToast({ title: msg, icon: 'none' })
      return false
    },
    toNum(v) {
      if (v === null || v === undefined || v === '') return null
      const n = Number(v)
      return isNaN(n) ? null : n
    },
    buildSubmitData() {
      return {
        id: this.form.id,
        settleDetailId: this.form.settleDetailId,
        totalBuyoutQuantity: this.toNum(this.totalSummary.totalBuyoutQuantity),
        totalBuyoutAmount: this.toNum(this.totalSummary.totalBuyoutAmount),
        totalInstockQuantity: this.toNum(this.totalSummary.totalInstockQuantity),
        totalInstockAmount: this.toNum(this.totalSummary.totalInstockAmount),
        totalDiffAmount: this.toNum(this.totalSummary.totalDiffAmount),
        weighDiff: this.toNum(this.form.weighDiff),
        // 核算单明细(携带行主键 id)
        lines: (this.lineList || []).map(item => ({
          id: item.id || '',
          settleLineId: item.settleLineId || '',
          procurementPlanId: item.procurementPlanId || '',
          productId: item.productId || '',
          productName: item.productName || '',
          buyoutQuantity: this.toNum(item.buyoutQuantity),
          buyoutPrice: this.toNum(item.buyoutPrice),
          buyoutAmount: this.toNum(item.buyoutAmount),
          instockQuantity: this.toNum(item.instockQuantity),
          instockPrice: this.toNum(item.instockPrice),
          instockAmount: this.toNum(item.instockAmount),
          expectTonLoss: this.toNum(item.expectTonLoss),
          actualTonLoss: this.toNum(item.actualTonLoss),
          tonLossDiff: this.toNum(item.tonLossDiff),
          diffAmount: this.toNum(item.diffAmount)
        }))
      }
    },
    async onSubmit() {
      if (!this.validate()) return
      const confirmRes = await new Promise(resolve => {
        uni.showModal({ title: '提示', content: '确定保存买断入库核算单吗?', confirmText: '确定', cancelText: '取消', success: resolve })
      })
      if (!(confirmRes && confirmRes.confirm)) return
      uni.showLoading({ title: '提交中...', mask: true })
      outrightPurchaseUpdateApi(this.buildSubmitData())
        .then(() => {
          uni.hideLoading()
          uni.showToast({ title: '保存成功', icon: 'none' })
          this.refreshFlags()
          setTimeout(() => { uni.navigateBack() }, 400)
        })
        .catch(e => {
          uni.hideLoading()
          uni.showToast({ title: (e && (e.msg || e.message)) || '保存失败', icon: 'none' })
        })
    },
    refreshFlags() {
      try {
        uni.setStorageSync('OUTRIGHT_PURCHASE_LIST_NEED_REFRESH', '1')
        uni.setStorageSync('OUTRIGHT_PURCHASE_DETAIL_NEED_REFRESH', '1')
      } catch (e) {}
    }
  }
}

// 生成前端临时行 id
function genRowId() {
  return 'row_' + Date.now().toString(36) + '_' + Math.random().toString(36).slice(2, 8)
}
</script>

<style lang="scss" scoped>
.page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.scroll {
  flex: 1;
  padding: 12rpx 0 200rpx !important;
}

.footer {
  z-index: 2;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 32rpx;
  padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
  background: #fff;
  box-shadow: 0 -8rpx 24rpx rgba(0, 0, 0, 0.06);

  .btn {
    height: 80rpx;
    line-height: 80rpx;
    border-radius: 12rpx;
    font-size: 32rpx;
  }

  .submit {
    background: $theme-primary;
    color: #fff;
  }
}

::v-deep .uni-list {
  .uni-easyinput {
    display: flex;

    .uni-input-input {
      color: rgba(0, 0, 0, 0.9);
    }
  }

  .uni-input-placeholder {
    z-index: 1;
  }

  .uni-input-input {
    background-color: #ffffff;
  }

  background: transparent;

  &-item {
    &__extra-text {
      font-size: 32rpx;
    }

    &__content-title {
      font-size: 32rpx;
      color: rgba(0, 0, 0, 0.9);
    }

    &__container {
      padding: 32rpx;

      .uni-easyinput {
        &__placeholder-class {
          font-size: 32rpx;
          color: rgba(0, 0, 0, 0.4);
        }

        &__content {
          border: none;
          background-color: #ffffff !important;

          &-input {
            padding-left: 0 !important;
            height: 48rpx;
            line-height: 48rpx;
            font-size: 32rpx;
          }

          .content-clear-icon {
            font-size: 44rpx !important;
          }
        }
      }

      .item-title,
      .uni-list-item__content {
        flex: none;
        min-height: 48rpx;
        line-height: 48rpx;
        font-size: 32rpx;
        position: relative;
        width: 240rpx;
        margin-right: 32rpx;
        color: rgba(0, 0, 0, 0.9);

        .required {
          color: red;
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          left: -16rpx;
        }
      }
    }
  }
}

.readonly-text {
  color: rgba(0, 0, 0, 0.9);
  font-size: 32rpx;
  line-height: 48rpx;
  text-align: right;
  white-space: pre-wrap;
  word-break: break-all;
}

.sub-table,
.remark-section {
  margin-top: 20rpx;
  background: #fff;
}

.header {
  display: flex;
  align-items: center;
  padding: 24rpx 32rpx;
  border-bottom: 1rpx solid #f0f0f0;

  .opCollapse {
    width: 32rpx;
    height: 28rpx;
    margin-right: 16rpx;
    margin-top: 8rpx;
  }

  .title {
    font-size: 32rpx;
    color: rgba(0, 0, 0, 0.9);
    font-weight: 600;
    flex: 1;
  }
}

.add-list {
  padding: 0 0 16rpx;

  .empty-tip {
    text-align: center;
    padding: 60rpx 0;
    color: rgba(0, 0, 0, 0.4);
    font-size: 28rpx;
  }

  .block {
    border-bottom: 1rpx solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }
  }
}

.total-card {
  margin-top: 20rpx;
  background: #fff;
  padding: 24rpx 32rpx;

  .total-title {
    font-size: 32rpx;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.9);
    margin-bottom: 20rpx;
  }

  .total-body {
    display: flex;
    flex-wrap: wrap;

    .total-item {
      width: 50%;
      display: flex;
      align-items: center;
      margin-bottom: 20rpx;

      &:nth-last-child(-n + 2) {
        margin-bottom: 0;
      }

      .total-label {
        font-size: 28rpx;
        color: rgba(0, 0, 0, 0.6);
        margin-right: 16rpx;
      }

      .total-value {
        font-size: 28rpx;
        color: rgba(0, 0, 0, 0.9);
        font-weight: 500;
      }
    }
  }
}

.section-content {
  padding: 32rpx;
}
</style>