def repanier_select_offer_item(context, *args, **kwargs): request = context['request'] user = request.user result = EMPTY_STRING customer_may_order = Customer.objects.filter( user_id=user.id, is_active=True).order_by('?').exists() if customer_may_order: offer_item = kwargs.get('offer_item', None) str_id = str(offer_item.id) if offer_item.may_order: purchase = PurchaseWoReceiver.objects.filter( customer_id=user.customer, offer_item_id=offer_item.id, is_box_content=False).order_by('?').only( 'quantity_ordered').first() if purchase is not None: is_open = purchase.status == PERMANENCE_OPENED option_dict = display_selected_value(offer_item, purchase.quantity_ordered, is_open=is_open) else: is_open = ProducerInvoice.objects.filter( permanence__offeritem=offer_item.id, producer__offeritem=offer_item.id, status=PERMANENCE_OPENED).order_by('?').exists() option_dict = display_selected_value(offer_item, DECIMAL_ZERO, is_open=is_open) # print(option_dict) if is_open: result = '<select name="offer_item{str_id}" id="offer_item{str_id}" onchange="order_ajax({str_id})" onmouseover="show_select_order_list_ajax({str_id})" class="form-control">{option}</select>'.format( str_id=str_id, option=option_dict['html']) else: result = '<select name="offer_item{str_id}" id="offer_item{str_id}" class="form-control">{option}</select>'.format( str_id=str_id, option=option_dict['html']) box_purchase = PurchaseWoReceiver.objects.filter( customer_id=user.customer, offer_item_id=offer_item.id, is_box_content=True).order_by('?').select_related( 'offer_item').only('offer_item', 'quantity_ordered').first() if box_purchase is not None: offer_item = box_purchase.offer_item option_dict = display_selected_box_value( offer_item, box_purchase.quantity_ordered) result = result + option_dict['html'] return mark_safe(result)
def order_select_ajax(request): if request.is_ajax(): # construct a list which will contain all of the data for the response user = request.user to_json = [] if user.is_authenticated(): customer = Customer.objects.filter( user_id=user.id, is_active=True, may_order=True) \ .only("id", "vat_id", "language").order_by('?').first() if customer is not None: translation.activate(customer.language) offer_item_id = sint(request.GET.get('offer_item', 0)) # Select one purchase purchase = Purchase.objects.filter( customer_id=customer.id, offer_item_id=offer_item_id, is_box_content=False ).order_by('?').first() if purchase is not None: q_previous_order = purchase.quantity_ordered else: q_previous_order = DECIMAL_ZERO offer_item = OfferItem.objects.filter(id=offer_item_id, is_active=True) \ .order_by('?').first() qs = ProducerInvoice.objects.filter( permanence__offeritem=offer_item_id, producer__offeritem=offer_item_id, status=PERMANENCE_OPENED ).order_by('?') if qs.exists(): if offer_item is not None: customer_invoice = CustomerInvoice.objects.filter( permanence_id=offer_item.permanence_id, customer=customer.id).only("status").order_by('?').first() if customer_invoice is not None: status = customer_invoice.status if PERMANENCE_OPENED <= status <= PERMANENCE_SEND: a_price = offer_item.customer_unit_price.amount + offer_item.unit_deposit.amount q_min = offer_item.customer_minimum_order_quantity if status == PERMANENCE_OPENED and offer_item.limit_order_quantity_to_stock: q_alert = offer_item.stock - offer_item.quantity_invoiced + q_previous_order if q_alert < DECIMAL_ZERO: q_alert = DECIMAL_ZERO else: q_alert = offer_item.customer_alert_order_quantity q_step = offer_item.customer_increment_order_quantity q_order_is_displayed = False q_select_id = 0 selected = EMPTY_STRING if q_previous_order <= 0: q_order_is_displayed = True selected = "selected" q_valid = q_min if q_valid <= q_alert: if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): option_dict = {'value': '0', 'selected': selected, 'label': '---'} to_json.append(option_dict) else: if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): sold_out = _("Sold out") option_dict = {'value': '0', 'selected': selected, 'label': sold_out} to_json.append(option_dict) q_counter = 0 # Limit to avoid too long selection list while q_valid <= q_alert and q_counter <= LIMIT_ORDER_QTY_ITEM: q_select_id += 1 q_counter += 1 selected = EMPTY_STRING if not q_order_is_displayed: if q_previous_order <= q_valid: q_order_is_displayed = True selected = "selected" if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): display = get_display( qty=q_valid, order_average_weight=offer_item.order_average_weight, order_unit=offer_item.order_unit, unit_price_amount=a_price, for_order_select=True ) option_dict = {'value': str(q_select_id), 'selected': selected, 'label': display} to_json.append(option_dict) if q_valid < q_step: # 1; 2; 4; 6; 8 ... q_min = 1; q_step = 2 # 0,5; 1; 2; 3 ... q_min = 0,5; q_step = 1 q_valid = q_step else: # 1; 2; 3; 4 ... q_min = 1; q_step = 1 # 0,125; 0,175; 0,225 ... q_min = 0,125; q_step = 0,50 q_valid = q_valid + q_step if not q_order_is_displayed: # An custom order_qty > q_alert q_select_id += 1 selected = "selected" display = get_display( qty=q_previous_order, order_average_weight=offer_item.order_average_weight, order_unit=offer_item.order_unit, unit_price_amount=a_price, for_order_select=True ) option_dict = {'value': str(q_select_id), 'selected': selected, 'label': display} to_json.append(option_dict) if status == PERMANENCE_OPENED: # _not_lazy string are not placed in the "django.po" other = _("Other qty") option_dict = {'value': 'other_qty', 'selected': EMPTY_STRING, 'label': other} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: if q_previous_order <= DECIMAL_ZERO: closed = _("Closed") option_dict = {'value': '0', 'selected': 'selected', 'label': '%s' % closed} to_json.append(option_dict) else: option_dict = display_selected_value(offer_item, purchase.quantity_ordered) to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: raise Http404 return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
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")
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': ' <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")
def order_select_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) \ .only("id", "vat_id", "language").order_by('?').first() if customer is None: raise Http404 to_json = [] translation.activate(customer.language) offer_item_id = sint(request.GET.get('offer_item', 0)) # Select one purchase purchase = PurchaseWoReceiver.objects.filter( customer_id=customer.id, offer_item_id=offer_item_id, is_box_content=False ).order_by('?').select_related('offer_item').only('offer_item', 'quantity_ordered').first() offer_item = purchase.offer_item qs = ProducerInvoice.objects.filter( permanence__offeritem=offer_item_id, producer__offeritem=offer_item_id, status=PERMANENCE_OPENED ).order_by('?') if qs.exists(): if offer_item is not None: customer_invoice = CustomerInvoice.objects.filter( permanence_id=offer_item.permanence_id, customer_id=customer.id).only("status").order_by('?').first() if customer_invoice is not None: status = customer_invoice.status if PERMANENCE_OPENED <= status <= PERMANENCE_SEND: a_price = offer_item.customer_unit_price.amount + offer_item.unit_deposit.amount q_min = offer_item.customer_minimum_order_quantity if purchase is not None: q_previous_order = purchase.quantity_ordered else: q_previous_order = DECIMAL_ZERO if status == PERMANENCE_OPENED and offer_item.limit_order_quantity_to_stock: q_alert = offer_item.stock - offer_item.quantity_invoiced + q_previous_order if q_alert < DECIMAL_ZERO: q_alert = DECIMAL_ZERO else: q_alert = offer_item.customer_alert_order_quantity q_step = offer_item.customer_increment_order_quantity q_order_is_displayed = False q_select_id = 0 selected = EMPTY_STRING if q_previous_order <= 0: q_order_is_displayed = True selected = "selected" q_valid = q_min if q_valid <= q_alert: if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): option_dict = {'value': '0', 'selected': selected, 'label': '---'} to_json.append(option_dict) else: if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): sold_out = _("Sold out") option_dict = {'value': '0', 'selected': selected, 'label': sold_out} to_json.append(option_dict) q_counter = 0 # Limit to avoid too long selection list while q_valid <= q_alert and q_counter <= LIMIT_ORDER_QTY_ITEM: q_select_id += 1 q_counter += 1 selected = EMPTY_STRING if not q_order_is_displayed: if q_previous_order <= q_valid: q_order_is_displayed = True selected = "selected" if (status == PERMANENCE_OPENED or (status <= PERMANENCE_SEND and selected == "selected")): display = offer_item.get_display( qty=q_valid, order_unit=offer_item.order_unit, unit_price_amount=a_price, for_order_select=True ) option_dict = {'value': str(q_select_id), 'selected': selected, 'label': display} to_json.append(option_dict) if q_valid < q_step: # 1; 2; 4; 6; 8 ... q_min = 1; q_step = 2 # 0,5; 1; 2; 3 ... q_min = 0,5; q_step = 1 q_valid = q_step else: # 1; 2; 3; 4 ... q_min = 1; q_step = 1 # 0,125; 0,175; 0,225 ... q_min = 0,125; q_step = 0,50 q_valid = q_valid + q_step if not q_order_is_displayed: # An custom order_qty > q_alert q_select_id += 1 selected = "selected" display = offer_item.get_display( qty=q_previous_order, order_unit=offer_item.order_unit, unit_price_amount=a_price, for_order_select=True ) option_dict = {'value': str(q_select_id), 'selected': selected, 'label': display} to_json.append(option_dict) if status == PERMANENCE_OPENED: # _not_lazy string are not placed in the "django.po" other = _("Other qty") option_dict = {'value': 'other_qty', 'selected': EMPTY_STRING, 'label': other} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: option_dict = {'value': '0', 'selected': 'selected', 'label': '---'} to_json.append(option_dict) else: if purchase is not None and purchase.quantity_ordered != DECIMAL_ZERO: option_dict = display_selected_value(offer_item, purchase.quantity_ordered, is_open=True) to_json.append(option_dict) else: closed = _("Closed") option_dict = {'value': '0', 'selected': 'selected', 'label': '%s' % closed} to_json.append(option_dict) return HttpResponse(json.dumps(to_json, cls=DjangoJSONEncoder), content_type="application/json")
def order_ajax(request): if not request.is_ajax(): raise Http404 user = request.user if not user.is_authenticated(): raise Http404 customer = Customer.objects.filter(user_id=user.id, is_active=True, may_order=True) \ .only("id", "vat_id") \ .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)) basket = sboolean(request.GET.get('basket', False)) qs = CustomerInvoice.objects.filter( permanence__offeritem=offer_item_id, customer_id=customer.id, status=PERMANENCE_OPENED).order_by('?') from repanier.apps import REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS if REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS: qs = qs.filter(is_order_confirm_send=False) result = None 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(): result = update_or_create_purchase( customer=customer, offer_item_id=offer_item_id, value_id=value_id, basket=basket ) if result is None: # 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() to_json = [] if purchase is not None: option_dict = display_selected_value( purchase.offer_item, purchase.quantity_ordered) to_json.append(option_dict) else: offer_item = OfferItem.objects.filter( id=offer_item_id ).select_related( "product" ).order_by('?').first() option_dict = display_selected_value( offer_item, DECIMAL_ZERO) to_json.append(option_dict) result = json.dumps(to_json, cls=DjangoJSONEncoder) return HttpResponse(result, content_type="application/json")