Ejemplo n.º 1
0
def close_order_delivery(permanence, delivery, all_producers, producers_id=None):
    today = timezone.now().date()
    getcontext().rounding = ROUND_HALF_UP
    # 0 - Delete unused purchases
    # No need to select : customer_invoice__delivery = delivery
    Purchase.objects.filter(
        permanence_id=permanence.id,
        quantity_ordered=0
    ).order_by('?').delete()
    if repanier.apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
        purchase_qs = Purchase.objects.filter(
            permanence_id=permanence.id,
            customer_invoice__delivery=delivery,
            customer_invoice__is_order_confirm_send=False,
            is_box_content=False
        ).exclude(
            quantity_ordered=DECIMAL_ZERO
        ).order_by('customer_invoice')
        if not all_producers:
            # This may never be the but, but...
            purchase_qs = purchase_qs.filter(producer_id__in=producers_id)
        customer_invoice_id_save = -1
        for purchase in purchase_qs.select_related("customer", "offer_item"):
            if customer_invoice_id_save != purchase.customer_invoice_id:
                customer_invoice_id_save =purchase.customer_invoice_id
                # This order has been cancelled
                # filename = force_filename("%s - %s.xlsx" % (_("Canceled order"), permanence))
                filename = "{0}-{1}.xlsx".format(
                    slugify(_("Canceled order")),
                    slugify(permanence)
                )
                sender_email, sender_function, signature, cc_email_staff = get_signature(
                    is_reply_to_order_email=True)
                export_order_2_1_customer(
                    purchase.customer, filename, permanence, sender_email,
                    sender_function, signature,
                    cancel_order=True)
            update_or_create_purchase(
                customer=purchase.customer,
                offer_item_id=purchase.offer_item.id,
                value_id=DECIMAL_ZERO,
                batch_job=True
            )
    if all_producers:
        # 1 - Do not round to multiple producer_order_by_quantity
        # 2 - Do not add Transport
        membership_fee_product = Product.objects.filter(is_membership_fee=True, is_active=True).order_by('?').first()
        membership_fee_product.producer_unit_price = repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE
        # Update the prices
        membership_fee_product.save()
        membership_fee_offer_item = get_or_create_offer_item(permanence, membership_fee_product.id,
                                                             membership_fee_product.producer_id)
        for customer in Customer.objects.filter(
                is_active=True, may_order=True,
                customerinvoice__permanence_id=permanence.id,
                customerinvoice__delivery=delivery,
                customerinvoice__total_price_with_tax__gt=DECIMAL_ZERO,
                represent_this_buyinggroup=False
        ).order_by('?'):
            # 3 - Add Deposit
            for offer_item in OfferItem.objects.filter(permanence_id=permanence.id,  # is_active=False,
                                                       order_unit=PRODUCT_ORDER_UNIT_DEPOSIT).order_by('?'):
                permanence.producers.add(offer_item.producer_id)
                create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False)
                create_or_update_one_purchase(customer, offer_item, 0, None, True, is_box_content=False)
            # 4 - Add Membership fee Subscription
            if repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION > 0:
                # There is a membership fee
                if customer.membership_fee_valid_until < today:
                    permanence.producers.add(membership_fee_offer_item.producer_id)
                    create_or_update_one_purchase(customer, membership_fee_offer_item, 1, None, True,
                                                  is_box_content=False)
                    customer.membership_fee_valid_until = add_months(
                        customer.membership_fee_valid_until,
                        repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION
                    )
                    customer.save(update_fields=['membership_fee_valid_until', ])
        # 5 - Add Common participation Subscription
        for offer_item in OfferItem.objects.filter(
                permanence_id=permanence.id, is_membership_fee=False,
                order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION).order_by('?'):
            for customer in Customer.objects.filter(is_active=True, may_order=True,
                                                    represent_this_buyinggroup=False).order_by('?'):
                permanence.producers.add(offer_item.producer_id)
                create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False)
    # 6 - Refresh the Purchase 'sum'
    recalculate_order_amount(
        permanence_id=permanence.id,
        all_producers=all_producers,
        producers_id=producers_id,
        send_to_producer=False
    )
    delivery.set_status(PERMANENCE_CLOSED, all_producers, producers_id)
Ejemplo n.º 2
0
def close_order(permanence, all_producers, producers_id=None):
    getcontext().rounding = ROUND_HALF_UP
    today = timezone.now().date()

    if repanier.apps.REPANIER_SETTINGS_CUSTOMERS_MUST_CONFIRM_ORDERS:
        # 0 - Cancel unconfirmed purchases
        purchase_qs = Purchase.objects.filter(
            permanence_id=permanence.id,
            customer_invoice__is_order_confirm_send=False,
            is_box_content=False
        ).exclude(
            quantity_ordered=DECIMAL_ZERO
        ).order_by('customer_invoice')
        if not all_producers:
            purchase_qs = purchase_qs.filter(producer_id__in=producers_id)
        customer_invoice_id_save = -1
        for purchase in purchase_qs.select_related("customer", "offer_item"):
            if customer_invoice_id_save != purchase.customer_invoice_id:
                customer_invoice_id_save = purchase.customer_invoice_id
                # This order has been cancelled
                # filename = force_filename("%s - %s.xlsx" % (_("Canceled order"), permanence))
                filename = "{0}-{1}.xlsx".format(
                    slugify(_("Canceled order")),
                    slugify(permanence)
                )
                sender_email, sender_function, signature, cc_email_staff = get_signature(
                    is_reply_to_order_email=True)
                export_order_2_1_customer(
                    purchase.customer, filename, permanence, sender_email,
                    sender_function, signature,
                    cancel_order=True)
            update_or_create_purchase(
                customer=purchase.customer,
                offer_item_id=purchase.offer_item.id,
                value_id=DECIMAL_ZERO,
                batch_job=True
            )
    else:
        # 0 - Delete unused purchases
        purchase_qs = Purchase.objects.filter(
            permanence_id=permanence.id,
            quantity_ordered=0
        ).order_by('?')
        if not all_producers:
            purchase_qs = purchase_qs.filter(producer_id__in=producers_id)
        purchase_qs.delete()
    # 1 - Round to multiple producer_order_by_quantity
    offer_item_qs = OfferItem.objects.filter(
        permanence_id=permanence.id,
        is_active=True,
        order_unit__lt=PRODUCT_ORDER_UNIT_DEPOSIT,
        producer_order_by_quantity__gt=1,
        quantity_invoiced__gt=0
    ).order_by('?')
    if not all_producers:
        offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id)
    for offer_item in offer_item_qs:
        # It's possible to round the ordered qty even If we do not manage stock
        if offer_item.manage_replenishment:
            needed = (offer_item.quantity_invoiced - offer_item.stock)
        else:
            needed = offer_item.quantity_invoiced
        if needed > DECIMAL_ZERO:
            offer_item.add_2_stock = offer_item.producer_order_by_quantity - (
            needed % offer_item.producer_order_by_quantity)
            offer_item.save()
    # 2 - Add Transport
    offer_item_qs = OfferItem.objects.filter(
        permanence_id=permanence.id,
        is_active=False,
        order_unit=PRODUCT_ORDER_UNIT_TRANSPORTATION
    ).order_by('?')
    if not all_producers:
        offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id)
    for offer_item in offer_item_qs:
        buying_group = Customer.objects.filter(is_active=True, represent_this_buyinggroup=True).order_by('?').first()
        create_or_update_one_purchase(buying_group, offer_item, 1, None, True, is_box_content=False)
    membership_fee_product = Product.objects.filter(is_membership_fee=True, is_active=True).order_by('?').first()
    membership_fee_product.producer_unit_price = repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE
    # Update the prices
    membership_fee_product.save()
    membership_fee_offer_item = get_or_create_offer_item(
        permanence, membership_fee_product.id,
        membership_fee_product.producer_id
    )
    for customer in Customer.objects.filter(
            is_active=True,
            may_order=True,
            customerinvoice__permanence_id=permanence.id,
            customerinvoice__total_price_with_tax__gt=DECIMAL_ZERO,
            represent_this_buyinggroup=False
    ).order_by('?'):
        # 3 - Add Deposit
        offer_item_qs = OfferItem.objects.filter(
            permanence_id=permanence.id,
            order_unit=PRODUCT_ORDER_UNIT_DEPOSIT
        ).order_by('?')
        if not all_producers:
            offer_item_qs = offer_item_qs.filter(producer_id__in=producers_id)
        for offer_item in offer_item_qs:
            create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False)
            create_or_update_one_purchase(customer, offer_item, 0, None, True, is_box_content=False)
        # 4 - Add Add Membership fee Subscription
        if repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION > 0:
            # There is a membership fee
            if customer.membership_fee_valid_until < today:
                permanence.producers.add(membership_fee_offer_item.producer_id)
                create_or_update_one_purchase(customer, membership_fee_offer_item, 1, None, True, is_box_content=False)
                while customer.membership_fee_valid_until < today:
                    # Do not pay the membership fee if no order passed during a certain amount of time
                    customer.membership_fee_valid_until = add_months(
                        customer.membership_fee_valid_until,
                        repanier.apps.REPANIER_SETTINGS_MEMBERSHIP_FEE_DURATION
                    )
                customer.save(update_fields=['membership_fee_valid_until', ])

    # 5 - Add Common participation Subscription
    if all_producers:
        for offer_item in OfferItem.objects.filter(
                permanence_id=permanence.id, is_membership_fee=False,
                order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION
        ).order_by('?'):
            for customer in Customer.objects.filter(
                    is_active=True, may_order=True,
                    represent_this_buyinggroup=False
            ).order_by('?'):
                permanence.producers.add(offer_item.producer_id)
                create_or_update_one_purchase(customer, offer_item, 1, None, True, is_box_content=False)
        # Disable subscription for next permanence
        Product.objects.filter(
            order_unit=PRODUCT_ORDER_UNIT_SUBSCRIPTION, is_into_offer=True, is_membership_fee=False
        ).order_by('?').update(is_into_offer=False)

        permanence.set_status(PERMANENCE_CLOSED, allow_downgrade=False)
        if not repanier.apps.REPANIER_SETTINGS_INVOICE and repanier.apps.REPANIER_SETTINGS_BANK_ACCOUNT is None:
            # No Invoice and no bank_account --> auto archive
            # Put send permanences to the done status, because they will "never" be invoiced
            for permanence in Permanence.objects.filter(status=PERMANENCE_SEND):
                permanence.set_status(PERMANENCE_ARCHIVED, update_payment_date=True)
    else:
        permanence.set_status(PERMANENCE_CLOSED, all_producers=all_producers, producers_id=producers_id)
    # 6 - Refresh the Purchase 'sum' for each customer
    recalculate_order_amount(
        permanence_id=permanence.id,
        all_producers=all_producers,
        producers_id=producers_id,
        send_to_producer=False,
    )
Ejemplo n.º 3
0
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")