Bläddra i källkod

采购订单修改币种时重新计算金额

z 3 dagar sedan
förälder
incheckning
add58a3f4e

+ 12 - 1
src/views/purchase/purchaseOrder/PurchaseOrderyForm.api.ts

@@ -28,6 +28,7 @@ enum Api {
   classList='baseCode/baseProductClass/list',
   supplierList='/cuspCode/cuspSupplierProfile/list',
   projectList='/baseCode/baseProjectArchive/list',
+  getRate='/baseCode/baseExchangeRate/getRateByUsd',
 }
 /**
  * 导出api
@@ -213,4 +214,14 @@ export const cancelBatchConfirm = (params, handleSuccess) => {
 //获取供应商列表
 export const supplierOption = (params) => defHttp.get({url: Api.supplierList, params});
 //获取项目下拉框列表
-export const ProjectOption = (params) => defHttp.get({ url: Api.projectList, params });
+export const ProjectOption = (params) => defHttp.get({ url: Api.projectList, params });
+
+/**
+* 获取对美元汇率
+* @param params 前两个为空,则默认获取当前月的汇率
+* @param {number} params.year - 年份
+* @param {number} params.month - 月份
+* @param {string} params.currency - 货币
+*/ 
+
+export const getExchangeRate = (params) => defHttp.get({ url: Api.getRate, params });

+ 64 - 1
src/views/purchase/purchaseOrder/components/PurchaseOrderFormForm.vue

@@ -197,7 +197,7 @@
             </a-col>
             <a-col :span="12">
               <a-form-item label="币种(currency)" v-bind="validateInfos.currency" id="PurchaseOrderFormModal-currency" name="currency">
-                <JDictSelectTag v-model:value="formData.currency" placeholder="请选择" dictCode="currency" disabled />
+                <JDictSelectTag v-model:value="formData.currency" placeholder="请选择" dictCode="currency" @change="handleChangeCurrency" />
               </a-form-item>
             </a-col>
             <a-col :span="12">
@@ -364,6 +364,7 @@
     queryVersonHistoryById,
     queryPurVersonFormShipListByMainId,
     queryPurVersonProductListByMainId,
+    getExchangeRate
   } from '../PurchaseOrderyForm.api';
   import { JVxeTable } from '/@/components/jeecg/JVxeTable';
   import { purchaseOrderShipColumns, purchaseOrderProductColumns } from '../PurchaseOrderForm.data';
@@ -886,7 +887,68 @@
         },
         { deep: true }
       );
+      // 监听币种变化,获取汇率
+      watch(() => formData.currency, 
+        async (newVal, oldVal) => {
+          if (newVal && newVal !== oldVal) {
+            // setExchangeRate();
+            await handleChangeCurrency(newVal, oldVal);
+          }
+        },
+        { immediate: true }
+      );
+      // 监听日期变化,获取汇率
+      watch(() => formData.billDate,
+        async (newDate, oldDate) => {
+        const currentCurrency = formData.currency;
+        if (currentCurrency && newDate !== oldDate) {
+          // 保留当前币种不变,但重新获取汇率并刷新子表
+          await handleChangeCurrency(currentCurrency, currentCurrency);
+        }
+      },
+      { immediate: false }
+      );
+      async function handleChangeCurrency (newCurrency?: string, oldCurrency?: string) {
+        if (!newCurrency || !oldCurrency) return;
+
+          const date = moment(formData.billDate);
+          const year = date.format('YYYY');
+          const month = date.format('MM');
 
+          try {
+            // 获取旧币种汇率(原币种)与 新币种汇率(新币种),并展示新币种汇率
+            const oldRateRes = await getExchangeRate({ year, month, currency: oldCurrency });
+            const oldExchangeRate = parseFloat(oldRateRes || '1');
+            const newRateRes = await getExchangeRate({ year, month, currency: newCurrency });
+            const newExchangeRate = parseFloat(newRateRes || '1');
+            formData.exchangeRate = isNaN(newExchangeRate) ? '' : newExchangeRate;
+            purOrderFormShipFormProductTable.dataSource = purOrderFormShipFormProductTable.dataSource.map(item => {
+              const originalTaxPriceOriginal = item.taxPriceOriginal;
+              if (!originalTaxPriceOriginal) return item;
+              // 换算逻辑:原币种金额 × 原汇率 ÷ 新汇率
+              const convertedTaxPriceOriginal = originalTaxPriceOriginal * oldExchangeRate / newExchangeRate;
+              return {
+                ...item,
+                taxPriceOriginal: convertedTaxPriceOriginal.toFixed(6) || '',
+                _needUpdate: true // 标记需要更新的行
+              };
+            });
+            // 手动触发 changeValues 计算其他字段
+              const updatedDataSource = purOrderFormShipFormProductTable.dataSource;
+              updatedDataSource.forEach((row,index) => {
+              if (row._needUpdate) {
+                changeValues({
+                  col: { key: 'taxPriceOriginal' },
+                  row: row,
+                  rowIndex: index
+                });
+              }
+            });
+          } catch (err) {
+            console.error('汇率换算失败:', err);
+            formData.exchangeRate = '';
+          }
+      }
       return {
         discountHeadChange,
         PurOrderFormShipFormShipTableRef,
@@ -928,6 +990,7 @@
         changeValues,
         SelectSupplierQuotationList,
         addFromQuotation,
+        handleChangeCurrency,
       };
     },
   });