Пример #1
0
        public void ensureTableEntryIsntEqualToInstanceOfOtherType()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            Assert.False(instance.Equals("bananas"));
        }
Пример #2
0
        public void ensureInstanceIsCreated()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(20),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            Assert.NotNull(instance);
        }
Пример #3
0
        /// <summary>
        /// Calculates the price of all child customized products of a given customized product
        /// </summary>
        /// <param name="customizedProduct">Parent Customized Product</param>
        /// <param name="fetchCustomizedProductPrice">Information about currency/area conversion</param>
        /// <param name="materialPriceTableRepository">MaterialPriceTableRepository instance to fetch current material prices</param>
        /// <param name="finishPriceTableRepository">FinishPriceTableRepository instance to fetch current finish prices</param>
        /// <param name="clientFactory">Injected HTTP Client Factory</param>
        /// <returns>IEnumerable containing the prices of all child customized products</returns>
        private static async Task <IEnumerable <CustomizedProductPriceModelView> > calculatePricesOfChildCustomizedProducts(List <CustomizedProductPriceModelView> childCustomizedProductPrices, CustomizedProduct customizedProduct, FetchCustomizedProductPriceModelView fetchCustomizedProductPrice,
                                                                                                                            MaterialPriceTableRepository materialPriceTableRepository, FinishPriceTableRepository finishPriceTableRepository, IHttpClientFactory clientFactory)
        {
            foreach (Slot slot in customizedProduct.slots)
            {
                foreach (CustomizedProduct childCustomizedProduct in slot.customizedProducts)
                {
                    MaterialPriceTableEntry materialPriceTableEntry =
                        getCurrentMaterialPrice(materialPriceTableRepository, childCustomizedProduct.customizedMaterial.material.Id);

                    FinishPriceTableEntry finishPriceTableEntry = null;

                    if (childCustomizedProduct.customizedMaterial.finish != null)
                    {
                        finishPriceTableEntry = getCurrentFinishPrice(finishPriceTableRepository,
                                                                      childCustomizedProduct.customizedMaterial.material.Finishes.Where(f => f.Equals(childCustomizedProduct.customizedMaterial.finish)).SingleOrDefault().Id);
                    }

                    CustomizedProductPriceModelView childCustomizedProductPriceModelView =
                        await buildCustomizedProductPriceModelView(childCustomizedProduct, fetchCustomizedProductPrice,
                                                                   materialPriceTableEntry, finishPriceTableEntry, clientFactory);

                    childCustomizedProductPrices.Add(childCustomizedProductPriceModelView);

                    if (childCustomizedProduct.hasCustomizedProducts())
                    {
                        await calculatePricesOfChildCustomizedProducts(childCustomizedProductPrices, childCustomizedProduct,
                                                                       fetchCustomizedProductPrice, materialPriceTableRepository, finishPriceTableRepository, clientFactory);
                    }
                }
            }

            return(childCustomizedProductPrices);
        }
Пример #4
0
        public void ensureTableEntryIsEqualToItself()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            Assert.True(instance.Equals(instance));
        }
        public static GetCurrentMaterialFinishPriceModelView fromMaterialFinish(GetCurrentMaterialFinishPriceModelView modelView, IHttpClientFactory clientFactory)
        {
            Material material = PersistenceContext.repositories().createMaterialRepository().find(modelView.finish.materialId);

            if (material == null)
            {
                throw new ResourceNotFoundException(string.Format(MATERIAL_NOT_FOUND, modelView.finish.materialId));
            }

            MaterialPriceTableEntry currentMaterialPrice = PersistenceContext.repositories().createMaterialPriceTableRepository().fetchCurrentMaterialPrice(modelView.finish.materialId);

            if (currentMaterialPrice == null)
            {
                throw new ResourceNotFoundException(string.Format(NO_CURRENT_MATERIAL_PRICE, modelView.finish.materialId));
            }

            foreach (Finish finish in material.Finishes)
            {
                if (finish.Id == modelView.finish.id)
                {
                    FinishPriceTableEntry currentMaterialFinishPrice = PersistenceContext.repositories().createFinishPriceTableRepository().fetchCurrentMaterialFinishPrice(modelView.finish.id);

                    if (currentMaterialFinishPrice == null)
                    {
                        throw new ResourceNotFoundException(string.Format(NO_CURRENT_FINISH_PRICE, modelView.finish.id, modelView.finish.materialId));
                    }

                    GetCurrentMaterialFinishPriceModelView currentMaterialFinishPriceModelView = new GetCurrentMaterialFinishPriceModelView();
                    currentMaterialFinishPriceModelView.finish                  = new GetMaterialFinishModelView();
                    currentMaterialFinishPriceModelView.currentPrice            = new PriceModelView();
                    currentMaterialFinishPriceModelView.timePeriod              = new TimePeriodModelView();
                    currentMaterialFinishPriceModelView.tableEntryId            = currentMaterialFinishPrice.Id;
                    currentMaterialFinishPriceModelView.finish.materialId       = material.Id;
                    currentMaterialFinishPriceModelView.finish.id               = finish.Id;
                    currentMaterialFinishPriceModelView.finish.description      = finish.description;
                    currentMaterialFinishPriceModelView.finish.shininess        = finish.shininess;
                    currentMaterialFinishPriceModelView.timePeriod.startingDate = LocalDateTimePattern.GeneralIso.Format(currentMaterialFinishPrice.timePeriod.startingDate);
                    currentMaterialFinishPriceModelView.timePeriod.endingDate   = LocalDateTimePattern.GeneralIso.Format(currentMaterialFinishPrice.timePeriod.endingDate);
                    if (modelView.currentPrice.currency == null || modelView.currentPrice.area == null)
                    {
                        currentMaterialFinishPriceModelView.currentPrice.value    = currentMaterialFinishPrice.price.value;
                        currentMaterialFinishPriceModelView.currentPrice.area     = CurrencyPerAreaConversionService.getBaseArea();
                        currentMaterialFinishPriceModelView.currentPrice.currency = CurrencyPerAreaConversionService.getBaseCurrency();
                    }
                    else
                    {
                        Task <double> convertedValueTask =
                            new CurrencyPerAreaConversionService(clientFactory)
                            .convertDefaultCurrencyPerAreaToCurrencyPerArea(currentMaterialFinishPrice.price.value, modelView.currentPrice.currency, modelView.currentPrice.area);
                        convertedValueTask.Wait();
                        currentMaterialFinishPriceModelView.currentPrice.value    = convertedValueTask.Result;
                        currentMaterialFinishPriceModelView.currentPrice.currency = modelView.currentPrice.currency;
                        currentMaterialFinishPriceModelView.currentPrice.area     = modelView.currentPrice.area;
                    }
                    return(currentMaterialFinishPriceModelView);
                }
            }
            throw new ResourceNotFoundException(string.Format(FINISH_NOT_FOUND, modelView.finish.id, modelView.finish.materialId));
        }
Пример #6
0
        public void ensureEqualTableEntriesHaveEqualHashCodes()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());
            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createFinish());

            Assert.Equal(instance.GetHashCode(), other.GetHashCode());
        }
Пример #7
0
        public void ensureTableEntriesWithDifferentFinishesArentEqual()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());
            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createOtherFinish());

            Assert.False(instance.Equals(other));
        }
Пример #8
0
        public void ensureTableEntriesWithEqualPropertiesAreEqual()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());
            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createFinish());

            Assert.True(instance.Equals(other));
        }
Пример #9
0
        public void ensureToStringWorks()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());
            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createFinish());

            Assert.Equal(instance.ToString(), other.ToString());
        }
Пример #10
0
        public void ensureChangeTimePeriodDoesntChangeTimePeriodIfNewTimePeriodIsNull()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(20),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            Action act = () => instance.changeTimePeriod(null);

            Assert.Throws <ArgumentException>(act);
        }
Пример #11
0
        public void ensureSameAsReturnsFalseForDifferentEntities()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hello", price: Price.valueOf(10),
                                                                    timePeriod: createOtherTimePeriod(), finish: createFinish());

            Assert.False(instance.sameAs(other.eId));
        }
Пример #12
0
        public void ensureIdReturnsTheBusinessIdentifier()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createFinish());


            Assert.Equal(instance.id(), other.id());
        }
