Ejemplo n.º 1
0
    def create(self, validated_data):
        user = validated_data["user"]
        if "blank_offer" in validated_data:
            company = Company.objects.get(id=validated_data["offer_company"])
            address = Address.objects.get(id=validated_data["delivery_address"])
            order = Order.objects.create(
                order_company=company,
                delivery_address=address,
                order_created_by=user,
                order_status_change_date=timezone.now(),
            )
            yr = str(date.today().year)[2:]
            order.order_number = "".join(["0", yr, str(order.id)])
            order.order_version = 01
            if "reference_number" in validated_data:
                order.reference_number = validated_data["reference_number"]
        else:
            order = Order.objects.get(id=validated_data["order"])
            order.order_status_change_date = timezone.now()
        if order.offer_version >= 01:
            order.order_version = order.order_version + 1
        order.offer_version = order.offer_version + 1
        order.order_offer = True
        order.order_total = validated_data["offer_total"]
        order.modified_by = user
        order.order_status_change_by = user
        order.order_status = "OFR"
        order.optiz_status = "OSB"
        order.optiz_status_change_by = user
        order.optiz_status_change_date = timezone.now()
        order.save()
        offer = Offer.objects.create(
            order=order,
            offer_version=order.offer_version,
            offer_created_by=user,
            offer_total=validated_data["offer_total"],
            offer_terms=validated_data["offer_terms"],
            offer_domain=validated_data["offer_domain"],
        )
        offer.save()
        for item in validated_data["offer_item"]:
            offer_item = OfferItem(offer=offer, **item)
            offer_item.save()
        log(
            user=user,
            company=order.order_company,
            not_action="offer created",
            obj=order,
            notification=True,
            extra={
                "order_id": order.id,
                "order_number": order.order_number,
                "order_version": order.offer_version,
                "offer_total": offer.offer_total,
                "order_status": order.order_status,
                "order_status_full": order.get_order_status_display(),
            },
        )
        mail_item.delay(user, order, subj="offer", tmp="registration/order_confirm_email.html")

        return offer
Ejemplo n.º 2
0
    def update(self, instance, validated_data):
        user = validated_data.pop("user")
        if "order_draft" in validated_data:
            if "False" in validated_data["order_draft"]:
                instance.order_draft = False
                if user.access_level >= "6":
                    instance.company_approval_status = "PEN"
                    instance.order_status = "PEN"
                    instance.order_status_change_by = user
                    instance.order_status_change_date = timezone.now()
                    instance.optiz_status = "PEN"
                    instance.company_approval_by = user
                    instance.company_approval_date = timezone.now()
                    instance.order_version = instance.order_version + 01
                    log(
                        user=user,
                        company=instance.order_company,
                        not_action="request submitted",
                        obj=instance,
                        notification=True,
                        extra={
                            "order_id": instance.id,
                            "order_number": instance.order_number,
                            "order_version": instance.order_version,
                            "order_status": instance.order_status,
                            "order_status_full": instance.get_order_status_display(),
                        },
                    )
                    mail_item.delay(user, instance, subj="demande", tmp="registration/order_confirm_email.html")

                else:
                    if instance.company_approval_status == "APN":
                        req_not_action = "request updated"
                    else:
                        instance.company_approval_status == "APN"
                        instance.order_status = "APN"
                        req_not_action = "request created"
                    log(
                        user=user,
                        company=instance.order_company,
                        not_action=req_not_action,
                        obj=instance,
                        notification=True,
                        extra={
                            "order_id": instance.id,
                            "order_number": instance.order_number,
                            "order_version": instance.order_version,
                            "order_status": instance.order_status,
                            "order_status_full": instance.get_order_status_display(),
                        },
                    )
                    mail_item.delay(user, instance, subj="demande", tmp="registration/order_confirm_email.html")

        if "order_status" in validated_data:
            if not validated_data["order_status"] in instance.order_status:
                instance.order_status = validated_data["order_status"]
                instance.order_status_change_by = user
                instance.order_status_change_date = timezone.now()
                if not instance.order_draft:
                    order_status_full = instance.get_order_status_display()
                    log(
                        user=user,
                        company=instance.order_company,
                        not_action="order status updated",
                        obj=instance,
                        notification=True,
                        extra={
                            "order_id": instance.id,
                            "order_number": instance.order_number,
                            "order_version": instance.order_version,
                            "order_status": instance.order_status,
                            "order_status_full": order_status_full,
                        },
                    )
                    mail_item.delay(user, instance, subj="demande", tmp="registration/order_confirm_email.html")

                if "optiz_status" in validated_data:
                    instance.optiz_status = validated_data["optiz_status"]
                    instance.optiz_status_change_by = user
                    instance.optiz_status_change_date = timezone.now()
                else:
                    instance.company_approval_status = validated_data["company_approval_status"]
                    instance.company_approval_by = user
                    instance.company_approval_date = timezone.now()

                if validated_data["order_status"] == "APV" or validated_data["order_status"] == "REF":
                    offer = Offer.objects.get(id=validated_data["offer"])
                    offer.offer_approval_status = validated_data["order_status"]
                    offer.offer_approval_by = user
                    offer.offer_approval = timezone.now()
                    offer.save()

        if "delivery_address" in validated_data:
            addr = Address.objects.get(id=validated_data["delivery_address"])
            instance.delivery_address = addr
            log(
                user=user,
                company=instance.order_company,
                not_action="order delivery address",
                obj=instance,
                notification=False,
                extra={
                    "order_id": instance.id,
                    "order_number": instance.order_number,
                    "order_version": instance.order_version,
                    "order_delivery_address": instance.delivery_address.addr_location,
                },
            )
        if "comment_body" in validated_data:
            comment = Comment.objects.create(order=instance, created_by=user, body=validated_data["comment_body"])
            comment.save()
            log(
                user=user,
                company=instance.order_company,
                not_action="comment added",
                obj=instance,
                notification=True,
                extra={
                    "order_id": instance.id,
                    "order_number": instance.order_number,
                    "order_version": instance.order_version,
                    "order_status": instance.order_status,
                    "order_status_full": instance.get_order_status_display(),
                    "comment": comment.body,
                },
            )
        instance.reference_number = validated_data.get("reference_number", instance.reference_number)
        instance.modified_by = user
        instance.save()

        return instance