public void TestRequestPostAllowBackorderBinding2()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");
            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            orderViewModel.Backorder = string.Empty;
            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(false, ((Order)orderServiceArgs1[0]).AllowBackorder);
            #endregion Assert
        }
        public void TestRequestPostAttachmentsBinding3()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");
            new FakeAttachments(5, RepositoryFactory.AttachmentRepository, null, false, true);

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            orderViewModel.FileIds = new[] { SpecificGuid.GetGuid(2), SpecificGuid.GetGuid(3), SpecificGuid.GetGuid(4) };

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(0, ((Order)orderServiceArgs1[0]).Attachments.Count); //This is zero now because the attachments are added to the order after the order is saved
            //Assert.AreEqual(SpecificGuid.GetGuid(2), ((Order)orderServiceArgs1[0]).Attachments[0].Id);
            //Assert.AreEqual(SpecificGuid.GetGuid(3), ((Order)orderServiceArgs1[0]).Attachments[1].Id);
            //Assert.AreEqual(SpecificGuid.GetGuid(4), ((Order)orderServiceArgs1[0]).Attachments[2].Id);
            #endregion Assert
        }
        public void TestRequestPostRedirectsToReview()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] {""}, "Me");
            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";
            orderViewModel.Account = "acct123";
            orderViewModel.Approvers = "some app";
            orderViewModel.AccountManagers = "acct manage";
            orderViewModel.ConditionalApprovals = new []{3,4,7};
            orderViewModel.Justification = "Some Just";
            orderViewModel.FormSaveId = SpecificGuid.GetGuid(7);

            RepositoryFactory.OrderRepository.Expect(a => a.EnsurePersistent(Arg<Order>.Is.Anything)).Do(new SetOrderDelegate(SetOrderInstance)); //Set the ID to 99 when it is saved

            #endregion Arrange

            #region Act
            var result = Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual("Some Just", ((Order)orderServiceArgs1[0]).Justification);
            Assert.AreEqual("acct123", orderServiceArgs1[2]);
            Assert.AreEqual("some app", orderServiceArgs1[3]);
            Assert.AreEqual("acct manage", orderServiceArgs1[4]);
            Assert.AreEqual(" 3 4 7", ((int[])orderServiceArgs1[1]).IntArrayToString());

            OrderService.AssertWasCalled(a => a.HandleSavedForm(Arg<Order>.Is.Anything, Arg<Guid>.Is.Anything));
            var orderServiceArgs2 = OrderService.GetArgumentsForCallsMadeOn(a => a.HandleSavedForm(Arg<Order>.Is.Anything, Arg<Guid>.Is.Anything))[0];
            Assert.AreEqual("Some Just", ((Order)orderServiceArgs2[0]).Justification);
            Assert.AreEqual(SpecificGuid.GetGuid(7), ((Guid)orderServiceArgs2[1]));

            OrderRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Order>.Is.Anything));
            var OrderArgs = (Order) OrderRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Order>.Is.Anything))[0][0];

            Assert.AreEqual(99, result.RouteValues["id"]);
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            #endregion Assert
        }
        public void TestRequestPostRestrictedBinding4()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            orderViewModel.Restricted = null;
            //orderViewModel.Restricted.Status = "true"; //Lower case is ignored
            //orderViewModel.Restricted.Class = "SomeClass";
            //orderViewModel.Restricted.Use = "SomeUse";
            //orderViewModel.Restricted.StorageSite = "SomeStorage";
            //orderViewModel.Restricted.Custodian = "Somebody";
            //orderViewModel.Restricted.Users = "Someone who is authorized";

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(false, ((Order)orderServiceArgs1[0]).HasControlledSubstance);
            Assert.IsNull(((Order)orderServiceArgs1[0]).GetAuthorizationInfo());
            //Assert.AreEqual("SomeClass", ((Order)orderServiceArgs1[0]).GetAuthorizationInfo().ClassSchedule);
            //Assert.AreEqual("SomeUse", ((Order)orderServiceArgs1[0]).GetAuthorizationInfo().Use);
            //Assert.AreEqual("SomeStorage", ((Order)orderServiceArgs1[0]).GetAuthorizationInfo().StorageSite);
            //Assert.AreEqual("Somebody", ((Order)orderServiceArgs1[0]).GetAuthorizationInfo().Custodian);
            //Assert.AreEqual("Someone who is authorized", ((Order)orderServiceArgs1[0]).GetAuthorizationInfo().EndUser);
            #endregion Assert
        }
        public void TestRequestPostOrderTypeBinding()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");
            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            var orderTypes = new List<OrderType>();
            for (int i = 0; i < 3; i++)
            {
                orderTypes.Add(CreateValidEntities.OrderType(i+1));
                orderTypes[i].SetIdTo((i + 1).ToString());
            }
            orderTypes[1].SetIdTo(OrderType.Types.OrderRequest);
            new FakeOrderTypes(0, RepositoryFactory.OrderTypeRepository, orderTypes, true);

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(OrderType.Types.OrderRequest, ((Order)orderServiceArgs1[0]).OrderType.Id);
            #endregion Assert
        }
        public void TestRequestPostOrganizationBinding()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");
            var workgroups = new List<Workgroup>();
            workgroups.Add(CreateValidEntities.Workgroup(1));
            workgroups[0].PrimaryOrganization = CreateValidEntities.Organization(77);
            workgroups[0].SetIdTo(1);
            new FakeWorkgroups(0, WorkgroupRepository, workgroups);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 1))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(1)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 1)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 1;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            orderViewModel.DateNeeded = DateTime.UtcNow.ToPacificTime().AddDays(3).Date;
            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual("Name77", ((Order)orderServiceArgs1[0]).Organization.Name);
            #endregion Assert
        }
        public void TestRequestPostIncludeLineItemsAndSplits8()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.99%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Price = "1.99";
            orderViewModel.Items[0].Quantity = "14";
            orderViewModel.Items[0].CatalogNumber = "Cat1";
            orderViewModel.Items[0].CommodityCode = "2";
            orderViewModel.Items[0].Description = "Desc1";
            orderViewModel.Items[0].Notes = "Notes1";
            orderViewModel.Items[0].Units = "Units1";
            orderViewModel.Items[0].Url = "Url99";

            new FakeCommodity(3, RepositoryFactory.CommodityRepository);

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            RepositoryFactory.OrderRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Order>.Is.Anything));
            var args = (Order)RepositoryFactory.OrderRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Order>.Is.Anything))[0][0];
            Assert.AreEqual(1, args.LineItems.Count);
            Assert.AreEqual(1.99m, args.LineItems[0].UnitPrice);
            Assert.AreEqual(14, args.LineItems[0].Quantity);
            Assert.AreEqual("Cat1", args.LineItems[0].CatalogNumber);

            Assert.AreEqual("2", args.LineItems[0].Commodity.Id);
            Assert.AreEqual("Desc1", args.LineItems[0].Description);
            Assert.AreEqual("Notes1", args.LineItems[0].Notes);
            Assert.AreEqual("Units1", args.LineItems[0].Unit);
            Assert.AreEqual("Url99", args.LineItems[0].Url);
            #endregion Assert
        }
