在django中保存工资模型时的问题

问题描述 投票:0回答:1

我同时具有四个模型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)

下面是我的工资视图

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名员工的工资。但就我而言,工资只能节省一名员工。这发生在循环的最后。

我无法在WagesView中找到错误。

[请给我一些想法或任何解决方案。这就是为什么会这样。

django python-3.x django-models django-views
1个回答
0
投票

请重新定义您的问题并提供minimal reproducible example

© www.soinside.com 2019 - 2024. All rights reserved.