示例#1
0
def btn_confirm_order_ajax(request):
    if not request.is_ajax():
        raise Http404
    permanence_id = sint(request.GET.get('permanence', 0))
    user = request.user
    if not user.is_authenticated():
        raise Http404
    customer = Customer.objects.filter(
        user_id=user.id, is_active=True, may_order=True).order_by('?').first()
    if customer is None:
        raise Http404
    translation.activate(customer.language)
    permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first()
    if permanence is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id,
        is_order_confirm_send=False,
        total_price_with_tax__gt=DECIMAL_ZERO
    ).order_by('?')
    if not customer_invoice.exists():
        raise Http404
    filename = "{0}-{1}.xlsx".format(
        slugify(_("Order")),
        slugify(permanence)
    )
    sender_email, sender_function, signature, cc_email_staff = get_signature(
        is_reply_to_order_email=True)
    export_order_2_1_customer(
        customer, filename, permanence, sender_email,
        sender_function, signature
    )
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id
    ).order_by('?').first()
    customer_invoice.confirm_order()
    customer_invoice.save()
    to_json = []
    my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax(), to_json)
    basket_message = calc_basket_message(
        customer,
        permanence,
        PERMANENCE_CLOSED
    )
    my_order_confirmation(
        permanence=permanence,
        customer_invoice=customer_invoice,
        is_basket=True,
        basket_message=basket_message,
        to_json=to_json
    )
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
示例#2
0
def my_cart_amount_ajax(request, permanence_id):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer__user_id=user.id).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    to_json = []
    my_basket(customer_invoice.is_order_confirm_send,
              customer_invoice.get_total_price_with_tax(), to_json)
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder),
                        content_type="application/json")
示例#3
0
def btn_confirm_order_ajax(request):
    if not request.is_ajax():
        raise Http404
    permanence_id = sint(request.GET.get('permanence', 0))
    user = request.user
    customer = Customer.objects.filter(user_id=user.id,
                                       is_active=True,
                                       may_order=True).order_by('?').first()
    if customer is None:
        raise Http404
    translation.activate(customer.language)
    permanence = Permanence.objects.filter(
        id=permanence_id).order_by('?').first()
    if permanence is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id,
        is_order_confirm_send=False,
        is_group=False,
        # total_price_with_tax__gt=DECIMAL_ZERO
        purchase__quantity_ordered__gt=DECIMAL_ZERO,
    ).order_by('?')
    if not customer_invoice.exists():
        raise Http404
    filename = "{0}-{1}.xlsx".format(slugify(_("Order")), slugify(permanence))
    sender_email, sender_function, signature, cc_email_staff = get_signature(
        is_reply_to_order_email=True)
    export_order_2_1_customer(customer, filename, permanence, sender_email,
                              sender_function, signature)
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id).order_by('?').first()
    customer_invoice.confirm_order()
    customer_invoice.save()
    to_json = []
    my_basket(customer_invoice.is_order_confirm_send,
              customer_invoice.get_total_price_with_tax(), to_json)
    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    basket_message = calc_basket_message(customer, permanence, status)
    customer_invoice.my_order_confirmation(permanence=permanence,
                                           is_basket=True,
                                           basket_message=basket_message,
                                           to_json=to_json)
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder),
                        content_type="application/json")
def my_cart_amount_ajax(request, permanence_id):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer__user_id=user.id).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    json_dict = my_basket(customer_invoice.is_order_confirm_send,
                          customer_invoice.get_total_price_with_tax())
    return JsonResponse(json_dict)
示例#5
0
def my_cart_amount_ajax(request, permanence_id):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer__user_id=user.id
    ).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    json_dict = my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax())
    return JsonResponse(json_dict)
def btn_confirm_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
    translation.activate(customer.language)
    permanence_id = sint(request.GET.get('permanence', 0))
    permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first()
    permanence_ok_or_404(permanence)
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id,
        is_order_confirm_send=False,
        is_group=False,
    ).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    filename = "{}-{}.xlsx".format(
        _("Order"),
        permanence
    )
    export_order_2_1_customer(customer, filename, permanence)
    customer_invoice.confirm_order()
    customer_invoice.save()
    json_dict = my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax())
    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    basket_message = get_html_basket_message(
        customer,
        permanence,
        status
    )
    json_dict.update(customer_invoice.get_html_my_order_confirmation(
        permanence=permanence,
        is_basket=True,
        basket_message=basket_message
    ))
    return JsonResponse(json_dict)