Example #8
0
        public ActionResult Copy(int id, OrderViewModel model)
        {
            var canCreateOrderInWorkgroup =
                _securityService.HasWorkgroupAccess(_repositoryFactory.WorkgroupRepository.GetById(model.Workgroup));

            Check.Require(canCreateOrderInWorkgroup, "canCreateOrderInWorkgroup");

            var order = new Order();

            BindOrderModel(order, model, includeLineItemsAndSplits: true);

            order.Tag = order.Workgroup.DefaultTag;

            _orderService.CreateApprovalsForNewOrder(order, accountId: model.Account, approverId: model.Approvers, accountManagerId: model.AccountManagers, conditionalApprovalIds: model.ConditionalApprovals);

            Check.Require(order.LineItems.Count > 0, "line count");

            _repositoryFactory.OrderRepository.EnsurePersistent(order);

            //Do attachment stuff Note, must be done by persisting the attachment directly, not adding it to the order and having the order save it, otherwise we get a transient exception if we have to add a new user because of an external account
            if (model.FileIds != null)
            {
                foreach (var fileId in model.FileIds.Where(x => !Guid.Empty.Equals(x)))
                {
                    var attachment = _repositoryFactory.AttachmentRepository.GetById(fileId);
                    attachment.Order = order;
                    _repositoryFactory.AttachmentRepository.EnsurePersistent(attachment);
                }
            }

            Message = Resources.OrderCopy_Success;

            return RedirectToAction("Review", new { id = order.Id });
        }
        public void TestRequestPostIncludeLineItemsAndSplits5()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "not valid";
            orderViewModel.Shipping = "not valid2";
            orderViewModel.Freight = "not valid 3";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            RepositoryFactory.OrderRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Order>.Is.Anything));
            var args = (Order)RepositoryFactory.OrderRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Order>.Is.Anything))[0][0];
            Assert.AreEqual(7.5m, args.EstimatedTax); //Default
            Assert.AreEqual(0, args.ShippingAmount); //Couldn't parse. Default used
            Assert.AreEqual(0, args.FreightAmount); //Couldn't parse. Default used
            #endregion Assert
        }
        public void TestRequestPostIncludeLineItemsAndSplits6()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.99%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[3];
            for (int i = 0; i < 3; i++)
            {
                orderViewModel.Items[i] = new OrderViewModel.LineItem();
                orderViewModel.Items[i].Price = "1.02";
                orderViewModel.Items[i].Quantity = (i + 1).ToString();
            }

            orderViewModel.Items[1].Quantity = string.Empty;

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            RepositoryFactory.OrderRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Order>.Is.Anything));
            var args = (Order)RepositoryFactory.OrderRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Order>.Is.Anything))[0][0];
            Assert.AreEqual(2, args.LineItems.Count);
            Assert.AreEqual(1.02m, args.LineItems[0].UnitPrice);
            Assert.AreEqual(1, args.LineItems[0].Quantity);
            Assert.AreEqual(1.02m, args.LineItems[1].UnitPrice);
            Assert.AreEqual(3, args.LineItems[1].Quantity);
            #endregion Assert
        }
        public void TestRequestPostIncludeLineItemsAndSplits2()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";
            orderViewModel.Splits = new OrderViewModel.Split[1];
            orderViewModel.Splits[0] = new OrderViewModel.Split();
            orderViewModel.Splits[0].Account = "Acct1";
            orderViewModel.Splits[0].Amount = "35";
            orderViewModel.Splits[0].SubAccount = "SubAcct1";

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));

            BugTrackingService.AssertWasCalled(a => a.CheckForClearedOutSubAccounts(Arg<Order>.Is.Anything, Arg<OrderViewModel.Split[]>.Is.Anything, Arg<OrderViewModel>.Is.Anything));
            var bugTrackingServiceArgs1 = BugTrackingService.GetArgumentsForCallsMadeOn(a => a.CheckForClearedOutSubAccounts(Arg<Order>.Is.Anything, Arg<OrderViewModel.Split[]>.Is.Anything, Arg<OrderViewModel>.Is.Anything))[0];
            Assert.AreEqual(1.23m, ((Order)bugTrackingServiceArgs1[0]).FreightAmount);
            Assert.AreEqual("Acct1", ((OrderViewModel.Split[])bugTrackingServiceArgs1[1])[0].Account);
            Assert.AreEqual("35", ((OrderViewModel.Split[])bugTrackingServiceArgs1[1])[0].Amount);
            Assert.AreEqual("SubAcct1", ((OrderViewModel.Split[])bugTrackingServiceArgs1[1])[0].SubAccount);
            Assert.AreEqual("$1.23", ((OrderViewModel)bugTrackingServiceArgs1[2]).Freight);
            #endregion Assert
        }
        public void TestRequestPostCustomFieldAnswersBinding3()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            new FakeCustomFields(3, RepositoryFactory.CustomFieldRepository);
            for (int i = 0; i < 3; i++)
            {
                RepositoryFactory.CustomFieldRepository.Expect(a => a.GetById(i + 1)).Return(
                    RepositoryFactory.CustomFieldRepository.Queryable.Single(b => b.Id == (i + 1)));
            }
            orderViewModel.CustomFields = new OrderViewModel.CustomField[3];
            orderViewModel.CustomFields[0] = new OrderViewModel.CustomField { Answer = "Answer1", Id = 1 };
            orderViewModel.CustomFields[1] = new OrderViewModel.CustomField { Answer = string.Empty, Id = 2 };
            orderViewModel.CustomFields[2] = new OrderViewModel.CustomField { Answer = "Answer3", Id = 3 };

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(2, ((Order)orderServiceArgs1[0]).CustomFieldAnswers.Count);
            Assert.AreEqual("Name1", ((Order)orderServiceArgs1[0]).CustomFieldAnswers[0].CustomField.Name);
            Assert.AreEqual("Name3", ((Order)orderServiceArgs1[0]).CustomFieldAnswers[1].CustomField.Name);
            Assert.AreEqual("Answer1", ((Order)orderServiceArgs1[0]).CustomFieldAnswers[0].Answer);
            Assert.AreEqual("Answer3", ((Order)orderServiceArgs1[0]).CustomFieldAnswers[1].Answer);
            #endregion Assert
        }
