flectra/addons/stock_account/data/stock_account_data_post_install.yml

65 lines
3.0 KiB
YAML

-
Setting journal and property field (if needed)
-
!python {model: account.journal, id: False} : |
#if we already have a coa installed, create journal and set property field
company_obj = self.env['res.company']
company_ids = company_obj.search([('chart_template_id', '!=', False)]).ids
for company_id in company_ids:
from flectra.tools.translate import _
#Check if property exists for stock account journal exists
PropertyObj = self.env['ir.property']
properties = PropertyObj.search([('name', '=', 'property_stock_journal'), ('company_id', '=', company_id)])
AccountJournal = self.env['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_id = AccountJournal.search([('name', '=', _('Stock Journal')), ('company_id', '=', company_id), ('type', '=', 'general')], limit=1).id
if not journal_id:
journal_id = AccountJournal.create({
'name': _('Stock Journal'),
'type': 'general',
'code': 'STJ',
'company_id': company_id,
'show_on_dashboard': False
}).id
vals = {'name': 'property_stock_journal',
'fields_id': self.env['ir.model.fields'].search([
('name', '=', 'property_stock_journal'),
('model', '=', 'product.category'),
('relation', '=', 'account.journal')], limit=1).id,
'company_id': company_id,
'value': 'account.journal,' + str(journal_id)}
PropertyObj.create(vals)
todo_list = [ # Property Stock Accounts
'property_stock_account_input_categ_id',
'property_stock_account_output_categ_id',
'property_stock_valuation_account_id',
]
company = company_obj.browse(company_id)
for record in todo_list:
account = getattr(company, record)
value = account and 'account.account,' + str(account.id) or False
if value:
field_id = self.env['ir.model.fields'].search([
('name', '=', record),
('model', '=', 'product.category'),
('relation', '=', 'account.account')
], limit=1).id
vals = {
'name': record,
'company_id': company_id,
'fields_id': field_id,
'value': value,
}
properties = PropertyObj.search([('name', '=', record), ('company_id', '=', company.id)])
if properties:
# the property exist: modify it
properties.write(vals)
else:
# create the property
PropertyObj.create(vals)