Categories: QuickBooks Desktop

How To Add Invoice In Quickbook Desktop Using C#

Introduction

In this article, we will learn how to add invoices in Quickbook Desktop using C#.

If you have not seen Quickbook Desktop Installation then I recommend you to see that first. Here.

First of all Install the Interop.QBFC13 package in your project.

Install Package

Right click on your project and select Manage Nuget Packages…

Then search Interop.QBFC13 and install it.

The code is as below.

public void AddInvoice()
{
    try
    {
        QBSessionManager sessionManager = new QBSessionManager();
        IMsgSetRequest requestSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
        requestSet.Attributes.OnError = ENRqOnError.roeStop;
        sessionManager.OpenConnection(@"Your company file path", "IDN InvoiceAdd C# sample");
        sessionManager.BeginSession("", ENOpenMode.omDontCare);
        string customerName = "sagar rana";
        string itemName = "Computer";
        double amount = 1500;
        IMsgSetResponse responseSet;
        //check customer exist or not
        if (!IsCustomerExistsOrNot(customerName))
        {
            //create customer
            ICustomerAdd newcustomer = requestSet.AppendCustomerAddRq();
            newcustomer.Name.SetValue(customerName);
            responseSet = sessionManager.DoRequests(requestSet);
            requestSet.ClearRequests();
        }

        //check item exist or not
        if (!IsCheckItemExitsOrNot(itemName))
        {
            //create item
            IItemNonInventoryAdd newitem = requestSet.AppendItemNonInventoryAddRq();
            newitem.Name.SetValue(itemName);
            newitem.ORSalesPurchase.SalesAndPurchase.Type.Equals("Non-inventory Part");
            newitem.ORSalesPurchase.SalesAndPurchase.PurchaseCost.SetValue(Convert.ToDouble(amount));
            newitem.ORSalesPurchase.SalesAndPurchase.SalesPrice.SetValue(Convert.ToDouble(amount));
            newitem.ORSalesPurchase.SalesAndPurchase.IncomeAccountRef.FullName.SetValue("Payroll Liabilities");
            newitem.ORSalesPurchase.SalesAndPurchase.ExpenseAccountRef.FullName.SetValue("Payroll Liabilities");
            responseSet = sessionManager.DoRequests(requestSet);
            requestSet.ClearRequests();
        }

        //create invoice
        IInvoiceAdd invoiceAdd = requestSet.AppendInvoiceAddRq();
        invoiceAdd.CustomerRef.FullName.SetValue(customerName);//existing customer name
        invoiceAdd.TxnDate.SetValue(DateTime.Now);
        invoiceAdd.DueDate.SetValue(DateTime.Now.AddDays(50));
        IInvoiceLineAdd invoiceLineAdd = invoiceAdd.ORInvoiceLineAddList.Append().InvoiceLineAdd;

        //invoice item
        invoiceLineAdd.ItemRef.FullName.SetValue(itemName);//existing item name
        invoiceLineAdd.ORRatePriceLevel.Rate.SetValue(amount);
        invoiceLineAdd.Quantity.SetValue(Convert.ToDouble(1));
        invoiceLineAdd.Amount.SetValue(amount);
        responseMsgSet = sessionManager.DoRequests(requestSet);
        IResponse response = responseMsgSet.ResponseList.GetAt(0);
        requestSet.ClearRequests();
        MessageBox.Show("Invoice created successfully.");

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
public bool IsCustomerExistsOrNot(string customerName)
{
    bool isExists = false;
    try
    {
        IMsgSetRequest requestSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
        ICustomerQuery customerQueryRq = requestSet.AppendCustomerQueryRq();
        IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestSet);
        IResponse response = responseMsgSet.ResponseList.GetAt(0);
        ICustomerRetList customerRetList = (ICustomerRetList)response.Detail;

        List<string> customers = new List<string>();
        if (customerRetList != null)
        {
            for (int i = 0; i < customerRetList.Count; i++)
            {
                ICustomerRet customerRet = customerRetList.GetAt(i);

                customers.Add(customerRet.Name.GetValue());
            }
        }
        isExists = customers.Any(x => x == customerName);
    }
    catch (Exception ex)
    {
        isExists = false;
    }
    return isExists;
}
public bool IsCheckItemExitsOrNot(string itemName)
{
    bool isExists = false;
    QBSessionManager sessionManager = new QBSessionManager();
    try
    {
        sessionManager.OpenConnection(CompanyFilePath, "IDN InvoiceAdd C# sample");
        sessionManager.BeginSession("", ENOpenMode.omDontCare);
        IMsgSetRequest requestSet = sessionManager.CreateMsgSetRequest("US", 13, 0);
        IItemNonInventoryQuery _IItemNonInventoryQuery = requestSet.AppendItemNonInventoryQueryRq();
        IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestSet);
        IResponse response = responseMsgSet.ResponseList.GetAt(0);

        IItemNonInventoryRetList itemRetList = (IItemNonInventoryRetList)response.Detail;
        List<string> items = new List<string>();
        for (int i = 0; i < itemRetList.Count; i++)
        {
            if (itemRetList != null)
            {
                IItemNonInventoryRet itemInventoryRet = itemRetList.GetAt(i);
                items.Add(itemInventoryRet.Name.GetValue());
            }
        }
        isExists = items.Any(x => x == itemName);

    }
    catch (Exception ex)
    {
        isExists = false;
    }
    return isExists;
}

 

If you have any questions about this article, please let me know and for more information click here.

Also, check Sales Order in Quickbook Desktop and Purchase Order in Quickbook Desktop

Sagar Rana

Sagar Rana is a Web Developer in Vision Infotech. He has strong skills and knowledge of ASP.NET C#, ASP.NET MVC, .Net Core, Jquery, JavaScript, WEB API, React.js, ADO.Net, Entity Framework, SQL and different integration like Xero, Stripe, Zoho CRM, Square, PayTM, PayKUN, RazorPay, Quickbook Desktop etc.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

3 years ago