@chenchaolong/plugin-trade-compliance-workbench 1.0.186 → 1.0.188

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2815,23 +2815,33 @@ let TradeComplianceWorkbenchService = class TradeComplianceWorkbenchService {
2815
2815
  item.updatedById = scope.userId ?? null;
2816
2816
  return repo.save(item);
2817
2817
  }
2818
- /** 批量自动补全采购商品:增值税率缺省 13%(来源 MANUAL),海关编码按商品名搜索、唯一命中自动绑定(含英文名/退税率)。 */
2818
+ /** 批量自动补全采购商品:
2819
+ * - 增值税率缺省 13%(来源 MANUAL);
2820
+ * - 海关编码缺失:按商品名称搜索,唯一命中自动绑定(编码/英文名/退税率,来源 HS_LOOKUP);
2821
+ * - 已有编码但缺退税率或英文名称:按编码反查补齐(真 0 税率以查询结果为准,查不到保持原值);
2822
+ * - 无法补齐的行集中返回原因,不阻断其他行。 */
2819
2823
  async autoCompletePurchaseLines(scope, purchaseOrderId, searchHs) {
2820
2824
  await this.ensureProcurementSchema();
2821
2825
  const order = await required(this.dataSource.getRepository(PurchaseOrder).findOne({ where: { ...activeScope(scope), id: purchaseOrderId, deletedAt: IsNull() } }), '采购订单不存在');
2822
2826
  const lines = await this.dataSource.getRepository(PurchaseOrderLine).find({ where: { ...activeScope(scope), purchaseOrderId, deletedAt: IsNull() }, order: { lineNumber: 'ASC' } });
2823
2827
  let vatFilled = 0;
2824
2828
  let hsBound = 0;
2829
+ let refundFilled = 0;
2830
+ let englishFilled = 0;
2825
2831
  const pending = [];
2826
2832
  const updates = [];
2827
2833
  for (const line of lines) {
2828
2834
  const candidate = { ...line };
2835
+ let changed = false;
2829
2836
  if (!candidate.vatRate) {
2830
2837
  candidate.vatRate = '0.13';
2831
2838
  candidate.vatRateSource = 'MANUAL';
2832
2839
  vatFilled += 1;
2840
+ changed = true;
2833
2841
  }
2834
- if (!candidate.hsCode) {
2842
+ const missingRefund = isMissingRefundRate(candidate.refundRate);
2843
+ const missingEnglish = !candidate.englishName;
2844
+ if (!candidate.hsCode && (missingRefund || missingEnglish || !candidate.hsCode)) {
2835
2845
  const keywords = (line.englishName || line.productName || '').trim();
2836
2846
  if (!keywords) {
2837
2847
  pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: '缺少可用于查询的商品名称' });
@@ -2845,28 +2855,59 @@ let TradeComplianceWorkbenchService = class TradeComplianceWorkbenchService {
2845
2855
  pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: '海关编码查询失败' });
2846
2856
  continue;
2847
2857
  }
2848
- if (results.length === 1 && results[0].code) {
2849
- candidate.hsCode = results[0].code;
2858
+ const hit = results.length === 1 ? results[0] : results.length > 1 ? null : undefined;
2859
+ if (hit && hit.code) {
2860
+ candidate.hsCode = hit.code;
2850
2861
  candidate.hsCodeSource = 'HS_LOOKUP';
2851
- if (!candidate.englishName && results[0].englishName) {
2852
- candidate.englishName = results[0].englishName;
2862
+ if (!candidate.englishName && hit.englishName) {
2863
+ candidate.englishName = hit.englishName;
2853
2864
  candidate.englishNameSource = 'HS_LOOKUP';
2865
+ englishFilled += 1;
2854
2866
  }
2855
- if (!candidate.refundRate && results[0].taxRefundRate) {
2856
- candidate.refundRate = results[0].taxRefundRate;
2867
+ if (isMissingRefundRate(candidate.refundRate) && hit.taxRefundRate) {
2868
+ candidate.refundRate = normalizeRefundRateText(hit.taxRefundRate);
2857
2869
  candidate.refundRateSource = 'HS_LOOKUP';
2870
+ refundFilled += 1;
2858
2871
  }
2872
+ changed = true;
2859
2873
  hsBound += 1;
2860
2874
  }
2861
- else if (results.length > 1) {
2862
- pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: `海关编码查询命中 ${results.length} 条,需人工选择` });
2863
- }
2864
2875
  else {
2865
- pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: '未查询到匹配的海关编码' });
2876
+ pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: hit === null ? `海关编码查询命中 ${results.length} 条,需人工选择` : '未查询到匹配的海关编码' });
2877
+ }
2878
+ }
2879
+ }
2880
+ else if (missingRefund || missingEnglish) {
2881
+ // 已有编码但缺退税率/英文名称:按编码反查补齐(查不到保持原值)
2882
+ let results = [];
2883
+ try {
2884
+ results = await searchHs(String(candidate.hsCode).trim());
2885
+ }
2886
+ catch {
2887
+ pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: '海关编码查询失败' });
2888
+ continue;
2889
+ }
2890
+ const normalizedCode = String(candidate.hsCode).replace(/\s+/g, '');
2891
+ const match = results.find(item => item.code.replace(/\s+/g, '') === normalizedCode);
2892
+ if (match) {
2893
+ if (missingRefund && match.taxRefundRate) {
2894
+ candidate.refundRate = normalizeRefundRateText(match.taxRefundRate);
2895
+ candidate.refundRateSource = 'HS_LOOKUP';
2896
+ refundFilled += 1;
2897
+ changed = true;
2866
2898
  }
2899
+ if (missingEnglish && match.englishName) {
2900
+ candidate.englishName = match.englishName;
2901
+ candidate.englishNameSource = 'HS_LOOKUP';
2902
+ englishFilled += 1;
2903
+ changed = true;
2904
+ }
2905
+ }
2906
+ else {
2907
+ pending.push({ lineNumber: line.lineNumber, productName: line.productName, reason: '未查询到该编码的退税信息' });
2867
2908
  }
