Beispiel #1
0
        public async Task <IActionResult> SaveModel(long id, PlanTransaction vm)
        {
            if (!ModelState.IsValid)
            {
                LoadLocalSelectList(vm);
                return(View("ModelForm", vm));
            }

            //var model = new PlanTransaction();
            //vm.Transfer(ref model, null, false);

            if (vm.IsNew)
            {
                Context.Add(vm);
            }
            else
            {
                if (id != vm.Id)
                {
                    return(NotFound());
                }

                var currentModel = await Context.PlanTransactions.FindAsync(vm.Id);

                //Only Update the Neccesary fields
                vm.Transfer(ref currentModel, null, false);

                Context.Update(currentModel);
            }

            await Context.SaveChangesAsync();

            return(RedirectToAction(nameof(Details), new { id = vm.Id }));
        }
Beispiel #2
0
        public IActionResult Create()
        {
            var vm = new PlanTransaction {
                IsNew = true
            };

            LoadLocalSelectList(vm);
            return(View("ModelForm", vm));
        }
Beispiel #3
0
 private void LoadLocalSelectList(PlanTransaction model)
 {
     ViewData["Currencies"]   = new SelectList(Context.Set <Currency>(), "Id", "Name", model.CurrencyId);
     ViewData["OwnerPlans"]   = new SelectList(Context.Set <OwnerPlan>(), "Id", "Name", model.OwnerPlanId);
     ViewData["PaymentTypes"] = new SelectList(Context.Set <PaymentType>(), "Id", "Name", model.PaymentTypeId);
 }
Beispiel #4
0
        public async Task <IActionResult> SaveModel(long id, OwnerPlan vm)
        {
            if (!ModelState.IsValid)
            {
                LoadLocalSelectList(vm);
                return(View("ModelForm", vm));
            }

            //var model = new OwnerPlan();
            //vm.Transfer(ref model, null, false);

            var plan = await Context.Plans.FindAsync(vm.PlanId);

            var owner = await Context.Owners.FindAsync(vm.OwnerId);

            if (vm.IsNew)
            {
                var planTransaction = new PlanTransaction
                {
                    Amount         = plan.Amount,
                    CurrencyId     = plan.CurrencyId,
                    Date           = DateTime.Now,
                    Megabytes      = plan.Megabytes,
                    PaymentTypeId  = 1,
                    Registers      = plan.RegiterCount,
                    UserPosibility = plan.UserPosibility
                };

                owner.MegabytesAvailable += plan.Megabytes;
                owner.RegitersAvailable  += plan.RegiterCount;
                owner.UsersAvailable     += plan.UserPosibility;

                vm.PlanTransactions.Add(planTransaction);
                Context.Add(vm);
            }
            else
            {
                if (id != vm.Id)
                {
                    return(NotFound());
                }

                var currentModel = await Context.OwnerPlans.FindAsync(vm.Id);

                if (vm.OwnerId != currentModel.OwnerId)
                {
                    ModelState.AddModelError(string.Empty, "No Puede Cambiar de Cliente para la Edicion");
                    return(View("ModelForm", vm));
                }

                if (currentModel.PlanId != vm.PlanId)
                {
                    var previousPlan = await Context.Plans.FindAsync(currentModel.PlanId);

                    owner.MegabytesAvailable -= previousPlan.Megabytes;
                    owner.RegitersAvailable  -= previousPlan.RegiterCount;
                    owner.UsersAvailable     -= previousPlan.UserPosibility;

                    owner.MegabytesAvailable += plan.Megabytes;
                    owner.RegitersAvailable  += plan.RegiterCount;
                    owner.UsersAvailable     += plan.UserPosibility;
                }

                //Only Update the Neccesary fields
                vm.Transfer(ref currentModel, null, false);

                Context.Update(currentModel);
            }
            Context.Update(owner);
            await Context.SaveChangesAsync();

            return(RedirectToAction(nameof(Details), new { id = vm.Id }));
        }