From f4e3ac1ccebb71a9f44fb736df18b922144893ef Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Thu, 11 Aug 2016 09:27:38 -0400 Subject: [PATCH] Add commodity and quantity to input forms --- .vs/config/applicationhost.config | 2 +- InventoryTraker.Web/App_Start/EFConfig.cs | 2 +- InventoryTraker.Web/App_Start/SeedData.cs | 28 ++++++- .../Controllers/InventoryController.cs | 35 ++++++-- InventoryTraker.Web/Core/Inventory.cs | 6 ++ .../Models/InventoryViewModel.cs | 1 - .../Views/Inventory/Index.cshtml | 2 +- .../js/inventory/InventoryAddDirective.js | 17 +++- .../js/inventory/InventoryDetailsDirective.js | 1 - .../js/inventory/inventorySvc.js | 52 +++++++++++- .../templates/inventoryAdd.tmpl.cshtml | 83 ++++++++++++++++--- 11 files changed, 200 insertions(+), 29 deletions(-) diff --git a/.vs/config/applicationhost.config b/.vs/config/applicationhost.config index 101dd69..3ef54eb 100644 --- a/.vs/config/applicationhost.config +++ b/.vs/config/applicationhost.config @@ -163,7 +163,7 @@ - + diff --git a/InventoryTraker.Web/App_Start/EFConfig.cs b/InventoryTraker.Web/App_Start/EFConfig.cs index 76de9fc..cf5dae9 100644 --- a/InventoryTraker.Web/App_Start/EFConfig.cs +++ b/InventoryTraker.Web/App_Start/EFConfig.cs @@ -7,7 +7,7 @@ namespace InventoryTraker.Web { public static void Initialize() { - Database.SetInitializer(new DropCreateDatabaseIfModelChanges()); + Database.SetInitializer(new DropCreateDatabaseAlways()); } } } \ No newline at end of file diff --git a/InventoryTraker.Web/App_Start/SeedData.cs b/InventoryTraker.Web/App_Start/SeedData.cs index d5409db..7bf3a8f 100644 --- a/InventoryTraker.Web/App_Start/SeedData.cs +++ b/InventoryTraker.Web/App_Start/SeedData.cs @@ -67,17 +67,39 @@ namespace InventoryTraker.Web private static void AddInventory(AppDbContext context) { + var peanutButterSmooth = new InventoryType + { + ContainerType = "18 oz jars", + Name = "Peanut Butter Smth 18", + Id = "100395", + UnitsPerCase = 12, + PricePerCase = 14.55M, + WeightPerCase = 13.5 + }; + context.InventoryTypes.Add(peanutButterSmooth); + var beanInventoryType = new InventoryType { ContainerType = "#300 cans", - Name = "Beans, Green 300", + Name = "Beans, Veg 300", Id = "100363", UnitsPerCase = 24, - PricePerCase = 20.42M, - WeightPerCase = 30.2 + PricePerCase = 10.18M, + WeightPerCase = 24 }; context.InventoryTypes.Add(beanInventoryType); + var corn = new InventoryType + { + ContainerType = "#300 cans", + Name = "Corn Kernel 300", + Id = "100311", + UnitsPerCase = 24, + PricePerCase = 10.03M, + WeightPerCase = 22.9 + }; + context.InventoryTypes.Add(corn); + context.Inventories.Add(new Inventory { InventoryType = beanInventoryType, diff --git a/InventoryTraker.Web/Controllers/InventoryController.cs b/InventoryTraker.Web/Controllers/InventoryController.cs index 6caba9c..634f9ee 100644 --- a/InventoryTraker.Web/Controllers/InventoryController.cs +++ b/InventoryTraker.Web/Controllers/InventoryController.cs @@ -8,6 +8,25 @@ using InventoryTraker.Web.Models; namespace InventoryTraker.Web.Controllers { + public class InventoryTypeController : ControllerBase + { + private readonly AppDbContext _context; + + public InventoryTypeController(AppDbContext context) + { + _context = context; + } + + public JsonResult All() + { + var viewModels = _context.InventoryTypes + .OrderByDescending(x => x.Name) + .ProjectTo(); + + return BetterJson(viewModels.ToArray()); + } + } + public class InventoryController : ControllerBase { private readonly AppDbContext _context; @@ -24,20 +43,22 @@ namespace InventoryTraker.Web.Controllers public JsonResult All() { - var customerModels = _context.Inventories + var viewModels = _context.Inventories .OrderByDescending(x => x.InventoryType.Name) - .ProjectTo(); + .ProjectTo() + .ToArray(); - return BetterJson(customerModels.ToArray()); + return BetterJson(viewModels); } - public JsonResult Arrival(AddCustomerForm form) + public JsonResult Add(InventoryAddForm form) { - var customer = Mapper.Map(form); - _context.Customers.Add(customer); + var inventory = Mapper.Map(form); + inventory.InventoryType = _context.InventoryTypes.Find(form.InventoryTypeId); + _context.Inventories.Add(inventory); _context.SaveChanges(); - var model = Mapper.Map(customer); + var model = Mapper.Map(inventory); return BetterJson(model); } } diff --git a/InventoryTraker.Web/Core/Inventory.cs b/InventoryTraker.Web/Core/Inventory.cs index 3928307..ad287fc 100644 --- a/InventoryTraker.Web/Core/Inventory.cs +++ b/InventoryTraker.Web/Core/Inventory.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace InventoryTraker.Web.Core @@ -11,10 +12,13 @@ namespace InventoryTraker.Web.Core [DatabaseGenerated(DatabaseGeneratedOption.None)] public string Id { get; set; } + [Required] public string Name { get; set; } + [Required] public int UnitsPerCase { get; set; } + [Required] public string ContainerType { get; set; } public double WeightPerCase { get; set; } @@ -30,8 +34,10 @@ namespace InventoryTraker.Web.Core public virtual ICollection Transactions { get; set; } + [Required] public DateTime ExpirationDate { get; set; } + [Required] public int Quantity { get; set; } public string Memo { get; set; } diff --git a/InventoryTraker.Web/Models/InventoryViewModel.cs b/InventoryTraker.Web/Models/InventoryViewModel.cs index 8719956..d8ca5a5 100644 --- a/InventoryTraker.Web/Models/InventoryViewModel.cs +++ b/InventoryTraker.Web/Models/InventoryViewModel.cs @@ -28,7 +28,6 @@ namespace InventoryTraker.Web.Models configuration.CreateMap() .ForMember(d => d.Name, opt => opt.MapFrom(s => s.InventoryType.Name)) .ForMember(d => d.UnitsPerCase, opt => opt.MapFrom(s => s.InventoryType.UnitsPerCase)) - .ForMember(d => d.ContainerType, opt => opt.MapFrom(s => s.InventoryType.ContainerType)) .ForMember(d => d.WeightPerCase, opt => opt.MapFrom(s => s.InventoryType.WeightPerCase)) .ForMember(d => d.PricePerCase, opt => opt.MapFrom(s => s.InventoryType.PricePerCase)); } diff --git a/InventoryTraker.Web/Views/Inventory/Index.cshtml b/InventoryTraker.Web/Views/Inventory/Index.cshtml index c6017ad..3ab8458 100644 --- a/InventoryTraker.Web/Views/Inventory/Index.cshtml +++ b/InventoryTraker.Web/Views/Inventory/Index.cshtml @@ -9,7 +9,7 @@ Inventory -
+
\ No newline at end of file diff --git a/InventoryTraker.Web/js/inventory/InventoryAddDirective.js b/InventoryTraker.Web/js/inventory/InventoryAddDirective.js index a0c6263..5680433 100644 --- a/InventoryTraker.Web/js/inventory/InventoryAddDirective.js +++ b/InventoryTraker.Web/js/inventory/InventoryAddDirective.js @@ -11,14 +11,22 @@ } } - controller.$inject = ['$scope', 'inventorySvc']; - function controller($scope, inventorySvc) { + controller.$inject = ['$scope', 'inventorySvc', 'inventoryTypeSvc']; + function controller($scope, inventorySvc, inventoryTypeSvc) { var vm = this; vm.add = add; vm.saving = false; vm.inventory = {}; + vm.inventoryTypes = inventoryTypeSvc.inventoryTypes; vm.errorMessage = null; + vm.quantity = quantity; + + function quantity() { + vm.inventory.quantity = + $scope.palletCount * $scope.casesPerPallet + $scope.caseCount; + return vm.inventory.quantity; + } function add() { vm.saving = true; @@ -34,5 +42,10 @@ vm.saving = false; }); } + + $scope.$watch('commodity', function (newValue, oldValue) { + if (newValue) + vm.inventory.inventoryTypeId = newValue.id; + }); } })(); \ No newline at end of file diff --git a/InventoryTraker.Web/js/inventory/InventoryDetailsDirective.js b/InventoryTraker.Web/js/inventory/InventoryDetailsDirective.js index 9f9fb53..05a161e 100644 --- a/InventoryTraker.Web/js/inventory/InventoryDetailsDirective.js +++ b/InventoryTraker.Web/js/inventory/InventoryDetailsDirective.js @@ -19,7 +19,6 @@ vm.inventory = $scope.inventory; vm.edit = edit; - vm.inventory = $scope.inventory; function edit() { $uibModal.open({ diff --git a/InventoryTraker.Web/js/inventory/inventorySvc.js b/InventoryTraker.Web/js/inventory/inventorySvc.js index 6d413b1..ba9cc33 100644 --- a/InventoryTraker.Web/js/inventory/inventorySvc.js +++ b/InventoryTraker.Web/js/inventory/inventorySvc.js @@ -11,7 +11,7 @@ add: add, update: update, inventories: inventories, - getInventory: getInventory + getInventory: getInventory, }; return svc; @@ -42,6 +42,56 @@ if (inventories[i].Id == id) return inventories[i]; } + return null; + } + } +})(); + + +(function () { + window.app.factory('inventoryTypeSvc', inventoryTypeSvc); + + inventoryTypeSvc.$inject = ['$http']; + function inventoryTypeSvc($http) { + var inventoryTypes = []; + + loadInventoryTypes(); + + var svc = { + add: add, + update: update, + inventoryTypes: inventoryTypes, + getInventoryType: getInventoryType + }; + + return svc; + + function loadInventoryTypes() { + $http.post('/InventoryType/All') + .success(function (data) { + inventoryTypes.addRange(data); + }); + } + + function add(inventoryType) { + return $http.post('/InventoryType/Add', inventoryType) + .success(function (inventoryType) { + inventoryTypes.unshift(inventoryType); + }); + } + + function update(existingInventory, updatedInventory) { + return $http.post('/InventoryType/Update', updatedInventory) + .success(function (inventory) { + angular.extend(existingInventory, inventory); + }); + } + + function getInventoryType(id) { + for (var i = 0; i < inventoryTypes.length; i++) { + if (inventoryTypes[i].Id == id) return inventoryTypes[i]; + } + return null; } } diff --git a/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml b/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml index c112fc4..0f6f58b 100644 --- a/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml +++ b/InventoryTraker.Web/js/inventory/templates/inventoryAdd.tmpl.cshtml @@ -17,20 +17,81 @@
Enter details for the inventory arrival below.
-
- {{vm.errorMessage}} -
+
+ {{vm.errorMessage}} +
- @inventory.FormGroupFor(m => m.ExpirationDate) - @inventory.FormGroupFor(m => m.AddedDate) - @inventory.FormGroupFor(m => m.Memo) + - +
+ + + +
+
Selected Commodity
+
+
+
Commodity ID
+
{{commodity.id}}
+
Name
+
{{commodity.name}}
+
Units per Case
+
{{commodity.unitsPerCase}} / {{commodity.containerType}}
+
+
+
+
- + @inventory.FormGroupFor(m => m.ExpirationDate) + +
+
+
+
+
Pallets
+
+ +
+
+
+
Cases per Pallet
+
+ +
+
+
+
+
Individual Cases
+
+ +
+
+
+
+
Total Units
+
{{vm.quantity()}}
+
+
+
+ + @inventory.FormGroupFor(m => m.AddedDate) + @inventory.FormGroupFor(m => m.Memo) + + + + \ No newline at end of file