def btn_confirm_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
    translation.activate(customer.language)
    permanence_id = sint(request.GET.get('permanence', 0))
    permanence = Permanence.objects.filter(
        id=permanence_id).order_by('?').first()
    if permanence is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence_id,
        customer_id=customer.id,
        is_order_confirm_send=False,
        is_group=False,
    ).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    filename = "{0}-{1}.xlsx".format(_("Order"), permanence)
    export_order_2_1_customer(customer, filename, permanence)
    customer_invoice.confirm_order()
    customer_invoice.save()
    json_dict = my_basket(customer_invoice.is_order_confirm_send,
                          customer_invoice.get_total_price_with_tax())
    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    basket_message = get_html_basket_message(customer, permanence, status)
    json_dict.update(
        customer_invoice.get_html_my_order_confirmation(
            permanence=permanence,
            is_basket=True,
            basket_message=basket_message))
    return JsonResponse(json_dict)
示例#8
0
def order_init_ajax(request):
    if not request.is_ajax():
        raise Http404
    permanence_id = sint(request.GET.get('pe', 0))
    permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first()
    permanence_ok_or_404(permanence)
    user = request.user
    customer = Customer.objects.filter(
        user_id=user.id, may_order=True
    ).only(
        "id", "vat_id", "short_basket_name", "email2", "delivery_point",
        "balance", "date_balance", "may_order"
    ).order_by('?').first()
    if customer is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence.id,
        customer_id=customer.id
    ).order_by('?').first()
    if customer_invoice is None:
        customer_invoice = CustomerInvoice.objects.create(
            permanence_id=permanence.id,
            customer_id=customer.id,
            status=permanence.status,
            customer_charged_id=customer.id,
        )
        customer_invoice.set_order_delivery(delivery=None)
        customer_invoice.calculate_order_price()
        customer_invoice.save()

    if customer_invoice is None:
        raise Http404

    basket = sboolean(request.GET.get('ba', False))

    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    if status <= PERMANENCE_OPENED:
        basket_message = get_html_basket_message(customer, permanence, status)
    else:
        if customer_invoice.delivery is not None:
            basket_message = EMPTY_STRING
        else:
            basket_message = "{}".format(
                _('The orders are closed.')
            )
    if settings.REPANIER_SETTINGS_TEMPLATE == "bs3":
        json_dict = customer_invoice.get_html_my_order_confirmation(
            permanence=permanence,
            is_basket=basket,
            basket_message=basket_message
        )
    else:
        json_dict = {}
    if customer.may_order:
        if settings.REPANIER_SETTINGS_SHOW_PRODUCER_ON_ORDER_FORM:
            for producer_invoice in ProducerInvoice.objects.filter(
                    permanence_id=permanence.id
            ).only(
                "total_price_with_tax", "status"
            ).order_by('?'):
                json_dict.update(producer_invoice.get_order_json())
        communication = sboolean(request.GET.get('co', False))
        if communication:
            now = timezone.now()
            permanence_boards = PermanenceBoard.objects.filter(
                customer_id=customer.id,
                permanence_date__gte=now,
                permanence__status__lte=PERMANENCE_WAIT_FOR_INVOICED
            ).order_by("permanence_date")[:2]
            from repanier.apps import REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
            if REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION > DECIMAL_ZERO or len(permanence_boards) > 0:
                if len(permanence_boards) == 0:
                    count_activity = PermanenceBoard.objects.filter(
                        customer_id=customer.id, permanence_date__lt=now,
                        permanence_date__gte=now - datetime.timedelta(
                            days=float(REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION) * 7
                        )
                    ).count()
                else:
                    count_activity = None
                template_name = get_repanier_template_name("communication_permanence_board.html")
                html = render_to_string(
                    template_name,
                    {'permanence_boards': permanence_boards, 'count_activity': count_activity}
                )
                json_dict["#communicationModal"] = mark_safe(html)
    json_dict.update(my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax()))
    return JsonResponse(json_dict)
