Example #1
0
    def get_or_create_offer_item(self, permanence, reset_add_2_stock=False):

        from repanier.models.offeritem import OfferItem, OfferItemWoReceiver
        from repanier.models.box import BoxContent

        offer_item_qs = OfferItem.objects.filter(
            permanence_id=permanence.id,
            product_id=self.id,
            permanences_dates=EMPTY_STRING).order_by('?')
        if not offer_item_qs.exists():
            OfferItemWoReceiver.objects.create(permanence_id=permanence.id,
                                               product_id=self.id,
                                               producer_id=self.producer_id,
                                               permanences_dates=EMPTY_STRING)
            clean_offer_item(permanence,
                             offer_item_qs,
                             reset_add_2_stock=reset_add_2_stock)
        else:
            offer_item = offer_item_qs.first()
            offer_item.contract = None
            offer_item.permanences_dates_order = 0
            if reset_add_2_stock:
                offer_item.may_order = True
            offer_item.save(update_fields=[
                "contract", "may_order", "permanences_dates_order"
            ])
        if self.is_box:
            # Add box products
            for box_content in BoxContent.objects.filter(
                    box=self.id).order_by('?'):
                box_offer_item_qs = OfferItem.objects.filter(
                    permanence_id=permanence.id,
                    product_id=box_content.product_id,
                    permanences_dates=EMPTY_STRING).order_by('?')
                if not box_offer_item_qs.exists():
                    OfferItemWoReceiver.objects.create(
                        permanence_id=permanence.id,
                        product_id=box_content.product_id,
                        producer_id=box_content.product.producer_id,
                        permanences_dates=EMPTY_STRING,
                        is_box_content=True)
                    clean_offer_item(permanence,
                                     box_offer_item_qs,
                                     reset_add_2_stock=reset_add_2_stock)
                else:
                    box_offer_item = box_offer_item_qs.first()
                    box_offer_item.is_box_content = True
                    box_offer_item.contract = None
                    box_offer_item.permanences_dates_order = 0
                    if reset_add_2_stock:
                        box_offer_item.may_order = True
                    box_offer_item.save(update_fields=[
                        "is_box_content", "contract", "may_order",
                        "permanences_dates_order"
                    ])

        offer_item = offer_item_qs.first()
        return offer_item
Example #2
0
    def get_or_create_offer_item(self, permanence, reset_add_2_stock=False):

        from repanier.models.offeritem import OfferItem, OfferItemWoReceiver
        from repanier.models.box import BoxContent

        offer_item_qs = OfferItem.objects.filter(
            permanence_id=permanence.id,
            product_id=self.id,
            permanences_dates=EMPTY_STRING
        ).order_by('?')
        if not offer_item_qs.exists():
            OfferItemWoReceiver.objects.create(
                permanence_id=permanence.id,
                product_id=self.id,
                producer_id=self.producer_id,
                permanences_dates=EMPTY_STRING
            )
            clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=reset_add_2_stock)
        else:
            offer_item = offer_item_qs.first()
            offer_item.contract = None
            offer_item.permanences_dates_order = 0
            if reset_add_2_stock:
                offer_item.may_order = True
            offer_item.save(update_fields=["contract", "may_order", "permanences_dates_order"])
        if self.is_box:
            # Add box products
            for box_content in BoxContent.objects.filter(
                    box=self.id
            ).order_by('?'):
                box_offer_item_qs = OfferItem.objects.filter(
                    permanence_id=permanence.id,
                    product_id=box_content.product_id,
                    permanences_dates=EMPTY_STRING
                ).order_by('?')
                if not box_offer_item_qs.exists():
                    OfferItemWoReceiver.objects.create(
                        permanence_id=permanence.id,
                        product_id=box_content.product_id,
                        producer_id=box_content.product.producer_id,
                        permanences_dates=EMPTY_STRING,
                        is_box_content=True
                    )
                    clean_offer_item(permanence, box_offer_item_qs, reset_add_2_stock=reset_add_2_stock)
                else:
                    box_offer_item = box_offer_item_qs.first()
                    box_offer_item.is_box_content = True
                    box_offer_item.contract = None
                    box_offer_item.permanences_dates_order = 0
                    if reset_add_2_stock:
                        box_offer_item.may_order = True
                    box_offer_item.save(
                        update_fields=["is_box_content", "contract", "may_order", "permanences_dates_order"])

        offer_item = offer_item_qs.first()
        return offer_item
Example #3
0
    def get_or_create_offer_item(self, permanence):
        from repanier.models.offeritem import OfferItem

        offer_item_qs = OfferItem.objects.filter(
            permanence_id=permanence.id,
            product_id=self.id,
        ).order_by('?')
        if not offer_item_qs.exists():
            OfferItem.objects.create(
                permanence=permanence,
                product_id=self.id,
                producer=self.producer,
            )
            clean_offer_item(permanence, offer_item_qs)
        offer_item = offer_item_qs.first()
        return offer_item
Example #4
0
    def get_or_create_offer_item(self, permanence):

        from repanier.models.offeritem import OfferItem, OfferItemWoReceiver
        from repanier.models.box import BoxContent

        offer_item_qs = OfferItem.objects.filter(
            permanence_id=permanence.id, product_id=self.id).order_by("?")
        if not offer_item_qs.exists():
            OfferItemWoReceiver.objects.create(
                permanence_id=permanence.id,
                product_id=self.id,
                producer_id=self.producer_id,
            )
        clean_offer_item(permanence, offer_item_qs)
        if self.is_box:
            # Add box products
            for box_content in BoxContent.objects.filter(
                    box=self.id).order_by("?"):
                box_offer_item_qs = OfferItem.objects.filter(
                    permanence_id=permanence.id,
                    product_id=box_content.product_id).order_by("?")
                if not box_offer_item_qs.exists():
                    OfferItemWoReceiver.objects.create(
                        permanence_id=permanence.id,
                        product_id=box_content.product_id,
                        producer_id=box_content.product.producer_id,
                        is_box_content=True,
                    )
                else:
                    box_offer_item = box_offer_item_qs.first()
                    box_offer_item.is_box_content = True
                    box_offer_item.save(update_fields=["is_box_content"])
                clean_offer_item(permanence, box_offer_item_qs)

        offer_item = offer_item_qs.first()
        return offer_item
def pre_order_update_product_ajax(request,
                                  offer_uuid=None,
                                  offer_item_id=None):
    if offer_item_id is None:
        raise Http404
    producer = Producer.objects.filter(
        offer_uuid=offer_uuid, is_active=True,
        producer_pre_opening=True).only('id').order_by('?').first()
    if producer is None:
        return render(
            request,
            "repanier/pre_order_closed_form.html",
        )
    offer_item = get_object_or_404(OfferItem, id=offer_item_id)
    if offer_item.producer_id != producer.id:
        raise Http404

    permanence = offer_item.permanence
    if permanence.status == PERMANENCE_PRE_OPEN:
        if request.method == 'POST':  # If the form has been submitted...
            form = ProducerProductForm(
                request.POST)  # A form bound to the POST data
            if form.is_valid():
                product = offer_item.product
                long_name = form.cleaned_data.get('long_name')
                product.long_name = long_name
                product.order_unit = form.cleaned_data.get('order_unit')
                product.producer_unit_price = form.cleaned_data.get(
                    'producer_unit_price')
                product.stock = form.cleaned_data.get('stock')
                if product.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
                    product.customer_increment_order_quantity = form.cleaned_data.get(
                        'customer_increment_order_quantity').quantize(
                            ONE_DECIMAL)
                    product.order_average_weight = form.cleaned_data.get(
                        'order_average_weight')
                    product.customer_alert_order_quantity = product.stock
                else:
                    product.customer_increment_order_quantity = 1
                    product.order_average_weight = form.cleaned_data.get(
                        'customer_increment_order_quantity').quantize(
                            ONE_DECIMAL)
                    if product.order_average_weight <= DECIMAL_ZERO:
                        product.order_average_weight = DECIMAL_ONE
                    product.producer_unit_price = (
                        product.producer_unit_price.amount *
                        product.order_average_weight).quantize(TWO_DECIMALS)
                    product.stock = product.customer_alert_order_quantity = product.stock / product.order_average_weight
                product.unit_deposit = form.cleaned_data.get('unit_deposit')
                product.vat_level = form.cleaned_data.get('vat_level')
                product.offer_description = form.cleaned_data.get(
                    'offer_description')
                product.customer_minimum_order_quantity = product.customer_increment_order_quantity
                product.picture2 = form.cleaned_data.get('picture')
                product.save()
                product.production_mode.clear()
                production_mode = form.cleaned_data.get(
                    'production_mode', None)
                if production_mode is not None:
                    product.production_mode.add(production_mode)
                offer_item_qs = OfferItem.objects.filter(
                    id=offer_item.id).order_by('?')
                clean_offer_item(permanence, offer_item_qs)
                # Refresh offer_item
                offer_item = get_object_or_404(OfferItem, id=offer_item_id)
                update = True
            else:
                update = False
        else:
            form = ProducerProductForm()  # An unbound form
            field = form.fields["long_name"]
            field.initial = offer_item.safe_translation_getter(
                'long_name', any_language=True)
            if settings.REPANIER_SETTINGS_PRODUCT_LABEL:
                field = form.fields["production_mode"]
                field.initial = offer_item.product.production_mode.first()
            field = form.fields["order_unit"]
            field.initial = offer_item.order_unit
            field = form.fields["order_average_weight"]
            field.initial = offer_item.order_average_weight.quantize(
                ONE_DECIMAL)
            if offer_item.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
                customer_increment_order_quantity = offer_item.customer_increment_order_quantity.quantize(
                    ONE_DECIMAL)
                field = form.fields["customer_increment_order_quantity"]
                field.initial = customer_increment_order_quantity
                field = form.fields["producer_unit_price"]
                field.initial = offer_item.producer_unit_price.amount
                field = form.fields["stock"]
                field.initial = offer_item.stock.quantize(ONE_DECIMAL)
            else:
                customer_increment_order_quantity = offer_item.order_average_weight.quantize(
                    ONE_DECIMAL)
                field = form.fields["customer_increment_order_quantity"]
                field.initial = customer_increment_order_quantity
                field = form.fields["producer_unit_price"]
                if customer_increment_order_quantity > DECIMAL_ZERO:
                    field.initial = (offer_item.producer_unit_price.amount /
                                     customer_increment_order_quantity
                                     ).quantize(TWO_DECIMALS)
                else:
                    field.initial = offer_item.producer_unit_price.amount
                field = form.fields["stock"]
                field.initial = (customer_increment_order_quantity *
                                 offer_item.stock).quantize(ONE_DECIMAL)
            field = form.fields["unit_deposit"]
            field.initial = offer_item.unit_deposit.amount
            field = form.fields["vat_level"]
            field.initial = offer_item.vat_level
            field = form.fields["offer_description"]
            field.initial = offer_item.product.offer_description
            field = form.fields["picture"]
            field.initial = offer_item.product.picture2
            field.widget.upload_to = "{}{}{}".format("product", os_sep,
                                                     offer_item.producer_id)
            update = None

        return render(
            request, "repanier/pre_order_update_product_form.html", {
                'form': form,
                'offer_uuid': offer_uuid,
                'offer_item': offer_item,
                'update': update
            })
    raise Http404
Example #6
0
def pre_order_update_product_ajax(request, offer_uuid=None, offer_item_id=None):
    if offer_item_id is None:
        raise Http404
    producer = Producer.objects.filter(offer_uuid=offer_uuid, is_active=True, producer_pre_opening=True).only(
        'id').order_by('?').first()
    if producer is None:
        return render(
            request,
            "repanier/pre_order_closed_form.html",
        )
    offer_item = get_object_or_404(OfferItem, id=offer_item_id)
    if offer_item.producer_id != producer.id:
        raise Http404
    if not offer_item.is_active:
        raise Http404

    permanence = offer_item.permanence
    if permanence.status == PERMANENCE_PRE_OPEN:
        if request.method == 'POST':  # If the form has been submitted...
            form = ProducerProductForm(request.POST)  # A form bound to the POST data
            if form.is_valid():
                product = offer_item.product
                long_name = form.cleaned_data.get('long_name')
                product.long_name = long_name
                product.order_unit = form.cleaned_data.get('order_unit')
                product.producer_unit_price = form.cleaned_data.get('producer_unit_price')
                product.stock = form.cleaned_data.get('stock')
                if product.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
                    product.customer_increment_order_quantity = form.cleaned_data.get(
                        'customer_increment_order_quantity').quantize(ONE_DECIMAL)
                    product.order_average_weight = form.cleaned_data.get('order_average_weight')
                    product.customer_alert_order_quantity = product.stock
                else:
                    product.customer_increment_order_quantity = 1
                    product.order_average_weight = form.cleaned_data.get('customer_increment_order_quantity').quantize(
                        ONE_DECIMAL)
                    if product.order_average_weight <= DECIMAL_ZERO:
                        product.order_average_weight = DECIMAL_ONE
                    product.producer_unit_price = (
                        product.producer_unit_price.amount * product.order_average_weight
                    ).quantize(TWO_DECIMALS)
                    product.stock = product.customer_alert_order_quantity = product.stock / product.order_average_weight
                product.unit_deposit = form.cleaned_data.get('unit_deposit')
                product.vat_level = form.cleaned_data.get('vat_level')
                product.offer_description = form.cleaned_data.get('offer_description')
                product.customer_minimum_order_quantity = product.customer_increment_order_quantity
                product.picture2 = form.cleaned_data.get('picture')
                product.save()
                product.production_mode.clear()
                production_mode = form.cleaned_data.get('production_mode')
                if production_mode is not None:
                    product.production_mode.add(form.cleaned_data.get('production_mode'))
                offer_item_qs = OfferItem.objects.filter(
                    id=offer_item.id
                ).order_by('?')
                clean_offer_item(permanence, offer_item_qs)
                # Refresh offer_item
                offer_item = get_object_or_404(OfferItem, id=offer_item_id)
                update = '1'
            else:
                update = None
        else:
            form = ProducerProductForm()  # An unbound form
            field = form.fields["long_name"]
            field.initial = offer_item.long_name
            field = form.fields["production_mode"]
            field.initial = offer_item.product.production_mode.first()
            field = form.fields["order_unit"]
            field.initial = offer_item.order_unit
            field = form.fields["order_average_weight"]
            field.initial = offer_item.order_average_weight.quantize(ONE_DECIMAL)
            if offer_item.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
                customer_increment_order_quantity = offer_item.customer_increment_order_quantity.quantize(ONE_DECIMAL)
                field = form.fields["customer_increment_order_quantity"]
                field.initial = customer_increment_order_quantity
                field = form.fields["producer_unit_price"]
                field.initial = offer_item.producer_unit_price.amount
                field = form.fields["stock"]
                field.initial = offer_item.stock.quantize(ONE_DECIMAL)
            else:
                customer_increment_order_quantity = offer_item.order_average_weight.quantize(ONE_DECIMAL)
                field = form.fields["customer_increment_order_quantity"]
                field.initial = customer_increment_order_quantity
                field = form.fields["producer_unit_price"]
                if customer_increment_order_quantity > DECIMAL_ZERO:
                    field.initial = (
                    offer_item.producer_unit_price.amount / customer_increment_order_quantity).quantize(TWO_DECIMALS)
                else:
                    field.initial = offer_item.producer_unit_price.amount
                field = form.fields["stock"]
                field.initial = (customer_increment_order_quantity * offer_item.stock).quantize(ONE_DECIMAL)
            field = form.fields["unit_deposit"]
            field.initial = offer_item.unit_deposit.amount
            field = form.fields["vat_level"]
            field.initial = offer_item.vat_level
            field = form.fields["offer_description"]
            field.initial = offer_item.product.offer_description
            field = form.fields["picture"]
            field.initial = offer_item.product.picture2
            field.widget.upload_to = "%s%s%d" % ("product", os_sep, offer_item.producer_id)
            update = None

        return render(
            request,
            "repanier/pre_order_update_product_form.html",
            {'form': form, 'offer_uuid': offer_uuid, 'offer_item': offer_item, 'update': update}
        )
    raise Http404
Example #7
0
def common_to_pre_open_and_open(permanence_id):
    getcontext().rounding = ROUND_HALF_UP
    # 1- Deactivate all offer item of this permanence
    OfferItem.objects.filter(
        permanence_id=permanence_id
    ).order_by('?').update(
        is_active=False, may_order=False, is_box=False, is_box_content=False
    )
    # 2 - Delete unused purchases
    Purchase.objects.filter(permanence_id=permanence_id, quantity_ordered=0,
                            quantity_invoiced=0).order_by('?').delete()
    # 3 - Activate offer items which can be purchased depending on selection in the admin
    producers_in_this_permanence = Producer.objects.filter(
        permanence=permanence_id, is_active=True).order_by('?').only("id")
    product_queryset = Product.objects.filter(
        producer__in=producers_in_this_permanence, is_into_offer=True
    ).order_by('?').only("id", "producer_id")

    OfferItem.objects.filter(product__in=product_queryset, permanence_id=permanence_id) \
        .order_by('?').update(is_active=True, may_order=True)
    for product in product_queryset:
        if not OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id) \
                .order_by('?').exists():
            OfferItem.objects.create(
                permanence_id=permanence_id,
                product_id=product.id,
                producer_id=product.producer_id,
                is_box=False,
                is_box_content=False,
                is_active=True,
                may_order=True
            )
    # 4 - Add composition products
    product_queryset = Product.objects.filter(
        is_box=True, is_into_offer=True
    ).order_by('?').only("id", "producer_id")
    OfferItem.objects.filter(product__in=product_queryset, permanence_id=permanence_id) \
        .order_by('?').update(is_active=True, may_order=True)
    for product in product_queryset:
        offer_item = OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id).order_by('?').first()
        if offer_item is None:
            OfferItem.objects.create(
                permanence_id=permanence_id,
                product_id=product.id,
                producer_id=product.producer_id,
                is_box=True,
                is_box_content=False,
                is_active=True,
                may_order=True
            )
        else:
            offer_item.is_box = True
            offer_item.is_active = True
            offer_item.save(update_fields=["is_active", "is_box"])
        for box_content in BoxContent.objects.filter(box=product.id).select_related("product__producer").order_by(
                '?'):
            box_offer_item = OfferItem.objects.filter(product_id=box_content.product_id,
                                                      permanence_id=permanence_id).order_by('?').first()
            if box_offer_item is None:
                OfferItem.objects.create(
                    permanence_id=permanence_id,
                    product_id=box_content.product_id,
                    producer_id=box_content.product.producer_id,
                    is_box=False,
                    is_box_content=True,
                    is_active=True,
                    may_order=False
                )
            else:
                box_offer_item.is_box_content = True
                box_offer_item.is_active = True
                box_offer_item.save(update_fields=["is_active", "is_box_content"])
    # 6 - Add subscriptions even if the producer is not selected
    product_queryset = Product.objects.filter(
        order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION, is_into_offer=True,
        producer__represent_this_buyinggroup=True
    ).order_by('?').only("id", "producer_id")
    for product in product_queryset:
        if not OfferItem.objects.filter(product_id=product.id, permanence_id=permanence_id) \
                .order_by('?').exists():
            OfferItem.objects.create(
                permanence_id=permanence_id,
                product_id=product.id,
                producer_id=product.producer_id,
                is_box=False,
                is_box_content=False,
            )
    # 7 - Activate purchased products even if not in selected in the admin
    OfferItem.objects.filter(
        purchase__permanence_id=permanence_id, is_active=False,
        order_unit__lt=PRODUCT_ORDER_UNIT_DEPOSIT
    ).order_by('?').update(is_active=True)
    # 8 - Create cache
    permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first()
    offer_item_qs = OfferItem.objects.filter(permanence_id=permanence_id).order_by('?')
    clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=True)
    # 9 - calculate the sort order of the order display screen
    reorder_offer_items(permanence_id)
    # 10 - Deactivate technical offer items
    OfferItem.objects.filter(order_unit__gte=PRODUCT_ORDER_UNIT_DEPOSIT, permanence_id=permanence_id) \
        .order_by('?').update(is_active=False, may_order=False)
    # 11 - Calculate the Purchase 'sum' for each customer
    recalculate_order_amount(
        permanence_id=permanence_id,
        all_producers=True,
        send_to_producer=False
    )
    return permanence
