ruckus / quickbooks-ruby

Quickbooks Online REST API V3 - Ruby
MIT License
374 stars 302 forks source link

How to set discount value in Quick Book? #267

Closed ghost closed 7 years ago

ghost commented 9 years ago

Hi, I am trying to apply discount value in Quick book but for some reason discount does not apply in Quickbook. Can you please guide me how can i resolve this issue?

Discount value code:

              discount_amount = discount_value
              discount_line_item = Quickbooks::Model::InvoiceLineItem.new
              discount_line_item.amount = discount_amount#149
              discount_line_item.discount_item! do |detail|
                detail.discount_account_id = 48
              end

Discount percentage code:

discount_line_item = Quickbooks::Model::InvoiceLineItem.new
              discount_line_item.amount = discount_percentage_value#149
              discount_line_item.discount_item! do |detail|
                detail.discount_percent = discount_percentage_value#60
                detail.percent_based = percent_based
                detail.discount_account_id = 48
end
 invoice.line_items << discount_line_item

Set Service:

service = Quickbooks::Service::Invoice.new
              service.company_id = realm_id #session[:user_realm_id]
              service.access_token = oauth_client
              created_invoice = service.create(invoice)

Also please tell me is there any problem with CurrentBalance with 0 balance?. please tell me how can i update this value.

<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2015-09-17T03:29:25.309-07:00">
  <Account domain="QBO" sparse="false">
    <Id>86</Id>
    <SyncToken>1</SyncToken>
    <MetaData>
      <CreateTime>2014-11-18T14:54:12-08:00</CreateTime>
      <LastUpdatedTime>2015-09-17T02:27:01-07:00</LastUpdatedTime>
    </MetaData>
    <Name>Discounts given</Name>
    <SubAccount>false</SubAccount>
    <Description>hello desctio</Description>
    <FullyQualifiedName>Discounts given</FullyQualifiedName>
    <Active>true</Active>
    <Classification>Revenue</Classification>
    <AccountType>Income</AccountType>
    <AccountSubType>DiscountsRefundsGiven</AccountSubType>
    <CurrentBalance>0</CurrentBalance>
    <CurrentBalanceWithSubAccounts>0</CurrentBalanceWithSubAccounts>
  </Account>
</IntuitResponse>
ghost commented 9 years ago

When i try test Json Request in browser https://developer.intuit.com/v2/apiexplorer?apiname=V3QBO#?id=Invoice

Response:

{"line_items":[{"amount":"112.0","description":"QB Test Product Sept 15, 14\n","detail_type":"SalesItemLineDetail","sales_line_item_detail":{"unit_price":"8.0","quantity":14,"item_ref":{"value":123}}},{"amount":67.2,"detail_type":"DiscountLineDetail","discount_line_detail":{"discount_percent":60,"percent_based":true,"discount_account_ref":{"value":86}}},{"amount":"0.0","detail_type":"DiscountLineDetail","discount_line_detail":{"discount_account_ref":{"value":86}}}],"customer_ref":{"value":"115"},"txn_date":"2015-09-17","doc_number":"","custom_fields":[{"id":1,"name":"LFI Invoice #","type":"StringType","string_value":"LFI5003"}]}

Response:

<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2015-09-17T05:43:09.854-07:00">
  <Fault type="SystemFault">
    <Error code="10000">
      <Message>An application error has occurred while processing your request</Message>
      <Detail>System Failure Error: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.</Detail>
    </Error>
  </Fault>
</IntuitResponse>
ghost commented 9 years ago

When I try to use test invoice XML using this link in Quick book https://developer.intuit.com/v2/apiexplorer?apiname=V3QBO#?id=Invoice

Request

<Invoice>
   <CustomField>
   <DefinitionId>1</DefinitionId>
<Name>LFI Invoice #</Name>
<Type>StringType</Type>
<StringValue>LFI5003</StringValue>
</CustomField>
<DocNumber/>
<TxnDate>2015-09-17</TxnDate>
<Line>
<Description>QB Test Product Sept 15, 14</Description>
<Amount>112.0</Amount>
<DetailType>SalesItemLineDetail</DetailType>
<SalesItemLineDetail>
<ItemRef>123</ItemRef>
<UnitPrice>8.0</UnitPrice>
<Qty>14.0</Qty>
</SalesItemLineDetail>
</Line>
<Line>
<Amount>67.2</Amount>
<DetailType>DiscountLineDetail</DetailType>
<DiscountLineDetail>
<PercentBased>true</PercentBased>
<DiscountPercent>60.0</DiscountPercent>
<DiscountAccountRef>86</DiscountAccountRef>
</DiscountLineDetail>
</Line>
<Line>
<Amount>0.0</Amount>
<DetailType>DiscountLineDetail</DetailType>
<DiscountLineDetail>
<DiscountAccountRef>86</DiscountAccountRef>
</DiscountLineDetail>
</Line>
<CustomerRef>115</CustomerRef>
</Invoice>

