Exemplo n.º 1
0
 def get_long_name(self, is_quantity_invoiced=False, customer_price=True, box_unicode=BOX_UNICODE):
     if self.is_box:
         # To avoid unicode error in email_offer.send_open_order
         qty_display = box_unicode
     else:
         if is_quantity_invoiced and self.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
             qty_display = get_display(
                 qty=1,
                 order_average_weight=self.order_average_weight,
                 order_unit=PRODUCT_ORDER_UNIT_KG,
                 for_customer=False,
                 without_price_display=True
             )
         else:
             qty_display = get_display(
                 qty=1,
                 order_average_weight=self.order_average_weight,
                 order_unit=self.order_unit,
                 for_customer=False,
                 without_price_display=True
             )
     unit_price = self.get_unit_price(customer_price=customer_price)
     if self.unit_deposit.amount > DECIMAL_ZERO:
         return '%s %s, %s + ♻ %s' % (self.long_name, qty_display, unit_price, self.unit_deposit)
     else:
         return '%s %s, %s' % (self.long_name, qty_display, unit_price)
Exemplo n.º 2
0
 def get_long_name(self, box_unicode=BOX_UNICODE, customer_price=True):
     if self.id:
         if self.is_box:
             # To avoid unicode error when print
             qty_display = box_unicode
         else:
             qty_display = get_display(
                 qty=1,
                 order_average_weight=self.order_average_weight,
                 order_unit=self.order_unit,
                 for_customer=False,
                 without_price_display=True
             )
         unit_price = self.get_unit_price(customer_price=customer_price)
         unit_deposit = self.unit_deposit
         if len(qty_display) > 0:
             if unit_deposit.amount > DECIMAL_ZERO:
                 return '%s %s, %s ♻ %s' % (self.long_name, qty_display, unit_price, unit_deposit)
             else:
                 return '%s %s, %s' % (self.long_name, qty_display, unit_price)
         else:
             if unit_deposit.amount > DECIMAL_ZERO:
                 return '%s, %s ♻ %s' % (self.long_name, unit_price, unit_deposit)
             else:
                 return '%s, %s' % (self.long_name, unit_price)
     else:
         raise AttributeError
Exemplo n.º 3
0
 def get_order_name(self, is_quantity_invoiced=False, box_unicode=BOX_UNICODE):
     if self.is_box:
         # To avoid unicode error in email_offer.send_open_order
         qty_display = box_unicode
     else:
         if is_quantity_invoiced and self.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
             qty_display = get_display(
                 qty=1,
                 order_average_weight=self.order_average_weight,
                 order_unit=PRODUCT_ORDER_UNIT_KG,
                 for_customer=False,
                 without_price_display=True
             )
         else:
             qty_display = get_display(
                 qty=1,
                 order_average_weight=self.order_average_weight,
                 order_unit=self.order_unit,
                 for_customer=False,
                 without_price_display=True
             )
     return '%s %s' % (self.long_name, qty_display)
Exemplo n.º 4
0
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")