Example #13
0
        public ActionResult Edit(int id, OrderViewModel model)
        {
            var order = _repositoryFactory.OrderRepository.GetNullableById(id);

            Check.Require(order != null, "order is null");

            var adjustRouting = model.AdjustRouting.HasValue && model.AdjustRouting.Value;

            List<string> existingAccounts = new List<string>();
            if (adjustRouting)
            {
                foreach (var split in order.Splits)
                {
                    existingAccounts.Add(split.FullAccountDisplay);
                }
            }
            BindOrderModel(order, model, includeLineItemsAndSplits: adjustRouting);

            if (adjustRouting)
            {
                // Do we really need to adjust the routing?
                if (order.StatusCode.Id == OrderStatusCode.Codes.Purchaser)
                {
                    List<string> accountsNow = new List<string>();
                    foreach (var split in order.Splits)
                    {
                        accountsNow.Add(split.FullAccountDisplay);
                    }

                    if (!accountsNow.Except(existingAccounts).Union( existingAccounts.Except(accountsNow) ).Any())
                    {
                        adjustRouting = false;
                    }
                }
            }

            Check.Require(order.LineItems.Count > 0, "Line count");

            if(adjustRouting)
            {
            //TODO: Add expense validation
                //order.ValidateExpenses().ToArray();

                _orderService.ReRouteApprovalsForExistingOrder(order, approverId: model.Approvers, accountManagerId: model.AccountManagers);
            }
            else
            {
                _orderService.EditExistingOrder(order);
            }

            _repositoryFactory.OrderRepository.EnsurePersistent(order);

            //Do attachment stuff Note, must be done by persisting the attachment directly, not adding it to the order and having the order save it, otherwise we get a transient exception if we have to add a new user because of an external account
            if (model.FileIds != null)
            {
                foreach (var fileId in model.FileIds.Where(x => !Guid.Empty.Equals(x)))
                {
                    var attachment = _repositoryFactory.AttachmentRepository.GetById(fileId);
                    attachment.Order = order;
                    _repositoryFactory.AttachmentRepository.EnsurePersistent(attachment);
                }
            }

            Message = Resources.OrderEdit_Success;

            return RedirectToAction("Review", new { id });
        }
        public void TestRequestPostCommentsBinding4()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "2");
            new FakeUsers(3, RepositoryFactory.UserRepository);
            RepositoryFactory.UserRepository.Expect(a => a.GetNullableById("2")).Return(RepositoryFactory.UserRepository.Queryable.Single(b => b.Id == "2"));

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.25%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[1];
            orderViewModel.Items[0] = new OrderViewModel.LineItem();
            orderViewModel.Items[0].Description = "Test";
            orderViewModel.Items[0].Quantity = "0";
            orderViewModel.Items[0].Price = "0";

            orderViewModel.Comments = null;

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            OrderService.AssertWasCalled(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything));
            var orderServiceArgs1 = OrderService.GetArgumentsForCallsMadeOn(a => a.CreateApprovalsForNewOrder(Arg<Order>.Is.Anything, Arg<int[]>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<string>.Is.Anything))[0];
            Assert.AreEqual(0, ((Order)orderServiceArgs1[0]).OrderComments.Count);
            //Assert.AreEqual("This is my Comment", ((Order)orderServiceArgs1[0]).OrderComments[0].Text);
            //Assert.AreEqual(DateTime.UtcNow.ToPacificTime().Date, ((Order)orderServiceArgs1[0]).OrderComments[0].DateCreated.Date);
            //Assert.AreEqual("FirstName2", ((Order)orderServiceArgs1[0]).OrderComments[0].User.FirstName);
            #endregion Assert
        }
        public void TestRequestPostOrderSplits2()
        {
            #region Arrange
            Controller.ControllerContext.HttpContext = new MockHttpContext(0, new[] { "" }, "Me");

            new FakeWorkgroups(3, WorkgroupRepository);
            SecurityService.Expect(a => a.HasWorkgroupAccess(WorkgroupRepository.Queryable.Single(b => b.Id == 2))).Return(true);
            WorkgroupRepository.Expect(a => a.GetById(2)).Return(WorkgroupRepository.Queryable.Single(b => b.Id == 2)).Repeat.Any();

            var orderViewModel = new OrderViewModel();
            orderViewModel.Workgroup = 2;
            orderViewModel.Tax = "7.99%";
            orderViewModel.Shipping = "$2.35";
            orderViewModel.Freight = "$1.23";
            orderViewModel.Items = new OrderViewModel.LineItem[3];
            for (int i = 0; i < 3; i++)
            {
                orderViewModel.Items[i] = new OrderViewModel.LineItem();
                orderViewModel.Items[i].Price = "1.02";
                orderViewModel.Items[i].Quantity = (i + 1).ToString();
                orderViewModel.Items[i].Id = i;
            }

            orderViewModel.Items[1].Quantity = string.Empty;

            orderViewModel.SplitType = OrderViewModel.SplitTypes.None;
            orderViewModel.Account = "DifferentAcct";
            orderViewModel.SubAccount = "DiffSubAcct";
            orderViewModel.Project = "DifferentProj";

            orderViewModel.Splits = new OrderViewModel.Split[4];
            for (int i = 0; i < 4; i++)
            {
                orderViewModel.Splits[i] = new OrderViewModel.Split();
                orderViewModel.Splits[i].Account = string.Format("account{0}", i + 1);
                orderViewModel.Splits[i].Amount = "9.99";
            }

            orderViewModel.Splits[0].LineItemId = 0;
            orderViewModel.Splits[1].LineItemId = 0;
            orderViewModel.Splits[2].LineItemId = 2;
            orderViewModel.Splits[2].SubAccount = "SubAcct";
            orderViewModel.Splits[2].Project = "proj";
            orderViewModel.Splits[3].LineItemId = 2;
            orderViewModel.Splits[3].Account = string.Empty;

            #endregion Arrange

            #region Act
            Controller.Request(orderViewModel)
                .AssertActionRedirect()
                .ToAction<OrderController>(a => a.Review(2));
            #endregion Act

            #region Assert
            Assert.AreEqual(Resources.NewOrder_Success, Controller.Message);
            RepositoryFactory.OrderRepository.AssertWasCalled(a => a.EnsurePersistent(Arg<Order>.Is.Anything));
            var args = (Order)RepositoryFactory.OrderRepository.GetArgumentsForCallsMadeOn(a => a.EnsurePersistent(Arg<Order>.Is.Anything))[0][0];
            Assert.AreEqual(2, args.LineItems.Count);
            Assert.AreEqual(1, args.Splits.Count);
            Assert.AreEqual("DifferentAcct", args.Splits[0].Account);
            Assert.AreEqual("DiffSubAcct", args.Splits[0].SubAccount);
            Assert.AreEqual("DifferentProj", args.Splits[0].Project);
            Assert.AreEqual(null, args.Splits[0].LineItem);
            Assert.AreEqual(4.08m, args.Splits[0].Amount);
            Assert.AreEqual(4.08m, args.TotalFromDb);
            #endregion Assert
        }
Example #16
0
        public void CheckForClearedOutSubAccounts(Order order, OrderViewModel.Split[] splits, OrderViewModel model)
        {
            try
            {
                if (splits == null || !splits.Any())
                {
                    var orderViewModelSplit = new OrderViewModel.Split{Account = model.Account,SubAccount = model.SubAccount,LineItemId = null};
                    splits = new OrderViewModel.Split[] { orderViewModelSplit };
                }

                var lastLineId = 0;
                var count = 0;
                foreach (var split in splits)
                {
                    if (string.IsNullOrWhiteSpace(split.SubAccount))
                    {
                        Split orderSplit = null;
                        if (split.LineItemId.HasValue)
                        {
                            if (split.LineItemId.Value != lastLineId)
                            {
                                lastLineId = split.LineItemId.Value;
                                count = 0;
                            }
                            else
                            {
                                count++;
                            }
                            var lineItem = order.LineItems.ElementAtOrDefault(split.LineItemId.Value - 1);
                            if (lineItem != null)
                            {
                                orderSplit = lineItem.Splits.ElementAtOrDefault(count);
                            }
                        }
                        else
                        {
                            orderSplit = order.Splits.FirstOrDefault(a => a.LineItem == null && a.Account == split.Account);
                        }

                        if (orderSplit != null)
                        {
                            if (orderSplit.Account == split.Account && !string.IsNullOrWhiteSpace(orderSplit.SubAccount))
                            {
                                //ok, the sub account has been cleared out.
                                var bugTracking = new BugTracking();
                                bugTracking.OrderId = order.Id;
                                bugTracking.UserId = _userIdentity.Current;
                                bugTracking.SplitId = orderSplit.Id;
                                bugTracking.TrackingMessage = string.Format("SubAccount Cleared Out for Account {0}. Previous value {1}", orderSplit.Account, orderSplit.SubAccount);
                                bugTracking.LineItemId = orderSplit.LineItem != null ? (int?)orderSplit.LineItem.Id : null;

                                _bugTrackingRepository.EnsurePersistent(bugTracking);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }
Example #17
0
        private void BindOrderModel(Order order, OrderViewModel model, bool includeLineItemsAndSplits = false)
        {
            var workgroup = _repositoryFactory.WorkgroupRepository.GetById(model.Workgroup);

            //TODO: automapper?
            order.Vendor = model.Vendor == 0 ? null : _repositoryFactory.WorkgroupVendorRepository.GetById(model.Vendor);
            order.Address = _repositoryFactory.WorkgroupAddressRepository.GetById(model.ShipAddress);
            order.ShippingType = _repositoryFactory.ShippingTypeRepository.GetById(model.ShippingType);
            order.DateNeeded = model.DateNeeded;
            order.AllowBackorder = model.AllowBackorder;
            order.Workgroup = workgroup;
            order.Organization = workgroup.PrimaryOrganization; //TODO: why is this needed?
            order.DeliverTo = model.ShipTo;
            order.DeliverToEmail = model.ShipEmail;
            order.DeliverToPhone = model.ShipPhone;
            order.OrderType = order.OrderType ?? _repositoryFactory.OrderTypeRepository.GetById(OrderType.Types.OrderRequest);
            order.CreatedBy = order.CreatedBy ?? _repositoryFactory.UserRepository.GetById(CurrentUser.Identity.Name); //Only replace created by if it doesn't already exist
            order.Justification = model.Justification;
            order.BusinessPurpose = model.BusinessPurpose;
            order.RequestType = model.RequestType;

            if (!string.IsNullOrWhiteSpace(model.Comments))
            {
                var comment = new OrderComment
                {
                    DateCreated = DateTime.UtcNow.ToPacificTime(),
                    User = _repositoryFactory.UserRepository.GetById(CurrentUser.Identity.Name),
                    Text = model.Comments
                };

                order.AddOrderComment(comment);
            }

            if (model.CustomFields != null)
            {
                order.CustomFieldAnswers.Clear();

                foreach (var customField in model.CustomFields.Where(x => !string.IsNullOrWhiteSpace(x.Answer)))
                {
                    var answer = new CustomFieldAnswer
                    {
                        Answer = customField.Answer,
                        CustomField = _repositoryFactory.CustomFieldRepository.GetById(customField.Id)
                    };

                    order.AddCustomAnswer(answer);
                }
            }

            if (model.Restricted != null && model.Restricted.IsRestricted)
            {
                var restricted = new ControlledSubstanceInformation
                {

                    ClassSchedule = model.Restricted.Class,
                    Custodian = model.Restricted.Custodian,
                    EndUser = model.Restricted.Users,
                    StorageSite = model.Restricted.StorageSite,
                    Use = model.Restricted.Use,
                    PharmaceuticalGrade = model.Restricted.PharmaceuticalGrade
                };

                order.SetAuthorizationInfo(restricted);
            }
            else
            {
                order.ClearAuthorizationInfo();
            }

            if (includeLineItemsAndSplits)
            {
                _bugTrackingService.CheckForClearedOutSubAccounts(order, model.Splits, model);

                decimal number;
                order.EstimatedTax = decimal.TryParse(model.Tax != null ? model.Tax.TrimEnd('%') : null, out number) ? number : order.EstimatedTax;
                order.ShippingAmount = decimal.TryParse(model.Shipping != null ? model.Shipping.TrimStart('$') : null, out number) ? number : order.ShippingAmount;
                order.FreightAmount = decimal.TryParse(model.Freight != null ? model.Freight.TrimStart('$') : null, out number) ? number : order.FreightAmount;

                order.LineItems.Clear(); //replace line items and splits
                order.Splits.Clear();

                //Add in line items
                foreach (var lineItem in model.Items)
                {
                    if (lineItem.IsValid())
                    {
                        Commodity commodity = null;
                        if (!string.IsNullOrWhiteSpace(lineItem.CommodityCode))
                        {
                            commodity = _repositoryFactory.CommodityRepository.Queryable.SingleOrDefault(a => a.Id == lineItem.CommodityCode && a.IsActive);
                        }

                        //TODO: could use automapper later, but need to do validation
                        var orderLineItem = new LineItem
                        {
                            CatalogNumber = lineItem.CatalogNumber,
                            Commodity = commodity,
                            Description = lineItem.Description,
                            Notes = lineItem.Notes,
                            Quantity = decimal.Parse(lineItem.Quantity),
                            Unit = lineItem.Units,//TODO: shouldn't this link to UOM?
                            UnitPrice = decimal.Parse(lineItem.Price),
                            Url = lineItem.Url
                        };

                        order.AddLineItem(orderLineItem);

                        if (model.SplitType == OrderViewModel.SplitTypes.Line)
                        {
                            var lineItemId = lineItem.Id;

                            //Go through each split created for this line item
                            foreach (var split in model.Splits.Where(x => x.LineItemId == lineItemId))
                            {
                                if (split.IsValid())
                                {
                                    order.AddSplit(new Split
                                    {
                                        Account = split.Account,
                                        Amount = decimal.Parse(split.Amount),
                                        LineItem = orderLineItem,
                                        SubAccount = split.SubAccount,
                                        Project = split.Project
                                    });
                                }
                            }
                        }
                    }
                }

                //TODO: note that I am not checking an order split actually has valid splits, or that they add to the total.
                if (model.SplitType == OrderViewModel.SplitTypes.Order)
                {
                    foreach (var split in model.Splits)
                    {
                        if (split.IsValid())
                        {
                            order.AddSplit(new Split
                            {
                                Account = split.Account,
                                Amount = decimal.Parse(split.Amount),
                                SubAccount = split.SubAccount,
                                Project = split.Project
                            });
                        }
                    }
                }
                else if (model.SplitType == OrderViewModel.SplitTypes.None)
                {
                    order.AddSplit(new Split { Amount = order.Total(), Account = model.Account, SubAccount = model.SubAccount, Project = model.Project }); //Order with "no" splits get one split for the full amount
                }

                order.TotalFromDb = order.Total();
                order.LineItemSummary = order.GenerateLineItemSummary();
            }
        }