Exemple #1
0
    def validate(self, data):
        """Validate this activity - check end date is after start date, etc."""
        # Check that start_date is before end_date
        if (
            "end_date" in data
            and data["end_date"]
            and data["start_date"] > data["end_date"]
        ):
            raise serializers.ValidationError(
                _("Startdato skal være før eller identisk med slutdato")
            )

        # One time payments should have a payment date in the payment plan.
        is_one_time_payment = (
            data["payment_plan"]["payment_type"]
            == PaymentSchedule.ONE_TIME_PAYMENT
        )

        if "start_date" not in data and not is_one_time_payment:
            raise serializers.ValidationError(
                _("der skal angives en startdato for ydelsen")
            )

        if is_one_time_payment and (
            "payment_date" not in data["payment_plan"]
            or data["payment_plan"]["payment_date"] is None
        ):
            raise serializers.ValidationError(
                _("der skal angives en betalingsdato for engangsbetaling")
            )

        # Monthly payments that are not expected adjustments should have a
        # valid start_date, end_date and day_of_month that results in payments.
        modifies = "modifies" in data and data["modifies"]
        is_monthly_payment = (
            "payment_frequency" in data["payment_plan"]
            and data["payment_plan"]["payment_frequency"]
            == PaymentSchedule.MONTHLY
        )
        if (
            is_monthly_payment
            and ("end_date" in data and data["end_date"])
            and not modifies
        ):
            start_date = data["start_date"]
            end_date = data["end_date"]
            payment_day_of_month = data["payment_plan"]["payment_day_of_month"]

            next_payment_date = start_date.replace(day=payment_day_of_month)
            if next_payment_date < start_date:
                next_payment_date += relativedelta(months=+1)

            if not (start_date <= next_payment_date <= end_date):
                raise serializers.ValidationError(
                    _("Betalingsparametre resulterer ikke i nogen betalinger")
                )

        # Cash payments that are not fictive should have a "valid" start_date
        # based on payment date exclusions.
        data_copy = data.copy()

        if (
            "price_per_unit" in data_copy["payment_plan"]
            and data_copy["payment_plan"]["price_per_unit"]
        ):
            data_copy["payment_plan"]["price_per_unit"] = PriceSerializer(
                data=data_copy["payment_plan"]["price_per_unit"]
            ).instance
        data_copy["payment_plan"] = PaymentSchedule(
            **data_copy.pop("payment_plan")
        )
        # Pop the service provider and attach a serialized version.
        if "service_provider" in data_copy and data_copy["service_provider"]:
            data_copy["service_provider"] = ServiceProvider(
                **data_copy.pop("service_provider")
            )

        instance = Activity(**data_copy)

        is_valid_start_date = instance.is_valid_activity_start_date()
        if not is_valid_start_date:
            raise serializers.ValidationError(
                _(
                    "Startdato skal være i fremtiden og "
                    "der skal være mindst to udbetalingsdage"
                    " fra nu og til startdatoen"
                )
            )

        if modifies:
            # run the validate_expected flow.
            try:
                instance.validate_expected()
            except forms.ValidationError as e:
                raise serializers.ValidationError(e.message)
        return data