获取唯一值并使用javascript计算金额总和

时间:2015-04-06 11:54:48

标签: javascript jquery

在下面的代码中,我想将值打印到两个部分。

第一部分工作正常我有展示桶,总价,材料描述,代码等

在第二部分中,我想从第一部分显示唯一的桶及其数量。

第一部分: - 工作

Code  MaterialDesc  Vat Total Price
11     XXX          10%   500
12     yyy          10 %  800
13     zzz          5%    100

第二部分: - 不工作

VAT Amount:
10%  1300
5%   100 

 <asp:LinkButton ID="lnkPrint" OnClick="lnkPrint_Click" OnClientClick = "return PrintPanel();" runat="server" CommandName="Print"
                            Text="Print" />
function PrintPanel() {
              var today = new Date();
              var dd = today.getDate();
              var mm = today.getMonth() + 1; //January is 0!

              var yyyy = today.getFullYear();
              if (dd < 10) {
                  dd = '0' + dd
              }
              if (mm < 10) {
                  mm = '0' + mm
              }
              var today = dd + '/' + mm + '/' + yyyy;
//              var map = new Object();
              var txtPrint = "";
              //txtPrint = '<table><h2 align=\"center\">Food Store</h2><h3 align=center>109,Eswaran Koil street,Puducherry-607001 </h3 ><h4 align=center>Tin No:34310000244,   Ph:0413-2227546-49</h4><h5 align=center><U>Cridit Bill</U></h5>';
              txtPrint = '<table><h3 align=center>Food Store.</h3 ><h4 align=center>' + currentLocationAddress + '</h4><h5 align=center><U>Cridit Bill</U></h5>';

              txtPrint += '<table width=\"100%\"><tr><td width=\"25%\"><b>Customer Name :</b></td><td width=\"25%\">' + currentCustomer + '</td>';
              txtPrint += '<td width=\"25%\"><b>Invoice No :</b></td><td width=\"25%\">' + document.getElementById("<%=txtInvoiceNO.ClientID%>").value + '</td></tr></table>';

              txtPrint += '<table width=\"100%\"><tr><td width=\"25%\"><b>Address:</b></td><td width=\"25%\">' + currentCustomerAddress + '</td>';
              txtPrint += '<td width=\"25%\"><b>Date :</b></td><td width=\"25%\">' + today + '</td></tr></table>';

              txtPrint += '<table width=\"36%\"><tr><td width=\"\"20%><b>Phone No:</b></td><td width=\"20%\">' + ContactNo + '</td>';
              //txtPrint += '// <td width=\"25%\"><b>TIN :</b></td><td width=\"25%\"></td>'
              txtPrint += '</tr></table>';

              txtPrint += '</table><BR>';
      txtPrint += '<hr width=\"100%\" align=left><table width=\"100%\" cellpadding=2 cellspacing=0><tr>';
                  txtPrint += '<th width=150px align=center><b> Code</b></th>';
                  txtPrint += '<th width=50px align=center><b>Barcode</b></th>';
                  txtPrint += '<th width=\"80%\" align=left><b>Material Description</b></th>';

                  txtPrint += '<th width=\"10%\" align=center><b>Unit Price</b></th>';
                  txtPrint += '<th width=\"10%\" align=center><b>VAT(%)</b></th>';
                  txtPrint += '<th width=\"10%\" align=center><b>Quantity</b></th>';
                  txtPrint += '<th width=\"10%\" align=center><b>Price</b></th>';
                  txtPrint += '<th width=\"10%\" align=center><b>VAT Amount</b></th>';
                  txtPrint += '<th width=\"10%\" align=center><b>Total Price</b></th></tr>';
                  txtPrint += '<table><table><hr width=\"100%\" align=left>';

                  $.each(invoiceData, function (i, item) {
                      var ProductName = GetProductName(item.ProductID);
                      var UnitPrice = item.UnitPrice;
                      var ProductCode = item.ProductCode;
                      var VAT = item.VAT;
                      var Quantity = item.Quantity; // item.QuantityRequested;
                      var PriceWithVAT = item.UnitPrice;
                      var VATAmount = CalcVATAmount(UnitPrice, VAT);
                      var TotalPrice = item.Total;
                      var Barcode = GenerateBarcodeImg(ProductCode);





                      txtPrint += '<table width=\"100%\"><tr><td width=\"10%\" align=left>' + ProductCode + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + imgContent + '</td>';

                      txtPrint += '<td width=\"40%\" align=center>' + ProductName + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + UnitPrice + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + VAT + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + Quantity + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + PriceWithVAT + '</td>';

                      txtPrint += '<td width=\"10%\" align=center>' + VATAmount + '</td>';



                      txtPrint += '<td width=\"10%\" align=center>' + TotalPrice + '</td></tr></table>';


                  });

                  txtPrint += '</table></table><hr width=\"100%\" align=left></table>';
                  txtPrint += '<table width=\"100%\"><tr>';
                  txtPrint += '<td width=\"85%\" align=right ><b>Invoice Total Value :</b></td><td width=\"15%\"  align=center>' + document.getElementById("<%=hidTotalCost.ClientID%>").value + '</td><tr></table>';

                  var UniqVAT = $.unique(VAT);


                  txtPrint += '</table><BR>';

                  txtPrint += '<hr width=\"50%\" align=left><table width=\"50%\" cellpadding=2 cellspacing=0><tr>';
                  txtPrint += '<th width=10px align=center><b> VAT%</b></th>';
                  txtPrint += '<th width=10px align=center><b>Amount</b></th>';
                  txtPrint += '<th width=\"10%\" align=left><b>VAT AMOUNT</b></th>';


                  txtPrint += '<table><table><hr width=\"50%\" align=left>';

                  txtPrint += '<table width=\"100%\"><tr><td width=\"10%\" align=left>' + UniqVAT + '</td>';
 txtPrint += 'Thanking you,<BR>  SVGFPPL.<BR><BR>';

              txtPrint += "(Authorised Signatory)";

              var printWindow = window.open('', '', 'height=400,width=800');
              printWindow.document.write('<body >');
              printWindow.document.write(txtPrint);
              printWindow.document.write('</body>');
              printWindow.document.close();
              setTimeout(function () {
                  printWindow.print();
              }, 500);
              return false;
          }

