Debian Codegraph @

addons/l10n_ec_stock/models/account_chart_template.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from odoo import models
3
4
5 class AccountChartTemplate(models.AbstractModel):
6 _inherit = 'account.chart.template'
7
8 def _load(self, template_code, company, install_demo, force_create=True):
9 # EXTENDS account to set up default accounts on stock locations
10 res = super()._load(template_code, company, install_demo, force_create)
11 if template_code == 'ec':
12 self._l10n_ec_setup_location_accounts(company)
13 return res
14
15 def _l10n_ec_setup_location_accounts(self, companies):
16 loss_locs = dict(self.env['stock.location']._read_group(domain=[('usage', '=', 'inventory')], groupby=['company_id', 'id']))
17 prod_locs = dict(self.env['stock.location']._read_group(domain=[('usage', '=', 'production')], groupby=['company_id', 'id']))
18 for company in companies:
19 # get template data
20 Template = self.env['account.chart.template'].with_company(company)
21 template_code = company.chart_template
22 full_data = Template._get_chart_template_data(template_code)
23 template_data = full_data.pop('template_data')
24
25 ref = template_data.get('loss_stock_valuation_account')
26 if (loss_loc := loss_locs.get(company)) and (loss_loc_account := ref and Template.ref(ref, raise_if_not_found=False)):
27 loss_loc.write({
28 'valuation_account_id': loss_loc_account.id,
29 })
30
31 ref = template_data.get('production_stock_valuation_account')
32 if (prod_loc := prod_locs.get(company)) and (prod_loc_account := ref and Template.ref(ref, raise_if_not_found=False)):
33 prod_loc.write({
34 'valuation_account_id': prod_loc_account.id,
35 })