Пример #13
0
        public void ensureSameAsReturnsTrueForEqualEntities()
        {
            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            FinishPriceTableEntry other = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                    timePeriod: createTimePeriod(), finish: createFinish());


            Assert.True(instance.sameAs(other.eId));
        }
Пример #14
0
        public void ensureChangeTimePeriodChangesTimePeriod()
        {
            TimePeriod oldTimePeriod = createTimePeriod();
            TimePeriod newTimePeriod = createOtherTimePeriod();

            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: Price.valueOf(10),
                                                                       timePeriod: oldTimePeriod, finish: createFinish());

            instance.changeTimePeriod(newTimePeriod);

            Assert.NotEqual(oldTimePeriod, instance.timePeriod);
        }
Пример #15
0
        public void ensureChangePriceChangesPrice()
        {
            Price oldPrice = Price.valueOf(20);
            Price newPrice = Price.valueOf(30);

            FinishPriceTableEntry instance = new FinishPriceTableEntry(materialEID: "hi", price: oldPrice,
                                                                       timePeriod: createTimePeriod(), finish: createFinish());

            instance.changePrice(newPrice);

            Assert.NotEqual(oldPrice, instance.price);
        }
        /// <summary>
        /// Creates a model view with a material finish price information
        /// </summary>
        /// <param name="materialFinishPriceTableEntry">MaterialFinishPriceTableEntry with the material finish price</param>
        /// <returns>GetMaterialFinishPriceModelView with the material finish price information model view</returns>
        public static GetMaterialFinishPriceModelView fromMaterialFinishEntity(FinishPriceTableEntry materialFinishPriceTableEntry, string currency, string area)
        {
            GetMaterialFinishPriceModelView getMaterialFinishPriceModelView = new GetMaterialFinishPriceModelView();

            getMaterialFinishPriceModelView.finishId     = materialFinishPriceTableEntry.entity.Id;
            getMaterialFinishPriceModelView.id           = materialFinishPriceTableEntry.Id;
            getMaterialFinishPriceModelView.value        = materialFinishPriceTableEntry.price.value;
            getMaterialFinishPriceModelView.currency     = currency;
            getMaterialFinishPriceModelView.area         = area;
            getMaterialFinishPriceModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(materialFinishPriceTableEntry.timePeriod.startingDate);
            getMaterialFinishPriceModelView.endingDate   = LocalDateTimePattern.GeneralIso.Format(materialFinishPriceTableEntry.timePeriod.endingDate);
            return(getMaterialFinishPriceModelView);
        }
Пример #17
0
        /// <summary>
        /// Checks if a material finish from a customized product has a current price
        /// </summary>
        /// <param name="finishPriceTableRepository">Finish Price Table Repository</param>
        /// <param name="finishId">Finish's PID</param>
        /// <returns>FinishPriceTableEntry with the finish's current price</returns>
        private static FinishPriceTableEntry getCurrentFinishPrice(FinishPriceTableRepository finishPriceTableRepository, long finishId)
        {
            FinishPriceTableEntry finishPriceTableEntry =
                finishPriceTableRepository.fetchCurrentMaterialFinishPrice(finishId);

            if (finishPriceTableEntry == null)
            {
                throw new ResourceNotFoundException
                      (
                          string.Format(FINISH_HAS_NO_CURRENT_PRICE, finishId)
                      );
            }

            return(finishPriceTableEntry);
        }
