zhangwencui
10 天以前 694a9ba6f142a7f9849d86444812018e808db2bd
打印成品对账单
已修改1个文件
238 ■■■■■ 文件已修改
src/views/salesManagement/salesLedger/index.vue 238 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/views/salesManagement/salesLedger/index.vue
@@ -69,6 +69,9 @@
          <el-button type="primary"
                     plain
                     @click="handlePrint">打印</el-button>
          <el-button type="success"
                     plain
                     @click="handlePrintLedger">成品对账单</el-button>
        </div>
      </div>
      <el-table :data="tableData"
@@ -2331,6 +2334,241 @@
      }, 500);
    };
  };
  // 打印成品对账单
  const handlePrintLedger = async () => {
    if (selectedRows.value.length === 0) {
      proxy.$modal.msgWarning("请选择要打印的数据");
      return;
    }
    // 显示加载状态
    proxy.$modal.loading("正在获取产品数据,请稍候...");
    try {
      // 收集所有产品数据
      const allProducts = [];
      for (const row of selectedRows.value) {
        try {
          // 调用productList接口查询产品数据
          const productRes = await productList({
            salesLedgerId: row.id,
            type: 1,
          });
          // 将产品数据添加到总列表中
          if (productRes.data && productRes.data.length > 0) {
            productRes.data.forEach(product => {
              allProducts.push({
                productName: product.productCategory,
                specificationModel: product.specificationModel,
                unit: product.unit,
                unitPrice:
                  product.currentCustomerType === "对私"
                    ? product.unitPrice
                    : product.taxInclusiveUnitPrice,
                quantity: product.quantity,
                totalPrice:
                  product.currentCustomerType === "对私"
                    ? product.totalPrice
                    : product.taxInclusiveTotalPrice,
                deliveryDate: row.executionDate,
                licensePlate: row.licensePlate || "",
                customerName: row.customerName,
                salesContractNo: row.salesContractNo,
              });
            });
          }
        } catch (error) {
          console.error(`获取销售台账 ${row.id} 的产品数据失败:`, error);
        }
      }
      if (allProducts.length === 0) {
        proxy.$modal.msgWarning("没有找到可打印的产品数据");
        proxy.$modal.closeLoading();
        return;
      }
      // 执行打印
      executeLedgerPrint(allProducts);
    } catch (error) {
      console.error("获取产品数据失败:", error);
      proxy.$modal.msgError("获取产品数据失败,请重试");
    } finally {
      proxy.$modal.closeLoading();
    }
  };
  // 执行成品对账单打印
  const executeLedgerPrint = products => {
    // 创建一个新的打印窗口
    const printWindow = window.open("");
    // 构建打印内容
    let printContent = `
                              <!DOCTYPE html>
                              <html>
                              <head>
                                <meta charset="UTF-8">
                                <title>成品对账单</title>
                                <style>
                                  body {
                                    margin: 0;
                                    padding: 20px;
                                    font-family: "SimSun", serif;
                                    background: white;
                                  }
                                  .ledger-page {
                                    width: 100%;
                                    margin: 0 auto;
                                    background: white;
                                    page-break-after: always;
                                  }
                                  .ledger-page:last-child {
                                    page-break-after: avoid;
                                  }
                                  .header {
                                    text-align: center;
                                    margin-bottom: 20px;
                                    border-bottom: 2px solid #000;
                                    padding-bottom: 10px;
                                  }
                                  .title {
                                    font-size: 24px;
                                    font-weight: bold;
                                    margin-bottom: 10px;
                                  }
                                  .print-info {
                                    text-align: right;
                                    font-size: 12px;
                                    margin-bottom: 10px;
                                  }
                                  .ledger-table {
                                    width: 100%;
                                    border-collapse: collapse;
                                    font-size: 12px;
                                    margin-bottom: 20px;
                                  }
                                  .ledger-table th,
                                  .ledger-table td {
                                    border: 1px solid #000;
                                    padding: 8px;
                                    text-align: center;
                                  }
                                  .ledger-table th {
                                    background-color: #f0f0f0;
                                    font-weight: bold;
                                  }
                                  .ledger-table td {
                                    text-align: center;
                                  }
                                  .ledger-table td:nth-child(4),
                                  .ledger-table td:nth-child(6) {
                                    text-align: right;
                                  }
                                  .total-row {
                                    font-weight: bold;
                                    background-color: #f9f9f9;
                                  }
                                  .footer {
                                    text-align: center;
                                    font-size: 12px;
                                    margin-top: 20px;
                                  }
                                </style>
                              </head>
                              <body>
                                <div class="ledger-page">
                                  <div class="header">
                                    <div class="title">成品对账单</div>
                                    <div class="print-info">打印日期:${formatDateTime(
                                      new Date()
                                    )}</div>
                                  </div>
                                  <table class="ledger-table">
                                    <thead>
                                      <tr>
                                        <th>序号</th>
                                        <th>产品名称</th>
                                        <th>规格型号</th>
                                        <th>单位</th>
                                        <th>单价</th>
                                        <th>零售数量</th>
                                        <th>零售金额</th>
                                        <th>发货日期</th>
                                        <th>发货车牌号</th>
                                        <th>客户名称</th>
                                        <th>单号</th>
                                      </tr>
                                    </thead>
                                    <tbody>
                            `;
    // 添加产品数据行
    products.forEach((product, index) => {
      printContent += `
                                <tr>
                                  <td>${index + 1}</td>
                                  <td>${product.productName || ""}</td>
                                  <td>${product.specificationModel || ""}</td>
                                  <td>${product.unit || ""}</td>
                                  <td>${formatNumber(product.unitPrice)}</td>
                                  <td>${product.quantity || 0}</td>
                                  <td>${formatNumber(product.totalPrice)}</td>
                                  <td>${formatDate(product.deliveryDate)}</td>
                                  <td>${product.licensePlate || ""}</td>
                                  <td>${product.customerName || ""}</td>
                                  <td>${product.salesContractNo || ""}</td>
                                </tr>
                              `;
    });
    // 计算合计
    const totalQuantity = products.reduce(
      (sum, p) => sum + (parseFloat(p.quantity) || 0),
      0
    );
    const totalAmount = products.reduce(
      (sum, p) => sum + (parseFloat(p.totalPrice) || 0),
      0
    );
    printContent += `
                                      <tr class="total-row">
                                        <td colspan="5">合计</td>
                                        <td>${totalQuantity}</td>
                                        <td>${formatNumber(totalAmount)}</td>
                                        <td colspan="4"></td>
                                      </tr>
                                    </tbody>
                                  </table>
                                  <div class="footer">
                                    共 ${products.length} 条记录
                                  </div>
                                </div>
                              </body>
                              </html>
                            `;
    // 写入内容到新窗口
    printWindow.document.write(printContent);
    printWindow.document.close();
    // 等待内容加载完成后打印
    printWindow.onload = () => {
      setTimeout(() => {
        printWindow.print();
        printWindow.close();
      }, 500);
    };
  };
  // 格式化数字
  const formatNumber = num => {
    if (num === null || num === undefined || num === "") return "0.00";
    const number = parseFloat(num);
    if (isNaN(number)) return "0.00";
    return number.toFixed(2);
  };
  // 格式化日期
  const formatDate = dateString => {
    if (!dateString) return getCurrentDate();