From 0f4176a5b254b8b3134eab64e5cfeea5b7a502f1 Mon Sep 17 00:00:00 2001 From: Leandro Hernan Rojas Date: Wed, 9 Apr 2025 23:52:00 -0300 Subject: [PATCH] Add CustomerForm --- Domain/Entities/ECustomerAddress.cs | 15 --- Domain/Entities/ECustomerDocument.cs | 4 - Domain/Entities/EDocumentType.cs | 2 - .../Pages/Sales/CustomerForm.razor | 105 ++++++++++++++++++ .../Pages/Sales/Customers.razor | 49 ++++---- .../Services/Sales/CustomerService.cs | 6 + phronCare.UIBlazor/Shared/NavMenu.razor | 2 +- 7 files changed, 137 insertions(+), 46 deletions(-) create mode 100644 phronCare.UIBlazor/Pages/Sales/CustomerForm.razor create mode 100644 phronCare.UIBlazor/Services/Sales/CustomerService.cs diff --git a/Domain/Entities/ECustomerAddress.cs b/Domain/Entities/ECustomerAddress.cs index 5c4743b..d195c6c 100644 --- a/Domain/Entities/ECustomerAddress.cs +++ b/Domain/Entities/ECustomerAddress.cs @@ -9,34 +9,19 @@ namespace Domain.Entities public class ECustomerAddress { public int Id { get; set; } - public int CustomersId { get; set; } - public string? BusinessName { get; set; } - public string? Streetaddress1 { get; set; } - public string? Streetaddress2 { get; set; } - public string? City { get; set; } - public string? Stateprovince { get; set; } - public string? Postalcode { get; set; } - public string? Country { get; set; } - public decimal? Latitude { get; set; } - public decimal? Longitude { get; set; } - public string? Phonenumber { get; set; } - public string? Email { get; set; } - public string? Notes { get; set; } - - //public virtual ECustomer? Customers { get; set; } } } diff --git a/Domain/Entities/ECustomerDocument.cs b/Domain/Entities/ECustomerDocument.cs index 0bba1f4..c6294c7 100644 --- a/Domain/Entities/ECustomerDocument.cs +++ b/Domain/Entities/ECustomerDocument.cs @@ -19,9 +19,5 @@ namespace Domain.Entities public DateOnly? IssueDate { get; set; } public DateOnly? ExpiryDate { get; set; } - - //public virtual ECustomer Customers { get; set; } = null!; - - public virtual EDocumentType Documenttypes { get; set; } = null!; } } diff --git a/Domain/Entities/EDocumentType.cs b/Domain/Entities/EDocumentType.cs index cb77cb3..54735a2 100644 --- a/Domain/Entities/EDocumentType.cs +++ b/Domain/Entities/EDocumentType.cs @@ -9,7 +9,5 @@ public string Name { get; set; } = null!; public string? Description { get; set; } - - //public virtual ICollection PhSCustomerDocuments { get; set; } = new List(); } } diff --git a/phronCare.UIBlazor/Pages/Sales/CustomerForm.razor b/phronCare.UIBlazor/Pages/Sales/CustomerForm.razor new file mode 100644 index 0000000..c83c207 --- /dev/null +++ b/phronCare.UIBlazor/Pages/Sales/CustomerForm.razor @@ -0,0 +1,105 @@ +@page "/sales/customerform" +@page "/sales/customerform/{CustomerId:int}" + +@inject HttpClient _httpClient +@inject NavigationManager Navigation +@inject IToastService toastService +@inject AuthenticationStateProvider authenticationStateProvider + + + + + +
+
+
+ + +
+
+
+
+ + + @foreach (var type in accountTypes) + { + + } + +
+
+
+ + + +
+
+ + +
+
+
+ +@code { + [Parameter] + public int? CustomerId { get; set; } + + private ECustomer customer { get; set; } = new(); + private List accountTypes = new(); + + private string returnUrl = "/sales/customers"; + + protected override async Task OnInitializedAsync() + { + // await LoadAccountTypes(); + + // if (CustomerId.HasValue) + // { + // // Cargar datos del cliente existente desde la API + // customer = await _httpClient.GetFromJsonAsync($"/api/Customer/GetById/{CustomerId.Value}") ?? new(); + // } + } + + private async Task LoadAccountTypes() + { + var result = await _httpClient.GetFromJsonAsync>("/api/AccountType/GetAll"); + accountTypes = result ?? new(); + } + + private async Task HandleValidSubmit() + { + try + { + HttpResponseMessage response; + + if (CustomerId.HasValue) + { + response = await _httpClient.PutAsJsonAsync("/api/Customer/Update", customer); + } + else + { + response = await _httpClient.PostAsJsonAsync("/api/Customer/Create", customer); + } + + if (response.IsSuccessStatusCode) + { + toastService.ShowSuccess("Cliente guardado exitosamente"); + Navigation.NavigateTo(returnUrl); + } + else + { + var error = await response.Content.ReadAsStringAsync(); + toastService.ShowError($"Error: {error}"); + } + } + catch (Exception ex) + { + toastService.ShowError($"Error: {ex.Message}"); + } + } + + private void Cancel() + { + Navigation.NavigateTo(returnUrl); + } +} diff --git a/phronCare.UIBlazor/Pages/Sales/Customers.razor b/phronCare.UIBlazor/Pages/Sales/Customers.razor index 2e508ed..6746331 100644 --- a/phronCare.UIBlazor/Pages/Sales/Customers.razor +++ b/phronCare.UIBlazor/Pages/Sales/Customers.razor @@ -13,8 +13,6 @@

Listado de clientes

@* wtf? *@
- @*

Buscar Clientes

*@ -
@@ -25,14 +23,14 @@ @if (TablaClientes != null && TablaClientes.Any()) { + Data="TablaClientes" + SelectionField="Id" + RowsPerPage=SearchParams.PageSize + RenderButtons="true" Buttons="botones" + ShowPageButtons="false" + ShowQuickSearch="false" + RenderSelect="false" + />
@@ -76,27 +74,30 @@ private async Task CargarClientes() { PagedResult = await CustomerService.SearchCustomersAsync(SearchParams); - - TablaClientes = PagedResult.Items.Select(c => + if (PagedResult?.Items is not null) { - var addr = c.PhSCustomerAddresses.FirstOrDefault(); - var doc = c.PhSCustomerDocuments.FirstOrDefault(); + TablaClientes = PagedResult.Items.Select(c => + { + var addr = c.PhSCustomerAddresses.FirstOrDefault(); + var doc = c.PhSCustomerDocuments.FirstOrDefault(); - return new Dictionary - { + return new Dictionary + { { "Id", c.Id }, { "Nombre", c.Name ?? string.Empty }, { "Activo", c.Active ? "Sí" : "No" }, { "Crédito", c.HasCreditAccount ? "Sí" : "No" }, { "Límite", c.CreditLimit }, - { "Email", addr?.Email ?? "" }, - { "Teléfono", addr?.Phonenumber ?? "" }, - { "Dirección", $"{addr?.Streetaddress1} {addr?.Streetaddress2}, {addr?.City}, {addr?.Postalcode}, {addr?.Country}" }, - { "Documento", $"{doc?.Documenttypes} | {doc?.DocumentNumber}" } - }; - }).ToList(); + { "Email", addr?.Email ?? string.Empty }, + { "Teléfono", addr?.Phonenumber ?? string.Empty }, + { "Dirección", addr is not null + ? $"{addr.Streetaddress1}, {addr.City}, {addr.Postalcode}" + : string.Empty }, + { "Documento", doc?.DocumentNumber ?? string.Empty } + }; + }).ToList(); + } } - private async Task SiguientePagina() => await CambiarPagina(1); private async Task AnteriorPagina() => await CambiarPagina(-1); private async Task CambiarPagina(int delta) @@ -121,7 +122,7 @@ } List botones = new List { - new PhTable.ButtonOptions{ Caption="Editar", ElementClass="btn btn-primary btn-circle btn-sm"} + new PhTable.ButtonOptions{ Caption="Editar", ElementClass="btn btn-primary btn-sm"} }; private int TotalPaginas => PagedResult is null ? 1 : (int)Math.Ceiling((double)(PagedResult.TotalItems) / SearchParams.PageSize); diff --git a/phronCare.UIBlazor/Services/Sales/CustomerService.cs b/phronCare.UIBlazor/Services/Sales/CustomerService.cs new file mode 100644 index 0000000..610bb29 --- /dev/null +++ b/phronCare.UIBlazor/Services/Sales/CustomerService.cs @@ -0,0 +1,6 @@ +namespace phronCare.UIBlazor.Services.Sales +{ + public class CustomerService + { + } +} diff --git a/phronCare.UIBlazor/Shared/NavMenu.razor b/phronCare.UIBlazor/Shared/NavMenu.razor index 0417354..cfcbe07 100644 --- a/phronCare.UIBlazor/Shared/NavMenu.razor +++ b/phronCare.UIBlazor/Shared/NavMenu.razor @@ -67,7 +67,7 @@ {