diff options
Diffstat (limited to 'archaeological_files/models.py')
-rw-r--r-- | archaeological_files/models.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/archaeological_files/models.py b/archaeological_files/models.py index c27086d34..5e586aa28 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -1520,12 +1520,18 @@ class File( equipments = [] service_types = list(GenericEquipmentServiceType.objects.all()) for service_type in service_types: - q = self.equipment_costs.filter( - equipment_service_cost__equipment_service_type__generic_equipment_type=service_type) + query = Q( + equipment_service_cost__equipment_service_type__generic_equipment_type=service_type, + equipment_service_cost__parent__isnull=True + ) | Q( + equipment_service_cost__parent__generic_equipment_type=service_type + ) + q = self.equipment_costs.filter(query) if not q.count(): continue equipments.append([service_type.label, 0, 0, 0, []]) - for cost in q.all(): + + for cost in PreventiveFileEquipmentServiceCost.get_sorted_costs(q): equipments[-1][-4] += cost.cost_planned equipments[-1][-3] += cost.cost_worked equipments[-1][-1].append(cost) @@ -1772,3 +1778,24 @@ class PreventiveFileEquipmentServiceCost(models.Model): @property def cost_worked(self): return (self.equipment_service_cost.unitary_cost or 0) * self.quantity_worked + + @classmethod + def get_sorted_costs(cls, query, values=None): + # group costs by parents in order to sort + costs = {} + for cost in query.all(): + ec = cost.equipment_service_cost + et = ec.equipment_service_type + if ec.parent_id: + key = ec.parent_id + else: + key = et.id + if key not in costs: + costs[key] = [] + costs[key].append((ec.id if ec.parent_id else 0, cost)) + + cost_list = [] + for key in costs: + for __, cost in sorted(costs[key], key=lambda x: x[0]): + cost_list.append(cost) + return cost_list |