From a8db3831ac21491d13a5417a340b5d02e35c6190 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 4 Apr 2018 12:58:35 +0200 Subject: [PATCH 1/2] [ADD] foreign currency total in xlsx General ledger report --- .../report/abstract_report_xlsx.py | 49 ++++ .../report/general_ledger.py | 58 +++-- .../report/general_ledger_xlsx.py | 9 +- .../report/open_items.py | 228 ++++++++++++++---- .../report/open_items_xlsx.py | 8 +- .../report/templates/general_ledger.xml | 18 +- .../report/templates/open_items.xml | 18 +- .../report/templates/trial_balance.xml | 6 +- .../report/trial_balance.py | 12 +- 9 files changed, 311 insertions(+), 95 deletions(-) diff --git a/account_financial_report_qweb/report/abstract_report_xlsx.py b/account_financial_report_qweb/report/abstract_report_xlsx.py index 32d5e803..adc6661d 100644 --- a/account_financial_report_qweb/report/abstract_report_xlsx.py +++ b/account_financial_report_qweb/report/abstract_report_xlsx.py @@ -108,6 +108,22 @@ class AbstractReportXslx(ReportXlsx): ) self.format_percent_bold_italic.set_num_format('#,##0.00%') + def _get_currency_amt_format(self, line_object): + """ Return amount format specific for each currency. """ + format_amt = getattr(self, 'format_amount') + if line_object.currency_id: + field_name = \ + 'format_amount_%s' % line_object.currency_id.name + if hasattr(self, field_name): + format_amt = getattr(self, field_name) + else: + format_amt = self.workbook.add_format() + setattr(self, 'field_name', format_amt) + format_amount = \ + '#,##0.' + ('0' * line_object.currency_id.decimal_places) + format_amt.set_num_format(format_amount) + return format_amt + def _set_column_width(self): """Set width for all defined columns. Columns are defined with `_get_report_columns` method. @@ -189,6 +205,14 @@ class AbstractReportXslx(ReportXlsx): self.sheet.write_number( self.row_pos, col_pos, float(value), self.format_amount ) + elif cell_type == 'amount_currency': + if line_object.currency_id: + format_amt = self._get_currency_amt_format(line_object) + self.sheet.write_number( + self.row_pos, col_pos, float(value), format_amt) + elif cell_type == 'many2one': + self.sheet.write_string( + self.row_pos, col_pos, value.name or '', self.format_right) self.row_pos += 1 def write_initial_balance(self, my_object, label): @@ -209,6 +233,19 @@ class AbstractReportXslx(ReportXlsx): self.sheet.write_number( self.row_pos, col_pos, float(value), self.format_amount ) + elif cell_type == 'amount_currency': + if my_object.currency_id: + format_amt = self._get_currency_amt_format( + my_object) + self.sheet.write_number( + self.row_pos, col_pos, + float(value), format_amt + ) + elif cell_type == 'many2one': + self.sheet.write_string( + self.row_pos, col_pos, value.name or '', + self.format_header_right) + self.row_pos += 1 def write_ending_balance(self, my_object, name, label): @@ -239,6 +276,18 @@ class AbstractReportXslx(ReportXlsx): self.row_pos, col_pos, float(value), self.format_header_amount ) + elif cell_type == 'amount_currency': + if my_object.currency_id: + format_amt = self._get_currency_amt_format( + my_object) + self.sheet.write_number( + self.row_pos, col_pos, + float(value), format_amt + ) + elif cell_type == 'many2one': + self.sheet.write_string( + self.row_pos, col_pos, value.name or '', + self.format_header_right) self.row_pos += 1 def _generate_report_content(self, workbook, report): diff --git a/account_financial_report_qweb/report/general_ledger.py b/account_financial_report_qweb/report/general_ledger.py index e2ceae31..8ebb2151 100644 --- a/account_financial_report_qweb/report/general_ledger.py +++ b/account_financial_report_qweb/report/general_ledger.py @@ -91,7 +91,7 @@ class GeneralLedgerReportAccount(models.TransientModel): initial_debit = fields.Float(digits=(16, 2)) initial_credit = fields.Float(digits=(16, 2)) initial_balance = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') initial_balance_foreign_currency = fields.Float(digits=(16, 2)) final_debit = fields.Float(digits=(16, 2)) final_credit = fields.Float(digits=(16, 2)) @@ -133,7 +133,7 @@ class GeneralLedgerReportPartner(models.TransientModel): initial_debit = fields.Float(digits=(16, 2)) initial_credit = fields.Float(digits=(16, 2)) initial_balance = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') initial_balance_foreign_currency = fields.Float(digits=(16, 2)) final_debit = fields.Float(digits=(16, 2)) final_credit = fields.Float(digits=(16, 2)) @@ -191,7 +191,7 @@ class GeneralLedgerReportMoveLine(models.TransientModel): debit = fields.Float(digits=(16, 2)) credit = fields.Float(digits=(16, 2)) cumul_balance = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') amount_currency = fields.Float(digits=(16, 2)) @@ -271,8 +271,12 @@ class GeneralLedgerReportCompute(models.TransientModel): SUM(ml.debit) AS debit, SUM(ml.credit) AS credit, SUM(ml.balance) AS balance, - c.name AS currency_name, - SUM(ml.amount_currency) AS balance_currency + c.id AS currency_id, + CASE + WHEN c.id IS NOT NULL + THEN SUM(ml.amount_currency) + ELSE NULL + END AS balance_currency FROM accounts a INNER JOIN @@ -319,7 +323,7 @@ class GeneralLedgerReportCompute(models.TransientModel): """ sub_subquery_sum_amounts += """ GROUP BY - a.id, c.name + a.id, c.id """ return sub_subquery_sum_amounts @@ -331,7 +335,7 @@ class GeneralLedgerReportCompute(models.TransientModel): SUM(COALESCE(sub.debit, 0.0)) AS debit, SUM(COALESCE(sub.credit, 0.0)) AS credit, SUM(COALESCE(sub.balance, 0.0)) AS balance, - MAX(sub.currency_name) AS currency_name, + MAX(sub.currency_id) AS currency_id, SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency FROM ( @@ -431,7 +435,7 @@ INSERT INTO initial_debit, initial_credit, initial_balance, - currency_name, + currency_id, initial_balance_foreign_currency, final_debit, final_credit, @@ -449,7 +453,7 @@ SELECT COALESCE(i.debit, 0.0) AS initial_debit, COALESCE(i.credit, 0.0) AS initial_credit, COALESCE(i.balance, 0.0) AS initial_balance, - c.name AS currency_name, + a.currency_id, COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency, COALESCE(f.debit, 0.0) AS final_debit, COALESCE(f.credit, 0.0) AS final_credit, @@ -462,8 +466,6 @@ LEFT JOIN initial_sum_amounts i ON a.id = i.account_id LEFT JOIN final_sum_amounts f ON a.id = f.account_id -LEFT JOIN - res_currency c ON c.id = a.currency_id WHERE ( i.debit IS NOT NULL AND i.debit != 0 @@ -543,14 +545,18 @@ AND SUM(ml.debit) AS debit, SUM(ml.credit) AS credit, SUM(ml.balance) AS balance, - c.name as currency_name, - SUM(ml.amount_currency) AS balance_currency + c.id as currency_id, + CASE + WHEN c.id IS NOT NULL + THEN SUM(ml.amount_currency) + ELSE NULL + END AS balance_currency FROM accounts_partners ap INNER JOIN account_account ac ON ac.id = ap.account_id LEFT JOIN - res_currency c ON ap.account_id = c.id + res_currency c ON ac.currency_id = c.id INNER JOIN account_move_line ml ON ap.account_id = ml.account_id @@ -594,7 +600,7 @@ AND """ sub_subquery_sum_amounts += """ GROUP BY - ap.account_id, ap.partner_id, c.name + ap.account_id, ap.partner_id, c.id """ return sub_subquery_sum_amounts @@ -610,7 +616,7 @@ AND SUM(COALESCE(sub.debit, 0.0)) AS debit, SUM(COALESCE(sub.credit, 0.0)) AS credit, SUM(COALESCE(sub.balance, 0.0)) AS balance, - MAX(sub.currency_name) AS currency_name, + MAX(sub.currency_id) AS currency_id, SUM(COALESCE(sub.balance_currency, 0.0)) AS balance_currency FROM ( @@ -736,7 +742,7 @@ INSERT INTO initial_debit, initial_credit, initial_balance, - currency_name, + currency_id, initial_balance_foreign_currency, final_debit, final_credit, @@ -752,7 +758,7 @@ SELECT COALESCE(i.debit, 0.0) AS initial_debit, COALESCE(i.credit, 0.0) AS initial_credit, COALESCE(i.balance, 0.0) AS initial_balance, - i.currency_name AS currency_name, + i.currency_id AS currency_id, COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency, COALESCE(f.debit, 0.0) AS final_debit, COALESCE(f.credit, 0.0) AS final_credit, @@ -900,7 +906,7 @@ INSERT INTO debit, credit, cumul_balance, - currency_name, + currency_id, amount_currency ) SELECT @@ -989,7 +995,7 @@ SELECT ) AS cumul_balance, """ query_inject_move_line += """ - c.name AS currency_name, + c.id AS currency_id, ml.amount_currency FROM """ @@ -1252,7 +1258,7 @@ SET ON l.report_account_id = a.id WHERE a.report_id = %s - AND l.currency_name IS NOT NULL + AND l.currency_id IS NOT NULL LIMIT 1 ) OR @@ -1269,7 +1275,7 @@ SET ON p.report_account_id = a.id WHERE a.report_id = %s - AND l.currency_name IS NOT NULL + AND l.currency_id IS NOT NULL LIMIT 1 ) WHERE id = %s @@ -1391,7 +1397,7 @@ WHERE id = %s is_partner_account, initial_balance, final_balance, - currency_name + currency_id ) SELECT %(report_id)s AS report_id, @@ -1403,9 +1409,11 @@ WHERE id = %s False AS is_partner_account, COALESCE(i.initial_balance, 0.0) AS initial_balance, COALESCE(i.final_balance, 0.0) AS final_balance, - '' + c.id as currency_id FROM - account_account a, + account_account a + LEFT JOIN + res_currency c ON c.id = a.currency_id, sum_amounts i WHERE a.company_id = %(company_id)s diff --git a/account_financial_report_qweb/report/general_ledger_xlsx.py b/account_financial_report_qweb/report/general_ledger_xlsx.py index a52f0ee9..9a97ba5a 100644 --- a/account_financial_report_qweb/report/general_ledger_xlsx.py +++ b/account_financial_report_qweb/report/general_ledger_xlsx.py @@ -54,9 +54,16 @@ class GeneralLedgerXslx(abstract_report_xlsx.AbstractReportXslx): 'field_final_balance': 'final_balance', 'type': 'amount', 'width': 14}, - 12: {'header': _('Cur.'), 'field': 'currency_name', 'width': 7}, + 12: {'header': _('Cur.'), + 'field': 'currency_id', + 'field_initial_balance': 'currency_id', + 'field_final_balance': 'currency_id', + 'type': 'many2one', + 'width': 7}, 13: {'header': _('Amount cur.'), 'field': 'amount_currency', + 'field_initial_balance': 'initial_balance_foreign_currency', + 'field_final_balance': 'final_balance_foreign_currency', 'type': 'amount', 'width': 14}, } diff --git a/account_financial_report_qweb/report/open_items.py b/account_financial_report_qweb/report/open_items.py index f9005b5a..21ac0cec 100644 --- a/account_financial_report_qweb/report/open_items.py +++ b/account_financial_report_qweb/report/open_items.py @@ -26,9 +26,6 @@ class OpenItemsReport(models.TransientModel): filter_account_ids = fields.Many2many(comodel_name='account.account') filter_partner_ids = fields.Many2many(comodel_name='res.partner') - # Flag fields, used for report display - has_second_currency = fields.Boolean() - # Data fields, used to browse report data account_ids = fields.One2many( comodel_name='report_open_items_qweb_account', @@ -56,7 +53,11 @@ class OpenItemsReportAccount(models.TransientModel): # Data fields, used for report display code = fields.Char() name = fields.Char() + currency_name = fields.Char() final_amount_residual = fields.Float(digits=(16, 2)) + final_amount_total_due = fields.Float(digits=(16, 2)) + final_amount_residual_currency = fields.Float(digits=(16, 2)) + final_amount_total_due_currency = fields.Float(digits=(16, 2)) # Data fields, used to browse report data partner_ids = fields.One2many( @@ -83,7 +84,11 @@ class OpenItemsReportPartner(models.TransientModel): # Data fields, used for report display name = fields.Char() + currency_name = fields.Char() final_amount_residual = fields.Float(digits=(16, 2)) + final_amount_total_due = fields.Float(digits=(16, 2)) + final_amount_residual_currency = fields.Float(digits=(16, 2)) + final_amount_total_due_currency = fields.Float(digits=(16, 2)) # Data fields, used to browse report data move_line_ids = fields.One2many( @@ -167,8 +172,6 @@ class OpenItemsReportCompute(models.TransientModel): self._clean_partners_and_accounts( only_delete_account_balance_at_0=True ) - # Compute display flag - self._compute_has_second_currency() # Refresh cache because all data are computed with SQL requests self.invalidate_cache() @@ -182,11 +185,14 @@ WITH a.id, a.code, a.name, - a.user_type_id + a.user_type_id, + c.name as currency_name FROM account_account a INNER JOIN account_move_line ml ON a.id = ml.account_id AND ml.date <= %s + LEFT JOIN + res_currency c ON a.currency_id = c.id """ if self.filter_partner_ids: query_inject_account += """ @@ -215,7 +221,7 @@ WITH """ query_inject_account += """ GROUP BY - a.id + a.id, c.name ) INSERT INTO report_open_items_qweb_account @@ -224,6 +230,7 @@ INSERT INTO create_uid, create_date, account_id, + currency_name, code, name ) @@ -232,6 +239,7 @@ SELECT %s AS create_uid, NOW() AS create_date, a.id AS account_id, + a.currency_name, a.code, a.name FROM @@ -606,7 +614,25 @@ ORDER BY """ Compute cumulative amount for report_open_items_qweb_partner and report_open_items_qweb_account. """ - query_compute_partners_cumul = """ + self._compute_partner_cumul() + self._compute_account_cumul() + + def _compute_partner_cumul(self): + where_condition_partner_by_account = """ +WHERE + id IN + ( + SELECT + rp.id + FROM + report_open_items_qweb_account ra + INNER JOIN + report_open_items_qweb_partner rp + ON ra.id = rp.report_account_id + WHERE + ra.report_id = %s + )""" + query_compute_partners_residual_cumul = """ UPDATE report_open_items_qweb_partner SET @@ -619,6 +645,31 @@ SET WHERE rml.report_partner_id = report_open_items_qweb_partner.id ) +""" + where_condition_partner_by_account + params_compute_partners_residual_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_residual_cumul, + params_compute_partners_residual_cumul) + + query_compute_partners_due_cumul = """ +UPDATE + report_open_items_qweb_partner +SET + final_amount_total_due = + ( + SELECT + SUM(rml.amount_total_due) AS final_amount_total_due + FROM + report_open_items_qweb_move_line rml + WHERE + rml.report_partner_id = report_open_items_qweb_partner.id + ) +""" + where_condition_partner_by_account + params_compute_partners_due_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_due_cumul, + params_compute_partners_due_cumul) + + # Manage currency in partner + where_condition_partner_by_account_cur = """ WHERE id IN ( @@ -630,13 +681,66 @@ WHERE report_open_items_qweb_partner rp ON ra.id = rp.report_account_id WHERE - ra.report_id = %s + ra.report_id = %s AND ra.currency_name IS NOT NULL + )""" + query_compute_partners_cur_name_cumul = """ +UPDATE + report_open_items_qweb_partner +SET + currency_name = + ( + SELECT + MAX(currency_name) as currency_name + FROM + report_open_items_qweb_move_line rml + WHERE + rml.report_partner_id = report_open_items_qweb_partner.id ) - """ - params_compute_partners_cumul = (self.id,) - self.env.cr.execute(query_compute_partners_cumul, - params_compute_partners_cumul) - query_compute_accounts_cumul = """ +""" + where_condition_partner_by_account_cur + params_compute_partners_cur_name_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_cur_name_cumul, + params_compute_partners_cur_name_cumul) + + query_compute_partners_cur_residual_cumul = """ +UPDATE + report_open_items_qweb_partner +SET + final_amount_residual_currency = + ( + SELECT + SUM(rml.amount_residual_currency) + AS final_amount_residual_currency + FROM + report_open_items_qweb_move_line rml + WHERE + rml.report_partner_id = report_open_items_qweb_partner.id + ) +""" + where_condition_partner_by_account_cur + params_compute_partners_cur_residual_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_cur_residual_cumul, + params_compute_partners_cur_residual_cumul) + + query_compute_partners_cur_due_cumul = """ +UPDATE + report_open_items_qweb_partner +SET + final_amount_total_due_currency = + ( + SELECT + SUM(rml.amount_total_due_currency) + AS final_amount_total_due_currency + FROM + report_open_items_qweb_move_line rml + WHERE + rml.report_partner_id = report_open_items_qweb_partner.id + ) +""" + where_condition_partner_by_account_cur + params_compute_partners_cur_due_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_cur_due_cumul, + params_compute_partners_cur_due_cumul) + + def _compute_account_cumul(self): + query_compute_accounts_residual_cumul = """ UPDATE report_open_items_qweb_account SET @@ -652,9 +756,71 @@ SET WHERE report_id = %s """ - params_compute_accounts_cumul = (self.id,) - self.env.cr.execute(query_compute_accounts_cumul, - params_compute_accounts_cumul) + params_compute_accounts_residual_cumul = (self.id,) + self.env.cr.execute(query_compute_accounts_residual_cumul, + params_compute_accounts_residual_cumul) + + query_compute_accounts_cur_residual_cumul = """ +UPDATE + report_open_items_qweb_account +SET + final_amount_residual_currency = + ( + SELECT + SUM(rp.final_amount_residual_currency) + AS final_amount_residual_currency + FROM + report_open_items_qweb_partner rp + WHERE + rp.report_account_id = report_open_items_qweb_account.id + ) +WHERE + report_id = %s + """ + params_compute_accounts_cur_residual_cumul = (self.id,) + self.env.cr.execute(query_compute_accounts_cur_residual_cumul, + params_compute_accounts_cur_residual_cumul) + + query_compute_accounts_due_cumul = """ +UPDATE + report_open_items_qweb_account +SET + final_amount_total_due = + ( + SELECT + SUM(rp.final_amount_total_due) AS final_amount_total_due + FROM + report_open_items_qweb_partner rp + WHERE + rp.report_account_id = report_open_items_qweb_account.id + ) +WHERE + report_id = %s + """ + params_compute_accounts_due_cumul = (self.id,) + self.env.cr.execute(query_compute_accounts_due_cumul, + params_compute_accounts_due_cumul) + + query_compute_accounts_cur_due_cumul = """ +UPDATE + report_open_items_qweb_account +SET + final_amount_total_due_currency = + ( + SELECT + SUM(rp.final_amount_total_due_currency) + AS final_amount_total_due_currency + FROM + report_open_items_qweb_partner rp + WHERE + rp.report_account_id = report_open_items_qweb_account.id + ) +WHERE + report_id = %s + """ + params_compute_accounts_cur_due_cumul = (self.id,) + self.env.cr.execute(query_compute_accounts_cur_due_cumul, + params_compute_accounts_cur_due_cumul) def _clean_partners_and_accounts(self, only_delete_account_balance_at_0=False): @@ -731,31 +897,3 @@ WHERE """ params_clean_accounts = (self.id,) self.env.cr.execute(query_clean_accounts, params_clean_accounts) - - def _compute_has_second_currency(self): - """ Compute "has_second_currency" flag which will used for display.""" - query_update_has_second_currency = """ -UPDATE - report_open_items_qweb -SET - has_second_currency = - ( - SELECT - TRUE - FROM - report_open_items_qweb_move_line l - INNER JOIN - report_open_items_qweb_partner p - ON l.report_partner_id = p.id - INNER JOIN - report_open_items_qweb_account a - ON p.report_account_id = a.id - WHERE - a.report_id = %s - AND l.currency_name IS NOT NULL - LIMIT 1 - ) -WHERE id = %s - """ - params = (self.id,) * 2 - self.env.cr.execute(query_update_has_second_currency, params) diff --git a/account_financial_report_qweb/report/open_items_xlsx.py b/account_financial_report_qweb/report/open_items_xlsx.py index 1d75c752..ca5f261b 100644 --- a/account_financial_report_qweb/report/open_items_xlsx.py +++ b/account_financial_report_qweb/report/open_items_xlsx.py @@ -29,6 +29,7 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): 6: {'header': _('Due date'), 'field': 'date_due', 'width': 11}, 7: {'header': _('Original'), 'field': 'amount_total_due', + 'field_final_balance': 'final_amount_total_due', 'type': 'amount', 'width': 14}, 8: {'header': _('Residual'), @@ -36,13 +37,18 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): 'field_final_balance': 'final_amount_residual', 'type': 'amount', 'width': 14}, - 9: {'header': _('Cur.'), 'field': 'currency_name', 'width': 7}, + 9: {'header': _('Cur.'), + 'field': 'currency_name', + 'field_final_balance': 'currency_name', + 'width': 7}, 10: {'header': _('Cur. Original'), 'field': 'amount_total_due_currency', + 'field_final_balance': 'final_amount_total_due_currency', 'type': 'amount', 'width': 14}, 11: {'header': _('Cur. Residual'), 'field': 'amount_residual_currency', + 'field_final_balance': 'final_amount_residual_currency', 'type': 'amount', 'width': 14}, } diff --git a/account_financial_report_qweb/report/templates/general_ledger.xml b/account_financial_report_qweb/report/templates/general_ledger.xml index 5ed49095..f1e38657 100644 --- a/account_financial_report_qweb/report/templates/general_ledger.xml +++ b/account_financial_report_qweb/report/templates/general_ledger.xml @@ -164,12 +164,12 @@
-
- +
+
- +
@@ -206,12 +206,12 @@
-
- +
+
- +
@@ -246,12 +246,12 @@
-
- +
+
- +
diff --git a/account_financial_report_qweb/report/templates/open_items.xml b/account_financial_report_qweb/report/templates/open_items.xml index 69a9a4c6..76fbe850 100644 --- a/account_financial_report_qweb/report/templates/open_items.xml +++ b/account_financial_report_qweb/report/templates/open_items.xml @@ -4,8 +4,6 @@ diff --git a/account_financial_report_qweb/report/templates/trial_balance.xml b/account_financial_report_qweb/report/templates/trial_balance.xml index 27ab6003..38534ac7 100644 --- a/account_financial_report_qweb/report/templates/trial_balance.xml +++ b/account_financial_report_qweb/report/templates/trial_balance.xml @@ -136,14 +136,14 @@
-
- +
+
- +
diff --git a/account_financial_report_qweb/report/trial_balance.py b/account_financial_report_qweb/report/trial_balance.py index 4d68c31b..1d36699d 100644 --- a/account_financial_report_qweb/report/trial_balance.py +++ b/account_financial_report_qweb/report/trial_balance.py @@ -67,7 +67,7 @@ class TrialBalanceReportAccount(models.TransientModel): initial_balance_foreign_currency = fields.Float(digits=(16, 2)) debit = fields.Float(digits=(16, 2)) credit = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') final_balance = fields.Float(digits=(16, 2)) final_balance_foreign_currency = fields.Float(digits=(16, 2)) @@ -100,7 +100,7 @@ class TrialBalanceReportPartner(models.TransientModel): initial_balance = fields.Float(digits=(16, 2)) debit = fields.Float(digits=(16, 2)) credit = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') final_balance = fields.Float(digits=(16, 2)) @api.model @@ -187,7 +187,7 @@ INSERT INTO debit, credit, final_balance, - currency_name, + currency_id, initial_balance_foreign_currency, final_balance_foreign_currency ) @@ -202,7 +202,7 @@ SELECT rag.final_debit - rag.initial_debit AS debit, rag.final_credit - rag.initial_credit AS credit, rag.final_balance AS final_balance, - rag.currency_name AS currency_name, + rag.currency_id, rag.initial_balance_foreign_currency AS initial_balance_foreign_currency, rag.final_balance_foreign_currency AS final_balance_foreign_currency FROM @@ -232,7 +232,7 @@ INSERT INTO debit, credit, final_balance, - currency_name + currency_id ) SELECT ra.id AS report_account_id, @@ -244,7 +244,7 @@ SELECT rpg.final_debit - rpg.initial_debit AS debit, rpg.final_credit - rpg.initial_credit AS credit, rpg.final_balance AS final_balance, - rpg.currency_name AS currency_name + rpg.currency_id FROM report_general_ledger_qweb_partner rpg INNER JOIN From 0a886af6d6fafd229c1ce21bfb881e78cb443af4 Mon Sep 17 00:00:00 2001 From: hveficent Date: Mon, 28 May 2018 09:16:03 +0200 Subject: [PATCH 2/2] [10.0] [BACKPORT] foreign currencies + dynamic reports --- account_financial_report_qweb/__manifest__.py | 8 +- .../report/abstract_report_xlsx.py | 87 ++- .../report/aged_partner_balance.py | 23 +- .../report/general_ledger.py | 75 +-- .../report/general_ledger_xlsx.py | 84 +-- .../report/journal_report.py | 73 ++- .../report/journal_report_xlsx.py | 2 +- .../report/open_items.py | 65 ++- .../report/open_items_xlsx.py | 57 +- .../report/templates/aged_partner_balance.xml | 535 +++++++++++++----- .../report/templates/general_ledger.xml | 527 ++++++++++++----- .../report/templates/journal.xml | 52 +- .../report/templates/open_items.xml | 237 +++++--- .../report/templates/trial_balance.xml | 502 +++++++++++++--- .../report/trial_balance.py | 82 ++- .../report/trial_balance_xlsx.py | 67 ++- account_financial_report_qweb/reports.xml | 47 +- .../static/src/css/report.css | 14 +- .../account_financial_report_qweb_backend.js | 95 ++++ .../account_financial_report_qweb_widgets.js | 69 +++ .../tests/__init__.py | 1 + .../tests/abstract_test.py | 305 +++++++--- .../tests/abstract_test_foreign_currency.py | 79 +++ .../tests/test_aged_partner_balance.py | 2 +- .../tests/test_general_ledger.py | 14 +- .../tests/test_journal.py | 45 ++ .../tests/test_open_items.py | 7 +- .../tests/test_trial_balance.py | 7 +- .../view/report_aged_partner_balance.xml | 9 + .../view/report_general_ledger.xml | 9 + .../view/report_journal_ledger.xml | 9 + .../view/report_open_items.xml | 9 + .../view/report_template.xml | 57 ++ .../view/report_trial_balance.xml | 11 + .../wizard/aged_partner_balance_wizard.py | 29 +- .../aged_partner_balance_wizard_view.xml | 3 + .../wizard/general_ledger_wizard.py | 36 +- .../wizard/general_ledger_wizard_view.xml | 4 + .../wizard/journal_report_wizard.py | 38 +- .../wizard/journal_report_wizard.xml | 19 +- .../wizard/open_items_wizard.py | 36 +- .../wizard/open_items_wizard_view.xml | 4 + .../wizard/trial_balance_wizard.py | 39 +- .../wizard/trial_balance_wizard_view.xml | 4 + 44 files changed, 2686 insertions(+), 791 deletions(-) create mode 100644 account_financial_report_qweb/static/src/js/account_financial_report_qweb_backend.js create mode 100644 account_financial_report_qweb/static/src/js/account_financial_report_qweb_widgets.js create mode 100644 account_financial_report_qweb/tests/abstract_test_foreign_currency.py create mode 100644 account_financial_report_qweb/view/report_aged_partner_balance.xml create mode 100644 account_financial_report_qweb/view/report_general_ledger.xml create mode 100644 account_financial_report_qweb/view/report_journal_ledger.xml create mode 100644 account_financial_report_qweb/view/report_open_items.xml create mode 100644 account_financial_report_qweb/view/report_template.xml create mode 100644 account_financial_report_qweb/view/report_trial_balance.xml diff --git a/account_financial_report_qweb/__manifest__.py b/account_financial_report_qweb/__manifest__.py index d22f3521..aa3c181b 100644 --- a/account_financial_report_qweb/__manifest__.py +++ b/account_financial_report_qweb/__manifest__.py @@ -34,7 +34,13 @@ 'report/templates/layouts.xml', 'report/templates/open_items.xml', 'report/templates/trial_balance.xml', - 'view/account_view.xml' + 'view/account_view.xml', + 'view/report_template.xml', + 'view/report_general_ledger.xml', + 'view/report_journal_ledger.xml', + 'view/report_trial_balance.xml', + 'view/report_open_items.xml', + 'view/report_aged_partner_balance.xml', ], 'installable': True, 'application': True, diff --git a/account_financial_report_qweb/report/abstract_report_xlsx.py b/account_financial_report_qweb/report/abstract_report_xlsx.py index adc6661d..16114503 100644 --- a/account_financial_report_qweb/report/abstract_report_xlsx.py +++ b/account_financial_report_qweb/report/abstract_report_xlsx.py @@ -46,6 +46,7 @@ class AbstractReportXslx(ReportXlsx): filters = self._get_report_filters(report) self.columns = self._get_report_columns(report) + self.workbook = workbook self.set_sheet(self.add_sheet(workbook, report_name[:31])) self._set_column_width() @@ -100,30 +101,17 @@ class AbstractReportXslx(ReportXlsx): {'bold': True, 'border': True, 'bg_color': '#FFFFCC'}) - self.format_header_amount.set_num_format('#,##0.00') + currency_id = self.env['res.company']._get_user_currency() + self.format_header_amount.set_num_format( + '#,##0.'+'0'*currency_id.decimal_places) self.format_amount = workbook.add_format() - self.format_amount.set_num_format('#,##0.00') + self.format_amount.set_num_format( + '#,##0.'+'0'*currency_id.decimal_places) self.format_percent_bold_italic = workbook.add_format( {'bold': True, 'italic': True} ) self.format_percent_bold_italic.set_num_format('#,##0.00%') - def _get_currency_amt_format(self, line_object): - """ Return amount format specific for each currency. """ - format_amt = getattr(self, 'format_amount') - if line_object.currency_id: - field_name = \ - 'format_amount_%s' % line_object.currency_id.name - if hasattr(self, field_name): - format_amt = getattr(self, field_name) - else: - format_amt = self.workbook.add_format() - setattr(self, 'field_name', format_amt) - format_amount = \ - '#,##0.' + ('0' * line_object.currency_id.decimal_places) - format_amt.set_num_format(format_amount) - return format_amt - def _set_column_width(self): """Set width for all defined columns. Columns are defined with `_get_report_columns` method. @@ -241,11 +229,15 @@ class AbstractReportXslx(ReportXlsx): self.row_pos, col_pos, float(value), format_amt ) - elif cell_type == 'many2one': - self.sheet.write_string( - self.row_pos, col_pos, value.name or '', - self.format_header_right) - + elif column.get('field_currency_balance'): + value = getattr(my_object, column['field_currency_balance']) + cell_type = column.get('type', 'string') + if cell_type == 'many2one': + if my_object.currency_id: + self.sheet.write_string( + self.row_pos, col_pos, value.name or '', + self.format_right + ) self.row_pos += 1 def write_ending_balance(self, my_object, name, label): @@ -278,18 +270,57 @@ class AbstractReportXslx(ReportXlsx): ) elif cell_type == 'amount_currency': if my_object.currency_id: - format_amt = self._get_currency_amt_format( + format_amt = self._get_currency_amt_header_format( my_object) self.sheet.write_number( self.row_pos, col_pos, float(value), format_amt ) - elif cell_type == 'many2one': - self.sheet.write_string( - self.row_pos, col_pos, value.name or '', - self.format_header_right) + elif column.get('field_currency_balance'): + value = getattr(my_object, column['field_currency_balance']) + cell_type = column.get('type', 'string') + if cell_type == 'many2one': + if my_object.currency_id: + self.sheet.write_string( + self.row_pos, col_pos, value.name or '', + self.format_header_right) self.row_pos += 1 + def _get_currency_amt_format(self, line_object): + """ Return amount format specific for each currency. """ + format_amt = getattr(self, 'format_amount') + if line_object.currency_id: + field_name = \ + 'format_amount_%s' % line_object.currency_id.name + if hasattr(self, field_name): + format_amt = getattr(self, field_name) + else: + format_amt = self.workbook.add_format() + setattr(self, 'field_name', format_amt) + format_amount = \ + '#,##0.' + ('0' * line_object.currency_id.decimal_places) + format_amt.set_num_format(format_amount) + return format_amt + + def _get_currency_amt_header_format(self, line_object): + """ Return amount header format for each currency. """ + format_amt = getattr(self, 'format_header_amount') + if line_object.currency_id: + field_name = \ + 'format_header_amount_%s' % line_object.currency_id.name + if hasattr(self, field_name): + format_amt = getattr(self, field_name) + else: + format_amt = self.workbook.add_format( + {'bold': True, + 'border': True, + 'bg_color': '#FFFFCC'}) + setattr(self, 'field_name', format_amt) + format_amount = \ + '#,##0.' + ('0' * line_object.currency_id.decimal_places) + format_amt.set_num_format(format_amount) + return format_amt + def _generate_report_content(self, workbook, report): pass diff --git a/account_financial_report_qweb/report/aged_partner_balance.py b/account_financial_report_qweb/report/aged_partner_balance.py index aabf09ec..d6f1a81c 100644 --- a/account_financial_report_qweb/report/aged_partner_balance.py +++ b/account_financial_report_qweb/report/aged_partner_balance.py @@ -185,10 +185,9 @@ class AgedPartnerBalanceReportCompute(models.TransientModel): _inherit = 'report_aged_partner_balance_qweb' @api.multi - def print_report(self, xlsx_report=False): + def print_report(self, report_type): self.ensure_one() - self.compute_data_for_report() - if xlsx_report: + if report_type == 'xlsx': report_name = 'account_financial_report_qweb.' \ 'report_aged_partner_balance_xlsx' else: @@ -197,6 +196,22 @@ class AgedPartnerBalanceReportCompute(models.TransientModel): return self.env['report'].get_action(docids=self.ids, report_name=report_name) + def _get_html(self): + result = {} + rcontext = {} + context = dict(self.env.context) + report = self.browse(context.get('active_id')) + if report: + rcontext['o'] = report + result['html'] = self.env.ref( + 'account_financial_report_qweb.' + 'report_aged_partner_balance_html').render(rcontext) + return result + + @api.model + def get_html(self, given_context=None): + return self._get_html() + def _prepare_report_open_items(self): self.ensure_one() return { @@ -439,6 +454,7 @@ INSERT INTO report_partner_id, create_uid, create_date, + move_line_id, date, date_due, entry, @@ -458,6 +474,7 @@ SELECT rp.id AS report_partner_id, %s AS create_uid, NOW() AS create_date, + rlo.move_line_id, rlo.date, rlo.date_due, rlo.entry, diff --git a/account_financial_report_qweb/report/general_ledger.py b/account_financial_report_qweb/report/general_ledger.py index 8ebb2151..f633f4d7 100644 --- a/account_financial_report_qweb/report/general_ledger.py +++ b/account_financial_report_qweb/report/general_ledger.py @@ -29,6 +29,7 @@ class GeneralLedgerReport(models.TransientModel): fy_start_date = fields.Date() only_posted_moves = fields.Boolean() hide_account_balance_at_0 = fields.Boolean() + foreign_currency = fields.Boolean() company_id = fields.Many2one(comodel_name='res.company') filter_account_ids = fields.Many2many(comodel_name='account.account') filter_partner_ids = fields.Many2many(comodel_name='res.partner') @@ -38,7 +39,6 @@ class GeneralLedgerReport(models.TransientModel): centralize = fields.Boolean() # Flag fields, used for report display - has_second_currency = fields.Boolean() show_cost_center = fields.Boolean( default=lambda self: self.env.user.has_group( 'analytic.group_analytic_accounting' @@ -203,10 +203,9 @@ class GeneralLedgerReportCompute(models.TransientModel): _inherit = 'report_general_ledger_qweb' @api.multi - def print_report(self, xlsx_report=False): + def print_report(self, report_type): self.ensure_one() - self.compute_data_for_report() - if xlsx_report: + if report_type == 'xlsx': report_name = 'account_financial_report_qweb.' \ 'report_general_ledger_xlsx' else: @@ -215,6 +214,22 @@ class GeneralLedgerReportCompute(models.TransientModel): return self.env['report'].get_action(docids=self.ids, report_name=report_name) + def _get_html(self): + result = {} + rcontext = {} + context = dict(self.env.context) + report = self.browse(context.get('active_id')) + if report: + rcontext['o'] = report + result['html'] = self.env.ref( + 'account_financial_report_qweb.' + 'report_general_ledger_html').render(rcontext) + return result + + @api.model + def get_html(self, given_context=None): + return self._get_html() + @api.multi def compute_data_for_report( self, with_line_details=True, with_partners=True): @@ -255,10 +270,6 @@ class GeneralLedgerReportCompute(models.TransientModel): if self.centralize: self._inject_line_centralized_values() - if with_line_details: - # Compute display flag - self._compute_has_second_currency() - # Refresh cache because all data are computed with SQL requests self.invalidate_cache() @@ -453,7 +464,7 @@ SELECT COALESCE(i.debit, 0.0) AS initial_debit, COALESCE(i.credit, 0.0) AS initial_credit, COALESCE(i.balance, 0.0) AS initial_balance, - a.currency_id, + c.id AS currency_id, COALESCE(i.balance_currency, 0.0) AS initial_balance_foreign_currency, COALESCE(f.debit, 0.0) AS final_debit, COALESCE(f.credit, 0.0) AS final_credit, @@ -466,6 +477,8 @@ LEFT JOIN initial_sum_amounts i ON a.id = i.account_id LEFT JOIN final_sum_amounts f ON a.id = f.account_id +LEFT JOIN + res_currency c ON c.id = a.currency_id WHERE ( i.debit IS NOT NULL AND i.debit != 0 @@ -529,7 +542,7 @@ AND tuple(self.filter_cost_center_ids.ids), ) query_inject_account_params += ( - self.id, + self.id or 'NULL', self.env.uid, ) self.env.cr.execute(query_inject_account, query_inject_account_params) @@ -1241,48 +1254,6 @@ ORDER BY query_inject_move_line_centralized_params ) - def _compute_has_second_currency(self): - """ Compute "has_second_currency" flag which will used for display.""" - query_update_has_second_currency = """ -UPDATE - report_general_ledger_qweb -SET - has_second_currency = - ( - SELECT - TRUE - FROM - report_general_ledger_qweb_move_line l - INNER JOIN - report_general_ledger_qweb_account a - ON l.report_account_id = a.id - WHERE - a.report_id = %s - AND l.currency_id IS NOT NULL - LIMIT 1 - ) - OR - ( - SELECT - TRUE - FROM - report_general_ledger_qweb_move_line l - INNER JOIN - report_general_ledger_qweb_partner p - ON l.report_partner_id = p.id - INNER JOIN - report_general_ledger_qweb_account a - ON p.report_account_id = a.id - WHERE - a.report_id = %s - AND l.currency_id IS NOT NULL - LIMIT 1 - ) -WHERE id = %s - """ - params = (self.id,) * 3 - self.env.cr.execute(query_update_has_second_currency, params) - def _get_unaffected_earnings_account_sub_subquery_sum_initial( self ): diff --git a/account_financial_report_qweb/report/general_ledger_xlsx.py b/account_financial_report_qweb/report/general_ledger_xlsx.py index 9a97ba5a..2b7ac916 100644 --- a/account_financial_report_qweb/report/general_ledger_xlsx.py +++ b/account_financial_report_qweb/report/general_ledger_xlsx.py @@ -20,7 +20,7 @@ class GeneralLedgerXslx(abstract_report_xlsx.AbstractReportXslx): return _('General Ledger') def _get_report_columns(self, report): - return { + res = { 0: {'header': _('Date'), 'field': 'date', 'width': 11}, 1: {'header': _('Entry'), 'field': 'entry', 'width': 18}, 2: {'header': _('Journal'), 'field': 'journal', 'width': 8}, @@ -54,39 +54,38 @@ class GeneralLedgerXslx(abstract_report_xlsx.AbstractReportXslx): 'field_final_balance': 'final_balance', 'type': 'amount', 'width': 14}, - 12: {'header': _('Cur.'), - 'field': 'currency_id', - 'field_initial_balance': 'currency_id', - 'field_final_balance': 'currency_id', - 'type': 'many2one', - 'width': 7}, - 13: {'header': _('Amount cur.'), - 'field': 'amount_currency', - 'field_initial_balance': 'initial_balance_foreign_currency', - 'field_final_balance': 'final_balance_foreign_currency', - 'type': 'amount', - 'width': 14}, } + if report.foreign_currency: + foreign_currency = { + 12: {'header': _('Cur.'), + 'field': 'currency_id', + 'field_currency_balance': 'currency_id', + 'type': 'many2one', + 'width': 7}, + 13: {'header': _('Amount cur.'), + 'field': 'amount_currency', + 'field_initial_balance': + 'initial_balance_foreign_currency', + 'field_final_balance': 'final_balance_foreign_currency', + 'type': 'amount_currency', + 'width': 14}, + } + res = dict(res.items() + foreign_currency.items()) + return res def _get_report_filters(self, report): return [ - [ - _('Date range filter'), - _('From: %s To: %s') % (report.date_from, report.date_to), - ], - [ - _('Target moves filter'), - _('All posted entries') if report.only_posted_moves - else _('All entries'), - ], - [ - _('Account balance at 0 filter'), - _('Hide') if report.hide_account_balance_at_0 else _('Show'), - ], - [ - _('Centralize filter'), - _('Yes') if report.centralize else _('No'), - ], + [_('Date range filter'), + _('From: %s To: %s') % (report.date_from, report.date_to)], + [_('Target moves filter'), + _('All posted entries') if report.only_posted_moves else _( + 'All entries')], + [_('Account balance at 0 filter'), + _('Hide') if report.hide_account_balance_at_0 else _('Show')], + [_('Centralize filter'), + _('Yes') if report.centralize else _('No')], + [_('Show foreign currency'), + _('Yes') if report.foreign_currency else _('No')], ] def _get_col_count_filter_name(self): @@ -115,7 +114,7 @@ class GeneralLedgerXslx(abstract_report_xlsx.AbstractReportXslx): self.write_array_header() # Display initial balance line for account - self.write_initial_balance(account, _('Initial balance')) + self.write_initial_balance(account) # Display account move lines for line in account.move_line_ids: @@ -131,30 +130,41 @@ class GeneralLedgerXslx(abstract_report_xlsx.AbstractReportXslx): self.write_array_header() # Display initial balance line for partner - self.write_initial_balance(partner, _('Initial balance')) + self.write_initial_balance(partner) # Display account move lines for line in partner.move_line_ids: self.write_line(line) # Display ending balance line for partner - self.write_ending_balance(partner, 'partner') + self.write_ending_balance(partner) # Line break self.row_pos += 1 # Display ending balance line for account - self.write_ending_balance(account, 'account') + self.write_ending_balance(account) # 2 lines break self.row_pos += 2 - def write_ending_balance(self, my_object, type_object): + def write_initial_balance(self, my_object): + """Specific function to write initial balance for General Ledger""" + if 'partner' in my_object._name: + label = _('Partner Initial balance') + my_object.currency_id = my_object.report_account_id.currency_id + elif 'account' in my_object._name: + label = _('Initial balance') + super(GeneralLedgerXslx, self).write_initial_balance( + my_object, label + ) + + def write_ending_balance(self, my_object): """Specific function to write ending balance for General Ledger""" - if type_object == 'partner': + if 'partner' in my_object._name: name = my_object.name label = _('Partner ending balance') - elif type_object == 'account': + elif 'account' in my_object._name: name = my_object.code + ' - ' + my_object.name label = _('Ending balance') super(GeneralLedgerXslx, self).write_ending_balance( diff --git a/account_financial_report_qweb/report/journal_report.py b/account_financial_report_qweb/report/journal_report.py index c70cc0ba..5f873a78 100644 --- a/account_financial_report_qweb/report/journal_report.py +++ b/account_financial_report_qweb/report/journal_report.py @@ -61,7 +61,7 @@ class ReportJournalQweb(models.TransientModel): comodel_name='report_journal_qweb_report_tax_line', inverse_name='report_id', ) - with_currency = fields.Boolean() + foreign_currency = fields.Boolean() with_account_name = fields.Boolean() @api.model @@ -76,6 +76,12 @@ class ReportJournalQweb(models.TransientModel): def _get_group_options(self): return self.env['journal.report.wizard']._get_group_options() + @api.multi + def refresh(self): + self.ensure_one() + self.report_journal_ids.unlink() + self.compute_data_for_report() + @api.multi def compute_data_for_report(self): self.ensure_one() @@ -88,15 +94,21 @@ class ReportJournalQweb(models.TransientModel): if self.group_option == 'none': self._inject_report_tax_values() - @api.multi - def refresh(self): - self.ensure_one() - self.report_journal_ids.unlink() - self.compute_data_for_report() + # Refresh cache because all data are computed with SQL requests + self.invalidate_cache() @api.multi def _inject_journal_values(self): self.ensure_one() + sql = """ + DELETE + FROM report_journal_qweb_journal + WHERE report_id = %s + """ + params = ( + self.id, + ) + self.env.cr.execute(sql, params) sql = """ INSERT INTO report_journal_qweb_journal ( create_uid, @@ -139,6 +151,15 @@ class ReportJournalQweb(models.TransientModel): @api.multi def _inject_move_values(self): self.ensure_one() + sql = """ + DELETE + FROM report_journal_qweb_move + WHERE report_id = %s + """ + params = ( + self.id, + ) + self.env.cr.execute(sql, params) sql = self._get_inject_move_insert() sql += self._get_inject_move_select() sql += self._get_inject_move_where_clause() @@ -225,6 +246,15 @@ class ReportJournalQweb(models.TransientModel): @api.multi def _inject_move_line_values(self): self.ensure_one() + sql = """ + DELETE + FROM report_journal_qweb_move_line + WHERE report_id = %s + """ + params = ( + self.id, + ) + self.env.cr.execute(sql, params) sql = """ INSERT INTO report_journal_qweb_move_line ( create_uid, @@ -414,7 +444,15 @@ class ReportJournalQweb(models.TransientModel): @api.multi def _inject_journal_tax_values(self): self.ensure_one() - + sql = """ + DELETE + FROM report_journal_qweb_journal_tax_line + WHERE report_id = %s + """ + params = ( + self.id, + ) + self.env.cr.execute(sql, params) sql_distinct_tax_id = """ SELECT distinct(jrqml.tax_id) @@ -553,10 +591,9 @@ class ReportJournalQweb(models.TransientModel): self.env.cr.execute(sql, (self.id,)) @api.multi - def print_report(self, xlsx_report=False): + def print_report(self, report_type): self.ensure_one() - self.compute_data_for_report() - if xlsx_report: + if report_type == 'xlsx': report_name = 'account_financial_report_qweb.' \ 'report_journal_xlsx' else: @@ -565,6 +602,22 @@ class ReportJournalQweb(models.TransientModel): return self.env['report'].get_action( docids=self.ids, report_name=report_name) + def _get_html(self): + result = {} + rcontext = {} + context = dict(self.env.context) + report = self.browse(context.get('active_id')) + if report: + rcontext['o'] = report + result['html'] = self.env.ref( + 'account_financial_report_qweb.' + 'report_journal_html').render(rcontext) + return result + + @api.model + def get_html(self, given_context=None): + return self._get_html() + class ReportJournalQwebJournal(models.TransientModel): diff --git a/account_financial_report_qweb/report/journal_report_xlsx.py b/account_financial_report_qweb/report/journal_report_xlsx.py index 7a36204c..0ed64ad3 100644 --- a/account_financial_report_qweb/report/journal_report_xlsx.py +++ b/account_financial_report_qweb/report/journal_report_xlsx.py @@ -76,7 +76,7 @@ class JournalXslx(abstract_report_xlsx.AbstractReportXslx): } ] - if report.with_currency: + if report.foreign_currency: columns += [ { 'header': _('Amount Currency'), diff --git a/account_financial_report_qweb/report/open_items.py b/account_financial_report_qweb/report/open_items.py index 21ac0cec..2c52f559 100644 --- a/account_financial_report_qweb/report/open_items.py +++ b/account_financial_report_qweb/report/open_items.py @@ -22,6 +22,7 @@ class OpenItemsReport(models.TransientModel): date_at = fields.Date() only_posted_moves = fields.Boolean() hide_account_balance_at_0 = fields.Boolean() + foreign_currency = fields.Boolean() company_id = fields.Many2one(comodel_name='res.company') filter_account_ids = fields.Many2many(comodel_name='account.account') filter_partner_ids = fields.Many2many(comodel_name='res.partner') @@ -53,7 +54,7 @@ class OpenItemsReportAccount(models.TransientModel): # Data fields, used for report display code = fields.Char() name = fields.Char() - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') final_amount_residual = fields.Float(digits=(16, 2)) final_amount_total_due = fields.Float(digits=(16, 2)) final_amount_residual_currency = fields.Float(digits=(16, 2)) @@ -84,7 +85,7 @@ class OpenItemsReportPartner(models.TransientModel): # Data fields, used for report display name = fields.Char() - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') final_amount_residual = fields.Float(digits=(16, 2)) final_amount_total_due = fields.Float(digits=(16, 2)) final_amount_residual_currency = fields.Float(digits=(16, 2)) @@ -133,7 +134,7 @@ class OpenItemsReportMoveLine(models.TransientModel): label = fields.Char() amount_total_due = fields.Float(digits=(16, 2)) amount_residual = fields.Float(digits=(16, 2)) - currency_name = fields.Char() + currency_id = fields.Many2one(comodel_name='res.currency') amount_total_due_currency = fields.Float(digits=(16, 2)) amount_residual_currency = fields.Float(digits=(16, 2)) @@ -146,10 +147,9 @@ class OpenItemsReportCompute(models.TransientModel): _inherit = 'report_open_items_qweb' @api.multi - def print_report(self, xlsx_report=False): + def print_report(self, report_type): self.ensure_one() - self.compute_data_for_report() - if xlsx_report: + if report_type == 'xlsx': report_name = 'account_financial_report_qweb.' \ 'report_open_items_xlsx' else: @@ -158,6 +158,22 @@ class OpenItemsReportCompute(models.TransientModel): return self.env['report'].get_action(docids=self.ids, report_name=report_name) + def _get_html(self): + result = {} + rcontext = {} + context = dict(self.env.context) + report = self.browse(context.get('active_id')) + if report: + rcontext['o'] = report + result['html'] = self.env.ref( + 'account_financial_report_qweb.' + 'report_open_items_html').render(rcontext) + return result + + @api.model + def get_html(self, given_context=None): + return self._get_html() + @api.multi def compute_data_for_report(self): self.ensure_one() @@ -186,7 +202,7 @@ WITH a.code, a.name, a.user_type_id, - c.name as currency_name + c.id as currency_id FROM account_account a INNER JOIN @@ -221,7 +237,7 @@ WITH """ query_inject_account += """ GROUP BY - a.id, c.name + a.id, c.id ) INSERT INTO report_open_items_qweb_account @@ -230,7 +246,7 @@ INSERT INTO create_uid, create_date, account_id, - currency_name, + currency_id, code, name ) @@ -239,7 +255,7 @@ SELECT %s AS create_uid, NOW() AS create_date, a.id AS account_id, - a.currency_name, + a.currency_id, a.code, a.name FROM @@ -505,7 +521,7 @@ INSERT INTO label, amount_total_due, amount_residual, - currency_name, + currency_id, amount_total_due_currency, amount_residual_currency ) @@ -538,7 +554,7 @@ SELECT CONCAT_WS(' - ', NULLIF(ml.ref, ''), NULLIF(ml.name, '')) AS label, ml.balance, ml2.amount_residual, - c.name AS currency_name, + c.id AS currency_id, ml.amount_currency, ml2.amount_residual_currency FROM @@ -632,7 +648,7 @@ WHERE WHERE ra.report_id = %s )""" - query_compute_partners_residual_cumul = """ + query_computer_partner_residual_cumul = """ UPDATE report_open_items_qweb_partner SET @@ -647,7 +663,7 @@ SET ) """ + where_condition_partner_by_account params_compute_partners_residual_cumul = (self.id,) - self.env.cr.execute(query_compute_partners_residual_cumul, + self.env.cr.execute(query_computer_partner_residual_cumul, params_compute_partners_residual_cumul) query_compute_partners_due_cumul = """ @@ -664,9 +680,9 @@ SET rml.report_partner_id = report_open_items_qweb_partner.id ) """ + where_condition_partner_by_account - params_compute_partners_due_cumul = (self.id,) + params_compute_partner_due_cumul = (self.id,) self.env.cr.execute(query_compute_partners_due_cumul, - params_compute_partners_due_cumul) + params_compute_partner_due_cumul) # Manage currency in partner where_condition_partner_by_account_cur = """ @@ -681,25 +697,26 @@ WHERE report_open_items_qweb_partner rp ON ra.id = rp.report_account_id WHERE - ra.report_id = %s AND ra.currency_name IS NOT NULL - )""" - query_compute_partners_cur_name_cumul = """ + ra.report_id = %s AND ra.currency_id IS NOT NULL + ) + """ + query_compute_partners_cur_id_cumul = """ UPDATE report_open_items_qweb_partner SET - currency_name = + currency_id = ( SELECT - MAX(currency_name) as currency_name + MAX(currency_id) as currency_id FROM report_open_items_qweb_move_line rml WHERE rml.report_partner_id = report_open_items_qweb_partner.id ) """ + where_condition_partner_by_account_cur - params_compute_partners_cur_name_cumul = (self.id,) - self.env.cr.execute(query_compute_partners_cur_name_cumul, - params_compute_partners_cur_name_cumul) + params_compute_partners_cur_id_cumul = (self.id,) + self.env.cr.execute(query_compute_partners_cur_id_cumul, + params_compute_partners_cur_id_cumul) query_compute_partners_cur_residual_cumul = """ UPDATE diff --git a/account_financial_report_qweb/report/open_items_xlsx.py b/account_financial_report_qweb/report/open_items_xlsx.py index ca5f261b..9f6c645d 100644 --- a/account_financial_report_qweb/report/open_items_xlsx.py +++ b/account_financial_report_qweb/report/open_items_xlsx.py @@ -19,7 +19,7 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): return _('Open Items') def _get_report_columns(self, report): - return { + res = { 0: {'header': _('Date'), 'field': 'date', 'width': 11}, 1: {'header': _('Entry'), 'field': 'entry', 'width': 18}, 2: {'header': _('Journal'), 'field': 'journal', 'width': 8}, @@ -29,7 +29,6 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): 6: {'header': _('Due date'), 'field': 'date_due', 'width': 11}, 7: {'header': _('Original'), 'field': 'amount_total_due', - 'field_final_balance': 'final_amount_total_due', 'type': 'amount', 'width': 14}, 8: {'header': _('Residual'), @@ -37,37 +36,36 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): 'field_final_balance': 'final_amount_residual', 'type': 'amount', 'width': 14}, - 9: {'header': _('Cur.'), - 'field': 'currency_name', - 'field_final_balance': 'currency_name', - 'width': 7}, - 10: {'header': _('Cur. Original'), - 'field': 'amount_total_due_currency', - 'field_final_balance': 'final_amount_total_due_currency', - 'type': 'amount', - 'width': 14}, - 11: {'header': _('Cur. Residual'), - 'field': 'amount_residual_currency', - 'field_final_balance': 'final_amount_residual_currency', - 'type': 'amount', - 'width': 14}, } + if report.foreign_currency: + foreign_currency = { + 9: {'header': _('Cur.'), 'field': 'currency_id', + 'field_currency_balance': 'currency_id', + 'type': 'many2one', 'width': 7}, + 10: {'header': _('Cur. Original'), + 'field': 'amount_total_due_currency', + 'field_final_balance': 'final_amount_total_due_currency', + 'type': 'amount_currency', + 'width': 14}, + 11: {'header': _('Cur. Residual'), + 'field': 'amount_residual_currency', + 'field_final_balance': 'final_amount_residual_currency', + 'type': 'amount_currency', + 'width': 14}, + } + res = dict(res.items() + foreign_currency.items()) + return res def _get_report_filters(self, report): return [ - [ - _('Date at filter'), - report.date_at - ], - [ - _('Target moves filter'), - _('All posted entries') if report.only_posted_moves - else _('All entries'), - ], - [ - _('Account balance at 0 filter'), - _('Hide') if report.hide_account_balance_at_0 else _('Show'), - ], + [_('Date at filter'), report.date_at], + [_('Target moves filter'), + _('All posted entries') if report.only_posted_moves else _( + 'All entries')], + [_('Account balance at 0 filter'), + _('Hide') if report.hide_account_balance_at_0 else _('Show')], + [_('Show foreign currency'), + _('Yes') if report.foreign_currency else _('No')], ] def _get_col_count_filter_name(self): @@ -117,6 +115,7 @@ class OpenItemsXslx(abstract_report_xlsx.AbstractReportXslx): if type_object == 'partner': name = my_object.name label = _('Partner ending balance') + my_object.currency_id = my_object.report_account_id.currency_id elif type_object == 'account': name = my_object.code + ' - ' + my_object.name label = _('Ending balance') diff --git a/account_financial_report_qweb/report/templates/aged_partner_balance.xml b/account_financial_report_qweb/report/templates/aged_partner_balance.xml index b146731d..7b35a28d 100644 --- a/account_financial_report_qweb/report/templates/aged_partner_balance.xml +++ b/account_financial_report_qweb/report/templates/aged_partner_balance.xml @@ -1,80 +1,88 @@ -