public async Task <IActionResult> Create([Bind("PayExpenseId,ProductId,ExpenseId,Description,Amount,Tax,PayExpenseDate")] PayExpense payExpense)
        {
            if (ModelState.IsValid)
            {
                payExpense.Product = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payExpense.ProductId);

                payExpense.Expense = await _context.Expense.SingleOrDefaultAsync(e => e.ExpenseId == payExpense.ExpenseId);

                if (payExpense.Product.Balance < (payExpense.Amount + payExpense.Tax))
                {
                    CreateInitial(payExpense.ProductId, payExpense.ExpenseId);
                    ModelState.AddModelError("", "Balance de Producto origen insuficiente.");
                    return(View(payExpense));
                }

                FunctionsConvert functionsConvert = new FunctionsConvert(_context);
                var _payExpense = functionsConvert.ConvertCurrency(payExpense, 0, 0);

                //restar el balance del producto origen
                payExpense.Product.Balance = payExpense.Product.Balance - _payExpense.Amount - _payExpense.Tax;

                payExpense.StatusPayExpense = Models.Enum.StatusCashFlow.Activo;
                payExpense.CreatedDate      = DateTime.Today;
                _context.Add(payExpense);
                //_context.Update(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(payExpense.ProductId, payExpense.ExpenseId);
            return(View(payExpense));
        }
        public async Task <IActionResult> Edit(int id, [Bind("PayProductId,ProductIdFrom,ProductIdTo,Description,Amount,Tax,PayProductDate,CreatedDate,StatusPayProduct")] PayProduct payProduct)
        {
            if (id != payProduct.PayProductId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    PayProduct payProductOld = await _context.PayProduct.SingleOrDefaultAsync(p => p.PayProductId == payProduct.PayProductId);

                    if (payProduct.Amount != payProductOld.Amount)
                    {
                        FunctionsConvert functionsConvert = new FunctionsConvert(_context);

                        var _amount = payProduct.Amount - payProductOld.Amount;
                        var _tax    = payProduct.Tax - payProductOld.Tax;

                        payProduct.ProductFrom = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdFrom);

                        payProduct.ProductTo = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdTo);

                        var _payProduct = functionsConvert.ConvertCurrency(payProduct, _amount, _tax);

                        if (payProduct.ProductFrom.Balance < (_payProduct.Amount + _payProduct.Tax))
                        {
                            CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo);
                            ModelState.AddModelError("", "Balance de Producto origen insuficiente.");
                            return(View(payProduct));
                        }

                        //restar el balance del producto origen
                        payProduct.ProductFrom.Balance = payProduct.ProductFrom.Balance - _payProduct.Amount - _payProduct.Tax;

                        //sumar al balance del prestamo destino
                        payProduct.ProductTo.Balance = payProduct.ProductTo.Balance - _amount;
                    }
                    _context.Entry(payProductOld).State = EntityState.Detached; //detach old to update the new
                    _context.Update(payProduct);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PayProductExists(payProduct.PayProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo);
            return(View(payProduct));
        }
        public async Task <IActionResult> Edit(int id, [Bind("PayExpenseId,ProductId,ExpenseId,Description,Amount,Tax,PayExpenseDate,CreatedDate,StatusPayExpense")] PayExpense payExpense)
        {
            if (id != payExpense.PayExpenseId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    PayExpense payExpenseOld = await _context.PayExpense.SingleOrDefaultAsync(c => c.PayExpenseId == payExpense.PayExpenseId);

                    if (payExpense.Amount != payExpenseOld.Amount)
                    {
                        FunctionsConvert functionsConvert = new FunctionsConvert(_context);

                        var _amount = payExpense.Amount - payExpenseOld.Amount;
                        var _tax    = payExpense.Tax - payExpenseOld.Tax;

                        payExpense.Product = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payExpense.ProductId);

                        payExpense.Expense = await _context.Expense.SingleOrDefaultAsync(e => e.ExpenseId == payExpense.ExpenseId);

                        var _payExpense = functionsConvert.ConvertCurrency(payExpense, _amount, _tax);

                        if (payExpense.Product.Balance < (_payExpense.Amount + _payExpense.Tax))
                        {
                            CreateInitial(payExpense.ProductId, payExpense.ExpenseId);
                            ModelState.AddModelError("", "Balance de Producto origen insuficiente.");
                            return(View(payExpense));
                        }

                        payExpense.Product.Balance = payExpense.Product.Balance - _payExpense.Amount - _payExpense.Tax;
                        //_context.Update(product);
                    }

                    _context.Entry(payExpenseOld).State = EntityState.Detached; //detach old to update the new
                    _context.Update(payExpense);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PayExpenseExists(payExpense.PayExpenseId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(payExpense.ProductId, payExpense.ExpenseId);
            return(View(payExpense));
        }
Exemple #4
0
        public async Task <IActionResult> Create([Bind("PayProductId,ProductIdFrom,ProductIdTo,CreditCardCutId,Description,Amount,Tax,PayProductDate")] PayProduct payProduct)
        {
            if (ModelState.IsValid)
            {
                payProduct.ProductFrom = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdFrom);

                payProduct.ProductTo = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdTo);

                payProduct.CreditCardCut = await _context.CreditCardCut.SingleOrDefaultAsync(p => p.CreditCardCutId == payProduct.CreditCardCutId);

                if (payProduct.ProductFrom.Balance < (payProduct.Amount + payProduct.Tax))
                {
                    CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo, payProduct.CreditCardCutId);
                    ModelState.AddModelError("", "Balance de Producto origen insuficiente.");
                    return(View(payProduct));
                }
                FunctionsConvert functionsConvert = new FunctionsConvert(_context);
                var _payProduct = functionsConvert.ConvertCurrency(payProduct, 0, 0);

                //restar el balance del producto origen
                payProduct.ProductFrom.Balance = payProduct.ProductFrom.Balance - payProduct.Amount - payProduct.Tax;

                //sumar al balance a la tc destino
                payProduct.ProductTo.Balance = payProduct.ProductTo.Balance + payProduct.Amount;

                //actualizar corte de tarjeta
                payProduct.CreditCardCut.AmountPayment = payProduct.CreditCardCut.AmountPayment + payProduct.Amount;
                payProduct.CreditCardCut.AmountPending = payProduct.CreditCardCut.AmountPending - payProduct.Amount;

                payProduct.CreatedDate      = DateTime.Today;
                payProduct.StatusPayProduct = Models.Enum.StatusPayment.Activo;
                _context.Add(payProduct);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo, payProduct.CreditCardCutId);
            return(View(payProduct));
        }
        public async Task <IActionResult> Create([Bind("PayProductId,ProductIdFrom,ProductIdTo,Description,Amount,Tax,PayProductDate")] PayProduct payProduct)
        {
            if (ModelState.IsValid)
            {
                payProduct.ProductFrom = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdFrom);

                payProduct.ProductTo = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdTo);

                if (payProduct.ProductFrom.Balance < (payProduct.Amount + payProduct.Tax))
                {
                    CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo);
                    ModelState.AddModelError("", "Balance de Producto origen insuficiente.");
                    return(View(payProduct));
                }

                FunctionsConvert functionsConvert = new FunctionsConvert(_context);
                var _payProduct = functionsConvert.ConvertCurrency(payProduct, 0, 0);

                //restar el balance del producto origen
                payProduct.ProductFrom.Balance = payProduct.ProductFrom.Balance - _payProduct.Amount - _payProduct.Tax;

                //sumar al balance del prestamo destino, pero convertir antes si hay diferencia de monedas
                payProduct.ProductTo.Balance = payProduct.ProductTo.Balance - payProduct.Amount;

                //ver como trabajar con los intereses y capital del prestamo

                payProduct.StatusPayProduct = Models.Enum.StatusPayment.Activo;
                payProduct.CreatedDate      = DateTime.Today;
                _context.Add(payProduct);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            CreateInitial(payProduct.ProductIdFrom, payProduct.ProductIdTo);
            return(View(payProduct));
        }
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            var payProduct = await _context.PayProduct.SingleOrDefaultAsync(m => m.PayProductId == id);

            payProduct.ProductFrom = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdFrom);

            payProduct.ProductTo = await _context.Product.SingleOrDefaultAsync(p => p.ProductId == payProduct.ProductIdTo);

            FunctionsConvert functionsConvert = new FunctionsConvert(_context);
            var _payProduct = functionsConvert.ConvertCurrency(payProduct, 0, 0);

            //sumar el balance del producto origen
            payProduct.ProductFrom.Balance = payProduct.ProductFrom.Balance + payProduct.Amount + payProduct.Tax;

            //restar al balance al prestamo destino
            payProduct.ProductTo.Balance = payProduct.ProductTo.Balance - payProduct.Amount;

            payProduct.StatusPayProduct = Models.Enum.StatusPayment.Inactivo;

            _context.Update(payProduct);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }