Example #1
0
 public ExpensesViewModel()
 {
     instance         = this;
     this.apiService  = new ApiService();
     this.dataService = new DataService();
     LoadExpenses();
     //this.IsRefreshing = false;
 }
Example #2
0
 private static ExpensesViewModel instance; // Atributo
 public static ExpensesViewModel GetInstance()
 {
     if (instance == null)
     {
         instance = new ExpensesViewModel();
     }
     return(instance);
 }
Example #3
0
        private async void Delete()
        {
            var answer = await Application.Current.MainPage.DisplayAlert(
                Languages.Confirm,
                Languages.DeleteConfirmation,
                Languages.Yes,
                Languages.No);

            if (!answer)
            {
                return;
            }

            this.IsRunning = true;
            this.isEnabled = false;

            var connection = await this.apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                this.IsRunning = false;
                this.isEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    connection.Message,
                    Languages.Accept);

                return;
            }

            var url        = Application.Current.Resources["UrlAPI"].ToString();                // Obtengo la url del diccionario de recursos.
            var prefix     = Application.Current.Resources["UrlPrefix"].ToString();             // Obtengo el prefijo del diccionario de recursos.
            var controller = Application.Current.Resources["UrlExpensesController"].ToString(); // Obtengo el controlador del diccionario de recursos.

            var response = await this.apiService.Delete(url, prefix, controller, this.Expense.ExpenseId, Settings.TokenType, Settings.AccessToken);

            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.isEnabled = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Message, Languages.Accept);

                return;
            }

            var expensesViewModel = ExpensesViewModel.GetInstance();
            var deletedExpense    = expensesViewModel.MyExpenses.Where(p => p.ExpenseId == this.Expense.ExpenseId).FirstOrDefault(); // LinQ

            if (deletedExpense != null)
            {
                expensesViewModel.MyExpenses.Remove(deletedExpense); // con esto me lo debe refrescar automaticamente en la lista
            }
            expensesViewModel.RefreshList();
            this.IsRunning = false;
            this.isEnabled = true;
            await App.Navigator.PopAsync();
        }
Example #4
0
 public ExpensesViewModel(Request request)
 {
     instance           = this;
     this.apiService    = new ApiService();
     this.dataService   = new DataService();
     this.Request       = request;
     RequestDescription = request.Description;
     LoadExpenses();
     //this.IsRefreshing = false;
 }
Example #5
0
        private async void Save()
        {
            if (this.ExpenseDate > this.Request.ExpenseDateEnd ||
                this.ExpenseDate < this.Request.ExpenseDateStart)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.DateRangeError,
                    Languages.Accept);

                return;
            }

            if (string.IsNullOrEmpty(this.DocumentNumber))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.DocumentNumberError,
                    Languages.Accept);

                return;
            }

            if (this.Amount <= 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountInvalid,
                    Languages.Accept);

                return;
            }

            if (this.AmountIVA > this.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountIvaInvalid,
                    Languages.Accept);

                return;
            }

            if (this.AmountPercepcion > this.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountPercepcionInvalid,
                    Languages.Accept);

                return;
            }

            if (this.AmountIVA + this.AmountPercepcion > this.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountPercepcionInvalid,
                    Languages.Accept);

                return;
            }

            this.IsRunning = true;  // Esto muestra el activity indicator
            this.IsEnabled = false; // Desabilito el botón de SAVE para que el usuario no le pegue varias veces.

            var connection = await this.apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    connection.Message,
                    Languages.Accept);

                return;
            }


            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelper.ReadFully(this.file.GetStream());
            }

            var expense = new Expense();

            expense.ExpenseDate = this.ExpenseDate;
            expense.CurrencyId  = this.CurrencySelected.CurrencyId;
            //expense.Currency = this.CurrencySelected;
            expense.DocumentTypeId = this.DocumentTypeSelected.DocumentTypeId;
            //expense.DocumentType = this.DocumentTypeSelected;
            expense.ExpenseTypeId = this.ExpenseTypeSelected.ExpenseTypeId;
            //expense.ExpenseType = this.ExpenseTypeSelected;
            expense.PaymentTypeId = this.PaymentTypeSelected.PaymentTypeId;
            //expense.PaymentType = this.PaymentTypeSelected;
            expense.VendorId = this.VendorSelected.VendorId;
            //expense.Vendor = this.VendorSelected;
            expense.RequestId = this.Request.RequestId;
            //expense.Request = this.Request;
            expense.Amount           = this.Amount;
            expense.AmountIVA        = this.AmountIVA;
            expense.AmountPercepcion = this.AmountPercepcion;
            expense.TotalAmount      = expense.Amount + expense.AmountIVA + expense.AmountPercepcion;
            expense.DocumentNumber   = this.DocumentNumber;
            expense.Comments         = this.Comments;
            expense.ImageArray       = imageArray;

            var url        = Application.Current.Resources["UrlAPI"].ToString();
            var prefix     = Application.Current.Resources["UrlPrefix"].ToString();
            var controller = Application.Current.Resources["UrlExpensesController"].ToString();
            var response   = await this.apiService.Post(url, prefix, controller, expense, Settings.TokenType, Settings.AccessToken);

            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    response.Message,
                    Languages.Accept);

                return;
            }

            var newExpense        = (Expense)response.Result;
            var expensesViewModel = ExpensesViewModel.GetInstance();

            // Le agrego las clases
            newExpense.Currency     = this.CurrencySelected;
            newExpense.DocumentType = this.DocumentTypeSelected;
            newExpense.ExpenseType  = this.ExpenseTypeSelected;
            newExpense.PaymentType  = this.PaymentTypeSelected;
            newExpense.Vendor       = this.VendorSelected;
            newExpense.Request      = this.Request;

            expensesViewModel.MyExpenses.Add(newExpense);
            expensesViewModel.RefreshList();

            this.IsRunning = false;
            this.IsEnabled = true;

            /*
             * await Application.Current.MainPage.DisplayAlert(
             *  Languages.Atention,
             *  Languages.DataSaved,
             *  Languages.Accept);
             */

            await App.Navigator.PopAsync();
        }