2868
2909
  }
2869
- if (candidate !== line)
2910
+ if (changed)
2870
2911
  updates.push(candidate);
2871
2912
  }
2872
2913
  if (updates.length) {
@@ -2876,7 +2917,7 @@ let TradeComplianceWorkbenchService = class TradeComplianceWorkbenchService {
2876
2917
  await lineRepo.save(item);
2877
2918
  });
2878
2919
  }
2879
- return { purchaseOrderId: order.id, contractNo: order.contractNo, total: lines.length, vatFilled, hsBound, pending };
2920
+ return { purchaseOrderId: order.id, contractNo: order.contractNo, total: lines.length, vatFilled, hsBound, refundFilled, englishFilled, pending };
2880
2921
  }
2881
2922
  async updatePurchaseLine(scope, id, input) {
2882
2923
  await this.ensureProcurementSchema();
@@ -5762,4 +5803,17 @@ function analyticsExportRowsWithinLimit(sheetName, rows, maxRows) {
5762
5803
  throw new Error(`${sheetName}工作表数据行数 ${text} 超过上限 ${maxRows}`);
5763
5804
  return rows;
5764
5805
  }
5806
+ /** 退税率缺省判定:空或 0 视为待补齐(以查询结果为准)。 */
5807
+ function isMissingRefundRate(value) {
5808
+ const text = String(value ?? '').trim();
5809
+ return !text || Number(text.replace('%', '')) === 0;
5810
+ }
5811
+ /** 海关编码网退税率文本归一化:'13%'/'0.13' → '0.13'。 */
5812
+ function normalizeRefundRateText(value) {
5813
+ const text = String(value ?? '').trim().replace('%', '');
5814
+ const number = Number(text);
5815
+ if (!Number.isFinite(number))
5816
+ return text;
5817
+ return String(Math.round((number > 1 ? number / 100 : number) * 1_000_000) / 1_000_000);
5818
+ }
5765
5819
  //# sourceMappingURL=workbench.service.js.map