예제 #1
0
파일: test_util.py 프로젝트: uktrade/tamato
def test_diff_components_update(
    workbasket,
    duty_sentence_parser,
    percent_or_amount,
):
    original_component = factories.MeasureComponentFactory.create(
        duty_amount=9.000,
        duty_expression=percent_or_amount,
    )
    new_measure = original_component.component_measure.new_version(
        original_component.transaction.workbasket, )
    util.diff_components(
        new_measure,
        "8.000%",
        original_component.component_measure.valid_between.lower,
        workbasket,
        new_measure.transaction,
        MeasureComponent,
        "component_measure",
    )
    components = new_measure.components.approved_up_to_transaction(
        workbasket.current_transaction, )

    assert components.count() == 1

    new_component = components.last()

    assert new_component.update_type == UpdateType.UPDATE
    assert new_component.version_group == original_component.version_group
    assert new_component.component_measure == new_measure
    assert new_component.transaction == workbasket.current_transaction
    assert new_component.duty_amount == 8.000
예제 #2
0
파일: test_util.py 프로젝트: uktrade/tamato
def test_diff_components_delete(
    workbasket,
    duty_sentence_parser,
    percent_or_amount,
):
    component = factories.MeasureComponentFactory.create(
        duty_amount=9.000,
        duty_expression=percent_or_amount,
    )
    new_measure = component.component_measure.new_version(
        component.transaction.workbasket, )
    util.diff_components(
        component.component_measure,
        "",
        component.component_measure.valid_between.lower,
        workbasket,
        new_measure.transaction,
        MeasureComponent,
        "component_measure",
    )
    components = component.component_measure.components.approved_up_to_transaction(
        workbasket.current_transaction, )

    assert components.count() == 0

    deleted = MeasureComponent.objects.filter(
        component_measure=component.component_measure,
        update_type=UpdateType.DELETE,
    )

    assert deleted.exists()
    assert deleted.first().transaction == workbasket.current_transaction
예제 #3
0
    def create_condition_and_components(
        self,
        data,
        component_sequence_number,
        measure,
        parser,
        workbasket,
    ):
        """
        Creates condition from data dict, component_sequence_number, and
        measure.

        If applicable_duty field is passed in data, uses parser to create
        measure condition components from newly created condition
        """
        condition = MeasureCondition(
            sid=data.get("sid") or self.measure_condition_sid_counter(),
            component_sequence_number=component_sequence_number,
            dependent_measure=measure,
            update_type=data.get("update_type") or UpdateType.CREATE,
            transaction=measure.transaction,
            duty_amount=data.get("duty_amount"),
            condition_code=data["condition_code"],
            action=data.get("action"),
            required_certificate=data.get("required_certificate"),
            monetary_unit=data.get("monetary_unit"),
            condition_measurement=data.get("condition_measurement", ),
        )
        if data.get("version_group"):
            condition.version_group = data.get("version_group")

        condition.clean()
        condition.save()

        if data.get("applicable_duty"):
            diff_components(
                condition,
                data.get("applicable_duty"),
                measure.valid_between.lower,
                workbasket,
                MeasureConditionComponent,
                "condition",
                transaction=condition.transaction,
            )
예제 #4
0
파일: test_util.py 프로젝트: uktrade/tamato
def test_diff_components_update_multiple(
    workbasket,
    duty_sentence_parser,
    percent_or_amount,
    plus_percent_or_amount,
    monetary_units,
    measurement_units,
):
    component_1 = factories.MeasureComponentFactory.create(
        duty_amount=12.000,
        duty_expression=percent_or_amount,
    )
    component_2 = factories.MeasureComponentFactory.create(
        component_measure=component_1.component_measure,
        duty_amount=253.000,
        duty_expression=plus_percent_or_amount,
        monetary_unit=monetary_units["GBP"],
        component_measurement__measurement_unit=measurement_units[1],
    )
    new_measure = component_1.component_measure.new_version(
        component_1.transaction.workbasket, )
    util.diff_components(
        component_2.component_measure,
        "13.000% + 254.000 GBP / 100 kg",
        component_1.component_measure.valid_between.lower,
        workbasket,
        new_measure.transaction,
        MeasureComponent,
        "component_measure",
    )
    components = component_1.component_measure.components.approved_up_to_transaction(
        workbasket.current_transaction, )

    assert components.count() == 2

    first = components.filter(
        duty_expression__sid=component_1.duty_expression.sid, ).first()
    second = components.filter(
        duty_expression__sid=component_2.duty_expression.sid, ).first()

    assert components.count() == 2
    assert first.duty_amount == 13.000
    assert second.duty_amount == 254.000
    assert components.first().transaction == components.last().transaction
예제 #5
0
파일: test_util.py 프로젝트: uktrade/tamato
def test_diff_components_create(workbasket, duty_sentence_parser):
    measure = factories.MeasureFactory.create()
    util.diff_components(
        measure,
        "8.000%",
        measure.valid_between.lower,
        workbasket,
        measure.transaction,
        MeasureComponent,
        "component_measure",
    )
    components = measure.components.approved_up_to_transaction(
        workbasket.current_transaction, )

    assert components.count() == 1

    new_component = components.last()

    assert new_component.update_type == UpdateType.CREATE
    assert new_component.component_measure == measure
    assert new_component.transaction == workbasket.current_transaction
    assert new_component.duty_amount == 8.000
예제 #6
0
    def save(self, commit=True):
        """Get the measure instance after form submission, get from session
        storage any footnote pks created via the Footnote formset and any pks
        not removed from the measure after editing and create footnotes via
        FootnoteAssociationMeasure."""
        instance = super().save(commit=False)
        if commit:
            instance.save()

        sid = instance.sid

        measure_creation_pattern = MeasureCreationPattern(
            workbasket=WorkBasket.current(self.request),
            base_date=instance.valid_between.lower,
            defaults={
                "generating_regulation": self.cleaned_data["generating_regulation"],
            },
        )

        if self.cleaned_data.get("exclusions"):
            for exclusion in self.cleaned_data.get("exclusions"):
                pattern = (
                    measure_creation_pattern.create_measure_excluded_geographical_areas(
                        instance,
                        exclusion,
                    )
                )
                [p for p in pattern]

        if (
            self.request.session[f"instance_duty_sentence_{self.instance.sid}"]
            != self.cleaned_data["duty_sentence"]
        ):
            diff_components(
                instance,
                self.cleaned_data["duty_sentence"],
                self.cleaned_data["valid_between"].lower,
                WorkBasket.current(self.request),
                # Creating components in the same transaction as the new version
                # of the measure minimises number of transaction and groups the
                # creation of measure and related objects in the same
                # transaction.
                instance.transaction,
                models.MeasureComponent,
                "component_measure",
            )

        footnote_pks = [
            dct["footnote"]
            for dct in self.request.session.get(f"formset_initial_{sid}", [])
        ]
        footnote_pks.extend(self.request.session.get(f"instance_footnotes_{sid}", []))

        self.request.session.pop(f"formset_initial_{sid}", None)
        self.request.session.pop(f"instance_footnotes_{sid}", None)

        for pk in footnote_pks:
            footnote = (
                Footnote.objects.filter(pk=pk)
                .approved_up_to_transaction(instance.transaction)
                .first()
            )

            existing_association = (
                models.FootnoteAssociationMeasure.objects.approved_up_to_transaction(
                    instance.transaction,
                )
                .filter(
                    footnoted_measure__sid=instance.sid,
                    associated_footnote__footnote_id=footnote.footnote_id,
                    associated_footnote__footnote_type__footnote_type_id=footnote.footnote_type.footnote_type_id,
                )
                .first()
            )
            if existing_association:
                existing_association.new_version(
                    workbasket=WorkBasket.current(self.request),
                    transaction=instance.transaction,
                    footnoted_measure=instance,
                )
            else:
                models.FootnoteAssociationMeasure.objects.create(
                    footnoted_measure=instance,
                    associated_footnote=footnote,
                    update_type=UpdateType.CREATE,
                    transaction=instance.transaction,
                )

        return instance