From 9c9ebe8cda6c4d6ec647ca7893b4adb308243db3 Mon Sep 17 00:00:00 2001 From: Laurent Mignon Date: Fri, 3 Apr 2015 09:04:54 +0200 Subject: [PATCH] [IMP] Let the res_partner_bank.post_write create the journal if it doesn't exist --- .../account_bank_statement_import.py | 28 +++++++++++++------ .../tests/test_import_bank_statement.py | 8 +++--- .../__openerp__.py | 4 +-- .../demo/demo_data.xml | 27 ------------------ 4 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 account_bank_statement_import_ofx/demo/demo_data.xml diff --git a/account_bank_statement_import/account_bank_statement_import.py b/account_bank_statement_import/account_bank_statement_import.py index 37d02aec..74317b0d 100644 --- a/account_bank_statement_import/account_bank_statement_import.py +++ b/account_bank_statement_import/account_bank_statement_import.py @@ -64,13 +64,19 @@ class account_bank_statement_import(models.TransientModel): # Try to find the bank account and currency in odoo currency_id, bank_account_id = self._find_additional_data( currency_code, account_number) + # Create the bank account if not already existing + if not bank_account_id and account_number: + journal_id = self.env.context.get('journal_id') + company_id = self.env.user.company_id.id + if journal_id: + journal = self.env['account.journal'].browse(journal_id) + company_id = journal.company_id.id + bank_account_id = self._create_bank_account( + account_number, company_id=company_id, + currency_id=currency_id).id # Find or create the bank journal journal_id = self._get_journal( currency_id, bank_account_id, account_number) - # Create the bank account if not already existing - if not bank_account_id and account_number: - self._create_bank_account( - account_number, journal_id=journal_id) # Prepare statement data to be used for bank statements creation stmts_vals = self._complete_stmts_vals( stmts_vals, journal_id, account_number) @@ -202,15 +208,19 @@ class account_bank_statement_import(models.TransientModel): # (from which statement transactions originate) don't. # Warning : if company_id is set, the method post_write of class # bank will create a journal - if journal_id: - company_id = self.env['account.journal'].browse( - journal_id).company_id.id + if company_id: vals = self.env['res.partner.bank'].onchange_company_id(company_id) vals_acc.update(vals.get('value', {})) - vals_acc['journal_id'] = journal_id vals_acc['company_id'] = company_id - return self.env['res.partner.bank'].create(vals_acc) + # When the journal is created at same time of the bank account, we need + # to specify the currency to use a first time for the account.account + res_bank = self.env['res.partner.bank'].with_context( + default_currency_id=currency_id).create(vals_acc) + # and a second time on the journal + if currency_id: + res_bank.journal_id.currency = currency_id + return res_bank @api.model def _complete_stmts_vals(self, stmts_vals, journal_id, account_number): diff --git a/account_bank_statement_import/tests/test_import_bank_statement.py b/account_bank_statement_import/tests/test_import_bank_statement.py index 43e59784..9d080d3b 100644 --- a/account_bank_statement_import/tests/test_import_bank_statement.py +++ b/account_bank_statement_import/tests/test_import_bank_statement.py @@ -49,11 +49,11 @@ class TestAccountBankStatemetImport(TransactionCase): "is_company": False, "email": "test@tes.ttest", }) - company_id = self.base_user_root.company_id.id + self.company_id = self.base_user_root.company_id.id self.other_user_id_a = self.res_users_model.create( {"partner_id": self.other_partner_id.id, - "company_id": company_id, - "company_ids": [(4, company_id)], + "company_id": self.company_id, + "company_ids": [(4, self.company_id)], "login": "my_login a", "name": "my user", "groups_id": [(4, self.ref('account.group_account_manager'))] @@ -68,7 +68,7 @@ class TestAccountBankStatemetImport(TransactionCase): st_import = self.statement_import_model.sudo(self.other_user_id_a.id) bank = st_import._create_bank_account( - '001251882303', journal_id=self.journal_id) + '001251882303', company_id=self.company_id) self.assertEqual(bank.partner_id.id, expected_id) diff --git a/account_bank_statement_import_ofx/__openerp__.py b/account_bank_statement_import_ofx/__openerp__.py index c71cd778..88329844 100644 --- a/account_bank_statement_import_ofx/__openerp__.py +++ b/account_bank_statement_import_ofx/__openerp__.py @@ -26,9 +26,7 @@ create periods for the year 2013. """, 'data' : [], 'depends': ['account_bank_statement_import'], - 'demo': [ - 'demo/demo_data.xml', - ], + 'demo': [], 'auto_install': True, 'installable': True, } diff --git a/account_bank_statement_import_ofx/demo/demo_data.xml b/account_bank_statement_import_ofx/demo/demo_data.xml deleted file mode 100644 index 8387f272..00000000 --- a/account_bank_statement_import_ofx/demo/demo_data.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - Bank Journal - (test ofx) - TBNKOFX - bank - - - - - - - - - Your Company - 123456 - - - - bank - - - - -