Skip to content

Commit

Permalink
Feature: Upload Fyle expense attachments as part of the quickbooks ex…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
chatterjeeshekhar authored May 27, 2020
1 parent 25548b2 commit 2c3c91d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
17 changes: 17 additions & 0 deletions apps/fyle/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ def sync_projects(self, active_only: bool):
project_attributes = ExpenseAttribute.bulk_upsert_expense_attributes(project_attributes, self.workspace_id)

return project_attributes

def get_attachments(self, expense_ids: List[str]):
"""
Get attachments against expense_ids
"""
attachments = []
if expense_ids:
for expense_id in expense_ids:
attachment = self.connection.Expenses.get_attachments(expense_id)
if attachment['data']:
attachment = attachment['data'][0]
attachment['expense_id'] = expense_id
attachments.append(attachment)
return attachments

return []

30 changes: 30 additions & 0 deletions apps/quickbooks_online/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@
logger = logging.getLogger(__name__)


def load_attachments(qbo_connection: QBOConnector, ref_id: str, ref_type: str, expense_group: ExpenseGroup):
"""
Get attachments from fyle
:param qbo_connection: QBO Connection
:param ref_id: object id
:param ref_type: type of object
:param expense_group: Expense group
"""
try:
fyle_credentials = FyleCredential.objects.get(workspace_id=expense_group.workspace_id)
expense_ids = expense_group.expenses.values_list('expense_id', flat=True)
fyle_connector = FyleConnector(fyle_credentials.refresh_token, expense_group.workspace_id)
attachments = fyle_connector.get_attachments(expense_ids)
qbo_connection.post_attachments(ref_id, ref_type, attachments)
except Exception:
error = traceback.format_exc()
logger.error(
'Attachment failed for expense group id %s / workspace id %s \n Error: %s',
expense_group.id, expense_group.workspace_id, error
)


def schedule_bills_creation(workspace_id: int, expense_group_ids: List[str], user):
"""
Schedule bills creation
Expand Down Expand Up @@ -87,6 +109,8 @@ def create_bill(expense_group, task_log):

created_bill = qbo_connection.post_bill(bill_object, bill_lineitems_objects)

load_attachments(qbo_connection, created_bill['Bill']['Id'], 'Bill', expense_group)

task_log.detail = created_bill
task_log.bill = bill_object
task_log.status = 'COMPLETE'
Expand Down Expand Up @@ -247,6 +271,8 @@ def create_cheque(expense_group, task_log):

created_cheque = qbo_connection.post_cheque(cheque_object, cheque_line_item_objects)

load_attachments(qbo_connection, created_cheque['Purchase']['Id'], 'Purchase', expense_group)

task_log.detail = created_cheque
task_log.cheque = cheque_object
task_log.status = 'COMPLETE'
Expand Down Expand Up @@ -356,6 +382,8 @@ def create_credit_card_purchase(expense_group, task_log):
credit_card_purchase_object, credit_card_purchase_lineitems_objects
)

load_attachments(qbo_connection, created_credit_card_purchase['Purchase']['Id'], 'Purchase', expense_group)

task_log.detail = created_credit_card_purchase
task_log.credit_card_purchase = credit_card_purchase_object
task_log.status = 'COMPLETE'
Expand Down Expand Up @@ -464,6 +492,8 @@ def create_journal_entry(expense_group, task_log):
created_journal_entry = qbo_connection.post_journal_entry(journal_entry_object,
journal_entry_lineitems_objects)

load_attachments(qbo_connection, created_journal_entry['JournalEntry']['Id'], 'JournalEntry', expense_group)

task_log.detail = created_journal_entry
task_log.journal_entry = journal_entry_object
task_log.status = 'COMPLETE'
Expand Down
23 changes: 23 additions & 0 deletions apps/quickbooks_online/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,26 @@ def get_company_preference(self):
:return:
"""
return self.connection.preferences.get()

def post_attachments(self, ref_id: str, ref_type: str, attachments: List[Dict]) -> List:
"""
Link attachments to objects Quickbooks
:param prep_id: prep id for export
:param ref_id: object id
:param ref_type: type of object
:param attachments: attachment[dict()]
:return: True for success, False for failure
"""

if len(attachments):
responses = []
for attachment in attachments:
response = self.connection.attachments.post(
ref_id=ref_id,
ref_type=ref_type,
content=attachment['content'],
file_name=attachment['filename']
)
responses.append(response)
return responses
return []

0 comments on commit 2c3c91d

Please sign in to comment.