Merge pull request '# 342 Fix' (#343) from 342_Rounding_error_on_db_SUM into master
Reviewed-on: SGD-Nord/konova#343
This commit is contained in:
		
						commit
						9035b07801
					
				@ -199,7 +199,9 @@ class AbstractCompensation(BaseObject,
 | 
				
			|||||||
        Returns:
 | 
					        Returns:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        return qs.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					        val = qs.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
				
			||||||
 | 
					        val = float('{:0.2f}'.format(val))
 | 
				
			||||||
 | 
					        return val
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def quality_check(self) -> CompensationQualityChecker:
 | 
					    def quality_check(self) -> CompensationQualityChecker:
 | 
				
			||||||
        """ Performs data quality check
 | 
					        """ Performs data quality check
 | 
				
			||||||
 | 
				
			|||||||
@ -57,7 +57,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def clean(self):
 | 
					    def clean(self):
 | 
				
			||||||
        # Deductable surface can not be larger than added states after surface
 | 
					        # Deductable surface can not be larger than added states after surface
 | 
				
			||||||
        after_state_sum = self.get_state_after_surface_sum()
 | 
					        after_state_sum = self.get_surface_after_states()
 | 
				
			||||||
        if self.deductable_surface > after_state_sum:
 | 
					        if self.deductable_surface > after_state_sum:
 | 
				
			||||||
            raise ValidationError(_("Deductable surface can not be larger than existing surfaces in after states"))
 | 
					            raise ValidationError(_("Deductable surface can not be larger than existing surfaces in after states"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -96,15 +96,9 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
 | 
				
			|||||||
        Returns:
 | 
					        Returns:
 | 
				
			||||||
            sum_surface (float)
 | 
					            sum_surface (float)
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        return self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					        val = self.deductions.all().aggregate(Sum("surface"))["surface__sum"] or 0
 | 
				
			||||||
 | 
					        val = float('{:0.2f}'.format(val))
 | 
				
			||||||
    def get_state_after_surface_sum(self) -> float:
 | 
					        return val
 | 
				
			||||||
        """ Calculates the account's after state surface sum
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Returns:
 | 
					 | 
				
			||||||
            sum_surface (float)
 | 
					 | 
				
			||||||
        """
 | 
					 | 
				
			||||||
        return self.after_states.all().aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __calculate_deductable_rest(self):
 | 
					    def __calculate_deductable_rest(self):
 | 
				
			||||||
        """ Calculates available rest surface of the eco account
 | 
					        """ Calculates available rest surface of the eco account
 | 
				
			||||||
@ -114,10 +108,7 @@ class EcoAccount(AbstractCompensation, ShareableObjectMixin, RecordableObjectMix
 | 
				
			|||||||
        Returns:
 | 
					        Returns:
 | 
				
			||||||
            ret_val_total (float): Total amount
 | 
					            ret_val_total (float): Total amount
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        deductions = self.deductions.filter(
 | 
					        deductions_surfaces = self.get_deductions_surface()
 | 
				
			||||||
            intervention__deleted=None,
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        deductions_surfaces = deductions.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        available_surface = self.deductable_surface
 | 
					        available_surface = self.deductable_surface
 | 
				
			||||||
        if available_surface is None:
 | 
					        if available_surface is None:
 | 
				
			||||||
 | 
				
			|||||||
@ -95,7 +95,7 @@ class EcoAccountQualityChecker(CompensationQualityChecker):
 | 
				
			|||||||
        is_surface_invalid = surface == 0
 | 
					        is_surface_invalid = surface == 0
 | 
				
			||||||
        if is_surface_invalid:
 | 
					        if is_surface_invalid:
 | 
				
			||||||
            self._add_missing_attr_name(_("Available Surface"))
 | 
					            self._add_missing_attr_name(_("Available Surface"))
 | 
				
			||||||
        after_state_surface = self.obj.get_state_after_surface_sum()
 | 
					        after_state_surface = self.obj.get_surface_after_states()
 | 
				
			||||||
        if surface > after_state_surface:
 | 
					        if surface > after_state_surface:
 | 
				
			||||||
            self.messages.append(
 | 
					            self.messages.append(
 | 
				
			||||||
                _("Deductable surface can not be larger than state surface")
 | 
					                _("Deductable surface can not be larger than state surface")
 | 
				
			||||||
 | 
				
			|||||||
@ -228,8 +228,6 @@ def detail_view(request: HttpRequest, id: str):
 | 
				
			|||||||
    _user = request.user
 | 
					    _user = request.user
 | 
				
			||||||
    is_data_shared = comp.intervention.is_shared_with(_user)
 | 
					    is_data_shared = comp.intervention.is_shared_with(_user)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Order states according to surface
 | 
					    # Order states according to surface
 | 
				
			||||||
    before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
 | 
					    before_states = comp.before_states.all().prefetch_related("biotope_type").order_by("-surface")
 | 
				
			||||||
    after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
 | 
					    after_states = comp.after_states.all().prefetch_related("biotope_type").order_by("-surface")
 | 
				
			||||||
@ -237,8 +235,8 @@ def detail_view(request: HttpRequest, id: str):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # Precalculate logical errors between before- and after-states
 | 
					    # Precalculate logical errors between before- and after-states
 | 
				
			||||||
    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
					    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
				
			||||||
    sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_before_states = comp.get_surface_before_states()
 | 
				
			||||||
    sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_after_states = comp.get_surface_after_states()
 | 
				
			||||||
    diff_states = abs(sum_before_states - sum_after_states)
 | 
					    diff_states = abs(sum_before_states - sum_after_states)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    request = comp.set_status_messages(request)
 | 
					    request = comp.set_status_messages(request)
 | 
				
			||||||
 | 
				
			|||||||
@ -208,8 +208,8 @@ def detail_view(request: HttpRequest, id: str):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # Precalculate logical errors between before- and after-states
 | 
					    # Precalculate logical errors between before- and after-states
 | 
				
			||||||
    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
					    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
				
			||||||
    sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_before_states = acc.get_surface_before_states()
 | 
				
			||||||
    sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_after_states = acc.get_surface_after_states()
 | 
				
			||||||
    diff_states = abs(sum_before_states - sum_after_states)
 | 
					    diff_states = abs(sum_before_states - sum_after_states)
 | 
				
			||||||
    # Calculate rest of available surface for deductions
 | 
					    # Calculate rest of available surface for deductions
 | 
				
			||||||
    available_total = acc.deductable_rest
 | 
					    available_total = acc.deductable_rest
 | 
				
			||||||
 | 
				
			|||||||
@ -149,8 +149,8 @@ def detail_view(request: HttpRequest, id: str):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # Precalculate logical errors between before- and after-states
 | 
					    # Precalculate logical errors between before- and after-states
 | 
				
			||||||
    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
					    # Sum() returns None in case of no states, so we catch that and replace it with 0 for easier handling
 | 
				
			||||||
    sum_before_states = before_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_before_states = ema.get_surface_before_states()
 | 
				
			||||||
    sum_after_states = after_states.aggregate(Sum("surface"))["surface__sum"] or 0
 | 
					    sum_after_states = ema.get_surface_after_states()
 | 
				
			||||||
    diff_states = abs(sum_before_states - sum_after_states)
 | 
					    diff_states = abs(sum_before_states - sum_after_states)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ema.set_status_messages(request)
 | 
					    ema.set_status_messages(request)
 | 
				
			||||||
 | 
				
			|||||||
@ -441,7 +441,7 @@ class BaseTestCase(TestCase):
 | 
				
			|||||||
        eco_account.actions.add(self.comp_action)
 | 
					        eco_account.actions.add(self.comp_action)
 | 
				
			||||||
        eco_account.geometry.geom = self.create_dummy_geometry()
 | 
					        eco_account.geometry.geom = self.create_dummy_geometry()
 | 
				
			||||||
        eco_account.geometry.save()
 | 
					        eco_account.geometry.save()
 | 
				
			||||||
        eco_account.deductable_surface = eco_account.get_state_after_surface_sum()
 | 
					        eco_account.deductable_surface = eco_account.get_surface_after_states()
 | 
				
			||||||
        eco_account.deadlines.add(self.finished_deadline)
 | 
					        eco_account.deadlines.add(self.finished_deadline)
 | 
				
			||||||
        eco_account.save()
 | 
					        eco_account.save()
 | 
				
			||||||
        return eco_account
 | 
					        return eco_account
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user