ej2 / python-quickbooks

A Python library for accessing the Quickbooks API.
MIT License
402 stars 194 forks source link

[HELP] Upload attachment to an invoice #215

Closed jalilbm closed 3 years ago

jalilbm commented 3 years ago

Hello everyone!

I'm new to quickbooks and I want to upload an attachment to quickbooks using this library, there is an example for employers here but not invoices.

so I would really appreciate any help!

Thanks a lot

ej2 commented 3 years ago

It should work the same as customers or employers. Just change the EntityRef on the AttachableRef to point to your Invoice. Like so:

attachment = Attachable()

attachable_ref = AttachableRef()
attachable_ref.EntityRef = invoice.to_ref()  # This is where to attach the invoice

attachment.AttachableRef.append(attachable_ref)

attachment.FileName = 'Filename'
attachment._FilePath = '/folder/filename'  # full path to file
attachment.ContentType = 'application/pdf'
attachment.save(qb=client)
ahmadberkeleypayment commented 3 years ago

Is there a way to attach a file that is already uploaded to the quickbooks online. instead of file path, is there an option for attachment id?

ej2 commented 3 years ago

I am not sure the Quickbooks API supports that.

thismatters commented 1 year ago

Indeed linking an existing attachment is supported by the API at this time. I've explored this use-case and gotten it to work using python-quickbooks, albeit with a little friction.

The QBO docs specify the minimum amount of data required to link the already uploaded attachable to another resource, however I've found that the whole Attachable instance that is returned with Attachable.get() can be modified and saved with a new AttachableRef appended to the list. Saving this mutated Attachable seems effective, but the .save() method of Attachable doesn't like it. Below is sample code which has worked for me:

from quickbooks.objects.attachable import Attachable, AttachableRef

def link_existing_attachable(self, *, qbo_attachable_id, qbo_target_resource ):
    attachable_ref = AttachableRef()
    attachable_ref.EntityRef = qbo_target_resource.to_ref()

    qbo_attachable = Attachable.get(qbo_attachable_id)
    qbo_attachable.AttachableRef.append(attachable_ref)
    try:
        qbo_attachable.save()
    except KeyError:
        # It seems like the save method expects the data structure to be 
        #   quite different than it is. It wants a key like `AttachableResponse`
        #   to exist, but it doesn't. 
        #   See `/quickbooks/objects/attachable.py` around line 62.
        pass