Pārlūkot izejas kodu

fix(purchase invoice print): 修复采购收货打印总价计算精度问题

liujt 6 dienas atpakaļ
vecāks
revīzija
2371eefda5

+ 41 - 13
src/views/purchasingManage/purchaseOrder/invoice/components/print-template-ht.vue

@@ -91,7 +91,7 @@
             <td style="padding: 5px"> {{ item.measuringUnit }}</td>
             <td style="padding: 5px"> {{ item.receiveTotalCount }}</td>
             <td style="padding: 5px"> {{ showPrice ? item.singlePrice : ''}} </td>
-            <td style="padding: 5px"> {{ showPrice ? item.totalPrice : ''}} </td>
+            <td style="padding: 5px"> {{ showPrice ? calculateTotalPrice(item) : ''}} </td>
             <td style="padding: 5px"> {{ item.batchNo }}</td>
             <td style="padding: 5px"> {{ item.remark }}</td>
           </tr>
@@ -119,7 +119,7 @@
             <td style="padding: 5px; text-align: left;" colspan="8">
               <div style="display: flex; margin-top: 6px;">
                 <span style="flex: 1"
-                  ></span
+                  >{{ showPrice ? convertToChinese(this.getAll('totalPrice')) : '' }}</span
                 >
               </div>
             </td>
@@ -217,13 +217,28 @@ export default {
     close() {
       this.QRvisible = false;
     },
+    calculateTotalPrice(item) {
+      const singlePrice = parseFloat(item.singlePrice) || 0;
+      const quantity = parseFloat(item.receiveTotalCount) || 0;
+      const total = singlePrice * quantity;
+      return formatPrice(total);
+    },
     getAll(key) {
       let num = 0;
-      this.formData.productList.forEach((item) => {
-        if (item[key]) {
-          num += Number(item[key]);
-        }
-      });
+      if (key === 'totalPrice') {
+        // 总价需要通过单价*数量计算,避免精度问题
+        this.formData.productList.forEach((item) => {
+          const singlePrice = parseFloat(item.singlePrice) || 0;
+          const quantity = parseFloat(item.receiveTotalCount) || 0;
+          num += singlePrice * quantity;
+        });
+      } else {
+        this.formData.productList.forEach((item) => {
+          if (item[key]) {
+            num += Number(item[key]);
+          }
+        });
+      }
       return num ? formatPrice(num) : '';
     },
     print() {
@@ -246,7 +261,15 @@ export default {
     exportExcel() {
       const list = this.formData?.productList || [];
       const totalCount = this.getAll('receiveTotalCount');
-      const totalPrice = showPrice ? this.getAll('totalPrice') : '';
+      let totalPriceNum = 0;
+      if (this.showPrice) {
+        list.forEach((item) => {
+          const singlePrice = parseFloat(item.singlePrice) || 0;
+          const quantity = parseFloat(item.receiveTotalCount) || 0;
+          totalPriceNum += singlePrice * quantity;
+        });
+      }
+      const totalPrice = this.showPrice ? totalPriceNum.toFixed(4).replace(/\.?0+$/, '') : '';
 
       const border = 'border-top:1px solid #000;border-right:1px solid #000;border-bottom:1px solid #000;border-left:1px solid #000;';
       const cellBase = `${border}padding:4px;mso-number-format:"\\@";vertical-align:middle;text-align:center;mso-horizontal-align:center;`;
@@ -257,7 +280,11 @@ export default {
       const colWidths = [50, 100, 120, 100, 60, 80, 90, 100, 80, 120];
       const colGroup = colWidths.map((w) => `<col width="${w}" style="width:${w}pt;" />`).join('');
 
-      const rows = list.map((item, index) => `
+      const rows = list.map((item, index) => {
+        const singlePrice = parseFloat(item.singlePrice) || 0;
+        const quantity = parseFloat(item.receiveTotalCount) || 0;
+        const totalPrice = this.showPrice ? (singlePrice * quantity).toFixed(4).replace(/\.?0+$/, '') : '';
+        return `
         <tr height="24" style="height:24pt;">
           <td align="center" valign="middle" style="${td}">${index + 1}</td>
           <td align="center" valign="middle" style="${td}">${item.productCode || ''}</td>
@@ -265,11 +292,12 @@ export default {
           <td align="center" valign="middle" style="${td}">${item.modelType || ''}</td>
           <td align="center" valign="middle" style="${td}">${item.measuringUnit || ''}</td>
           <td align="center" valign="middle" style="${td}">${item.receiveTotalCount ?? ''}</td>
-          <td align="center" valign="middle" style="${td}">${showPrice ? item.singlePrice : ''}</td>
-          <td align="center" valign="middle" style="${td}">${showPrice ? item.totalPrice : ''}</td>
+          <td align="center" valign="middle" style="${td}">${this.showPrice ? item.singlePrice : ''}</td>
+          <td align="center" valign="middle" style="${td}">${totalPrice}</td>
           <td align="center" valign="middle" style="${td}">${item.batchNo || ''}</td>
           <td align="center" valign="middle" style="${td}">${item.remark || ''}</td>
-        </tr>`).join('');
+        </tr>`;
+      }).join('');
 
       const title = `${this.groupName || ''}入库单`;
       const table = `
@@ -312,7 +340,7 @@ export default {
           <tr height="26" style="height:26pt;">
             <td align="center" valign="middle" style="${td};font-weight:bold;">合计金额(大写金额)</td>
             <td colspan="9" align="left" valign="middle" style="${td};font-weight:bold;">
-             
+              ${this.showPrice ? convertToChinese(totalPriceNum) : ''}
             </td>
           </tr>
           <tr height="28" style="height:28pt;">