From de1556ac7e9ddd6fd1753d4cfc5db5760f4a9eb9 Mon Sep 17 00:00:00 2001 From: Josse Colpaert Date: Mon, 18 May 2015 12:37:32 +0200 Subject: [PATCH] [FIX] stock_account: Post install should check for all companies and not always create The method will check all companies with a chart of account if they have a property for the stock journal. If there is no stock journal it will create one. Previously it created only one for the main company and this gave an error when updating the module again --- .../stock_account_post_install.yml | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/addons/stock_account/stock_account_post_install.yml b/addons/stock_account/stock_account_post_install.yml index 6aa755914c3..9c809124b3a 100644 --- a/addons/stock_account/stock_account_post_install.yml +++ b/addons/stock_account/stock_account_post_install.yml @@ -3,19 +3,26 @@ - !python {model: account.journal} : | #if we already have a coa installed, create journal and set property field - company_id = ref('base.main_company') - if self.pool.get('res.company').browse(cr, uid, company_id).chart_template_id: + company_obj = self.pool['res.company'] + company_ids = company_obj.search(cr, uid, [('chart_template_id', '!=', False)]) + for company_id in company_ids: from openerp.tools.translate import _ - journal_id = self.create(cr, uid, {'name': _('Stock Journal'), 'type': 'general', 'code': 'STJ', 'show_on_dashboard': False,}) - #update property + + #Check if property exists for stock account journal exists PropertyObj = self.pool.get('ir.property') properties = PropertyObj.search(cr, uid, [('name', '=', 'property_stock_journal'), ('company_id', '=', company_id)]) - vals = {'name': 'property_stock_journal', - 'fields_id': self.pool.get('ir.model.fields').search(cr, uid, [('name', '=', 'property_stock_journal'), ('model', '=', 'product.category'), ('relation', '=', 'account.journal')], limit=1)[0], - 'company_id': company_id, - 'value': 'account.journal,' + str(journal_id)} - if properties: - PropertyObj.write(cr, uid, properties, vals) - else: - #create the property + AccountJournal = self.pool.get('account.journal') + + #If not, check if you can find a journal that is already there with the same name, otherwise create one + if not properties: + journal_ids = AccountJournal.search(cr, uid, [('name', '=', _('Stock Journal')), ('company_id', '=', company_id), ('type', '=', 'general')]) + if journal_ids: + journal_id = journal_ids[0] + else: + journal_id = AccountJournal.create(cr, uid, {'name': _('Stock Journal'), 'type': 'general', 'code': 'STJ', 'company_id': company_id, 'show_on_dashboard': False}) + vals = {'name': 'property_stock_journal', + 'fields_id': self.pool.get('ir.model.fields').search(cr, uid, [('name', '=', 'property_stock_journal'), ('model', '=', 'product.category'), ('relation', '=', 'account.journal')], limit=1)[0], + 'company_id': company_id, + 'value': 'account.journal,' + str(journal_id)} + PropertyObj.create(cr, uid, vals)