private void LogSalesInvoice(SalesInvoiceDocument salesInvoiceDocument)
        {
            var url      = $"{Constants.ServerUrl}/api/resource/Sage 50 Export Log";
            var resource = new Resource(url);

            resource.LogSalesInvoice(salesInvoiceDocument);
        }
        private SalesInvoice _createNewSalesInvoice(SalesInvoiceDocument document, SalesInvoice salesInvoice,
                                                    EntityReference <Customer> customerEntityReference)
        {
            try
            {
                salesInvoice.CustomerReference           = customerEntityReference;
                salesInvoice.CustomerPurchaseOrderNumber = document.PoNo;
                salesInvoice.CustomerNote                = document.NotesOrSpecialInstructions;
                salesInvoice.Date                        = document.PostingDate;
                salesInvoice.DateDue                     = document.DueDate;
                salesInvoice.DiscountAmount              = document.DiscountAmount;
                salesInvoice.ReferenceNumber             = document.Name;
                salesInvoice.ShipDate                    = document.ShipDate;
                salesInvoice.ShipVia                     = document.ShippingMethod;
                salesInvoice.TermsDescription            = document.PaymentTermsTemplate;
                salesInvoice.CustomerPurchaseOrderNumber = document.PoNo;
                AddSalesRep(salesInvoice, document);
                AddShipAddress(salesInvoice);
                AddSalesOrderData(document, salesInvoice);

                salesInvoice.Save();
                Logger.Information("Sales Invoice - {@Name} was saved successfully", document.Name);
            }
            catch (Sage.Peachtree.API.Exceptions.RecordInUseException)
            {
                // abort. The unsaved data will eventually be re-queued
                salesInvoice = null;
                Logger.Debug("Record is in use. {@Name} will be sent back to the queue", document.Name);
            }
            catch (ArgumentException e)
            {
                salesInvoice = null;
                Logger.Debug("There was a problem with creating {@Name}. It will be sent back to the queue", document.Name);
                Logger.Debug("There error is {@E}", e.Message);
            }
            catch (Sage.Peachtree.API.Exceptions.ValidationException e)
            {
                Logger.Debug("Validation failed.");
                Logger.Debug(e.Message);
                if (e.ProblemList.OfType <DuplicateValueProblem>().Any(item => item.PropertyName == "ReferenceNumber"))
                {
                    Logger.Debug("{@Name} is already in Sage so will notify ERPNext", document.Name);
                }
                else
                {
                    Logger.Debug("{@Name} will be sent back to the queue", document.Name);
                    salesInvoice = null;
                }
            }
            catch (Exception e)
            {
                salesInvoice = null;
                Logger.Debug(e, e.Message);
                Logger.Debug("{@E}", e);
            }

            return(salesInvoice);
        }
        private void AddSalesRep(SalesInvoice salesInvoice, SalesInvoiceDocument document)
        {
            if (document.SalesRep == null)
            {
                return;
            }
            var salesRepRef = GetEmployeeEntityReference(document.SalesRep);

            if (salesRepRef == null)
            {
                Logger.Debug("Employee {@name} was not found in Sage.", document.SalesRep);
                throw new InvalidOperationException("Employee should not be null in sales invoice");
            }
            salesInvoice.SalesRepresentativeReference = (EntityReference <Employee>)salesRepRef;
        }
        private static IEnumerable <string> GetSalesOrderReferences(SalesInvoiceDocument salesInvoice)
        {
            var cache = new List <string>();

            // this seems more readable than a LINQ expression. Probably faster too.
            foreach (var item in salesInvoice.Items)
            {
                if (!cache.Contains(item.SalesOrder))
                {
                    cache.Add(item.SalesOrder);
                }
            }

            return(cache);
        }
Exemplo n.º 5
0
        public IRestResponse LogSalesInvoice(SalesInvoiceDocument document)
        {
            var log = new Log
            {
                document_name = document.Name,
                export_date   = DateTime.Now.ToString("yyyy-MM-dd"),
                document_date = document.PostingDate.ToString("yyyy-MM-dd"),
                document_type = "Sales Invoice"
            };
            var request = new RestRequest(Method.POST);

            request.AddJsonBody(log);
            var response = _restClient.Execute(request);

            return(response);
        }
        private void AddSalesOrderData(SalesInvoiceDocument invoiceDocument, SalesInvoice salesInvoice)
        {
            // if the references List below has more than one item, that could potentially cause problems.
            // In my chats with Sherri, I was assured invoices map to sales orders in a one-to-one manner.
            // Still, I'm assuming this 'contract' can be broken in which case, i'll let error handling
            // take over while the problem is discussed with EC
            var references = (List <string>)GetSalesOrderReferences(invoiceDocument);

            foreach (var reference in references)
            {
                if (!String.IsNullOrEmpty(reference))
                {
                    LoadSalesOrderFromName(reference, out var salesOrders);
                    // Sage takes just one Sales Order in the Sales invoice. SalesOrderList should only have one item
                    AddSalesOrderData(salesInvoice, reference, salesOrders.FirstOrDefault(), invoiceDocument.Items);
                }
            }
        }
        private SalesInvoice CreateNewSalesInvoice(SalesInvoiceDocument document)
        {
            var customerDocument        = GetCustomerFromErpNext(document.CustomerName, Logger);
            var salesInvoice            = Company.Factories.SalesInvoiceFactory.Create();
            var customerEntityReference = GetCustomerEntityReference(customerDocument?.OldCustomerId);

            if (customerEntityReference == null)
            {
                Logger.Debug("Customer {@name} in {@Document} was not found in Sage.", document.Customer,
                             document.Name);
                salesInvoice = null;
                SetNext(new CreateCustomerHandler(Company, Logger));
                Logger.Debug("Customer {@name} has been queued for creation in Sage", document.Customer);
            }
            else if (salesInvoice == null)
            {
                return(null);
            }
            salesInvoice = _createNewSalesInvoice(document, salesInvoice, customerEntityReference);
            return(salesInvoice);
        }