From 2f969182c35e86b2c57f20cfd2e26964ca6c8843 Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Tue, 26 Jan 2016 14:55:20 +0100 Subject: [PATCH] [ADD] account_extra_reports: 2 reports from saas6 rehabilitated (sale/purchase journal printing and partner ledger) --- addons/account_extra_reports/__init__.py | 5 + addons/account_extra_reports/__openerp__.py | 28 +++++ .../data/account_report.xml | 24 ++++ .../account_extra_reports/report/__init__.py | 5 + .../report/account_journal.py | 106 +++++++++++++++++ .../report/account_partner_ledger.py | 111 ++++++++++++++++++ .../views/report_journal.xml | 103 ++++++++++++++++ .../views/report_partnerledger.xml | 91 ++++++++++++++ .../account_extra_reports/wizard/__init__.py | 6 + .../wizard/account_report_common_journal.py | 16 +++ .../wizard/account_report_partner_ledger.py | 18 +++ .../account_report_partner_ledger_view.xml | 48 ++++++++ .../wizard/account_report_print_journal.py | 17 +++ .../account_report_print_journal_view.xml | 40 +++++++ 14 files changed, 618 insertions(+) create mode 100644 addons/account_extra_reports/__init__.py create mode 100644 addons/account_extra_reports/__openerp__.py create mode 100644 addons/account_extra_reports/data/account_report.xml create mode 100644 addons/account_extra_reports/report/__init__.py create mode 100644 addons/account_extra_reports/report/account_journal.py create mode 100644 addons/account_extra_reports/report/account_partner_ledger.py create mode 100644 addons/account_extra_reports/views/report_journal.xml create mode 100644 addons/account_extra_reports/views/report_partnerledger.xml create mode 100644 addons/account_extra_reports/wizard/__init__.py create mode 100644 addons/account_extra_reports/wizard/account_report_common_journal.py create mode 100644 addons/account_extra_reports/wizard/account_report_partner_ledger.py create mode 100644 addons/account_extra_reports/wizard/account_report_partner_ledger_view.xml create mode 100644 addons/account_extra_reports/wizard/account_report_print_journal.py create mode 100644 addons/account_extra_reports/wizard/account_report_print_journal_view.xml diff --git a/addons/account_extra_reports/__init__.py b/addons/account_extra_reports/__init__.py new file mode 100644 index 00000000000..b7771cae031 --- /dev/null +++ b/addons/account_extra_reports/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import report +import wizard diff --git a/addons/account_extra_reports/__openerp__.py b/addons/account_extra_reports/__openerp__.py new file mode 100644 index 00000000000..763ff021503 --- /dev/null +++ b/addons/account_extra_reports/__openerp__.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +{ + 'name': 'Extra Accounting Reports', + 'version': '1.1', + 'category': 'Accounting & Finance', + 'description': """ +Extra Accounting Reports. +==================================== + +This module adds two new reports: +* Sale/Purchase Journal report +* Partner Ledger + """, + 'website': 'https://www.odoo.com/page/accounting', + 'depends': ['account_accountant'], + 'data': [ + 'wizard/account_report_print_journal_view.xml', + 'views/report_journal.xml', + 'wizard/account_report_partner_ledger_view.xml', + 'views/report_partnerledger.xml', + 'data/account_report.xml' + ], + 'demo': [], + 'installable': True, + 'auto_install': False, +} diff --git a/addons/account_extra_reports/data/account_report.xml b/addons/account_extra_reports/data/account_report.xml new file mode 100644 index 00000000000..1264d7138bc --- /dev/null +++ b/addons/account_extra_reports/data/account_report.xml @@ -0,0 +1,24 @@ + + + + + + + + + + diff --git a/addons/account_extra_reports/report/__init__.py b/addons/account_extra_reports/report/__init__.py new file mode 100644 index 00000000000..e424cac8b5d --- /dev/null +++ b/addons/account_extra_reports/report/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import account_journal +import account_partner_ledger diff --git a/addons/account_extra_reports/report/account_journal.py b/addons/account_extra_reports/report/account_journal.py new file mode 100644 index 00000000000..77d7d8222cf --- /dev/null +++ b/addons/account_extra_reports/report/account_journal.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- + +import time +from openerp import api, models + + +class ReportJournal(models.AbstractModel): + _name = 'report.account_extra_reports.report_journal' + + def lines(self, target_move, journal_ids, sort_selection, data): + if isinstance(journal_ids, int): + journal_ids = [journal_ids] + + move_state = ['draft', 'posted'] + if target_move == 'posted': + move_state = ['posted'] + + query_get_clause = self._get_query_get_clause(data) + params = [tuple(move_state), tuple(journal_ids)] + query_get_clause[2] + query = 'SELECT "account_move_line".id FROM ' + query_get_clause[0] + ', account_move am WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' ORDER BY ' + if sort_selection == 'date': + query += '"account_move_line".date' + else: + query += 'am.name' + query += ', "account_move_line".move_id' + self.env.cr.execute(query, tuple(params)) + ids = map(lambda x: x[0], self.env.cr.fetchall()) + return self.env['account.move.line'].browse(ids) + + def _sum_debit(self, data, journal_id): + move_state = ['draft', 'posted'] + if data['form'].get('target_move', 'all') == 'posted': + move_state = ['posted'] + + query_get_clause = self._get_query_get_clause(data) + params = [tuple(move_state), tuple(journal_id.ids)] + query_get_clause[2] + self.env.cr.execute('SELECT SUM(debit) FROM ' + query_get_clause[0] + ', account_move am ' + 'WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' ', + tuple(params)) + return self.env.cr.fetchone()[0] or 0.0 + + def _sum_credit(self, data, journal_id): + move_state = ['draft', 'posted'] + if data['form'].get('target_move', 'all') == 'posted': + move_state = ['posted'] + + query_get_clause = self._get_query_get_clause(data) + params = [tuple(move_state), tuple(journal_id.ids)] + query_get_clause[2] + self.env.cr.execute('SELECT SUM(credit) FROM ' + query_get_clause[0] + ', account_move am ' + 'WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' ', + tuple(params)) + return self.env.cr.fetchone()[0] or 0.0 + + def _get_taxes(self, data, journal_id): + move_state = ['draft', 'posted'] + if data['form'].get('target_move', 'all') == 'posted': + move_state = ['posted'] + + query_get_clause = self._get_query_get_clause(data) + params = [tuple(move_state), tuple(journal_id.ids)] + query_get_clause[2] + self.env.cr.execute('SELECT DISTINCT tax_line_id FROM ' + query_get_clause[0] + ', account_move am ' + 'WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' AND tax_line_id is not null', + tuple(params)) + ids = map(lambda x: x[0], self.env.cr.fetchall()) + taxes = self.env['account.tax'].browse(ids) + res = {} + for tax in taxes: + res[tax] = {} + self.env.cr.execute('SELECT sum(debit - credit) FROM ' + query_get_clause[0] + ', account_move am ' + 'WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' AND tax_line_id = %s', + tuple(params + [tax.id])) + res[tax]['tax_amount'] = self.env.cr.fetchone()[0] or 0.0 + amls = self.env['account.move.line'].search([('tax_ids', 'in', [tax.id])]) + self.env.cr.execute('SELECT sum(debit - credit) FROM ' + query_get_clause[0] + ', account_move am ' + 'WHERE "account_move_line".move_id=am.id AND am.state IN %s AND "account_move_line".journal_id IN %s AND ' + query_get_clause[1] + ' AND "account_move_line".id in %s', + tuple(params + [tuple(amls.ids)])) + res[tax]['base_amount'] = self.env.cr.fetchone()[0] or 0.0 + if journal_id.type == 'sale': + #sales operation are credits + res[tax]['base_amount'] = res[tax]['base_amount'] * -1 + res[tax]['tax_amount'] = res[tax]['tax_amount'] * -1 + return res + + def _get_query_get_clause(self, data): + return self.env['account.move.line'].with_context(data['form'].get('used_context', {}))._query_get() + + @api.multi + def render_html(self, data): + target_move = data['form'].get('target_move', 'all') + sort_selection = data['form'].get('sort_selection', 'date') + + res = {} + for journal in data['form']['journal_ids']: + res[journal] = self.with_context(data['form'].get('used_context', {})).lines(target_move, journal, sort_selection, data) + docargs = { + 'doc_ids': data['form']['journal_ids'], + 'doc_model': self.env['account.journal'], + 'data': data, + 'docs': self.env['account.journal'].browse(data['form']['journal_ids']), + 'time': time, + 'lines': res, + 'sum_credit': self._sum_credit, + 'sum_debit': self._sum_debit, + 'get_taxes': self._get_taxes, + } + return self.env['report'].render('account_extra_reports.report_journal', docargs) diff --git a/addons/account_extra_reports/report/account_partner_ledger.py b/addons/account_extra_reports/report/account_partner_ledger.py new file mode 100644 index 00000000000..a5ca5ed2a3c --- /dev/null +++ b/addons/account_extra_reports/report/account_partner_ledger.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- + +import time +from openerp import api, models + + +class ReportPartnerLedger(models.AbstractModel): + _name = 'report.account_extra_reports.report_partnerledger' + + def _lines(self, data, partner): + full_account = [] + query_get_data = self.env['account.move.line'].with_context(data['form'].get('used_context', {}))._query_get() + reconcile_clause = "" if data['form']['reconciled'] else ' AND "account_move_line".reconciled = false ' + params = [partner.id, tuple(data['computed']['move_state']), tuple(data['computed']['account_ids'])] + query_get_data[2] + query = """ + SELECT "account_move_line".id, "account_move_line".date, j.code, acc.code as a_code, acc.name as a_name, "account_move_line".ref, m.name as move_name, "account_move_line".name, "account_move_line".debit, "account_move_line".credit, "account_move_line".amount_currency,"account_move_line".currency_id, c.symbol AS currency_code + FROM """ + query_get_data[0] + """ + LEFT JOIN account_journal j ON ("account_move_line".journal_id = j.id) + LEFT JOIN account_account acc ON ("account_move_line".account_id = acc.id) + LEFT JOIN res_currency c ON ("account_move_line".currency_id=c.id) + LEFT JOIN account_move m ON (m.id="account_move_line".move_id) + WHERE "account_move_line".partner_id = %s + AND m.state IN %s + AND "account_move_line".account_id IN %s AND """ + query_get_data[1] + reconcile_clause + """ + ORDER BY "account_move_line".date""" + self.env.cr.execute(query, tuple(params)) + res = self.env.cr.dictfetchall() + sum = 0.0 + for r in res: + r['displayed_name'] = '-'.join( + r['move_name'] not in ['', '/'] and [r['move_name']] or [] + + r['ref'] not in ['', '/'] and [r['ref']] or [] + + r['name'] not in ['', '/'] and [r['name']] or [] + ) + sum += r['debit'] - r['credit'] + r['progress'] = sum + full_account.append(r) + return full_account + + def _sum_partner(self, data, partner, field): + if field not in ['debit', 'credit', 'debit - credit']: + return + result = 0.0 + query_get_data = self.env['account.move.line'].with_context(data['form'].get('used_context', {}))._query_get() + reconcile_clause = "" if data['form']['reconciled'] else ' AND "account_move_line".reconciled = false ' + + params = [partner.id, tuple(data['computed']['move_state']), tuple(data['computed']['account_ids'])] + query_get_data[2] + query = """SELECT sum(""" + field + """) + FROM """ + query_get_data[0] + """, account_move AS m + WHERE "account_move_line".partner_id = %s + AND m.id = "account_move_line".move_id + AND m.state IN %s + AND account_id IN %s + AND """ + query_get_data[1] + reconcile_clause + self.env.cr.execute(query, tuple(params)) + + contemp = self.env.cr.fetchone() + if contemp is not None: + result = contemp[0] or 0.0 + return result + + @api.multi + def render_html(self, data): + data['computed'] = {} + + obj_partner = self.env['res.partner'] + query_get_data = self.env['account.move.line'].with_context(data['form'].get('used_context', {}))._query_get() + data['computed']['move_state'] = ['draft', 'posted'] + if data['form'].get('target_move', 'all') == 'posted': + data['computed']['move_state'] = ['posted'] + result_selection = data['form'].get('result_selection', 'customer') + if result_selection == 'supplier': + data['computed']['ACCOUNT_TYPE'] = ['payable'] + elif result_selection == 'customer': + data['computed']['ACCOUNT_TYPE'] = ['receivable'] + else: + data['computed']['ACCOUNT_TYPE'] = ['payable', 'receivable'] + + self.env.cr.execute(""" + SELECT a.id + FROM account_account a + WHERE a.internal_type IN %s + AND NOT a.deprecated""", (tuple(data['computed']['ACCOUNT_TYPE']),)) + data['computed']['account_ids'] = [a for (a,) in self.env.cr.fetchall()] + params = [tuple(data['computed']['move_state']), tuple(data['computed']['account_ids'])] + query_get_data[2] + reconcile_clause = "" if data['form']['reconciled'] else ' AND "account_move_line".reconciled = false ' + query = """ + SELECT DISTINCT "account_move_line".partner_id + FROM """ + query_get_data[0] + """, account_account AS account, account_move AS am + WHERE "account_move_line".partner_id IS NOT NULL + AND "account_move_line".account_id = account.id + AND am.id = "account_move_line".move_id + AND am.state IN %s + AND "account_move_line".account_id IN %s + AND NOT account.deprecated + AND """ + query_get_data[1] + reconcile_clause + self.env.cr.execute(query, tuple(params)) + partner_ids = [res['partner_id'] for res in self.env.cr.dictfetchall()] + partners = obj_partner.browse(partner_ids) + partners = sorted(partners, key=lambda x: (x.ref, x.name)) + + docargs = { + 'doc_ids': partner_ids, + 'doc_model': self.env['res.partner'], + 'data': data, + 'docs': partners, + 'time': time, + 'lines': self._lines, + 'sum_partner': self._sum_partner, + } + return self.env['report'].render('account_extra_reports.report_partnerledger', docargs) diff --git a/addons/account_extra_reports/views/report_journal.xml b/addons/account_extra_reports/views/report_journal.xml new file mode 100644 index 00000000000..73926363f09 --- /dev/null +++ b/addons/account_extra_reports/views/report_journal.xml @@ -0,0 +1,103 @@ + + + + diff --git a/addons/account_extra_reports/views/report_partnerledger.xml b/addons/account_extra_reports/views/report_partnerledger.xml new file mode 100644 index 00000000000..e8cb8c3e263 --- /dev/null +++ b/addons/account_extra_reports/views/report_partnerledger.xml @@ -0,0 +1,91 @@ + + + + diff --git a/addons/account_extra_reports/wizard/__init__.py b/addons/account_extra_reports/wizard/__init__.py new file mode 100644 index 00000000000..7755d1ec568 --- /dev/null +++ b/addons/account_extra_reports/wizard/__init__.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import account_report_common_journal +import account_report_print_journal +import account_report_partner_ledger diff --git a/addons/account_extra_reports/wizard/account_report_common_journal.py b/addons/account_extra_reports/wizard/account_report_common_journal.py new file mode 100644 index 00000000000..2108d655152 --- /dev/null +++ b/addons/account_extra_reports/wizard/account_report_common_journal.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- + +from openerp import api, fields, models + + +class AccountCommonJournalReport(models.TransientModel): + _name = 'account.common.journal.report' + _description = 'Account Common Journal Report' + _inherit = "account.common.report" + + amount_currency = fields.Boolean('With Currency', help="Print Report with the currency column if the currency differs from the company currency.") + + @api.multi + def pre_print_report(self, data): + data['form'].update({'amount_currency': self.amount_currency}) + return data diff --git a/addons/account_extra_reports/wizard/account_report_partner_ledger.py b/addons/account_extra_reports/wizard/account_report_partner_ledger.py new file mode 100644 index 00000000000..0a92f43eca7 --- /dev/null +++ b/addons/account_extra_reports/wizard/account_report_partner_ledger.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- + +from openerp import fields, models, _ +from openerp.exceptions import UserError + + +class AccountPartnerLedger(models.TransientModel): + _inherit = "account.common.partner.report" + _name = "account.report.partner.ledger" + _description = "Account Partner Ledger" + + amount_currency = fields.Boolean("With Currency", help="It adds the currency column on report if the currency differs from the company currency.") + reconciled = fields.Boolean('Reconciled Entries') + + def _print_report(self, data): + data = self.pre_print_report(data) + data['form'].update({'reconciled': self.reconciled, 'amount_currency': self.amount_currency}) + return self.env['report'].get_action(self, 'account_extra_reports.report_partnerledger', data=data) diff --git a/addons/account_extra_reports/wizard/account_report_partner_ledger_view.xml b/addons/account_extra_reports/wizard/account_report_partner_ledger_view.xml new file mode 100644 index 00000000000..d50e048de63 --- /dev/null +++ b/addons/account_extra_reports/wizard/account_report_partner_ledger_view.xml @@ -0,0 +1,48 @@ + + + + + Partner Ledger + account.report.partner.ledger + + + + + + + + + + + + + + + + Partner Ledger + ir.actions.act_window + account.report.partner.ledger + form + form + + new + + + + + Partner Ledger + client_print_multi + + action + account.account + + + + + diff --git a/addons/account_extra_reports/wizard/account_report_print_journal.py b/addons/account_extra_reports/wizard/account_report_print_journal.py new file mode 100644 index 00000000000..d8b465b2a96 --- /dev/null +++ b/addons/account_extra_reports/wizard/account_report_print_journal.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- + +from openerp import fields, models + + +class AccountPrintJournal(models.TransientModel): + _inherit = "account.common.journal.report" + _name = "account.print.journal" + _description = "Account Print Journal" + + sort_selection = fields.Selection([('date', 'Date'), ('move_name', 'Journal Entry Number'),], 'Entries Sorted by', required=True, default='move_name') + journal_ids = fields.Many2many('account.journal', string='Journals', required=True, default=lambda self: self.env['account.journal'].search([('type', 'in', ['sale', 'purchase'])])) + + def _print_report(self, data): + data = self.pre_print_report(data) + data['form'].update({'sort_selection': self.sort_selection}) + return self.env['report'].with_context(landscape=True).get_action(self, 'account_extra_reports.report_journal', data=data) diff --git a/addons/account_extra_reports/wizard/account_report_print_journal_view.xml b/addons/account_extra_reports/wizard/account_report_print_journal_view.xml new file mode 100644 index 00000000000..e40cac57f26 --- /dev/null +++ b/addons/account_extra_reports/wizard/account_report_print_journal_view.xml @@ -0,0 +1,40 @@ + + + + + Sale/Purchase Journal + account.print.journal + + + + + + + + + + [('type', 'in', ('sale', 'purchase'))] + + + + + + + Sale/Purchase Journal + ir.actions.act_window + account.print.journal + form + form + + new + + + + +