Esempio n. 1
0
 def save_box(self):
     if self.offer_item.is_box:
         for content in BoxContent.objects.filter(box_id=self.offer_item.product_id).order_by('?'):
             content_offer_item = get_or_create_offer_item(self.permanence, content.product_id,
                                                           content.product.producer_id)
             # Select one purchase
             content_purchase = Purchase.objects.filter(
                 customer_id=self.customer_id,
                 offer_item_id=content_offer_item.id,
                 is_box_content=True
             ).order_by('?').first()
             if content_purchase is None:
                 content_purchase = Purchase.objects.create(
                     permanence=self.permanence,
                     permanence_date=self.permanence.permanence_date,
                     offer_item=content_offer_item,
                     producer=self.producer,
                     customer=self.customer,
                     quantity_ordered=self.quantity_ordered * content.content_quantity,
                     quantity_invoiced=self.quantity_invoiced * content.content_quantity,
                     is_box_content=True,
                     status=self.status
                 )
             else:
                 content_purchase.status = self.status
                 content_purchase.quantity_ordered = self.quantity_ordered * content.content_quantity
                 content_purchase.quantity_invoiced = self.quantity_invoiced * content.content_quantity
                 content_purchase.save()
             content_purchase.permanence.producers.add(content_offer_item.producer)
Esempio 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,
    )
Esempio n. 3
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)