Friday, February 25, 2022

Re: Salary Generate

On Tue, Feb 15, 2022 at 06:32:34AM -0800, Feroz Ahmed wrote:
> i am stuck at point to generate salaries for upcoming month,
> as i already have last month January data as fields(month, name , gross
> sal, tax deduction, net sal)
> and it has records of 35 (employees)
> in form template i have placed submit button to generate salaries for
> February.
>
> ****Plz help how to get all get data as it is from previous Month month
> AND new records to be as for month February
>
> ****** month field data for 35 record to be as new records and month values
> as **February
> data file attached.
>
> Class employee(models. Model)
> month=models.CharField(max_length=64)
> name= models.CharField(max_length=64)
> gross sal= models.IntegerField()
> tax= models.IntegerField()
> net sal= models.IntegerField()


Hey Feroz,

Since you mentioned getting the data by "previous month", let's start
there. Your month field is a CharField so I assume your storing the
names of the months in there. If so, you'll need a way to figure out
what the previous month is. Let's say the current month is February,
you could use a list of month names[1] and lookup the index of the
current month:


month_names = list(calendar.month_name)[1:]
month_index = month_names.index('February')

Then get the previous month by subtraction:

previous_month_index = (month_index - 1) % 12
previous_month = month_names[previous_month_index]

But you have another problem. You're not keeping track of the year.
So, at most you can only store one year's worth of salary records.
So at this point, I'd recommend replacing the CharField month field with
a DateField. That solves your problem of keeping track of the year.
Then you can also use some date math[2] to calculate the previous month.

Once you have the previous month, you can use that to: lookup that model
instance, copy it[3], update the month to the current month, then save.

As an aside, I'd rename your model class to something that makes more
sense, from 'Employee' to 'PayRecord`


Hope this helps!

Ryan N


[1] https://docs.python.org/3/library/calendar.html#calendar.month_name
[2] https://stackoverflow.com/a/9725093/226697
[3] https://docs.djangoproject.com/en/3.2/topics/db/queries/#copying-model-instances

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20220225205324.GF11627%40fattuba.com.

No comments:

Post a Comment