Пример #1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,Description,Value,MaxValue")] EquipmentConsumable consumable)
        {
            if (ModelState.IsValid)
            {
                db.Entry(consumable).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Details", new { id = consumable.Id }));
            }
            return(View(consumable));
        }
Пример #2
0
        public async Task <ActionResult> CreateConfirmed(EquipmentConsumable consumable)
        {
            if (ModelState.IsValid)
            {
                db.EquipmentConsumables.Add(consumable);

                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(consumable));
        }
Пример #3
0
        public async Task <ActionResult> ConfirmContract(int?id)
        {
            ActionResult result = RedirectToAction("Index");

            if (id != null)
            {
                await db.Contracts.FindAsync(id).ContinueWith(async(t) =>
                {
                    var request = t.Result;
                    if (request != null)
                    {
                        var formRequest = HttpContext.Request.Form;

                        var consumableIdList = formRequest.AllKeys.Where(k => k.StartsWith("consumable")) // Берем только расходные материалы
                                               .Select(k => k.Replace("consumable-", string.Empty));      // Оставляем только id

                        var consumableWithValueList = consumableIdList.Select(k => new { consumableId = int.Parse(k), value = int.Parse(formRequest["consumable-" + k]) });

                        foreach (var item in consumableWithValueList)
                        {
                            EquipmentConsumable consumable = await db.EquipmentConsumables.FindAsync(item.consumableId);
                            db.ModifiedValueConsumables.Add(new ModifiedValueConsumable
                            {
                                Date = DateTime.Now,
                                EquipmentConsumable = consumable,
                                OldValue            = consumable.Value,
                                NewValue            = consumable.Value - consumableWithValueList.FirstOrDefault(cv => cv.consumableId == consumable.Id).value // Отнимаем потраченное кол-во материала
                            });
                            consumable.Value -= item.value;                                                                                                   // Отнимаем потраченное кол-во материала у расходного материала
                        }

                        request.Status = ContractStatus.Ready;

                        await db.SaveChangesAsync();
                    }
                });
            }
            else
            {
                result = new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            return(result);
        }