viewBuyoutApply.vue 19.9 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
<template>
  <view class="page">
    <scroll-view scroll-y class="scroll">
      <uni-list>
        <uni-list-item class="select-item is-filled">
          <template v-slot:body>
            <view class="item-title"><text>编号</text></view>
          </template>
          <template v-slot:footer>
            <view class="serial-number-row">
              <text class="readonly-text value-code">{{ safeText(main.code) }}</text>
            </view>
          </template>
        </uni-list-item>

        <uni-list-item title="供应商名称">
          <template v-slot:footer>
            <uni-easyinput v-model="main.supplierName" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="采购处">
          <template v-slot:footer>
            <uni-easyinput v-model="purchaseDepartmentDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="区域">
          <template v-slot:footer>
            <uni-easyinput v-model="regionDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="日期">
          <template v-slot:footer>
            <uni-easyinput v-model="dateDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="运费(另付运费)">
          <template v-slot:footer>
            <uni-easyinput v-model="shippingCostDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="外观描述">
          <template v-slot:footer>
            <uni-easyinput v-model="main.appearanceDesc" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="料质状况">
          <template v-slot:footer>
            <uni-easyinput v-model="main.materialQualityDesc" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="废杂物情况">
          <template v-slot:footer>
            <uni-easyinput v-model="main.impurityDesc" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="油水杂情况">
          <template v-slot:footer>
            <uni-easyinput v-model="main.oilWaterDesc" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="装车费情况">
          <template v-slot:footer>
            <uni-easyinput v-model="loadingFeeDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="买断原因">
          <template v-slot:footer>
            <uni-easyinput v-model="main.buyoutReason" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="漆损率(%)">
          <template v-slot:footer>
            <uni-easyinput v-model="paintLossRatioDisplay" :inputBorder="false" disabled placeholder="-" />
          </template>
        </uni-list-item>

        <uni-list-item title="带漆说明">
          <template v-slot:footer>
            <text class="readonly-text small">{{ paintSummaryText || '-' }}</text>
          </template>
        </uni-list-item>
      </uni-list>

      <view class="line-section">
        <view class="section-header">
          <image class="opCollapse" src="/static/images/title.png" />
          <text class="section-title">买断明细</text>
        </view>
        <view v-if="!lineList.length" class="empty-tip">暂无明细</view>
        <view v-else class="line-list">
          <view v-for="(item, idx) in lineList" :key="item._key || ('line-' + idx)" class="line-card">
            <view class="line-title">第 {{ idx + 1 }} 条明细</view>
            <uni-list>
              <uni-list-item title="买断品种">
                <template v-slot:footer>
                  <text class="readonly-text">{{ safeText(item.productName) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="料型">
                <template v-slot:footer>
                  <text class="readonly-text">{{ safeText(item.materialType) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="买断数量(吨)">
                <template v-slot:footer>
                  <text class="readonly-text">{{ formatNumberText(item.quantity, 4) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="买断价格">
                <template v-slot:footer>
                  <text class="readonly-text">{{ formatNumberText(item.buyoutPrice, 2) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="预计吨损耗">
                <template v-slot:footer>
                  <text class="readonly-text">{{ formatNumberText(item.estimatedLoss, 2) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="漆损率(%)">
                <template v-slot:footer>
                  <text class="readonly-text">{{ formatNumberText(item.paintLossRatio, 4) }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="是否带漆">
                <template v-slot:footer>
                  <text class="readonly-text">{{ item.withPaint ? '是(按漆损率计算)' : '否' }}</text>
                </template>
              </uni-list-item>
              <uni-list-item title="除损耗后纯铜价格">
                <template v-slot:footer>
                  <text class="readonly-text value-code price-code">{{ formatNumberText(item.pureCopperPrice, 2) }}</text>
                </template>
              </uni-list-item>
            </uni-list>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { buyoutApplyDetailByPlanIdApi } from '@/api/procure-manage/buyout.js'
import { procurementPlanDetailApi } from '@/api/procure-manage/procurementPlan.js'
import { breedRelationshipQueryApi } from '@/api/devManage.js'

export default {
  name: 'DomesticPlanViewBuyoutApply',
  data() {
    return {
      planId: '',
      planData: {},
      buyoutData: null,
      breedOptions: [],
      main: {
        code: '',
        supplierName: '',
        purchaseDepartment: '',
        purchaseDepartmentId: '',
        region: '',
        regionId: '',
        date: '',
        shippingCost: '',
        appearanceDesc: '',
        materialQualityDesc: '',
        impurityDesc: '',
        oilWaterDesc: '',
        loadingFee: '',
        buyoutReason: '',
        paintLossRatio: ''
      },
      lineList: []
    }
  },
  computed: {
    purchaseDepartmentDisplay() {
      var m = this.main || {}
      var p = this.planData || {}
      var v1 = m.purchaseDepartment != null ? String(m.purchaseDepartment) : ''
      var v2 = p.purchaseDepartmentName != null ? String(p.purchaseDepartmentName) : (p.purchaseDepartment != null ? String(p.purchaseDepartment) : '')
      return v1 ? v1 : v2
    },
    regionDisplay() {
      var m = this.main || {}
      var p = this.planData || {}
      var v1 = m.region != null ? String(m.region) : ''
      var v2 = p.regionName != null ? String(p.regionName) : (p.region != null ? String(p.region) : '')
      return v1 ? v1 : v2
    },
    dateDisplay() {
      var s = String(this.main.date || (this.planData.date || '')).trim()
      if (!s) return ''
      return s.length >= 10 ? s.slice(0, 10) : s
    },
    shippingCostDisplay() {
      var v = this.toNumber(this.main.shippingCost != null ? this.main.shippingCost : this.planData.shippingCost)
      return v === 0 ? '0.00' : v.toFixed(2)
    },
    loadingFeeDisplay() {
      var v = this.toNumber(this.main.loadingFee != null ? this.main.loadingFee : this.planData.loadingFee)
      return v === 0 ? '0.00' : v.toFixed(2)
    },
    paintLossRatioDisplay() {
      var v = this.toNumber(this.main.paintLossRatio)
      if (v === 0) return '0.0000'
      return v ? v.toFixed(4) : ''
    },
    paintSummaryText() {
      var list = Array.isArray(this.lineList) ? this.lineList : []
      var withPaintList = list.filter(function (r) { return r.withPaint })
      if (!withPaintList.length) return '本单明细未发现带漆的买断品种'
      var totalQty = 0
      var totalPureQty = 0
      for (var i = 0; i < withPaintList.length; i++) {
        var r = withPaintList[i]
        var qty = this.toNumber(r.quantity)
        var ratio = this.toNumber(r.paintLossRatio) / 100
        if (ratio < 0) ratio = 0
        if (ratio >= 1) ratio = 0.9999
        totalQty += qty
        totalPureQty += qty * (1 - ratio)
      }
      var avg = totalQty > 0 ? (totalPureQty / totalQty * 100) : 0
      return '带漆品种数量合计 ' + this.formatNumber(totalQty, 4) + ' 吨,按品种漆损率计算的纯料折合比例约 ' + this.formatNumber(avg, 2) + '%(=∑ 数量*(1-漆损率) / 带漆总数量)'
    }
  },
  onLoad(query) {
    this.planId = (query && (query.planId || query.id)) ? (query.planId || query.id) : ''
    this.loadAll()
  },
  methods: {
    toNumber(v) {
      if (v == null || v === '') return 0
      var n = Number(v)
      return isNaN(n) ? 0 : n
    },
    formatNumber(v, fraction) {
      var n = this.toNumber(v)
      if (fraction != null) return n.toFixed(fraction)
      return n.toLocaleString('zh-CN', { maximumFractionDigits: 4 })
    },
    formatNumberText(v, fraction) {
      if (v == null || v === '') return '-'
      var n = Number(v)
      if (isNaN(n)) return String(v)
      return fraction != null ? n.toFixed(fraction) : n.toLocaleString('zh-CN', { maximumFractionDigits: 4 })
    },
    safeText(v) {
      var s = v == null ? '' : String(v)
      return s.trim() ? s : '-'
    },
    async loadAll() {
      var self = this
      try {
        await this.loadBreedOptions()
        await this.loadPlanAndApply()
      } catch (e) {
        uni.showToast({ title: '加载失败', icon: 'none' })
      }
    },
    async loadBreedOptions() {
      try {
        var res = await breedRelationshipQueryApi({ pageIndex: 1, pageSize: 2000 })
        var d = (res && res.data) ? res.data : {}
        this.breedOptions = (d.datas && Array.isArray(d.datas)) ? d.datas : ((d.list && Array.isArray(d.list)) ? d.list : ((d.records && Array.isArray(d.records)) ? d.records : []))
      } catch (e) {
        this.breedOptions = []
      }
    },
    findBreedOption(productId) {
      if (!productId) return null
      var pid = String(productId)
      var list = Array.isArray(this.breedOptions) ? this.breedOptions : []
      for (var i = 0; i < list.length; i++) {
        var o = list[i] || {}
        var id = o.id != null ? String(o.id) : (o.productId != null ? String(o.productId) : (o.breedId != null ? String(o.breedId) : (o.value != null ? String(o.value) : '')))
        if (id === pid) return o
      }
      return null
    },
    getPaintLossRatioFromBreed(productId, fallbackRatio) {
      var opt = this.findBreedOption(productId)
      if (opt) {
        var keys = ['paintLossRatio', 'paintLossRate', 'lossRatio', 'lacquerLossRatio']
        for (var i = 0; i < keys.length; i++) {
          if (opt[keys[i]] != null && opt[keys[i]] !== '') {
            var n = Number(opt[keys[i]])
            if (!isNaN(n)) return n
          }
        }
      }
      if (fallbackRatio != null && fallbackRatio !== '') {
        var f = Number(fallbackRatio)
        if (!isNaN(f)) return f
      }
      return 0
    },
    isWithPaintByBreed(productId, productName, breedPaintLossRatio) {
      if (breedPaintLossRatio > 0) return true
      var name = String(productName || '')
      var tags = ['带漆', '漆', '漆包', '油漆', '喷漆', '覆漆']
      for (var i = 0; i < tags.length; i++) {
        if (name.indexOf(tags[i]) >= 0) return true
      }
      return false
    },
    async loadPlanAndApply() {
      var self = this
      var planRes = null
      var plan = {}
      if (this.planId) {
        try { planRes = await procurementPlanDetailApi({ id: this.planId }) } catch (e) { planRes = null }
        plan = (planRes && planRes.data) ? planRes.data : {}
        this.planData = plan || {}
      }
      var buyout = null
      if (this.planId) {
        try {
          var bRes = await buyoutApplyDetailByPlanIdApi(this.planId)
          if (bRes && (bRes.data != null || (bRes.code && bRes.code === 200))) {
            buyout = bRes.data ? bRes.data : {}
            if (buyout && (buyout.id || buyout.code || (Array.isArray(buyout.procurementBuyoutApplyLineList) && buyout.procurementBuyoutApplyLineList.length))) {
              this.buyoutData = buyout
            }
          }
        } catch (e) {
          buyout = null
        }
      }
      this.buildMain(plan, buyout)
      this.buildLineList(plan, buyout)
    },
    buildMain(plan, buyout) {
      var b = buyout || {}
      var p = plan || {}
      var code = b.code || p.code || ''
      var supplierName = b.supplierName != null ? b.supplierName : (p.supplierName != null ? p.supplierName : '')
      var pd = b.purchaseDepartment != null ? b.purchaseDepartment : (p.purchaseDepartmentName != null ? p.purchaseDepartmentName : p.purchaseDepartment)
      var pdId = b.purchaseDepartmentId != null ? b.purchaseDepartmentId : (p.purchaseDepartmentId != null ? p.purchaseDepartmentId : p.purchaseDepartment)
      var region = b.region != null ? b.region : (p.regionName != null ? p.regionName : p.region)
      var regionId = b.regionId != null ? b.regionId : (p.regionId != null ? p.regionId : p.region)
      var date = b.date != null ? b.date : p.date
      var shippingCost = b.shippingCost != null ? b.shippingCost : (p.shippingCost != null ? p.shippingCost : '')
      var appearanceDesc = b.appearanceDesc != null ? b.appearanceDesc : '厂家生产过程中报废的一手料'
      var materialQualityDesc = b.materialQualityDesc != null ? b.materialQualityDesc : '料质清爽'
      var impurityDesc = b.impurityDesc != null ? b.impurityDesc : '含少量杂质'
      var oilWaterDesc = b.oilWaterDesc != null ? b.oilWaterDesc : ''
      var loadingFee = b.loadingFee != null ? b.loadingFee : (p.loadingFee != null ? p.loadingFee : '')
      var buyoutReason = b.buyoutReason != null ? b.buyoutReason : ''
      var paintLossRatio = b.paintLossRatio != null ? b.paintLossRatio : (p.paintLossRatio != null ? p.paintLossRatio : '')
      this.main = {
        code: code,
        supplierName: supplierName,
        purchaseDepartment: pd,
        purchaseDepartmentId: pdId,
        region: region,
        regionId: regionId,
        date: date,
        shippingCost: shippingCost,
        appearanceDesc: appearanceDesc,
        materialQualityDesc: materialQualityDesc,
        impurityDesc: impurityDesc,
        oilWaterDesc: oilWaterDesc,
        loadingFee: loadingFee,
        buyoutReason: buyoutReason,
        paintLossRatio: paintLossRatio
      }
    },
    buildLineList(plan, buyout) {
      var self = this
      var lines = []
      var bLines = (buyout && buyout.procurementBuyoutApplyLineList && Array.isArray(buyout.procurementBuyoutApplyLineList)) ? buyout.procurementBuyoutApplyLineList : null
      var pLines = (plan && plan.procurementPlanLineList && Array.isArray(plan.procurementPlanLineList)) ? plan.procurementPlanLineList.slice() : []
      if (bLines && bLines.length) {
        lines = bLines
      } else {
        lines = pLines
      }
      var shippingCost = this.toNumber(this.main.shippingCost != null ? this.main.shippingCost : this.planData.shippingCost)
      var loadingFee = this.toNumber(this.main.loadingFee != null ? this.main.loadingFee : this.planData.loadingFee)
      var defaultPaintLossRatio = this.toNumber(this.main.paintLossRatio != null ? this.main.paintLossRatio : this.planData.paintLossRatio)
      this.lineList = lines.map(function (r, i) {
        var row = r || {}
        var productId = row.productId
        var breedPaintLossRatio = self.getPaintLossRatioFromBreed(productId, defaultPaintLossRatio)
        var withPaint = self.isWithPaintByBreed(productId, row.productName, breedPaintLossRatio)
        var ratio = withPaint ? breedPaintLossRatio : 0
        var ratioFrac = ratio / 100
        if (ratioFrac < 0) ratioFrac = 0
        if (ratioFrac >= 1) ratioFrac = 0.9999
        var actualPurchase = row.actualPurchasePrice != null ? row.actualPurchasePrice : (row.buyoutPrice != null ? row.buyoutPrice : 0)
        var buyoutPrice = row.buyoutPrice != null ? row.buyoutPrice : actualPurchase
        var estimatedLoss = row.estimatedLoss != null ? row.estimatedLoss : 0
        var divisor = 1 - ratioFrac
        var base = divisor > 0 ? (self.toNumber(actualPurchase) / divisor) : self.toNumber(actualPurchase)
        var pureCopperPrice = base + shippingCost + self.toNumber(estimatedLoss) + loadingFee
        return {
          _key: row._key || row.id || ('row-' + i + '-' + Date.now()),
          productId: productId,
          productName: row.productName || '',
          materialType: row.materialType || '',
          quantity: row.quantity != null ? row.quantity : 0,
          buyoutPrice: buyoutPrice,
          estimatedLoss: estimatedLoss,
          paintLossRatio: ratio,
          withPaint: !!withPaint,
          pureCopperPrice: pureCopperPrice
        }
      })
      if (!this.main.paintLossRatio && defaultPaintLossRatio > 0) {
        this.main.paintLossRatio = defaultPaintLossRatio
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.page { display: flex; flex-direction: column; height: 100%; background: #f7f7f7; }
.scroll { flex: 1; padding: 12rpx 0 40rpx; }

.line-section { margin-top: 20rpx; background: #fff; }
.section-header { display: flex; align-items: center; padding: 24rpx 32rpx; border-bottom: 1rpx solid #f0f0f0; }
.section-title { font-size: 32rpx; color: rgba(0,0,0,0.9); font-weight: 600; }
.opCollapse { width: 32rpx; height: 28rpx; margin-right: 16rpx; margin-top: 8rpx; }
.empty-tip { padding: 80rpx 32rpx; text-align: center; color: rgba(0,0,0,0.4); font-size: 28rpx; }
.line-list { padding: 0 0 20rpx; }
.line-card { margin: 24rpx 24rpx 0; background: #fff; border-radius: 16rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.05); overflow: hidden; }
.line-title { padding: 20rpx 32rpx; font-size: 30rpx; color: rgba(0,0,0,0.9); border-bottom: 1rpx solid #f2f2f2; font-weight: 600; }
.value-code.price-code { font-weight: 700; color: $theme-primary; font-size: 32rpx; }

::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 {
    &__container {
      padding: 32rpx;
      .uni-easyinput {
        &__content {
          border: none;
          background-color: #ffffff !important;
          &-input {
            padding-left: 0 !important;
            height: 48rpx;
            line-height: 48rpx;
            font-size: 32rpx;
          }
        }
      }
      .item-title,
      .uni-list-item__content {
        flex: none;
        min-height: 48rpx;
        line-height: 48rpx;
        font-size: 32rpx;
        width: 280rpx;
        margin-right: 32rpx;
        color: rgba(0,0,0,0.9);
      }
    }
    &.select-item {
      &.is-empty .uni-list-item__extra-text { color: rgba(0,0,0,0.4) !important; }
      &.is-filled .uni-list-item__extra-text { color: rgba(0,0,0,0.9) !important; }
    }
  }
}
.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;
  &.small { font-size: 26rpx; line-height: 1.6; text-align: right; }
}
.value-code { width: 260rpx; text-align: left; }
.serial-number-row { display: flex; align-items: center; }

::v-deep .uni-easyinput {
  width: 100%;
  &__content {
    background: #f8f8f8 !important;
    border-radius: 8rpx;
    &-input { font-size: 30rpx; }
  }
}
</style>