示例#9
0
def order_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer = Customer.objects.filter(user_id=user.id,
                                       is_active=True,
                                       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('?')
    to_json = []
    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():
            from repanier.apps import REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS, \
                REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM
            purchase, updated = create_or_update_one_cart_item(
                customer=customer,
                offer_item_id=offer_item_id,
                value_id=value_id,
                is_basket=is_basket,
                batch_job=False)
            offer_item = OfferItemWoReceiver.objects.filter(
                id=offer_item_id).order_by('?').first()
            if purchase is None:
                if offer_item.is_box:
                    sold_out = _("Sold out")
                    option_dict = {
                        'id': "#offer_item%d" % offer_item.id,
                        'html':
                        '<option value="0" selected>%s</option>' % sold_out
                    }
                else:
                    option_dict = display_selected_value(offer_item,
                                                         DECIMAL_ZERO,
                                                         is_open=True)
                to_json.append(option_dict)
            else:
                if offer_item is not None:
                    option_dict = display_selected_value(
                        offer_item, purchase.quantity_ordered, is_open=True)
                    to_json.append(option_dict)
            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 = OfferItem.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:
                            option_dict = display_selected_value(
                                box_offer_item,
                                purchase.quantity_ordered,
                                is_open=True)
                            to_json.append(option_dict)
                        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:
                            option_dict = display_selected_box_value(
                                box_offer_item, box_purchase.quantity_ordered)
                            to_json.append(option_dict)

            if REPANIER_SETTINGS_DISPLAY_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()
                producer_invoice.get_order_json(to_json)

            customer_invoice = CustomerInvoice.objects.filter(
                permanence_id=offer_item.permanence_id,
                customer_id=customer.id).order_by('?').first()
            status_changed = customer_invoice.cancel_confirm_order()
            if REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS and status_changed:
                html = render_to_string(
                    'repanier/communication_confirm_order.html')
                option_dict = {'id': "#communication", 'html': html}
                to_json.append(option_dict)
            customer_invoice.save()
            my_basket(customer_invoice.is_order_confirm_send,
                      customer_invoice.get_total_price_with_tax(), to_json)
            permanence = Permanence.objects.filter(
                id=offer_item.permanence_id).order_by('?').first()

            if is_basket:
                basket_message = calc_basket_message(customer, permanence,
                                                     PERMANENCE_OPENED)
            else:
                basket_message = EMPTY_STRING
            customer_invoice.my_order_confirmation(
                permanence=permanence,
                is_basket=is_basket,
                basket_message=basket_message,
                to_json=to_json)
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder),
                        content_type="application/json")
