Sage / SData-2.0

Contains documents pertaining to the SData 2.0 developments
33 stars 10 forks source link

Invoice with multiple lines #62

Closed TawTeuf closed 8 years ago

TawTeuf commented 8 years ago

Hello, I am new to sage 50 sdata. I have gone through different comments from this blog and now I have begin to understand how to make invoices using sdata through c#. My Problem is that how can i input multiple items on the same invoice.

My Code is : public void AddInvoice() { salesInvoiceFeedEntry salesInvoice = new salesInvoiceFeedEntry();

        // Find a customer to associate with the new sales invoice
        salesInvoice.tradingAccount = GetCustomer();

        if (salesInvoice.tradingAccount == null)
        {
            // No customer record means we can go no further
            Console.WriteLine("Unable to find a customer record");
            Console.ReadKey(true);
            return;
        }

        string strProdCode = "";
        salesInvoiceLineFeedEntry orderLine = new salesInvoiceLineFeedEntry();

        salesInvoice.salesInvoiceLines = new salesInvoiceLineFeed();

        SDataRequest invoiceRequest = null;

        for (int i = 0; i < 2; i++)
        {
            if (i == 0)
                strProdCode = "P001";
            else
                strProdCode = "P002";

            // Lookup a commodity to use on the new sales invoice
            commodityFeedEntry commodity = GetCommodity(strProdCode);
            if (commodity == null)
            {
                // No commodity record means we go no further
                Console.WriteLine("Unable to find a commodity record");
                Console.ReadKey(true);
                return;
            }
            commodityFeedEntry commodityReference = new commodityFeedEntry();
            commodityReference.UUID = commodity.UUID;

            // Lookup a commodity to use on the new sales invoice
            taxCodeFeedEntry taxCode = GetTaxCode();
            if (taxCode == null)
            {
                // No record means we go no further
                Console.WriteLine("Unable to find a tax code record");
                Console.ReadKey(true);
                return;
            }

            taxCodeFeedEntry taxReference = new taxCodeFeedEntry();
            taxReference.UUID = taxCode.UUID;

            salesInvoice.taxCodes = new taxCodeFeed();
            salesInvoice.taxCodes.Entries.Add(taxReference);
            /*
            salesInvoice.carrierTotalPrice = 100;
            salesInvoice.carrierTaxPrice = 20;
            salesInvoice.carrierNetPrice = 80;
             * 
             * 
            */

            if (i == 0)
            {
                orderLine.type = "Standard";
                orderLine.text = commodity.description;
                orderLine.commodity = commodityReference;
                orderLine.quantity = 2;
                orderLine.netTotal = 50;
                orderLine.taxTotal = 10;
                orderLine.grossTotal = 60;
                orderLine.taxCodes = new taxCodeFeed();
                orderLine.taxCodes.Entries.Add(taxReference);
            }
            else
            {
                orderLine.type = "Standard";
                orderLine.text = commodity.description;
                orderLine.commodity = commodityReference;
                orderLine.quantity = 5;
                orderLine.netTotal = 100;
                orderLine.taxTotal = 10;
                orderLine.grossTotal = 110;
                orderLine.taxCodes = new taxCodeFeed();
                orderLine.taxCodes.Entries.Add(taxReference);
            }

            salesInvoice.salesInvoiceLines.Entries.Add(orderLine);

        }

            // Now we have constructed our new invoice we can submit it using the HTTP POST verb  
            Sage.Common.Syndication.SDataUri salesInvoiceUri = new Sage.Common.Syndication.SDataUri();
            salesInvoiceUri.BuildLocalPath("Accounts50", "GCRM", "-", "salesInvoices");

            invoiceRequest = new SDataRequest(salesInvoiceUri.Uri, salesInvoice, Sage.Integration.Messaging.Model.RequestVerb.POST);
            invoiceRequest.Username = "MANAGER";
            invoiceRequest.Password = "";

            // IF successful the POST operation will provide us with a the newly created sales invoice
            salesInvoiceFeedEntry savedSalesInvoice = new salesInvoiceFeedEntry();
            invoiceRequest.RequestFeedEntry<salesInvoiceFeedEntry>(savedSalesInvoice);

            if (invoiceRequest.IsStatusValidForVerb)
            {
                Console.WriteLine(string.Format("Successfully created sales invoice {0}", savedSalesInvoice.reference));

            }
            else
            {
                // There was a problem
                LogSyncErr("", "", "", "", "");

            }

            Console.ReadKey(true);

    }

