У меня есть четыре модели EmployeeRegistration, посещаемость, ставка и заработная плата одновременно. Я хочу обновлять модель заработной платы в последние дни месяца. Для этого я синхронизирую всю посещаемость из модели посещаемости с даты 01.01.2020 по 30.01.2020. И сохраните данные манипулирования в Модель заработной платы.
Мои модели посещаемости
class Attendence(models.Model):
EmpId = models.IntegerField(verbose_name='EmpId')
Name = models.CharField(max_length=50,verbose_name='Name')
Site = models.CharField(max_length=50,verbose_name='Site')
Days = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Days')
Nh = models.IntegerField(verbose_name='Nh')
#SingleOt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='SingleOt',null=True)
DoubleOt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='DobleOt',null=True)
choices = [('P','Present'),('A','Absent'),('O','Off')]
Status = models.CharField(choices=choices,blank = False,max_length=10,verbose_name='Status')
AttendenceOn = models.DateField(default = timezone.now)
def __str__(self):
return '{EmpId}/{Name}/{date}'.format(EmpId=self.EmpId,Name=self.Name,date=self.AttendenceOn)
Моя модель ставок ниже
class Rate(models.Model):
Site = models.ForeignKey(Site,on_delete=models.CASCADE)
Category = models.ForeignKey(Category,on_delete=models.CASCADE,default=False)
Department = models.ForeignKey(Department,on_delete=models.CASCADE,default=False)
Basic = models.DecimalField(max_digits=10,decimal_places=2)
Da = models.DecimalField(max_digits=10,decimal_places=2)
Rate = models.DecimalField(max_digits=10,decimal_places=2)
Hra = models.DecimalField(max_digits=10,decimal_places=2)
Ca = models.DecimalField(max_digits=10,decimal_places=2)
SplAllow = models.DecimalField(max_digits=10,decimal_places=2)
CanteenAllow = models.DecimalField(max_digits=10,decimal_places=2)
def __str__(self):
return '{site}_{cat}'.format(site =self.Site,cat=self.Category)
Моя модель заработной платы ниже
class Wages(models.Model):
EmpId = models.IntegerField(verbose_name='EmpId')
Name = models.CharField(max_length=50,verbose_name='Name')
Site = models.CharField(max_length=50,verbose_name='Site')
Days = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Days')
Nh = models.IntegerField(verbose_name='Nh')
SingleOt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='SingleOt',null=True)
DoubleOt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='DobleOt',null=True)
Basic_rate = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Basic_rate')
Da_rate = models.DecimalField(max_digits=10,decimal_places=2,verbose_name ='Da_rate')
Basic_Amt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Basic_Amt')
Da_Amt = models.DecimalField(max_digits=10,decimal_places=2,verbose_name ='Da_Amt')
Ot_Amt = models.DecimalField(max_digits=50,decimal_places=2,verbose_name='Ot_Amt')
FoodingAdd = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Fooding')
AttendenceAward =
models.DecimalField(max_digits=10,decimal_places=2,verbose_name='AttendenceAward')
OtherPayment =
models.DecimalField(max_digits=10,decimal_places=2,verbose_name='otherPayment',null=True)
Gross = models.DecimalField(max_digits=50,decimal_places=2,verbose_name='Gross')
Pf = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Pf')
Esi = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Esi')
FoodingDeduct = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='Fooding')
OtherDeduction =
models.DecimalField(max_digits=10,decimal_places=2,verbose_name='OtherDeduction')
TotalDeduction =
models.DecimalField(max_digits=10,decimal_places=2,verbose_name='TotalDeduction')
NetPayment = models.DecimalField(max_digits=50,decimal_places=2,verbose_name='NetPayment')
Date = models.DateField(default=timezone.now)
def __str__(self):
return '{EmpId}/{Name}/{date}'.format(EmpId=self.EmpId,Name=self.Name,date=self.Date)
My WagesView ниже
def AttendenceAward(datefrom,dateto,EmpId):
att = Attendence.objects.filter(AttendenceOn__range=[datefrom,dateto],EmpId=EmpId)
absent = 0
present = 0
for attendence in att:
if(attendence.Status=="Absent"):
absent = absent+1
if(attendence.Status=="Present"):
present = present+1
if(absent>3):
award = 0
elif(absent==0):
award = present*40
elif(absent==1):
award = present*35
elif(absent==2):
award = present*30
return award
def PresentDays(datefrom,dateto,EmpId):
att = Attendence.objects.filter(AttendenceOn__range=[datefrom,dateto],EmpId=EmpId)
present = 0
for attendence in att:
present = present + attendence.Days
return present
def OtDone(datefrom,dateto,EmpId):
att = Attendence.objects.filter(AttendenceOn__range=[datefrom,dateto],EmpId=EmpId)
ot = 0
for attendence in att:
ot = ot + attendence.DoubleOt
return ot
def WagesView(request):
datefrom = request.POST.get('datefrom')
dateto = request.POST.get('dateto')
year = YearForm()
print("From : ",datefrom,"To : ",dateto)
if request.method == 'POST':
try:
site = str(request.user.SuperVisor.Site)
print("string in try catch",site)
att = Attendence.objects.filter(AttendenceOn__range=[datefrom,dateto],Site=site)
emp = EmployeeRegistration.objects.filter(Status="Working")
except:
msg = "Please fill valid date range!"
return render(request,"wages.html",{"msg":msg})
wagesform = WagesForm()
#rate = EmployeeRate.objects.get(EmpId=empid,Site=request.user.SuperVisor.Site)
#basicrate=rate.Basic
#darate=rate.Da
for attendence in emp:
days = PresentDays(datefrom,dateto,attendence.EmpId)
if(days!=0):
WagesObj = wagesform.save(commit=False)
print(attendence.EmpId,attendence.Name)
rate = EmployeeRate.objects.get(EmpId=attendence.EmpId,Site=request.user.SuperVisor.Site)
basicrate=rate.Basic
darate=rate.Da
incentive = AttendenceAward(datefrom,dateto,attendence.EmpId)
print("incentiv",incentive)
ot = OtDone(datefrom,dateto,attendence.EmpId)
print(rate.Basic,rate.Da)
WagesObj.EmpId = attendence.EmpId
WagesObj.Name = attendence.Name
WagesObj.Site = site
print(WagesObj.Site)
WagesObj.Days = days
print("Total Days Worked",WagesObj.Days)
WagesObj.Nh = 0
WagesObj.DoubleOt = ot
WagesObj.Basic_rate = basicrate
WagesObj.Da_rate = darate
WagesObj.Basic_Amt = basicrate*WagesObj.Days
WagesObj.Da_Amt = darate * WagesObj.Days
WagesObj.Ot_Amt = decimal.Decimal(((basicrate + darate) / 8))*ot*2
WagesObj.FoodingAdd = 0
WagesObj.AttendenceAward = incentive
WagesObj.OtherPayment = 0
WagesObj.Gross = (WagesObj.Basic_Amt+WagesObj.Da_Amt+WagesObj.Ot_Amt+WagesObj.FoodingAdd+WagesObj.AttendenceAward+WagesObj.OtherPayment )
WagesObj.Pf = (decimal.Decimal((WagesObj.Basic_Amt+WagesObj.Da_Amt)*12))/100
WagesObj.Esi = (WagesObj.Gross *decimal.Decimal(0.75))/100
WagesObj.FoodingDeduct = 0
WagesObj.OtherDeduction = 0
WagesObj.TotalDeduction = WagesObj.Pf+WagesObj.Esi+WagesObj.FoodingDeduct+WagesObj.OtherDeduction
WagesObj.NetPayment = WagesObj.Gross-WagesObj.TotalDeduction
WagesObj.save()
context = {"yearform":year}
return render(request,"wages.html",context)
Моя проблема в том, что если 10 сотрудников работают каждый день в любой месяц. Затем следует сохранить заработную плату всего 10 сотрудникам за этот месяц. Но в моем случае зарплата сохраняется только одному сотруднику. Это происходит в последнем из l oop.
Я не могу найти ошибку в моем WagesView.
Пожалуйста, дайте мне идею или какое-либо решение. Вот почему это так. происходит так.