1 个答案:

答案 0 :(得分:0)

您可以在迭代表时使用map并将VAT存储在地图中,并总结所有值以便以后使用它。见下面的代码

注意 - 我不理解您所遵循的表格结构,因为我可以看到很多<table>标记的开始/结束。请确保您有正确的html表格结构。

//define map here
var map = new Object();

txtPrint += '<hr width=\"100%\" align=left><table width=\"100%\" cellpadding=2 cellspacing=0><tr>';
txtPrint += '<th width=150px align=center><b> Code</b></th>';
txtPrint += '<th width=50px align=center><b>Barcode</b></th>';
txtPrint += '<th width=\"80%\" align=left><b>Material Description</b></th>';

txtPrint += '<th width=\"10%\" align=center><b>Unit Price</b></th>';
txtPrint += '<th width=\"10%\" align=center><b>VAT(%)</b></th>';
txtPrint += '<th width=\"10%\" align=center><b>Quantity</b></th>';
txtPrint += '<th width=\"10%\" align=center><b>Price</b></th>';
txtPrint += '<th width=\"10%\" align=center><b>VAT Amount</b></th>';
txtPrint += '<th width=\"10%\" align=center><b>Total Price</b></th></tr>';
txtPrint += '<table><table><hr width=\"100%\" align=left>';

  $.each(invoiceData, function (i, item) {
      var ProductName = GetProductName(item.ProductID);
      var UnitPrice = item.UnitPrice;
      var ProductCode = item.ProductCode;
      var VAT = item.VAT;
      var Quantity = item.Quantity; // item.QuantityRequested;
      var PriceWithVAT = item.UnitPrice;
      var VATAmount = CalcVATAmount(UnitPrice, VAT);
      var TotalPrice = item.Total;
      var Barcode = GenerateBarcodeImg(ProductCode);





      txtPrint += '<table width=\"100%\"><tr><td width=\"10%\" align=left>' + ProductCode + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + imgContent + '</td>';

      txtPrint += '<td width=\"40%\" align=center>' + ProductName + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + UnitPrice + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + VAT + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + Quantity + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + PriceWithVAT + '</td>';

      txtPrint += '<td width=\"10%\" align=center>' + VATAmount + '</td>';



      txtPrint += '<td width=\"10%\" align=center>' + TotalPrice + '</td></tr></table>';

      //add VAT to map
      var vatTotalAmount = parseInt(map.get(VAT)) || 0;
      vatTotalAmount += parseInt(VATAmount);
      map[VAT] = vatTotalAmount;
  });


      txtPrint += '</table></table><hr width=\"100%\" align=left></table>';
      txtPrint += '<table width=\"100%\"><tr>';
      txtPrint += '<td width=\"85%\" align=right ><b>Invoice Total Value :</b></td><td width=\"15%\"  align=center>' + document.getElementById("<%=hidTotalCost.ClientID%>").value + '</td><tr></table>';


      txtPrint += '</table><BR>';

      txtPrint += '<hr width=\"50%\" align=left><table width=\"50%\" cellpadding=2 cellspacing=0><tr>';
      txtPrint += '<th width=10px align=center><b> VAT%</b></th>';
      txtPrint += '<th width=10px align=center><b>Amount</b></th>';
      txtPrint += '<th width=\"10%\" align=left><b>VAT AMOUNT</b></th>';


      txtPrint += '<table><table><hr width=\"50%\" align=left>';
     $.each(map, function (k, v)
   {
      txtPrint += '<table width=\"100%\"><tr><td width=\"10%\" align=left>' + k + '</td><td width=\"10%\" align=left>' + v + '</td>';
   }