Problem is you try to use sth you can’t control it.
You should not modify admin page. Try to create a new one as your
knowledge. That way keep you learn and stay away unpredictable issues.
On Wed, 17 Aug 2022 at 20:02 Paul Kudla (SCOM.CA Internet Services Inc.) <
[email protected]> wrote:

>
> ok i had a hudge problem when writing my invoice system
>
> in general you need to over ride the save defination in the admin and
> models.py files
>
> this was never clearly documented in django.
>
>
> I use inlines so the code gets really complicated really quickly
>
> i have given my code you will need to muddle through it to do your own
> thing.
>
> my code was designed to run inlines, update an invoice number from
> another form, do the math, taxes etc etc etc
>
> it also interfaces to a customer database (kinda required for repeat
> invoices)
>
> i did everything in admin but you should look at the models override for
> other stuff :
>
> class InvoiceAdmin(admin.ModelAdmin):
>
>         class Invoice_Items_Inline(admin.TabularInline):
>                 model = Invoice_Items
>                 extra = 1
>
>         class Invoice_Payment_Inline(admin.TabularInline):
>                 model = Invoice_Payment
>                 extra = 1
>
>         formfield_overrides = {
>                 models.CharField: {'widget':
> TextInput(attrs={'size':'40','style':
> 'height: 1em;'})},
>                 models.TextField: {'widget': Textarea(attrs={'rows':1,
> 'cols':40,'style': 'height: 2em;'})},
>                 }
>
>         def customer_name_sold_to(self, obj):
>                 if (obj.sold_to):
>                         return obj.sold_to.customer_name
>                 return None
>         customer_name_sold_to.short_description = "Sold To"
>
>         def attention_sold_to(self, obj):
>                 if (obj.sold_to):
>                         return obj.sold_to.attention_to
>                 return None
>         attention_sold_to.short_description = "Attention to"
>
>         def getinvoicebody(self, obj):
>
>
>                 return None
>
>
>         def attention_ship_to(self, obj):
>                 if (obj.ship_to):
>                         return obj.ship_to.attention_to
>                 return None
>         attention_ship_to.short_description = "Attention to"
>
>
>         def invoice_paid_total(self,obj) :
>                 obj.paid_total = float(0.00)
>                 try :
>                         record = Invoice_Payment.objects.all().filter(
> payment_item = '%s'
> %obj.invoice_number )
>                         for nn in range (0,len(record)) : #start the update
>                                 paid_total = float(paid_total) +
> float(record[nn].cashbook_amount)
>
>                 except : pass #No entries found against this invoice
>
>                 return obj.paid_total
>
>         attention_ship_to.short_description = "Attention to"
>
>         def Print_Invoice(modeladmin, request, queryset) :
>                 queryset.update(printed=True)
>
>         Print_Invoice.short_description = "Print Invoice(s)"
>
>
>         actions = [Print_Invoice]
>
>         #('attention_sold_to','attention_ship_to'),
>
>         fieldsets = [
>         ('Info', {'fields':
> [('invoice_number','invoice_date'),('sold_to','ship_to',),('purchase_order','terms','currency_type','delivery_type','foreign_account',)]}),
>
> #,('sold_to_address',)
>                 ('#inline', {
>                 'fields': (),
>                 'description': 0
>                 }),
>         ('Details', {'fields':
>
> [('sub_total'),('gst_tax_exempt','gst_tax_value','gst_tax_amount'),('hst_tax_exempt','hst_tax_value','hst_tax_amount'),('pst_tax_exempt','pst_tax_value','pst_tax_amount'),('invoice_total'),]}),
>                 ('#inline', {
>                 'fields': (),
>                 'description': 1
>                 }),
>         ('Status', {'fields':
> [('ar_owing','printed','faxed','emailed','new_invoice'), ]}),
>         ]
>
>
>         inlines = [Invoice_Items_Inline, Invoice_Payment_Inline, ]
>         reorder_inlines = True
>         list_display = ('invoice_number',
>
> 'invoice_date','customer_name_sold_to','body_search','sub_total','hst_tax_amount','invoice_total','invoice_paid_total','ar_owing')
>
>         search_fields = search_with_fk(['^invoice_number',
>          'body_search',
>                 ])
>
>
>         #readonly_fields = ('sold_to_address','ship_to_address')
>
>         date_hierarchy = 'invoice_date'
>         ordering = ('-invoice_date','-invoice_number_sort','ar_owing')
>
>         list_per_page = 100
>
>         index = True
>
>         adminextra_fields = [
>                 ('m21', 'sold_to', Contacts_Admin.list_display),
>                 #('fkd', 'sold_to_address', ('sold_to',)),
>                 ('m21', 'ship_to', Contacts_Admin.list_display),
>                 #('fkd', 'ship_to_address', ('ship_to',)),
>                 ]
>
>         def save_model(self, request, obj, form, change): #This will save
> create
>                 #Check to see if this will be an invoice copy
>                 copy = False
>                 current_invoice = obj.invoice_number
>                 if obj.new_invoice :    #clear the flag and save the
> current object then
> set new & date
>                         obj.new_invoice = False
>                         obj.save()
>                         copy = True
>
>                         obj.invoice_number = 'None' #Set for New invoice
> to be generated
>
>                         import datetime
>                         now = datetime.datetime.now()
>
>                         obj.invoice_date = now.strftime("%Y-%m-%d")
>
>                         #now duplicate body of invoice
>
>
>
>
>                 if str(obj.invoice_number) == 'None' : #Assign An Invoice
> Number if needed
>                         #Go get the next number
>                         nextinvoice =
> Config_Data.objects.get(pk='INVOICE') #Go get the
> current invoice number
>                         next_invoice_num = int(nextinvoice.value) #Get the
> current invoice
> number to be used next
>                         next_invoice_num = next_invoice_num + 1
>                         nextinvoice.value = str(next_invoice_num)
>                         next_invoice_num = next_invoice_num - 1
>                         nextinvoice.save()      #Update the next invoice
> number to be used.
>
>                         #Check to see if the invoice number is elsewhere
> in the system
>                         #checkinvoice = Invoice.objects.get( pk='%s'
> %str(next_invoice_num) )
>
>                         obj.invoice_number = str(next_invoice_num) #Set
> the invoice number to
> the form.
>                         obj.ar_owing = '9999999999'
>
>                 #Set Search Index for Invoice Number
>                 i1 = '0000000000'
>                 obj.invoice_number_sort =
> i1[0:len(i1)-len(obj.invoice_number)] +
> obj.invoice_number
>
>                 obj.save() #Save the invoice
>
>                 if copy :
>                         for item in
> Invoice.objects.get(pk=current_invoice).invoicing_items.all():
>                                 #Now do the copy
>                                 newitem = Invoice_Items( invoicing_item =
> obj,
>
>       item_date = item.item_date,
>
>       item = item.item ,
>
>       item_description = item.item_description,
>
>       item_qty = item.item_qty,
>
>       item_unit = item.item_unit,
>
>       item_extention = item.item_extention ,
>
>       )
>
>                                 newitem.save()
>
>
>
>
>
>         #def save_formset(self, request, form, formset, change):
>         #               instances = formset.save(commit=False)
>         #               f = open('/tmp/formset.text','w')
>         #               f.write (str(len(instances)) + '\n')
>         #
>         #               for instance in instances:
>         #                       f.write (str(instance) + '\n')
>         #                       instance.save()
>
>         #               f.close()
>         #def save_formset(self, request, form, formset):
>                         #for f in formset.forms:
>                         #       obj = f.instance
>                         #       obj.color = obj.color.upper() # or
> whatever change you'd like
>         #               formset.save()
>
>         def save_related(self, request, form, formsets, change):
>                 super(InvoiceAdmin, self).save_related(request, form,
> formsets, change)
>
>                 invoice = form.instance
>
>                 #Ok do the sub total
>                 invoice.sub_total = float(0)
>                 invoice.body_search = str(invoice.sold_to) + ' ' +
> str(invoice.invoice_total) + ' '
>
>
>
>                 for item in
> Invoice.objects.get(pk=invoice.invoice_number).invoicing_items.all():
>                         #set the search parameters for the this body line
>                         if str(item.item) <> 'None' and str(item.item) <>
> '':
>                                 invoice.body_search = invoice.body_search
> + str(item.item) + ' '
>                         if str(item.item_description) <> 'None' and
> str(item.item_description) <> '' :
>                                 invoice.body_search = invoice.body_search
> +
> str(item.item_description) + ' '
>
>                         #Now do the math
>                         if str(item.item_qty) <> 'None' and
> str(item.item_unit) <> 'None' :
>                                 item.item_extention = float(item.item_qty)
> * float(item.item_unit)
>                                 invoice.sub_total =
> float(invoice.sub_total) +
> float(item.item_extention)
>                         item.save()
>
>                 #Now the taxes
>                 if invoice.gst_tax_exempt == False :
>                         invoice.gst_tax_amount = float(invoice.sub_total)
> *
> float(invoice.gst_tax_value) / 100
>                 else :
>                         invoice.gst_tax_amount = float(0)
>
>                 if invoice.hst_tax_exempt == False :
>                         invoice.hst_tax_amount = float(invoice.sub_total)
> *
> float(invoice.hst_tax_value) / 100
>                 else :
>                         invoice.hst_tax_amount = float(0)
>
>
>                 if invoice.pst_tax_exempt == False :
>                         invoice.pst_tax_amount = float(invoice.sub_total)
> *
> float(invoice.pst_tax_value) / 100
>                 else :
>                         invoice.pst_tax_amount = float(0)
>
>
>                 invoice.invoice_total = float(invoice.sub_total) +
> float(invoice.gst_tax_amount) + float(invoice.hst_tax_amount) +
> float(invoice.pst_tax_amount)
>
>                 if invoice.ar_owing == '9999999999' :
>                         invoice.ar_owing = invoice.invoice_total
>
>
>                 #Now the a/r
>                 total_ar = float(invoice.invoice_total)
>                 try :
>                         record = Invoice_Payment.objects.all().filter(
> payment_item = '%s'
> %invoice.invoice_number )
>                         for nn in range (0,len(record)) : #start the update
>                                 total_ar = total_ar -
> float(record[nn].cashbook_amount)
>                 except : pass
>
>                 invoice.ar_owing = total_ar #set the new ar value
>
>
>                 invoice.save()
>
>                 #Now generate the pdf's
>                 if invoice.printed == True or invoice.faxed == True or
> invoice.emailed
> == True :
>                         makeinvoicepdf(invoice.invoice_number)
>
>
>
> site.register(Invoice, InvoiceAdmin)
>
>
> this is an example of my contacts save overrides in models.py:
>
>         def save(self, *args, **kwargs):  #This will save / create the
> contact
> entry
>                 super(Contacts, self).save(*args, **kwargs) #Save the
> contact entry
>                 self.accountid_text = '00000'
>                 self.accountid_text =
> self.accountid_text[0:5-len(str(self.accountid))] + str(self.accountid)
>
>                 #See if i am going to email this contact ?
>                 if not self.send_to == '' :     #Send an email with
> contact info
>                         send_subject = 'Contact Info for : %s   -  %s'
> %(self.customer_name,self.send_to_subject)
>                         send_from = '[email protected]'
>                         send_files = []
>                         send_to = self.send_to.split(',')
>                         #Build the email text entry
>                         send_text = '\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n' \
>
>
> %(self.customer_name,self.attention_to,self.address_1,self.address_2,self.city,self.province,self.postal_code)
>
>                         #for nn in range (0,len(self.phone_number) ):
>                         #       phone_data = self.phone_number[nn]
>
>                         self.phonenumber = ''
>                         for self.item in
> Contacts.objects.get(pk=self.accountid).phone_number.all():
>                                 if str(self.item.phone_number) == '' or
> str(self.item.phone_number)
> == 'None' or str(self.item.phone_type) == 'Fax' :
>                                         pass
>                                 else :
>                                         if self.phonenumber <> '' :
>                                                 self.phonenumber =
> self.phonenumber + ' / '
>
>                                         self.phonenumber =
> self.phonenumber + '(' +
> str(self.item.phone_type) + ') ' + str(self.item.phone_number)
>
>                         send_text = send_text + self.phonenumber + '\n\n'
>
>
> sendmail(send_from,send_to,send_subject,send_text,send_files) #Send
> the warning email
>                         self.send_to = '' #Clear the entry
>                         self.send_to_subject = ''
>
>
>
>                 #Save the contact entry (in case of new record)
>                 super(Contacts, self).save(*args, **kwargs) #Save the
> contact entry
> (in case of new record)
>
>
>
>
> Happy Wednesday !!!
> Thanks - paul
>
> Paul Kudla
>
>
> Scom.ca Internet Services <http://www.scom.ca>
> 004-1009 Byron Street South
> Whitby, Ontario - Canada
> L1N 4S3
>
> Toronto 416.642.7266
> Main 1.866.411.7266
> Fax 1.888.892.7266
> Email [email protected]
>
> On 8/16/2022 1:21 PM, Viando Donwasta wrote:
> > Hi,
> > May I get help with this project? I am new to Django. Below is my view
> > and model for your review.
> >
> >
> > class Invoice(models.Model):
> >
> >      InvoiceNo = ShortUUIDField(length=16,
> >          prefix="GN_",
> >          alphabet="1234567890",
> >          primary_key=True,
> >          )
> >      InvoiceDate = models.DateField(default=datetime.now, blank=True)
> >      Account = models.CharField(max_length=50, blank=True)
> >      PaymentDueDate = models.DateField(default=datetime.now, blank=True)
> >      Location  = models.ForeignKey(Location,
> > on_delete=models.DO_NOTHING, blank=True, default=1)
> >     # Product  = models.ForeignKey(Product, on_delete=models.DO_NOTHING)
> >      Test = models.ManyToManyField("Products", blank=True,
> > related_name="products")
> >      Address   = models.ForeignKey(Address, on_delete=models.DO_NOTHING)
> >      #Quantity = models.IntegerField(blank=True, default=1)
> >      Tax = models.IntegerField(default =0)
> >      SubTotal = models.DecimalField(blank=True,
> > null=True,decimal_places=2,max_digits=10)
> >      Total = models.DecimalField(blank=True,
> > null=True,decimal_places=2,max_digits=10)
> >      #Ful = models.FloatField(blank=True)
> >
> >      def save(self, *args, **kwargs):
> >          #rate = Product.objects.get(pk=self.Product.id
> > <http://self.product.id/>)
> >          test = Products.objects.all()
> >          total = 0
> >          for my in test:
> >              total +=  my.SubTotal
> >          self.SubTotal = total
> >          self.Total = self.SubTotal + self.Tax
> >          #self.Total = Sum(self.Total) + Sum( self.SubTotal)
> >          super(Invoice, self).save(*args, **kwargs)
> >
> >      # @property
> >      # def PerformSub(self):
> >      #      myval = self.Quantity * self.Product.Price
> >      #      return myval
> >
> >      # @property
> >      # def PerformCal(self):
> >      #     self.Total = self.SubTotal + self.Tax
> >
> >      # def __str__(self):
> >      #     return self.InvoiceNo
> >
> >
> >
> >
> > class Products(models.Model):
> >      ProductDate = models.DateField(default=datetime.now, blank=True)
> >      Title = models.CharField(max_length=50, blank=True)
> >      Description = models.CharField(max_length=150, blank=True)
> >      Sku = models.CharField(max_length=50, blank=True)
> >      Price = models.FloatField(blank=True)
> >      Quantity = models.IntegerField(blank=True, default=1)
> >      SubTotal = models.DecimalField(blank=True,
> > null=True,decimal_places=2,max_digits=10)
> >      SalesPrice = models.CharField(max_length=50, blank=True)
> >
> >
> >      def save(self, *args, **kwargs):
> >         # rate = Products.objects.get(pk=self.Product.id
> > <http://self.product.id/>)
> >          self.SubTotal = self.Price * self.Quantity
> >          super(Products, self).save(*args, **kwargs)
> >
> >
> >      def __str__(self):
> >          return self.Title + "" + self.Description
> >
> >
> > class InvoiceForm(ModelForm):
> >      class Meta:
> >          model = Invoice
> >          fields = ['InvoiceNo', 'InvoiceDate',
> > 'Account','PaymentDueDate', 'Location','Test', 'Address', 'Tax',
> > 'SubTotal', 'Total'
> >          ]
> >
> >
> >
> > Here is my view. Any help will be much appreciated thank you .
> >
> >
> >
> >
> >
> > def dashboard(request):
> >
> >      # forms = admin.get_form(invoices, res)
> >      location = Location.objects.all()
> >      address = Address.objects.all()
> >      products = Products.objects.all()
> >      form = InvoiceForm()
> >      # invoice = Invoice.objects.all()
> >      # invoice = get_object_or_404(Invoice)
> >
> >      if request.method == 'POST':
> >          form = InvoiceForm(request.POST)
> >          if form.is_valid():
> >              # myinvoice = form.save(commit=False)
> >
> >              print("test")
> >
> >              invoiceTest = form.save(commit=True)
> >
> >
> >
> >              invoice = Invoice()
> >              invoice.InvoiceNo = form.cleaned_data['InvoiceNo']
> >              invoice.InvoiceDate = form.cleaned_data['InvoiceDate']
> >              invoice.Account = form.cleaned_data['Account']
> >              invoice.PaymentDueDate = form.cleaned_data['PaymentDueDate']
> >              invoice.Location = form.cleaned_data['Location']
> >              invoice.Test =  form.cleaned_data['Test']
> >              invoice.Tax = form.cleaned_data['Tax']
> >              invoice.SubTotal = form.cleaned_data['SubTotal']
> >              invoice.Total = form.cleaned_data['Total']
> >
> >
> >
> >
> >              invoiceTest.save()
> >              # invoice.save_m2m()
> >
> >              # print(invoiceForm)
> >
> >
> >
> >
> >              # invoice =
> >
> Invoice(InvoiceNo=InvoiceNo,InvoiceDate=InvoiceDate,Account=Account,PaymentDueDate=PaymentDueDate,Location=mylocation,Tax=Tax,SubTotal=SubTotal,Total=Total)
> >              # invoice.save()
> >
> >
> >              return HttpResponseRedirect('index')
> >
> >
> >      else:
> >          form = InvoiceForm()
> >          # return redirect('dashboard')
> >      return render(request, 'pages/dashboard.html', { "form" :form ,
> > 'Location': location, 'Address': address, 'Products': products})
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to [email protected]
> > <mailto:[email protected]>.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/59e95c89-1d77-4a61-9d1e-bd7846ca3c35n%40googlegroups.com
> <
> https://groups.google.com/d/msgid/django-users/59e95c89-1d77-4a61-9d1e-bd7846ca3c35n%40googlegroups.com?utm_medium=email&utm_source=footer
> >.
> >
> > --
> > This message has been scanned for viruses and
> > dangerous content by *MailScanner* <http://www.mailscanner.info/>, and
> is
> > believed to be clean.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/64870ff9-13fd-0cc8-4151-a1faaef9b0e3%40scom.ca
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAKaoNbQNNY9R0zGLf9Quo5C%3DHCoP%2Bz4mi_php0QZuGncg_2uFw%40mail.gmail.com.

Reply via email to