Esempio n. 1
0
def repanier_select_offer_item(context, *args, **kwargs):
    request = context["request"]
    user = request.user
    offer_item = kwargs.get("offer_item")
    date = kwargs.get("date", EMPTY_STRING)
    result = []
    if offer_item.may_order:
        select_offer_item(offer_item, result, user)
    if offer_item.is_box_content:
        box_purchase = (
            PurchaseWoReceiver.objects.filter(
                customer_id=user.customer,
                offer_item_id=offer_item.id,
                is_box_content=True,
            )
            .order_by("?")
            .only("quantity_ordered")
            .first()
        )
        if box_purchase is None:
            quantity_ordered = DECIMAL_ZERO
        else:
            quantity_ordered = box_purchase.quantity_ordered
        html = get_html_selected_box_value(offer_item, quantity_ordered)
        result.append(
            '<select id="box_offer_item{id}" name="box_offer_item{id}" disabled class="form-control">{option}</select>'.format(
                result=result, id=offer_item.id, option=html
            )
        )
    return mark_safe(EMPTY_STRING.join(result))
Esempio n. 2
0
def repanier_select_offer_item(context, *args, **kwargs):
    request = context['request']
    user = request.user
    offer_item = kwargs.get('offer_item')
    date = kwargs.get('date', EMPTY_STRING)
    result = []
    if offer_item.may_order:
        # Important : offer_item.permanences_dates_order is used to
        # group together offer item's of the same product of a contract
        # with different purchases dates on the order form
        # 0   : No group needed
        # 1   : Master of a group
        # > 1 : Displayed with the master of the group (filtered in order_class.py)
        select_offer_item(offer_item, result, user)
        if offer_item.permanences_dates_order == 1 and date == "all":
            for sub_offer_item in OfferItemWoReceiver.objects.filter(
                    permanence_id=offer_item.permanence_id,
                    product_id=offer_item.product_id,
                    permanences_dates_order__gt=1
            ).order_by("permanences_dates_order"):
                select_offer_item(sub_offer_item, result, user)
    if offer_item.is_box_content:
        box_purchase = PurchaseWoReceiver.objects.filter(
            customer_id=user.customer,
            offer_item_id=offer_item.id,
            is_box_content=True
        ).order_by('?').only('quantity_ordered').first()
        if box_purchase is None:
            quantity_ordered = DECIMAL_ZERO
        else:
            quantity_ordered = box_purchase.quantity_ordered
        html = get_html_selected_box_value(offer_item, quantity_ordered)
        result.append(
            "<select id=\"box_offer_item{id}\" name=\"box_offer_item{id}\" disabled class=\"form-control\">{option}</select>".format(
                result=result,
                id=offer_item.id,
                option=html
            ))
    return mark_safe(EMPTY_STRING.join(result))
Esempio n. 3
0
def repanier_select_offer_item(context, *args, **kwargs):
    request = context['request']
    user = request.user
    offer_item = kwargs.get('offer_item')
    date = kwargs.get('date', EMPTY_STRING)
    result = []
    if offer_item.may_order:
        # Important : offer_item.permanences_dates_order is used to
        # group together offer item's of the same product of a contract
        # with different purchases dates on the order form
        # 0   : No group needed
        # 1   : Master of a group
        # > 1 : Displayed with the master of the group (filtered in order_class.py)
        select_offer_item(offer_item, result, user)
        if offer_item.permanences_dates_order == 1 and date == "all":
            for sub_offer_item in OfferItemWoReceiver.objects.filter(
                    permanence_id=offer_item.permanence_id,
                    product_id=offer_item.product_id,
                    permanences_dates_order__gt=1).order_by(
                        "permanences_dates_order"):
                select_offer_item(sub_offer_item, result, user)
    if offer_item.is_box_content:
        box_purchase = PurchaseWoReceiver.objects.filter(
            customer_id=user.customer,
            offer_item_id=offer_item.id,
            is_box_content=True).order_by('?').only(
                'quantity_ordered').first()
        if box_purchase is None:
            quantity_ordered = DECIMAL_ZERO
        else:
            quantity_ordered = box_purchase.quantity_ordered
        html = get_html_selected_box_value(offer_item, quantity_ordered)
        result.append(
            "<select id=\"box_offer_item{id}\" name=\"box_offer_item{id}\" disabled class=\"form-control\">{option}</select>"
            .format(result=result, id=offer_item.id, option=html))
    return mark_safe(EMPTY_STRING.join(result))
Esempio n. 4
0
def order_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer = Customer.objects.filter(
        user_id=user.id, may_order=True
    ).order_by('?').first()
    if customer is None:
        raise Http404
    offer_item_id = sint(request.GET.get('offer_item', 0))
    value_id = sint(request.GET.get('value', 0))
    is_basket = sboolean(request.GET.get('is_basket', False))
    qs = CustomerInvoice.objects.filter(
        permanence__offeritem=offer_item_id,
        customer_id=customer.id,
        status=PERMANENCE_OPENED).order_by('?')
    json_dict = {}
    if qs.exists():
        qs = ProducerInvoice.objects.filter(
            permanence__offeritem=offer_item_id,
            producer__offeritem=offer_item_id,
            status=PERMANENCE_OPENED
        ).order_by('?')
        if qs.exists():
            purchase, updated = create_or_update_one_cart_item(
                customer=customer,
                offer_item_id=offer_item_id,
                value_id=value_id,
                batch_job=False,
                comment=EMPTY_STRING
            )
            offer_item = OfferItemWoReceiver.objects.filter(
                id=offer_item_id
            ).order_by('?').first()
            if purchase is None:
                json_dict["#offer_item{}".format(offer_item.id)] = get_html_selected_value(offer_item, DECIMAL_ZERO,
                                                                                           is_open=True)
            else:
                json_dict["#offer_item{}".format(offer_item.id)] = get_html_selected_value(offer_item,
                                                                                           purchase.quantity_ordered,
                                                                                           is_open=True)
            if updated and offer_item.is_box:
                # update the content
                for content in BoxContent.objects.filter(
                        box=offer_item.product_id
                ).only(
                    "product_id"
                ).order_by('?'):
                    box_offer_item = OfferItemWoReceiver.objects.filter(
                        product_id=content.product_id,
                        permanence_id=offer_item.permanence_id
                    ).order_by('?').first()
                    if box_offer_item is not None:
                        # Select one purchase
                        purchase = PurchaseWoReceiver.objects.filter(
                            customer_id=customer.id,
                            offer_item_id=box_offer_item.id,
                            is_box_content=False
                        ).order_by('?').only('quantity_ordered').first()
                        if purchase is not None:
                            json_dict["#offer_item{}".format(box_offer_item.id)] = get_html_selected_value(
                                box_offer_item,
                                purchase.quantity_ordered,
                                is_open=True
                            )
                        box_purchase = PurchaseWoReceiver.objects.filter(
                            customer_id=customer.id,
                            offer_item_id=box_offer_item.id,
                            is_box_content=True
                        ).order_by('?').only('quantity_ordered').first()
                        if box_purchase is not None:
                            json_dict["#box_offer_item{}".format(box_offer_item.id)] = get_html_selected_box_value(
                                box_offer_item,
                                box_purchase.quantity_ordered
                            )

            if settings.REPANIER_SETTINGS_SHOW_PRODUCER_ON_ORDER_FORM:
                producer_invoice = ProducerInvoice.objects.filter(
                    producer_id=offer_item.producer_id, permanence_id=offer_item.permanence_id
                ).only("total_price_with_tax").order_by('?').first()
                json_dict.update(producer_invoice.get_order_json())

            customer_invoice = CustomerInvoice.objects.filter(
                permanence_id=offer_item.permanence_id,
                customer_id=customer.id
            ).order_by('?').first()
            invoice_confirm_status_is_changed = customer_invoice.cancel_confirm_order()
            if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER and invoice_confirm_status_is_changed:
                template_name = get_repanier_template_name("communication_confirm_order.html")
                html = render_to_string(template_name)
                json_dict["#communicationModal"] = mark_safe(html)
                customer_invoice.save()

            json_dict.update(
                my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax()))
            permanence = Permanence.objects.filter(
                id=offer_item.permanence_id
            ).order_by('?').first()

            if is_basket:
                basket_message = get_html_basket_message(customer, permanence, PERMANENCE_OPENED)
            else:
                basket_message = EMPTY_STRING
            json_dict.update(customer_invoice.get_html_my_order_confirmation(
                permanence=permanence,
                is_basket=is_basket,
                basket_message=basket_message
            ))
    return JsonResponse(json_dict)
