@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Sales.PaymentReceive.Cqrs @using Indotalent.Features.Sales.PaymentReceive.Components @using Indotalent.Shared.Utils @using MudBlazor @using Microsoft.JSInterop @inject PaymentReceiveService PaymentReceiveService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
@(ReadOnly ? "Details" : "Edit") Payment Receive @_model.AutoNumber
@if (ReadOnly || !string.IsNullOrEmpty(_model.Id)) { @if (_isPrinting) { Generating Receipt... } else { Print Receipt } }
Basic Information Payment Date Invoice Reference @foreach (var item in _lookup.Invoices) { @item.Name } Payment Method @foreach (var item in _lookup.PaymentMethods) { @item.Name } Status @foreach (var item in _lookup.Statuses) { @item.Name } Payment Amount Customer Description <_PaymentReceiveItemDataTable Items="_items" /> Invoice Commercial Summary
Sub Total @_invoiceSubTotal.ToString("N2")
Tax Amount @_invoiceTax.ToString("N2")
Grand Total @_invoiceGrandTotal.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 UpdatePaymentReceiveRequest 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 UpdatePaymentReceiveRequest _model = new(); private List _items = new(); private PaymentReceiveLookupResponse _lookup = new(); private string _customerName = ""; private decimal _invoiceSubTotal = 0; private decimal _invoiceTax = 0; private decimal _invoiceGrandTotal = 0; private bool _processing = false; private bool _isPrinting = false; protected override async Task OnInitializedAsync() { var resLookup = await PaymentReceiveService.GetPaymentReceiveLookupAsync(); if (resLookup != null && resLookup.IsSuccess) _lookup = resLookup.Value ?? new(); _model = Data; await LoadInvoiceDetails(_model.InvoiceId ?? ""); } private async Task OnInvoiceChanged(string invoiceId) { if (ReadOnly || _model.InvoiceId == invoiceId) return; _model.InvoiceId = invoiceId; await LoadInvoiceDetails(invoiceId); Snackbar.Add("Invoice items and amounts updated.", Severity.Info); } private async Task LoadInvoiceDetails(string invoiceId) { if (string.IsNullOrEmpty(invoiceId)) return; try { var response = await PaymentReceiveService.GetInvoiceDetailForPaymentAsync(invoiceId); await Task.Delay(500); if (response != null && response.IsSuccess && response.Value != null) { _items = response.Value.Items; _customerName = response.Value.CustomerName ?? ""; _invoiceSubTotal = response.Value.SalesOrderBeforeTaxAmount; _invoiceTax = response.Value.SalesOrderTaxAmount; _invoiceGrandTotal = response.Value.SalesOrderAfterTaxAmount; if (!ReadOnly) { _model.PaymentAmount = response.Value.PaymentAmount; } 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 PaymentReceiveService.GetPaymentReceiveByIdAsync(_model.Id!); if (response != null && response.IsSuccess && response.Value != null) { var pdfBytes = PaymentReceivePdfGenerator.Generate(response.Value); var fileName = $"Receipt_{response.Value.AutoNumber}.pdf"; var base64 = Convert.ToBase64String(pdfBytes); await JSRuntime.InvokeVoidAsync("downloadFile", fileName, "application/pdf", base64); Snackbar.Add("Receipt 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 PaymentReceiveService.UpdatePaymentReceiveAsync(_model); await Task.Delay(500); if (response != null && response.IsSuccess) { Snackbar.Add("Updated successfully", Severity.Success); await OnSuccess.InvokeAsync(); } } finally { _processing = false; } } }