Generate Invoice Number

Hi All,

I’m facing an issue when generate Invoice Number. I’d like to generate invoice number with this format “191003/Delta” = yearMonthday/CustomerName. Unfortunately my below code doesn’t working. here is the error: invalid literal for int() with base 10: ‘01None’

Hopefully you guys can help me on this issue. Thank you.

class Customer(models.Model):
    name = models.CharField(max_length=255)
 
    def __str__(self):
        return self.name
 
class Order(models.Model):
    invoice_id = models.CharField(max_length=255, blank=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
 
    def __str__(self):
        return '{}'.format(self.invoice_id)
 
    def save(self, *args, **kwargs):
    	today = datetime.date.today()
    	today_string = today.strftime('%y%m%d')
    	get_name = Order.objects.filter(customer__name=self.name).first()
    	next_invoice_number = '01'
    	last_invoice = Order.objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()
    	if last_invoice:
    		last_invoice_number = int(last_invoice.invoice_id[6:])
    		next_invoice_number = '{0:02d}'.format(last_invoice_number + 1)
    	self.invoice_id = today_string + '/' + next_invoice_number + '/' + get_name
    	super(Order, self).save(*args, **kwargs)

You are providing very little context to the error, when it happens, what is the full stacktrace, etc. I assume you have a problem on this line:

It does not make any sense to transform the invoice_id to an integer.