Пример #18
0
        /// <summary>
        /// Builds a CustomizedProductPriceModelView out of a Customized Product
        /// </summary>
        /// <param name="customizedProduct">Customized Product to build the model view out of</param>
        /// <param name="fetchCustomizedProductPriceModelView">ModelView to know if currency/area conversion is needed</param>
        /// <returns>CustomizedProductPriceModelView</returns>
        private static async Task <CustomizedProductPriceModelView> buildCustomizedProductPriceModelView(CustomizedProduct customizedProduct, FetchCustomizedProductPriceModelView fetchCustomizedProductPriceModelView,
                                                                                                         MaterialPriceTableEntry materialPriceTableEntry, FinishPriceTableEntry finishPriceTableEntry, IHttpClientFactory clientFactory)
        {
            CustomizedProductPriceModelView customizedProductPriceModelView = new CustomizedProductPriceModelView();

            string defaultCurrency        = CurrencyPerAreaConversionService.getBaseCurrency();
            string defaultArea            = CurrencyPerAreaConversionService.getBaseArea();
            bool   convertCurrencyPerArea = fetchCustomizedProductPriceModelView.currency != null && fetchCustomizedProductPriceModelView.area != null;

            customizedProductPriceModelView.customizedProductId = customizedProduct.Id;
            customizedProductPriceModelView.reference           = customizedProduct.reference;
            customizedProductPriceModelView.productId           = customizedProduct.product.Id;
            string requestedMeasurementUnit = null;

            if (convertCurrencyPerArea)
            {
                requestedMeasurementUnit = new String(fetchCustomizedProductPriceModelView.area.Where(c => Char.IsLetter(c)).ToArray());
            }
            else
            {
                requestedMeasurementUnit = new String(CurrencyPerAreaConversionService.getBaseArea().Where(c => Char.IsLetter(c)).ToArray());
            }
            customizedProductPriceModelView.customizedDimensions       = new GetCustomizedDimensionsModelView();
            customizedProductPriceModelView.customizedDimensions.unit  = requestedMeasurementUnit;
            customizedProductPriceModelView.customizedDimensions.width =
                MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.width, requestedMeasurementUnit);
            customizedProductPriceModelView.customizedDimensions.height =
                MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.height, requestedMeasurementUnit);
            customizedProductPriceModelView.customizedDimensions.depth =
                MeasurementUnitService.convertToUnit(customizedProduct.customizedDimensions.depth, requestedMeasurementUnit);

            customizedProductPriceModelView.customizedMaterial = new CustomizedMaterialPriceModelView();
            customizedProductPriceModelView.customizedMaterial.customizedMaterialId = customizedProduct.customizedMaterial.Id;
            customizedProductPriceModelView.customizedMaterial.materialId           = customizedProduct.customizedMaterial.material.Id;
            if (customizedProduct.customizedMaterial.finish != null)
            {
                customizedProductPriceModelView.customizedMaterial.finish             = new FinishPriceModelView();
                customizedProductPriceModelView.customizedMaterial.finish.finishId    = customizedProduct.customizedMaterial.finish.Id;
                customizedProductPriceModelView.customizedMaterial.finish.description = customizedProduct.customizedMaterial.finish.description;
                customizedProductPriceModelView.customizedMaterial.finish.shininess   = customizedProduct.customizedMaterial.finish.shininess;
                customizedProductPriceModelView.customizedMaterial.finish.price       = new PriceModelView();
                if (convertCurrencyPerArea)
                {
                    customizedProductPriceModelView.customizedMaterial.finish.price.currency =
                        fetchCustomizedProductPriceModelView.currency;
                    customizedProductPriceModelView.customizedMaterial.finish.price.area =
                        fetchCustomizedProductPriceModelView.area;
                    customizedProductPriceModelView.customizedMaterial.finish.price.value =
                        await convertPriceValue(
                            finishPriceTableEntry.price.value, fetchCustomizedProductPriceModelView.currency,
                            fetchCustomizedProductPriceModelView.area, clientFactory
                            );
                }
                else
                {
                    customizedProductPriceModelView.customizedMaterial.finish.price.currency = defaultCurrency;
                    customizedProductPriceModelView.customizedMaterial.finish.price.area     = defaultArea;
                    customizedProductPriceModelView.customizedMaterial.finish.price.value    = finishPriceTableEntry.price.value;
                }
            }
            if (customizedProduct.customizedMaterial.color != null)
            {
                customizedProductPriceModelView.customizedMaterial.color = ColorModelViewService.fromEntity(customizedProduct.customizedMaterial.color);
            }
            customizedProductPriceModelView.customizedMaterial.price = new PriceModelView();
            if (convertCurrencyPerArea)
            {
                customizedProductPriceModelView.customizedMaterial.price.currency =
                    fetchCustomizedProductPriceModelView.currency;
                customizedProductPriceModelView.customizedMaterial.price.area =
                    fetchCustomizedProductPriceModelView.area;
                customizedProductPriceModelView.customizedMaterial.price.value =
                    await convertPriceValue(
                        materialPriceTableEntry.price.value, fetchCustomizedProductPriceModelView.currency,
                        fetchCustomizedProductPriceModelView.area, clientFactory
                        );
            }
            else
            {
                customizedProductPriceModelView.customizedMaterial.price.currency = defaultCurrency;
                customizedProductPriceModelView.customizedMaterial.price.area     = defaultArea;
                customizedProductPriceModelView.customizedMaterial.price.value    = materialPriceTableEntry.price.value;
            }

            customizedProductPriceModelView.price     = new PriceModelView();
            customizedProductPriceModelView.totalArea = new AreaModelView();

            if (convertCurrencyPerArea)
            {
                customizedProductPriceModelView.price.currency = fetchCustomizedProductPriceModelView.currency;
                customizedProductPriceModelView.totalArea.area = fetchCustomizedProductPriceModelView.area;
            }
            else
            {
                customizedProductPriceModelView.price.currency = defaultCurrency;
                customizedProductPriceModelView.totalArea.area = defaultArea;
            }

            calculateTotalAreaAndPriceOfCustomizedMaterial(customizedProductPriceModelView, customizedProduct);

            return(customizedProductPriceModelView);
        }
        /// <summary>
        /// Updates a finish's price table entry
        /// </summary>
        /// <param name="modelView">model view containing updatable information</param>
        /// <returns>True if the update is successful</returns>
        public static GetMaterialFinishPriceModelView update(UpdateFinishPriceTableEntryModelView modelView, IHttpClientFactory clientFactory)
        {
            string             defaultCurrency    = CurrencyPerAreaConversionService.getBaseCurrency();
            string             defaultArea        = CurrencyPerAreaConversionService.getBaseArea();
            MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository();
            long materialId = modelView.entityId;

            bool performedAtLeastOneUpdate = false;

            Material material = materialRepository.find(materialId);

            if (material == null)
            {
                throw new ResourceNotFoundException(MATERIAL_NOT_FOUND);
            }

            foreach (Finish finish in material.Finishes)
            {
                if (finish.Id == modelView.finishId)
                {
                    FinishPriceTableRepository finishPriceTableRepository = PersistenceContext.repositories().createFinishPriceTableRepository();
                    long finishPriceTableEntryId = modelView.tableEntryId;

                    FinishPriceTableEntry tableEntryToUpdate = finishPriceTableRepository.find(finishPriceTableEntryId);

                    if (tableEntryToUpdate == null)
                    {
                        throw new ResourceNotFoundException(ENTRY_NOT_FOUND);
                    }

                    if (tableEntryToUpdate.entity.Id != modelView.finishId)
                    {
                        throw new InvalidOperationException(ENTRY_DOESNT_BELONG_TO_FINISH);
                    }

                    LocalDateTime currentTime = NodaTime.LocalDateTime.FromDateTime(SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc());

                    if (modelView.priceTableEntry.startingDate != null && LocalDateTimePattern.GeneralIso.Format(tableEntryToUpdate.timePeriod.startingDate).Equals(modelView.priceTableEntry.startingDate))
                    {
                        if (tableEntryToUpdate.timePeriod.startingDate.CompareTo(currentTime) < 0)
                        {
                            throw new InvalidOperationException(PAST_DATE);
                        }
                    }

                    if (modelView.priceTableEntry.endingDate != null && LocalDateTimePattern.GeneralIso.Format(tableEntryToUpdate.timePeriod.endingDate).Equals(modelView.priceTableEntry.endingDate))
                    {
                        if (tableEntryToUpdate.timePeriod.endingDate.CompareTo(currentTime) < 0)
                        {
                            throw new InvalidOperationException(PAST_DATE);
                        }
                    }

                    if (modelView.priceTableEntry.price != null)
                    {
                        CurrenciesService.checkCurrencySupport(modelView.priceTableEntry.price.currency);
                        AreasService.checkAreaSupport(modelView.priceTableEntry.price.area);

                        Price newPrice = null;
                        try
                        {
                            if (defaultCurrency.Equals(modelView.priceTableEntry.price.currency) && defaultArea.Equals(modelView.priceTableEntry.price.area))
                            {
                                newPrice = Price.valueOf(modelView.priceTableEntry.price.value);
                            }
                            else
                            {
                                Task <double> convertedValueTask = new CurrencyPerAreaConversionService(clientFactory)
                                                                   .convertCurrencyPerAreaToDefaultCurrencyPerArea(
                                    modelView.priceTableEntry.price.currency,
                                    modelView.priceTableEntry.price.area,
                                    modelView.priceTableEntry.price.value);
                                convertedValueTask.Wait();
                                double convertedValue = convertedValueTask.Result;
                                newPrice = Price.valueOf(convertedValue);
                            }
                        }
                        catch (HttpRequestException)
                        {
                            newPrice = Price.valueOf(modelView.priceTableEntry.price.value);
                        }

                        tableEntryToUpdate.changePrice(newPrice);
                        performedAtLeastOneUpdate = true;
                    }

                    if (modelView.priceTableEntry.endingDate != null)
                    {
                        LocalDateTime newEndingDate;
                        try
                        {
                            string newEndingDateAsString = modelView.priceTableEntry.endingDate;
                            newEndingDate = LocalDateTimePattern.GeneralIso.Parse(newEndingDateAsString).GetValueOrThrow();
                            tableEntryToUpdate.changeTimePeriod(TimePeriod.valueOf(tableEntryToUpdate.timePeriod.startingDate, newEndingDate));
                            performedAtLeastOneUpdate = true;
                        }
                        catch (UnparsableValueException)
                        {
                            throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText);
                        }
                    }

                    if (modelView.priceTableEntry.startingDate != null)
                    {
                        LocalDateTime newStartingDate;
                        try
                        {
                            string newStartingDateAsString = modelView.priceTableEntry.startingDate;
                            newStartingDate = LocalDateTimePattern.GeneralIso.Parse(newStartingDateAsString).GetValueOrThrow();
                            tableEntryToUpdate.changeTimePeriod(TimePeriod.valueOf(newStartingDate, tableEntryToUpdate.timePeriod.endingDate));
                            performedAtLeastOneUpdate = true;
                        }
                        catch (UnparsableValueException)
                        {
                            throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText);
                        }
                    }

                    if (performedAtLeastOneUpdate)
                    {
                        FinishPriceTableEntry updatedTableEntry = finishPriceTableRepository.update(tableEntryToUpdate);

                        if (updatedTableEntry == null)
                        {
                            throw new InvalidOperationException(UPDATE_NOT_SUCCESSFUL);
                        }

                        GetMaterialFinishPriceModelView updatedTableEntryModelView = new GetMaterialFinishPriceModelView();

                        updatedTableEntryModelView.id           = updatedTableEntry.Id;
                        updatedTableEntryModelView.finishId     = updatedTableEntry.entity.Id;
                        updatedTableEntryModelView.value        = updatedTableEntry.price.value;
                        updatedTableEntryModelView.currency     = defaultCurrency;
                        updatedTableEntryModelView.area         = defaultArea;
                        updatedTableEntryModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(updatedTableEntry.timePeriod.startingDate);
                        updatedTableEntryModelView.endingDate   = LocalDateTimePattern.GeneralIso.Format(updatedTableEntry.timePeriod.endingDate);

                        return(updatedTableEntryModelView);
                    }
                }
            }
            throw new ResourceNotFoundException(FINISH_NOT_FOUND_OR_DOESNT_BELONG_TO_MATERIAL);
        }
