public void NewCostCentreMustUseParentHeirarchyInNode()
        {
            //Arrange
            var ctx = CreateContext();
            mockCentres
                .Setup(set => set.Find(It.Is<object[]>(objects => (string) objects[0] == CostCentre1)))
                .Returns(() => centres[1]);
            mockCentres
                .Setup(set => set.Create())
                .Returns(new CostCentre())
                .Verifiable();
            mockCentres
                .Setup(set => set.Add(It.Is<CostCentre>(c => c.Node == new HierarchyId("/1/3/"))))
                .Verifiable();
            ctx
                .SetupGet(c => c.CostCentres)
                .Returns(mockCentres.Object);
            var sut = new CostCentreController(ctx.Object);
            var newCentre = new CostCentreModel
            {
                costCode = "new",
                parentCostCode = CostCentre1,
                name = "name",
                color = "FFFFFF",
                currencyCode = new Select2Model()
            };

            //Act
            sut.UpsertCostCentre(newCentre);

            //Assert
            mockCentres.Verify();
        }
Esempio n. 2
0
        public ActionResult UpsertCostCentre(CostCentreModel costCentre)
        {
            var costCode = costCentre.costCode;
            if ((string.IsNullOrEmpty(costCentre.name)) ||
                (string.IsNullOrEmpty(costCentre.color)))
            {
                return Json(new {costCode, success = false});
            }

            var parentCostCode = string.IsNullOrEmpty(costCentre.parentCostCode)
                ? Settings.Default.RootCostCentre
                : costCentre.parentCostCode;

            if (string.Equals(parentCostCode, costCode, StringComparison.InvariantCultureIgnoreCase))
            {
                return Json(new {costCode, success = false});
            }

            var update = true;
            var centre = context.CostCentres.Find(costCode);
            var parentCentre = context.CostCentres.Find(parentCostCode);
            if (parentCentre == null)
            {
                return Json(new {costCode, success = false});
            }
            var parentNode = parentCentre.Node;
            if (centre == null)
            {
                update = false;
                centre = context.CostCentres.Create();
                costCentre.orderId =
                    context
                        .CostCentres
                        .Count(centre1 => centre1.ParentCostCentreCostCode == parentCostCode) + 1;
                costCentre.node = parentNode.ToString() + costCentre.orderId + "/";
                centre.ParentCostCentreCostCode = parentCostCode;
                centre.CostCode = costCode;
            }

            centre.Node = new HierarchyId(costCentre.node);
            centre.Color = costCentre.color;
            centre.ConsumptionType = GetConsumptionType(costCentre.consumptionTypes);
            centre.CurrencyCode = costCentre.currencyCode.id;
            centre.Description = costCentre.description;
            centre.Name = costCentre.name;
            centre.OrderId = costCentre.orderId;

            if (!update)
            {
                context.CostCentres.Add(centre);
            }

            context.SaveChanges();

            return Json(new {costCode, success = true});
        }