示例#10
0
def delivery_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    permanence_id = sint(request.GET.get('permanence', 0))
    permanence = Permanence.objects.filter(
        id=permanence_id
    ).only("id", "status").order_by('?').first()
    if permanence is None:
        raise Http404
    customer = Customer.objects.filter(
        user_id=user.id, is_active=True, may_order=True
    ).only(
        "id", "delivery_point", "balance"
    ).order_by('?').first()
    if customer is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        customer_id=customer.id,
        permanence_id=permanence_id
    ).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    to_json = []
    # if (customer_invoice.status == PERMANENCE_OPENED and not customer_invoice.is_order_confirm_send) \
    #         or (customer_invoice.total_price_with_tax == DECIMAL_ZERO):
    #     customer_invoice.status = PERMANENCE_OPENED
    #     customer_invoice.set_delivery(delivery)
    #     customer_invoice.save()
    #     # IMPORTANT : Set the status of the may be already existing purchase to "Open" so that
    #     # the total_price_with_tax will be correctly calculated on the customer order screen.
    #     Purchase.objects.filter(customer_invoice=customer_invoice).order_by('?').update(status=PERMANENCE_OPENED)
    if customer_invoice.status == PERMANENCE_OPENED:
        delivery_id = sint(request.GET.get('delivery', 0))
        if customer.delivery_point is not None:
            qs = DeliveryBoard.objects.filter(
                Q(
                    id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point_id=customer.delivery_point_id,
                    # delivery_point__closed_group=True, -> This is always the case
                    # when delivery_point_id == customer.delivery_point_id
                    status=PERMANENCE_OPENED
                ) | Q(
                    id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point__customer_responsible__isnull=False,
                    status=PERMANENCE_OPENED
                )
            ).order_by('?')
        else:
            qs = DeliveryBoard.objects.filter(
                id=delivery_id,
                permanence_id=permanence_id,
                delivery_point__customer_responsible__isnull=False,
                status=PERMANENCE_OPENED
            ).order_by('?')
        delivery = qs.first()
        if delivery is None:
            raise Http404
        if customer_invoice.delivery != delivery:
            from repanier.apps import REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS

            if customer_invoice.delivery is not None:
                status_changed = customer_invoice.cancel_confirm_order()
                my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax(),
                          to_json)
            else:
                status_changed = False
            customer_invoice.set_delivery(delivery)
            customer_invoice.save()
            if REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS and status_changed:
                html = render_to_string(
                    'repanier/communication_confirm_order.html')
                option_dict = {'id': "#communication", 'html': html}
                to_json.append(option_dict)

    is_basket = sboolean(request.GET.get('is_basket', False))
    customer_invoice.my_order_confirmation(
        permanence=permanence,
        is_basket=is_basket,
        to_json=to_json
    )
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
示例#11
0
def order_init_ajax(request):
    if request.is_ajax():
        # construct a list which will contain all of the data for the response
        permanence_id = sint(request.GET.get('pe', 0))
        permanence_qs = Permanence.objects.filter(id=permanence_id) \
            .only("id", "status", "with_delivery_point").order_by('?')
        if not permanence_qs.exists():
            raise Http404
        user = request.user
        to_json = []
        if user.is_authenticated():
            permanence = permanence_qs.prefetch_related("producers").first()
            customer = Customer.objects.filter(
                user_id=user.id, is_active=True
            ).only(
                "id", "vat_id", "short_basket_name", "email2", "delivery_point",
                "balance", "date_balance", "may_order"
            ).order_by('?').first()
            if customer is None:
                my_basket(False, REPANIER_MONEY_ZERO, to_json)
            else:
                customer_invoice = CustomerInvoice.objects.filter(
                    permanence_id=permanence.id,
                    customer_id=customer.id
                ).order_by('?').first()
                if customer_invoice is None:
                    customer_invoice = CustomerInvoice.objects.create(
                        permanence_id=permanence.id,
                        customer_id=customer.id,
                        status=permanence.status,
                        customer_who_pays_id=customer.id,
                    )
                    customer_invoice.set_delivery(delivery=None)
                    customer_invoice.save()
                if customer_invoice is None:
                    raise Http404
                my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax(), to_json)
                if customer.balance.amount < 0:
                    my_balance = _('My balance : <font color="red">%(balance)s</font> at %(date)s') % {
                        'balance': customer.balance,
                        'date'   : customer.date_balance.strftime(settings.DJANGO_SETTINGS_DATE)}
                else:
                    my_balance = _('My balance : <font color="green">%(balance)s</font> at %(date)s') % {
                        'balance': customer.balance,
                        'date'   : customer.date_balance.strftime(settings.DJANGO_SETTINGS_DATE)}
                option_dict = {'id': "#my_balance", 'html': my_balance}
                to_json.append(option_dict)
                basket = sboolean(request.GET.get('ba', False))
                from repanier.apps import REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS, \
                    REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM, \
                    REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
                if basket or (REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS
                              and customer_invoice.is_order_confirm_send):
                    if customer_invoice.status <= PERMANENCE_OPENED:
                        basket_message = calc_basket_message(customer, permanence, customer_invoice.status)
                    else:
                        if customer_invoice.delivery is not None:
                            basket_message = EMPTY_STRING
                        else:
                            basket_message = "%s" % (
                                _('The orders are closed.'),
                            )
                    my_order_confirmation(
                        permanence=permanence,
                        customer_invoice=customer_invoice,
                        is_basket=basket,
                        basket_message=basket_message,
                        to_json=to_json
                    )
                else:
                    if REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
                        my_order_confirmation(
                            permanence=permanence,
                            customer_invoice=customer_invoice,
                            is_basket=basket,
                            to_json=to_json
                        )
                if customer.may_order:
                    if REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM:
                        for producer in permanence.producers.all():
                            producer_invoice = ProducerInvoice.objects.filter(
                                producer_id=producer.id, permanence_id=permanence.id
                            ).only(
                                "total_price_with_tax", "status"
                            ).order_by('?').first()
                            if producer_invoice is None:
                                producer_invoice = ProducerInvoice.objects.create(
                                    permanence_id=permanence.id,
                                    producer_id=producer.id,
                                    status=permanence.status
                                )
                            if producer.minimum_order_value.amount > DECIMAL_ZERO:
                                if producer_invoice is None:
                                    ratio = 0
                                else:
                                    ratio = producer_invoice.total_price_with_tax.amount / producer.minimum_order_value.amount
                                    if ratio >= DECIMAL_ONE:
                                        ratio = 100
                                    else:
                                        ratio *= 100
                                option_dict = {'id'  : "#order_procent%d" % producer.id,
                                               'html': "%s%%" % number_format(ratio, 0)}
                                to_json.append(option_dict)
                            if producer_invoice.status != PERMANENCE_OPENED:
                                option_dict = {'id'  : "#order_closed%d" % producer.id,
                                               'html': '&nbsp;<span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span>'}
                                to_json.append(option_dict)
                    communication = sboolean(request.GET.get('co', False))
                    if communication \
                            and customer_invoice.total_price_with_tax == DECIMAL_ZERO \
                            and not customer_invoice.is_order_confirm_send:
                        now = timezone.now()
                        permanence_boards = PermanenceBoard.objects.filter(
                            customer_id=customer.id, permanence_date__gte=now,
                            permanence__status__lte=PERMANENCE_WAIT_FOR_DONE
                        ).order_by("permanence_date")[:2]
                        is_staff = Staff.objects.filter(
                            customer_responsible_id=customer.id
                        ).order_by('?').exists()
                        if (not is_staff and REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION > DECIMAL_ZERO) \
                                or len(permanence_boards) > 0:
                            if len(permanence_boards) == 0:
                                count_activity = PermanenceBoard.objects.filter(
                                    customer_id=customer.id, permanence_date__lt=now,
                                    permanence_date__gte=now - datetime.timedelta(
                                        days=float(REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION) * 7
                                    )
                                ).count()
                            else:
                                count_activity = None
                            html = render_to_string(
                                'repanier/communication.html',
                                {'permanence_boards': permanence_boards, 'count_activity': count_activity})
                            option_dict = {'id': "#communication", 'html': html}
                            to_json.append(option_dict)
                else:
                    option_dict = {'id': "#may_not_order", 'html': '1'}
                    to_json.append(option_dict)
        else:
            customer = None
            my_basket(False, REPANIER_MONEY_ZERO, to_json)
        request_offer_items = request.GET.getlist('oi')
        for request_offer_item in request_offer_items:
            offer_item_id = sint(request_offer_item)
            if user.is_authenticated() and customer is not None:
                # No need to check customer.may_order.
                # Select one purchase
                purchase = Purchase.objects.filter(
                    customer_id=customer.id,
                    offer_item_id=offer_item_id,
                    is_box_content=False
                ).select_related(
                    "offer_item"
                ).order_by('?').first()
                if purchase is not None:
                    offer_item = purchase.offer_item
                    if offer_item is not None:
                        option_dict = display_selected_value(
                            offer_item,
                            purchase.quantity_ordered)
                        to_json.append(option_dict)
                else:
                    offer_item = OfferItem.objects.filter(
                        id=offer_item_id
                    ).order_by('?').first()
                    if offer_item is not None:
                        option_dict = display_selected_value(
                            offer_item,
                            DECIMAL_ZERO)
                        to_json.append(option_dict)
                box_purchase = Purchase.objects.filter(
                    customer_id=customer.id,
                    offer_item_id=offer_item_id,
                    is_box_content=True
                ).select_related(
                    "offer_item"
                ).order_by('?').first()
                if box_purchase is not None:
                    offer_item = box_purchase.offer_item
                    if offer_item is not None:
                        option_dict = display_selected_box_value(customer, offer_item, box_purchase)
                        to_json.append(option_dict)
                option_dict = {'id': ".btn_like%s" % offer_item_id, 'html': offer_item.get_like(user)}
                to_json.append(option_dict)
            else:
                option_dict = {'id'  : "#offer_item%s" % offer_item_id,
                               'html': '<option value="0" selected>---</option>'}
                to_json.append(option_dict)
                msg_html = '<span class="glyphicon glyphicon-heart-empty"></span>'
                option_dict = {'id': ".btn_like%s" % offer_item_id, 'html': msg_html}
                to_json.append(option_dict)
    else:
        raise Http404
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
示例#12
0
def delivery_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    permanence_id = sint(request.GET.get('permanence', 0))
    permanence = Permanence.objects.filter(id=permanence_id).only(
        "id", "status").order_by('?').first()
    if permanence is None:
        raise Http404
    customer = Customer.objects.filter(user_id=user.id, may_order=True).only(
        "id", "delivery_point", "balance").order_by('?').first()
    if customer is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        customer_id=customer.id,
        permanence_id=permanence_id).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    json_dict = {}
    # if (customer_invoice.status == PERMANENCE_OPENED and not customer_invoice.is_order_confirm_send) \
    #         or (customer_invoice.total_price_with_tax == DECIMAL_ZERO):
    #     customer_invoice.status = PERMANENCE_OPENED
    #     customer_invoice.set_delivery(delivery)
    #     customer_invoice.save()
    #     # IMPORTANT : Set the status of the may be already existing purchase to "Open" so that
    #     # the total_price_with_tax will be correctly calculated on the customer order screen.
    #     Purchase.objects.filter(customer_invoice=customer_invoice).order_by('?').update(status=PERMANENCE_OPENED)
    if customer_invoice.status == PERMANENCE_OPENED:
        delivery_id = sint(request.GET.get('delivery', 0))
        if customer.delivery_point is not None:
            qs = DeliveryBoard.objects.filter(
                Q(
                    id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point_id=customer.delivery_point_id,
                    # delivery_point__closed_group=True, -> This is always the case
                    # when delivery_point_id == customer.delivery_point_id
                    status=PERMANENCE_OPENED)
                | Q(id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point__customer_responsible__isnull=True,
                    status=PERMANENCE_OPENED)).order_by('?')
        else:
            qs = DeliveryBoard.objects.filter(
                id=delivery_id,
                permanence_id=permanence_id,
                delivery_point__customer_responsible__isnull=True,
                status=PERMANENCE_OPENED).order_by('?')
        delivery = qs.first()
        if delivery is None:
            raise Http404
        if customer_invoice.delivery != delivery:
            if customer_invoice.delivery is not None:
                status_changed = customer_invoice.cancel_confirm_order()
                json_dict = my_basket(
                    customer_invoice.is_order_confirm_send,
                    customer_invoice.get_total_price_with_tax())
            else:
                status_changed = False
            customer_invoice.set_delivery(delivery)
            customer_invoice.save()
            if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER and status_changed:
                html = render_to_string(
                    'repanier/communication_confirm_order.html')
                json_dict["#communicationModal"] = mark_safe(html)

    is_basket = sboolean(request.GET.get('is_basket', False))
    json_dict.update(
        customer_invoice.get_html_my_order_confirmation(permanence=permanence,
                                                        is_basket=is_basket))
    return JsonResponse(json_dict)
示例#13
0
def delivery_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    permanence_id = sint(request.GET.get('permanence', 0))
    permanence = Permanence.objects.filter(
        id=permanence_id
    ).only("id", "status").order_by('?').first()
    if permanence is None:
        raise Http404
    customer = Customer.objects.filter(
        user_id=user.id, may_order=True
    ).order_by('?').first()
    if customer is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        customer_id=customer.id,
        permanence_id=permanence_id
    ).order_by('?').first()
    if customer_invoice is None:
        raise Http404
    json_dict = {}
    if customer_invoice.status == PERMANENCE_OPENED:
        delivery_id = sint(request.GET.get('delivery', 0))
        if customer.delivery_point is not None:
            qs = DeliveryBoard.objects.filter(
                Q(
                    id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point_id=customer.delivery_point_id,
                    # delivery_point__closed_group=True, -> This is always the case
                    # when delivery_point_id == customer.delivery_point_id
                    status=PERMANENCE_OPENED
                ) | Q(
                    id=delivery_id,
                    permanence_id=permanence_id,
                    delivery_point__customer_responsible__isnull=True,
                    status=PERMANENCE_OPENED
                )
            ).order_by('?')
        else:
            qs = DeliveryBoard.objects.filter(
                id=delivery_id,
                permanence_id=permanence_id,
                delivery_point__customer_responsible__isnull=True,
                status=PERMANENCE_OPENED
            ).order_by('?')
        delivery = qs.first()
        if delivery is None:
            raise Http404
        if customer_invoice.delivery != delivery:
            customer_invoice.set_order_delivery(delivery)
            customer_invoice.calculate_order_price()
            invoice_confirm_status_is_changed = customer_invoice.cancel_confirm_order()
            customer_invoice.save()
            if settings.REPANIER_SETTINGS_CUSTOMER_MUST_CONFIRM_ORDER and invoice_confirm_status_is_changed:
                html = render_to_string(template_communication_confirm_order)
                json_dict["#communicationModal"] = mark_safe(html)

            json_dict.update(
                my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax()))

    is_basket = sboolean(request.GET.get('is_basket', False))
    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    basket_message = get_html_basket_message(
        customer,
        permanence,
        status
    )
    json_dict.update(customer_invoice.get_html_my_order_confirmation(
        permanence=permanence,
        is_basket=is_basket,
        basket_message=basket_message
    ))
    return JsonResponse(json_dict)
示例#14
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)
示例#15
0
def order_init_ajax(request):
    if not request.is_ajax():
        raise Http404
    permanence_id = sint(request.GET.get('pe', 0))
    permanence = Permanence.objects.filter(
        id=permanence_id).order_by('?').first()
    permanence_ok_or_404(permanence)
    user = request.user
    customer = Customer.objects.filter(user_id=user.id, may_order=True).only(
        "id", "vat_id", "short_basket_name", "email2", "delivery_point",
        "balance", "date_balance", "may_order").order_by('?').first()
    if customer is None:
        raise Http404
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence.id,
        customer_id=customer.id).order_by('?').first()
    if customer_invoice is None:
        customer_invoice = CustomerInvoice.objects.create(
            permanence_id=permanence.id,
            customer_id=customer.id,
            status=permanence.status,
            customer_charged_id=customer.id,
        )
        customer_invoice.set_delivery(delivery=None)
        customer_invoice.save()

    if customer_invoice is None:
        raise Http404

    basket = sboolean(request.GET.get('ba', False))

    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    if status <= PERMANENCE_OPENED:
        basket_message = get_html_basket_message(customer, permanence, status)
    else:
        if customer_invoice.delivery is not None:
            basket_message = EMPTY_STRING
        else:
            basket_message = "{}".format(_('The orders are closed.'))
    json_dict = customer_invoice.get_html_my_order_confirmation(
        permanence=permanence, is_basket=basket, basket_message=basket_message)
    if customer.may_order:
        if settings.REPANIER_SETTINGS_SHOW_PRODUCER_ON_ORDER_FORM:
            for producer_invoice in ProducerInvoice.objects.filter(
                    permanence_id=permanence.id).only("total_price_with_tax",
                                                      "status").order_by('?'):
                json_dict.update(producer_invoice.get_order_json())
        communication = sboolean(request.GET.get('co', False))
        if communication:
            now = timezone.now()
            permanence_boards = PermanenceBoard.objects.filter(
                customer_id=customer.id,
                permanence_date__gte=now,
                permanence__status__lte=PERMANENCE_WAIT_FOR_INVOICED).order_by(
                    "permanence_date")[:2]
            from repanier.apps import REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
            if REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION > DECIMAL_ZERO or len(
                    permanence_boards) > 0:
                if len(permanence_boards) == 0:
                    count_activity = PermanenceBoard.objects.filter(
                        customer_id=customer.id,
                        permanence_date__lt=now,
                        permanence_date__gte=now -
                        datetime.timedelta(days=float(
                            REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION) *
                                           7)).count()
                else:
                    count_activity = None
                html = render_to_string(
                    'repanier/communication_permanence_board.html', {
                        'permanence_boards': permanence_boards,
                        'count_activity': count_activity
                    })
                json_dict["#communicationModal"] = mark_safe(html)
    json_dict.update(
        my_basket(customer_invoice.is_order_confirm_send,
                  customer_invoice.get_total_price_with_tax()))
    return JsonResponse(json_dict)
示例#16
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)
示例#17
0
def order_init_ajax(request):
    if not request.is_ajax():
        raise Http404
    user = request.user
    customer = Customer.objects.filter(
        user_id=user.id, is_active=True
    ).only(
        "id", "vat_id", "short_basket_name", "email2", "delivery_point",
        "balance", "date_balance", "may_order"
    ).order_by('?').first()
    if customer is None:
        raise Http404

    permanence_id = sint(request.GET.get('pe', 0))
    permanence = Permanence.objects.filter(id=permanence_id).order_by('?').first()
    permanence_ok_or_404(permanence)
    to_json = []
    customer_invoice = CustomerInvoice.objects.filter(
        permanence_id=permanence.id,
        customer_id=customer.id
    ).order_by('?').first()
    if customer_invoice is None:
        customer_invoice = CustomerInvoice.objects.create(
            permanence_id=permanence.id,
            customer_id=customer.id,
            status=permanence.status,
            customer_charged_id=customer.id,
        )
        customer_invoice.set_delivery(delivery=None)
        customer_invoice.save()

    if customer_invoice is None:
        raise Http404

    basket = sboolean(request.GET.get('ba', False))
    from repanier.apps import REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM, \
        REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
    if customer_invoice.delivery is not None:
        status = customer_invoice.delivery.status
    else:
        status = customer_invoice.status
    if status <= PERMANENCE_OPENED:
        basket_message = calc_basket_message(customer, permanence, status)
    else:
        if customer_invoice.delivery is not None:
            basket_message = EMPTY_STRING
        else:
            basket_message = "%s" % (
                _('The orders are closed.'),
            )
    customer_invoice.my_order_confirmation(
        permanence=permanence,
        is_basket=basket,
        basket_message=basket_message,
        to_json=to_json
    )
    if customer.may_order:
        if REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM:
            for producer_invoice in ProducerInvoice.objects.filter(
                permanence_id=permanence.id
            ).only(
                "total_price_with_tax", "status"
            ).order_by('?'):
                producer_invoice.get_order_json(to_json)
        communication = sboolean(request.GET.get('co', False))
        if communication \
                and customer_invoice.total_price_with_tax == DECIMAL_ZERO \
                and not customer_invoice.is_order_confirm_send:
            now = timezone.now()
            permanence_boards = PermanenceBoard.objects.filter(
                customer_id=customer.id,
                permanence_date__gte=now,
                permanence__status__lte=PERMANENCE_WAIT_FOR_INVOICED
            ).order_by("permanence_date")[:2]
            is_staff = Staff.objects.filter(
                customer_responsible_id=customer.id
            ).order_by('?').exists()
            if (not is_staff and REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION > DECIMAL_ZERO) \
                    or len(permanence_boards) > 0:
                if len(permanence_boards) == 0:
                    count_activity = PermanenceBoard.objects.filter(
                        customer_id=customer.id, permanence_date__lt=now,
                        permanence_date__gte=now - datetime.timedelta(
                            days=float(REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION) * 7
                        )
                    ).count()
                else:
                    count_activity = None
                html = render_to_string(
                    'repanier/communication_permanence_board.html',
                    {'permanence_boards': permanence_boards, 'count_activity': count_activity})
                option_dict = {'id': "#communication", 'html': html}
                to_json.append(option_dict)
    my_basket(customer_invoice.is_order_confirm_send, customer_invoice.get_total_price_with_tax(), to_json)
    return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")