如何使用QBO rest api v3.0添加采购订单

时间:2013-10-19 01:44:01

标签: c# .net intuit-partner-platform

我正在尝试发送使用Intuit .NET SDK v3创建的PurchaseOrder。我在Invoice中找到了一个合适的例子:how to add invoice or sales receipt quickbooks rest api v3.0

但我无法弄清楚我需要做些什么才能让它适用于采购订单。无论我设置什么属性,我都会收到BadRequest。

        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();

我尝试设置帐户并使用该行中的描述但没有成功。如果我添加使用上面的代码创建的PurchaseOrder,我的内部验证异常会收到BadRequest错误。

可以请有人在这里指出我正确的方向。我可能错过了一些PurchaseOrder属性。

我正在使用使用NuGet加载的August V3 SDK。

更新

这是Manas Mukherjee在

下面建议的XML的c#翻译
        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;

不幸的是我仍然收到BadRequest错误。

更新2

经过一些测试后,我发现它因APAccountRef属性而出错。没有设置这个属性就行了。

当我尝试在行中使用PurchaseOrderItemLineDetail时,出现了另一个问题。不确定此类型是否正确(基于名称看起来像一个)但使用此详细信息类型会导致BadRequest错误。使用ItemBasedExpenseLineDetail是一种解决方法。

我会将玛纳斯的回答设置为正确,因为它给了我进一步调查的指示。但非常感谢Nimisha Shrivastava帮助伐木。

2 个答案:

答案 0 :(得分:1)

这是一个有效的PurchaseOrder创建请求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>

我使用过java devkit(PFB代码)。希望你能在.net devkit中找到类似的属性。

public PurchaseOrder purchaseOrderWithAllProperties()抛出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;

}

由于

答案 1 :(得分:0)

尝试添加采购订单。

                                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;
                                }