Untitled

                Never    
# NEW PAYROLL DISPUTE REPORT  THIS IS THE TEST
def send_payroll_dispute_new_range_reports(request, from_date, to_date, employee_id):
    #In order not type NA everywhere
    non_applicable = ""
    #Get the from and to date plus the employee ID
    from_date = datetime.strptime(from_date, '%Y-%m-%d').date()
    to_date = datetime.strptime(to_date, '%Y-%m-%d').date()
    date_list = []  #Prepare list for dates in question
    employee = Employee.objects.get(id=employee_id)
    print("")
    print(employee.name)
    print("")
    #Populate the date list
    for date in daterange(from_date, to_date):
        date_list.append(date)


    csvfile = io.StringIO()
    writer = csv.writer(csvfile)
    writer.writerow([employee.name.upper()])
    writer.writerow([''])
    writer.writerow(['DATE', 'JOB', 'REGULAR', 'OVERTIME', 'DOUBLE TIME', 'HOLIDAY', 'HOLIDAY PAY', 'WORKED ON STAT'])
    #writer.writerow([manager_entry.report_date, manager_entry.job.name, regular_hours, overtime_hours, double_hours, is_stat, entitled_to, hours_worked_on_stat])

    #This goes true when we have exceeded 40 hours in a week
    over_40_mode = False

    #Keep track of the hours worked since last monday
    hours_worked_since_monday = 0

    #Create jobs dictionary to add TOTALS
    jobs_totals_dict = {}

    for date in date_list:
        #If it is a monday reset the 40 hours counter to zero
        if date.today().weekday() == 0:
            hours_worked_since_monday = 0

        #Check if date is stat holiday and if employee is entitled to get paid
        is_stat = check_stat(date)
        if is_stat:
            is_entitled = check_stat_entitled(employee_id, date)
        else:
            is_entitled = non_applicable

        #manager_entries_qs = ManagerEntry.objects.filter(report_date=date, employeeentry__employee=employee)
        employee_entries_qs = EmployeeEntry.objects.filter(employee=employee, manager_entry__report_date=date).values('employee', 'manager_entry__job__name').annotate(total_hours=Sum('hours') + Sum('equipment_hours'))
        #jobs_qs = employee_entries_qs.values("manager_entry__job").distinct()
        #Print to the terminal for debugging
        print("")
        print(date)
        print(employee_entries_qs)

        #Create the necessary variables we need per date
        regular_hours = 0
        overtime_hours = 0
        double_hours = 0
        holiday_pay = 0




        #If guy didn't work that day put everything on zeroes
        if len(employee_entries_qs) == 0:
            job = non_applicable
            my_regular = 0
            my_overtime = 0
            my_doubletime = 0

            #If employee is entitled to holiday pay then pay him!
            if is_entitled == True and holiday_pay == 0:
                holiday_pay = 8
                my_holiday_pay = 8
            else:
                my_holiday_pay = 0

            writer.writerow([str(date), job, my_regular, my_overtime, my_doubletime, is_stat, holiday_pay, '#WORKED ON STAT#'])
        else:
            #We are at a  specific day, let's now iterate over the employee entries on that day!
            for employee_entry in employee_entries_qs:
                #Keep track of the numbers for each individual employee entry
                my_regular = 0
                my_overtime = 0
                my_doubletime = 0

                #If employee is entitled to holiday pay then pay him!
                if is_entitled == True and holiday_pay == 0:
                    holiday_pay = 8
                    my_holiday_pay = 8
                else:
                    my_holiday_pay = 0

                #Get the job name for the employee entry
                job = employee_entry['manager_entry__job__name']

                #Get the number of hours that the employee worked on this job/day
                available_hours = employee_entry['total_hours']
                #If he worked then ump into the process
                if available_hours > 0:
                    #If there is space in regular_hours
                    if regular_hours < 8 and available_hours > 0:
                        space_left_regular = 8 - regular_hours
                        #If we have less hours than space in regular hours
                        if available_hours <= space_left_regular:
                            regular_hours += available_hours
                            my_regular += available_hours
                            available_hours = max(0, available_hours - space_left_regular)
                            #print("Place 1")
                            #print(available_hours)
                        #Elif we have more hours than space in regular_hours
                        elif available_hours > space_left_regular:
                            available_hours = max(0, available_hours - space_left_regular) #solo metele lo que le quepa 1
                            regular_hours += space_left_regular
                            my_regular += space_left_regular
                            #print("Place 2")
                            #print(available_hours)
                    if regular_hours == 8 and overtime_hours < 4 and available_hours > 0:
                        space_left_overtime = 4 - overtime_hours
                        if available_hours <= space_left_overtime:
                            overtime_hours += available_hours
                            my_overtime += available_hours
                            available_hours = max(0, available_hours - space_left_overtime)
                            #print("Place 3")
                            #print(available_hours)
                        elif available_hours > space_left_overtime:
                            available_hours -= space_left_overtime #solo metele lo que le quepa 1
                            overtime_hours += space_left_overtime
                            my_overtime += space_left_overtime
                            #print("Place 4")
                            #print(available_hours)
                    if regular_hours == 8 and overtime_hours == 4 and double_hours < 12 and available_hours > 0:
                        space_left_doubletime = 12 - double_hours
                        if available_hours <= space_left_doubletime:
                            double_hours += available_hours
                            my_doubletime += available_hours
                            #print(available_hours)
                        elif available_hours > space_left_doubletime:
                            available_hours -= space_left_doubletime #solo metele lo que le quepa 1
                            double_hours += space_left_doubletime
                            my_doubletime += space_left_doubletime
                            #print(available_hours)

                    #Print to terminal for debugging
                    print("Reg: " + str(my_regular))
                    print("OverT: " + str(my_overtime))
                    print("DoubleT: " + str(my_doubletime))

                    writer.writerow([str(date), job, my_regular, my_overtime, my_doubletime, is_stat, my_holiday_pay, '#WORKED ON STAT#'])

                    #Add the toltal for this employee entry to the jobs total dict
                    if job in jobs_totals_dict:
                        jobs_totals_dict[job] += np.array([my_regular, my_overtime, my_doubletime])
                        #[a+b for a, b in zip(list1, list2)]
                    else:
                        jobs_totals_dict[job] = np.array([my_regular, my_overtime, my_doubletime])

                    print(jobs_totals_dict)

    print("")
    print("---------------------")
    print("JOB TOTALS")
    writer.writerow(["", "", "", "", "", ""])
    for job in jobs_totals_dict:
        print(str(job))
        print("Regular: ", jobs_totals_dict[job][0])
        print("Overtime: ", jobs_totals_dict[job][1])
        print("Double: ", jobs_totals_dict[job][2])
        print("")
        writer.writerow([job, "", jobs_totals_dict[job][0], jobs_totals_dict[job][1], jobs_totals_dict[job][2]])





    #Nicely separate each report on the terminal
    print("")
    print("")
    print("")

    #message = EmailMessage("Payroll summary submitted" , 'Please see the attached payroll summary.<br><br>', 'app@kettlerivercontracting.com', [request.user.email])
    #message.content_subtype = "html"                                                                                                                                                         # mario_solorzano@me.com
    #message.attach('payroll_summary.csv', csvfile.getvalue(), 'text/csv')
    #message.send()
    #messages.add_message(request, messages.SUCCESS, 'Payroll summary has been sent.')
    csvfile.seek(0)
    response = HttpResponse(csvfile, content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename=payroll_dispute.csv'

    return response

Raw Text