Ejemplo n.º 1
0
def process_reimbursements(workspace_id):
    fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)

    fyle_connector = FyleConnector(fyle_credentials.refresh_token,
                                   workspace_id)

    fyle_connector.sync_reimbursements()

    reimbursements = Reimbursement.objects.filter(
        state='PENDING', workspace_id=workspace_id).all()

    reimbursement_ids = []

    if reimbursements:
        for reimbursement in reimbursements:
            expenses = Expense.objects.filter(
                settlement_id=reimbursement.settlement_id,
                fund_source='PERSONAL').all()
            paid_expenses = expenses.filter(paid_on_qbo=True)

            all_expense_paid = False
            if len(expenses):
                all_expense_paid = len(expenses) == len(paid_expenses)

            if all_expense_paid:
                reimbursement_ids.append(reimbursement.reimbursement_id)

    if reimbursement_ids:
        fyle_connector.post_reimbursement(reimbursement_ids)
        fyle_connector.sync_reimbursements()
Ejemplo n.º 2
0
def create_bill_payment(workspace_id):
    fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)

    fyle_connector = FyleConnector(fyle_credentials.refresh_token,
                                   workspace_id)

    fyle_connector.sync_reimbursements()

    bills = Bill.objects.filter(payment_synced=False,
                                expense_group__workspace_id=workspace_id,
                                expense_group__fund_source='PERSONAL').all()

    if bills:
        for bill in bills:
            expense_group_reimbursement_status = check_expenses_reimbursement_status(
                bill.expense_group.expenses.all())
            if expense_group_reimbursement_status:
                task_log, _ = TaskLog.objects.update_or_create(
                    workspace_id=workspace_id,
                    task_id='PAYMENT_{}'.format(bill.expense_group.id),
                    defaults={
                        'status': 'IN_PROGRESS',
                        'type': 'CREATING_BILL_PAYMENT'
                    })
                try:
                    with transaction.atomic():

                        bill_payment_object = BillPayment.create_bill_payment(
                            bill.expense_group)

                        qbo_object_task_log = TaskLog.objects.get(
                            expense_group=bill.expense_group)

                        linked_transaction_id = qbo_object_task_log.detail[
                            'Bill']['Id']

                        bill_payment_lineitems_objects = BillPaymentLineitem.create_bill_payment_lineitems(
                            bill_payment_object.expense_group,
                            linked_transaction_id)

                        qbo_credentials = QBOCredential.objects.get(
                            workspace_id=workspace_id)

                        qbo_connection = QBOConnector(qbo_credentials,
                                                      workspace_id)

                        created_bill_payment = qbo_connection.post_bill_payment(
                            bill_payment_object,
                            bill_payment_lineitems_objects)

                        bill.payment_synced = True
                        bill.paid_on_qbo = True
                        bill.save()

                        task_log.detail = created_bill_payment
                        task_log.bill_payment = bill_payment_object
                        task_log.status = 'COMPLETE'

                        task_log.save()

                except QBOCredential.DoesNotExist:
                    logger.error(
                        'QBO Credentials not found for workspace_id %s / expense group %s',
                        workspace_id, bill.expense_group)
                    detail = {
                        'expense_group_id': bill.expense_group,
                        'message': 'QBO Account not connected'
                    }
                    task_log.status = 'FAILED'
                    task_log.detail = detail

                    task_log.save()

                except BulkError as exception:
                    logger.error(exception.response)
                    detail = exception.response
                    task_log.status = 'FAILED'
                    task_log.detail = detail

                    task_log.save()

                except WrongParamsError as exception:
                    logger.error(exception.response)
                    detail = json.loads(exception.response)
                    task_log.status = 'FAILED'
                    task_log.detail = detail

                    task_log.save()

                except Exception:
                    error = traceback.format_exc()
                    task_log.detail = {'error': error}
                    task_log.status = 'FATAL'
                    task_log.save()
                    logger.error(
                        'Something unexpected happened workspace_id: %s %s',
                        task_log.workspace_id, task_log.detail)