This is my django views code snippet:
if (calltype == 'save'):
bill_data = json.loads(request.POST.get('bill_details'))
Invoice=salesInvoice()
customerkey = request.POST.get('customer')
Invoice.customer_key = Customer.object.get(key__iexact=customerkey)
Invoice.total = request.POST.get('total')
Invoice.grand_discount = request.POST.get('grand_discount')
Invoice.amount_paid = request.POST.get('amount_paid')
#Invoice.save()
#saving the salesLineItem and linking them with foreign key to invoice
for data in bill_data:
LineItem = salesLineItem()
#LineItem.invoice_no = salesInvoice
itemcode=data['itemCode']
LineItem.name = Product.object.get(key__iexact=itemcode).name
LineItem.unit = Product.object.get(key__iexact=itemcode).unit
LineItem.batch = Product.object.get(key__iexact=itemcode).batch
LineItem.discount1 = Product.object.get(key__iexact=itemcode).discount1
LineItem.discount2 = Product.object.get(key__iexact=itemcode).discount2
LineItem.quantity = int(data['itemQuantity'])
LineItem.free = int(data['itemFree'])
LineItem.mrp = Product.object.get(key__iexact=itemcode).mrp
LineItem.selling_price = Product.object.get(key__iexact=itemcode).selling_price
LineItem.vat_type = Product.object.get(key__iexact=itemcode).vat_type
LineItem.vat_percent = Product.object.get(key__iexact=itemcode).vat_percent
#LineItem.save()
#this part is just for checking
response_data['name'] = Product.object.get(key__iexact=itemcode).name
Do note that I've commented out the lines used to save the invoice and lineitem. Once I un-comment these lines, the error i get is:
name 'Invoice' is not defined
I also overrode the save method of the django model (for the salesInvoice model). Following is its code (in django models file) and in the commented line I've a small explanation of what that does:
#the save method is overriden to give unique invoice ids, slug and customer_name
def save(self, *args, **kwargs):
today = dt.date.today()
today_string = today.strftime('%y%m%d')
next_invoice_number = '01'
last_invoice = Invoice.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:03d}'.format(last_invoice_number + 1)
self.invoice_id = today_string + next_invoice_number
if not self.id:
self.slug=slugify(self.invoice_id)
customer_name = Customer.object.get(key__iexact=customer_key).name
address = Customer.object.get(key__iexact=customer_key).address
super(Invoice, self).save(*args, **kwargs)
I dont understand where's the error. Any help will be appreciated.
EDIT 1 - Adding the traceback
Traceback:
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\my_env\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\billinv\billbrain\views.py" in salesinvoice
173. Invoice.save()
File "C:\Users\Ganguly PC\Desktop\DjangoStudy\billinv\billbrain\models.py" in save
189. last_invoice = Invoice.objects.filter(invoice_id__startswith=today_string).order_by('invoice_id').last()
Exception Type: NameError at /master/salesinvoice/
Exception Value: name 'Invoice' is not defined
Request information:
GET: No GET data
POST:
amount_paid = '0.00'
customer = 'fr'
csrfmiddlewaretoken = 'xxxxxxxxxxxxxxxxxxxxx'
bill_details = '[{"itemCode":"rev","itemQuantity":"5","itemFree":"0"}]'
grand_discount = '0.00'
total = '435.00'
calltype = 'save'