简体   繁体   English

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

[英]To Get unique Values and Calculate the sum of amount using javascript

In the below code, I want to print values into two section. 在下面的代码中,我想将值打印到两个部分中。

The first section is working good i have display vat,total price,material description,code etc 第一部分工作正常,我可以显示增值税,总价,材料描述,代码等

In second section i want to display the unique vat and its amount from first section. 在第二部分中,我要显示第一部分中的唯一增值税及其数量。

First Section : - Working 第一部分:-工作

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

Second Section : - Not Working 第二部分:-不起作用

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;
          }

You can make use of map while iterating table and store VAT in a map and sum up all values to use it later. 您可以在迭代表格时使用地图,并将VAT存储在地图中,然后汇总所有值以供以后使用。 See below code 见下面的代码

NOTE - I did not understand the table structure you followed as I can see lots of <table> tags starting / ending. 注意 -我不了解您遵循的表结构,因为我可以看到很多<table>标记的开始/结束。 Please make sure you have proper html table structure. 请确保您具有正确的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>';
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM