2013-08-30 77 views
2

我很難弄清楚到底發生了什麼錯誤 - 我沒有從錯誤中得到很多信息 - 錯誤的請求 - 這裏是我的代碼:用於QuickBooks v3.0的IPP .NET SDK創建發票錯誤 - 錯誤的請求

OAuthRequestValidator oauthValidator = new OAuthRequestValidator(accessToken, accessTokenSecret, consumerKey, consumerSecret); 
       ServiceContext context = new ServiceContext(appToken, companyID, IntuitServicesType.QBO, oauthValidator); 

       DataService service = new DataService(context); 
       Customer customer = new Customer(); 
       customer.GivenName = "Mary " + DateTime.Now.Second; 
       customer.Title = "Ms."; 
       customer.MiddleName = "Jayne"; 
       customer.FamilyName = "Cooper"; 
       customer.CompanyName = "Mary " + DateTime.Now.Second; 
       Customer resultCustomer = service.Add(customer) as Customer; 

       Invoice invoice = new Invoice(); 
       //Mandatory fields 
       invoice.DocNumber = Guid.NewGuid().ToString("N").Substring(0, 10); 
       invoice.TxnDate = DateTime.Today.Date; 
       invoice.TxnDateSpecified = true; 
       invoice.CustomerRef = new ReferenceType() 
       { 
        Value = resultCustomer.Id 
       }; 

       Line invLine = new Line(); 

       invLine.Amount = 10000; 
       invLine.DetailType = LineDetailTypeEnum.SalesItemLineDetail; 
       invLine.Description = "Test Product"; 

       invoice.Line = new Line[] { invLine }; 

       Invoice resutlInvoice = service.Add(invoice) as Invoice; 

       var invId = resutlInvoice.Id; 

基本上我產生一個新的客戶(其中正常工作),然後我想爲他們創造上有一個單一的項目的發票。

看什麼XML文檔指出這裏: http://ippdocs.intuit.com/0025_QuickBooksAPI/0050_Data_Services/V3/030_Entity_Services_Reference/Invoice

的NuGet包丟失了一些東西,我知道不能是真實的 - 形成文檔:

<Invoice xmlns="http://schema.intuit.com/finance/v3"> 
    <Line> 
    <Amount>15</Amount> 
    <DetailType>SalesItemLineDetail</DetailType> 
    <SalesItemLineDetail> 
     <ItemRef>1</ItemRef> 
    </SalesItemLineDetail> 
    </Line> 
    <CustomerRef>67</CustomerRef> 
</Invoice> 

Line對象我從此SDK獲取沒有SalesItemLineDetail或ItemRef的屬性。

任何人都有這樣的工作示例?

回答

2

這是.NET devkit與Java不同的地方之一。

您必須將AnyIntuitObject屬性設置爲SalesItemLineDetail,然後將DetailType屬性設置爲LineDetailTypeEnum.SalesItemLineDetail"

圍繞着這個框架出現了一些小零件。

下面是一個簡短的示例,雖然基於它的評論是而不是使用最新的SDK。

Line l = new Line() 
{ 
    l.AnyIntuitObject = new SalesItemLineDetail() 
    { 
     ItemElementName = ItemChoiceType.UnitPrice, 
     AnyIntuitObject = amount, 
     ... 
    }, 
    DetailType = LineDetailTypeEnum.SalesItemLineDetail, 
    ... 
} 
+0

你也必須標記這被設置爲像AmountSpecified和DetailTypeSpecified必須設置爲真 - 奇怪,但它工作 – Slee

+0

@Slee:我看不到DetailTypeSpecified屬性?您使用的是哪個版本的SDK? –

+0

我使用通過NuGet引用的V3:https://developer.intuit.com/docs/0025_quickbooksapi/0050_data_services/v3 – Slee

0

