how to add purchase order with QBO rest api v3.0

2019-04-17 13:55发布

问题:

I am trying to send a PurchaseOrder created with Intuit .NET SDK v3. I found a suitable example with Invoice: how to add invoice or sales receipt quickbooks rest api v3.0

But I cannot work out what I need to do to make it working for Purchase Order. I am getting BadRequest no matter what properties do I set.

        DataService commonService = new DataService(context);

        QueryService<Vendor> accountQueryService = new QueryService<Vendor>(context);
        Vendor customer = accountQueryService.Where(x => x.Id == "9").FirstOrDefault<Vendor>();
        QueryService<Item> itemQueryService = new QueryService<Item>(context);
        Item item = itemQueryService.Where(x => x.Id == "1").FirstOrDefault<Item>();

        PurchaseOrder invoice = new PurchaseOrder();

        invoice.VendorRef = new ReferenceType()
        {
            name = customer.DisplayName,
            Value = customer.Id
        };

        /*invoice.APAccountRef = new ReferenceType()
        {
            type = Enum.GetName(typeof(objectNameEnumType), objectNameEnumType.Account),
            name = "Account Receivable",
            Value = "QB:37"
        };*/

        List<Line> lineList = new List<Line>();
        Line line = new Line();
        line.Description = "Description";
        line.Amount = new Decimal(100.00);
        line.AmountSpecified = true;

        //line.DetailType = LineDetailTypeEnum.DescriptionOnly;
        //line.DetailTypeSpecified = true;

        line.DetailType = LineDetailTypeEnum.PurchaseOrderItemLineDetail;
        line.DetailTypeSpecified = true;

        PurchaseOrderItemLineDetail det = new PurchaseOrderItemLineDetail();
        det.Qty = 10;
        det.QtySpecified = true;
        det.ItemRef = new ReferenceType()
        {
            name = item.Name,
            Value = item.Id
        };
        line.AnyIntuitObject = det;

        lineList.Add(line);
        invoice.Line = lineList.ToArray();

I've tried to set account and use just description in the line without success. If I add PurchaseOrder created with the above code I am getting BadRequest error with Validation exception inside.

Can please somebody point me in the right direction here. I am probably missing some PurchaseOrder property.

I am using the August V3 SDK loaded using NuGet.

UPDATE

This is c# translation of the XML suggested by Manas Mukherjee below

        DataService commonService = new DataService(context);

        QueryService<Vendor> accountQueryService = new QueryService<Vendor>(context);
        Vendor customer = accountQueryService.Where(x => x.Id == "9").FirstOrDefault<Vendor>();
        QueryService<Item> itemQueryService = new QueryService<Item>(context);
        Item item = itemQueryService.Where(x => x.Id == "1").FirstOrDefault<Item>();

        PurchaseOrder invoice = new PurchaseOrder();
        invoice.TxnDate = DateTime.Now;
        invoice.TxnDateSpecified = true;
        invoice.Memo = "For Internal usage";
        invoice.GlobalTaxCalculation = GlobalTaxCalculationEnum.TaxInclusive;
        invoice.GlobalTaxCalculationSpecified = true;

        invoice.ReplyEmail = new EmailAddress();
        invoice.ReplyEmail.Address = "testing@testing.com";
        invoice.ReplyEmail.Default = false;
        invoice.ReplyEmail.DefaultSpecified = true;
        invoice.ReplyEmail.Tag = "Home";

        invoice.ShipAddr = new PhysicalAddress();
        invoice.ShipAddr.Line1 = "shippingToAddr1Sat Oct 19 09:48:52 IST 2013";
        invoice.ShipAddr.Line2 = "shippingToAddr2Sat Oct 19 09:48:52 IST 2013";
        invoice.ShipAddr.Line3 = "shippingToAddr3Sat Oct 19 09:48:52 IST 2013";
        invoice.ShipAddr.City = "Bangalore";
        invoice.ShipAddr.Country = "India";
        invoice.ShipAddr.CountrySubDivisionCode = "KA";
        invoice.ShipAddr.PostalCode = "560045";

        invoice.POEmail = new EmailAddress();
        invoice.POEmail.Address = "testing@testing.com";
        invoice.POEmail.Default = true;
        invoice.POEmail.DefaultSpecified = true;
        invoice.POEmail.Tag = "Business";

        invoice.VendorRef = new ReferenceType()
        {
            name = customer.DisplayName,
            Value = customer.Id
        };
        invoice.TotalAmt = new Decimal(100.00);
        invoice.TotalAmtSpecified = true;

        invoice.APAccountRef = new ReferenceType()
        {
            //type = Enum.GetName(typeof(objectNameEnumType), objectNameEnumType.Account),
            name = "Accounts Payable (A/P)",
            Value = "32"
        };

        List<Line> lineList = new List<Line>();
        Line line = new Line();
        line.Amount = new Decimal(100.00);
        line.AmountSpecified = true;

        line.DetailType = LineDetailTypeEnum.AccountBasedExpenseLineDetail;
        line.DetailTypeSpecified = true;

        AccountBasedExpenseLineDetail det = new AccountBasedExpenseLineDetail();
        det.AccountRef = new ReferenceType()
        {
            name = "Accounts Payable (A/P)",
            Value = "32"
        };
        line.AnyIntuitObject = det;

        /*PurchaseOrderItemLineDetail det = new PurchaseOrderItemLineDetail();
        det.Qty = 10;
        det.QtySpecified = true;
        det.ItemRef = new ReferenceType()
        {
            name = item.Name,
            Value = item.Id
        };
        line.AnyIntuitObject = det;*/

        lineList.Add(line);
        invoice.Line = lineList.ToArray();

        return invoice;

Unfortunately I am still getting BadRequest error.

UPDATE 2

After some more testing I found out that it gives error because of APAccountRef property. It's OK without setting up this property.

The other issue came when I tried to use PurchaseOrderItemLineDetail in lines. Not sure if this type is correct (based on the name it looks like one) but using this details type is causing BadRequest error. Using ItemBasedExpenseLineDetail is a workaround.

I will set Manas answer as correct since it gave me a direction to investigate further. But big thanks to Nimisha Shrivastava for the help with logging.

回答1:

Here is one working PurchaseOrder create request XML.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PurchaseOrder domain="QBO" xmlns="http://schema.intuit.com/finance/v3">
    <TxnDate>2013-10-19</TxnDate>
    <Line>
        <Amount>3.00</Amount>
        <DetailType>AccountBasedExpenseLineDetail</DetailType>
        <AccountBasedExpenseLineDetail>
            <AccountRef name="Accounts Payable (A/P)">32</AccountRef>
        </AccountBasedExpenseLineDetail>
    </Line>
    <VendorRef name="TestDataVendor926e8Sample1">1386</VendorRef>
    <TotalAmt>3.00</TotalAmt>
    <ReplyEmail>
        <Address>testing@testing.com</Address>
        <Default>false</Default>
        <Tag>Home</Tag>
    </ReplyEmail>
    <Memo>For Internal usage</Memo>
    <GlobalTaxCalculation>TaxInclusive</GlobalTaxCalculation>
    <ShipAddr>
        <Line1>shippingToAddr1Sat Oct 19 09:48:52 IST 2013</Line1>
        <Line2>shippingToAddr2Sat Oct 19 09:48:52 IST 2013</Line2>
        <Line3>shippingToAddr3Sat Oct 19 09:48:52 IST 2013</Line3>
        <City>Bangalore</City>
        <Country>India</Country>
        <CountrySubDivisionCode>KA</CountrySubDivisionCode>
        <PostalCode>560045</PostalCode>
    </ShipAddr>
    <POEmail>
        <Address>test@testing.com</Address>
        <Default>true</Default>
        <Tag>Business</Tag>
    </POEmail>
</PurchaseOrder>

I've used java devkit(PFB code). Hope you'll find similar properties in .net devkit.

