Example #1
0
        public async Task <ActionResult> Edit(ItemViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(View(item));
            }
            // check quantities
            if (item.Quantity < 0)
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Неправильное количество (" + item.Quantity + ")!");
                return(View(item));
            }
            if (item.MinQuantity < -1)
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Неправильное количество (" + item.MinQuantity + ")!");
                return(View(item));
            }
            // find model item to edit
            var editItem = await _db.Items.FindAsync(item.Id);

            if (editItem == null)
            {
                return(HttpNotFound());
            }
            // update editable values
            editItem.Name        = item.Name;
            editItem.Quantity    = item.Quantity;
            editItem.MinQuantity = item.MinQuantity;
            // modify existing or create new attribute values
            foreach (var attributeValue in item.AttributeValues)
            {
                if (attributeValue.IsNumber && !StaticData.IsNumber(attributeValue.Value))
                {
                    // return view with error message
                    ModelState.AddModelError(string.Empty, attributeValue.Name + " не может быть " + attributeValue.Value + "!");
                    return(View(item));
                }
                var editValue = await _db.ItemAttributeValues.FindAsync(attributeValue.Id);

                // new attribute-value pair
                if (editValue == null)
                {
                    var newAttributeValue = new ItemAttributeValue
                    {
                        Attribute  = await _db.ItemAttributes.FindAsync(attributeValue.AttributeId),
                        ParentItem = editItem,
                        Value      = attributeValue.Value
                    };
                    // add new attribute value to db
                    _db.ItemAttributeValues.Add(newAttributeValue);
                    // add new attribute value to the model item
                    editItem.AttributeValues.Add(newAttributeValue);
                }
                // existing attribute-value pair
                else
                {
                    // update attribute value
                    editValue.Value            = attributeValue.Value;
                    _db.Entry(editValue).State = EntityState.Modified;
                }
            }
            _db.Entry(editItem).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #2
0
        public async Task <ActionResult> Transfer(ItemViewModel item)
        {
            if (!ModelState.IsValid)
            {
                return(View(item));
            }
            //find transferred item in db
            var itemInOldOffice = await _db.Items.FindAsync(item.Id);

            if (itemInOldOffice == null)
            {
                return(HttpNotFound());
            }
            //if trip notebook
            if (item.ItemTypeId == 8)
            {
                item.Quantity = 1;
            }
            //check if we transfer more than we have
            if (itemInOldOffice.Quantity < item.Quantity)
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Нельзя переместить больше, чем есть в наличии (" + itemInOldOffice.Quantity + " шт.)!");
                return(View(item));
            }

            //find item in target location
            Item itemInNewOffice;

            if (item.TargetOfficeId == 1)
            {
                //for the main office location may be null
                itemInNewOffice = await _db.Items.Where(i => i.Name == item.Name && (i.Location == null || i.Location.Id == 1)).FirstOrDefaultAsync();
            }
            else
            {
                itemInNewOffice = await _db.Items.Where(i => i.Name == item.Name && i.Location.Id == item.TargetOfficeId).FirstOrDefaultAsync();
            }

            //item doesn't exist in target location or it's a trip notebook - create new item
            if (item.ItemTypeId == 8 || itemInNewOffice == null)
            {
                itemInNewOffice = new Item
                {
                    Name        = item.Name,
                    Quantity    = item.Quantity,
                    MinQuantity = itemInOldOffice.MinQuantity,
                    ItemType    = await _db.ItemTypes.FindAsync(itemInOldOffice.ItemType.Id),
                    Location    = await _db.Offices.FindAsync(item.TargetOfficeId)
                };
                _db.Items.Add(itemInNewOffice);
                await _db.SaveChangesAsync();

                //copy attribute-value pairs from source item
                foreach (var attr in itemInOldOffice.AttributeValues)
                {
                    var newAttrValuePair = new ItemAttributeValue
                    {
                        Attribute  = await _db.ItemAttributes.FindAsync(attr.Attribute.Id),
                        ParentItem = itemInNewOffice,
                        Value      = attr.Value
                    };
                    _db.ItemAttributeValues.Add(newAttrValuePair);
                    itemInNewOffice.AttributeValues.Add(newAttrValuePair);
                }
                //cartridge
                if (item.ItemTypeId == 11)
                {
                    foreach (var printer in itemInOldOffice.Printers)
                    {
                        itemInNewOffice.Printers.Add(await _db.Printers.FindAsync(printer.Id));
                    }
                }
            }
            else
            {
                itemInNewOffice.Quantity += item.Quantity;
            }

            // decrease source item quantity
            //if trip notebook - delete
            if (item.ItemTypeId == 8)
            {
                foreach (var attr in _db.ItemAttributeValues.Where(a => a.ParentItem.Id == item.Id))
                {
                    _db.ItemAttributeValues.Remove(attr);
                }
                _db.Items.Remove(itemInOldOffice);
            }
            else
            {
                itemInOldOffice.Quantity        -= item.Quantity;
                _db.Entry(itemInOldOffice).State = EntityState.Modified;
            }

            _db.Entry(itemInNewOffice).State = EntityState.Modified;
            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Example #3
0
        public async Task <ActionResult> Create(ItemViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            // check quantities
            if (model.Quantity < 0)
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Неправильное количество (" + model.Quantity + ")!");
                return(View(model));
            }
            if (model.MinQuantity < -1)
            {
                // return view with error message
                ModelState.AddModelError(string.Empty, "Неправильное количество (" + model.MinQuantity + ")!");
                return(View(model));
            }

            // create new model item
            // if trip notebook:
            if (model.ItemTypeId == 8)
            {
                model.Quantity    = 1;
                model.MinQuantity = 0;
            }
            var newItem = new Item
            {
                Name        = model.Name,
                Quantity    = model.Quantity,
                MinQuantity = model.MinQuantity,
                ItemType    = await _db.ItemTypes.FindAsync(model.ItemTypeId),
                Location    = await _db.Offices.FindAsync(model.TargetOfficeId)
            };

            // add to db
            _db.Items.Add(newItem);
            // create new history item
            var newHistory = new History
            {
                Recieved = true,
                Quantity = model.Quantity,
                Date     = DateTime.Now,
                Item     = newItem
            };

            // add to db
            _db.Histories.Add(newHistory);
            // add history item to item history list
            newItem.Histories.Add(newHistory);
            // create attribute values from attribute viewmodels
            foreach (var attributeValue in model.AttributeValues)
            {
                if (attributeValue.IsNumber && !StaticData.IsNumber(attributeValue.Value))
                {
                    // return view with error message
                    ModelState.AddModelError(string.Empty, attributeValue.Name + " не может быть " + attributeValue.Value + "!");
                    return(View(model));
                }
                var newValue = new ItemAttributeValue
                {
                    Attribute  = await _db.ItemAttributes.FindAsync(attributeValue.AttributeId),
                    ParentItem = newItem,
                    Value      = attributeValue.Value
                };
                // add value to created model item
                newItem.AttributeValues.Add(newValue);
                // add value to db
                _db.ItemAttributeValues.Add(newValue);
            }
            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }