from decimal import Decimal from django.conf import settings from django_ledger.models.items import ItemModel def calculate_vat(value): """Helper to calculate VAT dynamically for a given value.""" vat_rate = getattr(settings, "VAT_RATE", Decimal("0.15")) # Default VAT rate return (value * vat_rate).quantize(Decimal("0.01")) # def get_financial_value(instance,attribute,vat=False): # if vat: # return calculate_vat(getattr(instance, attribute, Decimal('0.00')) if instance else Decimal('0.00')) # return getattr(instance, attribute, Decimal('0.00')) if instance else Decimal('0.00') def get_financial_value(name, vat=False): val = ItemModel.objects.filter(name=name).first() if not val: return 0 if vat: return (val.price * settings.VAT_RATE).quantize(Decimal("0.01")) return val.price def get_total_financials(instance, vat=False): total = 0 if instance.additional_services.count() != 0: total_additional_services = sum( x.price for x in instance.additional_services.all() ) total = instance.selling_price + total_additional_services if vat and total: total = (total * settings.VAT_RATE).quantize(Decimal("0.01")) + total return total def get_total(instance): total = get_total_financials(instance, vat=True) return total