Advertisement
bs23

NESCODailyCollectionDetailBuilder

Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.45 KB | None | 0 0
  1. using BMS.OB;
  2. using BMS.Report.DESCO;
  3. using BMS.WebServices;
  4. using iTextSharp.text;
  5. using iTextSharp.text.pdf;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12.  
  13. namespace BMS.Report.NESCO
  14. {
  15.     public class NESCODailyCollectionDetailBuilder : IDisposable
  16.     {
  17.         #region Fields & Contructor
  18.  
  19.         private readonly IList<NESCOBillInfo> _billInfo;
  20.         private readonly NESCOService _nescoService;
  21.         private DateTime _dateFrom { get; set; }
  22.         private DateTime _dateTo { get; set; }
  23.         private readonly Font tblHeaderFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD, BaseColor.BLACK);
  24.  
  25.         public NESCODailyCollectionDetailBuilder(IList<NESCOBillInfo> _billInfo, DateTime _dateFrom, DateTime _dateTo)
  26.         {
  27.             this._billInfo = _billInfo;
  28.             this._dateFrom = _dateFrom;
  29.             this._dateTo = _dateTo;
  30.             _nescoService = new NESCOService(AppConfig.NESCOServiceBaseUrl);
  31.         }
  32.  
  33.         #endregion
  34.  
  35.         public byte[] GetDailyCollectionDetailReport(string computerCenterId)
  36.         {
  37.             var computerCenterResponse = GetComputerCenterResponse();
  38.             var sndResponse = GetSNDs();
  39.  
  40.             try
  41.             {
  42.                 var computerCenters = computerCenterResponse.ComputerCenters;
  43.                 var snds = sndResponse.SNDs ?? new List<SND>();
  44.  
  45.                 //var tempSND = snds.Where(x => x.SNDId == 33).FirstOrDefault();
  46.                 //snds.Remove(tempSND);
  47.                 //tempSND.ComputerCenterId = 22;
  48.                 //snds.Add(tempSND);
  49.  
  50.                 var ccSNDs = snds.Where(x => x.ComputerCenterId == Convert.ToInt32(computerCenterId)).Select(x => x.SNDId).ToList();
  51.  
  52.                 var billInfos = _billInfo.Where(x => ccSNDs.Contains(Convert.ToInt32(x.SNDId))).ToList();
  53.  
  54.                 if (billInfos.Count > 0)
  55.                 {
  56.                     var pdf = GetGeneratedReportBytes(billInfos,
  57.                         computerCenters.Where(x=>x.ComputerCenterId == Convert.ToInt32(computerCenterId)).FirstOrDefault().ComputerCenterName);
  58.                     return pdf;
  59.                 }
  60.             }
  61.             catch { }
  62.  
  63.             return null;
  64.         }
  65.  
  66.         public byte[] GetGeneratedReportBytes(List<NESCOBillInfo> billInfos = null, string computerCenterName = null)
  67.         {
  68.             var billInfoList = new List<NESCOBillInfo>();
  69.             Document document = new Document(PageSize.A4.Rotate(), 72, 72, 72, 72);
  70.             MemoryStream memoryStream = new MemoryStream();
  71.             PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
  72.             PdfPTable pdfTable = new PdfPTable(11);
  73.  
  74.  
  75.             if (billInfos != null)
  76.                 billInfoList = billInfos;
  77.             else
  78.                 billInfoList = _billInfo.ToList(); ;
  79.  
  80.             document.Open();
  81.             Font tableFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, BaseColor.BLACK);
  82.             Font tableFontBold = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD, BaseColor.BLACK);
  83.  
  84.  
  85.             pdfWriter.PageEvent = new NESCOHeaderBuilder("Daily Collection Detail Report", computerCenterName, _dateFrom, _dateTo);
  86.  
  87.  
  88.             document.Add(new Paragraph("" + Environment.NewLine));
  89.             InitializeTable(pdfTable);
  90.             AddHeaders(pdfTable);
  91.  
  92.             PdfPCell cell = null;
  93.             DateTime dt = default(DateTime).Date;
  94.  
  95.             var snds = GetSNDs().SNDs ?? null;
  96.             int sl = 1;
  97.             var summaries = billInfoList.Select(t => new
  98.             {
  99.                 SNDName = snds != null ? snds.Where(x => x.SNDId == Convert.ToInt32(t.SNDId)).FirstOrDefault().SNDName : "",
  100.                 SNDId = Convert.ToInt32(t.SNDId),
  101.                 BillNumber = t.BillNumber,
  102.                 BankTxId = t.TransactionId,
  103.                 NescoTxId = t.ClientTransactionId,
  104.                 CollectedAmount = t.TotalPaidAmount,
  105.                 PrincipalAmount = t.TotalPaidAmount - t.VATPaidAmount,
  106.                 VATAmount = t.VATPaidAmount,
  107.                 LPCAmount = t.LPCPaidAmount,
  108.                 RevStampAmount = t.RevStampAmount
  109.             });
  110.  
  111.             int sndRowCount = 0;
  112.             int count = 0;
  113.             string currentSND = string.Empty;
  114.             bool isNewRow = true;
  115.             decimal sndCollectedAmount = decimal.Zero;
  116.             decimal sndPrincipalAmount = decimal.Zero;
  117.             decimal sndVATAmount = decimal.Zero;
  118.             decimal sndLPCAmount = decimal.Zero;
  119.             decimal sndRevStampAmount = decimal.Zero;
  120.             decimal sndNetAmount = decimal.Zero;
  121.  
  122.             int totalCount = 0;
  123.             decimal totalCollectedAmount = decimal.Zero;
  124.             decimal totalPrincipalAmount = decimal.Zero;
  125.             decimal totalVATAmount = decimal.Zero;
  126.             decimal totalLPCAmount = decimal.Zero;
  127.             decimal totalRevStampAmount = decimal.Zero;
  128.             decimal totalNetAmount = decimal.Zero;
  129.  
  130.  
  131.             foreach (var summary in summaries.OrderByDescending(x => x.SNDId))
  132.             {
  133.                 string sndName = snds.Where(x => x.SNDId == Convert.ToInt32(summary.SNDId)).FirstOrDefault().SNDName ?? null;
  134.  
  135.                 if (isNewRow)
  136.                 {
  137.                     sndRowCount = summaries.Where(x => x.SNDId == summary.SNDId).Count() + 1;
  138.                     isNewRow = false;
  139.  
  140.                     cell = new PdfPCell(new Phrase(summary.SNDName + "\r\n (Id: " + summary.SNDId + ")", tableFont));
  141.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  142.                     cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
  143.                     cell.Rowspan = sndRowCount;
  144.                     pdfTable.AddCell(cell);
  145.                 }
  146.  
  147.                 count++;
  148.  
  149.                 cell = new PdfPCell(new Phrase(sl.ToString(), tableFont));
  150.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  151.                 pdfTable.AddCell(cell);
  152.  
  153.                 cell = new PdfPCell(new Phrase(summary.BillNumber, tableFont));
  154.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  155.                 pdfTable.AddCell(cell);
  156.  
  157.                 cell = new PdfPCell(new Phrase(summary.BankTxId, tableFont));
  158.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  159.                 pdfTable.AddCell(cell);
  160.  
  161.                 cell = new PdfPCell(new Phrase(summary.NescoTxId, tableFont));
  162.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  163.                 pdfTable.AddCell(cell);
  164.  
  165.                 sndCollectedAmount += Convert.ToDecimal(summary.CollectedAmount);
  166.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.CollectedAmount), tableFont));
  167.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  168.                 pdfTable.AddCell(cell);
  169.  
  170.                 sndPrincipalAmount += Convert.ToDecimal(summary.PrincipalAmount);
  171.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.PrincipalAmount), tableFont));
  172.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  173.                 pdfTable.AddCell(cell);
  174.  
  175.                 sndVATAmount += Convert.ToDecimal(summary.VATAmount);
  176.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.VATAmount), tableFont));
  177.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  178.                 pdfTable.AddCell(cell);
  179.  
  180.                 sndLPCAmount += Convert.ToDecimal(summary.LPCAmount);
  181.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.LPCAmount), tableFont));
  182.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  183.                 pdfTable.AddCell(cell);
  184.  
  185.                 sndRevStampAmount += Convert.ToDecimal(summary.RevStampAmount);
  186.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.RevStampAmount), tableFont));
  187.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  188.                 pdfTable.AddCell(cell);
  189.  
  190.                 sndNetAmount += Convert.ToDecimal(summary.PrincipalAmount - summary.RevStampAmount);
  191.                 cell = new PdfPCell(new Phrase(FormatAmount(summary.PrincipalAmount - summary.RevStampAmount), tableFont));
  192.                 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  193.                 pdfTable.AddCell(cell);
  194.  
  195.                 sl++;
  196.  
  197.                 if (sndRowCount == sl)
  198.                 {
  199.                     cell = new PdfPCell(new Phrase($"SND Total (Count: {count})", tableFont));
  200.                     cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
  201.                     cell.Colspan = 4;
  202.                     cell.BorderWidthBottom = 1.2f;
  203.                     pdfTable.AddCell(cell);
  204.  
  205.                     cell = new PdfPCell(new Phrase(FormatAmount(sndCollectedAmount), tableFont));
  206.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  207.                     cell.BorderWidthBottom = 1.2f;
  208.                     cell.BackgroundColor = new BaseColor(220,220,220);
  209.                     pdfTable.AddCell(cell);
  210.  
  211.                     cell = new PdfPCell(new Phrase(FormatAmount(sndPrincipalAmount), tableFont));
  212.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  213.                     cell.BorderWidthBottom = 1.2f;
  214.                     cell.BackgroundColor = new BaseColor(220,220,220);
  215.                     pdfTable.AddCell(cell);
  216.  
  217.                     cell = new PdfPCell(new Phrase(FormatAmount(sndVATAmount), tableFont));
  218.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  219.                     cell.BorderWidthBottom = 1.2f;
  220.                     cell.BackgroundColor = new BaseColor(220,220,220);
  221.                     pdfTable.AddCell(cell);
  222.  
  223.                     cell = new PdfPCell(new Phrase(FormatAmount(sndLPCAmount), tableFont));
  224.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  225.                     cell.BorderWidthBottom = 1.2f;
  226.                     cell.BackgroundColor = new BaseColor(220,220,220);
  227.                     pdfTable.AddCell(cell);
  228.  
  229.                     cell = new PdfPCell(new Phrase(FormatAmount(sndRevStampAmount), tableFont));
  230.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  231.                     cell.BorderWidthBottom = 1.2f;
  232.                     cell.BackgroundColor = new BaseColor(220,220,220);
  233.                     pdfTable.AddCell(cell);
  234.  
  235.                     cell = new PdfPCell(new Phrase(FormatAmount(sndNetAmount), tableFont));
  236.                     cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  237.                     cell.BorderWidthBottom = 1.2f;
  238.                     cell.BackgroundColor = new BaseColor(220,220,220);
  239.                     pdfTable.AddCell(cell);
  240.  
  241.  
  242.                     totalCount += count;
  243.                     totalCollectedAmount += sndCollectedAmount;
  244.                     totalPrincipalAmount += sndPrincipalAmount;
  245.                     totalVATAmount += sndVATAmount;
  246.                     totalLPCAmount += sndLPCAmount;
  247.                     totalRevStampAmount += sndRevStampAmount;
  248.                     totalNetAmount += sndNetAmount;
  249.  
  250.                     sndCollectedAmount = decimal.Zero;
  251.                     sndPrincipalAmount = decimal.Zero;
  252.                     sndVATAmount = decimal.Zero;
  253.                     sndLPCAmount = decimal.Zero;
  254.                     sndRevStampAmount = decimal.Zero;
  255.                     sndNetAmount = decimal.Zero;
  256.  
  257.                     sl = 1;
  258.                     isNewRow = true;
  259.                     count = 0;
  260.                 }
  261.  
  262.             }
  263.  
  264.             cell = new PdfPCell(new Phrase($"Grand Total (Count: {totalCount})", tableFont));
  265.             cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
  266.             cell.Colspan = 5;
  267.             pdfTable.AddCell(cell);
  268.  
  269.             cell = new PdfPCell(new Phrase(FormatAmount(totalCollectedAmount), tableFont));
  270.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  271.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  272.             pdfTable.AddCell(cell);
  273.  
  274.             cell = new PdfPCell(new Phrase(FormatAmount(totalPrincipalAmount), tableFont));
  275.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  276.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  277.             pdfTable.AddCell(cell);
  278.  
  279.             cell = new PdfPCell(new Phrase(FormatAmount(totalVATAmount), tableFont));
  280.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  281.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  282.             pdfTable.AddCell(cell);
  283.  
  284.             cell = new PdfPCell(new Phrase(FormatAmount(totalLPCAmount), tableFont));
  285.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  286.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  287.             pdfTable.AddCell(cell);
  288.  
  289.             cell = new PdfPCell(new Phrase(FormatAmount(totalRevStampAmount), tableFont));
  290.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  291.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  292.             pdfTable.AddCell(cell);
  293.  
  294.             cell = new PdfPCell(new Phrase(FormatAmount(totalNetAmount), tableFont));
  295.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  296.             cell.BackgroundColor = new BaseColor(220, 220, 220);
  297.             pdfTable.AddCell(cell);
  298.  
  299.  
  300.             document.Add(pdfTable);
  301.             document.Close();
  302.  
  303.             return memoryStream.ToArray();
  304.         }
  305.  
  306.         private void InitializeTable(PdfPTable pdfTable)
  307.         {
  308.             pdfTable.WidthPercentage = 100;
  309.             pdfTable.SpacingBefore = 2;
  310.             pdfTable.SpacingAfter = 2;
  311.             pdfTable.HeaderRows = 1;
  312.  
  313.  
  314.             float[] widths = new float[] { 1f, 1f, 1f, 1f, 1.5f, 1f, 1f, 1f, 1f, 1f, 1f };
  315.             pdfTable.SetWidths(widths);
  316.         }
  317.  
  318.         private void AddHeaders(PdfPTable pdfTable)
  319.         {
  320.             PdfPCell cell = new PdfPCell(new Phrase("SNDName", tblHeaderFont));
  321.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  322.             pdfTable.AddCell(cell);
  323.             cell = new PdfPCell(new Phrase("Serial", tblHeaderFont));
  324.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  325.             pdfTable.AddCell(cell);
  326.             cell = new PdfPCell(new Phrase("Bill Number", tblHeaderFont));
  327.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  328.             pdfTable.AddCell(cell);
  329.             cell = new PdfPCell(new Phrase("Bank Transaction Id", tblHeaderFont));
  330.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  331.             pdfTable.AddCell(cell);
  332.             cell = new PdfPCell(new Phrase("NESCO Transaction Id", tblHeaderFont));
  333.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  334.             pdfTable.AddCell(cell);
  335.             cell = new PdfPCell(new Phrase("Collected Amount", tblHeaderFont));
  336.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  337.             pdfTable.AddCell(cell);
  338.             cell = new PdfPCell(new Phrase("Principal Amount", tblHeaderFont));
  339.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  340.             pdfTable.AddCell(cell);
  341.             cell = new PdfPCell(new Phrase("VAT Amount", tblHeaderFont));
  342.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  343.             pdfTable.AddCell(cell);
  344.             cell = new PdfPCell(new Phrase("LPC Amount", tblHeaderFont));
  345.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  346.             pdfTable.AddCell(cell);
  347.  
  348.             cell = new PdfPCell(new Phrase("Rev Stamp Amount", tblHeaderFont));
  349.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  350.             pdfTable.AddCell(cell);
  351.  
  352.             cell = new PdfPCell(new Phrase("Net Amount", tblHeaderFont));
  353.             cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
  354.             pdfTable.AddCell(cell);
  355.         }
  356.  
  357.         private SNDResponse GetSNDs()
  358.         {
  359.             return _nescoService.GetSNDList();
  360.         }
  361.  
  362.         private ComputerCenterResponse GetComputerCenterResponse()
  363.         {
  364.             return _nescoService.GetComputerCenterList();
  365.         }
  366.  
  367.         private string FormatAmount(double amount)
  368.         {
  369.             string amountStr = amount.ToString();
  370.             var charArray = amountStr.Reverse().ToArray();
  371.             StringBuilder sbAmount = new StringBuilder();
  372.  
  373.             for (int i = 0; i < charArray.Length; i++)
  374.             {
  375.                 string number = charArray[i].ToString();
  376.  
  377.                 if (i == 3 || i == 5 || i == 7)
  378.                 {
  379.                     number += ",";
  380.                 }
  381.  
  382.                 sbAmount.Insert(0, number);
  383.             }
  384.  
  385.             return sbAmount.ToString();
  386.         }
  387.  
  388.         private string FormatAmount(decimal amount)
  389.         {
  390.             string amountStr = amount.ToString();
  391.             var charArray = amountStr.Reverse().ToArray();
  392.             StringBuilder sbAmount = new StringBuilder();
  393.  
  394.             for (int i = 0; i < charArray.Length; i++)
  395.             {
  396.                 string number = charArray[i].ToString();
  397.  
  398.                 if (i == 3 || i == 5 || i == 7)
  399.                 {
  400.                     number += ",";
  401.                 }
  402.  
  403.                 sbAmount.Insert(0, number);
  404.             }
  405.  
  406.             return sbAmount.ToString();
  407.         }
  408.  
  409.         //---------------------------------------------------
  410.  
  411.         public void Dispose()
  412.         {
  413.             //if (_memoryStream != null)
  414.             //    _memoryStream.Dispose();
  415.             //if (_pdfWriter != null)
  416.             //    _pdfWriter.Dispose();
  417.         }
  418.     }
  419. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement