275 lines
10 KiB
Python
275 lines
10 KiB
Python
from decimal import Decimal
|
|
from django.db.models import Sum, Count, Min, Max, Avg
|
|
from django.core.exceptions import ValidationError
|
|
from custom_objects.models import CustomFieldDefinition, CustomRecord, CustomObjectDefinition
|
|
|
|
STANDARD_REVERSE_RELATIONS = {
|
|
'Account': {
|
|
'Opportunity': {'fk_field': 'account', 'related_name': 'opportunities'},
|
|
'Contact': {'fk_field': 'account', 'related_name': 'contacts'},
|
|
'Activity': {'fk_field': 'account', 'related_name': 'activities'},
|
|
},
|
|
'Contact': {
|
|
'Opportunity': {'fk_field': 'primary_contact', 'related_name': 'opportunities'},
|
|
'Activity': {'fk_field': 'contact', 'related_name': 'activities'},
|
|
},
|
|
'Lead': {
|
|
'Activity': {'fk_field': 'lead', 'related_name': 'activities'},
|
|
},
|
|
'Opportunity': {
|
|
'Activity': {'fk_field': 'opportunity', 'related_name': 'activities'},
|
|
}
|
|
}
|
|
|
|
|
|
def recalculate_rollups_for_record(parent_entity_or_object, parent_id):
|
|
"""
|
|
Recalculates all roll-up summary fields defined on the parent record.
|
|
parent_entity_or_object: Model class name (e.g. 'Account') or CustomObjectDefinition instance / name.
|
|
parent_id: Primary key of parent record.
|
|
"""
|
|
if not parent_id:
|
|
return
|
|
|
|
# Check for standard model vs custom object
|
|
if isinstance(parent_entity_or_object, str):
|
|
# Could be standard entity name or custom object api_name
|
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
|
from outreach.models import Campaign
|
|
model_map = {
|
|
'Account': Account,
|
|
'Contact': Contact,
|
|
'Lead': Lead,
|
|
'Opportunity': Opportunity,
|
|
'Activity': Activity,
|
|
'Campaign': Campaign,
|
|
}
|
|
parent_model = model_map.get(parent_entity_or_object)
|
|
if parent_model:
|
|
rollup_fields = CustomFieldDefinition.objects.filter(
|
|
entity_name=parent_entity_or_object,
|
|
field_type='rollup'
|
|
)
|
|
try:
|
|
parent_instance = parent_model.objects.get(pk=parent_id)
|
|
except parent_model.DoesNotExist:
|
|
return
|
|
|
|
if not rollup_fields.exists():
|
|
return
|
|
|
|
if parent_instance.custom_fields is None:
|
|
parent_instance.custom_fields = {}
|
|
|
|
modified = False
|
|
for rf in rollup_fields:
|
|
val = compute_rollup_value(parent_instance, rf)
|
|
parent_instance.custom_fields[rf.name] = val
|
|
modified = True
|
|
|
|
if modified:
|
|
parent_instance.save(update_fields=['custom_fields'])
|
|
return
|
|
|
|
# Custom object records rollup
|
|
# If parent is custom object
|
|
rollup_fields = CustomFieldDefinition.objects.filter(
|
|
custom_object__name=str(parent_entity_or_object),
|
|
field_type='rollup'
|
|
)
|
|
if not rollup_fields.exists():
|
|
return
|
|
try:
|
|
parent_record = CustomRecord.objects.get(pk=parent_id)
|
|
except CustomRecord.DoesNotExist:
|
|
return
|
|
|
|
if parent_record.data is None:
|
|
parent_record.data = {}
|
|
|
|
modified = False
|
|
for rf in rollup_fields:
|
|
val = compute_rollup_value(parent_record, rf)
|
|
parent_record.data[rf.name] = val
|
|
modified = True
|
|
|
|
if modified:
|
|
parent_record.save(update_fields=['data'])
|
|
|
|
|
|
def should_recalculate_child_rollups(child_name, instance, created):
|
|
"""
|
|
Early-exit check:
|
|
Returns True if:
|
|
- This is a new record (created=True)
|
|
- Or if any field relevant to active roll-up definitions (target field, filter criteria, or parent FK)
|
|
actually changed on the instance compared to its pre_save snapshot.
|
|
"""
|
|
if created:
|
|
return True
|
|
|
|
old_state = getattr(instance, '_rollup_old_state', None)
|
|
if not old_state:
|
|
return True
|
|
|
|
from custom_objects.models import CustomFieldDefinition
|
|
from django.db.models import Q
|
|
|
|
active_rollups = CustomFieldDefinition.objects.filter(
|
|
Q(field_type='rollup_summary') | Q(field_type='rollup')
|
|
).filter(
|
|
Q(rollup_summary_object__iexact=child_name)
|
|
)
|
|
|
|
if not active_rollups.exists():
|
|
return False
|
|
|
|
# Check if any relevant field changed
|
|
for rf in active_rollups:
|
|
# Check target aggregation field
|
|
if rf.rollup_field:
|
|
curr_val = getattr(instance, rf.rollup_field, None)
|
|
if isinstance(instance, dict):
|
|
curr_val = instance.get(rf.rollup_field)
|
|
elif hasattr(instance, 'data') and isinstance(instance.data, dict) and rf.rollup_field in instance.data:
|
|
curr_val = instance.data.get(rf.rollup_field)
|
|
elif hasattr(instance, 'custom_fields') and isinstance(instance.custom_fields, dict) and rf.rollup_field in instance.custom_fields:
|
|
curr_val = instance.custom_fields.get(rf.rollup_field)
|
|
|
|
if rf.rollup_field in old_state and curr_val != old_state[rf.rollup_field]:
|
|
return True
|
|
|
|
# Check filter criteria fields
|
|
filters = rf.rollup_filter or {}
|
|
for f_key in filters:
|
|
curr_val = getattr(instance, f_key, None)
|
|
if hasattr(instance, 'data') and isinstance(instance.data, dict) and f_key in instance.data:
|
|
curr_val = instance.data.get(f_key)
|
|
elif hasattr(instance, 'custom_fields') and isinstance(instance.custom_fields, dict) and f_key in instance.custom_fields:
|
|
curr_val = instance.custom_fields.get(f_key)
|
|
|
|
if f_key in old_state and curr_val != old_state[f_key]:
|
|
return True
|
|
|
|
# Check if parent relation FK changed
|
|
for fk in ['account_id', 'contact_id', 'opportunity_id', 'lead_id', 'parent_id', 'object_id']:
|
|
if fk in old_state:
|
|
curr_fk = getattr(instance, fk, None)
|
|
if hasattr(instance, 'data') and isinstance(instance.data, dict) and fk in instance.data:
|
|
curr_fk = instance.data.get(fk)
|
|
if curr_fk != old_state[fk]:
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def compute_rollup_value(parent_instance, rollup_field_def):
|
|
"""
|
|
Computes aggregation for a given rollup field definition against parent instance.
|
|
rollup_field_def: CustomFieldDefinition with rollup attributes:
|
|
- rollup_summary_object (e.g. 'Opportunity' or 'invoice_item__c')
|
|
- rollup_type ('COUNT', 'SUM', 'MIN', 'MAX', 'AVG')
|
|
- rollup_field (e.g. 'amount')
|
|
- rollup_filter (dict or None)
|
|
"""
|
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
|
from outreach.models import Campaign
|
|
model_map = {
|
|
'Account': Account,
|
|
'Contact': Contact,
|
|
'Lead': Lead,
|
|
'Opportunity': Opportunity,
|
|
'Activity': Activity,
|
|
'Campaign': Campaign,
|
|
}
|
|
|
|
child_entity = rollup_field_def.rollup_summary_object
|
|
rollup_type = (rollup_field_def.rollup_type or 'COUNT').upper()
|
|
target_field = rollup_field_def.rollup_field
|
|
filter_criteria = rollup_field_def.rollup_filter or {}
|
|
|
|
# Check standard child models
|
|
if child_entity in model_map:
|
|
child_model = model_map[child_entity]
|
|
parent_class_name = parent_instance.__class__.__name__
|
|
|
|
# Resolve foreign key lookup from child to parent
|
|
rel_info = STANDARD_REVERSE_RELATIONS.get(parent_class_name, {}).get(child_entity)
|
|
if rel_info:
|
|
filter_kwargs = {f"{rel_info['fk_field']}_id": parent_instance.id}
|
|
else:
|
|
filter_kwargs = {f"{parent_class_name.lower()}_id": parent_instance.id}
|
|
|
|
# Apply additional filter criteria if present
|
|
for k, v in filter_criteria.items():
|
|
filter_kwargs[k] = v
|
|
|
|
qs = child_model.objects.filter(**filter_kwargs)
|
|
|
|
if rollup_type == 'COUNT':
|
|
return qs.count()
|
|
elif rollup_type == 'SUM':
|
|
res = qs.aggregate(val=Sum(target_field))['val']
|
|
return float(res) if isinstance(res, (int, float, Decimal)) else 0.0
|
|
elif rollup_type == 'MIN':
|
|
res = qs.aggregate(val=Min(target_field))['val']
|
|
return float(res) if isinstance(res, (int, float, Decimal)) else (res or 0)
|
|
elif rollup_type == 'MAX':
|
|
res = qs.aggregate(val=Max(target_field))['val']
|
|
return float(res) if isinstance(res, (int, float, Decimal)) else (res or 0)
|
|
elif rollup_type == 'AVG':
|
|
res = qs.aggregate(val=Avg(target_field))['val']
|
|
return float(res) if isinstance(res, (int, float, Decimal)) else 0.0
|
|
|
|
# Custom object records rollup (e.g. child CustomRecord pointing to parent)
|
|
child_cobj = CustomObjectDefinition.objects.filter(
|
|
name__iexact=child_entity
|
|
).first()
|
|
if child_cobj:
|
|
parent_class_name = parent_instance.__class__.__name__
|
|
filter_kwargs = {'custom_object': child_cobj}
|
|
|
|
# Polymorphic parent link: check either parent standard object id or parent custom record id
|
|
if parent_class_name == 'CustomRecord':
|
|
filter_kwargs['data__parent_id'] = parent_instance.id
|
|
else:
|
|
filter_kwargs[f"data__{parent_class_name.lower()}_id"] = parent_instance.id
|
|
|
|
qs = CustomRecord.objects.filter(**filter_kwargs)
|
|
|
|
# Apply in-memory filtering for custom record JSON data
|
|
records = []
|
|
for r in qs:
|
|
match = True
|
|
for fk, fv in filter_criteria.items():
|
|
if r.data.get(fk) != fv:
|
|
match = False
|
|
break
|
|
if match:
|
|
records.append(r)
|
|
|
|
if rollup_type == 'COUNT':
|
|
return len(records)
|
|
|
|
vals = []
|
|
for r in records:
|
|
v = r.data.get(target_field)
|
|
if v is not None:
|
|
try:
|
|
vals.append(float(v))
|
|
except (ValueError, TypeError):
|
|
pass
|
|
|
|
if not vals:
|
|
return 0.0 if rollup_type in ('SUM', 'AVG') else 0
|
|
|
|
if rollup_type == 'SUM':
|
|
return sum(vals)
|
|
elif rollup_type == 'MIN':
|
|
return min(vals)
|
|
elif rollup_type == 'MAX':
|
|
return max(vals)
|
|
elif rollup_type == 'AVG':
|
|
return sum(vals) / len(vals)
|
|
|
|
return 0
|