its always saving the last product ( 2 lines with same item)

darroncockram commented 8 years ago

Looking at that code I'd fully expect it to be creating an invoice with 2 items every time. You have a for loop that will run for 2 iterations (you've hard coded this). In the for loop you are adding a line on each iteration with this line:

salesInvoice.salesInvoiceLines.Entries.Add(orderLine);

If you want to add more lines you need to construct further salesInvoiceLine instances, set the appropriate details on them and add them to the salesInvoice.salesInvoiceLines.Entries as per the above line.

Hope that helps.

asterix187 commented 8 years ago

Hi, This is because of the way you are adding the lines to the invoice object. You first need to add the lines to the lines feed then once all the lines are in the feed assign the feed to the lines collection.

Something like:

salesInvoiceFeedEntry salesInvoice = new salesInvoiceFeedEntry(); salesInvoiceLinesFeed orderLines = new salesInvoiceLineFeed();

for (int i = 0; i < 2; i++) { string strProdCode = ""; salesInvoiceLineFeedEntry orderLine = new salesInvoiceLineFeedEntry(); if (i == 0) strProdCode = "P001"; else strProdCode = "P002";

  orderLine.type = "Standard";
  orderLine.text = commodity.description;
  orderLine.commodity = commodityReference;
  orderLine.quantity = 2;
  orderLine.netTotal = 50;
  orderLine.taxTotal = 10;
  orderLine.grossTotal = 60;

  orderLines.Entries.Add(orderLine);

}

salesInvoice.salesInvoiceLines = orderLines;

Obviously not fully working but you should get the principal.

TawTeuf commented 8 years ago

Thanks you....will try it and revert back if i am having some issues

Sent from my Samsung device

-------- Original message -------- From: asterix187 notifications@github.com Date: 10/08/2016 22:49 (GMT+04:00) To: "Sage/SData-2.0" SData-2.0@noreply.github.com Cc: TawTeuf tawteuf@live.com, Author author@noreply.github.com Subject: Re: [Sage/SData-2.0] Invoice with multiple lines (#62)

Hi, This is because of the way you are adding the lines to the invoice object. You first need to add the lines to the lines feed then once all the lines are in the feed assign the feed to the lines collection.

Something like:

salesInvoiceFeedEntry salesInvoice = new salesInvoiceFeedEntry(); salesInvoiceLinesFeed orderLines = new salesInvoiceLineFeed();

for (int i = 0; i < 2; i++) { string strProdCode = ""; salesInvoiceLineFeedEntry orderLine = new salesInvoiceLineFeedEntry(); if (i == 0) strProdCode = "P001"; else strProdCode = "P002";

orderLine.type = "Standard"; orderLine.text = commodity.description; orderLine.commodity = commodityReference; orderLine.quantity = 2; orderLine.netTotal = 50; orderLine.taxTotal = 10; orderLine.grossTotal = 60;

salesInvoiceLinesFeed.Entries.Add(orderLine);

}

salesInvoice.salesInvoiceLines = salesInvoiceLinesFeed;

Obviously not working but you should get the principal.

You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Sage/SData-2.0/issues/62#issuecomment-238965454, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AT-2eGjFCANa_2h97gANYlarqEgf0bCiks5qeh0lgaJpZM4JhSX4.