diff --git a/.vs/OSS/v16/.suo b/.vs/OSS/v16/.suo index 8a4c69e..ecf327c 100644 Binary files a/.vs/OSS/v16/.suo and b/.vs/OSS/v16/.suo differ diff --git a/Controllers/NewCOIController.cs b/Controllers/NewCOIController.cs index 55d77ff..c77e4f5 100644 --- a/Controllers/NewCOIController.cs +++ b/Controllers/NewCOIController.cs @@ -4086,20 +4086,20 @@ namespace OSS.Controllers try { - // Get invoice from tblInvoice to get ApplicationCode - var invoice = myContext.Database.SqlQuery( + // Get invoice from tblInvoice using strongly-typed query + var invoice = myContext.Database.SqlQuery( "SELECT InvoiceID, ApplicationCode, ApplicationID FROM tblInvoice WHERE InvoiceID = @p0", - invoiceId.Value).FirstOrDefault(); + new System.Data.SqlClient.SqlParameter("@p0", invoiceId.Value)).FirstOrDefault(); - if (invoice == null) + if (invoice == null || string.IsNullOrEmpty(invoice.ApplicationCode)) { - TempData["PaymentError"] = "Invoice not found."; + TempData["PaymentError"] = "Invoice not found or invalid."; return RedirectToAction("ApplicationStatus"); } - // Set ProjectCode in session and redirect to DisplayPDF - Session["InvoiceID"] = invoiceId.Value; - + // Set ProjectCode and InvoiceID in session for DisplayPDF + Session["ProjectCode"] = invoice.ApplicationCode; + Session["InvoiceID"] = invoiceId.Value; // Store specific invoice ID to use in DisplayPDF return RedirectToAction("DisplayPDF"); } catch (Exception ex) @@ -4115,21 +4115,45 @@ namespace OSS.Controllers } public async Task DisplayPDF() { + if (Session["ProjectCode"] == null) + { + TempData["PaymentError"] = "Project code is required."; + return RedirectToAction("ApplicationStatus"); + } + var ProjectCode = Session["ProjectCode"].ToString(); - var checkIfExist = myContext.ApplicationManagers.SingleOrDefault(t => t.ProjectCode == ProjectCode); - // Changes to remove hardcoded exchange rates - ExchangeRate exchangeRate = myContext.ExchangeRates.SingleOrDefault(t => t.Currency ==checkIfExist.Currency); - COIPrice coiPrice = myContext.COIPrices.SingleOrDefault(t => t.ApplicationType == checkIfExist.ServiceName); + // Use FirstOrDefault instead of SingleOrDefault to handle multiple rows + var checkIfExist = myContext.ApplicationManagers.FirstOrDefault(t => t.ProjectCode == ProjectCode); + + if (checkIfExist == null) + { + TempData["PaymentError"] = "Application not found."; + return RedirectToAction("ApplicationStatus"); + } + + // Use specific invoice ID from session if available (from DownloadInvoice), + // otherwise use the one from ApplicationManagers + long? invoiceIdToUse = Session["InvoiceID"] as long?; + if (!invoiceIdToUse.HasValue) + { + invoiceIdToUse = checkIfExist.InvoiceID; + } + else + { + // Clear the session invoice ID after using it + Session.Remove("InvoiceID"); + } - decimal Price = coiPrice.Price * exchangeRate.Rate; + if (!invoiceIdToUse.HasValue) + { + TempData["PaymentError"] = "Invoice ID not found."; + return RedirectToAction("ApplicationStatus"); + } - Session["AmountinWords"] = CurrencyUtils.ToWords(Price); - - var InvoiceID = checkIfExist.InvoiceID; + var InvoiceID = invoiceIdToUse.Value; using (var client = new HttpClient()) { - client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage responseMessage = await client.GetAsync("http://192.168.2.31:8090/tic/invoice-gen/" + InvoiceID); @@ -4138,32 +4162,49 @@ namespace OSS.Controllers var checkresult = jObject.GetValue("resultcode").ToString(); if (checkresult == "0") { - - Session["ControlNo"] = jObject.SelectToken("result.ControlNo").Value(); - Session["QRCode"] = jObject.SelectToken("result.QRCode").Value(); - Session["SubServiceName"] = jObject.SelectToken("result.SubServiceName").Value(); - Session["FullName"] = jObject.SelectToken("result.FullName").Value(); + Session["ControlNo"] = jObject.SelectToken("result.ControlNo")?.Value(); + Session["QRCode"] = jObject.SelectToken("result.QRCode")?.Value(); + Session["SubServiceName"] = jObject.SelectToken("result.SubServiceName")?.Value(); + Session["FullName"] = jObject.SelectToken("result.FullName")?.Value(); Session["CompanyName"] = checkIfExist.CompanyName; - Session["PhoneNo"] = jObject.SelectToken("result.PhoneNo").Value(); - Session["Amount"] = jObject.SelectToken("result.Amount").Value(); - Session["BillDescription"] = jObject.SelectToken("result.SubServiceName").Value(); - Session["Currency"] = jObject.SelectToken("result.Currency").Value(); - Session["ExpireDate"] = jObject.SelectToken("result.ExpireDate").Value(); + Session["PhoneNo"] = jObject.SelectToken("result.PhoneNo")?.Value(); + Session["Amount"] = jObject.SelectToken("result.Amount")?.Value(); + Session["BillDescription"] = jObject.SelectToken("result.SubServiceName")?.Value(); + Session["Currency"] = jObject.SelectToken("result.Currency")?.Value(); + Session["ExpireDate"] = jObject.SelectToken("result.ExpireDate")?.Value(); Session["PrintedDate"] = DateTime.Now.ToString("dd-MM-yyyy"); - return RedirectToAction("DisplayInvoice", "NewCOI"); + // Calculate AmountinWords from the actual invoice amount from API, not from COIPrice + if (!string.IsNullOrEmpty(Session["Amount"]?.ToString())) + { + if (decimal.TryParse(Session["Amount"].ToString(), out decimal invoiceAmount)) + { + Session["AmountinWords"] = CurrencyUtils.ToWords(invoiceAmount); + } + else + { + // Fallback: use COIPrice calculation if parsing fails + ExchangeRate exchangeRate = myContext.ExchangeRates.FirstOrDefault(t => t.Currency == checkIfExist.Currency); + COIPrice coiPrice = myContext.COIPrices.FirstOrDefault(t => t.ApplicationType == checkIfExist.ServiceName); + if (exchangeRate != null && coiPrice != null) + { + decimal Price = coiPrice.Price * exchangeRate.Rate; + Session["AmountinWords"] = CurrencyUtils.ToWords(Price); + } + } + } + return RedirectToAction("DisplayInvoice", "NewCOI"); } if (checkresult == "Failure") { - var Reslt = jObject.GetValue("message").ToString(); - ViewData["Notfound"] = Reslt.ToString(); + var Reslt = jObject.GetValue("message")?.ToString(); + ViewData["Notfound"] = Reslt ?? "Invoice generation failed."; return View("NotFoundApp"); } return View("NotFoundApp"); } - } public async Task DisplayPDFTransfer() { diff --git a/bin/OSS.dll b/bin/OSS.dll index f1ada0b..7c20955 100644 Binary files a/bin/OSS.dll and b/bin/OSS.dll differ diff --git a/bin/OSS.pdb b/bin/OSS.pdb index ce12422..b186346 100644 Binary files a/bin/OSS.pdb and b/bin/OSS.pdb differ diff --git a/obj/Debug/OSS.dll b/obj/Debug/OSS.dll index f1ada0b..7c20955 100644 Binary files a/obj/Debug/OSS.dll and b/obj/Debug/OSS.dll differ diff --git a/obj/Debug/OSS.pdb b/obj/Debug/OSS.pdb index ce12422..b186346 100644 Binary files a/obj/Debug/OSS.pdb and b/obj/Debug/OSS.pdb differ