我已經使用V3 Java devkit創建了一張發票。它運行良好。 PFB請求XML。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Invoice xmlns="http://schema.intuit.com/finance/v3"> 
    <DocNumber>b2980</DocNumber> 
    <TxnDate>2013-09-05</TxnDate> 
    <Line> 
     <Id>3</Id> 
     <Description>test</Description> 
     <Amount>10000</Amount> 
     <DetailType>SalesItemLineDetail</DetailType> 
     <SalesItemLineDetail> 
      <TaxCodeRef type="TaxCode" name="TAX">TAX</TaxCodeRef> 
     </SalesItemLineDetail> 
    </Line> 
    <TxnTaxDetail> 
     <DefaultTaxCodeRef type="Customer" name="TAX">TAX</DefaultTaxCodeRef> 
     <TotalTax>0</TotalTax> 
    </TxnTaxDetail> 
    <CustomerRef name="TestDataCustomer620d5Sample1">1289</CustomerRef> 
    <ARAccountRef type="Account" name="Account Receivable">QB:37</ARAccountRef> 
</Invoice> 

Java代碼

List<Line> invLine = new ArrayList<Line>(); 
Line line = new Line(); 
line.setId("3"); 
line.setDescription("test"); 
line.setAmount(new BigDecimal("10000")); 
line.setDetailType(LineDetailTypeEnum.SALES_ITEM_LINE_DETAIL); 

SalesItemLineDetail silDetails = new SalesItemLineDetail(); 
ReferenceType refTaxCode = new ReferenceType(); 
refTaxCode.setName(taxCode.getName()); 
refTaxCode.setType(ObjectNameEnumType.TAX_CODE.value()); 
refTaxCode.setValue(taxCode.getId()); 

silDetails.setTaxCodeRef(refTaxCode); 

line.setSalesItemLineDetail(silDetails); 

invLine.add(line); 

invoice.setLine(invLine); 

請,如果你發現在.NET的devkit一些相似的對象進行檢查。我會嘗試使用.net開發工具包(它尚未安裝)。完成後,我會發布代碼。

謝謝

+0

那將是巨大的 - 我拉我的頭髮 – Slee

+0

請參考Java代碼片斷,看看你是否得到相同的.NET。我也會在明天嘗試一下(需要安裝.net開發工具包)。謝謝 –

+1

這是非常相似的,但我沒有Line對象上的SalesItemLineDetail的屬性,這對我來說似乎很奇怪 - 看起來就是這樣設置數量 – Slee

4
//Find Customer 
QueryService<Customer> customerQueryService = new QueryService<Customer>(context); 
Customer customer = customerQueryService.ExecuteIdsQuery("Select * From Customer StartPosition 1 MaxResults 1").FirstOrDefault<Customer>(); 

//Find Tax Code for Invoice - Searching for a tax code named 'StateSalesTax' in this example 
QueryService<TaxCode> stateTaxCodeQueryService = new QueryService<TaxCode>(context); 
TaxCode stateTaxCode = stateTaxCodeQueryService.ExecuteIdsQuery("Select * From TaxCode Where Name='StateSalesTax' StartPosition 1 MaxResults 1").FirstOrDefault<TaxCode>(); 

//Find Account - Accounts Receivable account required 
QueryService<Account> accountQueryService = new QueryService<Account>(context); 
Account account = accountQueryService.ExecuteIdsQuery("Select * From Account Where AccountType='Accounts Receivable' StartPosition 1 MaxResults 1").FirstOrDefault<Account>(); 

//Find Item 
QueryService<Item> itemQueryService = new QueryService<Item>(context); 
Item item = itemQueryService.ExecuteIdsQuery("Select * From Item StartPosition 1 MaxResults 1").FirstOrDefault<Item>(); 

//Find Term 
QueryService<Term> termQueryService = new QueryService<Term>(context); 
Term term = termQueryService.ExecuteIdsQuery("Select * From Term StartPosition 1 MaxResults 1").FirstOrDefault<Term>(); 


Invoice invoice = new Invoice(); 

//DocNumber - QBO Only, otherwise use DocNumber 
invoice.AutoDocNumber = true; 
invoice.AutoDocNumberSpecified = true; 

//TxnDate 
invoice.TxnDate = DateTime.Now.Date; 
invoice.TxnDateSpecified = true; 

//PrivateNote 
invoice.PrivateNote = "This is a private note"; 

//Line 
Line invoiceLine = new Line(); 
//Line Description 
invoiceLine.Description = "Invoice line description."; 
//Line Amount 
invoiceLine.Amount = 330m; 
invoiceLine.AmountSpecified = true; 
//Line Detail Type 
invoiceLine.DetailType = LineDetailTypeEnum.SalesItemLineDetail; 
invoiceLine.DetailTypeSpecified = true; 
//Line Sales Item Line Detail 
SalesItemLineDetail lineSalesItemLineDetail = new SalesItemLineDetail(); 
//Line Sales Item Line Detail - ItemRef 
lineSalesItemLineDetail.ItemRef = new ReferenceType() 
{ 
    name = item.Name, 
    Value = item.Id 
}; 
//Line Sales Item Line Detail - UnitPrice 
lineSalesItemLineDetail.AnyIntuitObject = 33m; 
lineSalesItemLineDetail.ItemElementName = ItemChoiceType.UnitPrice; 
//Line Sales Item Line Detail - Qty 
lineSalesItemLineDetail.Qty = 10; 
lineSalesItemLineDetail.QtySpecified = true; 
//Line Sales Item Line Detail - TaxCodeRef 
//For US companies, this can be 'TAX' or 'NON' 
lineSalesItemLineDetail.TaxCodeRef = new ReferenceType() 
{ 
    Value = "TAX" 
}; 
//Line Sales Item Line Detail - ServiceDate 
lineSalesItemLineDetail.ServiceDate = DateTime.Now.Date; 
lineSalesItemLineDetail.ServiceDateSpecified = true; 
//Assign Sales Item Line Detail to Line Item 
invoiceLine.AnyIntuitObject = lineSalesItemLineDetail; 
//Assign Line Item to Invoice 
invoice.Line = new Line[] { invoiceLine }; 

//TxnTaxDetail 
TxnTaxDetail txnTaxDetail = new TxnTaxDetail(); 
txnTaxDetail.TxnTaxCodeRef = new ReferenceType() 
{ 
    name = stateTaxCode.Name, 
    Value = stateTaxCode.Id 
}; 
Line taxLine = new Line(); 
taxLine.DetailType = LineDetailTypeEnum.TaxLineDetail; 
TaxLineDetail taxLineDetail = new TaxLineDetail(); 
//Assigning the fist Tax Rate in this Tax Code 
taxLineDetail.TaxRateRef = stateTaxCode.SalesTaxRateList.TaxRateDetail[0].TaxRateRef; 
taxLine.AnyIntuitObject = taxLineDetail; 
txnTaxDetail.TaxLine = new Line[] { taxLine }; 
invoice.TxnTaxDetail = txnTaxDetail; 

//Customer (Client) 
invoice.CustomerRef = new ReferenceType() 
{ 
    name = customer.DisplayName, 
    Value = customer.Id 
}; 

//Billing Address 
PhysicalAddress billAddr = new PhysicalAddress(); 
billAddr.Line1 = "123 Main St."; 
billAddr.Line2 = "Unit 506"; 
billAddr.City = "Brockton"; 
billAddr.CountrySubDivisionCode = "MA"; 
billAddr.Country = "United States"; 
billAddr.PostalCode = "02301"; 
billAddr.Note = "Billing Address Note"; 
invoice.BillAddr = billAddr; 

//Shipping Address 
PhysicalAddress shipAddr = new PhysicalAddress(); 
shipAddr.Line1 = "100 Fifth Ave."; 
shipAddr.City = "Waltham"; 
shipAddr.CountrySubDivisionCode = "MA"; 
shipAddr.Country = "United States"; 
shipAddr.PostalCode = "02452"; 
shipAddr.Note = "Shipping Address Note"; 
invoice.ShipAddr = shipAddr; 

//SalesTermRef 
invoice.SalesTermRef = new ReferenceType() 
{ 
    name = term.Name, 
    Value = term.Id 
}; 

//DueDate 
invoice.DueDate = DateTime.Now.AddDays(30).Date; 
invoice.DueDateSpecified = true; 

//ARAccountRef 
invoice.ARAccountRef = new ReferenceType() 
{ 
    name = account.Name, 
    Value = account.Id 
}; 

Invoice invoiceAdded = dataService.Add<Invoice>(invoice);