Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BMS.OB;
- using BMS.Report.DESCO;
- using BMS.WebServices;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace BMS.Report.NESCO
- {
- public class NESCODailyCollectionDetailBuilder : IDisposable
- {
- #region Fields & Contructor
- private readonly IList<NESCOBillInfo> _billInfo;
- private readonly NESCOService _nescoService;
- private DateTime _dateFrom { get; set; }
- private DateTime _dateTo { get; set; }
- private readonly Font tblHeaderFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD, BaseColor.BLACK);
- public NESCODailyCollectionDetailBuilder(IList<NESCOBillInfo> _billInfo, DateTime _dateFrom, DateTime _dateTo)
- {
- this._billInfo = _billInfo;
- this._dateFrom = _dateFrom;
- this._dateTo = _dateTo;
- _nescoService = new NESCOService(AppConfig.NESCOServiceBaseUrl);
- }
- #endregion
- public byte[] GetDailyCollectionDetailReport(string computerCenterId)
- {
- var computerCenterResponse = GetComputerCenterResponse();
- var sndResponse = GetSNDs();
- try
- {
- var computerCenters = computerCenterResponse.ComputerCenters;
- var snds = sndResponse.SNDs ?? new List<SND>();
- //var tempSND = snds.Where(x => x.SNDId == 33).FirstOrDefault();
- //snds.Remove(tempSND);
- //tempSND.ComputerCenterId = 22;
- //snds.Add(tempSND);
- var ccSNDs = snds.Where(x => x.ComputerCenterId == Convert.ToInt32(computerCenterId)).Select(x => x.SNDId).ToList();
- var billInfos = _billInfo.Where(x => ccSNDs.Contains(Convert.ToInt32(x.SNDId))).ToList();
- if (billInfos.Count > 0)
- {
- var pdf = GetGeneratedReportBytes(billInfos,
- computerCenters.Where(x=>x.ComputerCenterId == Convert.ToInt32(computerCenterId)).FirstOrDefault().ComputerCenterName);
- return pdf;
- }
- }
- catch { }
- return null;
- }
- public byte[] GetGeneratedReportBytes(List<NESCOBillInfo> billInfos = null, string computerCenterName = null)
- {
- var billInfoList = new List<NESCOBillInfo>();
- Document document = new Document(PageSize.A4.Rotate(), 72, 72, 72, 72);
- MemoryStream memoryStream = new MemoryStream();
- PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
- PdfPTable pdfTable = new PdfPTable(11);
- if (billInfos != null)
- billInfoList = billInfos;
- else
- billInfoList = _billInfo.ToList(); ;
- document.Open();
- Font tableFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.NORMAL, BaseColor.BLACK);
- Font tableFontBold = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD, BaseColor.BLACK);
- pdfWriter.PageEvent = new NESCOHeaderBuilder("Daily Collection Detail Report", computerCenterName, _dateFrom, _dateTo);
- document.Add(new Paragraph("" + Environment.NewLine));
- InitializeTable(pdfTable);
- AddHeaders(pdfTable);
- PdfPCell cell = null;
- DateTime dt = default(DateTime).Date;
- var snds = GetSNDs().SNDs ?? null;
- int sl = 1;
- var summaries = billInfoList.Select(t => new
- {
- SNDName = snds != null ? snds.Where(x => x.SNDId == Convert.ToInt32(t.SNDId)).FirstOrDefault().SNDName : "",
- SNDId = Convert.ToInt32(t.SNDId),
- BillNumber = t.BillNumber,
- BankTxId = t.TransactionId,
- NescoTxId = t.ClientTransactionId,
- CollectedAmount = t.TotalPaidAmount,
- PrincipalAmount = t.TotalPaidAmount - t.VATPaidAmount,
- VATAmount = t.VATPaidAmount,
- LPCAmount = t.LPCPaidAmount,
- RevStampAmount = t.RevStampAmount
- });
- int sndRowCount = 0;
- int count = 0;
- string currentSND = string.Empty;
- bool isNewRow = true;
- decimal sndCollectedAmount = decimal.Zero;
- decimal sndPrincipalAmount = decimal.Zero;
- decimal sndVATAmount = decimal.Zero;
- decimal sndLPCAmount = decimal.Zero;
- decimal sndRevStampAmount = decimal.Zero;
- decimal sndNetAmount = decimal.Zero;
- int totalCount = 0;
- decimal totalCollectedAmount = decimal.Zero;
- decimal totalPrincipalAmount = decimal.Zero;
- decimal totalVATAmount = decimal.Zero;
- decimal totalLPCAmount = decimal.Zero;
- decimal totalRevStampAmount = decimal.Zero;
- decimal totalNetAmount = decimal.Zero;
- foreach (var summary in summaries.OrderByDescending(x => x.SNDId))
- {
- string sndName = snds.Where(x => x.SNDId == Convert.ToInt32(summary.SNDId)).FirstOrDefault().SNDName ?? null;
- if (isNewRow)
- {
- sndRowCount = summaries.Where(x => x.SNDId == summary.SNDId).Count() + 1;
- isNewRow = false;
- cell = new PdfPCell(new Phrase(summary.SNDName + "\r\n (Id: " + summary.SNDId + ")", tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
- cell.Rowspan = sndRowCount;
- pdfTable.AddCell(cell);
- }
- count++;
- cell = new PdfPCell(new Phrase(sl.ToString(), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(summary.BillNumber, tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(summary.BankTxId, tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(summary.NescoTxId, tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndCollectedAmount += Convert.ToDecimal(summary.CollectedAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.CollectedAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndPrincipalAmount += Convert.ToDecimal(summary.PrincipalAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.PrincipalAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndVATAmount += Convert.ToDecimal(summary.VATAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.VATAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndLPCAmount += Convert.ToDecimal(summary.LPCAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.LPCAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndRevStampAmount += Convert.ToDecimal(summary.RevStampAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.RevStampAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sndNetAmount += Convert.ToDecimal(summary.PrincipalAmount - summary.RevStampAmount);
- cell = new PdfPCell(new Phrase(FormatAmount(summary.PrincipalAmount - summary.RevStampAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- sl++;
- if (sndRowCount == sl)
- {
- cell = new PdfPCell(new Phrase($"SND Total (Count: {count})", tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
- cell.Colspan = 4;
- cell.BorderWidthBottom = 1.2f;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndCollectedAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndPrincipalAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndVATAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndLPCAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndRevStampAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(sndNetAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BorderWidthBottom = 1.2f;
- cell.BackgroundColor = new BaseColor(220,220,220);
- pdfTable.AddCell(cell);
- totalCount += count;
- totalCollectedAmount += sndCollectedAmount;
- totalPrincipalAmount += sndPrincipalAmount;
- totalVATAmount += sndVATAmount;
- totalLPCAmount += sndLPCAmount;
- totalRevStampAmount += sndRevStampAmount;
- totalNetAmount += sndNetAmount;
- sndCollectedAmount = decimal.Zero;
- sndPrincipalAmount = decimal.Zero;
- sndVATAmount = decimal.Zero;
- sndLPCAmount = decimal.Zero;
- sndRevStampAmount = decimal.Zero;
- sndNetAmount = decimal.Zero;
- sl = 1;
- isNewRow = true;
- count = 0;
- }
- }
- cell = new PdfPCell(new Phrase($"Grand Total (Count: {totalCount})", tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT;
- cell.Colspan = 5;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalCollectedAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalPrincipalAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalVATAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalLPCAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalRevStampAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase(FormatAmount(totalNetAmount), tableFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- cell.BackgroundColor = new BaseColor(220, 220, 220);
- pdfTable.AddCell(cell);
- document.Add(pdfTable);
- document.Close();
- return memoryStream.ToArray();
- }
- private void InitializeTable(PdfPTable pdfTable)
- {
- pdfTable.WidthPercentage = 100;
- pdfTable.SpacingBefore = 2;
- pdfTable.SpacingAfter = 2;
- pdfTable.HeaderRows = 1;
- float[] widths = new float[] { 1f, 1f, 1f, 1f, 1.5f, 1f, 1f, 1f, 1f, 1f, 1f };
- pdfTable.SetWidths(widths);
- }
- private void AddHeaders(PdfPTable pdfTable)
- {
- PdfPCell cell = new PdfPCell(new Phrase("SNDName", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Serial", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Bill Number", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Bank Transaction Id", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("NESCO Transaction Id", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Collected Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Principal Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("VAT Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("LPC Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Rev Stamp Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- cell = new PdfPCell(new Phrase("Net Amount", tblHeaderFont));
- cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
- pdfTable.AddCell(cell);
- }
- private SNDResponse GetSNDs()
- {
- return _nescoService.GetSNDList();
- }
- private ComputerCenterResponse GetComputerCenterResponse()
- {
- return _nescoService.GetComputerCenterList();
- }
- private string FormatAmount(double amount)
- {
- string amountStr = amount.ToString();
- var charArray = amountStr.Reverse().ToArray();
- StringBuilder sbAmount = new StringBuilder();
- for (int i = 0; i < charArray.Length; i++)
- {
- string number = charArray[i].ToString();
- if (i == 3 || i == 5 || i == 7)
- {
- number += ",";
- }
- sbAmount.Insert(0, number);
- }
- return sbAmount.ToString();
- }
- private string FormatAmount(decimal amount)
- {
- string amountStr = amount.ToString();
- var charArray = amountStr.Reverse().ToArray();
- StringBuilder sbAmount = new StringBuilder();
- for (int i = 0; i < charArray.Length; i++)
- {
- string number = charArray[i].ToString();
- if (i == 3 || i == 5 || i == 7)
- {
- number += ",";
- }
- sbAmount.Insert(0, number);
- }
- return sbAmount.ToString();
- }
- //---------------------------------------------------
- public void Dispose()
- {
- //if (_memoryStream != null)
- // _memoryStream.Dispose();
- //if (_pdfWriter != null)
- // _pdfWriter.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement