yyb
2 天以前 1bd1061c60c286e3b2216a67090c4976c9c7b35f
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<template>
  <view class="scan-container">
    <PageHeader title="扫码入库"
                @back="goBack" />
    <view class="module-selector"
          v-if="!showForm">
      <view class="module-card"
            @click="startScan('qualified')">
        <view class="module-icon qualified">
          <u-icon name="checkbox-mark"
                  color="#fff"
                  size="40"></u-icon>
        </view>
        <view class="module-info">
          <text class="module-label">合格入库</text>
          <text class="module-desc">扫描合格产品进行入库</text>
        </view>
      </view>
      <view class="module-card"
            @click="startScan('unqualified')">
        <view class="module-icon unqualified">
          <u-icon name="close"
                  color="#fff"
                  size="40"></u-icon>
        </view>
        <view class="module-info">
          <text class="module-label">不合格入库</text>
          <text class="module-desc">记录不合格品的入库</text>
        </view>
      </view>
    </view>
    <scroll-view v-if="showForm"
                 scroll-y
                 class="detail-scroll">
      <view class="detail-card"
            v-for="(item, idx) in recordList"
            :key="idx">
        <view class="detail-card-title"
              :class="{
                'detail-card-title--collapsible': recordList.length > 1,
                'is-collapsed': recordList.length > 1 && !isCardExpanded(idx),
              }"
              @click="recordList.length > 1 && toggleCardDetail(idx)">
          <view class="detail-card-title-text">
            <text class="detail-card-title-no">{{ cardTitleMain }}</text>
            <text v-if="recordList.length > 1"
                  class="detail-card-title-seq">({{ idx + 1 }}/{{ recordList.length }})</text>
          </view>
          <u-icon v-if="recordList.length > 1"
                  :name="isCardExpanded(idx) ? 'arrow-up' : 'arrow-down'"
                  color="#999"
                  size="18"></u-icon>
        </view>
        <view v-show="recordList.length > 1 && !isCardExpanded(idx)"
              class="detail-card-summary"
              @click="toggleCardDetail(idx)">
          <view class="kv-row kv-row--summary"
                v-for="row in summaryFieldRows"
                :key="'sum-' + row.key">
            <text class="kv-label">{{ row.label }}</text>
            <view class="kv-value kv-value--tag"
                  v-if="row.key === 'approveStatus'">
              <u-tag :type="approveStatusTagType(item)"
                     size="small">{{ formatApproveStatus(item) }}</u-tag>
            </view>
            <view class="kv-value kv-value--tag"
                  v-else-if="row.key === 'productStockStatus'">
              <u-tag :type="productStockStatusTagType(item.productStockStatus)"
                     size="small">{{ formatProductStockStatus(item.productStockStatus) }}</u-tag>
            </view>
            <text class="kv-value"
                  v-else>{{ formatCell(item, row, idx) }}</text>
          </view>
          <text class="summary-tip">点击查看全部</text>
        </view>
        <view v-show="isCardExpanded(idx)"
              class="detail-card-body">
          <view class="kv-row"
                v-for="row in detailFieldRows"
                :key="row.key">
            <text class="kv-label">{{ row.label }}</text>
            <view class="kv-value kv-value--tag"
                  v-if="row.key === 'approveStatus'">
              <u-tag :type="approveStatusTagType(item)"
                     size="small">{{ formatApproveStatus(item) }}</u-tag>
            </view>
            <view class="kv-value kv-value--tag"
                  v-else-if="row.key === 'productStockStatus'">
              <u-tag :type="productStockStatusTagType(item.productStockStatus)"
                     size="small">{{ formatProductStockStatus(item.productStockStatus) }}</u-tag>
            </view>
            <text class="kv-value"
                  v-else>{{ formatCell(item, row, idx) }}</text>
          </view>
        </view>
        <view v-if="!isFullyStocked(item)"
              class="stocked-qty-block">
          <view class="kv-row stocked-qty-row">
            <text class="kv-label">入库数量</text>
            <view class="kv-value stocked-qty-input-wrap">
              <up-input :key="'stocked-' + idx"
                        v-model="item.operateQuantity"
                        type="number"
                        placeholder="请输入入库数量"
                        clearable
                        border="surround"
                        @blur="onStockedQtyBlur(item)" />
            </view>
          </view>
        </view>
      </view>
      <view class="footer-btns">
        <u-button class="footer-cancel-btn"
                  @click="cancelForm">返回</u-button>
        <u-button class="footer-confirm-btn"
                  :loading="submitLoading"
                  @click="confirmInbound">确认入库</u-button>
      </view>
    </scroll-view>
  </view>
</template>
 
<script setup>
  import { ref, computed } from "vue";
  import PageHeader from "@/components/PageHeader.vue";
  import { productList as salesProductList } from "@/api/salesManagement/salesLedger";
  import modal from "@/plugins/modal";
  import { QUALITY_TYPE, CONTRACT_KIND } from "../scanOut/scanOut.constants";
  import { useScanOutFieldRows } from "../scanOut/scanOut.fields";
  import {
    defaultStockedQuantityFromRow,
    resolveQrContractKind,
    resolveListTypeForDetail,
    resolveContractNo,
    buildSalesLedgerProductList,
    hasAnyPositiveStockedQty,
    resolveSubmitSceneKey,
  } from "../scanOut/scanOut.logic";
  import { createSubmitConfig } from "./scanIn.submit";
 
  const showForm = ref(false);
  const type = ref(QUALITY_TYPE.qualified);
  const recordList = ref([]);
  const expandedByIndex = ref({});
  const scanContractNo = ref("");
  const contractKind = ref(CONTRACT_KIND.sales);
  const scanLedgerId = ref(null);
  const submitLoading = ref(false);
  const submitConfigByScene = createSubmitConfig(scanLedgerId);
 
  const cardTitleMain = computed(() => {
    const no = scanContractNo.value?.trim();
    return no || "—";
  });
 
  const isCardExpanded = idx => {
    if (recordList.value.length <= 1) return true;
    return !!expandedByIndex.value[idx];
  };
 
  const toggleCardDetail = idx => {
    if (recordList.value.length <= 1) return;
    expandedByIndex.value = {
      ...expandedByIndex.value,
      [idx]: !expandedByIndex.value[idx],
    };
  };
 
  const { detailFieldRows: rawDetailFieldRows, summaryFieldRows: rawSummaryFieldRows } = useScanOutFieldRows(
    contractKind,
    "inbound"
  );
  const shouldShowInboundQuantityField = key => {
    if (type.value === QUALITY_TYPE.qualified) return key !== "unqualifiedStockedQuantity";
    if (type.value === QUALITY_TYPE.unqualified)
      return key !== "stockedQuantity" && key !== "remainingQuantity";
    return true;
  };
  const detailFieldRows = computed(() =>
    rawDetailFieldRows.value.filter(row => shouldShowInboundQuantityField(row.key))
  );
  const summaryFieldRows = computed(() =>
    rawSummaryFieldRows.value.filter(row => shouldShowInboundQuantityField(row.key))
  );
 
  const emptyDash = v => {
    if (v === null || v === undefined || v === "") return "-";
    return v;
  };
 
  const formatApproveStatus = row => {
    const a = row.approveStatus;
    const noShipInfo = !row.shippingDate || !row.shippingCarNumber;
    const hasShipInfo = !!(row.shippingDate || row.shippingCarNumber);
    if ((a === 1 || a === "1") && noShipInfo) return "充足";
    if ((a === 0 || a === "0") && hasShipInfo) return "已出库";
    return "不足";
  };
 
  const formatProductStockStatus = v => {
    if (v == 1) return "部分入库";
    if (v == 2) return "已入库";
    if (v == 0) return "未出库";
    return "不足";
  };
 
  const approveStatusTagType = row => {
    const a = row.approveStatus;
    const noShipInfo = !row.shippingDate || !row.shippingCarNumber;
    const hasShipInfo = !!(row.shippingDate || row.shippingCarNumber);
    if ((a === 1 || a === "1") && noShipInfo) return "success";
    if ((a === 0 || a === "0") && hasShipInfo) return "success";
    return "error";
  };
 
  const productStockStatusTagType = v => {
    if (v == 1) return "warning";
    if (v == 2) return "success";
    if (v == 0) return "info";
    return "error";
  };
 
  const formatHeavyBox = v => {
    if (v === 1 || v === true || v === "1") return "是";
    if (v === 0 || v === false || v === "0") return "否";
    return emptyDash(v);
  };
 
  const shouldValidateStockStatus = computed(() => {
    return (
      contractKind.value === CONTRACT_KIND.sales &&
      type.value === QUALITY_TYPE.qualified
    );
  });
 
  const isFullyStocked = item => {
    if (!shouldValidateStockStatus.value) return false;
    const s = item?.productStockStatus;
    return s == 2 || s === "2";
  };
 
  const onStockedQtyBlur = item => {
    if (isFullyStocked(item)) return;
    const raw = item.operateQuantity;
    if (raw === null || raw === undefined || String(raw).trim() === "") {
      item.operateQuantity = "0";
      return;
    }
    const n = Number(String(raw).trim());
    if (Number.isNaN(n)) {
      item.operateQuantity =
        type.value === QUALITY_TYPE.unqualified ? "0" : defaultStockedQuantityFromRow(item, "inbound");
      return;
    }
    item.operateQuantity = String(Math.max(0, n));
  };
 
  const formatCell = (item, row, idx) => {
    if (row.key === "index") {
      const v = item.index;
      if (v !== null && v !== undefined && v !== "") return String(v);
      return String(idx + 1);
    }
    if (row.key === "approveStatus") return formatApproveStatus(item);
    if (row.key === "productStockStatus")
      return formatProductStockStatus(item.productStockStatus);
    if (row.key === "heavyBox") return formatHeavyBox(item.heavyBox);
    if (row.key === "remainingQuantity") {
      const v = item.remainingQuantity;
      return emptyDash(v);
    }
    if (row.key === "remainingShippedQuantity") {
      const v = item.remainingShippedQuantity;
      return emptyDash(v);
    }
    if (row.key === "shippedQuantity") {
      const v = item.shippedQuantity;
      return emptyDash(v);
    }
    if (row.key === "unqualifiedShippedQuantity") {
      const v =
        item.unqualifiedShippedQuantity ??
        item.unQualifiedShippedQuantity ??
        item.unqualifiedShippedQty ??
        item.unqualifiedOutboundQuantity;
      return emptyDash(v);
    }
    if (row.key === "stockedQuantity") {
      const v = item.stockedQuantity;
      return emptyDash(v);
    }
    if (row.key === "unqualifiedStockedQuantity") {
      const v =
        item.unqualifiedStockedQuantity ??
        item.unQualifiedStockedQuantity ??
        item.unqualifiedStockedQty ??
        item.unqualifiedInboundQuantity;
      return emptyDash(v);
    }
    if (row.key === "availableQuality") {
      const v = item.availableQuality ?? item.availableQuantity;
      return emptyDash(v);
    }
    if (row.key === "returnQuality") {
      const v = item.returnQuality ?? item.returnQuantity;
      return emptyDash(v);
    }
    return emptyDash(item[row.key]);
  };
 
  const resetDetailView = () => {
    showForm.value = false;
    scanContractNo.value = "";
    contractKind.value = CONTRACT_KIND.sales;
    scanLedgerId.value = null;
    expandedByIndex.value = {};
    recordList.value = [];
  };
 
  const confirmInbound = async () => {
    if (scanLedgerId.value == null || scanLedgerId.value === "") {
      modal.msgError("缺少订单信息,请重新扫码");
      return;
    }
    const salesLedgerProductList = buildSalesLedgerProductList(recordList.value);
    if (!hasAnyPositiveStockedQty(salesLedgerProductList)) {
      modal.msgError("请至少填写一行大于 0 的入库数量");
      return;
    }
    const sceneKey = resolveSubmitSceneKey(contractKind.value, type.value);
    const currentSubmitConfig = submitConfigByScene[sceneKey];
    if (!currentSubmitConfig) {
      modal.msgError("暂不支持当前入库场景");
      return;
    }
    const runApi = currentSubmitConfig.runApi;
    const payload = currentSubmitConfig.payloadBuilder(salesLedgerProductList);
    try {
      submitLoading.value = true;
      modal.loading("提交中...");
      const res = await runApi(payload);
      modal.closeLoading();
      if (res.code === 200) {
        modal.msgSuccess("入库成功");
        resetDetailView();
      } else {
        modal.msgError(res.msg || "提交失败");
      }
    } catch (e) {
      modal.closeLoading();
      console.error("扫码入库提交失败", e);
    } finally {
      submitLoading.value = false;
    }
  };
 
  const goBack = () => {
    if (showForm.value) {
      resetDetailView();
    } else {
      uni.navigateBack();
    }
  };
 
  const cancelForm = () => {
    resetDetailView();
  };
 
  const startScan = scanType => {
    type.value = scanType;
    uni.scanCode({
      success: res => {
        handleScanResult(res.result);
      },
      fail: () => {
        modal.msgError("扫码失败");
      },
    });
  };
 
  const handleScanResult = async result => {
    try {
      const scanData = JSON.parse(result);
      if (!scanData.id) {
        modal.msgError("无效的二维码数据");
        return;
      }
      const kind = resolveQrContractKind(scanData);
      contractKind.value = kind;
      scanLedgerId.value = scanData.id;
      scanContractNo.value = resolveContractNo(scanData, kind);
      const listType = resolveListTypeForDetail(kind);
      modal.loading("获取产品库存详情...");
      const res = await salesProductList({
        salesLedgerId: scanData.id,
        type: listType,
      });
      modal.closeLoading();
      if (res.code === 200 && res.data && res.data.length > 0) {
        recordList.value = res.data.map(row => ({
          ...row,
          unqualifiedShippedQuantity:
            row.unqualifiedShippedQuantity ??
            row.unQualifiedShippedQuantity ??
            row.unqualifiedShippedQty ??
            row.unqualifiedOutboundQuantity,
          unqualifiedStockedQuantity:
            row.unqualifiedStockedQuantity ??
            row.unQualifiedStockedQuantity ??
            row.unqualifiedStockedQty ??
            row.unqualifiedInboundQuantity,
          operateQuantity:
            type.value === QUALITY_TYPE.unqualified ? "0" : defaultStockedQuantityFromRow(row, "inbound"),
        }));
        expandedByIndex.value = {};
        showForm.value = true;
      } else {
        scanLedgerId.value = null;
        modal.msgError("未查询到明细数据");
      }
    } catch (error) {
      modal.closeLoading();
      scanLedgerId.value = null;
      console.error("处理扫码结果失败", error);
      modal.msgError("扫码处理失败,请重试");
    }
  };
