Trade Finance - Facility - Limit ordering
This commit is contained in:
@@ -210,10 +210,8 @@ class FacilityLimit(ModelSQL, ModelView):
|
|||||||
tenor = fields.Integer('Tenor (days)',
|
tenor = fields.Integer('Tenor (days)',
|
||||||
help='Maximum duration of financing from drawdown to repayment')
|
help='Maximum duration of financing from drawdown to repayment')
|
||||||
sequence = fields.Integer('Sequence')
|
sequence = fields.Integer('Sequence')
|
||||||
path = fields.Char('Path', readonly=True,
|
|
||||||
help='Hierarchical sort key — auto-computed')
|
|
||||||
|
|
||||||
_order = [('path', 'ASC')]
|
_order = [('sequence', 'ASC'), ('id', 'ASC')]
|
||||||
|
|
||||||
is_global = fields.Function(
|
is_global = fields.Function(
|
||||||
fields.Boolean('Global Limit',
|
fields.Boolean('Global Limit',
|
||||||
@@ -247,59 +245,7 @@ class FacilityLimit(ModelSQL, ModelView):
|
|||||||
if values.get('parent') and not values.get('facility'):
|
if values.get('parent') and not values.get('facility'):
|
||||||
parent = cls(values['parent'])
|
parent = cls(values['parent'])
|
||||||
values['facility'] = parent.facility.id
|
values['facility'] = parent.facility.id
|
||||||
records = super().create(vlist)
|
return super().create(vlist)
|
||||||
cls._update_path(records)
|
|
||||||
return records
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def write(cls, *args):
|
|
||||||
super().write(*args)
|
|
||||||
records = []
|
|
||||||
actions = iter(args)
|
|
||||||
for recs, _ in zip(actions, actions):
|
|
||||||
records.extend(recs)
|
|
||||||
cls._update_path(records)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _update_path(cls, records):
|
|
||||||
to_write = []
|
|
||||||
for record in records:
|
|
||||||
path = cls._compute_path(record)
|
|
||||||
if record.path != path:
|
|
||||||
to_write.extend([[record], {'path': path}])
|
|
||||||
if to_write:
|
|
||||||
# Use super().write to avoid recursion
|
|
||||||
super().write(*to_write)
|
|
||||||
# Also update all descendants
|
|
||||||
all_children = []
|
|
||||||
for record in records:
|
|
||||||
all_children.extend(cls._get_descendants(record))
|
|
||||||
if all_children:
|
|
||||||
descendant_writes = []
|
|
||||||
for child in all_children:
|
|
||||||
path = cls._compute_path(child)
|
|
||||||
if child.path != path:
|
|
||||||
descendant_writes.extend([[child], {'path': path}])
|
|
||||||
if descendant_writes:
|
|
||||||
super().write(*descendant_writes)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _compute_path(cls, record):
|
|
||||||
parts = []
|
|
||||||
current = record
|
|
||||||
while current:
|
|
||||||
parts.append('%07d' % (current.sequence or 0))
|
|
||||||
current = current.parent
|
|
||||||
parts.reverse()
|
|
||||||
return '/'.join(parts)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_descendants(cls, record):
|
|
||||||
result = []
|
|
||||||
for child in record.children:
|
|
||||||
result.append(child)
|
|
||||||
result.extend(cls._get_descendants(child))
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_is_global(self, name):
|
def get_is_global(self, name):
|
||||||
return self.parent is None
|
return self.parent is None
|
||||||
|
|||||||
Reference in New Issue
Block a user