How To Add Paid Invoice In Quickbook Desktop Using C#

Introduction

In this article, we will learn How To Add Paid Invoice In Quickbook Desktop Using C#.

If you have not seen Quickbook Desktop Installation then I recommend you to see that first. in this article here.

Please check article like Add Invoice.

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

Install Package

Right click of your project and select Manage Nuget Packages.

After search Interop.QBFC13 and install it.

First, create an invoice so that invoice is paid.

After creating the invoice successfully so that apply the below code. so that invoice will be paid successfully.

Paid Invoice Code is as below.

IReceivePaymentAdd receivePaymentAdd = requestSet.AppendReceivePaymentAddRq();
receivePaymentAdd.CustomerRef.FullName.SetValue("");//existing customer name
receivePaymentAdd.ARAccountRef.FullName.SetValue("Accounts Receivable");
receivePaymentAdd.TotalAmount.SetValue(0);//total amount
receivePaymentAdd.ORApplyPayment.IsAutoApply.SetValue(true);
responseMsgSet = sessionManager.DoRequests(requestSet);
IResponse responsea = responseMsgSet.ResponseList.GetAt(0);
requestSet.ClearRequests();

Create Paid Invoice 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();
                IReceivePaymentAdd receivePaymentAdd = requestSet.AppendReceivePaymentAddRq();
                receivePaymentAdd.CustomerRef.FullName.SetValue(item.Name);
                receivePaymentAdd.ARAccountRef.FullName.SetValue("Accounts Receivable");
                receivePaymentAdd.TotalAmount.SetValue(Convert.ToDouble(item.Amount));
                receivePaymentAdd.ORApplyPayment.IsAutoApply.SetValue(true);
                responseMsgSet = sessionManager.DoRequests(requestSet);
                IResponse responsea = 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;
        }

Also, check Sales Order in Quickbook Desktop and Purchase Order in Quickbook Desktop and also here.

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

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories