diff --git a/.vs/OSS/v16/.suo b/.vs/OSS/v16/.suo index aec2dc5..23923bd 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 5c6705f..964d067 100644 --- a/Controllers/NewCOIController.cs +++ b/Controllers/NewCOIController.cs @@ -21,6 +21,7 @@ using System.Data.Entity.Migrations; using System.Web.WebPages; using System.Data.Entity.Validation; using System.Data.SqlClient; +using System.Data; namespace OSS.Controllers { @@ -2266,62 +2267,85 @@ namespace OSS.Controllers return RedirectToAction("Payment"); } - // Check if additional invoice already exists in tblInvoice - var existingInvoiceId = myContext.Database.SqlQuery( - "SELECT TOP 1 InvoiceID FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC", - code).FirstOrDefault(); + long? existingInvoiceId = null; + try + { + existingInvoiceId = myContext.Database.SqlQuery( + "SELECT TOP 1 InvoiceID FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC", + code).FirstOrDefault(); + } + catch + { + // Continue to create new invoice + } if (existingInvoiceId.HasValue) { - // Get the existing invoice details - var invoiceDetails = myContext.Database.SqlQuery( - "SELECT Amount, Currency FROM tblInvoice WHERE InvoiceID = @p0", - existingInvoiceId.Value).FirstOrDefault(); - - if (invoiceDetails != null) + try { - Session["Amount"] = invoiceDetails.Amount.ToString(); - var currency = invoiceDetails.Currency ?? "TZS"; - Session["Currency"] = currency; - Session["AmountinWords"] = CurrencyUtils.ToWords(Convert.ToDecimal(invoiceDetails.Amount)) + " " + currency; + var invoiceDetails = myContext.Database.SqlQuery( + "SELECT Amount, Currency FROM tblInvoice WHERE InvoiceID = @p0", + existingInvoiceId.Value).FirstOrDefault(); + + if (invoiceDetails != null) + { + Session["Amount"] = invoiceDetails.Amount.ToString(); + var currency = invoiceDetails.Currency ?? "TZS"; + Session["Currency"] = currency; + Session["AmountinWords"] = CurrencyUtils.ToWords(Convert.ToDecimal(invoiceDetails.Amount)) + " " + currency; + } + ViewBag.HideAdditionalButton = true; + ViewBag.HideInvoiceGrid = true; + TempData["HidePaidInvoices"] = true; + TempData["AdditionalContext"] = true; + return View("Payment"); + } + catch (Exception ex) + { + TempData["PaymentError"] = "Error retrieving existing invoice: " + ex.Message; + EnsurePaymentSessionDefaults("TZS"); + return RedirectToAction("Payment"); } - ViewBag.HideAdditionalButton = true; - TempData["HidePaidInvoices"] = true; - TempData["AdditionalContext"] = true; - return View("Payment"); } - // Generate BillItemRefNo (using timestamp and project code) string billItemRefNo = "BILL-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + code; - - // Determine ServiceTypeID (default to 1, adjust if needed based on business logic) int serviceTypeID = 1; - // Insert directly into tblInvoice using the parent application details string sql = @" INSERT INTO [dbo].[tblInvoice] ([FullName],[PhoneNo],[GFSCode],[BillItemRefNo],[SubServiceName],[Amount],FrontUserId,ApplicationID,StartDate,Expiredate,ServiceTypeID,ApplicationCode,[Currency],[TIN_No]) VALUES (@FullName,@PhoneNo,@GFSCode,@BillItemRefNo,@SubServiceName,@Amount,@FrontUserId,@ApplicationID,GETDATE(),DATEADD(day,30,GETDATE()),@ServiceTypeID,@ApplicationCode,@Currency,@TIN_No)"; - int rowsAffected = myContext.Database.ExecuteSqlCommand(sql, - new SqlParameter("@FullName", (object)parentApplication.FullName ?? DBNull.Value), - new SqlParameter("@PhoneNo", (object)parentApplication.MobileNo ?? DBNull.Value), - new SqlParameter("@GFSCode", (object)gfsCode ?? DBNull.Value), - new SqlParameter("@BillItemRefNo", (object)billItemRefNo ?? DBNull.Value), - new SqlParameter("@SubServiceName", (object)"Additional_Amount" ?? DBNull.Value), - new SqlParameter("@Amount", addAmount), - new SqlParameter("@FrontUserId", (object)username ?? DBNull.Value), - new SqlParameter("@ApplicationID", (object)parentApplication.ApplicationID ?? DBNull.Value), - new SqlParameter("@ServiceTypeID", serviceTypeID), - new SqlParameter("@ApplicationCode", (object)code ?? DBNull.Value), - new SqlParameter("@Currency", "TZS"), - new SqlParameter("@TIN_No", (object)parentApplication.CompanyTIN ?? DBNull.Value) - ); - - if (rowsAffected <= 0) + try { - TempData["PaymentError"] = "Could not create additional payment invoice in tblInvoice."; + var parameters = new SqlParameter[] + { + new SqlParameter("@FullName", SqlDbType.NVarChar, -1) { Value = (object)parentApplication.FullName ?? DBNull.Value }, + new SqlParameter("@PhoneNo", SqlDbType.NVarChar, -1) { Value = (object)parentApplication.MobileNo ?? DBNull.Value }, + new SqlParameter("@GFSCode", SqlDbType.NVarChar, 50) { Value = (object)gfsCode ?? DBNull.Value }, + new SqlParameter("@BillItemRefNo", SqlDbType.NVarChar, 50) { Value = (object)billItemRefNo ?? DBNull.Value }, + new SqlParameter("@SubServiceName", SqlDbType.VarChar, -1) { Value = "Additional_Amount" }, + new SqlParameter("@Amount", SqlDbType.Decimal) { Value = addAmount, Precision = 18, Scale = 2 }, + new SqlParameter("@FrontUserId", SqlDbType.BigInt) { Value = DBNull.Value }, + new SqlParameter("@ApplicationID", SqlDbType.BigInt) { Value = parentApplication.ApplicationID }, + new SqlParameter("@ServiceTypeID", SqlDbType.BigInt) { Value = (long)serviceTypeID }, + new SqlParameter("@ApplicationCode", SqlDbType.NVarChar, -1) { Value = (object)code ?? DBNull.Value }, + new SqlParameter("@Currency", SqlDbType.VarChar, 10) { Value = "TZS" }, + new SqlParameter("@TIN_No", SqlDbType.NVarChar, -1) { Value = (object)parentApplication.CompanyTIN ?? DBNull.Value } + }; + + int rowsAffected = myContext.Database.ExecuteSqlCommand(sql, parameters); + if (rowsAffected <= 0) + { + TempData["PaymentError"] = "Could not create additional payment invoice."; + EnsurePaymentSessionDefaults("TZS"); + return RedirectToAction("Payment"); + } + } + catch (Exception ex) + { + TempData["PaymentError"] = "Error creating additional payment invoice: " + ex.Message; EnsurePaymentSessionDefaults("TZS"); return RedirectToAction("Payment"); } @@ -2332,13 +2356,14 @@ namespace OSS.Controllers // In Additional Payment context: hide the "Pay Additional Amount" button and hide previous paid invoices ViewBag.HideAdditionalButton = true; + ViewBag.HideInvoiceGrid = true; TempData["HidePaidInvoices"] = true; TempData["AdditionalContext"] = true; return View("Payment"); } catch (Exception ex) { - TempData["PaymentError"] = "Error creating additional payment invoice. " + ex.Message; + TempData["PaymentError"] = "Error creating additional payment invoice: " + ex.Message; EnsurePaymentSessionDefaults("TZS"); return RedirectToAction("Payment"); } diff --git a/Views/NewCOI/Payment.cshtml b/Views/NewCOI/Payment.cshtml index 6047dbf..f3c678c 100644 --- a/Views/NewCOI/Payment.cshtml +++ b/Views/NewCOI/Payment.cshtml @@ -6,6 +6,12 @@ /**/ Layout = "~/Views/Shared/_Layout.cshtml"; + + bool hideInvoiceGrid = false; + if (ViewBag.HideInvoiceGrid != null) + { + bool.TryParse(ViewBag.HideInvoiceGrid.ToString(), out hideInvoiceGrid); + } } @@ -82,8 +88,10 @@

-
Invoice Details
- @(Html.DevExtreme().DataGrid() + @if (!hideInvoiceGrid) + { +
Invoice Details
+ @(Html.DevExtreme().DataGrid() .ID("invoiceGrid") .DataSource(ds => ds.WebApi() .RouteName("CompanyProfile") @@ -140,6 +148,7 @@ columns.AddFor(m => m.CompanyEmail).Caption("Company Email").Visible(true).Allow ) ) ) + } @@ -178,7 +187,7 @@ columns.AddFor(m => m.CompanyEmail).Caption("Company Email").Visible(true).Allow } - @if (TempData["HidePaidInvoices"] != null) + @if (TempData["HidePaidInvoices"] != null && !hideInvoiceGrid) { } - @if (TempData["AdditionalContext"] != null) + @if (TempData["AdditionalContext"] != null && !hideInvoiceGrid) {