public ActionResult Create(Inventory inventory) { if (ModelState.IsValid) { inventory.CompanyId = CurrentUser.CompanyId; inventory.AddedBy = CurrentUser.UserId; inventory.RemainingQuantity = inventory.OriginalQuantity; inventory.CreationDate = DateTime.Now; using (InventoryRepository inventoryRepository = new InventoryRepository(CurrentUser.CompanyId)) { if(inventoryRepository.Create(inventory)) return RedirectToAction("Index"); return Error(Loc.Dic.Error_DatabaseError); } } //using (OrderItemsRepository orderItemsRepository = new OrderItemsRepository()) using (LocationsRepository locationsRepository = new LocationsRepository(CurrentUser.CompanyId)) //using (InventoryRepository inventoryRepository = new InventoryRepository()) { //ViewBag.RelatedInventoryItem = new SelectList(orderItemsRepository.GetList(), "Id", "Title" + "SubTitle"); ViewBag.LocationId = new SelectList(locationsRepository.GetList().Where(x => x.CompanyId == CurrentUser.CompanyId), "Id", "Name"); } return View(inventory); }
public ActionResult AddToInventory(AddToInventoryModel model) { if (!Authorized(RoleType.InventoryManager)) return Error(Loc.Dic.error_no_permission); Order order; List<Inventory> createdItems = new List<Inventory>(); List<Location> locations; bool noCreationErrors = true; using (InventoryRepository inventoryRep = new InventoryRepository(CurrentUser.CompanyId)) using (LocationsRepository locationsRep = new LocationsRepository(CurrentUser.CompanyId)) using (OrdersRepository ordersRep = new OrdersRepository(CurrentUser.CompanyId)) { order = ordersRep.GetEntity(model.OrderId, "Supplier", "Orders_OrderToItem", "Orders_OrderToItem.Orders_Items"); if (order == null) return Error(Loc.Dic.error_order_get_error); if (order.WasAddedToInventory) return Error(Loc.Dic.error_order_was_added_to_inventory); if (order.StatusId < (int)StatusType.InvoiceApprovedByOrderCreatorPendingFileExport) return Error(Loc.Dic.error_invoice_not_scanned_and_approved); locations = locationsRep.GetList().ToList(); if (locations == null || locations.Count == 0) return Error(Loc.Dic.error_no_locations_found); foreach (SplittedInventoryItem splitedItem in model.InventoryItems) { if (!noCreationErrors) break; if (!splitedItem.AddToInventory) continue; int? itemId = splitedItem.ItemsToAdd[0].ItemId; Orders_OrderToItem originalItem = order.Orders_OrderToItem.FirstOrDefault(x => x.Id == itemId); bool isValidList = originalItem != null && splitedItem.ItemsToAdd.All(x => x.ItemId == itemId); if (!isValidList) { noCreationErrors = false; break; } if (splitedItem.ItemsToAdd.Count == 1) { Inventory listItem = splitedItem.ItemsToAdd[0]; if (!locations.Any(x => x.Id == listItem.LocationId)) return Error(Loc.Dic.error_invalid_form); Inventory newItem = new Inventory() { AssignedTo = listItem.AssignedTo, LocationId = listItem.LocationId, Notes = listItem.Notes, SerialNumber = listItem.SerialNumber, Status = listItem.Status, WarrentyPeriodStart = listItem.WarrentyPeriodStart, WarrentyPeriodEnd = listItem.WarrentyPeriodEnd, ItemId = originalItem.ItemId, OrderId = order.Id, CompanyId = CurrentUser.CompanyId, IsOutOfInventory = false, OriginalQuantity = originalItem.Quantity, RemainingQuantity = originalItem.Quantity }; if (!inventoryRep.Create(newItem)) { noCreationErrors = false; break; } createdItems.Add(newItem); } else if (originalItem.Quantity == splitedItem.ItemsToAdd.Count) { foreach (var item in splitedItem.ItemsToAdd) { if (!locations.Any(x => x.Id == item.LocationId)) { noCreationErrors = false; break; } item.ItemId = originalItem.ItemId; item.OrderId = order.Id; item.CompanyId = CurrentUser.CompanyId; item.IsOutOfInventory = false; if (!inventoryRep.Create(item)) { noCreationErrors = false; break; } createdItems.Add(item); } } else { noCreationErrors = false; break; } } if (!noCreationErrors) { foreach (var item in createdItems) { inventoryRep.Delete(item.Id); } return Error(Loc.Dic.error_inventory_create_error); } order.WasAddedToInventory = true; order.LastStatusChangeDate = DateTime.Now; if (ordersRep.Update(order) == null) return Error(Loc.Dic.error_database_error); bool hasInventoryItems = model.InventoryItems.Any(x => x.AddToInventory); string notes = hasInventoryItems ? Loc.Dic.AddToInventory_with_inventory_items : Loc.Dic.AddToInventory_no_inventory_items; int? historyActionId = null; historyActionId = (int)HistoryActions.AddedToInventory; Orders_History orderHistory = new Orders_History(); using (OrdersHistoryRepository ordersHistoryRep = new OrdersHistoryRepository(CurrentUser.CompanyId, CurrentUser.UserId, order.Id)) if (historyActionId.HasValue) ordersHistoryRep.Create(orderHistory, historyActionId.Value, notes); return RedirectToAction("PendingInventory"); } }
/// <summary> /// Deprecated Method for adding a new object to the Inventories EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToInventories(Inventory inventory) { base.AddObject("Inventories", inventory); }
/// <summary> /// Create a new Inventory object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="creationDate">Initial value of the CreationDate property.</param> /// <param name="companyId">Initial value of the CompanyId property.</param> /// <param name="locationId">Initial value of the LocationId property.</param> /// <param name="isOutOfInventory">Initial value of the IsOutOfInventory property.</param> /// <param name="inventarNumber">Initial value of the InventarNumber property.</param> public static Inventory CreateInventory(global::System.Int32 id, global::System.DateTime creationDate, global::System.Int32 companyId, global::System.Int32 locationId, global::System.Boolean isOutOfInventory, global::System.Int32 inventarNumber) { Inventory inventory = new Inventory(); inventory.Id = id; inventory.CreationDate = creationDate; inventory.CompanyId = companyId; inventory.LocationId = locationId; inventory.IsOutOfInventory = isOutOfInventory; inventory.InventarNumber = inventarNumber; return inventory; }
public ActionResult Edit(Inventory inventory) { if (ModelState.IsValid) { inventory.CompanyId = CurrentUser.CompanyId; db.Inventories.Attach(inventory); db.ObjectStateManager.ChangeObjectState(inventory, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name", inventory.CompanyId); ViewBag.RelatedInventoryItem = new SelectList(db.Inventories, "Id", "OrderId", inventory.RelatedInventoryItem); ViewBag.ItemId = new SelectList(db.Orders_Items, "Id", "Title", inventory.ItemId); ViewBag.LocationId = new SelectList(db.Locations, "Id", "City", inventory.LocationId); return View(inventory); }