public async Task <OutgoingInvoiceWithLines> UpdateInvoice(OutgoingInvoiceWithLines invoice)
        {
            var updateResponse = await AuthorizedPost("OutgoingInvoice", invoice);

            var updatedInvoice = await DeserializeScalarResponse <OutgoingInvoiceWithLines>(updateResponse, "outgoing invoice");

            return(updatedInvoice);
        }
Exemplo n.º 2
0
        private static string GetDeliveryNumber(OutgoingInvoiceWithLines powerofficeDelivery)
        {
            if (string.IsNullOrWhiteSpace(powerofficeDelivery.DocumentNo))
            {
                return(powerofficeDelivery.OrderNo.ToString());
            }

            return(powerofficeDelivery.DocumentNo);
        }
Exemplo n.º 3
0
        public DeliveryDto(
            OutgoingInvoiceWithLines powerofficeDelivery,
            int webcrmOrganisationId,
            PowerofficeConfiguration configuration)
        {
            DeliveryAssignedTo  = 0;
            DeliveryAssignedTo2 = 0;
            DeliveryResponsible = 0;

            Update(powerofficeDelivery, webcrmOrganisationId, configuration);
        }
Exemplo n.º 4
0
        public void Update(
            OutgoingInvoiceWithLines powerofficeDelivery,
            int webcrmOrganisationId,
            PowerofficeConfiguration configuration)
        {
            DeliveryErpSyncDateTime = DateTime.UtcNow;
            DeliveryNumber          = GetDeliveryNumber(powerofficeDelivery);
            DeliveryOrderDate       = powerofficeDelivery.OrderDate;
            DeliveryOrganisationId  = webcrmOrganisationId;
            DeliveryStatus          = ToWebcrmStatus(powerofficeDelivery.Status);

            SetPowerofficeDeliveryId(configuration.DeliveryIdFieldName, powerofficeDelivery.Id);
        }
Exemplo n.º 5
0
        public async Task CopyDeliveryFromPoweroffice(OutgoingInvoiceWithLines powerofficeDelivery)
        {
            var matchingWebcrmDelivery = await WebcrmClient.GetDeliveryByField(Configuration.DeliveryIdFieldName, powerofficeDelivery.Id.ToString());

            // Creating or updating a delivery in webCRM requires multiple calls to the API. If one of the call fails, the queue message will be retried, and this should make the incorrect state very temporary.
            int webcrmDeliveryId;
            int webcrmOrganisationId;

            if (matchingWebcrmDelivery == null)
            {
                if (!powerofficeDelivery.CustomerCode.HasValue)
                {
                    Logger.LogInformation("The PowerOffice delivery is not associated with a customer, so we cannot create it in webCRM.");
                    return;
                }

                // PowerOffice deliveries are associated with the Code of the organisation and not the Id.
                var webcrmOrganisation = await WebcrmClient.GetOrganisationByField(Configuration.OrganisationCodeFieldName, powerofficeDelivery.CustomerCode.ToString());

                if (webcrmOrganisation == null)
                {
                    var powerofficeOrganisation = await PowerofficeClient.GetCustomerByCode(powerofficeDelivery.CustomerCode.Value);

                    if (powerofficeOrganisation.IsArchived)
                    {
                        Logger.LogInformation("Not copying the delivery to webCRM because the organisation is archived and therefore not synchronised to webCRM.");
                        return;
                    }

                    if (powerofficeOrganisation.IsPerson)
                    {
                        Logger.LogInformation("Not copying the delivery to webCRM because the organisation is a person and therefore not synchronised to webCRM.");
                        return;
                    }

                    throw new ApplicationException($"Not copying the delivery to webCRM because no organisation was found with the PowerOffice code {powerofficeDelivery.CustomerCode}.");
                }

                Logger.LogTrace("Copying delivery from PowerOffice, creating a new one.");

                webcrmOrganisationId = webcrmOrganisation.OrganisationId;

                var newWebcrmDelivery = new DeliveryDto(powerofficeDelivery, webcrmOrganisation.OrganisationId, Configuration);
                webcrmDeliveryId = await WebcrmClient.CreateDelivery(newWebcrmDelivery);
            }
            else
            {
                if (!matchingWebcrmDelivery.DeliveryOrganisationId.HasValue)
                {
                    throw new ApplicationException("Not copying the delivery to webCRM because the matching webCRM delivery does not have a DeliveryOrganisationId.");
                }

                // Not comparing the two deliveries, since we're only synchronising deliveries one way.

                Logger.LogTrace("Copying delivery from PowerOffice, updating an existing one.");

                webcrmOrganisationId = matchingWebcrmDelivery.DeliveryOrganisationId.Value;

                // We cannot delete a delivery through the API, so we delete the associated delivery lines, but modify the delivery.
                var lines = await WebcrmClient.GetQuotationLines(matchingWebcrmDelivery.DeliveryId);

                Logger.LogTrace($"Deleting {lines.Count} associated quotation lines.");
                foreach (var line in lines)
                {
                    await WebcrmClient.DeleteQuotationLine(line.QuotationLineId);
                }

                matchingWebcrmDelivery.Update(powerofficeDelivery, matchingWebcrmDelivery.DeliveryOrganisationId.Value, Configuration);
                await WebcrmClient.UpdateDelivery(matchingWebcrmDelivery);

                webcrmDeliveryId = matchingWebcrmDelivery.DeliveryId;
            }

            // PowerOffice allow adding lines of text on the invoices. We do not synchronise these lines.
            var powerofficeLinesWithProductCode = powerofficeDelivery.OutgoingInvoiceLines
                                                  .Where(line => !string.IsNullOrWhiteSpace(line.ProductCode));

            Logger.LogTrace($"Copying {powerofficeLinesWithProductCode.Count()} quotation lines from PowerOffice.");
            const int millisecondsDelayBetweenCalls = 20;

            foreach (var powerofficeLine in powerofficeLinesWithProductCode)
            {
                var powerofficeProduct = await PowerofficeClient.GetProductByCode(powerofficeLine.ProductCode);

                double vatPercentage = await GetVatPercentage(powerofficeProduct);

                var webcrmLine = new QuotationLineDto(
                    powerofficeLine,
                    powerofficeProduct,
                    webcrmDeliveryId,
                    webcrmOrganisationId,
                    vatPercentage,
                    Configuration);

                await WebcrmClient.CreateQuotationLine(webcrmLine);

                await Task.Delay(millisecondsDelayBetweenCalls);
            }
        }