public void SaveInvoice(CampProductDTO campProductDto, string token) { var loggedInContact = _contactRepository.GetMyProfile(token); var family = _contactRepository.GetHouseholdFamilyMembers(loggedInContact.Household_ID); family.AddRange(_contactRepository.GetOtherHouseholdMembers(loggedInContact.Household_ID)); if (family.Where(f => f.ContactId == campProductDto.ContactId).ToList().Count <= 0) { throw new ContactNotFoundException(campProductDto.ContactId); } // set finainacial assistance flag in form response var participant = _participantRepository.GetParticipant(campProductDto.ContactId); var eventParticipantId = _eventRepository.GetEventParticipantRecordId(campProductDto.EventId, participant.ParticipantId); var answers = new List <MpFormAnswer> { new MpFormAnswer { Response = campProductDto.FinancialAssistance.ToString(), FieldId = _configurationWrapper.GetConfigIntValue("SummerCampForm.FinancialAssistance"), EventParticipantId = eventParticipantId } }; var formId = _configurationWrapper.GetConfigIntValue("SummerCampFormID"); var formResponse = new MpFormResponse { ContactId = campProductDto.ContactId, FormId = formId, FormAnswers = answers, EventId = campProductDto.EventId }; _formSubmissionRepository.SubmitFormResponse(formResponse); // if an invoice exists for this eventparticipant then don't create a new one if (_invoiceRepository.InvoiceExistsForEventParticipant(eventParticipantId)) { return; } // create the invoice with product from event and best pricing for the current date //get the product id for this event var campEvent = _eventRepository.GetEvent(campProductDto.EventId); var product = _productRepository.GetProductForEvent(campProductDto.EventId); var optionPrices = _productRepository.GetProductOptionPricesForProduct(product.ProductId); var productOptionPriceId = optionPrices.Count > 0 ? ConvertProductOptionPricetoDto(optionPrices, product.BasePrice, campEvent.EventStartDate) .Where(i => i.EndDate > DateTime.Now) .OrderBy(i => i.EndDate).FirstOrDefault()? .ProductOptionPriceId : (int?)null; _invoiceRepository.CreateInvoiceAndDetail(product.ProductId, productOptionPriceId, loggedInContact.Contact_ID, campProductDto.ContactId, eventParticipantId); }
public IHttpActionResult SaveProductDetails([FromBody] CampProductDTO campProductDto) { if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(val => val.Errors).Aggregate("", (current, err) => current + err.Exception.Message); var dataError = new ApiErrorDto("Product data Invalid", new InvalidOperationException("Product Data" + errors)); throw new HttpResponseException(dataError.HttpResponseMessage); } return(Authorized(token => { try { _campService.SaveInvoice(campProductDto, token); return Ok(); } catch (Exception e) { var apiError = new ApiErrorDto("Product Invoicing failed", e); throw new HttpResponseException(apiError.HttpResponseMessage); } })); }