from django.db.models.signals import pre_save
from django.dispatch import receiver
from decimal import Decimal


@receiver(pre_save, sender='users.User')
def compute_hourly_rate(sender, instance, **kwargs):
    """Auto-compute hourly rate whenever salary changes: salary / 176"""
    if instance.salary_pkr and instance.salary_pkr > 0:
        instance.hourly_rate = (instance.salary_pkr / Decimal('176')).quantize(Decimal('0.01'))
    else:
        instance.hourly_rate = Decimal('0.00')
