API Forum

This forum is in read-only mode.
Please refer to our API support in case you have any questions.
We can be reached at api@e-conomic.com
e-conomic API developer forum

NopCommerce to Economic : Order to Cashbook (Invoice)

0
So, my third post about sending data from NopCommerce to E-Conomic.
This time I want to mention I'm using NopCommerce v1.90


// Makes cashbook entries for given order numbers
        private void CreateCashBookEntry()
        {
// A list of all ordernumbers you want to send to economic
            Listint ordernumbers = new Listint();


            ordernumbers.Add(798);
            ordernumbers.Add(802);
            ordernumbers.Add(830);



            // Lag ny Econonic session
            EconomicSession session = new EconomicSession();
            session.Connect(id, user, pass);


            foreach (int OrderId in ordernumbers)
            {
                /* Make invoice */
                Order order = this.OrderService.GetOrderById(OrderId);
                Customer customer = this.CustomerService.GetCustomerById(order.CustomerId);


                // Find customer
                IDebtor debtor = session.Debtor.FindByNumber(order.CustomerId.ToString());

                if (debtor == null)  // Customer does not exist, create!
                {
                    IDebtorGroup debtorGroup = session.DebtorGroup.FindByNumber(1);
                    IDebtorData debtorData = session.DebtorData.Create(order.CustomerId.ToString(), debtorGroup, order.BillingFirstName +   + order.BillingLastName, VatZone.HomeCountry);
                    debtorData.Currency = session.Currency.FindByCode(NOK);
                    debtorData.TelephoneAndFaxNumber = customer.PhoneNumber;
                    debtorData.Address = customer.ShippingAddress.Address1 +   + customer.ShippingAddress.Address2;
                    debtorData.PostalCode = customer.ShippingAddress.ZipPostalCode;
                    debtorData.City = customer.ShippingAddress.City;
                    debtorData.Country = customer.ShippingAddress.Country.Name;
                    debtorData.Email = customer.Email;
                    session.Debtor.CreateFromData(debtorData);
                }
                else
                {
                    // we got the debtor
                }


                // Create  invoice
                ICurrentInvoice _order = session.CurrentInvoice.Create(debtor);

                // Create invoice-lines
                foreach (OrderProductVariant opv in order.OrderProductVariants)
                {
                    IProduct _product = session.Product.FindByNumber(opv.ProductVariantId.ToString()); // Get product
                    if (_product == null)
                    {
                        // Create product, because we didn't find it
                        _product = session.Product.Create(opv.ProductVariantId.ToString(), session.ProductGroup.FindByNumber(1), opv.ProductVariant.Product.Name +  /  + opv.ProductVariant.Product.ShortDescription);
                        _product.SalesPrice = opv.ProductVariant.Price / (decimal)1.25;  // ex vat which is 25% here..
                        _product.IsAccessible = true;

                    }
                    else
                    {
                        // Dont create, we found it...
                    }

                    ICurrentInvoiceLine _orderLine = session.CurrentInvoiceLine.Create(_order);
                    _orderLine.DeliveryDate = order.ShippedDate;
                    _orderLine.Invoice.Currency = session.Currency.FindByCode(NOK);
                    _orderLine.Product = _product;
                    _orderLine.Product.Name = _product.Name;
                    _orderLine.Description = _product.Name;
                    _orderLine.Quantity = opv.Quantity;
                    _orderLine.UnitNetPrice = opv.UnitPriceExclTax;
                }

                // Payment fee line...
                if (order.PaymentMethodAdditionalFeeInclTax  0)
                {
                    ICurrentInvoiceLine _orderLine = session.CurrentInvoiceLine.Create(_order);
                    _orderLine.DeliveryDate = order.ShippedDate;
                    _orderLine.Invoice.Currency = session.Currency.FindByCode(NOK);
                    _orderLine.Product = session.Product.FindByNumber(0);
                    _orderLine.Product.Name = Fakturagebyr;
                    _orderLine.Description = Fakturagebyr;
                    _orderLine.Product.ProductGroup = session.ProductGroup.FindByNumber(1);
                    _orderLine.Quantity = 1;
                    _orderLine.UnitNetPrice = order.PaymentMethodAdditionalFeeInclTax / (decimal)1.25;
                }

                // shipping fee line
                if (order.OrderShippingInclTax  0)
                {
                    ICurrentInvoiceLine _orderLine = session.CurrentInvoiceLine.Create(_order);
                    _orderLine.DeliveryDate = order.ShippedDate;
                    _orderLine.Invoice.Currency = session.Currency.FindByCode(NOK);
                    _orderLine.Product = session.Product.FindByNumber(0);
                    _orderLine.Product.Name = Forsendelsesgebyr;
                    _orderLine.Description = Forsendelsesgebyr;
                    _orderLine.Product.ProductGroup = session.ProductGroup.FindByNumber(1);
                    _orderLine.Quantity = 1;
                    _orderLine.UnitNetPrice = order.OrderShippingInclTax / (decimal)1.25;
                }


                // Discounts and other fees line (to make amount exact)
                if (order.OrderTotal - _order.GrossAmount != 0)
                {
                    ICurrentInvoiceLine _orderLine = session.CurrentInvoiceLine.Create(_order);
                    _orderLine.DeliveryDate = order.ShippedDate;
                    _orderLine.Invoice.Currency = session.Currency.FindByCode(NOK);
                    _orderLine.Product = session.Product.FindByNumber(0);
                    _orderLine.Description = Øvrige Rabatter/Gebyrer og Avrunding;
                    _orderLine.Product.ProductGroup = session.ProductGroup.FindByNumber(1);
                    _orderLine.Quantity = 1;
                    _orderLine.UnitNetPrice = (order.OrderTotal - _order.GrossAmount) / (decimal)1.25;
                }



                // add reference to orderid and paymentmethod and trans id
                _order.TextLine1 = ORDRE # + order.OrderId.ToString();
                if (!String.IsNullOrEmpty(order.PaymentMethodName))
                {
                    _order.TextLine2 = order.PaymentMethodName;
                    _order.TextLine2 +=  # + order.AuthorizationTransactionId;
                }

                if (order.PaidDate.HasValue)
                {
                    _order.Date = order.PaidDate.Value;
                }
                if (order.ShippedDate.HasValue)
                {
                    _order.DeliveryDate = order.ShippedDate.Value;
                }


                //ICurrentInvoice _orderSaved = session.CurrentInvoice.CreateFromData(_order);
                int fakturanr = _order.Book().Number;
                OrderService.InsertOrderNote(order.OrderId, Invoice is now inserted to E-Conomic with invoice ID :  + fakturanr +  , DateTime.Now);

            }
            session.Disconnect();




        }
created Feb 19, 2013 by olejohan
0% Accept Rate
Q 4 A 1 C 0

Visma e-conomic A/S
...