forked from beba/foerderbarometer
				
			Fixes wrong project counter when saving with modyfied start year. PID
is also constant now.
This commit is contained in:
		
							parent
							
								
									4e3d915e90
								
							
						
					
					
						commit
						2a77188887
					
				| 
						 | 
				
			
			@ -100,13 +100,60 @@ class Project(Volunteer):
 | 
			
		|||
    project_of_year = models.IntegerField(default=0)
 | 
			
		||||
    end_quartal = models.CharField(max_length=15, null=True, blank=True, verbose_name="Quartal Projekt Ende")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def save(self,*args,**kwargs):
 | 
			
		||||
        
 | 
			
		||||
        generate_finance_id=False
 | 
			
		||||
 | 
			
		||||
        '''we generate the autogenerated fields here'''
 | 
			
		||||
        # we don't call save with args/kwargs to avoid UNIQUE CONSTRAINT errors
 | 
			
		||||
        # but maybe there is a better solution?
 | 
			
		||||
        super().save()
 | 
			
		||||
        self.pid = str(self.start.year) + '-' + str(self.account.code) + str(self.pk).zfill(3)
 | 
			
		||||
        # self.pid = str(self.account.code) + str(self.pk).zfill(3)
 | 
			
		||||
 | 
			
		||||
        if not self.pk:
 | 
			
		||||
            print ("NO PK THERE");
 | 
			
		||||
            generate_finance_id=True
 | 
			
		||||
            super().save()
 | 
			
		||||
        else:
 | 
			
		||||
            orig = type(self).objects.get(pk=self.pk)  # Originaldaten aus der DB abrufen
 | 
			
		||||
            if orig.start.year != self.start.year:
 | 
			
		||||
                generate_finance_id=True
 | 
			
		||||
            if orig.account.code != self.account.code:
 | 
			
		||||
                if str(self.account.code) == '21111':
 | 
			
		||||
                    generate_finance_id=True
 | 
			
		||||
                else:
 | 
			
		||||
                    self.finance_id = str(self.account.code)
 | 
			
		||||
 | 
			
		||||
                
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        if generate_finance_id:
 | 
			
		||||
            print ("MUST GENERATE FINANCE ID")
 | 
			
		||||
            year = self.start.year
 | 
			
		||||
            projects = Project.objects.filter(start__year=year)
 | 
			
		||||
            if not projects:
 | 
			
		||||
                self.project_of_year = 1
 | 
			
		||||
                #self.pid = str(self.start.year) + '-' + str(self.account.code) + str(self.project_of_year).zfill(3)
 | 
			
		||||
            else:
 | 
			
		||||
                # get the project of year number of latest entry
 | 
			
		||||
                projects = projects.order_by("-project_of_year")[0]
 | 
			
		||||
                # add one to value of latest entry
 | 
			
		||||
                self.project_of_year = int(projects.project_of_year) + 1
 | 
			
		||||
#                self.pid = str(self.start.year) + '-' + str(self.account.code) + str(self.project_of_year).zfill(3)
 | 
			
		||||
 | 
			
		||||
                
 | 
			
		||||
            if str(self.account.code) == '21111':
 | 
			
		||||
                self.finance_id = str(self.account.code) + '-' + str(self.project_of_year).zfill(3)
 | 
			
		||||
            else:
 | 
			
		||||
                self.finance_id = str(self.account.code)
 | 
			
		||||
 | 
			
		||||
#        print (("Current PID",self.pid))
 | 
			
		||||
 | 
			
		||||
        if not self.pid:
 | 
			
		||||
            self.pid = str(self.account.code) + str(self.pk).zfill(8)
 | 
			
		||||
            # self.pid = str(self.account.code) + str(self.pk).zfill(3)
 | 
			
		||||
            print (("Hallo Leute! Ich save jetzt mal MIT PID DANN!!!",self.pid))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        # generation of field quartals
 | 
			
		||||
        if self.end.month in [1, 2, 3]:
 | 
			
		||||
| 
						 | 
				
			
			@ -118,34 +165,9 @@ class Project(Volunteer):
 | 
			
		|||
        if self.end.month in [10, 11, 12]:
 | 
			
		||||
            self.end_quartal = 'Q4'
 | 
			
		||||
 | 
			
		||||
        # generation of pid and financeID
 | 
			
		||||
        
 | 
			
		||||
        if not self.project_of_year:
 | 
			
		||||
            #print('AAA')
 | 
			
		||||
            # we need to determine if this is a new year with its first new project...
 | 
			
		||||
            year = self.start.year
 | 
			
		||||
            #print(year)
 | 
			
		||||
            projects = Project.objects.filter(start__year=year)
 | 
			
		||||
            if not projects:
 | 
			
		||||
                #print('BBB')
 | 
			
		||||
                self.project_of_year = 1
 | 
			
		||||
                self.pid = str(self.start.year) + '-' + str(self.account.code) + str(self.project_of_year).zfill(3)
 | 
			
		||||
            else:
 | 
			
		||||
                #print('CCC')
 | 
			
		||||
                # get the project of year number of latest entry
 | 
			
		||||
                projects = projects.order_by("-project_of_year")[0]
 | 
			
		||||
                # add one to value of latest entry
 | 
			
		||||
                self.project_of_year = int(projects.project_of_year) + 1
 | 
			
		||||
                self.pid = str(self.start.year) + '-' + str(self.account.code) + str(self.project_of_year).zfill(3)
 | 
			
		||||
                
 | 
			
		||||
                if str(self.account.code) == '21111':
 | 
			
		||||
                    self.finance_id = str(self.account.code) + str(self.project_of_year).zfill(3)
 | 
			
		||||
                else:
 | 
			
		||||
                    self.finance_id = str(self.account.code)
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        super().save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def __str__(self):
 | 
			
		||||
        return f"{self.pid} {self.name}"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue