@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Sales.SalesQuotation.Cqrs @using Indotalent.Features.Sales.SalesQuotation.Components @using Indotalent.Shared.Utils @using MudBlazor @using Microsoft.JSInterop @inject SalesQuotationService SalesQuotationService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
@(ReadOnly ? "Details" : "Edit") Quotation @_model.AutoNumber
@if (ReadOnly || !string.IsNullOrEmpty(_model.Id)) { @if (_isPrinting) { Generating PDF... } else { Print PDF } }
Basic Information Quotation Date Customer @foreach (var item in _lookup.Customers) { @item.Name } Status @foreach (var item in _lookup.Statuses) { @item.Name } Tax @foreach (var item in _lookup.Taxes) { @item.Name } Description <_SalesQuotationItemDataTable Items="_items" SalesQuotationId="@(_model.Id ?? string.Empty)" ReadOnly="ReadOnly" OnChanged="RefreshItems" /> Quotation Summary
Sub Total @_model.BeforeTaxAmount?.ToString("N2")
Tax Amount @_model.TaxAmount?.ToString("N2")
Total Amount @_model.AfterTaxAmount?.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 UpdateSalesQuotationRequest 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 UpdateSalesQuotationValidator _validator = new(); private UpdateSalesQuotationRequest _model = new(); private List _items = new(); private SalesQuotationLookupResponse _lookup = new(); private bool _processing = false; private bool _isPrinting = false; protected override async Task OnInitializedAsync() { var resLookup = await SalesQuotationService.GetSalesQuotationLookupAsync(); if (resLookup != null && resLookup.IsSuccess) _lookup = resLookup.Value ?? new(); _model = Data; await RefreshItems(); } private async Task OnTaxChanged(string newTaxId) { if (ReadOnly || _model.TaxId == newTaxId) return; _model.TaxId = newTaxId; StateHasChanged(); await SubmitInternal(false); Snackbar.Add("Tax updated and totals recalculated.", Severity.Info); await RefreshItems(); } private async Task RefreshItems() { try { var response = await SalesQuotationService.GetSalesQuotationByIdAsync(_model.Id!); await Task.Delay(500); if (response != null && response.IsSuccess && response.Value != null) { _items = response.Value.Items ?? new(); _model.BeforeTaxAmount = response.Value.BeforeTaxAmount; _model.TaxAmount = response.Value.TaxAmount; _model.AfterTaxAmount = response.Value.AfterTaxAmount; 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 SalesQuotationService.GetSalesQuotationByIdAsync(_model.Id!); if (response != null && response.IsSuccess && response.Value != null) { var pdfBytes = SalesQuotationPdfGenerator.Generate(response.Value); var fileName = $"SQ_{response.Value.AutoNumber}.pdf"; var base64 = Convert.ToBase64String(pdfBytes); await JSRuntime.InvokeVoidAsync("downloadFile", fileName, "application/pdf", base64); Snackbar.Add("PDF generated successfully.", Severity.Success); } } catch (Exception ex) { Snackbar.Add($"Error: {ex.Message}", Severity.Error); } finally { _isPrinting = false; StateHasChanged(); } } private async Task Submit() { await SubmitInternal(true); } private async Task SubmitInternal(bool showNotification) { if (ReadOnly) return; await _form.Validate(); if (!_form.IsValid) return; _processing = true; StateHasChanged(); try { var response = await SalesQuotationService.UpdateSalesQuotationAsync(_model); await Task.Delay(500); if (response != null && response.IsSuccess) { if (showNotification) { Snackbar.Add("Updated successfully", Severity.Success); await OnSuccess.InvokeAsync(); } } } catch (Exception ex) { Snackbar.Add($"Error: {ex.Message}", Severity.Error); } finally { _processing = false; StateHasChanged(); } } }