@@ -2474,20 +2474,27 @@ class FoodPurchase(LivelihoodActivity):
24742474 help_text = _ ("Number of times in a year that the purchase is made" ),
24752475 )
24762476
2477+ def calculate_fields (self ):
2478+ if self .times_per_year is None and self .times_per_month is not None and self .months_per_year is not None :
2479+ self .times_per_year = self .times_per_month * self .months_per_year
2480+ if self .quantity_purchased is None and self .unit_multiple is not None and self .times_per_year is not None :
2481+ self .quantity_purchased = self .unit_multiple * self .times_per_year
2482+ super ().calculate_fields ()
2483+
24772484 def validate_quantity_purchased (self ):
2478- if (
2479- self .quantity_purchased is not None
2480- and self .unit_multiple is not None
2481- and self .times_per_month is not None
2482- and self .months_per_year is not None
2483- ):
2484- if not math .isclose (
2485- self .quantity_purchased , self .unit_multiple * self .times_per_month * self .months_per_year
2486- ):
2485+ if self .times_per_month is not None and self .months_per_year is not None :
2486+ expected_times_per_year = self .times_per_month * self .months_per_year
2487+ if self .times_per_year is not None and not math .isclose (self .times_per_year , expected_times_per_year ):
24872488 raise ValidationError (
24882489 _ (
2489- "Quantity purchased for a Food Purchase must be purchase amount * purchases per month * months per year" # NOQA: E501
2490+ "Times per year must be times per month * months per year. Expected: %(expected)s, Found: %(found)s " # NOQA: E501
24902491 )
2492+ % {"expected" : expected_times_per_year , "found" : self .times_per_year }
2493+ )
2494+ if self .quantity_purchased is not None and self .unit_multiple is not None and self .times_per_year is not None :
2495+ if not math .isclose (self .quantity_purchased , self .unit_multiple * self .times_per_year ):
2496+ raise ValidationError (
2497+ _ ("Quantity purchased for a Food Purchase must be purchase amount * purchases per year" )
24912498 )
24922499
24932500 def validate_expenditure (self ):
@@ -2569,21 +2576,41 @@ def clean(self):
25692576 )
25702577 super ().clean ()
25712578
2579+ def calculate_fields (self ):
2580+ if self .times_per_year is None and self .times_per_month is not None and self .months_per_year is not None :
2581+ self .times_per_year = self .times_per_month * self .months_per_year
2582+ if (
2583+ self .quantity_produced is None
2584+ and self .payment_per_time is not None
2585+ and self .people_per_household is not None
2586+ and self .times_per_year is not None
2587+ ):
2588+ self .quantity_produced = self .payment_per_time * self .people_per_household * self .times_per_year
2589+ super ().calculate_fields ()
2590+
25722591 def validate_quantity_produced (self ):
2592+ if self .times_per_month is not None and self .months_per_year is not None :
2593+ expected_times_per_year = self .times_per_month * self .months_per_year
2594+ if self .times_per_year is not None and not math .isclose (self .times_per_year , expected_times_per_year ):
2595+ raise ValidationError (
2596+ _ (
2597+ "Times per year must be times per month * months per year. Expected: %(expected)s, Found: %(found)s" # NOQA: E501
2598+ )
2599+ % {"expected" : expected_times_per_year , "found" : self .times_per_year }
2600+ )
25732601 if (
25742602 self .quantity_produced is not None
25752603 and self .payment_per_time is not None
25762604 and self .people_per_household is not None
2577- and self .times_per_month is not None
2578- and self .months_per_year is not None
2605+ and self .times_per_year is not None
25792606 ):
25802607 if not math .isclose (
25812608 self .quantity_produced ,
2582- self .payment_per_time * self .people_per_household * self .times_per_month * self . months_per_year ,
2609+ self .payment_per_time * self .people_per_household * self .times_per_year ,
25832610 ):
25842611 raise ValidationError (
25852612 _ (
2586- "Quantity produced for Payment In Kind must be payment per time * number of people * labor per month * months per year" # NOQA: E501
2613+ "Quantity produced for Payment In Kind must be payment per time * number of people * times per year" # NOQA: E501
25872614 )
25882615 )
25892616
@@ -2626,7 +2653,23 @@ class ReliefGiftOther(LivelihoodActivity):
26262653 help_text = _ ("Number of times in a year that the item is received" ),
26272654 )
26282655
2656+ def calculate_fields (self ):
2657+ if self .times_per_year is None and self .times_per_month is not None and self .months_per_year is not None :
2658+ self .times_per_year = self .times_per_month * self .months_per_year
2659+ if self .quantity_produced is None and self .unit_multiple is not None and self .times_per_year is not None :
2660+ self .quantity_produced = self .unit_multiple * self .times_per_year
2661+ super ().calculate_fields ()
2662+
26292663 def validate_quantity_produced (self ):
2664+ if self .times_per_month is not None and self .months_per_year is not None :
2665+ expected_times_per_year = self .times_per_month * self .months_per_year
2666+ if self .times_per_year is not None and not math .isclose (self .times_per_year , expected_times_per_year ):
2667+ raise ValidationError (
2668+ _ (
2669+ "Times per year must be times per month * months per year. Expected: %(expected)s, Found: %(found)s" # NOQA: E501
2670+ )
2671+ % {"expected" : expected_times_per_year , "found" : self .times_per_year }
2672+ )
26302673 if self .quantity_produced is not None and self .unit_multiple is not None and self .times_per_year is not None :
26312674 if not math .isclose (self .quantity_produced , self .unit_multiple * self .times_per_year ):
26322675 raise ValidationError (
@@ -2733,26 +2776,31 @@ def clean(self):
27332776 def validate_income (self ):
27342777 if (
27352778 self .people_per_household is not None
2736- and self .income is not None
2737- and self .payment_per_time is not None
27382779 and self .times_per_month is not None
27392780 and self .months_per_year is not None
27402781 ):
2741- if not math .isclose (
2742- self .income ,
2743- self .payment_per_time * self .people_per_household * self .times_per_month * self .months_per_year ,
2744- ):
2782+ expected_times_per_year = self .people_per_household * self .times_per_month * self .months_per_year
2783+ if self .times_per_year is not None and not math .isclose (self .times_per_year , expected_times_per_year ):
27452784 raise ValidationError (
27462785 _ (
2747- "Quantity produced for Other Cash Income must be payment per time * number of people * labor per month * months per year" # NOQA: E501
2786+ "Times per year must be people per household * times per month * months per year. Expected: %(expected)s, Found: %(found)s " # NOQA: E501
27482787 )
2788+ % {"expected" : expected_times_per_year , "found" : self .times_per_year }
27492789 )
2750- elif self .income is not None and self .payment_per_time is not None and self .times_per_year is not None :
2790+ if self .income is not None and self .payment_per_time is not None and self .times_per_year is not None :
27512791 if not math .isclose (self .income , self .payment_per_time * self .times_per_year ):
27522792 raise ValidationError (_ ("Income for 'Other Cash Income' must be payment per time * times per year" ))
27532793
27542794 def calculate_fields (self ):
2755- self .times_per_year = self .people_per_household * self .times_per_month * self .months_per_year
2795+ if (
2796+ self .times_per_year is None
2797+ and self .people_per_household is not None
2798+ and self .times_per_month is not None
2799+ and self .months_per_year is not None
2800+ ):
2801+ self .times_per_year = self .people_per_household * self .times_per_month * self .months_per_year
2802+ if self .income is None and self .payment_per_time is not None and self .times_per_year is not None :
2803+ self .income = self .payment_per_time * self .times_per_year
27562804 super ().calculate_fields ()
27572805
27582806 class Meta :
@@ -2796,6 +2844,18 @@ class OtherPurchase(LivelihoodActivity):
27962844 help_text = _ ("Number of times in a year that the product is purchased" ),
27972845 )
27982846
2847+ def calculate_fields (self ):
2848+ if self .times_per_year is None and self .times_per_month is not None and self .months_per_year is not None :
2849+ self .times_per_year = self .times_per_month * self .months_per_year
2850+ if (
2851+ self .expenditure is None
2852+ and self .price is not None
2853+ and self .unit_multiple is not None
2854+ and self .times_per_year is not None
2855+ ):
2856+ self .expenditure = self .price * self .unit_multiple * self .times_per_year
2857+ super ().calculate_fields ()
2858+
27992859 def validate_expenditure (self ):
28002860 errors = []
28012861 if self .times_per_month is not None and self .months_per_year is not None :
0 commit comments