@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Sales.Invoice.Cqrs @using Indotalent.Features.Sales.Invoice.Components @using Indotalent.Shared.Utils @using MudBlazor @using Microsoft.JSInterop @inject InvoiceService InvoiceService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
@(ReadOnly ? "Details" : "Edit") Invoice @_model.AutoNumber
@if (ReadOnly || !string.IsNullOrEmpty(_model.Id)) { @if (_isPrinting) { Generating PDF... } else { Print PDF } }
Basic Information Invoice Date Sales Order Reference @foreach (var item in _lookup.SalesOrders) { @item.Name } Status @foreach (var item in _lookup.Statuses) { @item.Name } Description <_InvoiceSalesOrderItemDataTable Items="_items" ReadOnly="true" /> Payment Summary
Sub Total @_beforeTax.ToString("N2")
Tax Amount @_taxAmount.ToString("N2")
Total Amount @_afterTax.ToString("N2")
Audit History Created At @DateTimeExtensions.ToString(_model.CreatedAt) Created By @(!string.IsNullOrEmpty(_model.CreatedBy) ? _model.CreatedBy : "System") Last Updated At @DateTimeExtensions.ToString(_model.UpdatedAt) Last Updated By @(!string.IsNullOrEmpty(_model.UpdatedBy) ? _model.UpdatedBy : "System")
@(ReadOnly ? "Back to List" : "Cancel") @if (!ReadOnly) { @if (_processing) { Updating... } else { Save Changes } }
@code { [Parameter] public UpdateInvoiceRequest Data { get; set; } = new(); [Parameter] public bool ReadOnly { get; set; } = false; [Parameter] public EventCallback OnCancel { get; set; } [Parameter] public EventCallback OnSuccess { get; set; } private MudForm _form = default!; private UpdateInvoiceRequest _model = new(); private List _items = new(); private InvoiceLookupResponse _lookup = new(); private decimal _beforeTax = 0; private decimal _taxAmount = 0; private decimal _afterTax = 0; private bool _processing = false; private bool _isPrinting = false; protected override async Task OnInitializedAsync() { var resLookup = await InvoiceService.GetInvoiceLookupAsync(); if (resLookup != null && resLookup.IsSuccess) _lookup = resLookup.Value ?? new(); _model = Data; await RefreshItems(); } private async Task OnSalesOrderChanged(string soId) { if (ReadOnly || _model.SalesOrderId == soId) return; _model.SalesOrderId = soId; await RefreshItems(); Snackbar.Add("Sales Order reference changed. Payment summary and items have been updated.", Severity.Info); } private async Task RefreshItems() { if (string.IsNullOrEmpty(_model.SalesOrderId)) return; try { var response = await InvoiceService.GetSalesOrderDetailForInvoiceAsync(_model.SalesOrderId); await Task.Delay(500); if (response != null && response.IsSuccess && response.Value != null) { _items = response.Value.Items ?? new(); _beforeTax = response.Value.BeforeTaxAmount ?? 0; _taxAmount = response.Value.TaxAmount ?? 0; _afterTax = response.Value.AfterTaxAmount ?? 0; StateHasChanged(); } } catch (Exception ex) { Snackbar.Add($"Error: {ex.Message}", Severity.Error); } } private async Task DownloadPdf() { try { _isPrinting = true; StateHasChanged(); await Task.Delay(1000); var response = await InvoiceService.GetInvoiceByIdAsync(_model.Id!); if (response != null && response.IsSuccess && response.Value != null) { var pdfBytes = InvoicePdfGenerator.Generate(response.Value); var fileName = $"INV_{response.Value.AutoNumber}.pdf"; var base64 = Convert.ToBase64String(pdfBytes); await JSRuntime.InvokeVoidAsync("downloadFile", fileName, "application/pdf", base64); Snackbar.Add("PDF generated successfully.", Severity.Success); } } finally { _isPrinting = false; StateHasChanged(); } } private async Task Submit() { await _form.Validate(); if (!_form.IsValid) return; _processing = true; try { var response = await InvoiceService.UpdateInvoiceAsync(_model); await Task.Delay(500); if (response != null && response.IsSuccess) { Snackbar.Add("Updated successfully", Severity.Success); await OnSuccess.InvokeAsync(); } } finally { _processing = false; } } }