Commit f5fc3033 authored by Patrick's avatar Patrick

Move the producers not invoiced into a new permanence

parent 9052c521
...@@ -563,10 +563,11 @@ class Permanence(TranslatableModel): ...@@ -563,10 +563,11 @@ class Permanence(TranslatableModel):
translation.activate(cur_language) translation.activate(cur_language)
return new_permanence return new_permanence
def create_child(self): def create_child(self, status):
child_permanence = Permanence.objects.create( child_permanence = Permanence.objects.create(
permanence_date=self.permanence_date, permanence_date=self.permanence_date,
master_permanence_id=self.id master_permanence_id=self.id,
status=status
) )
return self.duplicate_short_name( return self.duplicate_short_name(
child_permanence, child_permanence,
......
...@@ -37,71 +37,64 @@ def generate_invoice(permanence, payment_date): ...@@ -37,71 +37,64 @@ def generate_invoice(permanence, payment_date):
to_be_paid=False to_be_paid=False
).order_by('?').exists() ).order_by('?').exists()
if permanence_partially_invoiced: if permanence_partially_invoiced:
# Move the producers not invoiced into a new permanence
# The values_list method returns a ValuesListQuerySet. producers_to_move = list(ProducerInvoice.objects.filter(
# This means it has the advantages of a queryset. For example it is lazy, so you only fetch the first 25 elements from the database when you slice it.
# To convert it to a list for e.g. because you reuse it, use list()
producers_to_invoice = list(ProducerInvoice.objects.filter(
permanence_id=permanence.id, permanence_id=permanence.id,
invoice_sort_order__isnull=True, invoice_sort_order__isnull=True,
to_be_paid=True to_be_paid=False
).values_list('producer_id', flat=True).order_by("producer_id")) ).values_list('producer_id', flat=True).order_by("producer_id"))
new_permanence = permanence.create_child() new_permanence = permanence.create_child(PERMANENCE_SEND)
# It's not needed to set new_permanence.status = PERMANENCE_WAIT_FOR_INVOICED because we are in @transaction
for purchase in Purchase.objects.filter(
permanence_id=permanence.id,
producer_id__in=producers_to_invoice
).order_by().distinct('customer_invoice'):
customer_invoice = CustomerInvoice.objects.filter(
id=purchase.customer_invoice_id
).order_by('?').first()
new_customer_invoice = customer_invoice.create_child(new_permanence=new_permanence)
new_customer_invoice.set_delivery(customer_invoice.delivery)
Purchase.objects.filter(
customer_invoice_id=customer_invoice.id,
producer_id__in=producers_to_invoice
).order_by('?').update(
permanence_id=new_permanence.id,
customer_invoice_id=new_customer_invoice.id,
customer_charged_id=new_customer_invoice.customer_charged_id
)
new_customer_invoice.calculate_and_save_delta_buyinggroup()
customer_invoice.calculate_and_save_delta_buyinggroup()
ProducerInvoice.objects.filter( ProducerInvoice.objects.filter(
permanence_id=permanence.id, permanence_id=permanence.id,
producer_id__in=producers_to_invoice producer_id__in=producers_to_move
).order_by('?').update( ).order_by('?').update(
permanence_id=new_permanence.id permanence_id=new_permanence.id
) )
CustomerProducerInvoice.objects.filter( CustomerProducerInvoice.objects.filter(
permanence_id=permanence.id, permanence_id=permanence.id,
producer_id__in=producers_to_invoice producer_id__in=producers_to_move
).order_by('?').update( ).order_by('?').update(
permanence_id=new_permanence.id permanence_id=new_permanence.id
) )
OfferItem.objects.filter( OfferItem.objects.filter(
permanence_id=permanence.id, permanence_id=permanence.id,
producer_id__in=producers_to_invoice producer_id__in=producers_to_move
).order_by('?').update( ).order_by('?').update(
permanence_id=new_permanence.id permanence_id=new_permanence.id
) )
permanence = new_permanence
else: for purchase in Purchase.objects.filter(
for customer_invoice in CustomerInvoice.objects.filter( permanence_id=permanence.id,
permanence_id=permanence.id).order_by('?'): producer_id__in=producers_to_move
# In case of changed delivery conditions ).order_by().distinct('customer_invoice'):
customer_invoice.set_delivery(customer_invoice.delivery) customer_invoice = CustomerInvoice.objects.filter(
# Need to calculate delta_price_with_tax, delta_vat and delta_transport id=purchase.customer_invoice_id
customer_invoice.calculate_and_save_delta_buyinggroup() ).order_by('?').first()
new_customer_invoice = customer_invoice.create_child(new_permanence=new_permanence)
new_customer_invoice.set_delivery(customer_invoice.delivery)
Purchase.objects.filter( Purchase.objects.filter(
customer_invoice_id=customer_invoice.id customer_invoice_id=customer_invoice.id,
producer_id__in=producers_to_move
).order_by('?').update( ).order_by('?').update(
customer_charged_id=customer_invoice.customer_charged_id permanence_id=new_permanence.id,
customer_invoice_id=new_customer_invoice.id,
customer_charged_id=new_customer_invoice.customer_charged_id
) )
new_customer_invoice.calculate_and_save_delta_buyinggroup()
for customer_invoice in CustomerInvoice.objects.filter(
permanence_id=permanence.id).order_by('?'):
# In case of changed delivery conditions
customer_invoice.set_delivery(customer_invoice.delivery)
# Need to calculate delta_price_with_tax, delta_vat and delta_transport
customer_invoice.calculate_and_save_delta_buyinggroup()
Purchase.objects.filter(
customer_invoice_id=customer_invoice.id
).order_by('?').update(
customer_charged_id=customer_invoice.customer_charged_id
)
# Important : linked to task_invoice.cancel # Important : linked to task_invoice.cancel
......
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