diff --git a/account_cutoff_base/models/account_cutoff.py b/account_cutoff_base/models/account_cutoff.py index 365147c76a4c..2768884e35a4 100644 --- a/account_cutoff_base/models/account_cutoff.py +++ b/account_cutoff_base/models/account_cutoff.py @@ -54,6 +54,17 @@ def _default_cutoff_journal_id(self): states={'draft': [('readonly', False)]}, copy=False, track_visibility='onchange' ) + analytic_tag_split = fields.Boolean( + string='Split by analytic tags', readonly=True, + states={'draft': [('readonly', False)]}, copy=False, + track_visibility='onchange' + ) + partner_split = fields.Boolean( + string='Split by Partner', readonly=True, + states={'draft': [('readonly', False)]}, copy=False, + track_visibility='onchange' + ) + cutoff_type = fields.Selection( selection='_selection_cutoff_type', string='Type', @@ -134,8 +145,7 @@ def _default_cutoff_journal_id(self): _sql_constraints = [( 'date_type_company_uniq', 'unique(cutoff_date, company_id, cutoff_type)', - _('A cutoff of the same type already exists with this cut-off date !') - )] + _('A cutoff of the same type already exists with this cut-off date !'))] @api.multi def back2draft(self): @@ -152,7 +162,36 @@ def _get_merge_keys(self): same values for these fields will be merged. The list must at least contain account_id. """ - return ['account_id', 'analytic_account_id'] + merge_keys = ['account_id', 'analytic_account_id'] + if self.analytic_tag_split: + merge_keys.append('analytic_tag_ids') + if self.partner_split: + merge_keys.append('partner_id') + return merge_keys + + def _prepare_merge_line(self, key, provision_line): + """ + Prepare value to be include in the tuple, + specially design for many2many + """ + if key == 'analytic_tag_ids': + val = tuple(sorted(provision_line.get(key)[0][2])) + else: + val = provision_line.get(key) + return val + + def _convert_merge_value(self, key, value): + """ + Return Value specially design for many2many + """ + if key == 'analytic_tag_ids': + if value: + val = [(6, 0, list(value))] + else: + val = [(6, 0, [])] + else: + val = value + return val @api.multi def _prepare_move(self, to_provision): @@ -168,7 +207,7 @@ def _prepare_move(self, to_provision): 'credit': amount >= 0 and amount or 0, } for k, v in zip(merge_keys, merge_values): - vals[k] = v + vals[k] = self._convert_merge_value(k, v) movelines_to_create.append((0, 0, vals)) amount_total += amount @@ -200,11 +239,23 @@ def _prepare_provision_line(self, cutoff_line): If you override this, the added fields must also be added in an override of _get_merge_keys. """ - return { + provision_line = { 'account_id': cutoff_line.cutoff_account_id.id, 'analytic_account_id': cutoff_line.analytic_account_id.id, + 'amount': cutoff_line.cutoff_amount, } + if self.analytic_tag_split: + provision_line.update( + { + 'analytic_tag_ids': [ + (6, 0, cutoff_line.analytic_tag_ids.ids) + ] + } + ) + if self.partner_split: + provision_line.update({'partner_id': cutoff_line.partner_id.id}) + return provision_line @api.multi def _prepare_provision_tax_line(self, cutoff_tax_line): @@ -215,6 +266,7 @@ def _prepare_provision_tax_line(self, cutoff_tax_line): return { 'account_id': cutoff_tax_line.cutoff_account_id.id, 'analytic_account_id': cutoff_tax_line.analytic_account_id.id, + 'analytic_tag_ids': [(6, 0, cutoff_tax_line.analytic_tag_ids.ids)], 'amount': cutoff_tax_line.cutoff_amount, } @@ -228,7 +280,7 @@ def _merge_provision_lines(self, provision_lines): to_provision = {} merge_keys = self._get_merge_keys() for provision_line in provision_lines: - key = tuple([provision_line.get(key) for key in merge_keys]) + key = tuple([self._prepare_merge_line(key, provision_line) for key in merge_keys]) if key in to_provision: to_provision[key] += provision_line['amount'] else: @@ -291,6 +343,8 @@ class AccountCutoffLine(models.Model): cutoff_account_code = fields.Char( related='cutoff_account_id.code', string='Cut-off Account Code', readonly=True) + analytic_tag_ids = fields.Many2many( + 'account.analytic.tag', string='Analytic Tags', readonly=True) analytic_account_id = fields.Many2one( 'account.analytic.account', string='Analytic Account', domain=[('account_type', '!=', 'closed')], readonly=True) @@ -330,6 +384,8 @@ class AccountCutoffTaxLine(models.Model): analytic_account_id = fields.Many2one( 'account.analytic.account', string='Analytic Account', domain=[('account_type', '!=', 'closed')], readonly=True) + analytic_tag_ids = fields.Many2many( + 'account.analytic.tag', string='Analytic Tags', readonly=True) base = fields.Monetary( currency_field='currency_id', readonly=True, help="Base Amount in the currency of the PO.") @@ -367,8 +423,9 @@ class AccountCutoffMapping(models.Model): ('accrued_revenue', 'Accrued Revenue'), ('accrued_expense', 'Accrued Expense'), ('prepaid_revenue', 'Prepaid Revenue'), - ('prepaid_expense', 'Prepaid Expense'), - ], string='Cut-off Type', required=True) + ('prepaid_expense', 'Prepaid Expense'), ], + string='Cut-off Type', + required=True) @api.model def _get_mapping_dict(self, company_id, cutoff_type='all'): @@ -381,8 +438,7 @@ def _get_mapping_dict(self, company_id, cutoff_type='all'): cutoff_type_filter = ('all', cutoff_type) mappings = self.search([ ('company_id', '=', company_id), - ('cutoff_type', 'in', cutoff_type_filter), - ]) + ('cutoff_type', 'in', cutoff_type_filter), ]) mapping = {} for item in mappings: mapping[item.account_id.id] = item.cutoff_account_id.id diff --git a/account_cutoff_base/views/account_cutoff.xml b/account_cutoff_base/views/account_cutoff.xml index 6a0815d41246..aaaf6896a04e 100644 --- a/account_cutoff_base/views/account_cutoff.xml +++ b/account_cutoff_base/views/account_cutoff.xml @@ -32,6 +32,8 @@ + + @@ -98,6 +100,7 @@ + @@ -124,6 +127,7 @@ + @@ -145,6 +149,7 @@ + @@ -167,6 +172,7 @@ + diff --git a/account_cutoff_prepaid/models/account_cutoff.py b/account_cutoff_prepaid/models/account_cutoff.py index a5f9e46d44bb..982904d9dd78 100644 --- a/account_cutoff_prepaid/models/account_cutoff.py +++ b/account_cutoff_prepaid/models/account_cutoff.py @@ -14,8 +14,7 @@ def _get_default_source_journals(self): cutoff_type = self.env.context.get('cutoff_type') mapping = { 'prepaid_expense': 'purchase', - 'prepaid_revenue': 'sale', - } + 'prepaid_revenue': 'sale', } if cutoff_type in mapping: src_journals = self.env['account.journal'].search( [('type', '=', mapping[cutoff_type])]) @@ -104,6 +103,7 @@ def _prepare_prepaid_lines(self, aml, mapping): 'account_id': aml.account_id.id, 'cutoff_account_id': cutoff_account_id, 'analytic_account_id': aml.analytic_account_id.id or False, + 'analytic_tag_ids': [(6, 0, aml.analytic_tag_ids.ids or [])], 'total_days': total_days, 'prepaid_days': prepaid_days, 'amount': aml.credit - aml.debit, @@ -129,15 +129,13 @@ def get_prepaid_lines(self): domain = [ ('start_date', '<=', self.end_date), ('end_date', '>=', self.start_date), - ('journal_id', 'in', self.source_journal_ids.ids) - ] + ('journal_id', 'in', self.source_journal_ids.ids)] else: domain = [ ('start_date', '!=', False), ('journal_id', 'in', self.source_journal_ids.ids), ('end_date', '>', cutoff_date_str), - ('date', '<=', cutoff_date_str) - ] + ('date', '<=', cutoff_date_str)] # Search for account move lines in the source journals amls = aml_obj.search(domain)