Commit a0d059b9 authored by Patrick's avatar Patrick

Code cleanup

parent 3d3fb946
......@@ -13,6 +13,7 @@ from repanier.admin.fkey_choice_cache_mixin import ForeignKeyCacheMixin
from repanier.const import *
from repanier.fields.RepanierMoneyField import FormMoneyField, RepanierMoney
from repanier.models import Customer, Permanence, Producer, OfferItemSend, Purchase
from repanier.tools import rule_of_3_reload_purchase
class CustomerPurchaseSendInlineFormSet(BaseInlineFormSet):
......@@ -216,33 +217,8 @@ class CustomerSendAdmin(admin.ModelAdmin):
if offer_item is None:
purchase_form.repanier_is_valid = False
else:
purchase_form.repanier_is_valid = True
# Reload purchase, because it has maybe be deleted
purchase = Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=offer_item.id,
is_box_content=False
).order_by('?').first()
if purchase is None:
# Doesn't exists ? Create one
purchase = Purchase.objects.create(
permanence=offer_item.permanence,
permanence_date=offer_item.permanence.permanence_date,
offer_item=offer_item,
producer=offer_item.producer,
customer=customer,
quantity_ordered=DECIMAL_ZERO,
quantity_invoiced=DECIMAL_ZERO,
comment=purchase_form_instance.comment,
is_box_content=False,
status=PERMANENCE_SEND
)
# And set the form's values
purchase.quantity_invoiced = purchase_form_instance.quantity_invoiced
purchase.purchase_price = purchase_form_instance.purchase_price
purchase.comment = purchase_form_instance.comment
# Set it as new form instance
purchase_form.instance = purchase
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:
......
......@@ -18,6 +18,7 @@ from repanier.admin.fkey_choice_cache_mixin import ForeignKeyCacheMixin
from repanier.const import *
from repanier.fields.RepanierMoneyField import FormMoneyField, RepanierMoney
from repanier.models import Customer, Permanence, Product, LUT_DepartmentForCustomer, Purchase, OfferItem
from repanier.tools import rule_of_3_reload_purchase
class OfferItemPurchaseSendInlineFormSet(BaseInlineFormSet):
......@@ -315,33 +316,8 @@ class OfferItemSendAdmin(admin.ModelAdmin):
if customer is None:
purchase_form.repanier_is_valid = False
else:
purchase_form.repanier_is_valid = True
# Reload purchase, because it has maybe be deleted
purchase = Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=offer_item.id,
is_box_content=False
).order_by('?').first()
if purchase is None:
# Doesn't exists ? Create one
purchase = Purchase.objects.create(
permanence=offer_item.permanence,
permanence_date=offer_item.permanence.permanence_date,
offer_item=offer_item,
producer=offer_item.producer,
customer=customer,
quantity_ordered=DECIMAL_ZERO,
quantity_invoiced=DECIMAL_ZERO,
comment=purchase_form_instance.comment,
is_box_content=False,
status=PERMANENCE_SEND
)
# And set the form's values
purchase.quantity_invoiced = purchase_form_instance.quantity_invoiced
purchase.purchase_price = purchase_form_instance.purchase_price
purchase.comment = purchase_form_instance.comment
# Set it as new form instance
purchase_form.instance = purchase
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
......
......@@ -237,7 +237,7 @@
<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">{% trans "Close" %}</button>
<script>
t_row.find('span[id=get_long_name]').html('{{ offer_item.get_long_name | safe }}{% if offer_item.picture2 %}<img class="img-responsive img-rounded" style = "float:right; margin:5px; min-height:32px; min-width:32px; max-height:48px; max-width:48px;" src="{{ MEDIA_URL }}{{ offer_item.picture2 }}"/>{% endif %}');
row = '{{ offer_item.producer_unit_price_wo_tax }}'
row = '{{ offer_item.producer_unit_price_wo_tax }}';
{% if offer_item.order_unit == "140" %}
row += '&nbsp;/&nbsp;({% trans "kg" %})';
{% endif %}
......
......@@ -1611,3 +1611,33 @@ def get_full_status_display(permanence):
else:
return mark_safe('<div class="wrap-text">%s</div>' % message)
def rule_of_3_reload_purchase(customer, offer_item, purchase_form, purchase_form_instance):
purchase_form.repanier_is_valid = True
# Reload purchase, because it has maybe be deleted
purchase = models.Purchase.objects.filter(
customer_id=customer.id,
offer_item_id=offer_item.id,
is_box_content=False
).order_by('?').first()
if purchase is None:
# Doesn't exists ? Create one
purchase = models.Purchase.objects.create(
permanence=offer_item.permanence,
permanence_date=offer_item.permanence.permanence_date,
offer_item=offer_item,
producer=offer_item.producer,
customer=customer,
quantity_ordered=DECIMAL_ZERO,
quantity_invoiced=DECIMAL_ZERO,
comment=purchase_form_instance.comment,
is_box_content=False,
status=PERMANENCE_SEND
)
# And set the form's values
purchase.quantity_invoiced = purchase_form_instance.quantity_invoiced
purchase.purchase_price = purchase_form_instance.purchase_price
purchase.comment = purchase_form_instance.comment
# Set it as new form instance
purchase_form.instance = purchase
return purchase
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment