Example #1
0
    def save_related(self, request, form, formsets, change):
        getcontext().rounding = ROUND_HALF_UP
        for formset in formsets:
            # option.py -> construct_change_message doesn't test the presence of those array not created at form initialisation...
            if not hasattr(formset, 'new_objects'): formset.new_objects = []
            if not hasattr(formset, 'changed_objects'): formset.changed_objects = []
            if not hasattr(formset, 'deleted_objects'): formset.deleted_objects = []
        offer_item = OfferItem.objects.filter(id=form.instance.id).order_by('?').first()
        formset = formsets[0]
        for purchase_form in formset:
            purchase = purchase_form.instance
            previous_customer = purchase_form.fields['previous_customer'].initial
            if previous_customer is not None and previous_customer != purchase.customer:
                # Delete the purchase because the customer has changed
                purchase = Purchase.objects.filter(
                    customer_id=previous_customer.id,
                    offer_item_id=offer_item.id,
                    is_box_content=False
                ).order_by('?').first()
                if purchase is not None:
                    purchase.quantity_invoiced = DECIMAL_ZERO
                    purchase.save()
                    purchase.save_box()
        for purchase_form in formset:
            purchase_form_instance = purchase_form.instance
            customer = purchase_form_instance.customer
            if customer is None:
                purchase_form.repanier_is_valid = False
            else:
                purchase = rule_of_3_reload_purchase(customer, offer_item, purchase_form,
                                                     purchase_form_instance)
                if offer_item.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG, PRODUCT_ORDER_UNIT_LT]:
                    purchase_price = purchase.purchase_price
                    previous_purchase_price = purchase_form.fields['previous_purchase_price'].initial
                else:
                    purchase_price = previous_purchase_price = REPANIER_MONEY_ZERO
                if purchase_price != previous_purchase_price:
                    purchase.purchase_price = purchase_price
                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                        purchase.quantity_invoiced = (purchase_price.amount / offer_item.producer_unit_price.amount) \
                            .quantize(FOUR_DECIMALS)
                    else:
                        purchase.quantity_invoiced = DECIMAL_ZERO
                else:
                    purchase.purchase_price.amount = (
                    purchase.quantity_invoiced * offer_item.producer_unit_price.amount) \
                        .quantize(TWO_DECIMALS)

        if not offer_item.wrapped and offer_item.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG]:
            rule_of_3 = form.cleaned_data['rule_of_3']
            if rule_of_3:
                rule_of_3_target = form.cleaned_data['offer_purchase_price'].amount.quantize(TWO_DECIMALS)
                rule_of_3_source = DECIMAL_ZERO
                max_purchase_counter = 0
                for purchase_form in formset:
                    if purchase_form.repanier_is_valid:
                        rule_of_3_source += purchase_form.instance.purchase_price.amount
                        max_purchase_counter += 1
                if rule_of_3_target is not None and rule_of_3_target != rule_of_3_source:
                    if rule_of_3_source != DECIMAL_ZERO:
                        ratio = rule_of_3_target / rule_of_3_source
                    else:
                        if rule_of_3_target == DECIMAL_ZERO:
                            ratio = DECIMAL_ZERO
                        else:
                            ratio = DECIMAL_ONE
                    # Rule of 3
                    if ratio != DECIMAL_ONE:
                        adjusted_invoice = DECIMAL_ZERO
                        i = 0
                        for purchase_form in formset:
                            if purchase_form.repanier_is_valid:
                                i += 1
                                purchase = purchase_form.instance
                                if i == max_purchase_counter:
                                    delta = rule_of_3_target - adjusted_invoice
                                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                                        purchase.quantity_invoiced = (
                                            delta / offer_item.producer_unit_price.amount).quantize(FOUR_DECIMALS)
                                    else:
                                        purchase.quantity_invoiced = DECIMAL_ZERO
                                else:
                                    purchase.quantity_invoiced = (purchase.quantity_invoiced * ratio).quantize(
                                        FOUR_DECIMALS)
                                    adjusted_invoice += (
                                        purchase.quantity_invoiced * offer_item.producer_unit_price.amount).quantize(
                                        TWO_DECIMALS)
                                purchase.save()
                                purchase.save_box()

        for purchase_form in formset:
            if purchase_form.has_changed() and purchase_form.repanier_is_valid:
                purchase_form.instance.save()
                purchase_form.instance.save_box()

        # Important : linked with ^^^^^
        offer_item.permanence.recalculate_order_amount(
            offer_item_qs=OfferItem.objects.filter(id=offer_item.id).order_by('?')
        )

        if offer_item.manage_replenishment:
            # qty_delivered and stock are displayed in the form
            offer_item = OfferItem.objects.filter(id=offer_item.id).order_by('?').first()
            qty_delivered = form.cleaned_data.get('qty_delivered', DECIMAL_ZERO).quantize(THREE_DECIMALS)
            result_set = Purchase.objects.filter(
                offer_item_id=offer_item.id,
                is_box_content=False
            ).order_by('?').aggregate(
                Sum('quantity_invoiced')
            )
            if result_set["quantity_invoiced__sum"] is not None:
                qty_invoiced = result_set["quantity_invoiced__sum"]
            else:
                qty_invoiced = DECIMAL_ZERO
            taken_from_stock = qty_invoiced if qty_invoiced < offer_item.stock else offer_item.stock
            new_add_2_stock = taken_from_stock + qty_delivered - qty_invoiced
            if new_add_2_stock < DECIMAL_ZERO:
                new_add_2_stock = DECIMAL_ZERO
            # Important : go through offer_item_post_init and offer_item_pre_save
            offer_item.add_2_stock = new_add_2_stock
            offer_item.save()
    def save_related(self, request, form, formsets, change):
        for formset in formsets:
            # option.py -> construct_change_message doesn't test the presence of those array not created at form initialisation...
            if not hasattr(formset, 'new_objects'):
                formset.new_objects = []
            if not hasattr(formset, 'changed_objects'):
                formset.changed_objects = []
            if not hasattr(formset, 'deleted_objects'):
                formset.deleted_objects = []
        offer_item = OfferItem.objects.filter(
            id=form.instance.id).order_by('?').first()
        formset = formsets[0]
        for purchase_form in formset:
            purchase = purchase_form.instance
            previous_customer = purchase_form.fields[
                'previous_customer'].initial
            if previous_customer is not None and previous_customer != purchase.customer:
                # Delete the purchase because the customer has changed
                purchase = Purchase.objects.filter(
                    customer_id=previous_customer.id,
                    offer_item_id=offer_item.id,
                    is_box_content=False).order_by('?').first()
                if purchase is not None:
                    purchase.quantity_invoiced = DECIMAL_ZERO
                    purchase.save()
                    purchase.save_box()
        for purchase_form in formset:
            purchase_form_instance = purchase_form.instance
            try:
                customer = purchase_form_instance.customer
            except Customer.DoesNotExist:  # RelatedObjectDoesNotExist
                customer = None
            if customer is None:
                purchase_form.repanier_is_valid = False
            else:
                purchase = rule_of_3_reload_purchase(customer, offer_item,
                                                     purchase_form,
                                                     purchase_form_instance)
                if offer_item.order_unit in [
                        PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG,
                        PRODUCT_ORDER_UNIT_LT
                ]:
                    purchase_price = purchase.purchase_price
                    previous_purchase_price = purchase_form.fields[
                        'previous_purchase_price'].initial
                else:
                    purchase_price = previous_purchase_price = REPANIER_MONEY_ZERO
                if purchase_price != previous_purchase_price:
                    purchase.purchase_price = purchase_price
                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                        purchase.quantity_invoiced = (purchase_price.amount / offer_item.producer_unit_price.amount) \
                            .quantize(FOUR_DECIMALS)
                    else:
                        purchase.quantity_invoiced = DECIMAL_ZERO
                else:
                    purchase.purchase_price.amount = (
                            purchase.quantity_invoiced * offer_item.producer_unit_price.amount) \
                        .quantize(TWO_DECIMALS)

        if not offer_item.wrapped and offer_item.order_unit in [
                PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG
        ]:
            rule_of_3 = form.cleaned_data['rule_of_3']
            if rule_of_3:
                rule_of_3_target = form.cleaned_data[
                    'offer_purchase_price'].amount.quantize(TWO_DECIMALS)
                rule_of_3_source = DECIMAL_ZERO
                max_purchase_counter = 0
                for purchase_form in formset:
                    if purchase_form.repanier_is_valid:
                        rule_of_3_source += purchase_form.instance.purchase_price.amount
                        max_purchase_counter += 1
                if rule_of_3_target is not None and rule_of_3_target != rule_of_3_source:
                    if rule_of_3_source != DECIMAL_ZERO:
                        ratio = rule_of_3_target / rule_of_3_source
                    else:
                        if rule_of_3_target == DECIMAL_ZERO:
                            ratio = DECIMAL_ZERO
                        else:
                            ratio = DECIMAL_ONE
                    # Rule of 3
                    if ratio != DECIMAL_ONE:
                        adjusted_invoice = DECIMAL_ZERO
                        i = 0
                        for purchase_form in formset:
                            if purchase_form.repanier_is_valid:
                                i += 1
                                purchase = purchase_form.instance
                                if i == max_purchase_counter:
                                    delta = rule_of_3_target - adjusted_invoice
                                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                                        purchase.quantity_invoiced = (
                                            delta /
                                            offer_item.producer_unit_price.
                                            amount).quantize(FOUR_DECIMALS)
                                    else:
                                        purchase.quantity_invoiced = DECIMAL_ZERO
                                else:
                                    purchase.quantity_invoiced = (
                                        purchase.quantity_invoiced *
                                        ratio).quantize(FOUR_DECIMALS)
                                    adjusted_invoice += (
                                        purchase.quantity_invoiced *
                                        offer_item.producer_unit_price.amount
                                    ).quantize(TWO_DECIMALS)
                                purchase.save()
                                purchase.save_box()

        for purchase_form in formset:
            if purchase_form.has_changed() and purchase_form.repanier_is_valid:
                purchase_form.instance.save()
                purchase_form.instance.save_box()

        # Important : linked with ^^^^^
        offer_item.permanence.recalculate_order_amount(
            offer_item_qs=OfferItem.objects.filter(
                id=offer_item.id).order_by('?'))
Example #3
0
    def save_related(self, request, form, formsets, change):
        for formset in formsets:
            # option.py -> construct_change_message doesn't test the presence of those array not created at form initialisation...
            if not hasattr(formset, 'new_objects'): formset.new_objects = []
            if not hasattr(formset, 'changed_objects'): formset.changed_objects = []
            if not hasattr(formset, 'deleted_objects'): formset.deleted_objects = []
        customer_producer_invoice = form.instance
        customer = customer_producer_invoice.customer
        formset = formsets[0]
        for purchase_form in formset:
            purchase = purchase_form.instance
            previous_offer_item = purchase_form.fields['previous_offer_item'].initial
            if previous_offer_item is not None and previous_offer_item != purchase.offer_item:
                # Delete the purchase because the offer_item has changed
                purchase = Purchase.objects.filter(
                    customer_id=customer.id,
                    offer_item_id=previous_offer_item.id,
                    is_box_content=False
                ).order_by('?').first()
                if purchase is not None:
                    purchase.quantity_invoiced = DECIMAL_ZERO
                    purchase.save()
                    purchase.save_box()
        for purchase_form in formset:
            purchase_form_instance = purchase_form.instance
            try:
                offer_item = purchase_form_instance.offer_item
            except OfferItem.DoesNotExist:  # RelatedObjectDoesNotExist
                offer_item = None
            if offer_item is None:
                purchase_form.repanier_is_valid = False
            else:
                purchase = rule_of_3_reload_purchase(customer, offer_item, purchase_form,
                                                     purchase_form_instance)
                previous_purchase_price = purchase_form.fields['previous_purchase_price'].initial
                if purchase.purchase_price != previous_purchase_price:
                    if purchase.get_producer_unit_price() != DECIMAL_ZERO:
                        purchase.quantity_invoiced = (
                                purchase.purchase_price.amount / purchase.get_producer_unit_price()) \
                            .quantize(FOUR_DECIMALS)
                    else:
                        purchase.quantity_invoiced = DECIMAL_ZERO
                purchase.save()
        rule_of_3 = form.cleaned_data['rule_of_3']
        if rule_of_3:
            if customer_producer_invoice.producer.price_list_multiplier >= DECIMAL_ONE:
                rule_of_3_target = form.cleaned_data['offer_purchase_price'].amount.quantize(TWO_DECIMALS)
                selling_price = False
            else:
                rule_of_3_target = form.cleaned_data['offer_selling_price'].amount.quantize(
                    TWO_DECIMALS)
                selling_price = True
            rule_of_3_source = DECIMAL_ZERO
            max_purchase_counter = 0
            for purchase_form in formset:
                if purchase_form.repanier_is_valid:
                    if selling_price:
                        rule_of_3_source += purchase_form.instance.selling_price.amount
                    else:
                        rule_of_3_source += purchase_form.instance.purchase_price.amount
                    max_purchase_counter += 1
            if rule_of_3_target is not None and rule_of_3_target != rule_of_3_source:
                if rule_of_3_source != DECIMAL_ZERO:
                    ratio = (rule_of_3_target / rule_of_3_source).quantize(FOUR_DECIMALS)
                else:
                    if rule_of_3_target == DECIMAL_ZERO:
                        ratio = DECIMAL_ZERO
                    else:
                        ratio = DECIMAL_ONE
                if ratio != DECIMAL_ONE:
                    adjusted_invoice = DECIMAL_ZERO
                    i = 0
                    for purchase_form in formset:
                        if purchase_form.repanier_is_valid:
                            i += 1
                            purchase = purchase_form.instance
                            if i == max_purchase_counter:
                                if purchase.get_producer_unit_price() != DECIMAL_ZERO:
                                    delta = rule_of_3_target - adjusted_invoice
                                    if selling_price:
                                        purchase.quantity_invoiced = (
                                                delta / purchase.get_customer_unit_price()).quantize(FOUR_DECIMALS)
                                    else:
                                        purchase.quantity_invoiced = (
                                                delta / purchase.get_producer_unit_price()).quantize(FOUR_DECIMALS)
                                else:
                                    purchase.quantity_invoiced = DECIMAL_ZERO
                            else:
                                purchase.quantity_invoiced = (purchase.quantity_invoiced * ratio).quantize(
                                    FOUR_DECIMALS)

                            purchase.save()
                            purchase.save_box()
                            if selling_price:
                                adjusted_invoice += purchase.selling_price.amount
                            else:
                                adjusted_invoice += purchase.purchase_price.amount
        for purchase_form in formset:
            if purchase_form.has_changed() and purchase_form.repanier_is_valid:
                purchase_form.instance.save()
                purchase_form.instance.save_box()
    def save_related(self, request, form, formsets, change):
        for formset in formsets:
            # option.py -> construct_change_message doesn't test the presence of those array not created at form initialisation...
            if not hasattr(formset, 'new_objects'): formset.new_objects = []
            if not hasattr(formset, 'changed_objects'): formset.changed_objects = []
            if not hasattr(formset, 'deleted_objects'): formset.deleted_objects = []
        offer_item = OfferItem.objects.filter(id=form.instance.id).order_by('?').first()
        formset = formsets[0]
        for purchase_form in formset:
            purchase = purchase_form.instance
            previous_customer = purchase_form.fields['previous_customer'].initial
            if previous_customer is not None and previous_customer != purchase.customer:
                # Delete the purchase because the customer has changed
                purchase = Purchase.objects.filter(
                    customer_id=previous_customer.id,
                    offer_item_id=offer_item.id,
                    is_box_content=False
                ).order_by('?').first()
                if purchase is not None:
                    purchase.quantity_invoiced = DECIMAL_ZERO
                    purchase.save()
                    purchase.save_box()
        for purchase_form in formset:
            purchase_form_instance = purchase_form.instance
            try:
                customer = purchase_form_instance.customer
            except Customer.DoesNotExist:  # RelatedObjectDoesNotExist
                customer = None
            if customer is None:
                purchase_form.repanier_is_valid = False
            else:
                purchase = rule_of_3_reload_purchase(customer, offer_item, purchase_form,
                                                     purchase_form_instance)
                if offer_item.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG, PRODUCT_ORDER_UNIT_LT]:
                    purchase_price = purchase.purchase_price
                    previous_purchase_price = purchase_form.fields['previous_purchase_price'].initial
                else:
                    purchase_price = previous_purchase_price = REPANIER_MONEY_ZERO
                if purchase_price != previous_purchase_price:
                    purchase.purchase_price = purchase_price
                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                        purchase.quantity_invoiced = (purchase_price.amount / offer_item.producer_unit_price.amount) \
                            .quantize(FOUR_DECIMALS)
                    else:
                        purchase.quantity_invoiced = DECIMAL_ZERO
                else:
                    purchase.purchase_price.amount = (
                            purchase.quantity_invoiced * offer_item.producer_unit_price.amount) \
                        .quantize(TWO_DECIMALS)

        if not offer_item.wrapped and offer_item.order_unit in [PRODUCT_ORDER_UNIT_KG, PRODUCT_ORDER_UNIT_PC_KG]:
            rule_of_3 = form.cleaned_data['rule_of_3']
            if rule_of_3:
                rule_of_3_target = form.cleaned_data['offer_purchase_price'].amount.quantize(TWO_DECIMALS)
                rule_of_3_source = DECIMAL_ZERO
                max_purchase_counter = 0
                for purchase_form in formset:
                    if purchase_form.repanier_is_valid:
                        rule_of_3_source += purchase_form.instance.purchase_price.amount
                        max_purchase_counter += 1
                if rule_of_3_target is not None and rule_of_3_target != rule_of_3_source:
                    if rule_of_3_source != DECIMAL_ZERO:
                        ratio = rule_of_3_target / rule_of_3_source
                    else:
                        if rule_of_3_target == DECIMAL_ZERO:
                            ratio = DECIMAL_ZERO
                        else:
                            ratio = DECIMAL_ONE
                    # Rule of 3
                    if ratio != DECIMAL_ONE:
                        adjusted_invoice = DECIMAL_ZERO
                        i = 0
                        for purchase_form in formset:
                            if purchase_form.repanier_is_valid:
                                i += 1
                                purchase = purchase_form.instance
                                if i == max_purchase_counter:
                                    delta = rule_of_3_target - adjusted_invoice
                                    if offer_item.producer_unit_price.amount != DECIMAL_ZERO:
                                        purchase.quantity_invoiced = (
                                                delta / offer_item.producer_unit_price.amount).quantize(FOUR_DECIMALS)
                                    else:
                                        purchase.quantity_invoiced = DECIMAL_ZERO
                                else:
                                    purchase.quantity_invoiced = (purchase.quantity_invoiced * ratio).quantize(
                                        FOUR_DECIMALS)
                                    adjusted_invoice += (
                                            purchase.quantity_invoiced * offer_item.producer_unit_price.amount).quantize(
                                        TWO_DECIMALS)
                                purchase.save()
                                purchase.save_box()

        for purchase_form in formset:
            if purchase_form.has_changed() and purchase_form.repanier_is_valid:
                purchase_form.instance.save()
                purchase_form.instance.save_box()

        # Important : linked with ^^^^^
        offer_item.permanence.recalculate_order_amount(
            offer_item_qs=OfferItem.objects.filter(id=offer_item.id).order_by('?')
        )

        if offer_item.manage_replenishment:
            # qty_delivered and stock are displayed in the form
            offer_item = OfferItem.objects.filter(id=offer_item.id).order_by('?').first()
            qty_delivered = form.cleaned_data.get('qty_delivered', DECIMAL_ZERO).quantize(THREE_DECIMALS)
            result_set = Purchase.objects.filter(
                offer_item_id=offer_item.id,
                is_box_content=False
            ).order_by('?').aggregate(
                Sum('quantity_invoiced')
            )
            if result_set["quantity_invoiced__sum"] is not None:
                qty_invoiced = result_set["quantity_invoiced__sum"]
            else:
                qty_invoiced = DECIMAL_ZERO
            taken_from_stock = qty_invoiced if qty_invoiced < offer_item.stock else offer_item.stock
            new_add_2_stock = taken_from_stock + qty_delivered - qty_invoiced
            if new_add_2_stock < DECIMAL_ZERO:
                new_add_2_stock = DECIMAL_ZERO
            # Important : go through offer_item_post_init and offer_item_pre_save
            offer_item.add_2_stock = new_add_2_stock
            offer_item.save()