Example #8
0
def common_to_pre_open_and_open(permanence_id):
    getcontext().rounding = ROUND_HALF_UP
    # Create offer items which can be purchased depending on selection in the admin
    producers_in_this_permanence = Producer.objects.filter(
        permanence=permanence_id, is_active=True).order_by('?').only("id")
    product_queryset = Product.objects.filter(
        producer__in=producers_in_this_permanence,
        is_box=False,
        is_into_offer=True).order_by('?').only("id", "producer_id")
    for product in product_queryset:
        if not OfferItem.objects.filter(
                product_id=product.id,
                permanence_id=permanence_id).order_by('?').exists():
            OfferItem.objects.create(
                permanence_id=permanence_id,
                product_id=product.id,
                producer_id=product.producer_id,
            )
    # Deactivate all offer item of this permanence
    OfferItem.objects.filter(permanence_id=permanence_id).order_by('?').update(
        is_active=False, may_order=False, is_box=False, is_box_content=False)
    # Activate all offer item of this permanence
    OfferItem.objects.filter(
        product__in=product_queryset,
        permanence_id=permanence_id).exclude(order_unit__in=[
            PRODUCT_ORDER_UNIT_DEPOSIT, PRODUCT_ORDER_UNIT_TRANSPORTATION
        ]).order_by('?').update(is_active=True, may_order=True)
    OfferItem.objects.filter(product__in=product_queryset,
                             permanence_id=permanence_id,
                             order_unit__in=[
                                 PRODUCT_ORDER_UNIT_DEPOSIT,
                                 PRODUCT_ORDER_UNIT_TRANSPORTATION
                             ]).order_by('?').update(is_active=True,
                                                     may_order=False)
    # Create box offer items which can be purchased depending on selection in the admin
    product_queryset = Product.objects.filter(
        is_box=True,
        is_into_offer=True).order_by('?').only("id", "producer_id")
    for product in product_queryset:
        offer_item = OfferItem.objects.filter(
            product_id=product.id,
            permanence_id=permanence_id).order_by('?').first()
        if offer_item is None:
            OfferItem.objects.create(permanence_id=permanence_id,
                                     product_id=product.id,
                                     producer_id=product.producer_id,
                                     is_box=True,
                                     is_box_content=False,
                                     is_active=True,
                                     may_order=True)
        else:
            offer_item.is_box = True
            offer_item.is_box_content = False
            offer_item.is_active = True
            offer_item.may_order = True
            offer_item.save(update_fields=[
                "is_active", "may_order", "is_box", "is_box_content"
            ])
        for box_content in BoxContent.objects.filter(
                box=product.id).select_related("product__producer").order_by(
                    '?'):
            box_offer_item = OfferItem.objects.filter(
                product_id=box_content.product_id,
                permanence_id=permanence_id).order_by('?').first()
            if box_offer_item is None:
                OfferItem.objects.create(
                    permanence_id=permanence_id,
                    product_id=box_content.product_id,
                    producer_id=box_content.product.producer_id,
                    is_box=False,
                    is_box_content=True,
                    is_active=True,
                    may_order=False)
            else:
                box_offer_item.is_box = False
                box_offer_item.is_box_content = True
                box_offer_item.is_active = True
                box_offer_item.may_order = False
                box_offer_item.save(update_fields=[
                    "is_active", "may_order", "is_box", "is_box_content"
                ])
    # Activate purchased products even if not in selected in the admin
    OfferItem.objects.filter(
        purchase__permanence_id=permanence_id,
        is_active=False,
        order_unit__lt=PRODUCT_ORDER_UNIT_DEPOSIT).order_by('?').update(
            is_active=True)
    # Create cache
    permanence = Permanence.objects.filter(
        id=permanence_id).order_by('?').first()
    offer_item_qs = OfferItem.objects.filter(
        permanence_id=permanence_id).order_by('?')
    clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=True)
    # Calculate the sort order of the order display screen
    reorder_offer_items(permanence_id)
    # Calculate the Purchase 'sum' for each customer
    permanence.recalculate_order_amount()
    return permanence
Example #9
0
def pre_order_create_product_ajax(request, permanence_id=None, offer_uuid=None):
    if permanence_id is None:
        raise Http404
    producer = Producer.objects.filter(offer_uuid=offer_uuid, is_active=True, producer_pre_opening=True).only(
        'id').order_by('?').first()
    if producer is None:
        return render(
            request,
            "repanier/pre_order_closed_form.html",
        )

    permanence = get_object_or_404(Permanence, id=permanence_id)
    offer_item = None
    if permanence.status == PERMANENCE_PRE_OPEN:
        if request.method == 'POST':  # If the form has been submitted...
            form = ProducerProductForm(request.POST)  # A form bound to the POST data
            if form.is_valid():
                long_name = form.cleaned_data.get('long_name')
                if long_name != _("long_name"):
                    order_unit = form.cleaned_data.get('order_unit')
                    producer_unit_price = form.cleaned_data.get('producer_unit_price')
                    stock = form.cleaned_data.get('stock')
                    if order_unit == PRODUCT_ORDER_UNIT_PC_KG:
                        customer_increment_order_quantity = form.cleaned_data.get(
                            'customer_increment_order_quantity').quantize(ONE_DECIMAL)
                        order_average_weight = form.cleaned_data.get('order_average_weight')
                        customer_alert_order_quantity = stock
                    else:
                        customer_increment_order_quantity = 1
                        order_average_weight = form.cleaned_data.get('customer_increment_order_quantity').quantize(
                            ONE_DECIMAL)
                        if order_average_weight <= DECIMAL_ZERO:
                            order_average_weight = DECIMAL_ONE
                        producer_unit_price = (producer_unit_price * order_average_weight).quantize(TWO_DECIMALS)
                        stock = customer_alert_order_quantity = stock / order_average_weight
                    unit_deposit = form.cleaned_data.get('unit_deposit')
                    vat_level = form.cleaned_data.get('vat_level')
                    offer_description = form.cleaned_data.get('offer_description')
                    customer_minimum_order_quantity = customer_increment_order_quantity
                    picture2 = form.cleaned_data.get('picture')
                    product = Product.objects.create(
                        producer_id=producer.id,
                        long_name=long_name,
                        order_unit=order_unit,
                        customer_increment_order_quantity=customer_increment_order_quantity,
                        customer_alert_order_quantity=customer_alert_order_quantity,
                        order_average_weight=order_average_weight,
                        producer_unit_price=producer_unit_price,
                        unit_deposit=unit_deposit,
                        stock=stock,
                        vat_level=vat_level,
                        offer_description=offer_description,
                        customer_minimum_order_quantity=customer_minimum_order_quantity,
                        picture2=picture2,
                        is_into_offer=True,
                        limit_order_quantity_to_stock=True,
                        is_active=True
                    )
                    production_mode = form.cleaned_data.get('production_mode')
                    if production_mode is not None:
                        product.production_mode.add(form.cleaned_data.get('production_mode'))
                    offer_item = OfferItem.objects.create(
                        permanence_id=permanence_id,
                        product_id=product.id,
                        producer_id=producer.id,
                        is_active=True
                    )
                    offer_item_qs = OfferItem.objects.filter(
                        id=offer_item.id
                    ).order_by('?')
                    clean_offer_item(permanence, offer_item_qs)
                    # Refresh offer_item
                    offer_item = get_object_or_404(OfferItem, id=offer_item.id)
        else:
            form = ProducerProductForm()  # An unbound form
            field = form.fields["long_name"]
            field.initial = _("long_name")
            field = form.fields["order_unit"]
            field.initial = PRODUCT_ORDER_UNIT_PC_PRICE_KG
            field = form.fields["order_average_weight"]
            field.initial = DECIMAL_ZERO
            field = form.fields["customer_increment_order_quantity"]
            field.initial = DECIMAL_ONE
            field = form.fields["producer_unit_price"]
            field.initial = DECIMAL_ZERO
            field = form.fields["unit_deposit"]
            field.initial = DECIMAL_ZERO
            field = form.fields["stock"]
            field.initial = DECIMAL_ZERO
            field = form.fields["vat_level"]
            field.initial = VAT_400
            field = form.fields["offer_description"]
            field.initial = EMPTY_STRING
            field = form.fields["picture"]
            field.widget.upload_to = "%s%s%d" % ("product", os_sep, producer.id)
        return render(
            request,
            "repanier/pre_order_create_product_form.html",
            {'form'    : form, 'permanence_id': permanence_id, 'offer_uuid': offer_uuid, 'offer_item': offer_item,
             'producer': producer}
        )
    raise Http404
Example #10
0
    def get_or_create_offer_item(self, permanence, reset_add_2_stock=False):
        from repanier.models.offeritem import OfferItem, OfferItemWoReceiver

        # In case of contract
        # -1, generate eventually several offer's items if dates are flexible
        # -2, boxes may not be used in contracts
        OfferItemWoReceiver.objects.filter(permanence_id=permanence.id).update(
            may_order=False)
        for contract_content in ContractContent.objects.filter(
                contract_id=self.id,
                permanences_dates__isnull=False).order_by('?'):
            all_dates_str = sorted(
                list(
                    filter(
                        None,
                        contract_content.permanences_dates.split(
                            settings.DJANGO_SETTINGS_DATES_SEPARATOR))))
            if contract_content.flexible_dates:
                permanences_dates_order = 0
                for one_date_str in all_dates_str:
                    permanences_dates_order += 1
                    offer_item_qs = OfferItem.objects.filter(
                        permanence_id=permanence.id,
                        product_id=contract_content.product_id,
                        permanences_dates=one_date_str).order_by('?')
                    if not offer_item_qs.exists():
                        OfferItemWoReceiver.objects.create(
                            permanence_id=permanence.id,
                            product_id=contract_content.product_id,
                            producer_id=contract_content.product.producer_id,
                            contract_id=self.id,
                            permanences_dates=one_date_str,
                            not_permanences_dates=contract_content.
                            not_permanences_dates,
                            permanences_dates_counter=1,
                            permanences_dates_order=permanences_dates_order)
                        clean_offer_item(permanence,
                                         offer_item_qs,
                                         reset_add_2_stock=reset_add_2_stock)
                    else:
                        offer_item = offer_item_qs.first()
                        offer_item.contract_id = self.id
                        offer_item.permanences_dates_order = permanences_dates_order
                        offer_item.not_permanences_dates = contract_content.not_permanences_dates
                        if reset_add_2_stock:
                            offer_item.may_order = True
                        offer_item.save(update_fields=[
                            "contract", "may_order", "permanences_dates_order",
                            "not_permanences_dates"
                        ])
                        clean_offer_item(permanence,
                                         offer_item_qs,
                                         reset_add_2_stock=reset_add_2_stock)
            else:
                offer_item_qs = OfferItem.objects.filter(
                    permanence_id=permanence.id,
                    product_id=contract_content.product_id,
                    permanences_dates=contract_content.permanences_dates
                ).order_by('?')
                if not offer_item_qs.exists():
                    OfferItemWoReceiver.objects.create(
                        permanence_id=permanence.id,
                        product_id=contract_content.product_id,
                        producer_id=contract_content.product.producer_id,
                        contract_id=self.id,
                        permanences_dates=contract_content.permanences_dates,
                        not_permanences_dates=contract_content.
                        not_permanences_dates,
                        permanences_dates_counter=len(all_dates_str))
                    clean_offer_item(permanence,
                                     offer_item_qs,
                                     reset_add_2_stock=reset_add_2_stock)
                else:
                    offer_item = offer_item_qs.first()
                    offer_item.contract_id = self.id
                    offer_item.permanences_dates_order = 0
                    offer_item.not_permanences_dates = contract_content.not_permanences_dates
                    if reset_add_2_stock:
                        offer_item.may_order = True
                    offer_item.save(update_fields=[
                        "contract", "may_order", "permanences_dates_order",
                        "not_permanences_dates"
                    ])
                    clean_offer_item(permanence,
                                     offer_item_qs,
                                     reset_add_2_stock=reset_add_2_stock)
