示例#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);
        }
示例#2
0
        /**
         * Method that will remove price history from the material with the passed reference.
         * 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 (database);
         * FOREACH PRICE HISTORY RECEIVED {
         * 4. Validation of the existence of each price history received, in the material with the passed dto.
         * 5. Validation for duplication between received price history.
         * 6. Validation that the date of the price history is future
         * }
         */
        public ValidationOutput DeleteFinishPriceHistoryFromMaterial(string reference, string finishReference,
                                                                     IEnumerable <PriceHistoryDto> enumerableHistoryDto)
        {
            List <PriceHistoryDto>
            listPriceHistoryDto =
                new List <PriceHistoryDto>(
                    enumerableHistoryDto);     //Since we receive an IEnumerable, we need to have something concrete

            ValidationOutput validationOutput = new ValidationOutputBadRequest();

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

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

            Material            materialToModify     = _materialRepository.GetByReference(reference);
            List <PriceHistory> priceHistoryToDelete = new List <PriceHistory>();

            //3.
            if (!materialToModify.ContainsFinish(finishReference))
            {
                validationOutput.AddError("Finish",
                                          "Finish with the reference '" + finishReference + "' does not exist in material '" +
                                          reference + "'!");
                return(validationOutput);
            }

            Finish finishToModify = materialToModify.GetFinish(finishReference);

            foreach (var currentPriceHistoryDto in listPriceHistoryDto)
            {
                validationOutput = new ValidationOutputBadRequest();
                PriceHistory currentPriceHistory = _mapper.Map <PriceHistory>(currentPriceHistoryDto);

                //4.
                if (!finishToModify.ContainsPriceHistory(currentPriceHistory))
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date + "' does not exist in finish '" +
                                              finishReference + "'!");
                    return(validationOutput);
                }

                //5.
                if (priceHistoryToDelete.Contains(currentPriceHistory))
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date +
                                              "' is duplicated in the list of selected price history.");
                    return(validationOutput);
                }

                //6.
                if (currentPriceHistoryDto.Date.CompareTo(DateTime.Now) < 0)
                {
                    validationOutput.AddError("Price History",
                                              "Price History with the date '" + currentPriceHistory.Date +
                                              "' can't be deleted.");
                    return(validationOutput);
                }

                priceHistoryToDelete.Add(currentPriceHistory);
            }

            foreach (var priceToDelete in priceHistoryToDelete)
            {
                finishToModify.RemovePriceHistory(priceToDelete);
            }

            //Removes the old Finish
            materialToModify.RemoveFinish(materialToModify.GetFinish(finishReference));

            //Adds the new one
            materialToModify.AddFinish(finishToModify);


            _materialRepository.Update(materialToModify);
            return(validationOutput);
        }