using Gestor.Application.Helpers; using Gestor.Application.Servicos; 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.Common; using Gestor.Model.Domain.Ferramentas; 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.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace Gestor.Application.ViewModels.Ferramentas { public class ReciboViewModel : BaseSegurosViewModel { private readonly ReciboServico _servico; public Recibo CancelRecibo; private Recibo _selectedRecibo = new Recibo(); private DateTime? _horaRecibo; private ObservableCollection _recibosFiltrados = new ObservableCollection(); private bool _isExpanded; private Cliente _pagante; private Cliente _recebedor; public DateTime? HoraRecibo { get { return this._horaRecibo; } set { DateTime? nullable; this._horaRecibo = value; if (!value.HasValue) { return; } Recibo selectedRecibo = this.SelectedRecibo; DateTime? dataRecibo = this.SelectedRecibo.get_DataRecibo(); if (dataRecibo.HasValue) { dataRecibo = this.SelectedRecibo.get_DataRecibo(); nullable = new DateTime?(DateTime.Parse(string.Format("{0:d} {1:T}", dataRecibo.Value, value))); } else { dataRecibo = null; nullable = dataRecibo; } selectedRecibo.set_DataRecibo(nullable); base.OnPropertyChanged("HoraRecibo"); } } public bool IsExpanded { get { return this._isExpanded; } set { this._isExpanded = value; base.OnPropertyChanged("IsExpanded"); } } public Cliente Pagante { get { return this._pagante; } set { this._pagante = value; this.WorkOnSelectedPagante(value); base.OnPropertyChanged("Pagante"); } } public Cliente Recebedor { get { return this._recebedor; } set { this._recebedor = value; this.WorkOnSelectedRecebedor(value); base.OnPropertyChanged("Recebedor"); } } public List Recibos { get; set; } public ObservableCollection RecibosFiltrados { get { return this._recibosFiltrados; } set { this._recibosFiltrados = value; this.IsExpanded = (value != null ? value.Count > 0 : false); base.OnPropertyChanged("RecibosFiltrados"); } } public Recibo SelectedRecibo { get { return this._selectedRecibo; } set { long? nullable; this._selectedRecibo = value; this.WorkOnSelectedRecibo(value, true); if (value != null) { nullable = new long?(value.get_Id()); } else { nullable = null; } base.VerificarEnables(nullable); if (!value.get_DataRecibo().HasValue) { value.set_DataRecibo(new DateTime?(DateTime.Now)); } if (value != null) { this.HoraRecibo = value.get_DataRecibo(); } base.OnPropertyChanged("SelectedRecibo"); } } public ReciboViewModel() { this._servico = new ReciboServico(); base.EnableMenu = true; this.Seleciona(); } public void CancelarAlteracao() { if (this.CancelRecibo == null || !this.Recibos.Any((Recibo x) => x.get_Id() == this.CancelRecibo.get_Id())) { this.Incluir(); } else { DomainBase.Copy(this.Recibos.First((Recibo x) => x.get_Id() == this.CancelRecibo.get_Id()), this.CancelRecibo); if (this.RecibosFiltrados.Count <= 0 || !this.RecibosFiltrados.Any((Recibo x) => x.get_Id() == this.CancelRecibo.get_Id())) { this.RecibosFiltrados.Add(this.CancelRecibo); } else { DomainBase.Copy(this.RecibosFiltrados.First((Recibo x) => x.get_Id() == this.CancelRecibo.get_Id()), this.CancelRecibo); } this.RecibosFiltrados = new ObservableCollection(this.RecibosFiltrados); this.SelectedRecibo = this.RecibosFiltrados.First((Recibo x) => x.get_Id() == this.CancelRecibo.get_Id()); } base.Alterar(false); } public async void Excluir() { string description; object obj; if (this.SelectedRecibo != null && this.SelectedRecibo.get_Id() != 0) { if (await base.ShowMessage("DESEJA REALMENTE EXCLUIR O RECIBO PERMANENTEMENTE?", "SIM", "NÃO", false)) { base.Loading(true); if (await this._servico.Delete(this.SelectedRecibo)) { ReciboViewModel reciboViewModel = this; string str = string.Format("EXCLUIU RECIBO DE NÚMERO \"{0}\"", this.SelectedRecibo.get_Id()); long id = this.SelectedRecibo.get_Id(); TipoTela? nullable = new TipoTela?(42); object[] valor = new object[9]; if (this.SelectedRecibo.get_Tipo().HasValue) { TipoRecibo? tipo = this.SelectedRecibo.get_Tipo(); description = ValidationHelper.GetDescription(tipo.Value); if (description == null) { description = ""; } } else { description = "-"; } valor[0] = description; TipoPagamento? pagamento = this.SelectedRecibo.get_Pagamento(); if (pagamento.HasValue) { obj = ValidationHelper.GetDescription(pagamento.GetValueOrDefault()); } else { obj = null; } valor[1] = obj; valor[2] = this.SelectedRecibo.get_Valor(); valor[3] = this.SelectedRecibo.get_DataRecibo(); valor[4] = this.SelectedRecibo.get_Pagante(); valor[5] = this.SelectedRecibo.get_DocumentoPagante(); valor[6] = this.SelectedRecibo.get_Recebedor(); valor[7] = this.SelectedRecibo.get_DocumentoRecebedor(); valor[8] = this.SelectedRecibo.get_Referente(); reciboViewModel.RegistrarAcao(str, id, nullable, string.Format("TIPO: {0}\nPAGAMENTO: {1}\nVALOR: {2:c}\nDATA RECIBO: {3:G}\nPAGANTE: {4}, {5}\nRECEBEDOR: {6}, {7}\nREFERENTE: \"{8}\"", valor)); await this.SelecionaRecibo(); base.Loading(false); base.ToggleSnackBar("RECIBO EXCLUÍDO COM SUCESSO", true); } else { base.Loading(false); } } } } internal async Task> Filtrar(string value) { List recibos = await Task.Run>(() => this.FiltrarRecibo(value)); return recibos; } public List FiltrarRecibo(string filter) { this.RecibosFiltrados = (string.IsNullOrWhiteSpace(filter) ? new ObservableCollection(this.Recibos) : new ObservableCollection(this.Recibos.Where((Recibo x) => { if (ValidationHelper.RemoveDiacritics(x.get_Pagante().Trim()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter))) { return true; } return ValidationHelper.RemoveDiacritics(x.get_Recebedor()).ToUpper().Contains(ValidationHelper.RemoveDiacritics(filter)); }))); return this.RecibosFiltrados.ToList(); } private static string GetImage(byte[] bytes, string extensao) { string str; try { Image image = Image.FromStream(new MemoryStream(bytes)); string str1 = (extensao.ToUpper().Contains("JPG") ? string.Concat("data:image/jpeg;base64,", Convert.ToBase64String(bytes, Base64FormattingOptions.None)) : string.Concat("data:image/png;base64,", Convert.ToBase64String(bytes, Base64FormattingOptions.None))); string[] strArrays = new string[] { ""; str = string.Concat(strArrays); } catch (Exception exception) { str = ""; } return str; } public void Incluir() { this.SelectedRecibo = new Recibo(); base.Alterar(true); } private bool IsImage(string extensao) { if (extensao == null) { return false; } if (extensao.ToLower().Contains("jpg")) { return true; } if (extensao.ToLower().Contains("jpeg")) { return true; } if (extensao.ToLower().Contains("png")) { return true; } return false; } public async void Print() { bool referente; byte[] arquivo; string str; string str1; string str2; string upper; string str3; string str4; string upper1; string description; object obj; string extensao; string extensao1; long? nullable; string bd; Gestor.Model.Domain.Common.ArquivoDigital arquivoDigital; string image; string str5; Recibo selectedRecibo = this.SelectedRecibo; if (selectedRecibo != null) { referente = selectedRecibo.get_Referente(); } else { referente = false; } if (referente && this.SelectedRecibo.get_Recebedor() != null) { arquivoDigital = null; List indiceArquivoDigitals = await this.ArquivoDigitalServico.BuscarPorTipo(13, Recursos.Empresa.get_Id()); if (indiceArquivoDigitals != null) { List indiceArquivoDigitals1 = indiceArquivoDigitals; if (indiceArquivoDigitals1.Any((IndiceArquivoDigital x) => x.get_Descricao() == "LOGO")) { List indiceArquivoDigitals2 = indiceArquivoDigitals; IndiceArquivoDigital indiceArquivoDigital = indiceArquivoDigitals2.FirstOrDefault((IndiceArquivoDigital x) => x.get_Descricao() == "LOGO"); if (indiceArquivoDigital != null) { nullable = new long?(indiceArquivoDigital.get_IdArquivoDigital()); } else { nullable = null; } long? nullable1 = nullable; List indiceArquivoDigitals3 = indiceArquivoDigitals; IndiceArquivoDigital indiceArquivoDigital1 = indiceArquivoDigitals3.FirstOrDefault((IndiceArquivoDigital x) => x.get_Descricao() == "LOGO"); if (indiceArquivoDigital1 != null) { bd = indiceArquivoDigital1.get_Bd(); } else { bd = null; } string str6 = bd; if (nullable1.HasValue) { arquivoDigital = await this.ArquivoDigitalServico.BuscarPorId(nullable1.Value, str6); } } } Gestor.Model.Domain.Common.ArquivoDigital arquivoDigital1 = arquivoDigital; if (arquivoDigital1 != null) { arquivo = arquivoDigital1.get_Arquivo(); } else { arquivo = null; } byte[] numArray = arquivo; image = ""; if (numArray != null && numArray.Length != 0) { ReciboViewModel reciboViewModel = this; Gestor.Model.Domain.Common.ArquivoDigital arquivoDigital2 = arquivoDigital; if (arquivoDigital2 != null) { extensao = arquivoDigital2.get_Extensao(); } else { extensao = null; } if (reciboViewModel.IsImage(extensao)) { byte[] numArray1 = numArray; Gestor.Model.Domain.Common.ArquivoDigital arquivoDigital3 = arquivoDigital; if (arquivoDigital3 != null) { extensao1 = arquivoDigital3.get_Extensao(); } else { extensao1 = null; } image = ReciboViewModel.GetImage(numArray1, extensao1); } } string longDatePattern = (new CultureInfo("pt-PT", false)).DateTimeFormat.LongDatePattern; DateTime? dataRecibo = this.SelectedRecibo.get_DataRecibo(); if (dataRecibo.HasValue) { DateTime valueOrDefault = dataRecibo.GetValueOrDefault(); str = valueOrDefault.ToString(longDatePattern, new CultureInfo("pt-PT")); } else { str = null; } str5 = str; bool flag = await base.ShowMessage("DESEJA IMPRIMIR DUAS VIAS DO RECIBO?", "SIM", "NÃO", false); int num = (!flag ? 1 : 2); flag = !string.IsNullOrEmpty(image); if (flag) { flag = await base.ShowMessage("DESEJA IMPRIMIR O LOGO DA CORRETORA NO RECIBO?\nPARA ADICIONAR UM LOGO, É NECESSÁRIO COLOCÁ-LO NO ARQUIVO\nDIGITAL NA TELA DE EMPRESA E FILIAIS COM DESCRIÇÃO \"LOGO\"", "SIM", "NÃO", false); } bool flag1 = flag; string str7 = "RECIBO"; for (int i = 0; i < num; i++) { if (!flag1) { str7 = string.Concat(str7, "
"); } else { str7 = string.Concat(str7, "
", image, "
"); str7 = string.Concat(str7, "
"); } str7 = string.Concat(str7, string.Format("
N° {0}

", this.SelectedRecibo.get_Id())); str7 = string.Concat(str7, "R E C I B O

"); if (this.SelectedRecibo.get_Tipo().GetValueOrDefault() != 1) { str1 = (this.SelectedRecibo.get_DocumentoPagante() == null || this.SelectedRecibo.get_DocumentoPagante().Clear().Length < 12 ? "EU" : "A"); string str8 = str1; str2 = (this.SelectedRecibo.get_DocumentoPagante() == null || this.SelectedRecibo.get_DocumentoPagante().Clear().Length < 12 ? "CPF" : "CNPJ"); string str9 = str2; string str10 = (str8 == "EU" ? "O" : "A"); string str11 = (str8 == "EU" ? "PAGUEI" : "PAGOU"); object[] pagante = new object[] { str8, this.SelectedRecibo.get_Pagante(), str10, str9, this.SelectedRecibo.get_DocumentoPagante(), str10, str11, this.SelectedRecibo.get_DataRecibo(), this.SelectedRecibo.get_Valor(), ValidationHelper.GetDescription(this.SelectedRecibo.get_Pagamento()), this.SelectedRecibo.get_Recebedor(), this.SelectedRecibo.get_DocumentoRecebedor(), this.SelectedRecibo.get_Referente().ToUpper() }; str7 = string.Concat(str7, string.Format("PELO PRESENTE, {0}, {1}, INSCRIT{2} NO {3} SOB Nº {4}, DECLAR{5} QUE {6}, NA DATA DE HOJE, {7:dd/MM/yyyy HH:mm}, O VALOR DE R$ {8:0.00} POR MEIO DE {9}, PARA {10}, INSCRITO NO CPF/CNPJ SOB Nº {11}, REFERENTE À(AO) {12}. DANDO-LHE POR ESTE RECIBO A DEVIDA QUITAÇÃO.
", pagante)); string[] cidade = new string[] { str7, "
", Recursos.Empresa.get_Cidade(), ", ", null, null }; string str12 = str5; if (str12 != null) { upper = str12.ToUpper(); } else { upper = null; } cidade[4] = upper; cidade[5] = ".

"; str7 = string.Concat(cidade); string[] recebedor = new string[] { str7, "_________________________________
ASSINATURA ", this.SelectedRecibo.get_Recebedor(), "
", this.SelectedRecibo.get_DocumentoRecebedor() }; str7 = string.Concat(recebedor); } else { str3 = (this.SelectedRecibo.get_DocumentoRecebedor() == null || this.SelectedRecibo.get_DocumentoRecebedor().Clear().Length < 12 ? "EU" : "A"); string str13 = str3; str4 = (this.SelectedRecibo.get_DocumentoRecebedor() == null || this.SelectedRecibo.get_DocumentoRecebedor().Clear().Length < 12 ? "CPF" : "CNPJ"); string str14 = str4; string str15 = (str13 == "EU" ? "O" : "A"); string str16 = (str13 == "EU" ? "RECEBI" : "RECEBEU"); object[] objArray = new object[] { str13, this.SelectedRecibo.get_Recebedor(), str15, str14, this.SelectedRecibo.get_DocumentoRecebedor(), str15, str16, this.SelectedRecibo.get_DataRecibo(), this.SelectedRecibo.get_Valor(), ValidationHelper.GetDescription(this.SelectedRecibo.get_Pagamento()), this.SelectedRecibo.get_Pagante(), this.SelectedRecibo.get_DocumentoPagante(), this.SelectedRecibo.get_Referente().ToUpper() }; str7 = string.Concat(str7, string.Format("PELO PRESENTE, {0}, {1}, INSCRIT{2} NO {3} SOB Nº {4}, DECLAR{5} QUE {6}, NA DATA DE HOJE, {7:dd/MM/yyyy HH:mm}, O VALOR DE R$ {8:0.00} POR MEIO DE {9}, DE {10}, INSCRITO NO CPF/CNPJ SOB Nº {11}, REFERENTE À(AO) {12}. DANDO-LHE POR ESTE RECIBO A DEVIDA QUITAÇÃO.
", objArray)); string[] strArrays = new string[] { str7, "", Recursos.Empresa.get_Cidade(), ", ", null, null }; string str17 = str5; if (str17 != null) { upper1 = str17.ToUpper(); } else { upper1 = null; } strArrays[4] = upper1; strArrays[5] = ".

"; str7 = string.Concat(strArrays); string[] recebedor1 = new string[] { str7, "_________________________________
ASSINATURA ", this.SelectedRecibo.get_Recebedor(), "
", this.SelectedRecibo.get_DocumentoRecebedor() }; str7 = string.Concat(recebedor1); } str7 = string.Concat(str7, "
"); } str7 = string.Concat(str7, " "); string tempPath = Path.GetTempPath(); string str18 = string.Format("{0}_{1:ddMMyyyyhhmmss}.html", tempPath, Funcoes.GetNetworkTime()); StreamWriter streamWriter = new StreamWriter(str18, true, Encoding.UTF8); streamWriter.Write(str7); streamWriter.Close(); Process.Start(str18); ReciboViewModel reciboViewModel1 = this; string str19 = string.Format("IMPRIMIU O RECIBO DE NÚMERO \"{0}\"", this.SelectedRecibo.get_Id()); long id = this.SelectedRecibo.get_Id(); TipoTela? nullable2 = new TipoTela?(42); object[] valor = new object[9]; if (this.SelectedRecibo.get_Tipo().HasValue) { TipoRecibo? tipo = this.SelectedRecibo.get_Tipo(); description = ValidationHelper.GetDescription(tipo.Value); if (description == null) { description = ""; } } else { description = "-"; } valor[0] = description; TipoPagamento? pagamento = this.SelectedRecibo.get_Pagamento(); if (pagamento.HasValue) { obj = ValidationHelper.GetDescription(pagamento.GetValueOrDefault()); } else { obj = null; } valor[1] = obj; valor[2] = this.SelectedRecibo.get_Valor(); valor[3] = this.SelectedRecibo.get_DataRecibo(); valor[4] = this.SelectedRecibo.get_Pagante(); valor[5] = this.SelectedRecibo.get_DocumentoPagante(); valor[6] = this.SelectedRecibo.get_Recebedor(); valor[7] = this.SelectedRecibo.get_DocumentoRecebedor(); valor[8] = this.SelectedRecibo.get_Referente(); reciboViewModel1.RegistrarAcao(str19, id, nullable2, string.Format("TIPO: {0}\nPAGAMENTO: {1}\nVALOR: {2:c}\nDATA RECIBO: {3:G}\nPAGANTE: {4}, {5}\nRECEBEDOR: {6}, {7}\nREFERENTE: \"{8}\"", valor)); } arquivoDigital = null; image = null; str5 = null; } public async Task>> Salvar() { List> keyValuePairs; string str; string description; object obj; string str1; List> keyValuePairs1 = this.SelectedRecibo.Validate(); if (keyValuePairs1.Count <= 0) { str = (this.SelectedRecibo.get_Id() == 0 ? "INCLUIU" : "ALTEROU"); str1 = str; Recibo recibo = await this._servico.Save(this.SelectedRecibo); if (this._servico.Sucesso) { ReciboViewModel reciboViewModel = this; string str2 = string.Format("{0} RECIBO DE NÚMERO \"{1}\"", str1, recibo.get_Id()); long id = recibo.get_Id(); TipoTela? nullable = new TipoTela?(42); object[] valor = new object[9]; if (recibo.get_Tipo().HasValue) { TipoRecibo? tipo = recibo.get_Tipo(); description = ValidationHelper.GetDescription(tipo.Value); if (description == null) { description = ""; } } else { description = "-"; } valor[0] = description; TipoPagamento? pagamento = recibo.get_Pagamento(); if (pagamento.HasValue) { obj = ValidationHelper.GetDescription(pagamento.GetValueOrDefault()); } else { obj = null; } valor[1] = obj; valor[2] = recibo.get_Valor(); valor[3] = recibo.get_DataRecibo(); valor[4] = recibo.get_Pagante(); valor[5] = recibo.get_DocumentoPagante(); valor[6] = recibo.get_Recebedor(); valor[7] = recibo.get_DocumentoRecebedor(); valor[8] = recibo.get_Referente(); reciboViewModel.RegistrarAcao(str2, id, nullable, string.Format("TIPO: {0}\nPAGAMENTO: {1}\nVALOR: {2:c}\nDATA RECIBO: {3:G}\nPAGANTE: {4}, {5}\nRECEBEDOR: {6}, {7}\nREFERENTE: \"{8}\"", valor)); if (!this.Recibos.Any((Recibo x) => x.get_Id() == recibo.get_Id())) { this.Recibos.Add(recibo); } else { DomainBase.Copy(this.Recibos.First((Recibo x) => x.get_Id() == recibo.get_Id()), recibo); } if (!this.RecibosFiltrados.Any((Recibo x) => x.get_Id() == recibo.get_Id())) { this.RecibosFiltrados.Add(recibo); } else { DomainBase.Copy(this.RecibosFiltrados.First((Recibo x) => x.get_Id() == recibo.get_Id()), recibo); } this.RecibosFiltrados = new ObservableCollection(this.RecibosFiltrados); this.WorkOnSelectedRecibo(recibo, false); base.Alterar(false); base.ToggleSnackBar("RECIBO SALVO COM SUCESSO", true); keyValuePairs = null; } else { keyValuePairs = null; } } else { keyValuePairs = keyValuePairs1; } str1 = null; return keyValuePairs; } private async void Seleciona() { base.Loading(true); await base.PermissaoTela(42); await this.SelecionaRecibo(); base.Loading(false); } private async Task SelecionaRecibo() { base.Loading(true); List recibos = await this._servico.BuscarRecibos(); this.Recibos = recibos.ToList(); this.RecibosFiltrados = new ObservableCollection(this.Recibos); if (this.RecibosFiltrados.Count <= 0) { this.SelectedRecibo = new Recibo(); base.Alterar(false); base.EnableMenu = false; } else { this.SelectedRecibo = this.RecibosFiltrados.First(); } base.Loading(false); } private void WorkOnSelectedPagante(Cliente value) { if (value != null) { this.SelectedRecibo.set_Pagante(Regex.Replace(value.get_NomeSocial(), "\\-\\s\\d.*", "").Trim()); this.SelectedRecibo.set_DocumentoPagante(value.get_Documento()); this.SelectedRecibo = this.SelectedRecibo; } } private void WorkOnSelectedRecebedor(Cliente value) { if (value != null) { this.SelectedRecibo.set_Recebedor(Regex.Replace(value.get_NomeSocial(), "\\-\\s\\d.*", "").Trim()); this.SelectedRecibo.set_DocumentoRecebedor(value.get_Documento()); this.SelectedRecibo = this.SelectedRecibo; } } private void WorkOnSelectedRecibo(Recibo value, bool registrar = true) { long? nullable; string description; object obj; this.CancelRecibo = (value == null || value.get_Id() == 0 ? this.CancelRecibo : (Recibo)value.Clone()); if (value == null || value.get_Id() == 0 || this.LastAccessId == value.get_Id() && this.LastAccessTela == 42) { return; } if (registrar) { string str = string.Format("ACESSOU RECIBO DE NÚMERO \"{0}\"", value.get_Id()); long id = value.get_Id(); TipoTela? nullable1 = new TipoTela?(42); object[] valor = new object[9]; if (value.get_Tipo().HasValue) { TipoRecibo? tipo = value.get_Tipo(); description = ValidationHelper.GetDescription(tipo.Value) ?? ""; } else { description = "-"; } valor[0] = description; TipoPagamento? pagamento = value.get_Pagamento(); if (pagamento.HasValue) { obj = ValidationHelper.GetDescription(pagamento.GetValueOrDefault()); } else { obj = null; } valor[1] = obj; valor[2] = value.get_Valor(); valor[3] = value.get_DataRecibo(); valor[4] = value.get_Pagante(); valor[5] = value.get_DocumentoPagante(); valor[6] = value.get_Recebedor(); valor[7] = value.get_DocumentoRecebedor(); valor[8] = value.get_Referente(); base.RegistrarAcao(str, id, nullable1, string.Format("TIPO: {0}\nPAGAMENTO: {1}\nVALOR: {2:c}\nDATA RECIBO: {3:G}\nPAGANTE: {4}, {5}\nRECEBEDOR: {6}, {7}\nREFERENTE: \"{8}\"", valor)); } this.LastAccessId = value.get_Id(); this.LastAccessTela = 42; Recibo selectedRecibo = this.SelectedRecibo; if (selectedRecibo != null) { nullable = new long?(selectedRecibo.get_Id()); } else { nullable = null; } long? nullable2 = nullable; long num = value.get_Id(); if (nullable2.GetValueOrDefault() != num | !nullable2.HasValue) { this.SelectedRecibo = this.RecibosFiltrados.FirstOrDefault((Recibo x) => x.get_Id() == value.get_Id()); } } } }