diff options
Diffstat (limited to 'Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs')
| -rw-r--r-- | Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs | 303 |
1 files changed, 303 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs b/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs new file mode 100644 index 0000000..40f14ae --- /dev/null +++ b/Codemerx/Gestor.Application/ViewModels/Ferramentas/ProdutoViewModel.cs @@ -0,0 +1,303 @@ +using Gestor.Application.Helpers;
+using Gestor.Application.Servicos.Ferramentas;
+using Gestor.Application.Servicos.Generic;
+using Gestor.Application.ViewModels.Generic;
+using Gestor.Common.Validation;
+using Gestor.Model.Common;
+using Gestor.Model.Domain.Generic;
+using Gestor.Model.Domain.Seguros;
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
+
+namespace Gestor.Application.ViewModels.Ferramentas
+{
+ public class ProdutoViewModel : BaseSegurosViewModel
+ {
+ private readonly ProdutoServico _servico;
+
+ private Produto _selectedProduto;
+
+ public Produto CancelProduto;
+
+ private ObservableCollection<Produto> _produtosFiltrados = new ObservableCollection<Produto>();
+
+ private bool _isExpanded;
+
+ public bool IsExpanded
+ {
+ get
+ {
+ return this._isExpanded;
+ }
+ set
+ {
+ this._isExpanded = value;
+ base.OnPropertyChanged("IsExpanded");
+ }
+ }
+
+ public List<Produto> Produtos
+ {
+ get;
+ set;
+ }
+
+ public ObservableCollection<Produto> ProdutosFiltrados
+ {
+ get
+ {
+ return this._produtosFiltrados;
+ }
+ set
+ {
+ this._produtosFiltrados = value;
+ this.IsExpanded = (value != null ? value.Count > 0 : false);
+ base.OnPropertyChanged("ProdutosFiltrados");
+ }
+ }
+
+ public Produto SelectedProduto
+ {
+ get
+ {
+ return this._selectedProduto;
+ }
+ set
+ {
+ long? nullable;
+ this._selectedProduto = value;
+ this.WorkOnSelectedProduto(value, true);
+ if (value != null)
+ {
+ nullable = new long?(value.get_Id());
+ }
+ else
+ {
+ nullable = null;
+ }
+ base.VerificarEnables(nullable);
+ base.OnPropertyChanged("SelectedProduto");
+ }
+ }
+
+ public ProdutoViewModel()
+ {
+ this._servico = new ProdutoServico();
+ base.EnableMenu = true;
+ this.Seleciona();
+ }
+
+ public void CancelarAlteracao()
+ {
+ if (this.CancelProduto == null || !this.Produtos.Any<Produto>((Produto x) => x.get_Id() == this.CancelProduto.get_Id()))
+ {
+ this.Incluir();
+ }
+ else
+ {
+ DomainBase.Copy<Produto, Produto>(this.Produtos.First<Produto>((Produto x) => x.get_Id() == this.CancelProduto.get_Id()), this.CancelProduto);
+ if (this.ProdutosFiltrados.Count <= 0 || !this.ProdutosFiltrados.Any<Produto>((Produto x) => x.get_Id() == this.CancelProduto.get_Id()))
+ {
+ this.ProdutosFiltrados.Add(this.CancelProduto);
+ }
+ else
+ {
+ DomainBase.Copy<Produto, Produto>(this.ProdutosFiltrados.First<Produto>((Produto x) => x.get_Id() == this.CancelProduto.get_Id()), this.CancelProduto);
+ }
+ this.ProdutosFiltrados = new ObservableCollection<Produto>(this.ProdutosFiltrados);
+ this.SelectedProduto = this.ProdutosFiltrados.First<Produto>((Produto x) => x.get_Id() == this.CancelProduto.get_Id());
+ }
+ base.Alterar(false);
+ }
+
+ internal async Task<List<Produto>> Filtrar(string value)
+ {
+ List<Produto> produtos = await Task.Run<List<Produto>>(() => this.FiltrarProduto(value));
+ return produtos;
+ }
+
+ public List<Produto> FiltrarProduto(string filter)
+ {
+ this.ProdutosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection<Produto>(this.Produtos) : new ObservableCollection<Produto>(
+ from x in this.Produtos
+ where ValidationHelper.RemoveDiacritics(x.get_Nome().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))
+ orderby x.get_Ativo() descending, x.get_Nome()
+ select x));
+ this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault<Produto>();
+ return this.ProdutosFiltrados.ToList<Produto>();
+ }
+
+ public void Incluir()
+ {
+ Produto produto = new Produto();
+ produto.set_Ativo(true);
+ this.SelectedProduto = produto;
+ base.Alterar(true);
+ }
+
+ public async Task<List<KeyValuePair<string, string>>> Salvar()
+ {
+ List<KeyValuePair<string, string>> keyValuePairs;
+ string str;
+ string str1;
+ List<KeyValuePair<string, string>> keyValuePairs1 = this.SelectedProduto.Validate();
+ List<KeyValuePair<string, string>> keyValuePairs2 = keyValuePairs1;
+ keyValuePairs2.AddRange(await this.Validate());
+ keyValuePairs2 = null;
+ if (keyValuePairs1.Count <= 0)
+ {
+ str = (this.SelectedProduto.get_Id() == 0 ? "INCLUIU" : "ALTEROU");
+ str1 = str;
+ Produto produto = await this._servico.Save(this.SelectedProduto);
+ if (this._servico.Sucesso)
+ {
+ base.RegistrarAcao(string.Concat(str1, " PRODUTO \"", produto.get_Nome(), "\""), produto.get_Id(), new TipoTela?(10), string.Format("PRODUTO \"{0}\", ID: {1}", produto.get_Nome(), produto.get_Id()));
+ if (!this.Produtos.Any<Produto>((Produto x) => x.get_Id() == produto.get_Id()))
+ {
+ this.Produtos.Add(produto);
+ }
+ else
+ {
+ DomainBase.Copy<Produto, Produto>(this.Produtos.First<Produto>((Produto x) => x.get_Id() == produto.get_Id()), produto);
+ }
+ if (!this.ProdutosFiltrados.Any<Produto>((Produto x) => x.get_Id() == produto.get_Id()))
+ {
+ this.ProdutosFiltrados.Add(produto);
+ }
+ else
+ {
+ DomainBase.Copy<Produto, Produto>(this.ProdutosFiltrados.First<Produto>((Produto x) => x.get_Id() == produto.get_Id()), produto);
+ }
+ this.ProdutosFiltrados = new ObservableCollection<Produto>(this.ProdutosFiltrados);
+ List<Produto> produtos = this.Produtos;
+ Recursos.Produtos = (
+ from x in produtos
+ orderby x.get_Nome()
+ select x).ToList<Produto>();
+ this.WorkOnSelectedProduto(produto, false);
+ base.Alterar(false);
+ base.ToggleSnackBar("PRODUTO SALVO COM SUCESSO", true);
+ keyValuePairs = null;
+ }
+ else
+ {
+ keyValuePairs = null;
+ }
+ }
+ else
+ {
+ keyValuePairs = keyValuePairs1;
+ }
+ keyValuePairs1 = null;
+ str1 = null;
+ return keyValuePairs;
+ }
+
+ private async void Seleciona()
+ {
+ base.Loading(true);
+ await base.PermissaoTela(10);
+ await this.SelecionaProdutos();
+ base.Loading(false);
+ }
+
+ public void SelecionaProduto(Produto produto)
+ {
+ this.SelectedProduto = produto;
+ }
+
+ private async Task SelecionaProdutos()
+ {
+ base.Loading(true);
+ List<Produto> produtos = await (new BaseServico()).BuscarProdutosAsync();
+ ProdutoViewModel list = this;
+ List<Produto> produtos1 = produtos;
+ IOrderedEnumerable<Produto> ativo =
+ from x in produtos1
+ orderby x.get_Ativo() descending
+ select x;
+ list.Produtos = ativo.ThenBy<Produto, string>((Produto x) => x.get_Nome()).ToList<Produto>();
+ this.ProdutosFiltrados = new ObservableCollection<Produto>(this.Produtos);
+ this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault<Produto>();
+ List<Produto> produtos2 = this.Produtos;
+ Recursos.Produtos = (
+ from x in produtos2
+ orderby x.get_Nome()
+ select x).ToList<Produto>();
+ base.Loading(false);
+ base.Loading(true);
+ }
+
+ public async Task<List<KeyValuePair<string, string>>> Validate()
+ {
+ List<KeyValuePair<string, string>> keyValuePairs;
+ List<KeyValuePair<string, string>> keyValuePairs1;
+ if (!string.IsNullOrWhiteSpace(this.SelectedProduto.get_Nome()))
+ {
+ keyValuePairs1 = new List<KeyValuePair<string, string>>();
+ bool flag = true;
+ List<Produto> produtos = await (new BaseServico()).BuscarProduto(this.SelectedProduto.get_Nome());
+ if (produtos.Count > 0)
+ {
+ produtos.ForEach((Produto x) => {
+ if (x.get_Id() == this.SelectedProduto.get_Id())
+ {
+ return;
+ }
+ if (x.get_Nome() == this.SelectedProduto.get_Nome())
+ {
+ flag = false;
+ }
+ });
+ }
+ if (!flag)
+ {
+ keyValuePairs1.Add(new KeyValuePair<string, string>("Nome", "UM PRODUTO COM O MESMO NOME JÁ EXISTE."));
+ }
+ keyValuePairs = keyValuePairs1;
+ }
+ else
+ {
+ keyValuePairs = new List<KeyValuePair<string, string>>();
+ }
+ keyValuePairs1 = null;
+ return keyValuePairs;
+ }
+
+ private void WorkOnSelectedProduto(Produto value, bool registrar = true)
+ {
+ long? nullable;
+ this.CancelProduto = (value == null || value.get_Id() == 0 ? this.CancelProduto : (Produto)value.Clone());
+ if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 10)
+ {
+ return;
+ }
+ if (registrar)
+ {
+ base.RegistrarAcao(string.Concat("ACESSOU PRODUTO \"", value.get_Nome(), "\""), value.get_Id(), new TipoTela?(10), string.Format("ID PRODUTO: {0}", value.get_Id()));
+ }
+ this.LastAccessId = value.get_Id();
+ this.LastAccessTela = 10;
+ Produto selectedProduto = this.SelectedProduto;
+ if (selectedProduto != null)
+ {
+ nullable = new long?(selectedProduto.get_Id());
+ }
+ else
+ {
+ nullable = null;
+ }
+ long? nullable1 = nullable;
+ long id = value.get_Id();
+ if (nullable1.GetValueOrDefault() != id | !nullable1.HasValue)
+ {
+ this.SelectedProduto = this.ProdutosFiltrados.FirstOrDefault<Produto>((Produto x) => x.get_Id() == value.get_Id());
+ }
+ }
+ }
+}
\ No newline at end of file |