Example #1
0
def download_transactions(request):
    company_ID = utils.get_company(request)

    snapshot_time = datetime.datetime.now()
    strategy = QueryManagerStrategyFactory().get('snapshot')
    strategy.set_cache(None)

    trans = strategy.get_all_transactions(company_ID)

    all_accts_list = api_func('gl', 'account')
    all_accts = dict((r['id'], r) for r in all_accts_list)
    
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="transactions.csv"'
    writer = csv.writer(response)

    writer.writerow(['', '', '', '', '', '','debit', 'debit', 'debit', 'debit','credit', 'credit', 'credit', 'credit'])
    writer.writerow(['Id', 'dateEnd', 'date', 'type', 'comment', 'counterpartyId', 'amount', 'accountId', 
                    'account name','counterpartyId', 'amount', 'accountId', 'account name'])

    for ex in trans:
        first_line = ex['lines'][0]
        acct_id = first_line['accountId']
        acct = api_func('gl', 'account', acct_id)
        if (acct['role'] in ['asset', 'expense'] and float(first_line['amount']) >0) or \
            (acct['role'] in ['liability', 'income', 'capital'] and float(first_line['amount']) < 0):
            debit = ex['lines'][0]
            credit = ex['lines'][1]
        else:
            debit = ex['lines'][1]
            credit = ex['lines'][0]

        row = [ex[k] for k in ['bmoId', 'dateEnd', 'date', 'type', 'comment']]
        row += [debit[k] for k in ['counterpartyId', 'amount', 'accountId']]
        row.append(all_accts[debit['accountId']]['display_name'])
        row += [credit[k] for k in ['counterpartyId', 'amount', 'accountId']]
        row.append(all_accts[credit['accountId']]['display_name'])

        writer.writerow(row)

    return response