Esempio n. 5
0
def order_ajax(request):
    """
    Add a selected offer item to a customer order (i.e. update the customer's invoice and the producer's invoice)
    """

    if not request.is_ajax():
        raise Http404
    user = request.user
    customer = Customer.objects.filter(
        user_id=user.id, may_order=True
    ).first()
    if customer is None:
        raise Http404
    offer_item_id = sint(request.GET.get('offer_item', 0))
    value_id = sint(request.GET.get('value', 0))
    is_basket = sboolean(request.GET.get('is_basket', False))
    qs = CustomerInvoice.objects.filter(
        permanence__offeritem=offer_item_id,
        customer_id=customer.id,
        status=PERMANENCE_OPENED)
    json_dict = {}
    if qs.exists():
        qs = ProducerInvoice.objects.filter(
            permanence__offeritem=offer_item_id,
            producer__offeritem=offer_item_id,
            status=PERMANENCE_OPENED
        )
        if qs.exists():
            purchase, updated = create_or_update_one_cart_item(
                customer=customer,
                offer_item_id=offer_item_id,
                value_id=value_id,
                batch_job=False,
                comment=EMPTY_STRING
            )
            offer_item = OfferItemWoReceiver.objects.filter(
                id=offer_item_id
            ).first()
            if purchase is None:
                json_dict["#offer_item{}".format(offer_item.id)] = get_html_selected_value(offer_item, DECIMAL_ZERO,
                                                                                           is_open=True)
            else:
                json_dict["#offer_item{}".format(offer_item.id)] = get_html_selected_value(offer_item,
                                                                                           purchase.quantity_ordered,
                                                                                           is_open=True)
            if updated and offer_item.is_box:
                # update the content
                for content in BoxContent.objects.filter(
                        box=offer_item.product_id
                ).only(
                    "product_id"
                ):
                    box_offer_item = OfferItemWoReceiver.objects.filter(
                        product_id=content.product_id,
                        permanence_id=offer_item.permanence_id
                    ).first()
                    if box_offer_item is not None:
                        # Select one purchase
                        purchase = PurchaseWoReceiver.objects.filter(
                            customer_id=customer.id,
                            offer_item_id=box_offer_item.id,
                            is_box_content=False
                        ).only('quantity_ordered').first()
                        if purchase is not None:
                            json_dict["#offer_item{}".format(box_offer_item.id)] = get_html_selected_value(
                                box_offer_item,
                                purchase.quantity_ordered,
                                is_open=True
                            )
                        box_purchase = PurchaseWoReceiver.objects.filter(
                            customer_id=customer.id,
                            offer_item_id=box_offer_item.id,
                            is_box_content=True
                        ).only('quantity_ordered').first()
                        if box_purchase is not None:
                            json_dict["#box_offer_item{}".format(box_offer_item.id)] = get_html_selected_box_value(
                                box_offer_item,
                                box_purchase.quantity_ordered
                            )

            if settings.REPANIER_SETTINGS_SHOW_PRODUCER_ON_ORDER_FORM:
                producer_invoice = ProducerInvoice.objects.filter(
                    producer_id=offer_item.producer_id, permanence_id=offer_item.permanence_id
                ).only("total_price_with_tax").first()
                json_dict.update(producer_invoice.get_order_json())

            customer_invoice = CustomerInvoice.objects.filter(
                permanence_id=offer_item.permanence_id,
                customer_id=customer.id
            ).first()
            invoice_confirm_status_is_changed = customer_invoice.cancel_confirm_order()
            if invoice_confirm_status_is_changed:
                if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER:
                    template_name = get_repanier_template_name("communication_confirm_order.html")
                    html = render_to_string(template_name)
                    json_dict["#communicationModal"] = mark_safe(html)
                customer_invoice.save()

            json_dict.update(
                my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax()))
            permanence = Permanence.objects.filter(
                id=offer_item.permanence_id
            ).first()

            if is_basket:
                basket_message = get_html_basket_message(customer, permanence, PERMANENCE_OPENED)
            else:
                basket_message = EMPTY_STRING
            json_dict.update(customer_invoice.get_html_my_order_confirmation(
                permanence=permanence,
                is_basket=is_basket,
                basket_message=basket_message
            ))
    return JsonResponse(json_dict)