Response:


<IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2015-09-17T05:52:01.051-07:00">
  <Fault type="SystemFault">
    <Error code="10000">
      <Message>An application error has occurred while processing your request</Message>
      <Detail>System Failure Error: unexpected element (uri:"", local:"Invoice"). Expected elements are &lt;{http://schema.intuit.com/finance/v3}Account&gt;,&lt;{http://schema.intuit.com/finance/v3}Attachable&gt;,&lt;{http://schema.intuit.com/finance/v3}Bill&gt;,&lt;{http://schema.intuit.com/finance/v3}BillPayment&gt;,&lt;{http://schema.intuit.com/finance/v3}BooleanTypeCustomFieldDefinition&gt;,&lt;{http://schema.intuit.com/finance/v3}Budget&gt;,&lt;{http://schema.intuit.com/finance/v3}ChargeCredit&gt;,&lt;{http://schema.intuit.com/finance/v3}Class&gt;,&lt;{http://schema.intuit.com/finance/v3}Company&gt;,&lt;{http://schema.intuit.com/finance/v3}CompanyCurrency&gt;,&lt;{http://schema.intuit.com/finance/v3}CompanyInfo&gt;,&lt;{http://schema.intuit.com/finance/v3}CreditMemo&gt;,&lt;{http://schema.intuit.com/finance/v3}CustomFieldDefinition&gt;,&lt;{http://schema.intuit.com/finance/v3}Customer&gt;,&lt;{http://schema.intuit.com/finance/v3}DateTypeCustomFieldDefinition&gt;,&lt;{http://schema.intuit.com/finance/v3}Department&gt;,&lt;{http://schema.intuit.com/finance/v3}Deposit&gt;,&lt;{http://schema.intuit.com/finance/v3}EmailDeliveryInfo&gt;,&lt;{http://schema.intuit.com/finance/v3}Employee&gt;,&lt;{http://schema.intuit.com/finance/v3}Estimate&gt;,&lt;{http://schema.intuit.com/finance/v3}ExchangeRate&gt;,&lt;{http://schema.intuit.com/finance/v3}FixedAsset&gt;,&lt;{http://schema.intuit.com/finance/v3}IntuitBatchRequest&gt;,&lt;{http://schema.intuit.com/finance/v3}IntuitObject&gt;,&lt;{http://schema.intuit.com/finance/v3}IntuitResponse&gt;,&lt;{http://schema.intuit.com/finance/v3}InventorySite&gt;,&lt;{http://schema.intuit.com/finance/v3}Invoice&gt;,&lt;{http://schema.intuit.com/finance/v3}Item&gt;,&lt;{http://schema.intuit.com/finance/v3}JournalCode&gt;,&lt;{http://schema.intuit.com/finance/v3}JournalEntry&gt;,&lt;{http://schema.intuit.com/finance/v3}MasterAccount&gt;,&lt;{http://schema.intuit.com/finance/v3}NumberTypeCustomFieldDefinition&gt;,&lt;{http://schema.intuit.com/finance/v3}OLBStatus&gt;,&lt;{http://schema.intuit.com/finance/v3}OtherName&gt;,&lt;{http://schema.intuit.com/finance/v3}Payment&gt;,&lt;{http://schema.intuit.com/finance/v3}PaymentMethod&gt;,&lt;{http://schema.intuit.com/finance/v3}Preferences&gt;,&lt;{http://schema.intuit.com/finance/v3}PriceLevel&gt;,&lt;{http://schema.intuit.com/finance/v3}Purchase&gt;,&lt;{http://schema.intuit.com/finance/v3}PurchaseOrder&gt;,&lt;{http://schema.intuit.com/finance/v3}QbdtEntityIdMapping&gt;,&lt;{http://schema.intuit.com/finance/v3}RefundReceipt&gt;,&lt;{http://schema.intuit.com/finance/v3}ReimburseCharge&gt;,&lt;{http://schema.intuit.com/finance/v3}Report&gt;,&lt;{http://schema.intuit.com/finance/v3}SalesOrder&gt;,&lt;{http://schema.intuit.com/finance/v3}SalesReceipt&gt;,&lt;{http://schema.intuit.com/finance/v3}SalesRep&gt;,&lt;{http://schema.intuit.com/finance/v3}StatementCharge&gt;,&lt;{http://schema.intuit.com/finance/v3}Status&gt;,&lt;{http://schema.intuit.com/finance/v3}StringTypeCustomFieldDefinition&gt;,&lt;{http://schema.intuit.com/finance/v3}SyncActivity&gt;,&lt;{http://schema.intuit.com/finance/v3}SyncErrorResponse&gt;,&lt;{http://schema.intuit.com/finance/v3}TDSMetadata&gt;,&lt;{http://schema.intuit.com/finance/v3}Task&gt;,&lt;{http://schema.intuit.com/finance/v3}TaxAgency&gt;,&lt;{http://schema.intuit.com/finance/v3}TaxCode&gt;,&lt;{http://schema.intuit.com/finance/v3}TaxRate&gt;,&lt;{http://schema.intuit.com/finance/v3}Term&gt;,&lt;{http://schema.intuit.com/finance/v3}TimeActivity&gt;,&lt;{http://schema.intuit.com/finance/v3}Transfer&gt;,&lt;{http://schema.intuit.com/finance/v3}UserAlert&gt;,&lt;{http://schema.intuit.com/finance/v3}Vendor&gt;,&lt;{http://schema.intuit.com/finance/v3}VendorCredit&gt;,&lt;{http://schema.intuit.com/finance/v3}VendorType&gt;</Detail>
    </Error>
  </Fault>
</IntuitResponse>
minimul commented 9 years ago

Do you have in the company preference Sales Form Entry -> Discounts enabled?

minimul commented 9 years ago

On the JSON request you probably didn't have the Content-Type application/json . I believe you get that error because Intuit defaults to application/xml.

ghost commented 9 years ago

image

Thanks for you reply , attached screen for discount already enabled

ghost commented 9 years ago

image image

Now trying with json with content-type= application/json

Request:

{"line_items":[{"amount":"160.0","description":"QB Test Product Sept 15, 20\n","detail_type":"SalesItemLineDetail","sales_line_item_detail":{"unit_price":"8.0","quantity":20,"item_ref":{"value":123}}},{"amount":"0.0","detail_type":"DiscountLineDetail","discount_line_detail":{"discount_account_ref":{"value":86}}}],"customer_ref":{"value":"115"},"txn_date":"2015-09-18","doc_number":"","custom_fields":[{"id":1,"name":"LFI Invoice #","type":"StringType","string_value":"LFI5007"}]}

Response:

{"Fault":{"Error":[{"Message":"Request has invalid or unsupported property","Detail":"Property Name:Unrecognized field \"line_items\" (class com.intuit.schema.finance.v3.Invoice), not marked as ignorable (67 known properties: \"DocNumber\", \"HomeTotalAmt\", \"TotalAmt\", \"TxnSource\", \"AllowOnlinePayment\", \"EmailSt specified is unsupported or invalid","code":"2010"}],"type":"ValidationFault"},"time":"2015-09-18T04:22:32.355-07:00"}
minimul commented 9 years ago

Needs to be "Line": not "line_items": Actually, all of your JSON is in snake case. See proper JSON -> https://developer.intuit.com/docs/api/accounting/Invoice

If you're using this gem you need to be using XML. If you prefer JSON build something basic with Faraday, HTTParty, or Unirest.

ghost commented 9 years ago

If i use line instead of line_item it gives me below error

undefined method `line' for #Quickbooks::Model::Invoice:0x00000007d11440

AND I just render invoice object in json and xml format and try to double check with query browser

 discount_line_item = Quickbooks::Model::InvoiceLineItem.new
            discount_line_item.amount = discount_amount#149
            discount_line_item.discount_item! do |detail|
              detail.discount_account_id = 86
            end
            invoice.line_items << discount_line_item
return render :json => invoice
#or
return render :xml => invoice
drewish commented 7 years ago

For reference here's some code I'm using to build a discount:

    discount_item = Quickbooks::Model::InvoiceLineItem.new(
      amount: discount_amount.abs.round(2),
      description: "Coupon"
    )
    discount_item.discount_item! do |detail|
      detail.percent_based = false
    end

    invoice.line_items << discount_item

One thing that was surprising to me is that you can put multiple discounts on an invoice, but QBO ignores all but the last one. I was expecting it to act more like QB Desktop where you can put a discount line below each sale line.