XeroAPI / xero-ruby

Xero Ruby SDK for OAuth 2.0 generated from XeroAPI/Xero-OpenAPI
http://developer.xero.com/
MIT License
56 stars 90 forks source link

Can't seem to create invoices with existing xero id. #13

Closed joegaudet closed 4 years ago

joegaudet commented 4 years ago

The following code returns an error that the contact already exists, despite me including the contact id in the request.

def call(invoices)
  created_invoices = api_instance.create_invoices(
    tenant_id,
    XeroRuby::Invoices.new(
      invoices: invoices.map do |invoice|
        XeroRuby::Invoice.new(
          invoice_number: invoice.identifier,
          invoice_id: invoice.xero_identifier&.id,
          type: 'ACCREC'.freeze,
          contact: XeroRuby::Contact.new(
            contact_id: invoice.recipient.xero_identifier&.id
          ),
          line_items: invoice.line_items.map do |line_item|
            XeroRuby::LineItem.new(
              quantity: line_item.quantity,
              unit_amount: line_item.unit_amount,
              tax_amount: line_item.tax_amount,
              description: line_item.description&.strip_emoji,
              account_code: line_item.accounting_code,
            #TODO tracking
            )
          end
        )
      end
    )
  )
end
jenksguo commented 4 years ago

Hi Joe, this will need some investigation from me. "Already exists" error is returned when "name" string already exists in Xero. We don't allow duplicate contacts with the same "name".

The instantiation of the new contact object should return an object with only "contactID" key and not have a "name" key. I will take a look and let you when a fix is released.

XeroRuby::Contact.new( contact_id: invoice.recipient.xero_identifier&.id (<= this is returning only a GUID string?) )

The gem tested fine when a simple contact JSON object was passed in, so I would suggest a walk-around for now like so:

invoice = { "Invoices":[ { "Type":"ACCREC", "Contact":{ "ContactID" :"30ab4104-f8cf-4672-bccb-7b8f3c6eeb4e" }, "LineItems":[ { "Description":"Acme Tires", "Quantity":2.0, "UnitAmount":20.0, "AccountCode":"200", "TaxType" :"NONE", "LineAmount":40.0 } ], "Date":"2019-03-11", "DueDate":"2018-12-10", "Reference":"Website Design", "Status":"AUTHORISED" } ] } result = api_instance.create_invoice(xero_tenant_id, invoice)

On Thu, Jan 16, 2020 at 2:02 PM Joe Gaudet notifications@github.com wrote:

The following code returns an error that the contact already exists, despite me including the contact id in the request.

def call(invoices) created_invoices = api_instance.create_invoices( tenant_id, XeroRuby::Invoices.new( invoices: invoices.map { |invoice| XeroRuby::Invoice.new( invoice_number: invoice.identifier, invoice_id: invoice.xero_identifier&.id, type: 'ACCREC'.freeze, contact: XeroRuby::Contact.new( contact_id: invoice.recipient.xero_identifier&.id ), line_items: invoice.line_items.map do |line_item| XeroRuby::LineItem.new( quantity: line_item.quantity, unit_amount: line_item.unit_amount, tax_amount: line_item.tax_amount, description: line_item.description&.strip_emoji, account_code: line_item.accounting_code,

TODO tracking

            )
          end
        )

} ) )end

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/XeroAPI/xero-ruby/issues/13?email_source=notifications&email_token=AJ3PMS2RAO55DBCONBVVL4LQ57E23A5CNFSM4KHNENN2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IGQUYMQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJ3PMS7UTQBRQFKKFSCUIR3Q57E23ANCNFSM4KHNENNQ .

--

Jenks Guo Developer Evangelist Mobile: +61 (04) 22590259 1/6 Elizabeth St, Hawthorn, VIC, 3122 www.xero.com

[image: Facebook] https://www.facebook.com/Xero.Accounting/[image: Twitter] https://twitter.com/Xero[image: LinkedIn] https://www.linkedin.com/company/xero[image: Instagram] https://www.instagram.com/xero/?hl=en[image: YouTube] https://www.youtube.com/user/XeroOnlineAccounting [image: We're hiring!] https://www.xero.com/about/careers/

joegaudet commented 4 years ago

@jenksguo thanks for the quick reply, I was having issues building requests with hashes earlier, so this seemed like a better way forward.

I'll give the hash approach a look.

joegaudet commented 4 years ago

The downside of using the hashes directly is you don't get all of the local attribute validation, and you have to use pascal case, which is decidedly unruby like :P

jenksguo commented 4 years ago

Hi @joegaudet, sorry for the late reply.

I have managed to test this successfully via the following code using v0.2.0. I think this can be closed now, please re-open if any issues arise.

      contact = XeroRuby::Contact.new(
        contact_id: "30ab4104-f8cf-4672-bccb-xxxxxxxx"
      )

      lineitem = XeroRuby::LineItem.new(
        quantity: 1,
        unit_amount: 100.00,
        tax_amount: 10.00,
        description: "Test DESC",
        account_code: "200"
      )

      invoice = XeroRuby::Invoice.new(
        type: "ACCREC",
        contact: contact,
        line_items: [ lineitem ]
      )

      invoices = XeroRuby::Invoices.new(
        invoices: [ invoice ] 
      )

      result = api_instance.create_invoices(xero_tenant_id, invoices)