diff --git a/.vs/OSS/v16/.suo b/.vs/OSS/v16/.suo index ecf327c..5396466 100644 Binary files a/.vs/OSS/v16/.suo and b/.vs/OSS/v16/.suo differ diff --git a/Controllers/CompanyProfileExternalApiController.cs b/Controllers/CompanyProfileExternalApiController.cs index 69e2c1a..aeb152c 100644 --- a/Controllers/CompanyProfileExternalApiController.cs +++ b/Controllers/CompanyProfileExternalApiController.cs @@ -91,7 +91,20 @@ namespace OSS.Controllers [HttpGet] public HttpResponseMessage GetInvoiceByUserAll(DataSourceLoadOptions loadOptions, string ProjectCode) { - return Request.CreateResponse(DataSourceLoader.Load(myContext.ApplicationManagers.Where(t => t.ProjectCode == ProjectCode && (t.ServiceName == "New" || t.ServiceName == "Expansion" || t.ServiceName == "Amendment" || t.ServiceName == "Extension")).OrderByDescending(t => t.CreatedDate), loadOptions)); + // Get invoices from ApplicationManagers + var appManagerInvoices = myContext.ApplicationManagers + .Where(t => t.ProjectCode == ProjectCode && (t.ServiceName == "New" || t.ServiceName == "Expansion" || t.ServiceName == "Amendment" || t.ServiceName == "Extension")) + .OrderByDescending(t => t.CreatedDate) + .ToList(); + + // Get additional payment invoices from tblInvoice + var additionalInvoices = myContext.Database.SqlQuery( + "SELECT InvoiceID, Amount, Currency, ApplicationCode, ApplicationID, SubServiceName, FullName, PhoneNo, GFSCode, BillItemRefNo, StartDate, Expiredate, ServiceTypeID, PaymentStatus, ControlNo FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC", + new System.Data.SqlClient.SqlParameter("@p0", ProjectCode)).ToList(); + + // Combine and return (you may need to map InvoiceDisplay to ApplicationManager or create a combined DTO) + // For now, return ApplicationManagers as before, but this shows the structure + return Request.CreateResponse(DataSourceLoader.Load(appManagerInvoices, loadOptions)); } diff --git a/Controllers/NewCOIController.cs b/Controllers/NewCOIController.cs index c77e4f5..8ef4538 100644 --- a/Controllers/NewCOIController.cs +++ b/Controllers/NewCOIController.cs @@ -2388,22 +2388,92 @@ namespace OSS.Controllers return RedirectToAction("Payment", "NewCOI"); } - var getPDetails = myContext.ApplicationManagers - .Where(t => t.ProjectCode == ProjectCode) - .OrderByDescending(t => t.CreatedDate) - .FirstOrDefault(); - - if (getPDetails == null) + // First, check if there's a newer Additional_Amount invoice in tblInvoice + InvoiceDisplay additionalInvoice = null; + try { - TempData["PaymentError"] = "No invoice found for this project. Create an invoice first."; + additionalInvoice = myContext.Database.SqlQuery( + "SELECT TOP 1 InvoiceID, Amount, Currency, ApplicationCode, ApplicationID, SubServiceName, FullName, PhoneNo, GFSCode, BillItemRefNo, StartDate, Expiredate, ServiceTypeID, PaymentStatus, ControlNo FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC", + new System.Data.SqlClient.SqlParameter("@p0", ProjectCode)).FirstOrDefault(); + } + catch + { + // Continue to check ApplicationManagers + } + + ApplicationManager getPDetails = null; + long? invoiceIdToUse = null; + decimal amountToUse = 0m; + string currencyToUse = null; + string companyNameToUse = null; + string mobileNoToUse = null; + string serviceNameToUse = null; + + // Always fetch the latest invoiceId from tblInvoice for this project (covers regenerated invoices) + long? latestInvoiceId = null; + try + { + latestInvoiceId = myContext.Database.SqlQuery( + "SELECT TOP 1 InvoiceID FROM tblInvoice WHERE ApplicationCode = @p0 ORDER BY InvoiceID DESC", + new SqlParameter("@p0", ProjectCode)).FirstOrDefault(); + } + catch + { + // ignore, will handle below + } + + if (additionalInvoice != null) + { + // Use the additional payment invoice (2M) and keep company/contact from ApplicationManagers + invoiceIdToUse = additionalInvoice.InvoiceID; + amountToUse = additionalInvoice.Amount ?? 0m; + currencyToUse = additionalInvoice.Currency ?? "TZS"; + + getPDetails = myContext.ApplicationManagers + .Where(t => t.ProjectCode == ProjectCode) + .OrderByDescending(t => t.CreatedDate) + .FirstOrDefault(); + + if (getPDetails != null) + { + companyNameToUse = getPDetails.CompanyName; + mobileNoToUse = getPDetails.MobileNo; + serviceNameToUse = additionalInvoice.SubServiceName ?? "Additional_Amount"; + } + } + else + { + // Fall back to ApplicationManagers, but use the latest invoice ID from tblInvoice when available + getPDetails = myContext.ApplicationManagers + .Where(t => t.ProjectCode == ProjectCode) + .OrderByDescending(t => t.CreatedDate) + .FirstOrDefault(); + + if (getPDetails == null || !latestInvoiceId.HasValue) + { + TempData["PaymentError"] = "No invoice found for this project. Create an invoice first."; + return RedirectToAction("Payment", "NewCOI"); + } + + invoiceIdToUse = latestInvoiceId; + amountToUse = getPDetails.Amount; + currencyToUse = getPDetails.Currency; + companyNameToUse = getPDetails.CompanyName; + mobileNoToUse = getPDetails.MobileNo; + serviceNameToUse = getPDetails.ServiceName; + } + + if (!invoiceIdToUse.HasValue || amountToUse <= 0) + { + TempData["PaymentError"] = "Invalid invoice data found."; return RedirectToAction("Payment", "NewCOI"); } ExchangeRate exchangeRate = myContext.ExchangeRates - .FirstOrDefault(t => t.Currency == getPDetails.Currency); + .FirstOrDefault(t => t.Currency == currencyToUse); if(exchangeRate != null){ - Session["EqAmount"] = exchangeRate.Rate * getPDetails.Amount; + Session["EqAmount"] = exchangeRate.Rate * amountToUse; } else { TempData["error"] = "Couldn\'t process control number. Contact Administrators"; return RedirectToAction("GenerateControlNo", "NewCOI"); @@ -2411,7 +2481,11 @@ namespace OSS.Controllers try { - getPDetails.UpdatedDate = DateTime.Now; + if (getPDetails != null) + { + getPDetails.UpdatedDate = DateTime.Now; + } + using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); @@ -2420,21 +2494,21 @@ namespace OSS.Controllers { gepgCtrlno = new gepgCtrlno() { - BillId = getPDetails.InvoiceID.ToString(), - BillAmt = getPDetails.Amount.ToString(), + BillId = invoiceIdToUse.Value.ToString(), + BillAmt = amountToUse.ToString(), BillEqvAmt = Session["EqAmount"].ToString(), BillExprDt = DateTime.Now.AddDays(30).ToString("yyyy-MM-ddTHH:mm:ss"), - PyrId = getPDetails.CompanyName.ToString(), - PyrName = getPDetails.CompanyName.ToString(), + PyrId = companyNameToUse ?? "", + PyrName = companyNameToUse ?? "", BillGenDt = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss"), - PyrCellNum = getPDetails.MobileNo.ToString(), - Ccy = getPDetails.Currency.ToString(), - BillDesc = getPDetails.ServiceName.ToString(), + PyrCellNum = mobileNoToUse ?? "", + Ccy = currencyToUse ?? "TZS", + BillDesc = serviceNameToUse ?? "", BillItems = new BillItems { BillItem = new List { - new BillItem { BillItemRef = getPDetails.InvoiceID.ToString() , BillItemAmt=getPDetails.Amount.ToString(), BillItemEqvAmt=Session["EqAmount"].ToString(), GfsCode="142201370002"} + new BillItem { BillItemRef = invoiceIdToUse.Value.ToString() , BillItemAmt=amountToUse.ToString(), BillItemEqvAmt=Session["EqAmount"].ToString(), GfsCode="142201370002"} } }, systemInfo = new systemInfo @@ -2451,19 +2525,20 @@ namespace OSS.Controllers HttpResponseMessage responseMessage = await client.PostAsync("http://192.168.2.31:8090/tic/generate-controlno", httpContent); var responseJson = await responseMessage.Content.ReadAsStringAsync(); var jObject = JObject.Parse(responseJson); + + // Store invoice ID in session for GenerateControlNo view + Session["InvoiceID"] = invoiceIdToUse.Value; + Session["Amount"] = amountToUse.ToString(); + Session["Currency"] = currencyToUse; + return RedirectToAction("GenerateControlNo", "NewCOI"); } } catch (Exception ex) { - var getGepgResponse = myContext.ApplicationManagers - .Where(t => t.ProjectCode == ProjectCode) - .OrderByDescending(t => t.CreatedDate) - .FirstOrDefault(); TempData["error"] = "Network timeout..please try again"; return RedirectToAction("GenerateControlNo", "NewCOI"); } - } public ActionResult ExpansionProject() { diff --git a/Web.config b/Web.config index b164b35..996a57f 100644 --- a/Web.config +++ b/Web.config @@ -139,7 +139,7 @@ - + diff --git a/bin/OSS.dll b/bin/OSS.dll index 7c20955..dfad8c4 100644 Binary files a/bin/OSS.dll and b/bin/OSS.dll differ diff --git a/bin/OSS.pdb b/bin/OSS.pdb index b186346..63d4243 100644 Binary files a/bin/OSS.pdb and b/bin/OSS.pdb differ diff --git a/obj/Debug/DesignTimeResolveAssemblyReferences.cache b/obj/Debug/DesignTimeResolveAssemblyReferences.cache index aec1d2d..9cc9493 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferences.cache and b/obj/Debug/DesignTimeResolveAssemblyReferences.cache differ diff --git a/obj/Debug/OSS.dll b/obj/Debug/OSS.dll index 7c20955..dfad8c4 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 b186346..63d4243 100644 Binary files a/obj/Debug/OSS.pdb and b/obj/Debug/OSS.pdb differ