Commit 8975f63a authored by Patrick's avatar Patrick

First returns

parent f4a2f224
...@@ -92,7 +92,7 @@ class PermanenceDoneAdmin(TranslatableAdmin): ...@@ -92,7 +92,7 @@ class PermanenceDoneAdmin(TranslatableAdmin):
def add_delivery(self, request): def add_delivery(self, request):
return import_xslx_view( return import_xslx_view(
self, admin, request, None, _("Import an invoice"), self, admin, request, None, _("Import an invoice"),
handle_uploaded_invoice, action='import_xlsx', form_klass=ImportInvoiceForm handle_uploaded_invoice, action='add_delivery', form_klass=ImportInvoiceForm
) )
def cancel_delivery(self, request, permanence_qs): def cancel_delivery(self, request, permanence_qs):
......
...@@ -42,7 +42,11 @@ class CustomerPurchaseSendInlineForm(forms.ModelForm): ...@@ -42,7 +42,11 @@ class CustomerPurchaseSendInlineForm(forms.ModelForm):
super(CustomerPurchaseSendInlineForm, self).__init__(*args, **kwargs) super(CustomerPurchaseSendInlineForm, self).__init__(*args, **kwargs)
purchase = self.instance purchase = self.instance
self.fields["previous_purchase_price"].initial = purchase.purchase_price self.fields["previous_purchase_price"].initial = purchase.purchase_price
self.fields["previous_offer_item"].initial = purchase.offer_item try:
offer_item = purchase.offer_item
except AttributeError:
offer_item = None
self.fields["previous_offer_item"].initial = offer_item
class Meta: class Meta:
widgets = { widgets = {
......
...@@ -18,7 +18,7 @@ class Command(BaseCommand): ...@@ -18,7 +18,7 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
recalculate_order_amount( recalculate_order_amount(
permanence_id=60, permanence_id=32,
re_init=True re_init=True
) )
......
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.db.models import Sum
from repanier.models import BankAccount
from repanier.models import LUT_DeliveryPoint, DeliveryBoard
from repanier.models import CustomerInvoice, ProducerInvoice
from repanier.const import PERMANENCE_CLOSED, \
PERMANENCE_INVOICED, PERMANENCE_ARCHIVED, PERMANENCE_SEND, DECIMAL_ZERO
from repanier.models import Permanence
from repanier.tools import reorder_offer_items, recalculate_order_amount
from repanier.task import task_invoice
class Command(BaseCommand):
args = '<none>'
help = 'Recalculate permanence profit'
def handle(self, *args, **options):
for permanence in Permanence.objects.filter(status__gte=PERMANENCE_CLOSED).order_by('?'):
print ("%s %s" % (permanence.permanence_date, permanence.get_status_display()))
permanence.recalculate_profit()
permanence.save()
...@@ -24,7 +24,7 @@ def permanence_verbose_name(): ...@@ -24,7 +24,7 @@ def permanence_verbose_name():
if DJANGO_IS_MIGRATION_RUNNING: if DJANGO_IS_MIGRATION_RUNNING:
return EMPTY_STRING return EMPTY_STRING
from repanier.apps import REPANIER_SETTINGS_PERMANENCE_NAME from repanier.apps import REPANIER_SETTINGS_PERMANENCE_NAME
return lambda: "%s" % REPANIER_SETTINGS_PERMANENCE_NAME return _('order') # lambda: "%s" % REPANIER_SETTINGS_PERMANENCE_NAME
@python_2_unicode_compatible @python_2_unicode_compatible
...@@ -115,10 +115,6 @@ class CustomerInvoice(models.Model): ...@@ -115,10 +115,6 @@ class CustomerInvoice(models.Model):
help_text=_("This is the minimum order amount to avoid shipping cost."), help_text=_("This is the minimum order amount to avoid shipping cost."),
default=DECIMAL_ZERO, max_digits=5, decimal_places=2, default=DECIMAL_ZERO, max_digits=5, decimal_places=2,
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
# customer_charged = models.ForeignKey(
# 'Customer', verbose_name=_("customer"),
# related_name='invoices_paid',
# on_delete=models.PROTECT, db_index=True)
master_permanence = models.ForeignKey( master_permanence = models.ForeignKey(
'Permanence', verbose_name=_("master permanence"), 'Permanence', verbose_name=_("master permanence"),
related_name='child_customer_invoice', related_name='child_customer_invoice',
...@@ -205,34 +201,33 @@ class CustomerInvoice(models.Model): ...@@ -205,34 +201,33 @@ class CustomerInvoice(models.Model):
self.calculate_and_save_delta_buyinggroup(confirm_order=True) self.calculate_and_save_delta_buyinggroup(confirm_order=True)
self.is_order_confirm_send = True self.is_order_confirm_send = True
@transaction.atomic
def calculate_and_save_delta_buyinggroup(self, confirm_order=False): def calculate_and_save_delta_buyinggroup(self, confirm_order=False):
producer_invoice_buyinggroup = ProducerInvoice.objects.filter( previous_delta_price_with_tax = self.delta_price_with_tax.amount
producer__represent_this_buyinggroup=True, previous_delta_vat = self.delta_vat.amount
permanence_id=self.permanence_id, previous_delta_transport = self.delta_transport.amount
).order_by('?').first()
if producer_invoice_buyinggroup is None:
producer_buyinggroup = producer.Producer.objects.filter(
represent_this_buyinggroup=True
).order_by('?').first()
producer_invoice_buyinggroup = ProducerInvoice.objects.create(
producer_id=producer_buyinggroup.id,
permanence_id=self.permanence_id,
status=self.permanence.status
)
else:
producer_invoice_buyinggroup.delta_price_with_tax.amount -= self.delta_price_with_tax.amount
producer_invoice_buyinggroup.delta_vat.amount -= self.delta_vat.amount
producer_invoice_buyinggroup.delta_transport.amount -= self.delta_transport.amount
self.calculate_delta_price(confirm_order) self.calculate_delta_price(confirm_order)
self.calculate_delta_transport() self.calculate_delta_transport()
producer_invoice_buyinggroup.delta_price_with_tax.amount += self.delta_price_with_tax.amount if previous_delta_price_with_tax != self.delta_price_with_tax.amount or previous_delta_vat != self.delta_vat.amount or previous_delta_transport != self.delta_transport.amount:
producer_invoice_buyinggroup.delta_vat.amount += self.delta_vat.amount producer_invoice_buyinggroup = ProducerInvoice.objects.filter(
producer_invoice_buyinggroup.delta_transport.amount += self.delta_transport.amount producer__represent_this_buyinggroup=True,
permanence_id=self.permanence_id,
).order_by('?').first()
if producer_invoice_buyinggroup is None:
producer_buyinggroup = producer.Producer.objects.filter(
represent_this_buyinggroup=True
).order_by('?').first()
producer_invoice_buyinggroup = ProducerInvoice.objects.create(
producer_id=producer_buyinggroup.id,
permanence_id=self.permanence_id,
status=self.permanence.status
)
producer_invoice_buyinggroup.delta_price_with_tax.amount += self.delta_price_with_tax.amount - previous_delta_price_with_tax
producer_invoice_buyinggroup.delta_vat.amount += self.delta_vat.amount - previous_delta_vat
producer_invoice_buyinggroup.delta_transport.amount += self.delta_transport.amount - previous_delta_transport
producer_invoice_buyinggroup.save() producer_invoice_buyinggroup.save()
def calculate_delta_price(self, confirm_order=False): def calculate_delta_price(self, confirm_order=False):
getcontext().rounding = ROUND_HALF_UP getcontext().rounding = ROUND_HALF_UP
......
...@@ -7,7 +7,7 @@ from django.conf import settings ...@@ -7,7 +7,7 @@ from django.conf import settings
from django.core import urlresolvers from django.core import urlresolvers
from django.core.cache import cache from django.core.cache import cache
from django.db import models from django.db import models
from django.db.models import F from django.db.models import F, Sum
from django.utils import timezone, translation from django.utils import timezone, translation
from django.utils.encoding import python_2_unicode_compatible from django.utils.encoding import python_2_unicode_compatible
from django.utils.formats import number_format from django.utils.formats import number_format
...@@ -101,11 +101,28 @@ class Permanence(TranslatableModel): ...@@ -101,11 +101,28 @@ class Permanence(TranslatableModel):
verbose_name=_("producers"), verbose_name=_("producers"),
blank=True blank=True
) )
total_price_wo_tax = ModelMoneyField( # Calculated with Purchase
invoiced_with_tax = ModelMoneyField(
_("Total amount"), _("Total amount"),
help_text=_('Total purchase amount vat excluded'), help_text=_('Total purchase amount vat included'),
default=DECIMAL_ZERO, max_digits=8, decimal_places=2) default=DECIMAL_ZERO, max_digits=8, decimal_places=2)
total_profit = ModelMoneyField( vat = ModelMoneyField(
_("Total vat"),
help_text=_('Vat part of the total purchased'),
default=DECIMAL_ZERO, max_digits=9, decimal_places=4)
deposit = ModelMoneyField(
_("deposit"),
help_text=_('deposit to add to the original unit price'),
default=DECIMAL_ZERO, max_digits=8, decimal_places=2)
transport = ModelMoneyField(
_("Delivery point transport"),
help_text=_("transport to add"),
default=DECIMAL_ZERO, max_digits=5, decimal_places=2)
# total_price_wo_tax = ModelMoneyField(
# _("Total amount"),
# help_text=_('Total purchase amount vat excluded'),
# default=DECIMAL_ZERO, max_digits=8, decimal_places=2)
profit = ModelMoneyField(
_("Total profit"), _("Total profit"),
help_text=_('Total profit'), help_text=_('Total profit'),
default=DECIMAL_ZERO, max_digits=8, decimal_places=2) default=DECIMAL_ZERO, max_digits=8, decimal_places=2)
...@@ -575,6 +592,65 @@ class Permanence(TranslatableModel): ...@@ -575,6 +592,65 @@ class Permanence(TranslatableModel):
cur_language=translation.get_language(), cur_language=translation.get_language(),
) )
def recalculate_profit(self):
result_set = invoice.CustomerInvoice.objects.filter(
permanence_id=self.id
).order_by('?').aggregate(
Sum('total_price_with_tax'),
Sum('delta_price_with_tax'),
Sum('total_vat'),
Sum('delta_vat'),
Sum('total_deposit'),
Sum('delta_transport')
)
if result_set["total_price_with_tax__sum"] is not None:
ci_sum_total_price_with_tax = result_set["total_price_with_tax__sum"]
else:
ci_sum_total_price_with_tax = DECIMAL_ZERO
if result_set["delta_price_with_tax__sum"] is not None:
ci_sum_delta_price_with_tax = result_set["delta_price_with_tax__sum"]
else:
ci_sum_delta_price_with_tax = DECIMAL_ZERO
if result_set["total_vat__sum"] is not None:
ci_sum_total_vat = result_set["total_vat__sum"]
else:
ci_sum_total_vat = DECIMAL_ZERO
if result_set["delta_vat__sum"] is not None:
ci_sum_delta_vat = result_set["delta_vat__sum"]
else:
ci_sum_delta_vat = DECIMAL_ZERO
if result_set["total_deposit__sum"] is not None:
ci_sum_total_deposit = result_set["total_deposit__sum"]
else:
ci_sum_total_deposit = DECIMAL_ZERO
if result_set["delta_transport__sum"] is not None:
ci_sum_delta_transport = result_set["delta_transport__sum"]
else:
ci_sum_delta_transport = DECIMAL_ZERO
self.invoiced_with_tax.amount = ci_sum_total_price_with_tax + ci_sum_delta_price_with_tax + ci_sum_delta_transport
self.vat.amount = ci_sum_total_vat + ci_sum_delta_vat
self.deposit.amount = ci_sum_total_deposit
result_set = invoice.ProducerInvoice.objects.filter(
permanence_id=self.id
).order_by('?').aggregate(
Sum('total_price_with_tax'),
Sum('total_vat'),
Sum('total_deposit')
)
if result_set["total_price_with_tax__sum"] is not None:
pi_sum_total_price_with_tax = result_set["total_price_with_tax__sum"]
else:
pi_sum_total_price_with_tax = DECIMAL_ZERO
if result_set["total_vat__sum"] is not None:
pi_sum_total_vat = result_set["total_vat__sum"]
else:
pi_sum_total_vat = DECIMAL_ZERO
if result_set["total_deposit__sum"] is not None:
pi_sum_total_deposit = result_set["total_deposit__sum"]
else:
pi_sum_total_deposit = DECIMAL_ZERO
self.profit.amount = self.invoiced_with_tax.amount - pi_sum_total_price_with_tax - self.vat.amount + pi_sum_total_vat
def get_full_status_display(self): def get_full_status_display(self):
return get_full_status_display(self) return get_full_status_display(self)
...@@ -595,20 +671,18 @@ class Permanence(TranslatableModel): ...@@ -595,20 +671,18 @@ class Permanence(TranslatableModel):
return permanence_display return permanence_display
def get_permanence_admin_display(self): def get_permanence_admin_display(self):
if self.total_price_wo_tax != DECIMAL_ZERO: if self.invoiced_with_tax.amount != DECIMAL_ZERO:
if self.total_profit.amount != DECIMAL_ZERO: if self.profit.amount != DECIMAL_ZERO:
return '%s<br/>%s %s<br/>৺ %s<br/>%s%%' % ( invoiced_wo_tax = self.invoiced_with_tax.amount - self.vat.amount
self.get_permanence_display(), _('wo tax'), self.total_price_wo_tax, self.total_profit, if invoiced_wo_tax != DECIMAL_ZERO:
number_format((self.total_profit.amount / self.total_price_wo_tax.amount) * 100, 2) return '%s<br/>%s<br/>৺ %s<br/>%s%%' % (
) self.get_permanence_display(), self.invoiced_with_tax, self.profit,
else: number_format((self.profit.amount / invoiced_wo_tax) * 100, 2)
return '%s<br/>%s %s' % ( )
self.get_permanence_display(), _('wo tax'), self.total_price_wo_tax) return '%s<br/>%s' % (
self.get_permanence_display(), self.invoiced_with_tax)
else: else:
if self.total_profit.amount != DECIMAL_ZERO: return self.get_permanence_display()
return '%s<br/>%s ৺ %s' % (self.get_permanence_display(), _('wo tax'), self.total_profit)
else:
return self.get_permanence_display()
get_permanence_admin_display.short_description = lambda: "%s" % repanier.apps.REPANIER_SETTINGS_PERMANENCES_NAME get_permanence_admin_display.short_description = lambda: "%s" % repanier.apps.REPANIER_SETTINGS_PERMANENCES_NAME
get_permanence_admin_display.allow_tags = True get_permanence_admin_display.allow_tags = True
......
...@@ -109,13 +109,13 @@ class Purchase(models.Model): ...@@ -109,13 +109,13 @@ class Purchase(models.Model):
is_updated_on = models.DateTimeField( is_updated_on = models.DateTimeField(
_("is_updated_on"), auto_now=True, db_index=True) _("is_updated_on"), auto_now=True, db_index=True)
def get_customer_unit_price(self, with_price_list_multiplier=True): def get_customer_unit_price(self):
offer_item = self.offer_item offer_item = self.offer_item
customer_unit_price = offer_item.customer_unit_price.amount if self.price_list_multiplier == DECIMAL_ONE:
if self.price_list_multiplier == DECIMAL_ONE or not with_price_list_multiplier: return offer_item.customer_unit_price.amount
return customer_unit_price
else: else:
return (customer_unit_price * self.price_list_multiplier).quantize(TWO_DECIMALS) getcontext().rounding = ROUND_HALF_UP
return (offer_item.customer_unit_price.amount * self.price_list_multiplier).quantize(TWO_DECIMALS)
get_customer_unit_price.short_description = (_("customer unit price")) get_customer_unit_price.short_description = (_("customer unit price"))
get_customer_unit_price.allow_tags = False get_customer_unit_price.allow_tags = False
...@@ -124,18 +124,18 @@ class Purchase(models.Model): ...@@ -124,18 +124,18 @@ class Purchase(models.Model):
return self.offer_item.unit_deposit.amount return self.offer_item.unit_deposit.amount
def get_customer_unit_vat(self): def get_customer_unit_vat(self):
offer_item = self.offer_item
if self.price_list_multiplier == DECIMAL_ONE: if self.price_list_multiplier == DECIMAL_ONE:
return self.offer_item.customer_vat.amount return offer_item.customer_vat.amount
else: else:
getcontext().rounding = ROUND_HALF_UP getcontext().rounding = ROUND_HALF_UP
return (self.offer_item.customer_vat.amount * self.price_list_multiplier).quantize(FOUR_DECIMALS) return (offer_item.customer_vat.amount * self.price_list_multiplier).quantize(FOUR_DECIMALS)
def get_producer_unit_vat(self): def get_producer_unit_vat(self):
if self.price_list_multiplier == DECIMAL_ONE: offer_item = self.offer_item
return self.offer_item.producer_vat.amount if offer_item.manage_production:
else: return self.get_customer_unit_vat()
getcontext().rounding = ROUND_HALF_UP return offer_item.producer_vat.amount
return (self.offer_item.producer_vat.amount * self.price_list_multiplier).quantize(FOUR_DECIMALS)
def get_selling_price(self): def get_selling_price(self):
# workaround for a display problem with Money field in the admin list_display # workaround for a display problem with Money field in the admin list_display
...@@ -144,10 +144,10 @@ class Purchase(models.Model): ...@@ -144,10 +144,10 @@ class Purchase(models.Model):
get_selling_price.short_description = (_("customer row price")) get_selling_price.short_description = (_("customer row price"))
get_selling_price.allow_tags = False get_selling_price.allow_tags = False
def get_producer_unit_price(self, with_price_list_multiplier=True): def get_producer_unit_price(self):
offer_item = self.offer_item offer_item = self.offer_item
if offer_item.manage_production: if offer_item.manage_production:
return self.get_customer_unit_price(with_price_list_multiplier) return self.get_customer_unit_price()
return offer_item.producer_unit_price.amount return offer_item.producer_unit_price.amount
get_producer_unit_price.short_description = (_("producer unit price")) get_producer_unit_price.short_description = (_("producer unit price"))
...@@ -366,14 +366,14 @@ def purchase_pre_save(sender, **kwargs): ...@@ -366,14 +366,14 @@ def purchase_pre_save(sender, **kwargs):
purchase.price_list_multiplier = DECIMAL_ONE purchase.price_list_multiplier = DECIMAL_ONE
else: else:
purchase.price_list_multiplier = purchase.customer.price_list_multiplier purchase.price_list_multiplier = purchase.customer.price_list_multiplier
producer_unit_price = purchase.get_producer_unit_price()
customer_unit_price = purchase.get_customer_unit_price()
unit_deposit = purchase.get_unit_deposit() unit_deposit = purchase.get_unit_deposit()
purchase.purchase_price.amount = ( purchase.purchase_price.amount = (
(producer_unit_price + unit_deposit) * quantity).quantize(TWO_DECIMALS) (purchase.get_producer_unit_price() + unit_deposit) * quantity).quantize(TWO_DECIMALS)
purchase.selling_price.amount = ( purchase.selling_price.amount = (
(customer_unit_price + unit_deposit) * quantity).quantize(TWO_DECIMALS) (purchase.get_customer_unit_price() + unit_deposit) * quantity).quantize(TWO_DECIMALS)
delta_purchase_price = purchase.purchase_price.amount - purchase.previous_purchase_price delta_purchase_price = purchase.purchase_price.amount - purchase.previous_purchase_price
delta_selling_price = purchase.selling_price.amount - purchase.previous_selling_price delta_selling_price = purchase.selling_price.amount - purchase.previous_selling_price
...@@ -382,53 +382,46 @@ def purchase_pre_save(sender, **kwargs): ...@@ -382,53 +382,46 @@ def purchase_pre_save(sender, **kwargs):
delta_purchase_price != DECIMAL_ZERO): delta_purchase_price != DECIMAL_ZERO):
purchase.vat_level = purchase.offer_item.vat_level purchase.vat_level = purchase.offer_item.vat_level
purchase.producer_vat.amount = purchase.get_producer_unit_vat() * quantity purchase.producer_vat.amount = (purchase.get_producer_unit_vat() * quantity).quantize(FOUR_DECIMALS)
purchase.customer_vat.amount = purchase.get_customer_unit_vat() * quantity purchase.customer_vat.amount = (purchase.get_customer_unit_vat() * quantity).quantize(FOUR_DECIMALS)
purchase.deposit.amount = unit_deposit * quantity purchase.deposit.amount = unit_deposit * quantity
delta_producer_vat = purchase.producer_vat.amount - purchase.previous_producer_vat delta_purchase_vat = purchase.producer_vat.amount - purchase.previous_producer_vat
delta_customer_vat = purchase.customer_vat.amount - purchase.previous_customer_vat delta_selling_vat = purchase.customer_vat.amount - purchase.previous_customer_vat
delta_deposit = purchase.deposit.amount - purchase.previous_deposit delta_deposit = purchase.deposit.amount - purchase.previous_deposit
offeritem.OfferItem.objects.filter(id=purchase.offer_item_id).update( offeritem.OfferItem.objects.filter(id=purchase.offer_item_id).update(
quantity_invoiced=F('quantity_invoiced') + quantity_invoiced=F('quantity_invoiced') + delta_quantity,
delta_quantity, total_purchase_with_tax=F('total_purchase_with_tax') + delta_purchase_price,
total_purchase_with_tax=F('total_purchase_with_tax') + total_selling_with_tax=F('total_selling_with_tax') + delta_selling_price
delta_purchase_price,
total_selling_with_tax=F('total_selling_with_tax') +
delta_selling_price
) )
purchase.offer_item = offeritem.OfferItem.objects.filter( purchase.offer_item = offeritem.OfferItem.objects.filter(
id=purchase.offer_item_id).order_by('?').first() id=purchase.offer_item_id).order_by('?').first()
invoice.CustomerInvoice.objects.filter(id=purchase.customer_invoice.id).update( invoice.CustomerInvoice.objects.filter(id=purchase.customer_invoice.id).update(
total_price_with_tax=F('total_price_with_tax') + total_price_with_tax=F('total_price_with_tax') + delta_selling_price,
delta_selling_price, total_vat=F('total_vat') + delta_selling_vat,
total_vat=F('total_vat') + delta_customer_vat,
total_deposit=F('total_deposit') + delta_deposit total_deposit=F('total_deposit') + delta_deposit
) )
invoice.CustomerProducerInvoice.objects.filter(id=purchase.customer_producer_invoice_id).update( invoice.CustomerProducerInvoice.objects.filter(id=purchase.customer_producer_invoice_id).update(
total_purchase_with_tax=F('total_purchase_with_tax') + total_purchase_with_tax=F('total_purchase_with_tax') + delta_purchase_price,
delta_purchase_price, total_selling_with_tax=F('total_selling_with_tax') + delta_selling_price
total_selling_with_tax=F('total_selling_with_tax') +
delta_selling_price
) )
if purchase.offer_item.price_list_multiplier <= DECIMAL_ONE and not purchase.offer_item.is_resale_price_fixed: if purchase.offer_item.price_list_multiplier <= DECIMAL_ONE and not purchase.offer_item.is_resale_price_fixed:
delta_total_price_with_tax = delta_selling_price delta_purchase_price = delta_selling_price
delta_total_vat = delta_customer_vat delta_purchase_vat = delta_selling_vat
delta_profit = DECIMAL_ZERO
else:
delta_total_price_with_tax = delta_purchase_price
delta_total_vat = delta_producer_vat
delta_profit = delta_selling_price - delta_purchase_price - delta_customer_vat + delta_producer_vat
invoice.ProducerInvoice.objects.filter(id=purchase.producer_invoice_id).update( invoice.ProducerInvoice.objects.filter(id=purchase.producer_invoice_id).update(
total_price_with_tax=F('total_price_with_tax') + total_price_with_tax=F('total_price_with_tax') + delta_purchase_price,
delta_total_price_with_tax, total_vat=F('total_vat') + delta_purchase_vat,
total_vat=F('total_vat') + delta_total_vat,
total_deposit=F('total_deposit') + delta_deposit total_deposit=F('total_deposit') + delta_deposit
) )
delta_profit = delta_selling_price - delta_purchase_price - delta_selling_vat + delta_purchase_vat
permanence.Permanence.objects.filter(id=purchase.permanence_id).update( permanence.Permanence.objects.filter(id=purchase.permanence_id).update(
total_price_wo_tax=F('total_price_wo_tax') + invoiced_with_tax=F('invoiced_with_tax') + delta_selling_price,
delta_total_price_with_tax - delta_total_vat, vat=F('vat') + delta_selling_vat,
total_profit=F('total_profit') + delta_profit deposit=F('deposit') + delta_deposit,
profit=F('profit') + delta_profit
) )
# Do not do it twice # Do not do it twice
purchase.previous_quantity_ordered = purchase.quantity_ordered purchase.previous_quantity_ordered = purchase.quantity_ordered
......
...@@ -103,6 +103,10 @@ def generate_invoice(permanence, payment_date): ...@@ -103,6 +103,10 @@ def generate_invoice(permanence, payment_date):
customer_invoice_id=new_customer_invoice.id, customer_invoice_id=new_customer_invoice.id,
customer_charged_id=new_customer_invoice.customer_charged_id customer_charged_id=new_customer_invoice.customer_charged_id
) )
recalculate_order_amount(
permanence_id=new_permanence.id,
re_init=True
)
# Important : linked to task_invoice.cancel # Important : linked to task_invoice.cancel
# First pass, set customer_charged # First pass, set customer_charged
...@@ -163,7 +167,8 @@ def generate_invoice(permanence, payment_date): ...@@ -163,7 +167,8 @@ def generate_invoice(permanence, payment_date):
) )
customer.save(update_fields=['membership_fee_valid_until', ]) customer.save(update_fields=['membership_fee_valid_until', ])
permanence.recalculate_profit()
permanence.save()
for customer_invoice in CustomerInvoice.objects.filter( for customer_invoice in CustomerInvoice.objects.filter(
permanence_id=permanence.id permanence_id=permanence.id
......
...@@ -752,8 +752,10 @@ def recalculate_order_amount(permanence_id, ...@@ -752,8 +752,10 @@ def recalculate_order_amount(permanence_id,
models.Permanence.objects.filter( models.Permanence.objects.filter(
id=permanence_id id=permanence_id
).update( ).update(
total_profit=DECIMAL_ZERO, invoiced_with_tax=DECIMAL_ZERO,
total_price_wo_tax=DECIMAL_ZERO vat=DECIMAL_ZERO,
deposit=DECIMAL_ZERO,
profit=DECIMAL_ZERO
) )
for offer_item in models.OfferItem.objects.filter( for offer_item in models.OfferItem.objects.filter(
permanence_id=permanence_id, permanence_id=permanence_id,
......
...@@ -9,6 +9,7 @@ from django.utils.html import format_html ...@@ -9,6 +9,7 @@ from django.utils.html import format_html
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.views.decorators.cache import never_cache from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
from parler.models import TranslationDoesNotExist
from repanier.const import PERMANENCE_OPENED, EMPTY_STRING from repanier.const import PERMANENCE_OPENED, EMPTY_STRING
from repanier.models import Permanence from repanier.models import Permanence
...@@ -65,7 +66,10 @@ def home_info_ajax(request): ...@@ -65,7 +66,10 @@ def home_info_ajax(request):
""".format( """.format(
permanences="".join(permanences) permanences="".join(permanences)
) )
if REPANIER_SETTINGS_CONFIG.notification:
notification = REPANIER_SETTINGS_CONFIG.safe_translation_getter(
'notification', any_language=True, default=EMPTY_STRING)
if notification:
if REPANIER_SETTINGS_CONFIG.notification_is_public or request.user.is_authenticated: if REPANIER_SETTINGS_CONFIG.notification_is_public or request.user.is_authenticated:
home_info = """ home_info = """
<div class="container"> <div class="container">
...@@ -81,7 +85,7 @@ def home_info_ajax(request): ...@@ -81,7 +85,7 @@ def home_info_ajax(request):
</div> </div>
{home_info} {home_info}
""".format( """.format(
notification=REPANIER_SETTINGS_CONFIG.notification, notification=notification,
home_info=home_info home_info=home_info
) )
......
...@@ -52,7 +52,8 @@ def login_view(request, template_name='repanier/registration/login.html', ...@@ -52,7 +52,8 @@ def login_view(request, template_name='repanier/registration/login.html',
redirect_field_name: redirect_to, redirect_field_name: redirect_to,
'site' : current_site, 'site' : current_site,
'site_name' : current_site.name, 'site_name' : current_site.name,
'how_to_register' : REPANIER_SETTINGS_CONFIG.how_to_register 'how_to_register' : REPANIER_SETTINGS_CONFIG.safe_translation_getter(
'how_to_register', any_language=True, default=EMPTY_STRING)
} }
if extra_context is not None: if extra_context is not None:
context.update(extra_context) context.update(extra_context)
......
...@@ -143,7 +143,8 @@ class OrderView(ListView): ...@@ -143,7 +143,8 @@ class OrderView(ListView):
context['q'] = self.q context['q'] = self.q
context['is_anonymous'] = self.is_anonymous context['is_anonymous'] = self.is_anonymous
if self.is_anonymous: if self.is_anonymous:
context['how_to_register'] = REPANIER_SETTINGS_CONFIG.how_to_register context['how_to_register'] = REPANIER_SETTINGS_CONFIG.safe_translation_getter(
'how_to_register', any_language=True, default=EMPTY_STRING)
else: else:
context['how_to_register'] = EMPTY_STRING context['how_to_register'] = EMPTY_STRING
context['may_order'] = self.may_order context['may_order'] = self.may_order
......
...@@ -20,7 +20,9 @@ def order_info_ajax(request): ...@@ -20,7 +20,9 @@ def order_info_ajax(request):
if request.is_ajax(): if request.is_ajax():
from repanier.apps import REPANIER_SETTINGS_CONFIG from repanier.apps import REPANIER_SETTINGS_CONFIG
order_info = EMPTY_STRING order_info = EMPTY_STRING
if REPANIER_SETTINGS_CONFIG.notification: notification = REPANIER_SETTINGS_CONFIG.safe_translation_getter(
'notification', any_language=True, default=EMPTY_STRING)
if notification:
if REPANIER_SETTINGS_CONFIG.notification_is_public or request.user.is_authenticated: if REPANIER_SETTINGS_CONFIG.notification_is_public or request.user.is_authenticated:
order_info = """ order_info = """
<div class="row"> <div class="row">
...@@ -35,7 +37,7 @@ def order_info_ajax(request): ...@@ -35,7 +37,7 @@ def order_info_ajax(request):
</div>