Skip to content

Commit

Permalink
[IMP] Allow to split by partner + account analytic tags
Browse files Browse the repository at this point in the history
  • Loading branch information
vrenaville committed Nov 5, 2019
1 parent cf6d475 commit 6940f51
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
79 changes: 69 additions & 10 deletions account_cutoff_base/models/account_cutoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -134,8 +145,8 @@ 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):
Expand All @@ -152,7 +163,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):
Expand All @@ -168,7 +208,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

Expand Down Expand Up @@ -200,11 +240,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):
Expand All @@ -215,6 +267,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,
}

Expand All @@ -228,7 +281,9 @@ 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:
Expand Down Expand Up @@ -291,6 +346,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)
Expand Down Expand Up @@ -330,6 +387,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.")
Expand Down Expand Up @@ -367,8 +426,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'):
Expand All @@ -381,8 +441,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
Expand Down
6 changes: 6 additions & 0 deletions account_cutoff_base/views/account_cutoff.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
<group name="top">
<group name="general-params">
<field name="cutoff_date"/>
<field name="partner_split"/>
<field name="analytic_tag_split"/>
<field name="total_cutoff_amount"/>
<field name="company_id" groups="base.group_multi_company" widget="selection" />
<field name="company_currency_id" invisible="1"/>
Expand Down Expand Up @@ -98,6 +100,7 @@
<field name="account_id"/>
<field name="cutoff_account_id"/>
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
<field name="analytic_tag_ids" widget="many2many_tags" groups="analytic.group_analytic_accounting"/>
<field name="tax_ids" widget="many2many_tags" invisible="'accrued' not in context.get('cutoff_type', '-')"/>
<field name="amount"/>
<field name="currency_id" invisible="1"/>
Expand All @@ -124,6 +127,7 @@
<field name="name"/>
<field name="cutoff_account_code"/>
<field name="analytic_account_code" groups="analytic.group_analytic_accounting"/>
<field name="analytic_tag_ids" widget="many2many_tags" groups="analytic.group_analytic_accounting"/>
<field name="tax_ids" widget="many2many_tags" invisible="'accrued' not in context.get('cutoff_type', '-')"/>
<field name="amount"/>
<field name="currency_id" invisible="1"/>
Expand All @@ -145,6 +149,7 @@
<field name="sequence"/>
<field name="cutoff_account_id"/>
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
<field name="analytic_tag_ids" widget="many2many_tags" groups="analytic.group_analytic_accounting"/>
<field name="base"/>
<field name="amount"/>
<field name="currency_id" invisible="1"/>
Expand All @@ -167,6 +172,7 @@
<field name="tax_id"/>
<field name="cutoff_account_id"/>
<field name="analytic_account_id" groups="analytic.group_analytic_accounting"/>
<field name="analytic_tag_ids" groups="analytic.group_analytic_accounting"/>
<field name="base"/>
<field name="amount"/>
<field name="currency_id" invisible="1"/>
Expand Down
10 changes: 4 additions & 6 deletions account_cutoff_prepaid/models/account_cutoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])])
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 6940f51

Please sign in to comment.