public async Task <FileStream> CreateAndStoreOfferDocumentAsync(int offerId)
        {
            var includeQuery = new QuerySet();

            includeQuery.Include.Fields = new string[] {
                "offerlines", "offerlines.vat-rate", "customer", "request", "contact", "building"
            };
            var offer = await _offerDataProvider.GetByIdAsync(offerId, includeQuery);

            await EmbedCustomerAndContactTelephonesAsync(offer);

            string visitorInitials = null;

            if (offer.Request != null)
            {
                visitorInitials = await GetVisitorInitialsByOfferIdAsync(offer.Id);
            }

            dynamic documentData = new ExpandoObject();

            documentData.Offer   = offer;
            documentData.Visitor = visitorInitials;

            var url      = $"{_documentGenerationConfig.BaseUrl}/documents/offer";
            var filePath = ConstructOfferDocumentFilePath(offer);

            return(await GenerateAndStoreDocumentAsync(url, documentData, filePath));
        }
Exemple #2
0
        // Embed relations in request resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelations(Offerline offerline, Offerline oldOfferline = null)
        {
            try {
                if (offerline.VatRate != null)
                {
                    if (oldOfferline != null && oldOfferline.VatRate != null && oldOfferline.VatRate.Id == offerline.VatRate.Id)
                    {
                        offerline.VatRate = oldOfferline.VatRate;
                    }
                    else
                    {
                        offerline.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(offerline.VatRate.Id));
                    }
                }

                // Offer cannot be updated. Take Offer of oldOfferline on update.
                if (oldOfferline != null)
                {
                    offerline.Offer = oldOfferline.Offer;
                }
                else
                {
                    offerline.Offer = await _offerDataProvider.GetByIdAsync(offerline.Offer.Id);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }
Exemple #3
0
 public async Task <Offer> GetByIdAsync(int id, QuerySet query)
 {
     return(await _offerDataProvider.GetByIdAsync(id, query));
 }
Exemple #4
0
        // Embed relations in request resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelations(Order order, Order oldOrder = null)
        {
            try {
                if (order.VatRate != null)
                {
                    if (oldOrder != null && oldOrder.VatRate != null && oldOrder.VatRate.Id == order.VatRate.Id)
                    {
                        order.VatRate = oldOrder.VatRate;
                    }
                    else
                    {
                        order.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(order.VatRate.Id));
                    }
                }

                // Customer cannot be updated. Take customer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Customer = oldOrder.Customer;
                }
                else
                {
                    order.Customer = await _customerDataProvider.GetByNumberAsync(order.Customer.Id);
                }

                // Offer cannot be updated. Take offer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Offer = oldOrder.Offer;
                }
                else
                {
                    order.Offer = await _offerDataProvider.GetByIdAsync(order.Offer.Id);
                }

                // Invoice cannot be updated. Take invoice of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Invoice = oldOrder.Invoice;
                }
                else
                {
                    order.Invoice = null;
                }

                var includeCustomer = new QuerySet();
                includeCustomer.Include.Fields = new string[] { "customer" };

                // Contact can only be updated through CaseManager. Take contact of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Contact = oldOrder.Contact;
                }
                else if (order.Contact != null)
                {
                    order.Contact = await _contactDataProvider.GetByIdAsync(order.Contact.Id, includeCustomer);
                }

                // Building can only be updated through CaseManager. Take building of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Building = oldOrder.Building;
                }
                else if (order.Building != null)
                {
                    order.Building = await _buildingDataProvider.GetByIdAsync(order.Building.Id, includeCustomer);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }
Exemple #5
0
        // Embed relations in request resource: reuse old relation if there is one and it hasn't changed
        private async Task EmbedRelations(Order order, Order oldOrder = null)
        {
            try {
                if (order.VatRate != null)
                {
                    if (oldOrder != null && oldOrder.VatRate != null && oldOrder.VatRate.Id == order.VatRate.Id)
                    {
                        order.VatRate = oldOrder.VatRate;
                    }
                    else
                    {
                        order.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(order.VatRate.Id));
                    }
                }
                else
                {
                    // Order must have a VAT rate. Take VAT rate of oldOrder if none is passed.
                    if (oldOrder != null)
                    {
                        order.VatRate = oldOrder.VatRate;
                        _logger.LogDebug("Received an update for order {0} without a VAT rate", order.Id);
                    }
                }

                var technicians = new List <Employee>();
                foreach (var technicianRelation in order.Technicians)
                {
                    var technician = await _employeeDataProvider.GetByIdAsync(technicianRelation.Id);

                    technicians.Add(technician);
                }
                order.Technicians = technicians;

                // Customer cannot be updated. Take customer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Customer = oldOrder.Customer;
                }
                else
                {
                    order.Customer = await _customerDataProvider.GetByNumberAsync(order.Customer.Id);
                }

                // Offer cannot be updated. Take offer of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Offer = oldOrder.Offer;
                }
                else
                {
                    order.Offer = await _offerDataProvider.GetByIdAsync(order.Offer.Id);
                }

                // Invoice cannot be updated. Take invoice of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Invoice = oldOrder.Invoice;
                }
                else
                {
                    order.Invoice = null;
                }

                var includeCustomer = new QuerySet();
                includeCustomer.Include.Fields = new string[] { "customer" };

                // Contact can only be updated through CaseManager. Take contact of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Contact = oldOrder.Contact;
                }
                else if (order.Contact != null)
                {
                    order.Contact = await _contactDataProvider.GetByIdAsync(order.Contact.Id, includeCustomer);
                }

                // Building can only be updated through CaseManager. Take building of oldOrder on update.
                if (oldOrder != null)
                {
                    order.Building = oldOrder.Building;
                }
                else if (order.Building != null)
                {
                    order.Building = await _buildingDataProvider.GetByIdAsync(order.Building.Id, includeCustomer);
                }
            }
            catch (EntityNotFoundException)
            {
                _logger.LogDebug($"Failed to find a related entity");
                throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist.");
            }
        }