Example #6
0
        private async void Save()
        {
            if (Expense.ExpenseDate > RequestSelected.ExpenseDateEnd ||
                Expense.ExpenseDate < RequestSelected.ExpenseDateStart)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.DateRangeError,
                    Languages.Accept);

                return;
            }

            if (string.IsNullOrEmpty(this.Expense.DocumentNumber))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.DocumentNumberError,
                    Languages.Accept);

                return;
            }

            if (Expense.Amount <= 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountInvalid,
                    Languages.Accept);

                return;
            }

            if (Expense.AmountIVA > Expense.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountIvaInvalid,
                    Languages.Accept);

                return;
            }

            if (Expense.AmountPercepcion > Expense.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountPercepcionInvalid,
                    Languages.Accept);

                return;
            }

            if (Expense.AmountIVA + Expense.AmountPercepcion > Expense.Amount)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.AmountPercepcionInvalid,
                    Languages.Accept);

                return;
            }

            this.IsRunning = true;  // Esto muestra el activity indicator
            this.IsEnabled = false; // Desabilito el botón de SAVE para que el usuario no le pegue varias veces.

            var connection = await this.apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    connection.Message,
                    Languages.Accept);

                return;
            }

            this.expense.CurrencyId     = this.CurrencySelected.CurrencyId;
            this.expense.Currency       = this.CurrencySelected;
            this.expense.DocumentTypeId = this.DocumentTypeSelected.DocumentTypeId;
            this.expense.DocumentType   = this.DocumentTypeSelected;
            this.expense.ExpenseTypeId  = this.ExpenseTypeSelected.ExpenseTypeId;
            this.expense.ExpenseType    = this.ExpenseTypeSelected;
            this.expense.PaymentTypeId  = this.PaymentTypeSelected.PaymentTypeId;
            this.expense.PaymentType    = this.PaymentTypeSelected;
            this.expense.VendorId       = this.VendorSelected.VendorId;
            this.expense.Vendor         = this.VendorSelected;
            this.expense.RequestId      = this.RequestSelected.RequestId;
            this.expense.Request        = this.RequestSelected;
            this.expense.TotalAmount    = this.expense.Amount + this.expense.AmountIVA + this.expense.AmountPercepcion;

            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelper.ReadFully(this.file.GetStream());
                this.expense.ImageArray = imageArray;
            }

            var url        = Application.Current.Resources["UrlAPI"].ToString();
            var prefix     = Application.Current.Resources["UrlPrefix"].ToString();
            var controller = Application.Current.Resources["UrlExpensesController"].ToString();
            var response   = await this.apiService.Put(url, prefix, controller, this.expense, this.Expense.ExpenseId, Settings.TokenType, Settings.AccessToken);

            //var response = await this.apiService.Put(url, prefix, controller, this.expense, this.Expense.ExpenseId);

            if (!response.IsSuccess)
            {
                this.IsRunning = false;
                this.IsEnabled = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    response.Message,
                    Languages.Accept);

                return;
            }

            /*
             *   Refrescamos la ListView eliminado y agregando el producto.
             *   (Es la unica manera en que refresca. Bug de xamarin)
             */

            var newExpense        = (Expense)response.Result;
            var expensesViewModel = ExpensesViewModel.GetInstance();
            var oldExpense        = expensesViewModel.MyExpenses.Where(p => p.ExpenseId == this.Expense.ExpenseId).FirstOrDefault();

            if (oldExpense != null)
            {
                expensesViewModel.MyExpenses.Remove(oldExpense);
            }

            expensesViewModel.MyExpenses.Add(newExpense);
            expensesViewModel.RefreshList();


            this.IsRunning = false;
            this.IsEnabled = true;

            await Application.Current.MainPage.DisplayAlert(
                Languages.Atention,
                Languages.DataSaved,
                Languages.Accept);

            await App.Navigator.PopAsync();
        }