def make_expense_stubs(cf_data): """ given a list of cashflows create expenses from those cashflows which have not already had an expense created from them where the cashflows were booked versus Accounts Payable """ today = datetime.datetime.now().date() stub_account = env_api.variable('UNALLOCATED_ACCT', {}) unallocated_employee = env_api.variable('UNALLOCATED_EMPLOYEE_ID', {}) ap_account = env_api.variable('GL_ACCOUNTS_PAYABLE', {}) new_stubs = 0 from_AP = [cf for cf in cf_data if cf['trans_type_id'] == ap_account] for cf in from_AP: if Expense.objects.filter(from_cf_id=cf['id']).count() == 0: new_stubs += 1 # if expense acct is on the cashflow then use that if cf['expense_acct_id']: account_id = cf['expense_acct_id'] else: account_id = stub_account Expense(comment=cf['description'], counterparty_id=cf['counterparty_id'], account_id=account_id, from_cf_id=cf['id'], expense_date=cf['post_date'], start_date=cf['post_date'], amount=-cf['amount'], stub=False, paid_from_id=cf['trans_type_id'], process_date=today, employee_id=unallocated_employee).save() return {'new': new_stubs, 'duplicates': len(from_AP) - new_stubs}
def make_stubs_from_ccard(cc_data): """ given a list of credit card transactions create expenses from those credit card trans which have not already had an expense created from them """ today = datetime.datetime.now().date() stub_account = env_api.variable('UNALLOCATED_ACCT', {}) unallocated_employee = env_api.variable('UNALLOCATED_EMPLOYEE_ID', {}) ap_account = env_api.variable('GL_ACCOUNTS_PAYABLE', {}) new_stubs = 0 for cc in cc_data: if Expense.objects.filter(from_ccard_id=cc['id']).count()==0: new_stubs += 1 # if expense acct is on the cashflow then use that if cc['expense_acct_id']: account_id = cc['expense_acct_id'] else: account_id = stub_account Expense(comment=cc['description'], counterparty_id=cc['counterparty_id'], account_id=account_id, from_ccard_id=cc['id'], expense_date=cc['trans_date'], start_date=cc['post_date'], amount=-cc['amount'], stub=False, paid_from_id=ap_account, process_date=today, employee_id=unallocated_employee).save() return {'new': new_stubs, 'duplicates': len(cc_data) - new_stubs}
def queryset(self, request, qs): unalloc_account = env_api.variable('UNALLOCATED_ACCT', {}) if self.value() == 'UNMATCHED': return qs.filter(account_id=unalloc_account) if self.value() == 'MATCHED': return qs.exclude(account_id=unalloc_account)
def get_gl_transactions(self): """We just debit the expense account and credit the generic Accounts Payable, whoever it is. We do not at this stage try to handle anything on how the debt was paid off. """ capitalize_it, debit, acc_asset_dep, months = self._capitalize(self.account) ACCTS_PAYABLE = accountifie.gl.models.Account.objects.get(id=env_api.variable('GL_ACCOUNTS_PAYABLE', {})) PREPAID_EXP = accountifie.gl.models.Account.objects.get(id=env_api.variable('GL_PREPAID_EXP', {})) ACCRUED_LIAB = accountifie.gl.models.Account.objects.get(id=env_api.variable('GL_ACCRUED_LIAB', {})) trans = [] # now three different paths if capitalize_it: # book to asset account tran = dict( company=self.company, date=self.start_date, date_end=None, trans_id='%s.%s.%s' % (self.short_code, self.id, 'CPLZ'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "Capitalized Asset, %s: %s" % (self.id, self.comment), lines=[(ACCTS_PAYABLE, DZERO - Decimal(self.amount), self.counterparty, [])] ) tran['lines'] += self._get_exp_lines(debit) trans.append(tran) # and now amort/deprec over appropriate time period amort_accts = accountifie.gl.models.Account.objects.filter(path=debit.path + '.amortization') if len(amort_accts) > 0: acc_pl_dep = amort_accts[0] deprec_accts = accountifie.gl.models.Account.objects.filter(path=debit.path + '.depreciation') if len(deprec_accts) > 0: acc_pl_dep = deprec_accts[0] trans.append(dict( company=self.company, date=self.start_date, date_end=self.start_date + relativedelta(months=months), trans_id='%s.%s.%s' % (self.short_code, self.id, 'DPRC'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "Depreciating asset, %s: %s" % (self.id, self.comment), lines=[ (acc_pl_dep, DZERO - Decimal(self.amount), self.counterparty, []), (acc_asset_dep, Decimal(self.amount), self.counterparty, []), ] )) elif self.end_date is not None and self.start_date != self.end_date and abs(self.amount) >=500.0: if self.expense_date == self.start_date: # paid in advance # create account payable trans.append(dict( company=self.company, date=self.start_date, date_end=None, trans_id='%s.%s.%s' % (self.short_code, self.id, 'AP'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "AP for %s: %s" % (self.id, self.comment), lines=[ (PREPAID_EXP, Decimal(self.amount), self.counterparty, []), (ACCTS_PAYABLE, DZERO - Decimal(self.amount), self.counterparty, []), ] )) # expense over period tran = dict( company=self.company, date=self.start_date, date_end=self.end_date, trans_id='%s.%s.%s' % (self.short_code, self.id, 'EXPS'), bmo_id=self.id, comment= "Expensing %s: %s" % (self.id, self.comment), lines=[(PREPAID_EXP, DZERO - Decimal(self.amount), self.counterparty, []),] ) tran['lines'] += self._get_exp_lines(debit) trans.append(tran) else: # paid in arrears # transfer from accrued liab to account payable at end of period trans.append(dict( company=self.company, date=self.end_date, date_end=None, trans_id='%s.%s.%s' % (self.short_code, self.id, 'AL2AP'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "AP for %s: %s" % (self.id, self.comment), lines=[ (ACCRUED_LIAB, Decimal(self.amount), self.counterparty, []), (ACCTS_PAYABLE, DZERO - Decimal(self.amount), self.counterparty, []), ] )) # accrue expense over period tran = dict( company=self.company, date=self.start_date, date_end=self.end_date, trans_id='%s.%s.%s' % (self.short_code, self.id, 'AL'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "Accruing %s: %s" % (self.id, self.comment), lines=[(ACCRUED_LIAB, DZERO - Decimal(self.amount), self.counterparty, []),] ) tran['lines'] += self._get_exp_lines(debit) trans.append(tran) else: # single date tran = dict( company=self.company, date=self.start_date, date_end=None, trans_id='%s.%s.%s' % (self.short_code, self.id, 'EXP'), bmo_id='%s.%s' % (self.short_code, self.id), comment= "%s: %s" % (self.id, self.comment), lines=[(ACCTS_PAYABLE, DZERO - Decimal(self.amount), self.counterparty, []),] ) tran['lines'] += self._get_exp_lines(debit) trans.append(tran) return trans