diff --git a/cashflow.py b/cashflow.py index 46cf248..e4c845a 100644 --- a/cashflow.py +++ b/cashflow.py @@ -3,6 +3,7 @@ # the full copyright notices and license terms. from decimal import Decimal from dateutil.relativedelta import relativedelta +from datetime import date, timedelta from trytond.model import ModelView, fields from trytond.wizard import Wizard, StateView, StateReport, Button @@ -200,23 +201,70 @@ def _get_sale_summary(cls, columns, sales_raw): return records @classmethod - def _get_purchase_records(cls): - return [] + def _get_purchase_records(cls, company_id, analytic_account, from_date, to_date): + pool = Pool() + Company = pool.get('company.company') + Currency = pool.get('currency.currency') + PurchaseLine = pool.get('purchase.line') + AnalyticAccount = pool.get('analytic_account.account') + + records = {} + company = Company(company_id) + + clause = [ + ('purchase.company', '=', company), + ('purchase.state', 'in', + ['projected', 'confirmed', 'processing', 'done']), + ('type', '=', 'line'), + ('purchase.purchase_date', '>=', from_date), + ('purchase.purchase_date', '<=', to_date), + ] + purchase_lines = PurchaseLine.search(clause, + order=[('purchase.purchase_date', 'ASC')]) + if not purchase_lines: + return records + + for line in purchase_lines: + year = line.purchase_date.year + month = line.purchase_date.month + category_id = None + for analytic_line in line.analytic_accounts: + if (analytic_line.root.id == analytic_account and + analytic_line.account): + category_id = analytic_line.account.id + key = (year, month, category_id) + if key not in records: + records[key] = { + 'year': year, + 'month': month, + 'category': (category_id and + AnalyticAccount(category_id).name or ''), + 'amount': Decimal(0), + } + with Transaction().set_context(date=line.purchase.purchase_date): + records[key]['amount'] += Currency.compute( + company.currency, line.amount, line.currency) + return records @classmethod def _get_expense_records(cls, data): pool = Pool() MoveLine = pool.get('account.move.line') AnalyticAccount = pool.get('analytic_account.account') + Date = pool.get('ir.date') + today = Date.today() records = {} + # last date of previous month + to_date_expenses = today.replace(day=1) - timedelta(days=1) + clause = [ ('move.company', '=', data['company']), ('account.cashflow_report', '=', True), ('move.state', '=', 'posted'), ('move.date', '>=', data['from_date']), - ('move.date', '<=', data['to_date']), + ('move.date', '<=', to_date_expenses), ] move_lines = MoveLine.search(clause, order=[('move.date', 'ASC')]) @@ -243,7 +291,14 @@ def _get_expense_records(cls, data): } with Transaction().set_context(date=line.move.date): records[key]['amount'] += abs(line.debit - line.credit) - return records + + # first date of current month + from_date_purchase = today.replace(day=1) + to_date_purchase = data['to_date'] + purchase_records = cls._get_purchase_records(data['company'], data['analytic_account'], + from_date_purchase, to_date_purchase) + + return records | purchase_records @classmethod def _get_expense_summary(cls, columns, expenses_raw):