def forwards(self, orm): # Quotation for quotation in Quotation.objects(): quotation._changed_fields = [ 'tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_invoice', 'down_payments', 'subscribers' ] set_embedded_changed_fields(quotation) quotation.save() # Invoice for invoice in Invoice.objects(): invoice._changed_fields = [ 'tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_quotation', 'payments', 'subscribers' ] set_embedded_changed_fields(invoice) invoice.save() # DownPaymentInvoice for down_payment_invoice in DownPaymentInvoice.objects(): down_payment_invoice._changed_fields = [ 'tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_quotation', 'payments', 'tax_applied', 'subscribers' ] set_embedded_changed_fields(down_payment_invoice) down_payment_invoice.save() # CreditNote for credit_note in CreditNote.objects(): credit_note._changed_fields = [ 'tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_invoice', 'subscribers' ] set_embedded_changed_fields(credit_note) credit_note.save() # Item for item in Item.objects(): item._changed_fields = ['tenant', 'currency', 'tax'] item.save() # Payment for payment in Payment.objects(): payment._changed_fields = ['tenant', 'issuer', 'currency'] payment.save() # Tax for tax in Tax.objects(): tax._changed_fields = ['tenant'] tax.save()
def forwards(self, orm): # Quotation for quotation in Quotation.objects(): quotation._changed_fields = ['tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_invoice', 'down_payments', 'subscribers'] set_embedded_changed_fields(quotation) quotation.save() # Invoice for invoice in Invoice.objects(): invoice._changed_fields = ['tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_quotation', 'payments', 'subscribers'] set_embedded_changed_fields(invoice) invoice.save() # DownPaymentInvoice for down_payment_invoice in DownPaymentInvoice.objects(): down_payment_invoice._changed_fields = ['tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_quotation', 'payments', 'tax_applied', 'subscribers'] set_embedded_changed_fields(down_payment_invoice) down_payment_invoice.save() # CreditNote for credit_note in CreditNote.objects(): credit_note._changed_fields = ['tenant', 'issuer', 'organization', 'contact', 'attachments', 'related_invoice', 'subscribers'] set_embedded_changed_fields(credit_note) credit_note.save() # Item for item in Item.objects(): item._changed_fields = ['tenant', 'currency', 'tax'] item.save() # Payment for payment in Payment.objects(): payment._changed_fields = ['tenant', 'issuer', 'currency'] payment.save() # Tax for tax in Tax.objects(): tax._changed_fields = ['tenant'] tax.save()
def make_down_payment_invoice(self, issuer, percentage, tax, date): """Creates a down payment invoice based on the current quotation/purchase order""" from invoicing.models import DownPaymentInvoice if percentage <= 0 or percentage >= 1: raise InvalidDownPaymentPercentage( "Percentage must be a decimal between 0 and 1.") inv_data = self.current_revision # Calculate the total amount from the base (excluding taxes) to avoid decimal differences. # Check with amount=97.72 and tax_rate=0.196. excl_tax_amount = ((self.amount * percentage).quantize( Decimal('1.00'), ROUND_HALF_UP) / (Decimal('1.00') + tax.rate)).quantize( Decimal('1.00'), ROUND_HALF_UP) down_payment_amount = (excl_tax_amount * (Decimal('1.00') + tax.rate)).quantize( Decimal('1.00'), ROUND_HALF_UP) # Sum existing down payments percentage current_percentage = Decimal('0.00') for down_payment_invoice in self.group.down_payment_invoices: current_percentage += down_payment_invoice.percentage # Ensure that down payments can't exceed 100% if current_percentage + percentage > 1: raise InvalidDownPaymentPercentage( "Total of down-payments percentages exceeds 1 (100%).") # Ensure that date is correct if date < datetime.date.today(): raise InvalidDownPaymentDueDate("Invalid down-payment due date.") down_payment_invoice = DownPaymentInvoice( full_init=False, tenant=self.tenant, account_type=self.account_type, issuer=issuer, state="REGISTERED", organization=self.organization, contact=self.contact, related_to=self, percentage=percentage, tax_applied=tax, total=down_payment_amount, amount=down_payment_amount, balance=down_payment_amount, group=self.group, attachments=self.attachments) down_payment_invoice.add_revision( state=down_payment_invoice.state, issuer=issuer, issue_date=datetime_now(), sender=inv_data.sender, organization=inv_data.organization, contact=inv_data.contact, sender_address=inv_data.sender_address, billing_address=inv_data.billing_address, delivery_address=inv_data.delivery_address, customer_reference=inv_data.customer_reference, currency=inv_data.currency, invoicing_date=date.today(), due_date=date) # Save the down payment invoice down_payment_invoice.save() return down_payment_invoice
def make_down_payment_invoice(self, issuer, percentage, tax, date): """Creates a down payment invoice based on the current quotation/purchase order""" from invoicing.models import DownPaymentInvoice if percentage <= 0 or percentage >= 1: raise InvalidDownPaymentPercentage("Percentage must be a decimal between 0 and 1.") inv_data = self.current_revision # Calculate the total amount from the base (excluding taxes) to avoid decimal differences. # Check with amount=97.72 and tax_rate=0.196. excl_tax_amount = ((self.amount * percentage).quantize(Decimal('1.00'), ROUND_HALF_UP) / (Decimal('1.00') + tax.rate)).quantize(Decimal('1.00'), ROUND_HALF_UP) down_payment_amount = (excl_tax_amount * (Decimal('1.00') + tax.rate)).quantize(Decimal('1.00'), ROUND_HALF_UP) # Sum existing down payments percentage current_percentage = Decimal('0.00') for down_payment_invoice in self.group.down_payment_invoices: current_percentage += down_payment_invoice.percentage # Ensure that down payments can't exceed 100% if current_percentage + percentage > 1: raise InvalidDownPaymentPercentage("Total of down-payments percentages exceeds 1 (100%).") # Ensure that date is correct if date < datetime.date.today(): raise InvalidDownPaymentDueDate("Invalid down-payment due date.") down_payment_invoice = DownPaymentInvoice( full_init=False, tenant=self.tenant, account_type=self.account_type, issuer=issuer, state="REGISTERED", organization=self.organization, contact=self.contact, related_to=self, percentage=percentage, tax_applied=tax, total=down_payment_amount, amount=down_payment_amount, balance=down_payment_amount, group=self.group, attachments=self.attachments ) down_payment_invoice.add_revision( state=down_payment_invoice.state, issuer=issuer, issue_date=datetime_now(), sender=inv_data.sender, organization=inv_data.organization, contact=inv_data.contact, sender_address=inv_data.sender_address, billing_address=inv_data.billing_address, delivery_address=inv_data.delivery_address, customer_reference=inv_data.customer_reference, currency=inv_data.currency, invoicing_date=date.today(), due_date=date ) # Save the down payment invoice down_payment_invoice.save() return down_payment_invoice