Пример #20
0
        /// <summary>
        /// Calculates the price
        /// </summary>
        /// <param name="fetchCustomizedProductPrice">FetchCustomizedProductModelView with the necessary information to fetch the customized product's price</param>
        /// <returns>CustomizedProductPriceModelView with the price of the customized product</returns>
        public static async Task <CustomizedProductFinalPriceModelView> calculatePrice(FetchCustomizedProductPriceModelView fetchCustomizedProductPrice, IHttpClientFactory clientFactory)
        {
            if (fetchCustomizedProductPrice.currency != null)
            {
                CurrenciesService.checkCurrencySupport(fetchCustomizedProductPrice.currency);
            }

            if (fetchCustomizedProductPrice.area != null)
            {
                AreasService.checkAreaSupport(fetchCustomizedProductPrice.area);
            }

            CustomizedProduct customizedProduct =
                PersistenceContext.repositories().createCustomizedProductRepository().find(fetchCustomizedProductPrice.id);

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException
                      (
                          string.Format(CUSTOMIZED_PRODUCT_NOT_FOUND, fetchCustomizedProductPrice.id)
                      );
            }

            if (customizedProduct.status == CustomizationStatus.PENDING)
            {
                throw new ArgumentException
                      (
                          CUSTOMIZED_PRODUCT_NOT_FINISHED
                      );
            }

            //TODO Should the fetching of prices of customized products that aren't base products be allowed?

            MaterialPriceTableRepository materialPriceTableRepository =
                PersistenceContext.repositories().createMaterialPriceTableRepository();
            FinishPriceTableRepository finishPriceTableRepository =
                PersistenceContext.repositories().createFinishPriceTableRepository();

            CustomizedProductFinalPriceModelView   customizedProductTotalPrice = new CustomizedProductFinalPriceModelView();
            List <CustomizedProductPriceModelView> customizedProductPriceList  = new List <CustomizedProductPriceModelView>();

            MaterialPriceTableEntry materialPriceTableEntry =
                getCurrentMaterialPrice(materialPriceTableRepository, customizedProduct.customizedMaterial.material.Id);

            FinishPriceTableEntry finishPriceTableEntry = null;

            if (customizedProduct.customizedMaterial.finish != null)
            {
                finishPriceTableEntry = getCurrentFinishPrice(finishPriceTableRepository,
                                                              customizedProduct.customizedMaterial.material.Finishes.Where(f => f.Equals(customizedProduct.customizedMaterial.finish)).SingleOrDefault().Id);
            }

            //TODO What should we do about doors?
            //TODO Should we consider that every component has a box geometry?
            //!For now the surface area of every product is being calculated as if it the product was a SIX FACED RIGHT RECTANGULAR PRISM

            CustomizedProductPriceModelView parentCustomizedProductModelView =
                await buildCustomizedProductPriceModelView(customizedProduct, fetchCustomizedProductPrice, materialPriceTableEntry, finishPriceTableEntry, clientFactory);

            customizedProductPriceList.Add(parentCustomizedProductModelView);

            if (customizedProduct.hasCustomizedProducts())
            {
                List <CustomizedProductPriceModelView> childCustomizedProducts = new List <CustomizedProductPriceModelView>();
                customizedProductPriceList.AddRange(await
                                                    calculatePricesOfChildCustomizedProducts(childCustomizedProducts, customizedProduct, fetchCustomizedProductPrice,
                                                                                             materialPriceTableRepository, finishPriceTableRepository, clientFactory));
            }

            customizedProductTotalPrice.customizedProducts = customizedProductPriceList;
            customizedProductTotalPrice.finalPrice         = calculateFinalPriceOfCustomizedProduct(customizedProductTotalPrice, fetchCustomizedProductPrice);

            return(customizedProductTotalPrice);
        }
        /// <summary>
        /// Transforms and creates a finish price table entry
        /// </summary>
        /// <param name="modelView">model view with the necessary info to create a finish price table entry</param>
        /// <param name="clientFactory">injected client factory</param>
        /// <returns></returns>
        public static GetMaterialFinishPriceModelView create(AddFinishPriceTableEntryModelView modelView, IHttpClientFactory clientFactory)
        {
            string             defaultCurrency    = CurrencyPerAreaConversionService.getBaseCurrency();
            string             defaultArea        = CurrencyPerAreaConversionService.getBaseArea();
            MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository();
            long materialId = modelView.entityId;

            Material material = materialRepository.find(materialId);

            if (material == null)
            {
                throw new ResourceNotFoundException(MATERIAL_NOT_FOUND);
            }

            //TODO Is this null check enough? Should we check ,if an entry exists, that the time period of the price entry is valid?
            MaterialPriceTableEntry materialPriceTableEntry = PersistenceContext.repositories().createMaterialPriceTableRepository().find(materialId);

            if (materialPriceTableEntry == null)
            {
                throw new InvalidOperationException(MATERIAL_HAS_NO_PRICE);
            }

            foreach (Finish finish in material.Finishes)
            {
                if (finish.Id == modelView.finishId)
                {
                    string startingDateAsString = modelView.priceTableEntry.startingDate;
                    string endingDateAsString   = modelView.priceTableEntry.endingDate;

                    LocalDateTime startingDate;
                    LocalDateTime endingDate;
                    LocalDateTime currentTime = NodaTime.LocalDateTime.FromDateTime(SystemClock.Instance.GetCurrentInstant().ToDateTimeUtc());

                    try
                    {
                        startingDate = LocalDateTimePattern.GeneralIso.Parse(startingDateAsString).GetValueOrThrow();

                        if (startingDate.CompareTo(currentTime) < 0)
                        {
                            throw new InvalidOperationException(PAST_DATE);
                        }
                    }
                    catch (UnparsableValueException)
                    {
                        throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText);
                    }

                    TimePeriod timePeriod = null;

                    if (endingDateAsString != null)
                    {
                        try
                        {
                            endingDate = LocalDateTimePattern.GeneralIso.Parse(endingDateAsString).GetValueOrThrow();

                            if (endingDate.CompareTo(currentTime) < 0)
                            {
                                throw new InvalidOperationException(PAST_DATE);
                            }

                            timePeriod = TimePeriod.valueOf(startingDate, endingDate);
                        }
                        catch (UnparsableValueException)
                        {
                            throw new UnparsableValueException(DATES_WRONG_FORMAT + LocalDateTimePattern.GeneralIso.PatternText);
                        }
                    }
                    else
                    {
                        timePeriod = TimePeriod.valueOf(startingDate);
                    }

                    CurrenciesService.checkCurrencySupport(modelView.priceTableEntry.price.currency);
                    AreasService.checkAreaSupport(modelView.priceTableEntry.price.area);

                    Price price = null;
                    try
                    {
                        if (defaultCurrency.Equals(modelView.priceTableEntry.price.currency) && defaultArea.Equals(modelView.priceTableEntry.price.area))
                        {
                            price = Price.valueOf(modelView.priceTableEntry.price.value);
                        }
                        else
                        {
                            Task <double> convertedValueTask = new CurrencyPerAreaConversionService(clientFactory)
                                                               .convertCurrencyPerAreaToDefaultCurrencyPerArea(
                                modelView.priceTableEntry.price.currency,
                                modelView.priceTableEntry.price.area,
                                modelView.priceTableEntry.price.value);
                            convertedValueTask.Wait();
                            double convertedValue = convertedValueTask.Result;
                            price = Price.valueOf(convertedValue);
                        }
                    }
                    catch (HttpRequestException)
                    {
                        price = Price.valueOf(modelView.priceTableEntry.price.value);
                    }

                    FinishPriceTableEntry finishPriceTableEntry      = new FinishPriceTableEntry(material.id(), finish, price, timePeriod);
                    FinishPriceTableEntry savedFinishPriceTableEntry = PersistenceContext.repositories()
                                                                       .createFinishPriceTableRepository().save(finishPriceTableEntry);

                    if (savedFinishPriceTableEntry == null)
                    {
                        throw new InvalidOperationException(PRICE_TABLE_ENTRY_NOT_CREATED);
                    }

                    GetMaterialFinishPriceModelView createdPriceModelView = new GetMaterialFinishPriceModelView();

                    createdPriceModelView.startingDate = LocalDateTimePattern.GeneralIso.Format(savedFinishPriceTableEntry.timePeriod.startingDate);
                    createdPriceModelView.endingDate   = LocalDateTimePattern.GeneralIso.Format(savedFinishPriceTableEntry.timePeriod.endingDate);
                    createdPriceModelView.value        = savedFinishPriceTableEntry.price.value;
                    createdPriceModelView.currency     = defaultCurrency;
                    createdPriceModelView.area         = defaultArea;
                    createdPriceModelView.finishId     = finish.Id;
                    createdPriceModelView.id           = savedFinishPriceTableEntry.Id;

                    return(createdPriceModelView);
                }
            }
            throw new ResourceNotFoundException(FINISH_NOT_FOUND);
        }