</script>
 
<style scoped lang="scss">
  .scan-container {
    min-height: 100vh;
    background-color: #f5f7fa;
  }
 
  .module-selector {
    display: flex;
    flex-direction: column;
    padding: 40rpx;
    height: 80vh;
    justify-content: center;
  }
 
  .module-card {
    display: flex;
    align-items: center;
    background-color: #fff;
    padding: 80rpx 50rpx;
    border-radius: 32rpx;
    box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.05);
    margin-bottom: 50rpx;
    transition: all 0.3s ease;
    border: 2rpx solid transparent;
 
    &:active {
      transform: scale(0.98);
      background-color: #f9f9f9;
    }
  }
 
  .module-icon {
    width: 140rpx;
    height: 140rpx;
    border-radius: 32rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 40rpx;
 
    &.qualified {
      background: linear-gradient(135deg, #52c41a, #73d13d);
      box-shadow: 0 10rpx 20rpx rgba(82, 196, 26, 0.2);
    }
 
    &.unqualified {
      background: linear-gradient(135deg, #ff4d4f, #ff7875);
      box-shadow: 0 10rpx 20rpx rgba(255, 77, 79, 0.2);
    }
  }
 
  .module-info {
    display: flex;
    flex-direction: column;
  }
 
  .module-label {
    font-size: 40rpx;
    font-weight: 700;
    color: #1a1a1a;
    margin-bottom: 12rpx;
  }
 
  .module-desc {
    font-size: 28rpx;
    color: #999;
  }
 
  .detail-scroll {
    max-height: calc(100vh - 120rpx);
    box-sizing: border-box;
  }
 
  .detail-card {
    background-color: #fff;
    margin: 20rpx;
    padding: 28rpx;
    border-radius: 16rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
  }
 
  .detail-card-title {
    font-size: 30rpx;
    font-weight: 600;
    color: #333;
    margin-bottom: 20rpx;
    padding-bottom: 16rpx;
    border-bottom: 1rpx solid #eee;
  }
 
  .detail-card-title--collapsible {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }
 
  .detail-card-title--collapsible.is-collapsed {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
  }
 
  .detail-card-title-text {
    flex: 1;
    min-width: 0;
    margin-right: 16rpx;
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
  }
 
  .detail-card-title-no {
    word-break: break-all;
    line-height: 1.4;
  }
 
  .detail-card-title-seq {
    flex-shrink: 0;
    font-size: 26rpx;
    font-weight: 500;
    color: #888;
    margin-left: 8rpx;
  }
 
  .detail-card-body {
    padding-top: 4rpx;
  }
 
  .detail-card-summary {
    padding-top: 8rpx;
  }
 
  .kv-row--summary {
    padding: 12rpx 0;
    font-size: 26rpx;
  }
 
  .summary-tip {
    display: block;
    font-size: 24rpx;
    color: #999;
    text-align: center;
    padding: 20rpx 0 8rpx;
  }
 
  .kv-row {
    display: flex;
    align-items: flex-start;
    padding: 16rpx 0;
    border-bottom: 1rpx solid #f0f0f0;
    font-size: 28rpx;
  }
 
  .kv-label {
    flex-shrink: 0;
    width: 220rpx;
    color: #888;
    line-height: 1.5;
  }
 
  .kv-value {
    flex: 1;
    color: #1a1a1a;
    line-height: 1.5;
    word-break: break-all;
    text-align: right;
  }
 
  .kv-value--tag {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
 
  .stocked-qty-block {
    margin-top: 8rpx;
    padding-top: 8rpx;
    border-top: 1rpx solid #f0f0f0;
  }
 
  .stocked-qty-row {
    border-bottom: none;
    align-items: center;
  }
 
  .stocked-qty-input-wrap {
    min-width: 0;
  }
 
  .footer-btns {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 24rpx;
    padding: 20rpx 40rpx 60rpx;
  }
 
  .footer-cancel-btn {
    flex: 1;
    background-color: #f5f5f5;
    color: #666;
    border: none;
  }
 
  .footer-confirm-btn {
    flex: 1;
    background: linear-gradient(140deg, #00baff 0%, #006cfb 100%);
    color: #fff;
    border: none;
  }
</style>