This commit is contained in:
parent
9496a74f1c
commit
4b36f5e995
BIN
.vs/OSS/v16/.suo
BIN
.vs/OSS/v16/.suo
Binary file not shown.
|
|
@ -20,6 +20,7 @@ using Rotativa;
|
|||
using System.Data.Entity.Migrations;
|
||||
using System.Web.WebPages;
|
||||
using System.Data.Entity.Validation;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace OSS.Controllers
|
||||
{
|
||||
|
|
@ -2220,16 +2221,22 @@ namespace OSS.Controllers
|
|||
return RedirectToAction("Payment");
|
||||
}
|
||||
|
||||
var project = myContext.ProjectProfilesExternal.FirstOrDefault(t => t.ProjectCode == code);
|
||||
if (project == null)
|
||||
// Find the parent application using ProjectCode
|
||||
var parentApplication = myContext.ApplicationManagers
|
||||
.Where(a => a.ProjectCode == code)
|
||||
.OrderByDescending(a => a.CreatedDate)
|
||||
.FirstOrDefault();
|
||||
|
||||
if (parentApplication == null)
|
||||
{
|
||||
TempData["PaymentError"] = "Project details not found.";
|
||||
TempData["PaymentError"] = "Parent application not found for this project code.";
|
||||
EnsurePaymentSessionDefaults("TZS");
|
||||
return RedirectToAction("Payment");
|
||||
}
|
||||
|
||||
// Resolve additional amount from tblService (ServiceName='Additional_Amount', Currency='TZS', Status='1')
|
||||
// Resolve additional amount and GFSCode from tblService (ServiceName='Additional_Amount', Currency='TZS', Status='1')
|
||||
decimal addAmount = 0m;
|
||||
string gfsCode = "142201370002"; // Default fallback
|
||||
try
|
||||
{
|
||||
var feeRow = myContext.ServiceFees
|
||||
|
|
@ -2240,7 +2247,11 @@ namespace OSS.Controllers
|
|||
(s.ServiceName ?? "").Trim() == "Additional_Amount");
|
||||
|
||||
if (feeRow != null && feeRow.Fee.HasValue && feeRow.Fee.Value > 0m)
|
||||
{
|
||||
addAmount = feeRow.Fee.Value;
|
||||
if (!string.IsNullOrWhiteSpace(feeRow.GFSCode))
|
||||
gfsCode = feeRow.GFSCode;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
|
@ -2255,87 +2266,70 @@ namespace OSS.Controllers
|
|||
return RedirectToAction("Payment");
|
||||
}
|
||||
|
||||
// If an additional invoice already exists, reuse it (check for all Additional_Amount variants)
|
||||
var existingInv = myContext.ApplicationManagers
|
||||
.OrderByDescending(a => a.CreatedDate)
|
||||
.FirstOrDefault(a => a.ProjectCode == code );
|
||||
// Check if additional invoice already exists in tblInvoice
|
||||
var existingInvoiceId = myContext.Database.SqlQuery<long?>(
|
||||
"SELECT TOP 1 InvoiceID FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC",
|
||||
code).FirstOrDefault();
|
||||
|
||||
if (existingInv != null)
|
||||
if (existingInvoiceId.HasValue)
|
||||
{
|
||||
Session["Amount"] = existingInv.Amount.ToString("0");
|
||||
var currency = string.IsNullOrWhiteSpace(existingInv.Currency) ? "TZS" : existingInv.Currency;
|
||||
Session["Currency"] = currency;
|
||||
Session["AmountinWords"] = CurrencyUtils.ToWords(existingInv.Amount) + " " + currency;
|
||||
// In Additional Payment context: hide the button and paid invoices, and mark additional context
|
||||
// Get the existing invoice details
|
||||
var invoiceDetails = myContext.Database.SqlQuery<dynamic>(
|
||||
"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;
|
||||
TempData["HidePaidInvoices"] = true;
|
||||
TempData["AdditionalContext"] = true;
|
||||
return View("Payment");
|
||||
}
|
||||
|
||||
// Find the original application invoice to copy all details from (exclude all Additional_Amount variants)
|
||||
var originalInvoice = myContext.ApplicationManagers
|
||||
.Where(a => a.ProjectCode == code &&
|
||||
a.ServiceName != "Additional_Amount" &&
|
||||
a.ServiceName != "Additional_Amount_New" &&
|
||||
a.ServiceName != "Additional_Amount_Expansion")
|
||||
.OrderByDescending(a => a.CreatedDate)
|
||||
.FirstOrDefault();
|
||||
// Generate BillItemRefNo (using timestamp and project code)
|
||||
string billItemRefNo = "BILL-" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + code;
|
||||
|
||||
var applicant = myContext.InvestorExternl.FirstOrDefault(x => x.CompanyEmail == username);
|
||||
// Determine ServiceTypeID (default to 1, adjust if needed based on business logic)
|
||||
int serviceTypeID = 1;
|
||||
|
||||
// Determine ServiceName based on original invoice (New or Expansion)
|
||||
string serviceNameForAdditional = "Additional_Amount";
|
||||
if (originalInvoice != null && !string.IsNullOrWhiteSpace(originalInvoice.ServiceName))
|
||||
// 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)
|
||||
{
|
||||
var originalService = originalInvoice.ServiceName.Trim();
|
||||
if (originalService.IndexOf("new", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
originalService.Equals("New", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
serviceNameForAdditional = "Additional_Amount_New";
|
||||
}
|
||||
else if (originalService.IndexOf("expansion", StringComparison.OrdinalIgnoreCase) >= 0 ||
|
||||
originalService.Equals("Expansion", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
serviceNameForAdditional = "Additional_Amount_Expansion";
|
||||
}
|
||||
TempData["PaymentError"] = "Could not create additional payment invoice in tblInvoice.";
|
||||
EnsurePaymentSessionDefaults("TZS");
|
||||
return RedirectToAction("Payment");
|
||||
}
|
||||
|
||||
// Create new invoice copying all details from original invoice, but with Additional_Amount service and amount
|
||||
var inv = new ApplicationManager
|
||||
{
|
||||
ProjectCode = code,
|
||||
ProjectName = originalInvoice?.ProjectName ?? (project.ProjectName ?? string.Empty),
|
||||
CompanyName = originalInvoice?.CompanyName ?? (project.CompanyName ?? string.Empty),
|
||||
ServiceName = serviceNameForAdditional,
|
||||
MobileNo = originalInvoice?.MobileNo ?? (applicant?.Mobile ?? string.Empty),
|
||||
FullName = originalInvoice?.FullName ?? string.Empty,
|
||||
CreatedDate = DateTime.Now,
|
||||
CompanyTIN = originalInvoice?.CompanyTIN ?? (Session["CompanyTIN"] as string) ?? string.Empty,
|
||||
CompanyEmail = username,
|
||||
Currency = "TZS",
|
||||
Station = originalInvoice?.Station ?? "HQ",
|
||||
Amount = addAmount, // 2,000,000 TZS from tblService
|
||||
StartDate = originalInvoice?.StartDate ?? DateTime.Now,
|
||||
Expiredate = originalInvoice?.Expiredate ?? DateTime.Now.AddYears(1),
|
||||
Comments = originalInvoice?.Comments ?? string.Empty,
|
||||
EvaluationStatus = originalInvoice?.EvaluationStatus ?? string.Empty,
|
||||
GePGComment = originalInvoice?.GePGComment ?? string.Empty
|
||||
};
|
||||
|
||||
Session["Amount"] = addAmount.ToString("0");
|
||||
Session["Currency"] = "TZS";
|
||||
Session["AmountinWords"] = CurrencyUtils.ToWords(addAmount) + " Tanzanian Shillings";
|
||||
|
||||
string saveErr;
|
||||
var company = myContext.CompanyProfileExternal.FirstOrDefault(c => c.AddedBy == username);
|
||||
if (!TrySaveInvoice(myContext, inv, company, out saveErr))
|
||||
{
|
||||
TempData["PaymentError"] = "Could not save additional payment invoice. " + saveErr;
|
||||
EnsurePaymentSessionDefaults("TZS");
|
||||
return RedirectToAction("Payment");
|
||||
}
|
||||
|
||||
// In Additional Payment context: hide the "Pay Additional Amount" button and hide previous paid invoices
|
||||
ViewBag.HideAdditionalButton = true;
|
||||
TempData["HidePaidInvoices"] = true;
|
||||
|
|
|
|||
BIN
bin/OSS.dll
BIN
bin/OSS.dll
Binary file not shown.
BIN
bin/OSS.pdb
BIN
bin/OSS.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue