Commit 738f0b9f authored by Patrick's avatar Patrick

RC

parent 3d4f0b37
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import ConfigParser try:
import configparser
except:
from six.moves import configparser
import codecs import codecs
import logging import logging
...@@ -38,7 +42,7 @@ MEDIA_URL = "%s%s%s" % (os.sep, "media", os.sep) ...@@ -38,7 +42,7 @@ MEDIA_URL = "%s%s%s" % (os.sep, "media", os.sep)
STATIC_ROOT = os.path.join(PROJECT_DIR, "collect-static") STATIC_ROOT = os.path.join(PROJECT_DIR, "collect-static")
DJANGO_SETTINGS_SITE_NAME = os.path.split(PROJECT_DIR)[-1] DJANGO_SETTINGS_SITE_NAME = os.path.split(PROJECT_DIR)[-1]
config = ConfigParser.RawConfigParser(allow_no_value=True) config = configparser.RawConfigParser(allow_no_value=True)
conf_file_name = '%s%s%s.ini' % ( conf_file_name = '%s%s%s.ini' % (
PROJECT_DIR, PROJECT_DIR,
os.sep, os.sep,
......
...@@ -272,7 +272,7 @@ class PermanenceInPreparationAdmin(TranslatableAdmin): ...@@ -272,7 +272,7 @@ class PermanenceInPreparationAdmin(TranslatableAdmin):
self.message_user(request, user_message, user_message_level) self.message_user(request, user_message, user_message_level)
return return
# Also display order without delivery point -> The customer has not selected it yet # Also display order without delivery point -> The customer has not selected it yet
deliveries_to_be_exported.append(None) # deliveries_to_be_exported.append(None)
else: else:
deliveries_to_be_exported = None deliveries_to_be_exported = None
response = None response = None
......
...@@ -8,8 +8,8 @@ from django.utils.html import strip_tags ...@@ -8,8 +8,8 @@ from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from openpyxl.writer.excel import save_virtual_workbook from openpyxl.writer.excel import save_virtual_workbook
from repanier.models import DeliveryBoard
from repanier.models import Customer from repanier.models import Customer
from repanier.models import DeliveryBoard
from repanier.models import Permanence, Configuration, CustomerInvoice from repanier.models import Permanence, Configuration, CustomerInvoice
from repanier.models import PermanenceBoard from repanier.models import PermanenceBoard
from repanier.models import Producer, ProducerInvoice from repanier.models import Producer, ProducerInvoice
...@@ -17,7 +17,7 @@ from repanier.tools import * ...@@ -17,7 +17,7 @@ from repanier.tools import *
from repanier.xlsx.xlsx_order import generate_customer_xlsx, generate_producer_xlsx from repanier.xlsx.xlsx_order import generate_customer_xlsx, generate_producer_xlsx
def email_order(permanence_id, all_producers=True, closed_deliveries_id=None, producers_id=None): def email_order(permanence_id, all_producers=True, producers_id=None, closed_deliveries_id=None):
from repanier.apps import REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, \ from repanier.apps import REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_BOARD, \
REPANIER_SETTINGS_GROUP_NAME, REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER, \ REPANIER_SETTINGS_GROUP_NAME, REPANIER_SETTINGS_SEND_ORDER_MAIL_TO_PRODUCER, \
REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_PRODUCER, \ REPANIER_SETTINGS_SEND_ABSTRACT_ORDER_MAIL_TO_PRODUCER, \
...@@ -42,6 +42,7 @@ def email_order(permanence_id, all_producers=True, closed_deliveries_id=None, pr ...@@ -42,6 +42,7 @@ def email_order(permanence_id, all_producers=True, closed_deliveries_id=None, pr
if closed_deliveries_id: if closed_deliveries_id:
# closed_deliveries_id is not empty list and not "None" # closed_deliveries_id is not empty list and not "None"
# all_producers is True # all_producers is True
all_producers = True
for delivery_id in closed_deliveries_id: for delivery_id in closed_deliveries_id:
delivery_board = DeliveryBoard.objects.filter( delivery_board = DeliveryBoard.objects.filter(
id=delivery_id id=delivery_id
...@@ -110,7 +111,7 @@ def email_order(permanence_id, all_producers=True, closed_deliveries_id=None, pr ...@@ -110,7 +111,7 @@ def email_order(permanence_id, all_producers=True, closed_deliveries_id=None, pr
permanence=permanence.id, permanence=permanence.id,
language=language_code, language=language_code,
).order_by('?') ).order_by('?')
if producers_id is not None: if producers_id:
producer_set = producer_set.filter(id__in=producers_id) producer_set = producer_set.filter(id__in=producers_id)
for producer in producer_set: for producer in producer_set:
long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name long_profile_name = producer.long_profile_name if producer.long_profile_name is not None else producer.short_profile_name
......
...@@ -17,102 +17,107 @@ class Command(BaseCommand): ...@@ -17,102 +17,107 @@ class Command(BaseCommand):
help = 'Recalculate order amount' help = 'Recalculate order amount'
def handle(self, *args, **options): def handle(self, *args, **options):
latest_total = BankAccount.objects.filter( recalculate_order_amount(
producer__isnull=True, permanence_id=60,
customer__isnull=True, re_init=True
permanence__isnull=False )
).only(
"permanence"
).order_by(
"-id"
).first()
while latest_total:
permanence = latest_total.permanence
if permanence.status == PERMANENCE_INVOICED:
print ("Cancel %s %s" % (permanence.permanence_date, permanence.get_status_display()))
task_invoice.admin_cancel(permanence)
else:
latest_total.delete()
latest_total = BankAccount.objects.filter(
producer__isnull=True,
customer__isnull=True,
permanence__isnull=False
).only(
"permanence"
).order_by(
"-id"
).first()
for permanence in Permanence.objects.filter( # latest_total = BankAccount.objects.filter(
status=PERMANENCE_ARCHIVED # producer__isnull=True,
).order_by('permanence_date'): # customer__isnull=True,
print ("Cancel %s %s" % (permanence.permanence_date, permanence.get_status_display())) # permanence__isnull=False
task_invoice.admin_cancel(permanence) # ).only(
# "permanence"
for permanence in Permanence.objects.filter( # ).order_by(
status__lt=PERMANENCE_CLOSED # "-id"
).order_by('permanence_date'): # ).first()
print ("Recalculate %s %s" % (permanence.permanence_date, permanence.get_status_display())) # while latest_total:
recalculate_order_amount( # permanence = latest_total.permanence
permanence_id=permanence.id, # if permanence.status == PERMANENCE_INVOICED:
re_init=True # print ("Cancel %s %s" % (permanence.permanence_date, permanence.get_status_display()))
) # task_invoice.admin_cancel(permanence)
reorder_offer_items(permanence.id) # else:
for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id): # latest_total.delete()
delivery_point = LUT_DeliveryPoint.objects.filter( # latest_total = BankAccount.objects.filter(
customer_responsible=customer_invoice.customer_id # producer__isnull=True,
).order_by('?').first() # customer__isnull=True,
if delivery_point is not None: # permanence__isnull=False
delivery = DeliveryBoard.objects.filter( # ).only(
delivery_point_id=delivery_point.id, # "permanence"
permanence_id=permanence.id, # ).order_by(
).order_by('?').first() # "-id"
customer_invoice.delivery = delivery # ).first()
customer_invoice.set_delivery(customer_invoice.delivery) #
if customer_invoice.is_order_confirm_send: # for permanence in Permanence.objects.filter(
customer_invoice.confirm_order() # status=PERMANENCE_ARCHIVED
customer_invoice.save() # ).order_by('permanence_date'):
# if customer_invoice.is_order_confirm_send: # print ("Cancel %s %s" % (permanence.permanence_date, permanence.get_status_display()))
# confirm_customer_invoice(permanence.id, customer_invoice.customer_id) # task_invoice.admin_cancel(permanence)
for permanence in Permanence.objects.filter( #
status__gte=PERMANENCE_CLOSED, # for permanence in Permanence.objects.filter(
status__lt=PERMANENCE_INVOICED # status__lt=PERMANENCE_CLOSED
).order_by('permanence_date'): # ).order_by('permanence_date'):
# Important : Do not reclaculte if permanence is invoiced or archived. # print ("Recalculate %s %s" % (permanence.permanence_date, permanence.get_status_display()))
# First, cancel the invoice / archiving. # recalculate_order_amount(
print ("Recalculate %s %s" % (permanence.permanence_date, permanence.get_status_display())) # permanence_id=permanence.id,
status = permanence.status # re_init=True
permanence.set_status(status) # )
# if status >= PERMANENCE_SEND: # reorder_offer_items(permanence.id)
recalculate_order_amount( # for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id):
permanence_id=permanence.id, # delivery_point = LUT_DeliveryPoint.objects.filter(
re_init=True # customer_responsible=customer_invoice.customer_id
) # ).order_by('?').first()
reorder_offer_items(permanence.id) # if delivery_point is not None:
for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id): # delivery = DeliveryBoard.objects.filter(
delivery_point = LUT_DeliveryPoint.objects.filter( # delivery_point_id=delivery_point.id,
customer_responsible=customer_invoice.customer_id # permanence_id=permanence.id,
).order_by('?').first() # ).order_by('?').first()
if delivery_point is not None: # customer_invoice.delivery = delivery
print("---- %s" % delivery_point) # customer_invoice.set_delivery(customer_invoice.delivery)
delivery = DeliveryBoard.objects.filter( # if customer_invoice.is_order_confirm_send:
delivery_point_id=delivery_point.id, # customer_invoice.confirm_order()
permanence_id=permanence.id, # customer_invoice.save()
).order_by('?').first() # # if customer_invoice.is_order_confirm_send:
customer_invoice.delivery = delivery # # confirm_customer_invoice(permanence.id, customer_invoice.customer_id)
customer_invoice.set_delivery(customer_invoice.delivery) # for permanence in Permanence.objects.filter(
if customer_invoice.is_order_confirm_send: # status__gte=PERMANENCE_CLOSED,
customer_invoice.confirm_order() # status__lt=PERMANENCE_INVOICED
customer_invoice.save() # ).order_by('permanence_date'):
# # Important : Do not reclaculte if permanence is invoiced or archived.
for permanence in Permanence.objects.filter( # # First, cancel the invoice / archiving.
status=PERMANENCE_SEND, # print ("Recalculate %s %s" % (permanence.permanence_date, permanence.get_status_display()))
highest_status__in=[PERMANENCE_INVOICED, PERMANENCE_ARCHIVED] # status = permanence.status
).order_by( # permanence.set_status(status)
"payment_date", "is_updated_on" # # if status >= PERMANENCE_SEND:
): # recalculate_order_amount(
# if permanence.highest_status == PERMANENCE_INVOICED: # permanence_id=permanence.id,
# else: # re_init=True
pass # )
# reorder_offer_items(permanence.id)
# for customer_invoice in CustomerInvoice.objects.filter(permanence_id=permanence.id):
# delivery_point = LUT_DeliveryPoint.objects.filter(
# customer_responsible=customer_invoice.customer_id
# ).order_by('?').first()
# if delivery_point is not None:
# print("---- %s" % delivery_point)
# delivery = DeliveryBoard.objects.filter(
# delivery_point_id=delivery_point.id,
# permanence_id=permanence.id,
# ).order_by('?').first()
# customer_invoice.delivery = delivery
# customer_invoice.set_delivery(customer_invoice.delivery)
# if customer_invoice.is_order_confirm_send:
# customer_invoice.confirm_order()
# customer_invoice.save()
#
# for permanence in Permanence.objects.filter(
# status=PERMANENCE_SEND,
# highest_status__in=[PERMANENCE_INVOICED, PERMANENCE_ARCHIVED]
# ).order_by(
# "payment_date", "is_updated_on"
# ):
# # if permanence.highest_status == PERMANENCE_INVOICED:
# # else:
# pass
...@@ -202,11 +202,11 @@ class CustomerInvoice(models.Model): ...@@ -202,11 +202,11 @@ class CustomerInvoice(models.Model):
purchase.Purchase.objects.filter( purchase.Purchase.objects.filter(
customer_invoice__id=self.id customer_invoice__id=self.id
).update(quantity_confirmed=F('quantity_ordered')) ).update(quantity_confirmed=F('quantity_ordered'))
self.calculate_and_save_delta_buyinggroup() self.calculate_and_save_delta_buyinggroup(confirm_order=True)
self.is_order_confirm_send = True self.is_order_confirm_send = True
@transaction.atomic @transaction.atomic
def calculate_and_save_delta_buyinggroup(self): def calculate_and_save_delta_buyinggroup(self, confirm_order=False):
producer_invoice_buyinggroup = ProducerInvoice.objects.filter( producer_invoice_buyinggroup = ProducerInvoice.objects.filter(
producer__represent_this_buyinggroup=True, producer__represent_this_buyinggroup=True,
permanence_id=self.permanence_id, permanence_id=self.permanence_id,
...@@ -225,7 +225,7 @@ class CustomerInvoice(models.Model): ...@@ -225,7 +225,7 @@ class CustomerInvoice(models.Model):
producer_invoice_buyinggroup.delta_vat.amount -= self.delta_vat.amount producer_invoice_buyinggroup.delta_vat.amount -= self.delta_vat.amount
producer_invoice_buyinggroup.delta_transport.amount -= self.delta_transport.amount producer_invoice_buyinggroup.delta_transport.amount -= self.delta_transport.amount
self.calculate_delta_price() 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 producer_invoice_buyinggroup.delta_price_with_tax.amount += self.delta_price_with_tax.amount
...@@ -234,10 +234,11 @@ class CustomerInvoice(models.Model): ...@@ -234,10 +234,11 @@ class CustomerInvoice(models.Model):
producer_invoice_buyinggroup.save() producer_invoice_buyinggroup.save()
def calculate_delta_price(self): def calculate_delta_price(self, confirm_order=False):
getcontext().rounding = ROUND_HALF_UP getcontext().rounding = ROUND_HALF_UP
if self.customer_charged is None: if self.customer_charged is None or confirm_order:
# confirm_order : the customer confirm his/her order
result_set = purchase.Purchase.objects.filter( result_set = purchase.Purchase.objects.filter(
permanence_id=self.permanence_id, permanence_id=self.permanence_id,
customer_id=self.customer_id, customer_id=self.customer_id,
...@@ -307,7 +308,6 @@ class CustomerInvoice(models.Model): ...@@ -307,7 +308,6 @@ class CustomerInvoice(models.Model):
self.delta_price_with_tax.amount = DECIMAL_ZERO self.delta_price_with_tax.amount = DECIMAL_ZERO
self.delta_vat.amount = DECIMAL_ZERO self.delta_vat.amount = DECIMAL_ZERO
def calculate_delta_transport(self): def calculate_delta_transport(self):
self.delta_transport.amount = DECIMAL_ZERO self.delta_transport.amount = DECIMAL_ZERO
......
...@@ -348,10 +348,10 @@ class OfferItem(TranslatableModel): ...@@ -348,10 +348,10 @@ class OfferItem(TranslatableModel):
unit_price = self.get_unit_price(customer_price=customer_price) unit_price = self.get_unit_price(customer_price=customer_price)
if len(qty_display) > 0: if len(qty_display) > 0:
if self.unit_deposit.amount > DECIMAL_ZERO: if self.unit_deposit.amount > DECIMAL_ZERO:
return '%s, %s + ♻ %s' % ( return '%s; %s + ♻ %s' % (
qty_display, unit_price, self.unit_deposit) qty_display, unit_price, self.unit_deposit)
else: else:
return '%s, %s' % (qty_display, unit_price) return '%s; %s' % (qty_display, unit_price)
else: else:
if self.unit_deposit.amount > DECIMAL_ZERO: if self.unit_deposit.amount > DECIMAL_ZERO:
return '%s + ♻ %s' % ( return '%s + ♻ %s' % (
...@@ -361,7 +361,10 @@ class OfferItem(TranslatableModel): ...@@ -361,7 +361,10 @@ class OfferItem(TranslatableModel):
def get_order_name(self, is_quantity_invoiced=False, box_unicode=BOX_UNICODE): def get_order_name(self, is_quantity_invoiced=False, box_unicode=BOX_UNICODE):
return '%s %s' % (self.long_name, self.get_qty_display(is_quantity_invoiced, box_unicode)) qty_display = self.get_qty_display(is_quantity_invoiced, box_unicode)
if qty_display:
return '%s %s' % (self.long_name, qty_display)
return '%s' % self.long_name
def get_long_name_with_producer_price(self): def get_long_name_with_producer_price(self):
return self.get_long_name(customer_price=False) return self.get_long_name(customer_price=False)
...@@ -370,7 +373,11 @@ class OfferItem(TranslatableModel): ...@@ -370,7 +373,11 @@ class OfferItem(TranslatableModel):
get_long_name_with_producer_price.admin_order_field = 'translations__long_name' get_long_name_with_producer_price.admin_order_field = 'translations__long_name'
def get_long_name(self, is_quantity_invoiced=False, customer_price=True, box_unicode=BOX_UNICODE): def get_long_name(self, is_quantity_invoiced=False, customer_price=True, box_unicode=BOX_UNICODE):
return '%s %s' % (self.long_name, self.get_qty_and_price_display(is_quantity_invoiced, customer_price, box_unicode))
qty_and_price_display = self.get_qty_and_price_display(is_quantity_invoiced, customer_price, box_unicode)
if qty_and_price_display:
return '%s %s' % (self.long_name, qty_and_price_display)
return '%s' % self.long_name
get_long_name.short_description = (_("long_name")) get_long_name.short_description = (_("long_name"))
get_long_name.allow_tags = False get_long_name.allow_tags = False
......
...@@ -264,10 +264,10 @@ class Product(TranslatableModel): ...@@ -264,10 +264,10 @@ class Product(TranslatableModel):
unit_price = self.get_unit_price(customer_price=customer_price) unit_price = self.get_unit_price(customer_price=customer_price)
if len(qty_display) > 0: if len(qty_display) > 0:
if self.unit_deposit.amount > DECIMAL_ZERO: if self.unit_deposit.amount > DECIMAL_ZERO:
return '%s, %s + ♻ %s' % ( return '%s; %s + ♻ %s' % (
qty_display, unit_price, self.unit_deposit) qty_display, unit_price, self.unit_deposit)
else: else:
return '%s, %s' % (qty_display, unit_price) return '%s; %s' % (qty_display, unit_price)
else: else:
if self.unit_deposit.amount > DECIMAL_ZERO: if self.unit_deposit.amount > DECIMAL_ZERO:
return '%s + ♻ %s' % ( return '%s + ♻ %s' % (
...@@ -276,8 +276,10 @@ class Product(TranslatableModel): ...@@ -276,8 +276,10 @@ class Product(TranslatableModel):
return '%s' % unit_price return '%s' % unit_price
def get_long_name(self, is_quantity_invoiced=False, customer_price=True, box_unicode=BOX_UNICODE): def get_long_name(self, is_quantity_invoiced=False, customer_price=True, box_unicode=BOX_UNICODE):
return '%s %s' % ( qty_and_price_display = self.get_qty_and_price_display(is_quantity_invoiced, customer_price, box_unicode)
self.long_name, self.get_qty_and_price_display(is_quantity_invoiced, customer_price, box_unicode)) if qty_and_price_display:
return '%s %s' % (self.long_name, qty_and_price_display)
return "%s" % self.long_name
get_long_name.short_description = (_("long_name")) get_long_name.short_description = (_("long_name"))
get_long_name.allow_tags = True get_long_name.allow_tags = True
......
...@@ -184,7 +184,7 @@ class Purchase(models.Model): ...@@ -184,7 +184,7 @@ class Purchase(models.Model):
get_delivery_display.allow_tags = False get_delivery_display.allow_tags = False
def get_quantity(self): def get_quantity(self):
if self.status < PERMANENCE_SEND: if self.status < PERMANENCE_WAIT_FOR_SEND:
return self.quantity_ordered return self.quantity_ordered
else: else:
return self.quantity_invoiced return self.quantity_invoiced
...@@ -192,19 +192,29 @@ class Purchase(models.Model): ...@@ -192,19 +192,29 @@ class Purchase(models.Model):
get_quantity.short_description = (_("quantity invoiced")) get_quantity.short_description = (_("quantity invoiced"))
get_quantity.allow_tags = False get_quantity.allow_tags = False
def get_producer_quantity(self):
if self.status < PERMANENCE_WAIT_FOR_SEND:
return self.quantity_ordered
else:
offer_item = self.offer_item
if offer_item.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
if offer_item.order_average_weight != 0:
return (self.quantity_invoiced / offer_item.order_average_weight).quantize(FOUR_DECIMALS)
return self.quantity_invoiced
def get_long_name(self, customer_price=True): def get_long_name(self, customer_price=True):
if self.offer_item is not None: if self.offer_item is not None:
if self.is_box_content: if self.is_box_content:
return "%s %s" % ( return "%s %s" % (
self.offer_item.get_long_name( self.offer_item.get_long_name(
is_quantity_invoiced=self.status >= PERMANENCE_SEND, # is_quantity_invoiced=self.status >= PERMANENCE_WAIT_FOR_SEND,
customer_price=customer_price customer_price=customer_price
), ),
BOX_UNICODE BOX_UNICODE
) )
else: else:
return self.offer_item.get_long_name( return self.offer_item.get_long_name(
is_quantity_invoiced=self.status >= PERMANENCE_SEND, # is_quantity_invoiced=self.status >= PERMANENCE_WAIT_FOR_SEND,
customer_price=customer_price customer_price=customer_price
) )
else: else:
...@@ -307,10 +317,8 @@ class Purchase(models.Model): ...@@ -307,10 +317,8 @@ class Purchase(models.Model):
def purchase_post_init(sender, **kwargs): def purchase_post_init(sender, **kwargs):
purchase = kwargs["instance"] purchase = kwargs["instance"]
if purchase.id is not None: if purchase.id is not None:
if purchase.status < PERMANENCE_WAIT_FOR_SEND: purchase.previous_quantity_ordered = purchase.quantity_ordered
purchase.previous_quantity = purchase.quantity_ordered purchase.previous_quantity_invoiced = purchase.quantity_invoiced
else:
purchase.previous_quantity = purchase.quantity_invoiced
purchase.previous_purchase_price = purchase.purchase_price.amount purchase.previous_purchase_price = purchase.purchase_price.amount
purchase.previous_selling_price = purchase.selling_price.amount purchase.previous_selling_price = purchase.selling_price.amount
purchase.previous_producer_vat = purchase.producer_vat.amount purchase.previous_producer_vat = purchase.producer_vat.amount
...@@ -318,6 +326,8 @@ def purchase_post_init(sender, **kwargs): ...@@ -318,6 +326,8 @@ def purchase_post_init(sender, **kwargs):
purchase.previous_deposit = purchase.deposit.amount purchase.previous_deposit = purchase.deposit.amount
purchase.previous_comment = purchase.comment purchase.previous_comment = purchase.comment
else: else:
purchase.previous_quantity_ordered = DECIMAL_ZERO
purchase.previous_quantity_invoiced = DECIMAL_ZERO
purchase.previous_quantity = DECIMAL_ZERO purchase.previous_quantity = DECIMAL_ZERO
purchase.previous_purchase_price = DECIMAL_ZERO purchase.previous_purchase_price = DECIMAL_ZERO
purchase.previous_selling_price = DECIMAL_ZERO purchase.previous_selling_price = DECIMAL_ZERO
...@@ -332,11 +342,17 @@ def purchase_pre_save(sender, **kwargs): ...@@ -332,11 +342,17 @@ def purchase_pre_save(sender, **kwargs):
purchase = kwargs["instance"] purchase = kwargs["instance"]
if purchase.status < PERMANENCE_WAIT_FOR_SEND: if purchase.status < PERMANENCE_WAIT_FOR_SEND:
quantity = purchase.quantity_ordered quantity = purchase.quantity_ordered
delta_quantity = quantity - purchase.previous_quantity_ordered
if purchase.offer_item.order_unit == PRODUCT_ORDER_UNIT_PC_KG: if purchase.offer_item.order_unit == PRODUCT_ORDER_UNIT_PC_KG:
# This quantity is used to calculate the price
# The unit price is for 1 kg.
# 1 = 1 piece of order_average_weight
# 2 = 2 pices of order_average_weight
quantity *= purchase.offer_item.order_average_weight quantity *= purchase.offer_item.order_average_weight
else: else:
quantity = purchase.quantity_invoiced quantity = purchase.quantity_invoiced
delta_quantity = quantity - purchase.previous_quantity delta_quantity = quantity - purchase.previous_quantity_invoiced
if purchase.is_box_content: if purchase.is_box_content:
purchase.is_resale_price_fixed = True purchase.is_resale_price_fixed = True
if delta_quantity != DECIMAL_ZERO: if delta_quantity != DECIMAL_ZERO:
...@@ -415,10 +431,8 @@ def purchase_pre_save(sender, **kwargs): ...@@ -415,10 +431,8 @@ def purchase_pre_save(sender, **kwargs):
total_profit=F('total_profit') + delta_profit total_profit=F('total_profit') + delta_profit
) )
# Do not do it twice # Do not do it twice
if purchase.status < PERMANENCE_WAIT_FOR_SEND: purchase.previous_quantity_ordered = purchase.quantity_ordered
purchase.previous_quantity = purchase.quantity_ordered purchase.previous_quantity_invoiced = purchase.quantity_invoiced
else:
purchase.previous_quantity = purchase.quantity_invoiced
purchase.previous_purchase_price = purchase.purchase_price.amount purchase.previous_purchase_price = purchase.purchase_price.amount
purchase.previous_selling_price = purchase.selling_price.amount purchase.previous_selling_price = purchase.selling_price.amount
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import thread import threading
from django.contrib import messages from django.contrib import messages
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
...@@ -670,7 +670,9 @@ def admin_cancel(permanence): ...@@ -670,7 +670,9 @@ def admin_cancel(permanence):
def admin_send(permanence): def admin_send(permanence):
if permanence.status == PERMANENCE_INVOICED: if permanence.status == PERMANENCE_INVOICED:
thread.start_new_thread(email_invoice.send_invoice, (permanence.id,)) # thread.start_new_thread(email_invoice.send_invoice, (permanence.id,))
t = threading.Thread(target=email_invoice.send_invoice, args=(permanence.id,))
t.start()
user_message = _("Emails containing the invoices will be send to the customers and the producers.") user_message = _("Emails containing the invoices will be send to the customers and the producers.")
user_message_level = messages.INFO user_message_level = messages.INFO
else: else:
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import datetime import datetime
import thread import threading
import uuid import uuid
from django.conf import settings from django.conf import settings
...@@ -19,10 +19,8 @@ from repanier.models import OfferItem ...@@ -19,10 +19,8 @@ from repanier.models import OfferItem
from repanier.models import Permanence from repanier.models import Permanence
from repanier.models import Producer from repanier.models import Producer
from repanier.models import Product from repanier.models import Product
from repanier.tools import clean_offer_item from repanier.tools import clean_offer_item, reorder_purchases
from repanier.tools import recalculate_order_amount, create_or_update_one_purchase, get_or_create_offer_item, \ from repanier.tools import recalculate_order_amount, create_or_update_one_purchase, reorder_offer_items
add_months, \
reorder_offer_items
@transaction.atomic @transaction.atomic
...@@ -261,13 +259,17 @@ def admin_open_and_send(request, permanence, do_not_send_any_mail=False): ...@@ -261,13 +259,17 @@ def admin_open_and_send(request, permanence, do_not_send_any_mail=False):
else: else:
permanence.set_status(PERMANENCE_WAIT_FOR_PRE_OPEN) permanence.set_status(PERMANENCE_WAIT_FOR_PRE_OPEN)
# pre_open_order(permanence.id) # pre_open_order(permanence.id)
thread.start_new_thread(pre_open_order, (permanence.id,)) # thread.start_new_thread(pre_open_order, (permanence.id,))
t = threading.Thread(target=pre_open_order, args=(permanence.id,))
t.start()
user_message = _("The offers are being generated.") user_message = _("The offers are being generated.")
user_message_level = messages.INFO user_message_level = messages.INFO
else: else:
permanence.set_status(PERMANENCE_WAIT_FOR_OPEN) permanence.set_status(PERMANENCE_WAIT_FOR_OPEN)
# open_order(permanence.id) # open_order(permanence.id)
thread.start_new_thread(open_order, (permanence.id, do_not_send_any_mail)) # thread.start_new_thread(open_order, (permanence.id, do_not_send_any_mail))
t = threading.Thread(target=open_order, args=(permanence.id, do_not_send_any_mail))
t.start()
user_message = _("The offers are being generated.") user_message = _("The offers are being generated.")
user_message_level = messages.INFO user_message_level = messages.INFO
return user_message, user_message_level return user_message, user_message_level
...@@ -417,9 +419,14 @@ def close_order(permanence, all_producers, producers_id=None): ...@@ -417,9 +419,14 @@ def close_order(permanence, all_producers, producers_id=None):
@transaction.atomic @transaction.atomic
def send_order(permanence, all_producers, producers_id=None, deliveries_id=None): def send_order(permanence, all_producers=True, producers_id=None, deliveries_id=None):
recalculate_order_amount(
permanence_id=permanence.id,
send_to_producer=True
)
reorder_purchases(permanence.id)
try: try:
email_order.email_order(permanence.id, all_producers, closed_deliveries_id=deliveries_id, producers_id=producers_id) email_order.email_order(permanence.id, all_producers, producers_id=producers_id, closed_deliveries_id=deliveries_id)
except Exception as error_str: except Exception as error_str:
print("################################## send_order") print("################################## send_order")
print(error_str) print(error_str)
...@@ -491,7 +498,10 @@ def close_send_order(permanence_id, all_producers, producers_id=None, deliveries ...@@ -491,7 +498,10 @@ def close_send_order(permanence_id, all_producers, producers_id=None, deliveries
def admin_close(permanence_id, all_producers=False, deliveries_id=None, producers_id=None): def admin_close(permanence_id, all_producers=False, deliveries_id=None, producers_id=None):
# close_send_order(permanence_id, deliveries_id, False, False) # close_send_order(permanence_id, deliveries_id, False, False)
thread.start_new_thread(close_send_order, (permanence_id, all_producers, producers_id, deliveries_id, False)) # thread.start_new_thread(close_send_order, (permanence_id, all_producers, producers_id, deliveries_id, False))
t = threading.Thread(target=close_send_order,
args=(permanence_id, all_producers, producers_id, deliveries_id, False))
t.start()
user_message = _("The orders are being closed.") user_message = _("The orders are being closed.")
user_message_level = messages.INFO user_message_level = messages.INFO
return user_message, user_message_level return user_message, user_message_level
...@@ -499,7 +509,10 @@ def admin_close(permanence_id, all_producers=False, deliveries_id=None, producer ...@@ -499,7 +509,10 @@ def admin_close(permanence_id, all_producers=False, deliveries_id=None, producer
def admin_send(permanence_id, all_producers=False, deliveries_id=None, producers_id=None): def admin_send(permanence_id, all_producers=False, deliveries_id=None, producers_id=None):
# close_send_order(permanence_id, deliveries_id, True) # close_send_order(permanence_id, deliveries_id, True)
thread.start_new_thread(close_send_order, (permanence_id, all_producers, producers_id, deliveries_id, True)) # thread.start_new_thread(close_send_order, (permanence_id, all_producers, producers_id, deliveries_id, True))
t = threading.Thread(target=close_send_order,
args=(permanence_id, all_producers, producers_id, deliveries_id, True))
t.start()