Пример #1
0
        /**
         * Method that will add prices that the finish with the passed reference in the material with, equally, the passed reference will have, in the future.
         * It is assumed that a list with 1 or more objects is received.
         *
         * Validations performed:
         * 1. Validation of the passed material's reference (database);
         * 2. The received list has 1 or more elements.
         * 3. Validation of the passed finish's reference (existence in the material);
         * FOREACH PRICE HISTORY RECEIVED {
         * 4. Validation of each price history's definition (business rules);
         * 6. Validation of the existence of each price history received, in the the finish of material with the passed reference.
         * 5. Validation for duplication between received price history items.
         * }
         */
        public ValidationOutput AddPriceHistoryItemsToFinishOfMaterial(string materialReference, string finishReference,
                                                                       IEnumerable <PriceHistoryDto> enumerablePriceHistoryDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            List <PriceHistoryDto> listPriceHistoryDto = new List <PriceHistoryDto>(enumerablePriceHistoryDto);

            //1.
            validationOutput = new ValidationOutputNotFound();
            if (!MaterialExists(materialReference))
            {
                validationOutput.AddError("Reference of material",
                                          "No material with the reference '" + materialReference + "' exists in the system.");
                return(validationOutput);
            }

            //2.
            if (listPriceHistoryDto.Count == 0)
            {
                validationOutput.AddError("Price history items defined", "No price history items were defined!");
                return(validationOutput);
            }

            Material materialToModify = _materialRepository.GetByReference(materialReference);

            //3.
            validationOutput = new ValidationOutputNotFound();
            if (!materialToModify.ContainsFinish(finishReference))
            {
                validationOutput.AddError("Reference of finish",
                                          "No finish with the reference '" + finishReference + "' exists in the material '" +
                                          materialReference + "'.");
                return(validationOutput);
            }

            Finish finishToModify = materialToModify.GetFinish(finishReference);

            List <PriceHistory> priceHistoryItemsToAdd = new List <PriceHistory>();

            validationOutput = new ValidationOutputBadRequest();
            foreach (var currentPriceHistoryDto in listPriceHistoryDto)
            {
                //4.
                validationOutput = _priceHistoryDTOValidator.DTOIsValid(currentPriceHistoryDto);
                if (validationOutput.HasErrors())
                {
                    return(validationOutput);
                }

                PriceHistory currentPriceHistory = _mapper.Map <PriceHistory>(currentPriceHistoryDto);

                //5.
                if (finishToModify.ContainsPriceHistory(currentPriceHistory))
                {
                    validationOutput.AddError("Price history item",
                                              "A price history item set to the date " + currentPriceHistory.Date + " with the price " +
                                              currentPriceHistory.Price.Value + " has already been defined in the finish '" +
                                              finishReference +
                                              "' present in the material '" + materialReference + "'!");
                    return(validationOutput);
                }

                //6.
                if (priceHistoryItemsToAdd.Contains(currentPriceHistory))
                {
                    validationOutput.AddError("Price history item",
                                              "A price history item is duplicated in the list of defined price history items.");
                    return(validationOutput);
                }

                priceHistoryItemsToAdd.Add(currentPriceHistory);
            }

            foreach (var priceHistoryItemToAdd in priceHistoryItemsToAdd)
            {
                finishToModify.AddPriceToHistory(priceHistoryItemToAdd);
            }

            validationOutput.DesiredReturn = enumerablePriceHistoryDto;
            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }