order_init_ajax.py 10.4 KB
Newer Older
Patrick's avatar
Patrick committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# -*- coding: utf-8
from __future__ import unicode_literals

import datetime
import json

from django.core.serializers.json import DjangoJSONEncoder
from django.http import Http404
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.formats import number_format
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET

Patrick's avatar
Patrick committed
17
from repanier.const import DECIMAL_ZERO, PERMANENCE_WAIT_FOR_INVOICED, PERMANENCE_OPENED, DECIMAL_ONE, \
Patrick's avatar
Patrick committed
18
    REPANIER_MONEY_ZERO, EMPTY_STRING
Patrick's avatar
Patrick committed
19 20 21 22 23 24 25 26 27 28 29 30
from repanier.models import Customer, Permanence, CustomerInvoice, PermanenceBoard, Staff, OfferItem, \
    ProducerInvoice, Purchase
from repanier.tools import sboolean, sint, display_selected_value, \
    display_selected_box_value, my_basket, my_order_confirmation, calc_basket_message


@never_cache
@require_GET
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))
Patrick's avatar
Patrick committed
31 32 33
        permanence_qs = Permanence.objects.filter(id=permanence_id).only(
            "id", "status", "with_delivery_point"
        ).order_by('?')
Patrick's avatar
Patrick committed
34 35 36 37
        if not permanence_qs.exists():
            raise Http404
        user = request.user
        to_json = []
38
        if user.is_authenticated:
Patrick's avatar
Patrick committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            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,
Patrick's avatar
Patrick committed
58
                        customer_charged_id=customer.id,
Patrick's avatar
Patrick committed
59 60 61 62 63 64 65
                    )
                    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)
                basket = sboolean(request.GET.get('ba', False))
Patrick's avatar
Patrick committed
66
                from repanier.apps import REPANIER_SETTINGS_DISPLAY_PRODUCER_ON_ORDER_FORM, \
Patrick's avatar
Patrick committed
67
                    REPANIER_SETTINGS_MAX_WEEK_WO_PARTICIPATION
Patrick's avatar
Patrick committed
68 69 70 71 72 73 74
                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:
75
                    if customer_invoice.delivery is not None:
Patrick's avatar
Patrick committed
76
                        basket_message = EMPTY_STRING
Patrick's avatar
Patrick committed
77
                    else:
Patrick's avatar
Patrick committed
78 79
                        basket_message = "%s" % (
                            _('The orders are closed.'),
Patrick's avatar
Patrick committed
80
                        )
Patrick's avatar
Patrick committed
81 82 83 84 85 86 87
                my_order_confirmation(
                    permanence=permanence,
                    customer_invoice=customer_invoice,
                    is_basket=basket,
                    basket_message=basket_message,
                    to_json=to_json
                )
Patrick's avatar
Patrick committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                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,
Patrick's avatar
Patrick committed
125
                            permanence__status__lte=PERMANENCE_WAIT_FOR_INVOICED
Patrick's avatar
Patrick committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                        ).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(
142
                                'repanier/communication_permanence_board.html',
Patrick's avatar
Patrick committed
143 144 145 146 147 148 149 150 151
                                {'permanence_boards': permanence_boards, 'count_activity': count_activity})
                            option_dict = {'id': "#communication", 'html': html}
                            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)
152
            if user.is_authenticated and customer is not None:
Patrick's avatar
Patrick committed
153 154 155 156 157 158 159
                # 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(
160
                    "offer_item"
Patrick's avatar
Patrick committed
161 162 163
                ).order_by('?').first()
                if purchase is not None:
                    offer_item = purchase.offer_item
164 165 166 167 168
                    if offer_item is not None:
                        option_dict = display_selected_value(
                            offer_item,
                            purchase.quantity_ordered)
                        to_json.append(option_dict)
Patrick's avatar
Patrick committed
169 170 171
                else:
                    offer_item = OfferItem.objects.filter(
                        id=offer_item_id
172 173 174 175 176 177 178
                    ).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(
Patrick's avatar
Patrick committed
179 180 181 182
                    customer_id=customer.id,
                    offer_item_id=offer_item_id,
                    is_box_content=True
                ).select_related(
183
                    "offer_item"
Patrick's avatar
Patrick committed
184
                ).order_by('?').first()
185 186 187 188 189
                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)
Patrick's avatar
Patrick committed
190 191 192 193 194 195 196 197 198 199 200 201
                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")