Skip to content

Instantly share code, notes, and snippets.

@josemoralesp
Created June 19, 2017 17:04
Show Gist options
  • Save josemoralesp/8bd6ab6dede1831aacc74aa70e7dc44f to your computer and use it in GitHub Desktop.
Save josemoralesp/8bd6ab6dede1831aacc74aa70e7dc44f to your computer and use it in GitHub Desktop.
from collections import defaultdict
@api.depends('document_ids', 'document_ids.residual',
'document_ids.payment', 'document_ids.total')
def _get_amount(self):
field_sum = ['payment', 'total', 'aawp_id']
field_spans = ['span01', 'span02', 'span03', 'span04', 'span05']
for record in self:
res = defaultdict(float)
direction = record.aaw_id.direction == 'past'
spans = [record.aaw_id.period_length * x * (direction and 1 or -1)
for x in range(5)]
vals = record.document_ids.read_group(
[('aawp_id', '=', record.id)], field_sum, ['aawp_id'])
vals = vals[0] if vals else {}
res['payment'] += vals[0].get('payment', 0)
res['total'] += vals[0].get('total', 0)
vals = record.document_ids.read_group(
[('aawp_id', '=', record.id),
('due_days', '<=' if direction else '>', 0)],
['residual', 'aawp_id'], ['aawp_id'])
if vals:
res['not_due'] += vals[0].get('residual', 0)
else:
record._get_amount_span(
field_spans, res, doc, spans, direction)
record.update(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment