using BCD.admin; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing.Printing; using System.Globalization; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Linq; namespace Newdelhibarassocia.dhadmin { public partial class accountledger : System.Web.UI.Page { Class1 obj = new Class1(); string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request.QueryString["id"] != null) { DateTime dts=System.DateTime.Now; string qrajneeti = "select * from Memberslist where id='" + Request.QueryString["id"].ToString() + "' order by id desc"; DataSet dsrajneeti = obj.ReturnDataSet(qrajneeti); if (dsrajneeti != null && dsrajneeti.Tables[0].Rows.Count > 0) { //if (dsrajneeti.Tables[0].Rows[0]["Ndbadate"].ToString() != "0" && dsrajneeti.Tables[0].Rows[0]["Ndbadate"].ToString() != "") //{ // dts =Convert.ToDateTime(dsrajneeti.Tables[0].Rows[0]["Ndbadate"].ToString()); //} // DateTime dts; string dateValue = ""; if (dsrajneeti.Tables[0].Rows[0]["Ndbadate"].ToString() != "") { DateTime dt = Convert.ToDateTime(dsrajneeti.Tables[0].Rows[0]["Ndbadate"]); dateValue = dt.ToString("dd/MM/yyyy"); } //DateTime minDate = new DateTime(2016, 1, 1); //// ✅ Membership Date from //if (Convert.ToDateTime(dateValue) <= minDate) //{ // dateValue = minDate.ToString("yyyy-MM-dd"); //} string[] formats = { "dd/MM/yyyy", "yyyy-MM-dd", "MM/dd/yyyy" }; if (!string.IsNullOrWhiteSpace(dateValue) && dateValue != "0") { if (DateTime.TryParseExact(dateValue, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dts)) { // ✅ success Literal4.Text = dateValue; } else { // ❌ invalid date dts = DateTime.MinValue; } } Literal1.Text = dsrajneeti.Tables[0].Rows[0]["Ndbano"].ToString(); Literal3.Text = dsrajneeti.Tables[0].Rows[0]["Bcdenroll"].ToString(); Literal2.Text = dsrajneeti.Tables[0].Rows[0]["F_name"].ToString()+" "+ dsrajneeti.Tables[0].Rows[0]["L_name"].ToString(); LoadLedger(Literal3.Text, dts); } } } } public DataTable GetMembershipLedger(DateTime startDate) { DataTable dt = new DataTable(); dt.Columns.Add("Date", typeof(DateTime)); dt.Columns.Add("RefNo"); dt.Columns.Add("Type"); dt.Columns.Add("Credit", typeof(decimal)); dt.Columns.Add("Debit", typeof(decimal)); DateTime today = DateTime.Today; // ✅ Fee Rules DateTime fee100Start = new DateTime(2016, 1, 1); DateTime fee50Start = new DateTime(2010, 1, 1); DateTime fee50End = new DateTime(2015, 12, 31); DateTime fee20End = new DateTime(2009, 12, 31); // ✅ Discount Period DateTime discountStart = new DateTime(2020, 4, 1); DateTime discountEnd = new DateTime(2021, 9, 30); // ✅ Start From First Day Of Month DateTime loopDate = new DateTime(startDate.Year, startDate.Month, 1); while (loopDate <= today) { decimal amount = 0; // ✅ Discount Period if (loopDate >= discountStart && loopDate <= discountEnd) { amount = 100; } // ✅ 2016 onward else if (loopDate >= fee100Start) { amount = 100; } // ✅ 2010 - 2015 else if (loopDate >= fee50Start && loopDate <= fee50End) { amount = 50; } // ✅ Before 2010 else if (loopDate <= fee20End) { amount = 20; } // ✅ Add Row dt.Rows.Add( loopDate, "SUB", "SUBSCRIPTION", 0, amount ); loopDate = loopDate.AddMonths(1); } return dt; } // ✅ MAIN LEDGER FUNCTION public void LoadLedger(string memberId, DateTime membershipDate) { using (SqlConnection con = new SqlConnection(connStr)) { string query = @" SELECT Recp_dt AS Date, Recp_no AS RefNo, CASE WHEN CAST(Recp_dt AS DATE) = '2021-09-30' THEN 'COVID WAVE OFF' ELSE 'RECEIPT' END AS Type, Amount AS Credit, 0 AS Debit FROM Membersallrecpit WHERE Bcdenroll = @id UNION ALL SELECT Daten AS Date, Billno AS RefNo, btype AS Type, CASE WHEN btype = 'SUBSCRIPTION FEE' THEN TRY_CAST(totalamount AS DECIMAL(18,2)) ELSE 0 END AS Credit, CASE WHEN btype <> 'SUBSCRIPTION FEE' THEN TRY_CAST(totalamount AS DECIMAL(18,2)) ELSE 0 END AS Debit FROM newdelhibar_bill WHERE BCDno = @id AND btype = @btype ORDER BY Date"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@id", memberId); cmd.Parameters.AddWithValue("@btype", "SUBSCRIPTION FEE"); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); // ✅ ADD MEMBERSHIP MONTHLY DATA DataTable membership = GetMembershipLedger(membershipDate); dt.Merge(membership); // ✅ SORT DATE WISE DataView dv = dt.DefaultView; dv.Sort = "Date ASC"; dt = dv.ToTable(); // ✅ RUNNING BALANCE dt.Columns.Add("Balance", typeof(decimal)); decimal balance = 0; foreach (DataRow row in dt.Rows) { decimal cr = Convert.ToDecimal(row["Credit"]); decimal dr = Convert.ToDecimal(row["Debit"]); balance += cr - dr; row["Balance"] = balance; } GridViewLedger.DataSource = dt; GridViewLedger.DataBind(); ViewState["Ledger"] = dt; } } // ✅ EXPORT TO EXCEL DateTime dtss; protected void btnExcel_Click(object sender, EventArgs e) { // LoadLedger(Literal3.Text,); string dateValue = Literal4.Text.ToString(); string[] formats = { "dd/MM/yyyy", "yyyy-MM-dd", "MM/dd/yyyy" }; if (!string.IsNullOrWhiteSpace(dateValue) && dateValue != "0") { if (DateTime.TryParseExact(dateValue, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtss)) { // ✅ success Literal4.Text = dateValue; } else { // ❌ invalid date dtss = DateTime.MinValue; } } LoadLedger(Literal3.Text, dtss); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=Ledger.xls"); Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); sw.Write("

Ledger Report

"); GridViewLedger.RenderControl(hw); Response.Write(sw.ToString()); Response.End(); } // ✅ EXPORT TO PDF protected void btnPdf_Click(object sender, EventArgs e) { // LoadLedger(Literal3.Text); string dateValue = Literal4.Text.ToString(); string[] formats = { "dd/MM/yyyy", "yyyy-MM-dd", "MM/dd/yyyy" }; if (!string.IsNullOrWhiteSpace(dateValue) && dateValue != "0") { if (DateTime.TryParseExact(dateValue, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dtss)) { // ✅ success Literal4.Text = dateValue; } else { // ❌ invalid date dtss = DateTime.MinValue; } } LoadLedger(Literal3.Text, dtss); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=Ledger.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); Document pdfDoc = new Document(PageSize.A4, 10, 10, 20, 20); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); // ✅ ADD LOGO (LOCAL) string imagePath = Server.MapPath("assets/img/logos.jpg"); if (System.IO.File.Exists(imagePath)) { iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(imagePath); logo.ScaleToFit(80f, 80f); logo.Alignment = Element.ALIGN_CENTER; pdfDoc.Add(logo); } // ✅ HEADER pdfDoc.Add(new Paragraph("New Delhi Bar Association")); pdfDoc.Add(new Paragraph("Ledger Report - Name :"+ Literal2.Text+", NDBA No.:"+ Literal1.Text)); pdfDoc.Add(new Paragraph(" ")); // ✅ GRID EXPORT StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridViewLedger.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } // ✅ REQUIRED public override void VerifyRenderingInServerForm(Control control) { } } }