Migliorato modal activity

This commit is contained in:
2025-09-15 17:11:17 +02:00
parent 0f3047a2b6
commit 83264731f3

View File

@@ -40,19 +40,18 @@
<div class="form-container"> <div class="form-container">
<span class="disable-full-width">Commessa</span> <span class="disable-full-width">Commessa</span>
@if (Commesse.IsNullOrEmpty()) <MudAutocomplete
{ Disabled="ActivityModel.Cliente.IsNullOrEmpty()"
<span class="warning-text">Nessuna commessa presente</span> T="JtbComt?"
} @bind-Value="SelectedComessa"
else @bind-Value:after="OnCommessaSelectedAfter"
{ SearchFunc="SearchCommesseAsync"
<MudSelectExtended FullWidth="true" ReadOnly="@(IsView || Commesse.IsNullOrEmpty())" T="string?" Variant="Variant.Text" @bind-Value="ActivityModel.CodJcom" @bind-Value:after="OnCommessaChanged" Class="customIcon-select" AdornmentIcon="@Icons.Material.Filled.Code"> ToStringFunc="@(c => c == null ? string.Empty : $"{c.CodJcom} - {c.Descrizione}")"
@foreach (var com in Commesse) Clearable="true"
{ ShowProgressIndicator="true"
<MudSelectItemExtended Class="custom-item-select" Value="@com.CodJcom">@($"{com.CodJcom} - {com.Descrizione}")</MudSelectItemExtended> DebounceInterval="300"
} MaxItems="50"
</MudSelectExtended> Class="customIcon-select" AdornmentIcon="@Icons.Material.Filled.Code"/>
}
</div> </div>
</div> </div>
@@ -91,13 +90,19 @@
<div class="input-card"> <div class="input-card">
<div class="form-container"> <div class="form-container">
<span class="disable-full-width">Assegnata a</span> <span class="disable-full-width">Assegnata a</span>
<MudAutocomplete
<MudSelectExtended FullWidth="true" ReadOnly="IsView" T="string?" Variant="Variant.Text" @bind-Value="ActivityModel.UserName" @bind-Value:after="OnUserChanged" Class="customIcon-select" AdornmentIcon="@Icons.Material.Filled.Code"> Disabled="Users.IsNullOrEmpty()"
@foreach (var user in Users) T="StbUser"
{ @bind-Value="SelectedUser"
<MudSelectItemExtended Class="custom-item-select" Value="@user.UserName">@user.FullName</MudSelectItemExtended> @bind-Value:after="OnUserSelectedAfter"
} SearchFunc="SearchUtentiAsync"
</MudSelectExtended> ToStringFunc="@(u => u == null ? string.Empty : u.FullName)"
Clearable="true"
ShowProgressIndicator="true"
DebounceInterval="300"
MaxItems="50"
Class="customIcon-select"
AdornmentIcon="@Icons.Material.Filled.Code"/>
</div> </div>
<div class="divider"></div> <div class="divider"></div>
@@ -213,7 +218,7 @@
<MudMessageBox @ref="AddNamePosition" Class="c-messageBox" Title="Nome della posizione" CancelText="Annulla"> <MudMessageBox @ref="AddNamePosition" Class="c-messageBox" Title="Nome della posizione" CancelText="Annulla">
<MessageContent> <MessageContent>
<MudTextField @bind-Value="NamePosition" Variant="Variant.Outlined" /> <MudTextField @bind-Value="NamePosition" Variant="Variant.Outlined"/>
</MessageContent> </MessageContent>
<YesButton> <YesButton>
<MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary" <MudButton Size="Size.Small" Variant="Variant.Filled" Color="Color.Primary"
@@ -282,6 +287,15 @@
private string? NamePosition { get; set; } private string? NamePosition { get; set; }
private bool CanAddPosition { get; set; } = true; private bool CanAddPosition { get; set; } = true;
// cache per commesse
private string? _lastLoadedCodAnag;
//Commessa
private JtbComt? SelectedComessa { get; set; }
//User
private StbUser? SelectedUser { get; set; }
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter; Snackbar.Configuration.PositionClass = Defaults.Classes.Position.TopCenter;
@@ -291,24 +305,24 @@
ActivityModel = ActivityCopied.Clone(); ActivityModel = ActivityCopied.Clone();
} }
else if (!Id.IsNullOrEmpty()) else if (!Id.IsNullOrEmpty())
{
ActivityModel = (await ManageData.GetActivity(new WhereCondActivity { ActivityId = Id }, true)).Last(); ActivityModel = (await ManageData.GetActivity(new WhereCondActivity { ActivityId = Id }, true)).Last();
}
if (Id.IsNullOrEmpty()) Id = ActivityModel.ActivityId; if (Id.IsNullOrEmpty()) Id = ActivityModel.ActivityId;
IsNew = Id.IsNullOrEmpty(); IsNew = Id.IsNullOrEmpty();
LabelSave = IsNew ? "Aggiungi" : null; LabelSave = IsNew ? "Aggiungi" : null;
_ = LoadData();
await LoadCommesse();
if (IsNew) if (IsNew)
{ {
ActivityModel.EstimatedTime = DateTime.Today.Add(TimeSpan.FromHours(DateTime.Now.Hour)); ActivityModel.EstimatedTime = DateTime.Today.AddHours(DateTime.Now.Hour);
ActivityModel.EstimatedEndtime = DateTime.Today.Add(TimeSpan.FromHours(DateTime.Now.Hour) + TimeSpan.FromHours(1)); ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime?.AddHours(1);
ActivityModel.UserName = UserSession.User.Username; ActivityModel.UserName = UserSession.User.Username;
} }
await LoadActivityType(); _ = LoadData();
await LoadActivityType();
OriginalModel = ActivityModel.Clone(); OriginalModel = ActivityModel.Clone();
} }
@@ -320,67 +334,59 @@
StateHasChanged(); StateHasChanged();
await SavePosition(); await SavePosition();
var response = await IntegryApiService.SaveActivity(ActivityModel); var response = await IntegryApiService.SaveActivity(ActivityModel);
if (response == null) if (response == null)
return; return;
var newActivity = response.Last(); var newActivity = response.Last();
await ManageData.InsertOrUpdate(newActivity); await ManageData.InsertOrUpdate(newActivity);
await SaveAttached(newActivity.ActivityId!); await SaveAttached(newActivity.ActivityId!);
SuccessAnimation = true; SuccessAnimation = true;
StateHasChanged(); StateHasChanged();
await Task.Delay(1250); await Task.Delay(1250);
MudDialog.Close(newActivity); MudDialog.Close(newActivity);
} }
private async Task SavePosition() private async Task SavePosition()
{ {
if (AttachedList != null) if (AttachedList is null) return;
{
foreach (var attached in AttachedList)
{
if (attached.Type != AttachedDTO.TypeAttached.Position) continue;
var positionTasks = AttachedList
.Where(a => a.Type == AttachedDTO.TypeAttached.Position)
.Select(async attached =>
{
var position = new PositionDTO var position = new PositionDTO
{ {
Description = attached.Description, Description = attached.Description,
Lat = attached.Lat, Lat = attached.Lat,
Lng = attached.Lng Lng = attached.Lng
}; };
ActivityModel.Position = await IntegryApiService.SavePosition(position); ActivityModel.Position = await IntegryApiService.SavePosition(position);
} });
}
await Task.WhenAll(positionTasks);
} }
private async Task SaveAttached(string activityId) private async Task SaveAttached(string activityId)
{ {
if (AttachedList != null) if (AttachedList is null) return;
{
foreach (var attached in AttachedList) var uploadTasks = AttachedList
{ .Where(a => a.FileContent is not null && a.Type != AttachedDTO.TypeAttached.Position)
if (attached.FileContent is not null && attached.Type != AttachedDTO.TypeAttached.Position) .Select(a => IntegryApiService.UploadFile(activityId, a.FileBytes, a.Name));
{
await IntegryApiService.UploadFile(activityId, attached.FileBytes, attached.Name); await Task.WhenAll(uploadTasks);
}
}
}
} }
private bool CheckPreSave() private bool CheckPreSave()
{ {
Snackbar.Clear(); Snackbar.Clear();
if (!ActivityModel.ActivityTypeId.IsNullOrEmpty()) return true; if (!ActivityModel.ActivityTypeId.IsNullOrEmpty()) return true;
Snackbar.Add("Tipo attività obbligatorio!", Severity.Error);
Snackbar.Add("Tipo attività obbligatorio!", Severity.Error);
return false; return false;
} }
@@ -389,13 +395,14 @@
return Task.Run(async () => return Task.Run(async () =>
{ {
if (!IsNew && Id != null) if (!IsNew && Id != null)
{
ActivityFileList = await IntegryApiService.GetActivityFile(Id); ActivityFileList = await IntegryApiService.GetActivityFile(Id);
}
Users = await ManageData.GetTable<StbUser>(); Users = await ManageData.GetTable<StbUser>();
if (!ActivityModel.UserName.IsNullOrEmpty())
SelectedUser = Users.FindLast(x => x.UserName.Equals(ActivityModel.UserName));
ActivityResult = await ManageData.GetTable<StbActivityResult>(); ActivityResult = await ManageData.GetTable<StbActivityResult>();
Clienti = await ManageData.GetClienti(new WhereCondContact {FlagStato = "A"}); Clienti = await ManageData.GetClienti(new WhereCondContact { FlagStato = "A" });
Pros = await ManageData.GetProspect(); Pros = await ManageData.GetProspect();
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);
@@ -404,76 +411,137 @@
private async Task LoadActivityType() private async Task LoadActivityType()
{ {
if (ActivityModel.UserName is null) ActivityType = []; if (ActivityModel.UserName is null)
{
ActivityType = [];
return;
}
ActivityType = await ManageData.GetTable<SrlActivityTypeUser>(x => ActivityType = await ManageData.GetTable<SrlActivityTypeUser>(x => x.UserName != null && x.UserName.Equals(ActivityModel.UserName)
x.UserName != null && x.UserName.Equals(ActivityModel.UserName)
); );
} }
private async Task LoadCommesse() => private async Task LoadCommesse()
Commesse = await ManageData.GetTable<JtbComt>(x => x.CodAnag != null && x.CodAnag.Equals(ActivityModel.CodAnag));
private async Task<IEnumerable<string>?> SearchCliente(string value, CancellationToken token)
{ {
if (string.IsNullOrEmpty(value)) if (_lastLoadedCodAnag == ActivityModel.CodAnag) return;
return null;
var listToReturn = new List<string>(); Commesse = await ManageData.GetTable<JtbComt>(x => x.CodAnag == ActivityModel.CodAnag);
Commesse = Commesse.OrderByDescending(x => x.CodJcom).ToList();
listToReturn.AddRange( _lastLoadedCodAnag = ActivityModel.CodAnag;
Clienti.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodAnag} - {x.RagSoc}")
);
listToReturn.AddRange(
Pros.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase)).Select(x => $"{x.CodPpro} - {x.RagSoc}")
);
return listToReturn;
} }
private async Task OnClienteChanged() private async Task<IEnumerable<JtbComt>> SearchCommesseAsync(string value, CancellationToken token)
{
await LoadCommesse();
if (Commesse.IsNullOrEmpty()) return [];
IEnumerable<JtbComt> list;
if (string.IsNullOrWhiteSpace(value))
{
list = Commesse.OrderByDescending(x => x.CodJcom).Take(10);
}
else
{
list = Commesse
.Where(x => (x.CodJcom.Contains(value, StringComparison.OrdinalIgnoreCase)
|| x.Descrizione.Contains(value, StringComparison.OrdinalIgnoreCase))
&& (x.CodAnag == ActivityModel.CodAnag || ActivityModel.CodAnag == null))
.OrderByDescending(x => x.CodJcom)
.Take(50);
}
return token.IsCancellationRequested ? [] : list;
}
private Task<IEnumerable<StbUser>> SearchUtentiAsync(string value, CancellationToken token)
{
IEnumerable<StbUser> list;
if (string.IsNullOrWhiteSpace(value))
{
list = Users.OrderBy(u => u.FullName).Take(50);
}
else
{
list = Users
.Where(x => x.UserName.Contains(value, StringComparison.OrdinalIgnoreCase)
|| x.FullName.Contains(value, StringComparison.OrdinalIgnoreCase))
.OrderBy(u => u.FullName)
.Take(50);
}
return Task.FromResult(token.IsCancellationRequested ? [] : list);
}
private Task<IEnumerable<string>?> SearchCliente(string value, CancellationToken token)
{
if (string.IsNullOrWhiteSpace(value))
return Task.FromResult<IEnumerable<string>?>(null);
var results = new List<string>();
results.AddRange(Clienti
.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase))
.Select(x => $"{x.CodAnag} - {x.RagSoc}"));
results.AddRange(Pros
.Where(x => x.RagSoc.Contains(value, StringComparison.OrdinalIgnoreCase))
.Select(x => $"{x.CodPpro} - {x.RagSoc}"));
return Task.FromResult<IEnumerable<string>?>(results);
}
private Task OnClienteChanged()
{ {
ActivityModel.CodJcom = null; ActivityModel.CodJcom = null;
if (string.IsNullOrWhiteSpace(ActivityModel.Cliente)) return Task.CompletedTask;
if (ActivityModel.Cliente.IsNullOrEmpty()) return; var parts = ActivityModel.Cliente.Split('-', 2, StringSplitOptions.TrimEntries);
if (parts.Length == 2)
{
ActivityModel.CodAnag = parts[0];
ActivityModel.Cliente = parts[1];
}
var match = Regex.Match(ActivityModel.Cliente!, @"^\s*(\S+)\s*-\s*(.*)$"); OnAfterChangeValue();
if (!match.Success) return Task.CompletedTask;
return; }
ActivityModel.CodAnag = match.Groups[1].Value; private async Task OnCommessaSelectedAfter()
ActivityModel.Cliente = match.Groups[2].Value; {
var com = SelectedComessa;
if (com != null)
{
ActivityModel.CodJcom = com.CodJcom;
ActivityModel.Commessa = com.Descrizione;
}
else
{
ActivityModel.CodJcom = null;
ActivityModel.Commessa = null;
}
await LoadCommesse();
OnAfterChangeValue(); OnAfterChangeValue();
} }
private async Task OnCommessaChanged() private async Task OnUserSelectedAfter()
{
ActivityModel.Commessa = (await ManageData.GetTable<JtbComt>(x => x.CodJcom.Equals(ActivityModel.CodJcom))).Last().Descrizione;
OnAfterChangeValue();
}
private async Task OnUserChanged()
{ {
ActivityModel.UserName = SelectedUser?.UserName;
await LoadActivityType(); await LoadActivityType();
OnAfterChangeValue(); OnAfterChangeValue();
} }
private void OnAfterChangeTimeBefore() private void OnAfterChangeTimeBefore()
{ {
if (ActivityModel.EstimatedTime != null) if (ActivityModel.EstimatedTime is not null)
{ {
if (ActivityModel.MinuteBefore != -1) ActivityModel.NotificationDate = ActivityModel.MinuteBefore switch
{ {
ActivityModel.NotificationDate = ActivityModel.MinuteBefore == 0 ? -1 => null,
ActivityModel.EstimatedTime : ActivityModel.EstimatedTime.Value.AddMinutes(ActivityModel.MinuteBefore * -1); 0 => ActivityModel.EstimatedTime,
} _ => ActivityModel.EstimatedTime.Value.AddMinutes(ActivityModel.MinuteBefore * -1)
else };
{
ActivityModel.NotificationDate = null;
}
} }
OnAfterChangeValue(); OnAfterChangeValue();
@@ -482,24 +550,21 @@
private void OnAfterChangeValue() private void OnAfterChangeValue()
{ {
if (!IsNew) if (!IsNew)
{
LabelSave = !OriginalModel.Equals(ActivityModel) ? "Aggiorna" : null; LabelSave = !OriginalModel.Equals(ActivityModel) ? "Aggiorna" : null;
}
if (ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime) if (ActivityModel.EstimatedTime is not null &&
(ActivityModel.EstimatedEndtime is null || ActivityModel.EstimatedEndtime <= ActivityModel.EstimatedTime))
{ {
ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime.Value.AddHours(1); ActivityModel.EstimatedEndtime = ActivityModel.EstimatedTime.Value.AddHours(1);
} }
StateHasChanged();
} }
private async Task OnAfterChangeEsito() private Task OnAfterChangeEsito()
{ {
OnAfterChangeValue(); OnAfterChangeValue();
return Task.CompletedTask;
// var result = await ConfirmMemo.ShowAsync();
// if (result is true)
// OpenAddMemo = !OpenAddMemo;
} }
private void OpenSelectEsito() private void OpenSelectEsito()