public PurchaseOrder purchaseOrderWithAllProperties() throws FMSException, ParseException {

PurchaseOrder purchaseOrder = new PurchaseOrder();
purchaseOrder.setVendorRef(this.vendorRef);
purchaseOrder.setMemo("For Internal usage");

Line line1 = new Line();
line1.setAmount(new BigDecimal("3.00"));
line1.setDetailType(LineDetailTypeEnum.ACCOUNT_BASED_EXPENSE_LINE_DETAIL);
AccountBasedExpenseLineDetail detail = new AccountBasedExpenseLineDetail();
ReferenceType expenseAccountRef = this.accountRef;
detail.setAccountRef(expenseAccountRef);
line1.setAccountBasedExpenseLineDetail(detail);

List<Line> lines1 = new ArrayList<Line>();
lines1.add(line1);
purchaseOrder.setLine(lines1);

EmailAddress emailAddr = new EmailAddress();
emailAddr.setAddress("test@testing.com");
emailAddr.setDefault(true);
emailAddr.setTag("Business");

purchaseOrder.setPOEmail(emailAddr);
//purchaseOrder.setDueDate(DateUtils.getDateWithNextDays(45));
purchaseOrder.setDomain("QBO");
//purchaseOrder.setExchangeRate(new BigDecimal("20.00")); 
purchaseOrder.setGlobalTaxCalculation(GlobalTaxCalculationEnum.TAX_INCLUSIVE);

EmailAddress replyEmail = new EmailAddress();
replyEmail.setAddress("testing@testing.com");
replyEmail.setDefault(false);
replyEmail.setTag("Home");
purchaseOrder.setReplyEmail(replyEmail);

PhysicalAddress shipAddr = new PhysicalAddress();
shipAddr.setLine1("shippingToAddr1" + DateUtils.getCurrentDateTime());
shipAddr.setLine2("shippingToAddr2" + DateUtils.getCurrentDateTime());
shipAddr.setLine3("shippingToAddr3" + DateUtils.getCurrentDateTime());
shipAddr.setCity("Bangalore");
shipAddr.setCountry("India");
shipAddr.setCountrySubDivisionCode("KA");
shipAddr.setPostalCode("560045");
purchaseOrder.setShipAddr(shipAddr);

//purchaseOrder.setStatus(EntityStatusEnum.IN_TRANSIT);
purchaseOrder.setTotalAmt(new BigDecimal("3.00"));
purchaseOrder.setTxnDate(DateUtils.getCurrentDateTime());

return purchaseOrder;

}

Thanks



回答2:

Try this one adding purchase order.

                                PurchaseOrder po = new PurchaseOrder();
                                po.ReplyEmail = new EmailAddress();
                                po.ReplyEmail.Address = "kalpana.kodavaluru@gmail.com";
                                po.ReplyEmail.Default = false;
                                po.ReplyEmail.DefaultSpecified = true;
                                po.ReplyEmail.Tag = "Home";

                                po.ShipAddr = new PhysicalAddress();
                                po.ShipAddr.Line1 = "shippingToAddr1Sat Oct 19 09:48:52 IST 2013";
                                po.ShipAddr.Line2 = "shippingToAddr2Sat Oct 19 09:48:52 IST 2013";
                                po.ShipAddr.Line3 = "shippingToAddr3Sat Oct 19 09:48:52 IST 2013";
                                po.ShipAddr.City = "Bangalore";
                                po.ShipAddr.Country = "India";
                                po.ShipAddr.CountrySubDivisionCode = "KA";
                                po.ShipAddr.PostalCode = "560045";

                                po.POEmail = new EmailAddress();
                                po.POEmail.Address = "testing@testing.com";
                                po.POEmail.Default = true;
                                po.POEmail.DefaultSpecified = true;
                                po.POEmail.Tag = "Business";

                                po.VendorRef = new ReferenceType()
                                {
                                    name = supplier.DisplayName,
                                    Value = supplier.Id
                                };
                                po.TotalAmt = new Decimal(100.00);
                                po.TotalAmtSpecified = true;

                                List<Line> lineList1 = new List<Line>();
                                Line line1 = new Line();
                                line1.Id = "1";
                                line1.Amount = new Decimal(100.00);
                                line1.AmountSpecified = true;

                                line1.DetailType = LineDetailTypeEnum.ItemBasedExpenseLineDetail;
                                line1.DetailTypeSpecified = true;
                                ItemBasedExpenseLineDetail det = new ItemBasedExpenseLineDetail();
                                det.ItemRef = new ReferenceType()
                                {
                                    name = item.Name,
                                    Value = item.Id
                                };

                                det.Qty = new decimal(1);
                                det.QtySpecified = true;

                                det.ItemElementName = ItemChoiceType.UnitPrice;
                                det.AnyIntuitObject = new decimal(100);
                                det.BillableStatus = new BillableStatusEnum();
                                line1.AnyIntuitObject = det;


                                lineList1.Add(line1);
                                po.Line = lineList1.ToArray();
                                try
                                {
                                    var podata=commonService.Add(po);
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }