From 640e851f4e256d22b0d04084efd7148cf3b0feb4 Mon Sep 17 00:00:00 2001 From: qdp-odoo Date: Tue, 17 May 2016 11:14:01 +0200 Subject: [PATCH] [FIX] l10n_fr_fec: intial balance of accounts from the P&L should be summarized into a single line --- addons/l10n_fr_fec/wizard/fec.py | 85 +++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/addons/l10n_fr_fec/wizard/fec.py b/addons/l10n_fr_fec/wizard/fec.py index 6ab15ccbd12..ccc3a26e30b 100644 --- a/addons/l10n_fr_fec/wizard/fec.py +++ b/addons/l10n_fr_fec/wizard/fec.py @@ -23,6 +23,56 @@ class AccountFrFec(models.TransientModel): ('nonofficial', 'Non-official FEC report (posted and unposted entries)'), ], string='Export Type', required=True, default='official') + def do_query_unaffected_earnings(self): + ''' Compute the sum of ending balances for all accounts that are of a type that does not bring forward the balance in new fiscal years. + This is needed because we have to display only one line for the initial balance of all expense/revenue accounts in the FEC. + ''' + + sql_query = ''' + SELECT + 'OUV' AS JournalCode, + 'Balance initiale' AS JournalLib, + 'Balance initiale PL' AS EcritureNum, + %s AS EcritureDate, + '120/129' AS CompteNum, + 'Benefice (perte) reporte(e)' AS CompteLib, + '' AS CompAuxNum, + '' AS CompAuxLib, + '-' AS PieceRef, + %s AS PieceDate, + '/' AS EcritureLib, + replace(CASE WHEN sum(aml.balance) <= 0 THEN '0,00' ELSE to_char(SUM(aml.balance), '999999999999999D99') END, '.', ',') AS Debit, + replace(CASE WHEN sum(aml.balance) >= 0 THEN '0,00' ELSE to_char(-SUM(aml.balance), '999999999999999D99') END, '.', ',') AS Credit, + '' AS EcritureLet, + '' AS DateLet, + %s AS ValidDate, + '' AS Montantdevise, + '' AS Idevise + FROM + account_move_line aml + LEFT JOIN account_move am ON am.id=aml.move_id + JOIN account_account aa ON aa.id = aml.account_id + LEFT JOIN account_account_type aat ON aa.user_type_id = aat.id + WHERE + am.date < %s + AND am.company_id = %s + AND aat.include_initial_balance = 'f' + AND (aml.debit != 0 OR aml.credit != 0) + ''' + # For official report: only use posted entries + if self.export_type == "official": + sql_query += ''' + AND am.state = 'posted' + ''' + company = self.env.user.company_id + formatted_date_from = self.date_from.replace('-', '') + self._cr.execute( + sql_query, (formatted_date_from, formatted_date_from, formatted_date_from, self.date_from, company.id)) + listrow = [] + row = self._cr.fetchone() + listrow = list(row) + return listrow + @api.multi def generate_fec(self): self.ensure_one() @@ -68,6 +118,13 @@ class AccountFrFec(models.TransientModel): w.writerow(header) # INITIAL BALANCE + unaffected_earnings_xml_ref = self.env.ref('account.data_unaffected_earnings') + unaffected_earnings_line = True # used to make sure that we add the unaffected earning initial balance only once + if unaffected_earnings_xml_ref: + #compute the benefit/loss of last year to add in the initial balance of the current year earnings account + unaffected_earnings_results = self.do_query_unaffected_earnings() + unaffected_earnings_line = False + sql_query = ''' SELECT 'OUV' AS JournalCode, @@ -87,14 +144,17 @@ class AccountFrFec(models.TransientModel): '' AS DateLet, %s AS ValidDate, '' AS Montantdevise, - '' AS Idevise + '' AS Idevise, + MIN(aa.id) AS CompteID FROM account_move_line aml LEFT JOIN account_move am ON am.id=aml.move_id JOIN account_account aa ON aa.id = aml.account_id + LEFT JOIN account_account_type aat ON aa.user_type_id = aat.id WHERE am.date < %s AND am.company_id = %s + AND aat.include_initial_balance = 't' AND (aml.debit != 0 OR aml.credit != 0) ''' @@ -113,7 +173,30 @@ class AccountFrFec(models.TransientModel): for row in self._cr.fetchall(): listrow = list(row) + account_id = listrow.pop() + if not unaffected_earnings_line: + account = self.env['account.account'].browse(account_id) + if account.user_type_id.id == self.env.ref('account.data_unaffected_earnings').id: + #add the benefit/loss of previous fiscal year to the first unaffected earnings account found. + unaffected_earnings_line = True + current_amount = float(listrow[11].replace(',', '.')) - float(listrow[12].replace(',', '.')) + unaffected_earnings_amount = float(unaffected_earnings_results[11].replace(',', '.')) - float(unaffected_earnings_results[12].replace(',', '.')) + listrow_amount = current_amount + unaffected_earnings_amount + if listrow_amount > 0: + listrow[11] = str(listrow_amount) + listrow[12] = '0.00' + else: + listrow[11] = '0.00' + listrow[12] = str(listrow_amount) w.writerow([s.encode("utf-8") for s in listrow]) + #if the unaffected earnings account wasn't in the selection yet: add it manually + if not unaffected_earnings_line and unaffected_earnings_results: + #search an unaffected earnings account + unaffected_earnings_account = self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_unaffected_earnings').id)], limit=1) + if unaffected_earnings_account: + unaffected_earnings_results[4] = unaffected_earnings_account.code + unaffected_earnings_results[5] = unaffected_earnings_account.name + w.writerow([s.encode("utf-8") for s in unaffected_earnings_results]) # LINES sql_query = '''