@using Indotalent.Features.Sales.Invoice.Cqrs @using Indotalent.Shared.Utils @using MudBlazor @inject InvoiceService InvoiceService @inject ISnackbar Snackbar Add Invoice Create a new billing invoice from Sales Order. 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") Cancel @if (_processing) { Processing... } else { Create Invoice } @code { [Parameter] public EventCallback OnCancel { get; set; } [Parameter] public EventCallback OnSuccess { get; set; } private MudForm _form = default!; private CreateInvoiceRequest _model = new() { InvoiceDate = DateTime.Today, InvoiceStatus = Indotalent.Data.Enums.InvoiceStatus.Draft }; private InvoiceLookupResponse _lookup = new(); private List _items = new(); private decimal _beforeTax = 0; private decimal _taxAmount = 0; private decimal _afterTax = 0; private bool _processing = false; protected override async Task OnInitializedAsync() { var res = await InvoiceService.GetInvoiceLookupAsync(); if (res != null && res.IsSuccess) _lookup = res.Value ?? new(); } private async Task OnSalesOrderChanged(string soId) { _model.SalesOrderId = soId; if (string.IsNullOrEmpty(soId)) { _items.Clear(); _beforeTax = 0; _taxAmount = 0; _afterTax = 0; return; } var res = await InvoiceService.GetSalesOrderDetailForInvoiceAsync(soId); await Task.Delay(500); if (res != null && res.IsSuccess && res.Value != null) { _items = res.Value.Items; _beforeTax = res.Value.BeforeTaxAmount ?? 0; _taxAmount = res.Value.TaxAmount ?? 0; _afterTax = res.Value.AfterTaxAmount ?? 0; Snackbar.Add("Sales Order reference selected. Payment summary and items have been loaded.", Severity.Info); } } private async Task Submit() { await _form.Validate(); if (!_form.IsValid) return; _processing = true; try { var response = await InvoiceService.CreateInvoiceAsync(_model); await Task.Delay(500); if (response != null && response.IsSuccess) { Snackbar.Add("Created successfully", Severity.Success); await OnSuccess.InvokeAsync(); } } catch (Exception ex) { Snackbar.Add($"Error: {ex.Message}", Severity.Error); } finally { _processing = false; } } }