In this blog, I will explain to you how to get a purchase order as PDF and download it in .NET MVC web application using SDK.
Before using any Quickbooks online API we need access token, if you don’t know how to get access token then you can find it here.
We will download purchase order pdf by purchase order Id.
For that, we have to get a “purchase order” form Quickbooks Online and call GetPdf function. it will return a byte array of pdf, we have to convert byte array to a pdf file and return pdf file to a user.
Below are a few steps to get a purchase order as PDF,
public FileResult GetPurchaseOrderPdfById(string PurchaseOrderID) { byte[] PurchaseOrderPdfByteArray; try { OAuth2RequestValidator oauthValidator = new OAuth2RequestValidator(Access_token); // Create a ServiceContext with Auth tokens and realmId ServiceContext serviceContext = new ServiceContext(RealmId, IntuitServicesType.QBO, oauthValidator); serviceContext.IppConfiguration.MinorVersion.Qbo = "23"; serviceContext.IppConfiguration.BaseUrl.Qbo = QboBaseUrl; string EXISTING_PURCHASEORDER_QUERYBYID = string.Format("select * from PurchaseOrder where id = '{0}'", PurchaseOrderID); var queryService = new QueryService<PurchaseOrder>(serviceContext); PurchaseOrder objPurchaseOrderFound = queryService.ExecuteIdsQuery(EXISTING_PURCHASEORDER_QUERYBYID).FirstOrDefault<PurchaseOrder>(); //If Purchase Order found on Quickbooks online if (objPurchaseOrderFound != null) { DataService dataService = new DataService(serviceContext); //it will return Byte Array of pdf PurchaseOrderPdfByteArray = dataService.GetPdf<PurchaseOrder>(objPurchaseOrderFound); if (PurchaseOrderPdfByteArray != null) { //This is for unique name format like-"INVOICE-148-11_05_2019114648". var CurrentDate = DateTime.Now; var FileName = string.Format("{0}-{1}-{2}{3}{4}{5}{6}", "PURCHASEORDER", PurchaseOrderID, CurrentDate.ToString("MM/dd/yyyy"), CurrentDate.ToString("HH"), CurrentDate.ToString("mm"), CurrentDate.ToString("ss"), ".pdf"); return File(PurchaseOrderPdfByteArray, "application/pdf", FileName); } } return null; } catch (IdsException ex) { return null; } catch (Exception ex) { return null; } }
You can set a download link in the list view of the purchase order. if you don’t know how to get a purchase order from Quickbooks Online you can find it here.
@model List<Intuit.Ipp.Data.PurchaseOrder> @using Intuit.Ipp.Data; @{ ViewBag.Title = "GetAllPurchaseOrder"; } <h2>Quickbooks online Purchase Order List</h2> <div> <table class="table table-bordered"> @*<tr style="border: 2px solid black;background-color:#a9a9a9"> <th>QBO Purchase Order ID</th> <th>Purchase Order Number</th> <th>Purchase Order Date</th> <th>Vendor</th> <th>Total Amount</th> </tr>*@ @foreach (var item in Model) { <tr style="border: 2px solid black;border-bottom: none;background-color:#a9a9a9"> <th>QBO Purchase Order ID</th> <th>Purchase Order Number</th> <th>Purchase Order Date</th> <th>Vendor</th> <th>Total Amount</th> <th style="text-align:center;">Download</th> </tr> <tr style="border: 2px solid black;border-bottom: none;border-top: none;"> <td>@item.Id</td> <td>@item.DocNumber</td> <td>@item.TxnDate.ToString("MM/dd/yyyy")</td> <td>@item.VendorRef.name</td> <td>@item.TotalAmt</td> <td style="text-align:center;"><a href="@Url.Action("GetPurchaseOrderPdfById", "Home", new { PurchaseOrderID = item.Id })" target="_blank"><img src="~/Content/Images/downloadICON.png" height="25" width="25" /></a></td> </tr> <tr style="border: 2px solid black;border-top: none;"> @*<td>Items Details</td>*@ <td colspan="7"> <table class="table table-bordered"> <tr> <td colspan="5" style="text-align:center;"><label>Items Details</label></td> </tr> <tr> <th>Line Id</th> <th>Item Name</th> <th>Qty</th> <th>Total Amount</th> <th>Description</th> </tr> @foreach (var LineItem in item.Line) { <tr> <td>@LineItem.Id</td> @{ ItemBasedExpenseLineDetail Expenseitem = (ItemBasedExpenseLineDetail)LineItem.AnyIntuitObject; } <td>@Expenseitem.ItemRef.name</td> <td> @if (Expenseitem.QtySpecified) { @Expenseitem.Qty } </td> <td> @if (LineItem.AmountSpecified) { @LineItem.Amount } </td> <td>@LineItem.Description</td> </tr> } </table> </td> </tr> } </table> </div>
OUTPUT:
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular
In this article, we have to show Create and Used PIPE in angular