示例#1
0
        public ConstructionSiteCalculationResponse Create(ConstructionSiteCalculationViewModel constructionSiteCalculationViewModel)
        {
            ConstructionSiteCalculationResponse response = new ConstructionSiteCalculationResponse();

            try
            {
                ConstructionSiteCalculation lastConstructionSiteCalculation = unitOfWork.GetConstructionSiteCalculationRepository()
                                                                              .GetLastConstructionSiteCalculation(constructionSiteCalculationViewModel.Company.Id, constructionSiteCalculationViewModel.ConstructionSite.Id);

                if (constructionSiteCalculationViewModel.PlusMinus == "+")
                {
                    decimal enteredValue = constructionSiteCalculationViewModel.NumOfEmployees * constructionSiteCalculationViewModel.NumOfMonths * constructionSiteCalculationViewModel.EmployeePrice;

                    constructionSiteCalculationViewModel.ValueDifference = (lastConstructionSiteCalculation?.ValueDifference ?? 0) - enteredValue;
                    if (constructionSiteCalculationViewModel.ValueDifference < 0)
                    {
                        constructionSiteCalculationViewModel.ValueDifference = 0;
                    }
                    decimal valueToAdd = enteredValue - (lastConstructionSiteCalculation?.ValueDifference ?? 0);
                    if (valueToAdd < 0)
                    {
                        valueToAdd = 0;
                    }
                    constructionSiteCalculationViewModel.NewValue = (lastConstructionSiteCalculation?.NewValue ?? 0) + valueToAdd;
                }
                else
                {
                    decimal enteredValue = constructionSiteCalculationViewModel.NumOfEmployees * constructionSiteCalculationViewModel.NumOfMonths * constructionSiteCalculationViewModel.EmployeePrice;

                    constructionSiteCalculationViewModel.ValueDifference = (lastConstructionSiteCalculation?.ValueDifference ?? 0) + enteredValue;
                    constructionSiteCalculationViewModel.NewValue        = lastConstructionSiteCalculation?.NewValue ?? 0;
                }

                constructionSiteCalculationViewModel.UpdatedAt = DateTime.Now.AddMilliseconds(1);

                ConstructionSiteCalculation addedConstructionSiteCalculation = unitOfWork.GetConstructionSiteCalculationRepository()
                                                                               .Create(constructionSiteCalculationViewModel.ConvertToConstructionSiteCalculation());
                unitOfWork.Save();
                response.ConstructionSiteCalculation = addedConstructionSiteCalculation.ConvertToConstructionSiteCalculationViewModel();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.ConstructionSiteCalculation = new ConstructionSiteCalculationViewModel();
                response.Success = false;
                response.Message = ex.Message;
            }
            return(response);
        }