Example #11
0
    def get_or_create_offer_item(self, permanence, reset_add_2_stock=False):
        from repanier.models.offeritem import OfferItem, OfferItemWoReceiver

        # In case of contract
        # -1, generate eventually several offer's items if dates are flexible
        # -2, boxes may not be used in contracts
        OfferItemWoReceiver.objects.filter(permanence_id=permanence.id).update(may_order=False)
        for contract_content in ContractContent.objects.filter(
                contract_id=self.id,
                permanences_dates__isnull=False
        ).order_by('?'):
            all_dates_str = sorted(
                list(filter(None, contract_content.permanences_dates.split(settings.DJANGO_SETTINGS_DATES_SEPARATOR))))
            if contract_content.flexible_dates:
                permanences_dates_order = 0
                for one_date_str in all_dates_str:
                    permanences_dates_order += 1
                    offer_item_qs = OfferItem.objects.filter(
                        permanence_id=permanence.id,
                        product_id=contract_content.product_id,
                        permanences_dates=one_date_str
                    ).order_by('?')
                    if not offer_item_qs.exists():
                        OfferItemWoReceiver.objects.create(
                            permanence_id=permanence.id,
                            product_id=contract_content.product_id,
                            producer_id=contract_content.product.producer_id,
                            contract_id=self.id,
                            permanences_dates=one_date_str,
                            not_permanences_dates=contract_content.not_permanences_dates,
                            permanences_dates_counter=1,
                            permanences_dates_order=permanences_dates_order
                        )
                        clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=reset_add_2_stock)
                    else:
                        offer_item = offer_item_qs.first()
                        offer_item.contract_id = self.id
                        offer_item.permanences_dates_order = permanences_dates_order
                        offer_item.not_permanences_dates = contract_content.not_permanences_dates
                        if reset_add_2_stock:
                            offer_item.may_order = True
                        offer_item.save(update_fields=[
                            "contract", "may_order", "permanences_dates_order", "not_permanences_dates"
                        ])
                        clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=reset_add_2_stock)
            else:
                offer_item_qs = OfferItem.objects.filter(
                    permanence_id=permanence.id,
                    product_id=contract_content.product_id,
                    permanences_dates=contract_content.permanences_dates
                ).order_by('?')
                if not offer_item_qs.exists():
                    OfferItemWoReceiver.objects.create(
                        permanence_id=permanence.id,
                        product_id=contract_content.product_id,
                        producer_id=contract_content.product.producer_id,
                        contract_id=self.id,
                        permanences_dates=contract_content.permanences_dates,
                        not_permanences_dates=contract_content.not_permanences_dates,
                        permanences_dates_counter=len(all_dates_str)
                    )
                    clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=reset_add_2_stock)
                else:
                    offer_item = offer_item_qs.first()
                    offer_item.contract_id = self.id
                    offer_item.permanences_dates_order = 0
                    offer_item.not_permanences_dates = contract_content.not_permanences_dates
                    if reset_add_2_stock:
                        offer_item.may_order = True
                    offer_item.save(update_fields=[
                        "contract", "may_order", "permanences_dates_order", "not_permanences_dates"
                    ])
                    clean_offer_item(permanence, offer_item_qs, reset_add_2_stock=reset_add_2_stock)