Restore all zeroed views + add bulk gift certificate creation
The HTML entity sweep script had a bug where it wrote empty files for any
view that contained no target Unicode characters, zeroing out 215 view files.
All views restored from the pre-sweep commit (cefdf3e).
Bulk gift certificate feature:
- BulkCreateGiftCertificateDto with Quantity (1-500), Amount, Reason, Expiry, Notes
- GenerateBulkGiftCertificatePdfAsync on IPdfService / PdfService: one Letter page
per cert, reusing the same purple/gold branded ComposeGiftCertificateContent helper
- GiftCertificatesController: BulkCreate GET/POST, BulkResult GET, BulkDownloadPdf POST
- Views: BulkCreate.cshtml (form with live total preview), BulkResult.cshtml (table +
Download All PDF button that POSTs cert IDs to avoid URL length limits)
- gift-certificate-bulk.js: live preview + spinner/disable on submit
- Index.cshtml: Bulk Create button added alongside New Certificate
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -440,6 +440,184 @@ public class GiftCertificatesController : Controller
|
||||
return acct?.Id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows the bulk certificate creation form. Defaults to Promotional reason and 25 certificates
|
||||
/// since the primary use case is car shows and events where a batch of same-value certificates
|
||||
/// is distributed to attendees.
|
||||
/// </summary>
|
||||
public IActionResult BulkCreate()
|
||||
{
|
||||
return View(new BulkCreateGiftCertificateDto());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates N gift certificates in a single batch, records GL entries for each, then redirects
|
||||
/// to a confirmation page where the user can download the full batch as a single print-ready PDF.
|
||||
/// Certificate codes are generated sequentially so the batch occupies a contiguous range (e.g.
|
||||
/// GC-2506-0012 through GC-2506-0036), making it easy to audit which codes belong to each event.
|
||||
/// GL treatment mirrors single-certificate issuance: Sold certs debit Checking, all others debit
|
||||
/// Sales Discounts (4950) and credit GC Liability (2500).
|
||||
/// </summary>
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> BulkCreate(BulkCreateGiftCertificateDto dto)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return View(dto);
|
||||
|
||||
try
|
||||
{
|
||||
var currentUser = await _userManager.GetUserAsync(User);
|
||||
var companyId = currentUser?.CompanyId ?? 0;
|
||||
|
||||
var gcLiabilityAcctId = await GetGcLiabilityAccountIdAsync(companyId);
|
||||
int? checkingAcctId = null;
|
||||
int? discountAcctId = null;
|
||||
|
||||
if (dto.IssuedReason == GiftCertificateIssuedReason.Sold)
|
||||
{
|
||||
var acct = await _unitOfWork.Accounts.FirstOrDefaultAsync(
|
||||
a => a.IsActive && (a.AccountSubType == AccountSubTypeEnum.Checking
|
||||
|| a.AccountSubType == AccountSubTypeEnum.Cash));
|
||||
checkingAcctId = acct?.Id;
|
||||
}
|
||||
else
|
||||
{
|
||||
var acct = await _unitOfWork.Accounts.FirstOrDefaultAsync(
|
||||
a => a.IsActive && a.AccountNumber == "4950");
|
||||
discountAcctId = acct?.Id;
|
||||
}
|
||||
|
||||
var createdIds = new List<int>();
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
for (int i = 0; i < dto.Quantity; i++)
|
||||
{
|
||||
var code = await GenerateCertificateCodeAsync(companyId);
|
||||
|
||||
var cert = new GiftCertificate
|
||||
{
|
||||
CertificateCode = code,
|
||||
OriginalAmount = dto.Amount,
|
||||
RedeemedAmount = 0,
|
||||
IssuedReason = dto.IssuedReason,
|
||||
Status = GiftCertificateStatus.Active,
|
||||
IssueDate = now,
|
||||
ExpiryDate = dto.ExpiryDate,
|
||||
Notes = dto.Notes,
|
||||
IssuedById = currentUser?.Id,
|
||||
CompanyId = companyId,
|
||||
CreatedAt = now,
|
||||
CreatedBy = currentUser?.Email
|
||||
};
|
||||
|
||||
await _unitOfWork.GiftCertificates.AddAsync(cert);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
createdIds.Add(cert.Id);
|
||||
|
||||
await _accountBalanceService.CreditAsync(gcLiabilityAcctId, cert.OriginalAmount);
|
||||
if (dto.IssuedReason == GiftCertificateIssuedReason.Sold)
|
||||
await _accountBalanceService.DebitAsync(checkingAcctId, cert.OriginalAmount);
|
||||
else
|
||||
await _accountBalanceService.DebitAsync(discountAcctId, cert.OriginalAmount);
|
||||
}
|
||||
|
||||
TempData["BulkCertIds"] = System.Text.Json.JsonSerializer.Serialize(createdIds);
|
||||
TempData["BulkCertCount"] = dto.Quantity;
|
||||
TempData["BulkCertAmount"] = dto.Amount.ToString("F2");
|
||||
return RedirectToAction(nameof(BulkResult));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error creating bulk gift certificates");
|
||||
this.ToastError("An error occurred creating the certificates.");
|
||||
return View(dto);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays the bulk creation confirmation page with the list of generated certificate codes
|
||||
/// and a button to download all as a single print-ready PDF.
|
||||
/// </summary>
|
||||
public async Task<IActionResult> BulkResult()
|
||||
{
|
||||
if (TempData["BulkCertIds"] is not string json)
|
||||
return RedirectToAction(nameof(Index));
|
||||
|
||||
var ids = System.Text.Json.JsonSerializer.Deserialize<List<int>>(json) ?? new();
|
||||
var certs = await _unitOfWork.GiftCertificates.FindAsync(gc => ids.Contains(gc.Id), false);
|
||||
|
||||
ViewBag.CertCount = TempData["BulkCertCount"];
|
||||
ViewBag.CertAmount = TempData["BulkCertAmount"];
|
||||
ViewBag.CertIds = ids;
|
||||
return View(certs.OrderBy(c => c.CertificateCode).ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates and streams a single PDF containing one page per certificate in the batch.
|
||||
/// The certificate IDs are posted as a form array so there is no URL-length limit on batch size.
|
||||
/// </summary>
|
||||
[HttpPost, ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> BulkDownloadPdf(int[] certIds)
|
||||
{
|
||||
if (certIds == null || certIds.Length == 0)
|
||||
return BadRequest();
|
||||
|
||||
var currentUser = await _userManager.GetUserAsync(User);
|
||||
var companyId = currentUser?.CompanyId ?? 0;
|
||||
var company = await _unitOfWork.Companies.GetByIdAsync(companyId);
|
||||
|
||||
var companyInfo = new Application.DTOs.Company.CompanyInfoDto
|
||||
{
|
||||
CompanyName = company?.CompanyName ?? string.Empty,
|
||||
Phone = company?.Phone,
|
||||
Address = company?.Address,
|
||||
City = company?.City,
|
||||
State = company?.State,
|
||||
ZipCode = company?.ZipCode,
|
||||
PrimaryContactEmail = company?.PrimaryContactEmail
|
||||
};
|
||||
|
||||
var certs = await _unitOfWork.GiftCertificates.FindAsync(
|
||||
gc => certIds.Contains(gc.Id), false,
|
||||
gc => gc.RecipientCustomer);
|
||||
|
||||
var dtos = certs.OrderBy(c => c.CertificateCode).Select(cert => new GiftCertificateDto
|
||||
{
|
||||
Id = cert.Id,
|
||||
CertificateCode = cert.CertificateCode,
|
||||
OriginalAmount = cert.OriginalAmount,
|
||||
RedeemedAmount = cert.RedeemedAmount,
|
||||
RemainingBalance = cert.RemainingBalance,
|
||||
RecipientName = cert.RecipientCustomer != null
|
||||
? (cert.RecipientCustomer.CompanyName ?? $"{cert.RecipientCustomer.ContactFirstName} {cert.RecipientCustomer.ContactLastName}".Trim())
|
||||
: cert.RecipientName,
|
||||
RecipientEmail = cert.RecipientEmail,
|
||||
IssuedReason = cert.IssuedReason,
|
||||
Status = cert.Status,
|
||||
IssueDate = cert.IssueDate,
|
||||
ExpiryDate = cert.ExpiryDate,
|
||||
Notes = cert.Notes
|
||||
}).ToList();
|
||||
|
||||
try
|
||||
{
|
||||
var (logoData, logoContentType) = await LoadCompanyLogoAsync(company);
|
||||
var pdfBytes = await _pdfService.GenerateBulkGiftCertificatePdfAsync(dtos, logoData, logoContentType, companyInfo);
|
||||
var firstName = dtos.FirstOrDefault()?.CertificateCode ?? "batch";
|
||||
var lastName = dtos.LastOrDefault()?.CertificateCode ?? string.Empty;
|
||||
var fileName = dtos.Count == 1
|
||||
? $"GiftCertificate-{firstName}.pdf"
|
||||
: $"GiftCertificates-{firstName}-to-{lastName}.pdf";
|
||||
return File(pdfBytes, "application/pdf", fileName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error generating bulk gift certificate PDF");
|
||||
TempData["Error"] = "Could not generate PDF.";
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<(byte[]? LogoData, string? LogoContentType)> LoadCompanyLogoAsync(Company? company)
|
||||
{
|
||||
if (company == null) return (null, null);
|
||||
|
||||
Reference in New Issue
Block a user