Search code examples
pythonmysqlpython-2.7tornadopeewee

Guidance needed for leave Report logic and functions in python


I am working in python 2.7, tornado web service, peewee for database.

Scenario To get leave report for each month 26 to next month 25.

So I did this below sample scenario(but logically i am stuck it out)

I am getting from_date,to_date from UI(selecting report for 26-04-2016 to 25-05-2016)

Leavetable is a table name (it has Employee id, leave from date, leave to date, working_days)

working_days is nothing but count of each leave(29-04-2016 - 02-05-2016) = 2 (saturday and sunday excluded already)

For a single employee total leave in particular month sample leaves are

  • 24-04-16 - 26-04-16,

  • 28-04-16 - 29-04-16 ,

  • 15-05-2016 to 29-05-2016

My code (Help me to change)

       value=0
       for report in Leavetable.select().where(Leavetable.Employee_ID==employees_id):
            db_from_date = report.From_Date
            if (from_date<=db_from_date and to_date>=db_from_date):
                workingday=float(report.Working_Days)+value
                value= workingday
                print value

It getting in value = 3 for the same leave above of

  • 24-04-16 - 26-04-16(it is have to take 1day but not working out by this code)

  • 28-04-16 - 29-04-16(it is taken (2 days)

  • 15-05-2016 to 29-05-2016( 26,27,28,29 have to exclude this 4 days)

Guide me what is the simplest solution to get this logic work out please do the needful.

As per solarflare guidance I added the below code.

But even logically its not fit.The above scenario leave of 15-05-2016 to 29-05-2016 This leave also counting when i am taking report of 15-04-2016 to 15-05-2016

        value=0
        for report in Leavetable.select().where(Leavetable.Employee_ID==employees_id):
            for dt in rrule(DAILY, dtstart=report.From_Date,until=report.To_Date):
                if (dt.weekday() < 5):
                    if (report.To_Date>=from_date and report.From_Date<=to_date):
                        value += 1 
                    print value

Solution

  • I Altered the above code with basic if logic and the above scenario got satisfied The code I listed below may be some variable name may difficult to understand if any comment here:

                        if (db_from_date>= prev_month and db_to_date<=this_month):
                            counting = float(working_day)
                            leave_availed=value+counting
                            value=leave_availed
    
                        elif(db_from_date >= prev_month and db_from_date <= this_month and db_to_date > this_month):
                            counting = float(working_day)
                            check_point =counting + 0.5
                            extra_days = workdays.networkdays(this_month_date,db_to_date,holidays)
                            if int(check_point)==check_point:
                                extra_days=extra_days - 0.5
                            count=counting-extra_days
                            leave_availed=value+count
                            value=leave_availed
    
                        elif(db_from_date < prev_month and db_to_date>=prev_month and db_to_date <= this_month):
                            counting = float(working_day)
                            extra_days = workdays.networkdays(db_from_date,prev_month_date,holidays)
                            count = counting - extra_days
                            leave_availed=value+count
                            value=leave_availed
    
                        elif(db_from_date < prev_month and db_to_date > this_month):
                            count = workdays.networkdays(prev_month,this_month,holidays)
                            leave_availed=value+count
                            value=leave_availed
    
                        else:
                            leave_availed=0