Guest User

Untitled

a guest
Jul 30th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 432.41 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. #    OpenERP, Open Source Management Solution
  5. #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU Affero General Public License as
  9. #    published by the Free Software Foundation, either version 3 of the
  10. #    License, or (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU Affero General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU Affero General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from __future__ import division
  22. import time
  23. import math
  24. import random
  25.  
  26.  
  27. from openerp.osv import fields, osv
  28. from openerp.tools.translate import _
  29. from datetime import datetime ,timedelta
  30. from dateutil.relativedelta import relativedelta
  31. from tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
  32. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
  33. from datetime import datetime
  34. import xlwt
  35. from xlwt import Workbook, XFStyle, Borders, Pattern, Font, Alignment,  easyxf
  36. import cStringIO
  37. import base64, urllib
  38.  
  39.  
  40.    
  41.  
  42. class hr_calculate_salary(osv.osv_memory):
  43.     _name = 'hr.calculate.salary1'
  44.     _columns = {
  45.                 'employee_id': fields.many2one('hr.employee','Employee', size=32, ),
  46.                 'holiday_list_id': fields.many2one('holiday.list','Months', size=32, ),
  47.                 'date': fields.date("Date",),
  48.                 'by_month': fields.boolean('By Month'),
  49.                
  50.                 'address_id' : fields.many2one('res.partner','Working Address'),
  51.                 'category': fields.selection([('worker', 'Worker'), ('staff', 'Staff'), ('contractor', 'Contractor')], 'Category',),
  52.                 }
  53.     _defaults={
  54.                #'by_month':True,
  55.                 }
  56.    
  57.    
  58.  
  59.     def get_month_year(self ,cr ,uid ,ids ,date ,context=False):
  60.         dict={'holiday_id':[],'day':False,'month':False,'year_id':False}
  61.         tm_tuple = datetime.strptime(str(date),'%Y-%m-%d').timetuple()
  62.         month1 = tm_tuple.tm_mon
  63.         #print "month1............",month1
  64.         year = tm_tuple.tm_year  
  65.         #print "year...........",year  
  66.         if int(month1) in [1,3,5,7,8,10,12]:
  67.             month = 31
  68.         if int(month1) in [4,6,9,11]:
  69.             month = 30
  70.         if int(month1) in [2]:
  71.             if int(year) % 4 == 0:
  72.                 month = 29
  73.             else:
  74.                 month = 28  
  75.         year_ids=self.pool.get('holiday.year').search(cr ,uid ,[('name','=',int(year))])
  76.         if not year_ids:
  77.             raise osv.except_osv(_('Warning !'),_("First ,Create a holiday year = %s." % (year)))
  78.         holiday_id=self.pool.get('holiday.list').search(cr ,uid ,[('month','=',int(month1)),('year_id','=',year_ids and year_ids[0])])
  79.         if not holiday_id:
  80.             raise osv.except_osv(_('Warning !'),_("No Holidays are defined for month= %s and year = %s." % (month1,int(year))))
  81.         #print "holiday_id................",holiday_id
  82.         dict.update({'holiday_id':holiday_id,'month':month1,'year_id':year_ids,'day':tm_tuple.tm_mday})
  83.         return dict
  84.     def float_time_convert_main(self,float_val):
  85.             factor = float_val < 0 and -1 or 1
  86.             val = abs(float_val)
  87.             return (factor * int(math.floor(val)), int(round((val % 1) * 60)))
  88.    
  89.     def float_time_convert(self,float_val):
  90.         factor = float_val < 0 and -1 or 1
  91.         val = abs(float_val)
  92.         str_time=str(val % 1).split('.')[1]
  93.         #print "str_time.......",str_time
  94.         return (factor * int(math.floor(val)), int(str_time))
  95.    
  96.     def calculate_time(self, cr, uid, ids, date, start, end):
  97.         #print "start.........end..",start,end
  98.         time1 = time2 = '00:00'
  99.         val1=self.float_time_convert(float(start))
  100.         #print "val1.........",val1
  101.         if val1 and len(str(val1[1])) == 1:
  102.             time1 = str(val1[0]) +':'+ '0' + str(val1[1])
  103.        
  104.         if val1 and len(str(val1[1])) == 2:
  105.             time1 = str(val1[0]) +':'+str(val1[1])
  106.        
  107.         start_hr = val1[0]
  108.         start_time = str(date) +' '+ str(time1) + ':00'
  109.        
  110.         val2=self.float_time_convert(float(end))
  111.         #print "val2..........",val2
  112.         if val2 and len(str(val2[1])) == 1:
  113.             time2 = str(val2[0]) +':'+ '0' + str(val2[1])
  114.        
  115.         if val2 and len(str(val2[1])) == 2:
  116.             time2 = str(val2[0]) +':'+str(val2[1])
  117.        
  118.         end_hr = val2[0]
  119.         #print "start_time...........",start_time
  120.         start_time1 = datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S').timetuple()
  121.         year = start_time1.tm_year
  122.         mon = start_time1.tm_mon
  123.         day = start_time1.tm_mday
  124.         hour = start_time1.tm_hour
  125.         min  = start_time1.tm_min
  126.         sec = start_time1.tm_sec
  127.         hour1 = hour
  128.         mon1 = mon
  129.         day1 = day
  130.         year1 = year
  131. #===========================================================================
  132.         if int(start_hr) > int(end_hr):
  133.             day2 = day + 1
  134.             if mon in [1,3,5,7,8,10,12]:
  135.                 if day >= 31:
  136.                     day2 = 1
  137.                     mon2 = mon + 1
  138.                     year2 = year
  139.                     if mon2 > 12:
  140.                         year2 = year + 1
  141.                         mon2 = 1
  142.                 else:
  143.                     mon2 = mon
  144.                     year2 = year
  145.                
  146.             elif mon in [4,6,9,11]:
  147.                 if day >= 30:
  148.                     day2 = 1
  149.                     mon2 = mon + 1
  150.                     year2 = year
  151.                 else:
  152.                     mon2 = mon
  153.                     year2 = year
  154.             elif mon == 2:
  155.                 if year % 4 == 0:
  156.                     if day >= 29:
  157.                         day2 = 1
  158.                         mon2 = mon + 1
  159.                         year2 = year
  160.                     else:
  161.                         mon2 = mon
  162.                         year2 = year
  163.                 else:
  164.                     if day >= 28:
  165.                         day2 = 1
  166.                         mon2 = mon + 1
  167.                         year2 = year
  168.                     else:
  169.                         mon2 = mon
  170.                         year2 = year
  171.             time2_split = time2.split(':')
  172.             hour2 = int(end_hr)
  173.             min2 = int(time2_split[1])
  174.         else:
  175.             time2_split = time2.split(':')
  176.             hour2 = int(end_hr)
  177.             min2 = int(time2_split[1])
  178.             mon2 = mon
  179.             day2 = day
  180.             year2 = year            
  181.        
  182.         if (hour + 22) > 24:
  183.             hour = abs(24 - (hour + 22))
  184.             day = day + 1
  185.                                    
  186.             if mon in [1,3,5,7,8,10,12]:
  187.                 if day > 31:
  188.                     day = 1
  189.                     mon = mon + 1
  190.                 if mon > 12:
  191.                     year = year + 1
  192.                     mon = 1
  193.             elif mon in [4,6,9,11]:
  194.                 if day > 30:
  195.                     day = 1
  196.                     mon = mon + 1
  197.             elif mon == 2:
  198.                 if year % 4 == 0:
  199.                     if day > 29:
  200.                         day = 1
  201.                         mon = mon + 1
  202.                 else:
  203.                     if day > 28:
  204.                         day = 1
  205.                         mon = mon + 1
  206.         else:
  207.             hour = hour + 22
  208.            
  209.         if len(str(mon1)) < 2:
  210.             mon1 = '0'+str(mon1)
  211.         if len(str(day1)) < 2:
  212.             day1 = '0'+str(day1)
  213.         if len(str(mon2)) < 2:
  214.             mon2 = '0'+str(mon2)
  215.         if len(str(day2)) < 2:
  216.             day2 = '0'+str(day2)
  217.         if len(str(mon)) < 2:
  218.             mon = '0'+str(mon)
  219.         if len(str(day)) < 2:
  220.             day = '0'+str(day)
  221.  
  222.         start_time = str(year1) +'-'+str(mon1)+'-'+str(day1) +' '+ str(hour1)+':'+str(min)+':'+str(sec)
  223.         end_time = str(year2) +'-'+str(mon2)+'-'+str(day2) +' '+ str(hour2)+':'+str(min2)+':'+str('00')
  224.         final_time = str(year) +'-'+str(mon)+'-'+str(day) +' '+ str(hour)+':'+str(min)+':'+str(sec)
  225.         working_time = (datetime.strptime(end_time,'%Y-%m-%d %H:%M:%S') - datetime.strptime(start_time,'%Y-%m-%d %H:%M:%S'))
  226.         working_hour = working_time.total_seconds()/3600
  227.         timing = {
  228.                   'start_time':start_time,
  229.                   'end_time':end_time,
  230.                   'final_time':final_time,
  231.                   'working_hour':working_hour,
  232.         }
  233.         return timing
  234.     def working_hours_on_day(self, cr, uid, resource_calendar_id, day, context=None):
  235.         shift = {
  236.                  'start':0.0,
  237.                  'end':0.0,
  238.         }
  239.         count = 0
  240.         print "Working day "
  241.         for working_day in resource_calendar_id.attendance_ids:
  242.             if count > 1:
  243.                 continue
  244.             count += 1
  245.             val1 = self.float_time_convert_main(working_day.hour_from)
  246.             #print "val1.............",val1
  247.             if val1 and len(str(val1[1])) == 1:
  248.                 time1 = str(val1[0]) +'.'+str(val1[1]) + '0'
  249.            
  250.             if val1 and len(str(val1[1])) == 2:
  251.                 time1 = str(val1[0]) +'.'+str(val1[1])
  252.             val2 = self.float_time_convert_main(working_day.hour_to)
  253.             #print "val2............",val2
  254.             if val2 and len(str(val2[1])) == 1:
  255.                 time2 = str(val2[0]) +'.'+ str(val2[1]) + '0'
  256.             if val2 and len(str(val2[1])) == 2:
  257.                 time2 = str(val2[0]) +'.'+str(val2[1])
  258.             shift.update({'start':time1,'end':time2})
  259.            
  260.         return shift
  261.    
  262.     def working_hours_on_day_weekly(self, cr, uid, resource_calendar_id, day, context=None):
  263.         shift = {
  264.                  'start':0.0,
  265.                  'end':0.0,
  266.         }
  267.         count = 0
  268.         print "Working day "
  269.         val1 = self.float_time_convert_main(resource_calendar_id.hour_from)
  270.             #print "val1.............",val1
  271.         if val1 and len(str(val1[1])) == 1:
  272.             time1 = str(val1[0]) +'.'+str(val1[1]) + '0'
  273.        
  274.         if val1 and len(str(val1[1])) == 2:
  275.             time1 = str(val1[0]) +'.'+str(val1[1])
  276.         val2 = self.float_time_convert_main(resource_calendar_id.hour_to)
  277.         #print "val2............",val2
  278.         if val2 and len(str(val2[1])) == 1:
  279.             time2 = str(val2[0]) +'.'+ str(val2[1]) + '0'
  280.         if val2 and len(str(val2[1])) == 2:
  281.             time2 = str(val2[0]) +'.'+str(val2[1])
  282.         shift.update({'start':time1,'end':time2})
  283.            
  284.         print "========================",shift    
  285.         return shift
  286.    
  287.     def calculate_ot(self ,cr ,uid ,ids ,context=False):
  288.         att_obj = self.pool.get('hr.attendance')
  289.         emp_obj = self.pool.get('hr.employee')
  290.         punch_obj = self.pool.get('hr.punch.card')
  291.         ot_obj = self.pool.get('ot.history')
  292.         employee_id = self.browse(cr ,uid , ids[0]).employee_id
  293.         print "employee_id..........",employee_id
  294.         salary_date= self.browse(cr ,uid , ids[0]).date
  295.         print "salary_date...........",salary_date
  296.         salary_month= self.browse(cr ,uid , ids[0]).holiday_list_id
  297.         print "salary_month...........",salary_month
  298. # ++++++++++++++ An Employeess  For a Date  ++++++++++++++++++++++++++++++        
  299.         if employee_id:
  300.             if salary_date:
  301.                 dict = self.get_month_year(cr ,uid ,ids ,salary_date)
  302.                 print "dict................",dict
  303.                 ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  304.                 #print "ot_ids...........",ot_ids
  305.                 punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  306.                 #print "punch_ids............",punch_ids
  307.                 shown_ot=0.0
  308.                 not_shown_ot=0.0
  309.                 for punch in punch_ids:
  310.                     for p in punch_obj.browse(cr ,uid ,[punch]):
  311.                         shown_ot += p.new_ot
  312.                         not_shown_ot +=p.extra_ot
  313.                 if ot_ids:
  314.                     ot_obj.write( cr ,uid ,ot_ids ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot})
  315.                 else:
  316.                     ot_obj.create(cr ,uid ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot,'employee_id':employee_id.id,'month_id':dict['holiday_id'] and dict['holiday_id'][0],})
  317.                 print salary_date
  318.             elif salary_month:
  319.                 month1=salary_month.month
  320.                 print "month1.........",month1
  321.                 #print "month......... ",month1
  322.                 year=salary_month.year_id.name
  323.                 #print "year............",year
  324.                 year_ids=self.pool.get('holiday.year').search(cr ,uid ,[('name','=',int(year))])
  325.                 #print "year_ids...........",year_ids
  326.                
  327.                 punch_ids=punch_obj.search(cr ,uid ,[('month','=',month1),('year_id','=',year_ids and year_ids[0]),('employee_id','=',employee_id.id)])
  328.                 #print "punch_ids............",punch_ids
  329.                 holiday_id=self.pool.get('holiday.list').search(cr ,uid ,[('month','=',int(month1)),('year_id','=',salary_month.year_id.id)])
  330.                 #print "holiday_id..........",holiday_id
  331.                 ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',holiday_id and holiday_id[0]),('employee_id','=',employee_id.id)])
  332.                 #print "ot_ids...........",ot_ids
  333.                 shown_ot=0.0
  334.                 not_shown_ot=0.0
  335.                 for punch in punch_ids:
  336.                     for p in punch_obj.browse(cr ,uid ,[punch]):
  337.                         shown_ot += p.new_ot
  338.                         not_shown_ot +=p.extra_ot
  339.                 if ot_ids:
  340.                     ot_obj.write( cr ,uid ,ot_ids ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot})
  341.                 else:
  342.                     ot_obj.create(cr ,uid ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot,'employee_id':employee_id.id,'month_id':holiday_id and holiday_id[0],})
  343.                 print salary_month
  344.         else:
  345.             if salary_date:
  346.                 dict = self.get_month_year(cr ,uid ,ids ,salary_date)
  347.                 print "dict................",dict
  348.                 emp_list=emp_obj.search(cr,uid,[])  
  349.                 if 1 in emp_list:
  350.                     emp_list.remove(1)
  351.                 if emp_list:
  352.                     for emp_id in emp_list:
  353.                         for emp in emp_obj.browse(cr ,uid ,[emp_id]):
  354.                            
  355.                             ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',emp.id)])
  356.                             #print "ot_ids...........",ot_ids
  357.                             punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',emp.id)])
  358.                             #print "punch_ids............",punch_ids
  359.                             shown_ot=0.0
  360.                             not_shown_ot=0.0
  361.                             for punch in punch_ids:
  362.                                 for p in punch_obj.browse(cr ,uid ,[punch]):
  363.                                     shown_ot += p.new_ot
  364.                                     not_shown_ot +=p.extra_ot
  365.                             if ot_ids:
  366.                                 ot_obj.write( cr ,uid ,ot_ids ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot})
  367.                             else:
  368.                                 ot_obj.create(cr ,uid ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot,'employee_id':emp.id,'month_id':dict['holiday_id'] and dict['holiday_id'][0],})
  369.                             #print salary_date
  370.             elif salary_month :
  371.                 emp_list=emp_obj.search(cr,uid,[])  
  372.                 if 1 in emp_list:
  373.                     emp_list.remove(1)
  374.                 month1=salary_month.month
  375.                 print "month......... ",month1
  376.                 year=salary_month.year_id.name  
  377.                 #print "year............",year
  378.                 year_ids=self.pool.get('holiday.year').search(cr ,uid ,[('name','=',int(year))])
  379.                 if emp_list:
  380.                     for emp_id in emp_list:
  381.                         for emp in emp_obj.browse(cr ,uid ,[emp_id]):
  382.                            
  383.                             #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',month1),('employee_id','=',emp.id)])
  384.                             #print "ot_ids...........",ot_ids
  385.                             year_ids=self.pool.get('holiday.year').search(cr ,uid ,[('name','=',int(year))])
  386.                             punch_ids=punch_obj.search(cr ,uid ,[('month','=',month1),('year_id','=',year_ids and year_ids[0]),('employee_id','=',emp.id)])
  387.                             #print "punch_ids............",punch_ids
  388.                             holiday_id=self.pool.get('holiday.list').search(cr ,uid ,[('month','=',int(month1)),('year_id','=',salary_month.year_id.id)])
  389.                             ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',holiday_id and holiday_id[0]),('employee_id','=',employee_id.id)])
  390.                             shown_ot=0.0
  391.                             not_shown_ot=0.0
  392.                             for punch in punch_ids:
  393.                                 for p in punch_obj.browse(cr ,uid ,[punch]):
  394.                                     shown_ot += p.new_ot
  395.                                     not_shown_ot +=p.extra_ot
  396.                             if ot_ids:
  397.                                 ot_obj.write( cr ,uid ,ot_ids ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot})
  398.                             else:
  399.                                 ot_obj.create(cr ,uid ,{'shown_ot':shown_ot,'extra_ot':not_shown_ot,'employee_id':emp.id,'month_id':holiday_id and holiday_id[0],})
  400.                             #print salary_date
  401.         return True
  402.    
  403.     def timing_main_func(self ,cr ,uid ,ids ,working_hours,salary_date ,context=False):
  404.         shift = {
  405.                 'shift_start':0.0,
  406.                  'shift_end':0.0,
  407.                  'shift_final':0.0,
  408.             }
  409.         shift = self.working_hours_on_day(cr, uid, working_hours, salary_date, context)
  410.         #print "shift............",shift
  411.         start_time = datetime.strptime(str(datetime.strptime(str(salary_date),"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S')),"%Y-%m-%d %H:%M:%S")
  412.         hour= int(float(shift['start']))
  413.         min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  414.         if min and len(min) == 1:
  415.             min = str(min) +'0'
  416.         if min and len(min) == 1:
  417.             min = str(min)
  418.         start_time = str(hour) + '.' + str(min)
  419.         #print "start_time..............",start_time,type(start_time)
  420.         hour= int(float(shift['end']))
  421.         min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  422.         if min and len(min) == 1:
  423.             min = str(min) +'0'
  424.         if min and len(min) == 1:
  425.             min = str(min)
  426.         end_time = str(hour) + '.' + str(min)
  427.         #print "start_time......main........",start_time
  428.         #print "end_time................",end_time
  429.         salary_date = str(datetime.strptime(str(salary_date),"%Y-%m-%d").strftime('%Y-%m-%d'))
  430.         #print "salary_date.............",salary_date
  431.         timing_main = self.calculate_time(cr, uid, ids, salary_date, float(start_time), float(end_time))
  432.         #print "timing_main..............",timing_main
  433.         return timing_main
  434.    
  435.     def timing_main_weekly_func(self ,cr ,uid ,ids ,working_hours,salary_date ,context=False):
  436.         shift = {
  437.                 'shift_start':0.0,
  438.                  'shift_end':0.0,
  439.                  'shift_final':0.0,
  440.             }
  441.         shift = self.working_hours_on_day_weekly(cr, uid, working_hours, salary_date, context)
  442.         #print "shift............",shift
  443.         start_time = datetime.strptime(str(datetime.strptime(str(salary_date),"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S')),"%Y-%m-%d %H:%M:%S")
  444.         hour= int(float(shift['start']))
  445.         min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  446.         if min and len(min) == 1:
  447.             min = str(min) +'0'
  448.         if min and len(min) == 1:
  449.             min = str(min)
  450.         start_time = str(hour) + '.' + str(min)
  451.         #print "start_time..............",start_time,type(start_time)
  452.         hour= int(float(shift['end']))
  453.         min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  454.         if min and len(min) == 1:
  455.             min = str(min) +'0'
  456.         if min and len(min) == 1:
  457.             min = str(min)
  458.         end_time = str(hour) + '.' + str(min)
  459.         #print "start_time......main........",start_time
  460.         #print "end_time................",end_time
  461.         salary_date = str(datetime.strptime(str(salary_date),"%Y-%m-%d").strftime('%Y-%m-%d'))
  462.         #print "salary_date.............",salary_date
  463.         timing_main = self.calculate_time(cr, uid, ids, salary_date, float(start_time), float(end_time))
  464.         #print "timing_main..............",timing_main
  465.         return timing_main
  466.    
  467.     def calculate_punch_card123(self, cr, uid, ids, context=None):
  468.         print "Gaurav"
  469.         att_obj = self.pool.get('hr.attendance')
  470.         punch_obj = self.pool.get('hr.punch.card')
  471.         emp_obj = self.pool.get('hr.employee')
  472.         cont_obj = self.pool.get('hr.contract')
  473.         ot_obj = self.pool.get('ot.history')
  474.         holiday_obj = self.pool.get('holiday.list')
  475.         # ++++++++++++++++ Late Count (Gaurav) ++++++++++++++++++++++++++++
  476.        
  477.         cur_obj = self.browse(cr ,uid , ids)
  478.         for line in self.browse(cr ,uid , ids):
  479.             print "aaaaaaaaa",line.employee_id
  480.             if line.employee_id:
  481.                 employee_id = line.employee_id
  482.                 print "=========",employee_id
  483.             else:
  484.                 employee_id =False
  485.         #print "employee_id..........",employee_id
  486.         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  487.         salary_date= self.browse(cr ,uid , ids[0]).date
  488.         #print "salary_date...........",salary_date
  489.         salary_month= self.browse(cr ,uid , ids[0]).holiday_list_id
  490.         #print "salary_month...........",salary_month
  491. # ++++++++++++++ An Employeess  For a Date  ++++++++++++++++++++++++++++++        
  492.         print "ewtwwwwwwwwwwwwwwwwww====employee_id======salary_date====="
  493.         if employee_id :
  494.             print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%====employee_id======salary_date====="
  495.             if salary_date:
  496.                 print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%====employee_id==========="
  497.                 late_count = 0;
  498.                 ot_sum_gaurav = 0
  499.                 contract_ids = cont_obj.search(cr,uid,[('employee_id','=',employee_id.id)])
  500.                 if contract_ids:
  501.                     contract_data = cont_obj.browse(cr, uid, contract_ids)[0]
  502.                 else:
  503.                     raise osv.except_osv(_('Warning !'),_("Contract is missing for employee %s." % (employee_id.name)))
  504.                 #print "contract_data..............",contract_data,contract_data.working_hours.name
  505.                 if contract_data:
  506.                         category = employee_id.category
  507.                         timing_main= self.timing_main_func( cr ,uid ,ids ,contract_data.working_hours ,salary_date)
  508.                         #print "timing_main...........",timing_main
  509.                         total_punch = "select min(name + interval'5 hour 30 minutes') ,max(name + interval'5 hour 30 minutes') from hr_attendance where name + interval '5 hours 30 minute' >= to_timestamp('"+str(timing_main['start_time'])+"',\
  510.                        'YYYY-MM-DD HH24:MI:SS')::timestamp - interval '1 hours 30 minute' and name + interval '5 hours 30 minute' < to_timestamp('"+str(timing_main['final_time'])+"','YYYY-MM-DD HH24:MI:SS')::timestamp and employee_id = \
  511.                        '"+str(employee_id.id)+"' "
  512.                         cr.execute(total_punch)
  513.                         total_punch_result = cr.fetchall()
  514.                        
  515.                         for data in total_punch_result:  
  516.                             if len(data)>1 and data[0] != None and data[1] != None and data[0] != data[1]:
  517.                                 #if total_punch_result and len(total_punch_result[0]) == 2 and total_punch_result[0][0] != None and total_punch_result[0][1] != None and total_punch_result[0][0] != total_punch_result[0][1]:
  518.                                 in_punch = data[0]
  519.                                 #print "in_punch....................",in_punch
  520.                                 out_punch1 = data[1]
  521.                                 #print "out_punch1..................",out_punch1
  522.                                 #category =employee_id.category
  523.                                 in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  524.                                 #print "in_time.........",in_time
  525.                                 out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  526.                                 #print "out_time.........",out_time
  527.                                 dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  528.                                
  529.                                 timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  530.                                 #print "timing.................",timing
  531.                                 lunch_mid = late = hard_absent = short_leave = hard_hd = False
  532.                                 start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  533.                                 time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  534.                                 working_main= False
  535.                                 working_main_first_half = False
  536.                                 working_main_second_half = False
  537.                                 lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  538.                                 time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  539.                                 elunch = lunch + timedelta(hours=4,minutes=30)
  540. # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  541.                                 shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  542.                                 #print "shift............",shift
  543.                                 start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  544.                                 hour= int(float(shift['start']))
  545.                                 min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  546.                                 if min and len(min) == 1:
  547.                                     min = str(min) +'0'
  548.                                 if min and len(min) == 1:
  549.                                     min = str(min)
  550.                                 start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  551.                                 #print "start_time..............",start_time,type(start_time)
  552.                                 end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  553.                                 hour= int(float(shift['end']))
  554.                                 min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  555.                                 if min and len(min) == 1:
  556.                                     min = str(min) +'0'
  557.                                 if min and len(min) == 1:
  558.                                     min = str(min)
  559.                                 end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  560.                                 #print "end_time................",end_time
  561.                                 if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  562.                                     actual_start = start_time
  563.                                 else:
  564.                                     actual_start = in_punch
  565.                                 #print "actual_start............",actual_start
  566.                                 if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  567.                                     actual_end = end_time
  568.                                 else:
  569.                                     actual_end = out_punch1
  570.                                 #print "actual_end.............",actual_end
  571. #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  572.                                 #if category == 'staff':
  573.                                 if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=30)):
  574.                                     hard_absent =True
  575.                                     #print "setting absent1...hard_absent =True......."
  576.                                     working_main='A'
  577.                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  578.                                     #print "setting hd1..........."
  579.                                     working_main='HD'
  580.                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  581.                                     print "setting P1 and late.....492......12"
  582.                                     working_main ='P'
  583.                                     late_count = late_count+1
  584.                                     print "+++++++++++++++++++++++++++++==================================",late_count,in_punch
  585.                                     late=True
  586.                                     print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  587.                                     if late_count>=3 and category=='staff':
  588.                                         working_main ='A'
  589.                                        
  590.                                        
  591.                                         leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  592.                                         if leave_obj1:
  593.                                            
  594.                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  595.                                             self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  596.                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  597.                                             leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  598.                                        
  599.                                         cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  600.                                         ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  601.                                         el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  602.                                        
  603.                                         punch_in_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  604.                                         punch_in_hd= punch_in_hd.strftime("%Y-%m-%d 00:01:00")
  605.                                         punch_out_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  606.                                         punch_out_hd = punch_out_hd.strftime("%Y-%m-%d 00:01:00")
  607.                                        
  608.                                         print "==================================",punch_in_hd,punch_out_hd
  609.                                         error
  610.                                        
  611.                                         if cl_status[5]['remaining_leaves']>=0.5:
  612.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  613.                                             print "Hiii",create_id
  614.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  615.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  616.                                         elif ml_status[2]['remaining_leaves']>=0.5:
  617.                                             print "Ml"
  618.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  619.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  620.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  621.                                        
  622.                                         elif el_status[6]['remaining_leaves']>=0.5:
  623.                                             print "El"
  624.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  625.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  626.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  627.                                         else:
  628.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  629.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  630.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  631.                                     else:
  632.                                         working_main = 'P'
  633.                                 else:
  634.                                     #print "setting P1..........."
  635.                                     working_main ='P'
  636.                                
  637.                                 if not in_punch and not out_punch1:
  638.                                     working_main = "A"
  639.                                     #continue
  640.                                 #print "working_main..........",working_main
  641.         # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  642.                                 #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  643.                                 #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  644.                                 #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  645.                                 hard_hd = short_leave = False
  646.                                 if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  647.                                     if late_count>=3 and category=='staff':
  648.                                         print "Late Count Reseted$$$$$$$$$$$$$$$$$$$$$$$",late_count                                    
  649.                                         working_main = 'A'
  650.                                         #print "hard hd true....."
  651.                                     else:
  652.                                         working_main = 'HD'
  653.                                         hard_hd = True
  654.                                         #print "hard_hd......setting...",hard_hd
  655.                                 elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  656.                                   (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))):
  657.                                     #print "short leave  true....."
  658.                                     short_leave = True
  659.         # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  660. #                                missing = new_hrs_miss = 0.0
  661. #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  662. #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  663. #                                    missing = value.total_seconds()
  664. #                                    #print "missing................",missing
  665. #                                    miss_min = float(missing / 60)
  666. #                                    print "miss_min............",miss_min
  667. #                                    miss_hr = float(miss_min / 60)
  668. #                                    #print "miss_hr.................",miss_hr
  669. #                                    miss_hr = divmod(miss_hr,1)[0]
  670. #                                    #print "miss_hr.................",miss_hr
  671. #                                    if miss_hr:
  672. #                                        #print "miss_hr...............",miss_hr
  673. #                                        nw_min = miss_min - miss_hr * 60
  674. #                                        #print "nw_min...1............",nw_min
  675. #                                    else:
  676. #                                        nw_min = miss_min
  677. #                                        #print "nw_min......2.........",nw_min
  678. #                                    nw_min = round(nw_min,0)
  679. #                                    if nw_min > 5 and nw_min <= 35:
  680. #                                        nm_min = 0.5
  681. #                                    elif nw_min > 35:
  682. #                                        miss_hr = miss_hr + 1
  683. #                                        nm_min = 0.0
  684. #                                    else:
  685. #                                        nm_min = 0.0
  686. #                                    print "miss_hr............",miss_hr
  687. #                                    print "nm_min..............",nm_min
  688. #                                    new_hrs_miss = miss_hr + nm_min
  689. #                                    print "new_hrs_miss................",new_hrs_miss  
  690.         #                        if new_hrs_miss < 4.0:
  691.         #                            working_main ="A"
  692.         #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  693.         #                            working_main ="HD"
  694.         #                        elif new_hrs_miss >= 6.0 :
  695.         #                            working_main ="A"
  696.         #                        print "working_main............",working_main
  697.                                     # ++++ START TIME & END TIME +++++++++++++++++++
  698.                                 st= ed= False
  699.                                 if in_punch:
  700.                                     st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  701.                                 if out_punch1:
  702.                                     ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  703.         #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  704.                                 #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  705.                                 #print "ed.................",ed
  706.                                 #if category == 'staff':
  707.                                 if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  708.                                  ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  709.                                     lunch_mid =True
  710.     #                            if category != 'staff':
  711.     #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  712.     #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  713.     #                                    lunch_mid =True
  714.                                 #print "lunch_mid...........",lunch_mid
  715.         # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  716.                                 new_hrs=0.0
  717.                                 ot_hr = 0
  718.                                 ot_min = 0
  719.                                 if st and ed:
  720.         # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  721.        
  722.                                    
  723.                                     working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  724.                                     punch_min = float(working_hour.total_seconds() / 60)
  725.                                     if lunch_mid:
  726.                                         punch_min = punch_min - 30.0
  727.                                     punch_hr = float(punch_min / 60)
  728.                                     punch_hr = divmod(punch_hr,1)[0]
  729.                                     if punch_hr:
  730.                                         nw_min = punch_min - punch_hr * 60
  731.                                     else:
  732.                                         nw_min = punch_min
  733.                                    
  734.                                     if nw_min > 24 and nw_min < 55:
  735.                                         nm_min = 0.5
  736.                                     elif nw_min >= 55:
  737.                                         punch_hr = punch_hr + 1
  738.                                         nm_min = 0.0
  739.                                     else:
  740.                                         nm_min = 0.0
  741.                                     new_hrs = punch_hr + nm_min
  742.                                     #print "new_hrs..........",new_hrs,type(new_hrs)
  743.         # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  744.                                     over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  745.                                     #print "over_time.............",over_time
  746.                                     punch_min1 = float(over_time.total_seconds() / 60)
  747.                                    
  748.                                     punch_hr1 = float(punch_min1 / 60)
  749.                                     punch_hr1 = divmod(punch_hr1,1)[0]
  750.                                     if punch_hr1:
  751.                                         nw_min1 = punch_min1 - punch_hr1 * 60
  752.                                     else:
  753.                                         nw_min1 = punch_min1
  754.                                    
  755.                                     if nw_min1 > 25 and nw_min1 < 55:
  756.                                         nm_min1 = 0.5
  757.                                     elif nw_min1 >= 55:
  758.                                         punch_hr1 = punch_hr1 + 1
  759.                                         nm_min1 = 0.0
  760.                                     else:
  761.                                         nm_min1 = 0.0
  762.                                     new_hrs1 = punch_hr1 + nm_min1
  763.                                     #print "actual ot new_hrs1.........",new_hrs1
  764.                                     ot=round(new_hrs1/2,2)
  765.                                     #print "shown  ot.............",ot
  766.                                     extra_ot = 0.0
  767.                                     if ot > 2.0 and ot_sum_gaurav<16:
  768.                                         extra_ot= float(float(ot -2.0))
  769.                                         #print "ot setting to 2.0...extra_ot...........",extra_ot
  770.                                         ot= 2.0
  771.                                     if ot_sum_gaurav>15:
  772.                                         extra_ot= float(float(ot))
  773.                                         ot = 0.0
  774. # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  775.                                     val1=self.float_time_convert_main(ot)
  776.                                     hr=0
  777.                                     #print "val1...............",val1
  778.                                     time1 = 0.0
  779.                                     if val1 and len(str(val1[1])) == 1:
  780.                                         hr = 0
  781.                                         ot_hr = val1[0]
  782.                                         ot_min = hr
  783.                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  784.                                    
  785.                                     if val1 and len(str(val1[1])) == 2:
  786.                                         if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  787.                                             hr = 0
  788.                                         else:
  789.                                             hr = 5
  790.                                         ot_hr = val1[0]
  791.                                         ot_min = hr
  792.                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  793.                                     ot = time1
  794.                                     #print "new ot...........",ot
  795.                                     val2=self.float_time_convert_main(extra_ot)
  796.                                     hr1=0
  797.                                     #print "val2...............",val2
  798.                                     time2 = 0.0
  799.                                     if val2 and len(str(val2[1])) == 1:
  800.                                         hr1 = 0
  801.                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  802.                                    
  803.                                     if val2 and len(str(val2[1])) == 2:
  804.                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  805.                                             hr1 = 0
  806.         #                                elif val2[1] == 50:
  807.         #                                    hr1 = 5
  808.                                         else:
  809.                                              hr1 = 5
  810.                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  811.                                     extra_ot = time2
  812.                                     #print "new extra_ot..............",extra_ot
  813.         #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  814.                                 print "working_main............",working_main
  815.                                 if working_main == 'HD' or working_main == 'P':
  816.                                     #print "going in.........."
  817.                                     if new_hrs < 4.0:
  818.                                         working_main = 'A'
  819.                                         #print "working hours are less than 4.0.....",working_main
  820.                                     elif new_hrs >= 4.0 and new_hrs < 6.0:
  821.                                         working_main = 'HD'
  822.                                         #late = False
  823.                                         #print "working setting HD....",working_main
  824.                                     elif new_hrs >= 6.0 :
  825.                                         working_main = 'P'
  826.                                
  827.                                 if category == 'worker':
  828.                                     if working_main == 'HD' or working_main == 'P':
  829.                                     #print "going in.........."
  830.                                         if new_hrs < 4.0:
  831.                                             working_main = 'A'
  832.                                             #print "working hours are less than 4.0.....",working_main
  833.                                         elif new_hrs >= 4.0 and new_hrs < 7.5:
  834.                                             working_main = 'HD'
  835.                                             #late = False
  836.                                             #print "working setting HD....",working_main
  837.                                         elif new_hrs >= 7.5 :
  838.                                             working_main = 'P'
  839.                                        
  840.                                    
  841.                                    
  842.                                         #print "working setting P....",working_main
  843.                                 print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave
  844.                                 if working_main == 'P' and hard_hd:
  845.                                     working_main = 'HD'
  846.     #                            if working_main == 'HD' and hard_absent:
  847.     #                                working_main = 'A'
  848.                                 #print "working main....",working_main
  849.                                 if new_hrs1 < 0:
  850.                                     new_hrs1 = 0
  851.                                 if working_main == 'A':
  852.                                     short_leave = False
  853.                                     late = False
  854.                                 print "working main..working hour...ot.",working_main,new_hrs,new_hrs1
  855. #                                print "end_time.............",end_time,type(end_time)
  856. #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  857.                                 if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  858.                                     out_punch2 = out_punch1
  859.                                 else:
  860.                                     random_second = random.randrange(1, 59, 1)
  861.                                     random_min = random.randrange(1, 10, 1)
  862.                                     if ot_min >= 5 :
  863.                                         random_min +=  30
  864.                                     else:
  865.                                         random_min +=  0
  866.                                     #print "random_min..........",random_min
  867.                                     out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  868.                                     #print "out_punch2.............",str(out_punch2)
  869.                                     #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  870. #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  871. #                                print "out_punch2..........",str(out_punch2)
  872.                                 in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  873.                                 out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  874.                                 out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  875.                                 #print "ot hr ,ot min......",ot_hr, ot_min
  876.                                
  877.                                 if category == 'staff':
  878.                                     extra_ot = 0.0
  879.                                     ot = 0.0
  880.                                 if category != 'staff':
  881.                                     short_leave = False
  882.                                     late = False
  883.         # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  884.                                 holiday_flag = False
  885.                                 if late_count >=3:
  886.                                     late_count =0
  887.                                 try:
  888.                                     cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  889.                                     #print "deleted ...punch card .............",cr.fetchall()
  890.                                     #print "going................."
  891.                                     #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  892.                                     #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  893.                                     if dict['holiday_id']:
  894.                                         for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  895.                                             if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  896.                                                 holiday_flag = True
  897.                                     if holiday_flag :
  898.                                         working_main ='L'
  899.                                         short_leave = False
  900.                                         late = False
  901.                                     if holiday_flag and (category =='worker' or category=='contractor'):
  902.                                         print "ooooooooooooooooooooooootttttttttMy Block",
  903.                                         working_main ='L'
  904.  
  905.                                         punch_min1 = float(working_hour.total_seconds() / 60)
  906.                                    
  907.                                         # punch_hr1 = float(punch_min1 / 60)
  908.                                         # punch_hr1 = divmod(punch_hr1,1)[0]
  909.                                         punch_hr1 = float(punch_min1 / 60)
  910.                                         punch_hr1 = divmod(punch_hr1,1)[0]
  911.                                         if punch_hr1:
  912.                                             nw_min1 = punch_min1 - punch_hr1 * 60
  913.                                         else:
  914.                                             nw_min1 = punch_min1
  915.                                        
  916.                                         if nw_min1 > 25 and nw_min1 < 55:
  917.                                             nm_min1 = 0.5
  918.                                         elif nw_min1 >= 55:
  919.                                             punch_hr1 = punch_hr1 + 1
  920.                                             nm_min1 = 0.0
  921.                                         else:
  922.                                             nm_min1 = 0.0
  923.                                         new_hrs1 = punch_hr1 + nm_min1
  924.                                         extra_ot = new_hrs1
  925.                                         new_hrs = 0
  926.                                         ot = 0
  927.                                         print "Punch hr",punch_hr1,new_hrs1
  928.                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  929.                                             'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  930.                                             'out_punch2':str(out_punch2),'late':late,})
  931.                                                          
  932.                                     print "=========================NEW  PUNCH CREATED===========================",created_id
  933.                                 except Exception,e:
  934.                                     print "Exception Punch Card Creation...........", e.args
  935.                                     pass
  936.                             else:
  937.                                 print "IN and Out Pair attendance not found for emp on date......",employee_id.name,salary_date
  938.                                 return True
  939. # ++++++++++++++ SINGLE Employee  for Whole Month  ++++++++++++++++++++++++++++++
  940.             else:
  941.                 late_count = 0
  942.                 week_days = 0
  943.                 week_day = 0
  944.                
  945.                 week_off_first = True
  946.                 print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%==============="
  947.                 short_leave_late = 0
  948.                 ot_sum_gaurav = 0
  949.                 month1=salary_month.month
  950.                 print "month......... ",month1
  951.                
  952.                 year=salary_month.year_id.name  
  953.                 print "year............",year
  954.                 if int(month1) in [1,3,5,7,8,10,12]:
  955.                     month = 31
  956.                 if int(month1) in [4,6,9,11]:
  957.                     month = 30
  958.                 if int(month1) in [2]:
  959.                     if int(year) % 4 == 0:
  960.                         month = 29
  961.                     else:
  962.                         month = 28
  963.                 day_from = str(year) + '-' + str(month1) + '-' + '01'
  964.                 day_from = datetime.strptime(day_from,"%Y-%m-%d")
  965.                 print "day_from..............",day_from
  966.                 day_to = str(year) + '-' + str(month1) + '-' + str(month)
  967.                 day_to = datetime.strptime(day_to,"%Y-%m-%d")
  968.                 #print "day_to.....................",day_to
  969.                 next_date = day_from
  970.                
  971.                 contract_ids = cont_obj.search(cr,uid,[('employee_id','=',employee_id.id)])
  972.                 if contract_ids:
  973.                     contract_data = cont_obj.browse(cr, uid, contract_ids)[0]
  974.                 else:
  975.                     raise osv.except_osv(_('Warning !'),_("Contract is missing for employee %s." % (employee_id.name)))
  976.                 #print "contract_data..............",contract_data
  977.                
  978.                 for day in range(0, month):
  979.                     #print "next_date.............",next_date
  980.                    
  981.                     if contract_data:
  982.                         week_day +=1
  983.                         termination_date = self.pool.get('hr.employee').browse(cr,uid,employee_id.id).termination_date
  984.                         joining_date = self.pool.get('hr.employee').browse(cr,uid,employee_id.id).joining_date
  985.                         salary_date = next_date.strftime('%Y-%m-%d')
  986.                         flag = 0
  987.                         if contract_data.weekly_shedule_id:
  988.                             for each in contract_data.weekly_shedule_id.attendance_ids:
  989.                                 flag = 0
  990.                                 # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  991.                                 date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  992.                                 date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  993.                                 salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  994.                                
  995.                                 time_diff = date_to_week - date_from_week
  996.                                
  997.                                 diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  998.                                 diff_day = round(math.ceil(diff_day))
  999.        
  1000.                                 # print "Diffre========================",diff_day
  1001.                                 next_date_week = date_from_week
  1002.                                 for day in range(0, int(diff_day+1)):
  1003.                                     if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  1004.                                         print "Hello",
  1005.                                         flag=1
  1006.                                         break
  1007.                                     next_date_week +=  timedelta(days=1)
  1008.                                 if flag == 1:
  1009.                                     print "====Each=========",each,salary_date
  1010.                                     timing_main= self.timing_main_weekly_func( cr ,uid ,ids ,each ,salary_date)
  1011.                                     print "==========Custom======",timing_main
  1012.                         else:
  1013.                             timing_main= self.timing_main_func( cr ,uid ,ids ,contract_data.working_hours ,salary_date)
  1014.                             print "hjhjhjhjhjhjhj",timing_main
  1015.                                    
  1016.                        
  1017. #                        print "salary_date.............",salary_date
  1018. #                        att_list=att_obj.search(cr,uid,[('employee_id','=',employee_id.id),('day','=',salary_date)])
  1019. #                        print "att_list..............",att_list
  1020.                         category = employee_id.category
  1021.                        
  1022.                        
  1023.                        
  1024.                         # print "Gaurav====================Working Hours",timing_main
  1025.                         #print "timing_main...........",timing_main
  1026.                         total_punch = "select min(name + interval'5 hour 30 minutes') ,max(name + interval'5 hour 30 minutes') from hr_attendance where name + interval '5 hours 30 minute' >= to_timestamp('"+str(timing_main['start_time'])+"',\
  1027.                        'YYYY-MM-DD HH24:MI:SS')::timestamp - interval '1 hours 30 minute' and name + interval '5 hours 30 minute' < to_timestamp('"+str(timing_main['final_time'])+"','YYYY-MM-DD HH24:MI:SS')::timestamp and employee_id = \
  1028.                        '"+str(employee_id.id)+"' "
  1029.                         cr.execute(total_punch)
  1030.                         total_punch_result = cr.fetchall()
  1031.                         # print "Gaurav====================Working Hours",timing_main,total_punch_result
  1032.                         for data in total_punch_result:
  1033.                             print "=======================================data",data  
  1034.                             if len(data)>1 and data[0] != None and data[1] != None and data[0] != data[1] and joining_date <= salary_date:
  1035.                                 #if total_punch_result and len(total_punch_result[0]) == 2 and total_punch_result[0][0] != None and total_punch_result[0][1] != None and total_punch_result[0][0] != total_punch_result[0][1]:
  1036.                                 if termination_date:
  1037.                                     if termination_date>=salary_date:
  1038.                                         in_punch = data[0]
  1039.                                         #print "in_punch....................",in_punch
  1040.                                         out_punch1 = data[1]
  1041.                                         #print "out_punch1..................",out_punch1
  1042.                                         #category =employee_id.category
  1043.                                         in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  1044.                                         #print "in_time.........",in_time
  1045.                                         out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  1046.                                         #print "out_time.........",out_time
  1047.                                         dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  1048.                                        
  1049.                                         timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  1050.                                         #print "timing.................",timing
  1051.                                         lunch_mid = late = hard_absent = short_leave = hard_hd = False
  1052.                                         start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  1053.                                         time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1054.                                         working_main= False
  1055.                                         working_main_first_half = False
  1056.                                         working_main_second_half = False
  1057.                                         unpaid_flag=False
  1058.                                         third_late = False
  1059.                                         second_short = False
  1060.                                        
  1061.                                         lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  1062.                                         time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1063.                                         elunch = lunch + timedelta(hours=4,minutes=30)
  1064.         # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  1065.                                        
  1066.                                         if contract_data.weekly_shedule_id:
  1067.                                             for each in contract_data.weekly_shedule_id.attendance_ids:
  1068.                                                 flag = 0
  1069.                                                 # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  1070.                                                 date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  1071.                                                 date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  1072.                                                 salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  1073.                                                
  1074.                                                 time_diff = date_to_week - date_from_week
  1075.                                                
  1076.                                                 diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  1077.                                                 diff_day = round(math.ceil(diff_day))
  1078.                        
  1079.                                                 # print "Diffre========================",diff_day
  1080.                                                 next_date_week = date_from_week
  1081.                                                 for day in range(0, int(diff_day+1)):
  1082.                                                     if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  1083.         #                                                 print "Hello",
  1084.                                                         flag=1
  1085.                                                         break
  1086.                                                     next_date_week +=  timedelta(days=1)
  1087.                                                 if flag == 1:
  1088.         #                                             print "====Each=========",each,salary_date
  1089.                                                     shift = self.working_hours_on_day_weekly(cr, uid, each, salary_date, context)
  1090.                                         else:
  1091.                                             shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  1092.                                        
  1093.                                         # print "shift............",shift,salary_date
  1094.                                        
  1095.                                         start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  1096.                                         hour= int(float(shift['start']))
  1097.                                         min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  1098.                                         if min and len(min) == 1:
  1099.                                             min = str(min) +'0'
  1100.                                         if min and len(min) == 1:
  1101.                                             min = str(min)
  1102.                                         start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1103.                                         # print "start_time..............",start_time,type(start_time)
  1104.                                         end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  1105.                                         hour= int(float(shift['end']))
  1106.                                         min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  1107.                                         if min and len(min) == 1:
  1108.                                             min = str(min) +'0'
  1109.                                         if min and len(min) == 1:
  1110.                                             min = str(min)
  1111.                                         end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1112.                                         #print "end_time................",end_time
  1113.                                         if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  1114.                                             actual_start = start_time
  1115.                                         else:
  1116.                                             actual_start = in_punch
  1117.                                         #print "actual_start............",actual_start
  1118.                                         if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  1119.                                             actual_end = end_time
  1120.                                         else:
  1121.                                             actual_end = out_punch1
  1122.                                         #print "actual_end.............",actual_end
  1123.         #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  1124.                                         #if category == 'staff':
  1125.                                         if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=32)):
  1126.                                             hard_absent =True
  1127.                                             #print "setting absent1...hard_absent =True......."
  1128.                                             working_main='A'
  1129.                                         elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  1130.                                             #print "setting hd1..........."
  1131.                                             working_main='HD'
  1132.                                         elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  1133.                                            
  1134.                                             print "setting P1 and late..859.........12",in_punch
  1135.                                            # working_main ='P'
  1136.                                             late=True
  1137.                                             late_count = late_count+1
  1138.                                             print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  1139.                                             holiday_flag_1=False
  1140.                                             if dict['holiday_id']:
  1141.                                                     for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  1142.                                                         if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  1143.                                                             holiday_flag_1 = True
  1144.                                             if late_count>=3 and category=='staff' and holiday_flag_1==False:
  1145.                                                 working_main ='HD'
  1146.                                                
  1147.                                                
  1148.                                                 punch_in_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  1149.                                                 punch_in_hd= punch_in_hd.strftime("%Y-%m-%d 04:00:30")
  1150.                                                 punch_out_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  1151.                                                 punch_out_hd = punch_out_hd.strftime("%Y-%m-%d 08:10:30")
  1152.                                                
  1153.                                                 print "==================================",punch_in_hd,punch_out_hd
  1154.                                                
  1155.                                                
  1156.                                                
  1157.                                                
  1158.                                                 leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  1159.                                                 if leave_obj1:
  1160.                                                    
  1161.                                                     self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  1162.                                                     self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  1163.                                                     self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  1164.                                                     leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  1165.                                                
  1166.                                                 cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  1167.                                                 ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  1168.                                                 el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  1169.                                                
  1170.                    
  1171.                                                 if cl_status[5]['remaining_leaves']>=0.5:
  1172.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1173.                                                     print "Hiii",create_id
  1174.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1175.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1176.                                                 elif ml_status[2]['remaining_leaves']>=0.5:
  1177.                                                     print "Ml"
  1178.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1179.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1180.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1181.                                                 elif el_status[6]['remaining_leaves']>=0.5:
  1182.                                                     print "El"
  1183.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1184.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1185.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1186.                                                 else:
  1187.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1188.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1189.                                                     unpaid_flag = True
  1190.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  1191.                                             else:
  1192.                                                 working_main = 'P'
  1193.                                         else:
  1194.                                             #print "setting P1..........."
  1195.                                             working_main ='P'
  1196.                                         if not in_punch and not out_punch1:
  1197.                                             working_main = "A"
  1198.                                             #continue
  1199.                                         #print "working_main..........",working_main
  1200.                 # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  1201.                                         #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  1202.                                         #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  1203.                                         #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  1204.                                         hard_hd = short_leave = False
  1205.                                         if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  1206.                                             if late_count>=3 and category=='staff':
  1207.                                                 print "Late Count Reseted$$$$$$$$$$$$$$$$$$$$$$$",late_count                                    
  1208.                                                 working_main = 'HD'
  1209.                                                
  1210.                                             else:
  1211.                                                 #print "hard hd true....."
  1212.                                                 working_main = 'HD'
  1213.                                                 hard_hd = True
  1214.                                             #print "hard_hd......setting...",hard_hd
  1215.                                         elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  1216.                                           (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=5))):
  1217.                                             #print "short leave  true....."
  1218.                                             short_leave = True
  1219.                                             short_leave_late = short_leave_late + 1
  1220.                 # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  1221.         #                                missing = new_hrs_miss = 0.0
  1222.         #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  1223.         #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  1224.         #                                    missing = value.total_seconds()
  1225.         #                                    #print "missing................",missing
  1226.         #                                    miss_min = float(missing / 60)
  1227.         #                                    print "miss_min............",miss_min
  1228.         #                                    miss_hr = float(miss_min / 60)
  1229.         #                                    #print "miss_hr.................",miss_hr
  1230.         #                                    miss_hr = divmod(miss_hr,1)[0]
  1231.         #                                    #print "miss_hr.................",miss_hr
  1232.         #                                    if miss_hr:
  1233.         #                                        #print "miss_hr...............",miss_hr
  1234.         #                                        nw_min = miss_min - miss_hr * 60
  1235.         #                                        #print "nw_min...1............",nw_min
  1236.         #                                    else:
  1237.         #                                        nw_min = miss_min
  1238.         #                                        #print "nw_min......2.........",nw_min
  1239.         #                                    nw_min = round(nw_min,0)
  1240.         #                                    if nw_min > 5 and nw_min <= 35:
  1241.         #                                        nm_min = 0.5
  1242.         #                                    elif nw_min > 35:
  1243.         #                                        miss_hr = miss_hr + 1
  1244.         #                                        nm_min = 0.0
  1245.         #                                    else:
  1246.         #                                        nm_min = 0.0
  1247.         #                                    print "miss_hr............",miss_hr
  1248.         #                                    print "nm_min..............",nm_min
  1249.         #                                    new_hrs_miss = miss_hr + nm_min
  1250.         #                                    print "new_hrs_miss................",new_hrs_miss  
  1251.                 #                        if new_hrs_miss < 4.0:
  1252.                 #                            working_main ="A"
  1253.                 #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  1254.                 #                            working_main ="HD"
  1255.                 #                        elif new_hrs_miss >= 6.0 :
  1256.                 #                            working_main ="A"
  1257.                 #                        print "working_main............",working_main
  1258.                                             # ++++ START TIME & END TIME +++++++++++++++++++
  1259.                                         st= ed= False
  1260.                                         if in_punch:
  1261.                                             st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  1262.                                         if out_punch1:
  1263.                                             ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  1264.                 #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  1265.                                         #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  1266.                                         #print "ed.................",ed
  1267.                                         #if category == 'staff':
  1268.                                         if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  1269.                                          ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  1270.                                             lunch_mid =True
  1271.             #                            if category != 'staff':
  1272.             #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  1273.             #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  1274.             #                                    lunch_mid =True
  1275.                                         #print "lunch_mid...........",lunch_mid
  1276.                 # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  1277.                                         new_hrs=0.0
  1278.                                         ot_hr = 0
  1279.                                         ot_min = 0
  1280.                                         if st and ed:
  1281.                 # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  1282.                
  1283.                                            
  1284.                                             working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  1285.                                             punch_min = float(working_hour.total_seconds() / 60)
  1286.                                            
  1287.                                             if category == 'worker' and category=='contractor':
  1288.                                                 if lunch_mid:
  1289.                                                     punch_min = punch_min - 30.0
  1290.                                                
  1291.                                             punch_hr = float(punch_min / 60)
  1292.                                             punch_hr = divmod(punch_hr,1)[0]
  1293.                                             if punch_hr:
  1294.                                                 nw_min = punch_min - punch_hr * 60
  1295.                                             else:
  1296.                                                 nw_min = punch_min
  1297.                                            
  1298.                                             if nw_min > 24 and nw_min < 55:
  1299.                                                 nm_min = 0.5
  1300.                                             elif nw_min >= 55:
  1301.                                                 punch_hr = punch_hr + 1
  1302.                                                 nm_min = 0.0
  1303.                                             else:
  1304.                                                 nm_min = 0.0
  1305.                                             new_hrs = punch_hr + nm_min
  1306.                                             #print "new_hrs..........",new_hrs,type(new_hrs)
  1307.                 # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  1308.                                             over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  1309.                                             #print "over_time.............",over_time
  1310.                                             punch_min1 = float(over_time.total_seconds() / 60)
  1311.                                            
  1312.                                             punch_hr1 = float(punch_min1 / 60)
  1313.                                             punch_hr1 = divmod(punch_hr1,1)[0]
  1314.                                             if punch_hr1:
  1315.                                                 nw_min1 = punch_min1 - punch_hr1 * 60
  1316.                                             else:
  1317.                                                 nw_min1 = punch_min1
  1318.                                            
  1319.                                             if nw_min1 > 25 and nw_min1 < 55:
  1320.                                                 nm_min1 = 0.5
  1321.                                             elif nw_min1 >= 55:
  1322.                                                 punch_hr1 = punch_hr1 + 1
  1323.                                                 nm_min1 = 0.0
  1324.                                             else:
  1325.                                                 nm_min1 = 0.0
  1326.                                             new_hrs1 = punch_hr1 + nm_min1
  1327.                                             #print "actual ot new_hrs1.........",new_hrs1
  1328.                                             ot=round(new_hrs1/2,2)
  1329.                                             #print "shown  ot.............",ot
  1330.                                             extra_ot = 0.0
  1331.                                             if ot > 2.0:
  1332.                                                 extra_ot= float(float(ot -2.0))
  1333.                                                 #print "ot setting to 2.0...extra_ot...........",extra_ot
  1334.                                                 ot= 2.0
  1335.                                             # if ot > 2.0 and ot_sum_gaurav<16:
  1336.                                             #     extra_ot= float(float(ot -2.0))
  1337.                                             #     #print "ot setting to 2.0...extra_ot...........",extra_ot
  1338.                                             #     ot= 2.0
  1339.                                             # if ot_sum_gaurav>15.5:
  1340.                                             #     extra_ot= float(float(ot))
  1341.                                             #     ot = 0.0
  1342.                                                 #print "ot setting to 2.0...extra_ot...........",extra_ot
  1343.                                             print "33333333333333333333333333##################",ot,extra_ot,new_hrs1,ot_sum_gaurav
  1344.                                             ot_sum_gaurav = ot_sum_gaurav+ot
  1345.         # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  1346.                                             val1=self.float_time_convert_main(ot)
  1347.                                             hr=0
  1348.                                             #print "val1...............",val1
  1349.                                             time1 = 0.0
  1350.                                             if val1 and len(str(val1[1])) == 1:
  1351.                                                 hr = 0
  1352.                                                 ot_hr = val1[0]
  1353.                                                 ot_min = hr
  1354.                                                 time1 = str(val1[0]) +'.'+str(hr) + '0'
  1355.                                            
  1356.                                             if val1 and len(str(val1[1])) == 2:
  1357.                                                 if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  1358.                                                     hr = 0
  1359.                                                 else:
  1360.                                                     hr = 5
  1361.                                                 ot_hr = val1[0]
  1362.                                                 ot_min = hr
  1363.                                                 time1 = str(val1[0]) +'.'+str(hr) + '0'
  1364.                                             ot = time1
  1365.                                             #print "new ot...........",ot
  1366.                                             val2=self.float_time_convert_main(extra_ot)
  1367.                                             hr1=0
  1368.                                             #print "val2...............",val2
  1369.                                             time2 = 0.0
  1370.                                             if val2 and len(str(val2[1])) == 1:
  1371.                                                 hr1 = 0
  1372.                                                 time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1373.                                            
  1374.                                             if val2 and len(str(val2[1])) == 2:
  1375.                                                 if int(val2[1]) >=0 and int(val2[1]) <= 24:
  1376.                                                     hr1 = 0
  1377.                 #                                elif val2[1] == 50:
  1378.                 #                                    hr1 = 5
  1379.                                                 else:
  1380.                                                      hr1 = 5
  1381.                                                 time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1382.                                             extra_ot = time2
  1383.                                             #print "new extra_ot..............",extra_ot
  1384.                 #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  1385.                                         print "working_main............",working_main
  1386.                                        
  1387.                                         if category == 'worker' or category == 'contractor' :
  1388.                                             if working_main == 'HD' or working_main == 'P':
  1389.                                             #print "going in.........."
  1390.                                                 if new_hrs < 4.0:
  1391.                                                     working_main = 'A'
  1392.                                                     #print "working hours are less than 4.0.....",working_main
  1393.                                                 elif new_hrs >= 4.0 and new_hrs < 7.5:
  1394.                                                     working_main = 'HD'
  1395.                                                     #late = False
  1396.                                                     #print "working setting HD....",working_main
  1397.                                                 elif new_hrs >= 7.5 :
  1398.                                                     working_main = 'P'
  1399.                                                
  1400.                                                                              
  1401.                                                 #print "working setting P....",working_main
  1402.                                         if working_main == 'HD' and working_main == 'A':
  1403.                                            working_main = 'A'
  1404.                                        
  1405.                                         print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%===========fjjjjjjjh====",short_leave_late,short_leave,working_main
  1406.                                         if category == 'staff':
  1407.                                             if new_hrs <= 3.0:
  1408.                                                 working_main = 'A'
  1409.                                         # print "===================================",working_main
  1410.                                         # error
  1411.                                         if short_leave_late >= 2 and short_leave == True and category == 'staff':
  1412.                                             second_short = True
  1413.                                             working_main = 'HD'
  1414.                                             working_main_second_half = 'A'
  1415.                                             working_main_first_half ='P'
  1416.                                        
  1417.                                         if late_count >= 3 and late==True and category == 'staff' and (working_main=='HD' or working_main=='P'):
  1418.                                             third_late = True
  1419.                                             if unpaid_flag == True:
  1420.                                                 working_main = 'HD'
  1421.                                                 working_main_first_half = 'A'
  1422.                                                 working_main_second_half = 'P'
  1423.                                             else:
  1424.                                                 working_main = 'P'
  1425.                                                 working_main_first_half = 'L'
  1426.                                                 working_main_second_half = 'P'
  1427.                                                
  1428.                                            
  1429.                                        
  1430.                                              
  1431.        
  1432.                                         print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave_late
  1433.                                         if working_main == 'P' and hard_hd:
  1434.                                             working_main = 'HD'
  1435.             #                            if working_main == 'HD' and hard_absent:
  1436.             #                                working_main = 'A'
  1437.                                         #print "working main....",working_main
  1438.                                         if new_hrs1 < 0:
  1439.                                             new_hrs1 = 0
  1440.  
  1441.                                         in_punch_sun = False
  1442.                                         out_punch_sun = False
  1443.                                         if working_main == 'A':
  1444.                                             short_leave = False
  1445.                                             late = False
  1446.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  1447.                                             leave_qty = 0.0
  1448.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  1449.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  1450.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  1451.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  1452.                                             leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  1453.                                             leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  1454.                                             time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  1455.                                             time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  1456.                                             # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  1457.                                             # print "time", time_from
  1458.                                
  1459.                                             cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  1460.                                             leave_search = cr.fetchall()
  1461.                                            
  1462.                                             # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  1463.                                             # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  1464.                                             if leave_search:
  1465.                                                 leave_qty = leave_search[0][1]
  1466.                                            
  1467.                                             if leave_search and leave_qty == 0.5:
  1468.                                                 working_main_first_half = 'A'
  1469.                                                 working_main_second_half= 'L'
  1470.                                                 working_main = 'HD'
  1471.                                                 in_punch_sun = True
  1472.                                                 out_punch_sun = True
  1473.                                             elif leave_search and leave_qty > 0.5:
  1474.                                                 working_main_first_half = 'L'
  1475.                                                 working_main_second_half= 'L'
  1476.                                                 working_main = 'L'
  1477.                                                 in_punch_sun = True
  1478.                                                 out_punch_sun = True
  1479.                                             else:
  1480.                                                 working_main_first_half = 'A'
  1481.                                                 working_main_second_half= 'A'
  1482.                                                 working_main = 'A'
  1483.                                        
  1484.                                         if working_main == 'P' and third_late==False:
  1485.                                             working_main_first_half = 'P'
  1486.                                             working_main_second_half= 'P'
  1487.                                            
  1488.                                            
  1489.                                         if working_main == 'HD' and third_late==False:
  1490.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  1491.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  1492.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  1493.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  1494.                                             leave_date = leave_date + timedelta(hours=03,minutes=00)
  1495.                                             leave_date1 = leave_date + timedelta(hours=23,minutes=00)
  1496.                                             leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  1497.                                             print "time", leave_date
  1498.                                             leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','>=',str(leave_date)),('date_to','<=',str(leave_date1)),('holiday_status_id','!=',9),('employee_id','=',employee_id.id)])
  1499.                                             print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  1500.                                            
  1501.                                             if leave_list:
  1502.                                                 leave_qty = self.pool.get('hr.holidays').browse(cr,uid,leave_list[0]).number_of_days_temp
  1503.                                            
  1504.                                            
  1505.                                            
  1506.                                                
  1507.                                             in_punch_time_in = datetime.strptime(in_punch, DATETIME_FORMAT)
  1508.                                             out_punch_time_out = datetime.strptime(out_punch1, DATETIME_FORMAT)
  1509.                                             time_from = datetime.strptime(str(in_punch_time_in),'%Y-%m-%d %H:%M:%S').timetuple()
  1510.                                             time_to = datetime.strptime(str(out_punch_time_out),'%Y-%m-%d %H:%M:%S').timetuple()
  1511.                                             if (time_from.tm_hour >= 8 and time_from.tm_hour<=17) and (time_to.tm_hour<=17):
  1512.                                                 if leave_list and leave_qty == 0.5:
  1513.                                                     working_main = 'P'  
  1514.                                                     working_main_first_half = 'P'
  1515.                                                     working_main_second_half= 'L'
  1516.                                                 else:
  1517.                                                     working_main_first_half= 'P'
  1518.                                                     working_main_second_half= 'A'
  1519.                                             elif (time_from.tm_hour >= 10 and time_from.tm_hour<=18) and (time_to.tm_hour<=23):
  1520.                                                 if leave_list and leave_qty == 0.5:
  1521.                                                     working_main = 'P'
  1522.                                                     working_main_second_half = 'P'
  1523.                                                     working_main_first_half = 'L'
  1524.                                                 else:
  1525.                                                     working_main_first_half = 'A'
  1526.                                                     working_main_second_half = 'P'
  1527.                                            
  1528.                                        
  1529.                                            
  1530.                                        
  1531.                                         print "working main..working hour...ot.",working_main,new_hrs,new_hrs1,ot,ot_sum_gaurav
  1532.                                        
  1533.         #                                print "end_time.............",end_time,type(end_time)
  1534.         #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  1535.                                         if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  1536.                                             out_punch2 = out_punch1
  1537.                                         else:
  1538.                                             random_second = random.randrange(1, 59, 1)
  1539.                                             random_min = random.randrange(1, 10, 1)
  1540.                                             if ot_min >= 5 :
  1541.                                                 random_min +=  30
  1542.                                             else:
  1543.                                                 random_min +=  0
  1544.                                             #print "random_min..........",random_min
  1545.                                             out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  1546.                                             #print "out_punch2.............",str(out_punch2)
  1547.                                             #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  1548.         #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  1549.         #                                print "out_punch2..........",str(out_punch2)
  1550.                                         in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  1551.                                         out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  1552.                                         out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  1553.                                         #print "ot hr ,ot min......",ot_hr, ot_min
  1554.                                        
  1555.                                         if (category == 'worker' or category == 'contractor') and (working_main == 'A' or working_main=='L'):
  1556.                                             extra_ot = 0.0
  1557.                                             ot = 0.0
  1558.                                             new_hrs1 = 0.0
  1559.                                            
  1560.                                            
  1561.                                         if category == 'staff':
  1562.                                             extra_ot = 0.0
  1563.                                             ot = 0.0
  1564.                                         if category != 'staff':
  1565.                                             short_leave = False
  1566.                                             late = False
  1567.                 # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  1568.                                         holiday_flag = False
  1569.                                        
  1570.                                         try:
  1571.                                             cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  1572.                                             #print "deleted ...punch card .............",cr.fetchall()
  1573.                                             #print "going................."
  1574.                                             #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  1575.                                             #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  1576.                                            
  1577.                                             if dict['holiday_id']:
  1578.                                                 for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  1579.                                                     if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  1580.                                                         holiday_flag = True
  1581.                                            
  1582.                                             if holiday_flag == True and category == 'staff':
  1583.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  1584.                                                 start_date = salary_date
  1585.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  1586.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  1587.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  1588.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  1589.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  1590.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  1591.                                                
  1592.                                                 full_day = cr.fetchall()
  1593.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  1594.                                                
  1595.                                                 half_day = cr.fetchall()
  1596.                                                
  1597.                                                 if week_off_first == True:
  1598.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  1599.                                                     leave_first = cr.fetchall()
  1600.                                                     week_off_first =False
  1601.                                                
  1602.                                                 if leave_first:
  1603.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  1604.                                                     leave_first =False
  1605.                                                 else:    
  1606.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  1607.                                                
  1608.  
  1609.                                                 print "========================AAAAAAAAAA==============",int(full_day[0][0]),int((half_day[0][0])/2),total_day_week,full_day[0][0],half_day[0][0]
  1610.                                                
  1611.                                                 if total_day_week >=3:
  1612.                                                     working_main ='WO'
  1613.                                                     working_main_first_half = 'WO'
  1614.                                                     working_main_second_half = 'WO'
  1615.                                                     short_leave = False
  1616.                                                     late = False
  1617.                                                     late_count = late_count-1
  1618.                                                     in_punch_sun = True
  1619.                                                     out_punch_sun = True
  1620.                                                 else:
  1621.                                                     working_main ='S'
  1622.                                                     working_main_first_half ='S'
  1623.                                                     working_main_second_half = 'S'
  1624.                                                     short_leave = False
  1625.                                                     late = False
  1626.                                                     late_count = late_count-1
  1627.                                                     in_punch_sun = True
  1628.                                                     out_punch_sun = True
  1629.                                        
  1630.                                            
  1631.                                             if holiday_flag == True and (category =='worker' or category=='contractor'):
  1632.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  1633.                                                 start_date = salary_date
  1634.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  1635.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  1636.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  1637.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  1638.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  1639.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  1640.                                                
  1641.                                                 full_day = cr.fetchall()
  1642.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  1643.                                                
  1644.                                                 half_day = cr.fetchall()
  1645.                                                
  1646.                                                
  1647.                                                
  1648.                                                
  1649.                                                
  1650.                                                 if week_off_first == True:
  1651.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  1652.                                                     leave_first = cr.fetchall()
  1653.                                                     week_off_first =False
  1654.                                                
  1655.                                                 if leave_first:
  1656.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  1657.                                                     leave_first =False
  1658.                                                 else:    
  1659.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  1660.                                                
  1661.  
  1662.     #                                             print "======================================",start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  1663.                                                 if total_day_week >=3:
  1664.                                                     working_main ='WO'
  1665.                                                     working_main_first_half ='WO'
  1666.                                                     working_main_second_half = 'WO'
  1667.            
  1668.                                                     punch_min1 = float(working_hour.total_seconds() / 60)
  1669.                                                
  1670.                                                     # punch_hr1 = float(punch_min1 / 60)
  1671.                                                     # punch_hr1 = divmod(punch_hr1,1)[0]
  1672.                                                     punch_hr1 = float(punch_min1 / 60)
  1673.                                                     punch_hr1 = divmod(punch_hr1,1)[0]
  1674.                                                     if punch_hr1:
  1675.                                                         nw_min1 = punch_min1 - punch_hr1 * 60
  1676.                                                     else:
  1677.                                                         nw_min1 = punch_min1
  1678.                                                    
  1679.                                                     if nw_min1 > 25 and nw_min1 < 55:
  1680.                                                         nm_min1 = 0.5
  1681.                                                     elif nw_min1 >= 55:
  1682.                                                         punch_hr1 = punch_hr1 + 1
  1683.                                                         nm_min1 = 0.0
  1684.                                                     else:
  1685.                                                         nm_min1 = 0.0
  1686.                                                     new_hrs1 = punch_hr1 + nm_min1
  1687.                                                     extra_ot = new_hrs1
  1688.                                                     new_hrs = 0
  1689.                                                     ot = 0
  1690.                                                    
  1691.                                                     extra_ot = round(extra_ot/2,2)
  1692.                                                     val2=self.float_time_convert_main(extra_ot)
  1693.                                                     hr1=0
  1694.                                                     #print "val2...............",val2
  1695.                                                     time2 = 0.0
  1696.                                                     if val2 and len(str(val2[1])) == 1:
  1697.                                                         hr1 = 0
  1698.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1699.                                                    
  1700.                                                     if val2 and len(str(val2[1])) == 2:
  1701.                                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  1702.                                                             hr1 = 0
  1703.                         #                                elif val2[1] == 50:
  1704.                         #                                    hr1 = 5
  1705.                                                         else:
  1706.                                                              hr1 = 5
  1707.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1708.                                                     extra_ot = time2
  1709.                                                     in_punch_sun = True
  1710.                                                     out_punch_sun = True
  1711.                                                 else:
  1712.                                                     working_main ='S'
  1713.                                                     working_main_first_half ='S'
  1714.                                                     working_main_second_half = 'S'
  1715.                                                     punch_min1 = float(working_hour.total_seconds() / 60)
  1716.                                                
  1717.                                                     # punch_hr1 = float(punch_min1 / 60)
  1718.                                                     # punch_hr1 = divmod(punch_hr1,1)[0]
  1719.                                                     punch_hr1 = float(punch_min1 / 60)
  1720.                                                     punch_hr1 = divmod(punch_hr1,1)[0]
  1721.                                                     if punch_hr1:
  1722.                                                         nw_min1 = punch_min1 - punch_hr1 * 60
  1723.                                                     else:
  1724.                                                         nw_min1 = punch_min1
  1725.                                                    
  1726.                                                     if nw_min1 > 25 and nw_min1 < 55:
  1727.                                                         nm_min1 = 0.5
  1728.                                                     elif nw_min1 >= 55:
  1729.                                                         punch_hr1 = punch_hr1 + 1
  1730.                                                         nm_min1 = 0.0
  1731.                                                     else:
  1732.                                                         nm_min1 = 0.0
  1733.                                                     new_hrs1 = punch_hr1 + nm_min1
  1734.                                                     extra_ot = new_hrs1
  1735.                                                     new_hrs = 0
  1736.                                                     ot = 0
  1737.                                                    
  1738.                                                     extra_ot = round(extra_ot/2,2)
  1739.                                                     val2=self.float_time_convert_main(extra_ot)
  1740.                                                     hr1=0
  1741.                                                     #print "val2...............",val2
  1742.                                                     time2 = 0.0
  1743.                                                     if val2 and len(str(val2[1])) == 1:
  1744.                                                         hr1 = 0
  1745.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1746.                                                    
  1747.                                                     if val2 and len(str(val2[1])) == 2:
  1748.                                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  1749.                                                             hr1 = 0
  1750.                         #                                elif val2[1] == 50:
  1751.                         #                                    hr1 = 5
  1752.                                                         else:
  1753.                                                              hr1 = 5
  1754.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  1755.                                                     extra_ot = time2
  1756.                                                     in_punch_sun = True
  1757.                                                     out_punch_sun = True
  1758.                                                     print "Punch hr",punch_hr1,new_hrs1
  1759.                                             if in_punch_sun == True and out_punch_sun== True:
  1760.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  1761.                                                     'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch)})
  1762.                                             else:
  1763.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  1764.                                                     'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  1765.                                                     'out_punch2':str(out_punch2),'in_punch_custom':str(in_punch),'late':late,})
  1766.                                                                  
  1767.                                             print "=========================NEW  PUNCH CREATED===========================",created_id
  1768.                                         except Exception,e:
  1769.                                             print "Exception Punch Card Creation...........", e.args
  1770.                                             pass
  1771.  
  1772.                                 else:
  1773.                                     in_punch = data[0]
  1774.                                     #print "in_punch....................",in_punch
  1775.                                     out_punch1 = data[1]
  1776.                                     #print "out_punch1..................",out_punch1
  1777.                                     #category =employee_id.category
  1778.                                     in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  1779.                                     #print "in_time.........",in_time
  1780.                                     out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  1781.                                     #print "out_time.........",out_time
  1782.                                     dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  1783.                                    
  1784.                                     timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  1785.                                     #print "timing.................",timing
  1786.                                     lunch_mid = late = hard_absent = short_leave = hard_hd = False
  1787.                                     start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  1788.                                     time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1789.                                     working_main= False
  1790.                                     working_main_first_half = False
  1791.                                     working_main_second_half = False
  1792.                                     unpaid_flag=False
  1793.                                     third_late = False
  1794.                                     second_short = False
  1795.                                    
  1796.                                     lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  1797.                                     time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1798.                                     elunch = lunch + timedelta(hours=4,minutes=30)
  1799.     # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  1800.                                    
  1801.                                     if contract_data.weekly_shedule_id:
  1802.                                         for each in contract_data.weekly_shedule_id.attendance_ids:
  1803.                                             flag = 0
  1804.                                             # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  1805.                                             date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  1806.                                             date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  1807.                                             salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  1808.                                            
  1809.                                             time_diff = date_to_week - date_from_week
  1810.                                            
  1811.                                             diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  1812.                                             diff_day = round(math.ceil(diff_day))
  1813.                    
  1814.                                             # print "Diffre========================",diff_day
  1815.                                             next_date_week = date_from_week
  1816.                                             for day in range(0, int(diff_day+1)):
  1817.                                                 if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  1818.     #                                                 print "Hello",
  1819.                                                     flag=1
  1820.                                                     break
  1821.                                                 next_date_week +=  timedelta(days=1)
  1822.                                             if flag == 1:
  1823.     #                                             print "====Each=========",each,salary_date
  1824.                                                 shift = self.working_hours_on_day_weekly(cr, uid, each, salary_date, context)
  1825.                                     else:
  1826.                                         shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  1827.                                    
  1828.                                     # print "shift............",shift,salary_date
  1829.                                    
  1830.                                     start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  1831.                                     hour= int(float(shift['start']))
  1832.                                     min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  1833.                                     if min and len(min) == 1:
  1834.                                         min = str(min) +'0'
  1835.                                     if min and len(min) == 1:
  1836.                                         min = str(min)
  1837.                                     start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1838.                                     # print "start_time..............",start_time,type(start_time)
  1839.                                     end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  1840.                                     hour= int(float(shift['end']))
  1841.                                     min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  1842.                                     if min and len(min) == 1:
  1843.                                         min = str(min) +'0'
  1844.                                     if min and len(min) == 1:
  1845.                                         min = str(min)
  1846.                                     end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  1847.                                     #print "end_time................",end_time
  1848.                                     if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  1849.                                         actual_start = start_time
  1850.                                     else:
  1851.                                         actual_start = in_punch
  1852.                                     #print "actual_start............",actual_start
  1853.                                     if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  1854.                                         actual_end = end_time
  1855.                                     else:
  1856.                                         actual_end = out_punch1
  1857.                                     #print "actual_end.............",actual_end
  1858.     #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  1859.                                     #if category == 'staff':
  1860.                                     if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=32)):
  1861.                                         hard_absent =True
  1862.                                         #print "setting absent1...hard_absent =True......."
  1863.                                         working_main='A'
  1864.                                     elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  1865.                                         #print "setting hd1..........."
  1866.                                         working_main='HD'
  1867.                                     elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  1868.                                        
  1869.                                         print "setting P1 and late..859.........12",in_punch
  1870.                                        # working_main ='P'
  1871.                                         late=True
  1872.                                         late_count = late_count+1
  1873.                                         print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  1874.                                         holiday_flag_1=False
  1875.                                         if dict['holiday_id']:
  1876.                                                 for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  1877.                                                     if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  1878.                                                         holiday_flag_1 = True
  1879.                                         if late_count>=3 and category=='staff' and holiday_flag_1==False:
  1880.                                             working_main ='HD'
  1881.                                            
  1882.                                            
  1883.                                             punch_in_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  1884.                                             punch_in_hd= punch_in_hd.strftime("%Y-%m-%d 04:00:30")
  1885.                                             punch_out_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  1886.                                             punch_out_hd = punch_out_hd.strftime("%Y-%m-%d 08:10:30")
  1887.                                            
  1888.                                             print "==================================",punch_in_hd,punch_out_hd
  1889.                                            
  1890.                                            
  1891.                                            
  1892.                                            
  1893.                                             leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  1894.                                             if leave_obj1:
  1895.                                                
  1896.                                                 self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  1897.                                                 self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  1898.                                                 self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  1899.                                                 leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  1900.                                            
  1901.                                             cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  1902.                                             ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  1903.                                             el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  1904.                                            
  1905.                
  1906.                                             if cl_status[5]['remaining_leaves']>=0.5:
  1907.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1908.                                                 print "Hiii",create_id
  1909.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1910.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1911.                                             elif ml_status[2]['remaining_leaves']>=0.5:
  1912.                                                 print "Ml"
  1913.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1914.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1915.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1916.                                             elif el_status[6]['remaining_leaves']>=0.5:
  1917.                                                 print "El"
  1918.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1919.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1920.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  1921.                                             else:
  1922.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  1923.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  1924.                                                 unpaid_flag = True
  1925.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  1926.                                         else:
  1927.                                             working_main = 'P'
  1928.                                     else:
  1929.                                         #print "setting P1..........."
  1930.                                         working_main ='P'
  1931.                                     if not in_punch and not out_punch1:
  1932.                                         working_main = "A"
  1933.                                         #continue
  1934.                                     #print "working_main..........",working_main
  1935.             # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  1936.                                     #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  1937.                                     #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  1938.                                     #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  1939.                                     hard_hd = short_leave = False
  1940.                                     if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  1941.                                         if late_count>=3 and category=='staff':
  1942.                                             print "Late Count Reseted$$$$$$$$$$$$$$$$$$$$$$$",late_count                                    
  1943.                                             working_main = 'HD'
  1944.                                            
  1945.                                         else:
  1946.                                             #print "hard hd true....."
  1947.                                             working_main = 'HD'
  1948.                                             hard_hd = True
  1949.                                         #print "hard_hd......setting...",hard_hd
  1950.                                     elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  1951.                                       (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=5))):
  1952.                                         #print "short leave  true....."
  1953.                                         short_leave = True
  1954.                                         short_leave_late = short_leave_late + 1
  1955.             # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  1956.     #                                missing = new_hrs_miss = 0.0
  1957.     #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  1958.     #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  1959.     #                                    missing = value.total_seconds()
  1960.     #                                    #print "missing................",missing
  1961.     #                                    miss_min = float(missing / 60)
  1962.     #                                    print "miss_min............",miss_min
  1963.     #                                    miss_hr = float(miss_min / 60)
  1964.     #                                    #print "miss_hr.................",miss_hr
  1965.     #                                    miss_hr = divmod(miss_hr,1)[0]
  1966.     #                                    #print "miss_hr.................",miss_hr
  1967.     #                                    if miss_hr:
  1968.     #                                        #print "miss_hr...............",miss_hr
  1969.     #                                        nw_min = miss_min - miss_hr * 60
  1970.     #                                        #print "nw_min...1............",nw_min
  1971.     #                                    else:
  1972.     #                                        nw_min = miss_min
  1973.     #                                        #print "nw_min......2.........",nw_min
  1974.     #                                    nw_min = round(nw_min,0)
  1975.     #                                    if nw_min > 5 and nw_min <= 35:
  1976.     #                                        nm_min = 0.5
  1977.     #                                    elif nw_min > 35:
  1978.     #                                        miss_hr = miss_hr + 1
  1979.     #                                        nm_min = 0.0
  1980.     #                                    else:
  1981.     #                                        nm_min = 0.0
  1982.     #                                    print "miss_hr............",miss_hr
  1983.     #                                    print "nm_min..............",nm_min
  1984.     #                                    new_hrs_miss = miss_hr + nm_min
  1985.     #                                    print "new_hrs_miss................",new_hrs_miss  
  1986.             #                        if new_hrs_miss < 4.0:
  1987.             #                            working_main ="A"
  1988.             #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  1989.             #                            working_main ="HD"
  1990.             #                        elif new_hrs_miss >= 6.0 :
  1991.             #                            working_main ="A"
  1992.             #                        print "working_main............",working_main
  1993.                                         # ++++ START TIME & END TIME +++++++++++++++++++
  1994.                                     st= ed= False
  1995.                                     if in_punch:
  1996.                                         st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  1997.                                     if out_punch1:
  1998.                                         ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  1999.             #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  2000.                                     #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  2001.                                     #print "ed.................",ed
  2002.                                     #if category == 'staff':
  2003.                                     if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  2004.                                      ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  2005.                                         lunch_mid =True
  2006.         #                            if category != 'staff':
  2007.         #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  2008.         #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  2009.         #                                    lunch_mid =True
  2010.                                     #print "lunch_mid...........",lunch_mid
  2011.             # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  2012.                                     new_hrs=0.0
  2013.                                     ot_hr = 0
  2014.                                     ot_min = 0
  2015.                                     if st and ed:
  2016.             # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  2017.            
  2018.                                        
  2019.                                         working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  2020.                                         punch_min = float(working_hour.total_seconds() / 60)
  2021.                                        
  2022.                                         if category == 'worker' and category=='contractor':
  2023.                                             if lunch_mid:
  2024.                                                 punch_min = punch_min - 30.0
  2025.                                            
  2026.                                         punch_hr = float(punch_min / 60)
  2027.                                         punch_hr = divmod(punch_hr,1)[0]
  2028.                                         if punch_hr:
  2029.                                             nw_min = punch_min - punch_hr * 60
  2030.                                         else:
  2031.                                             nw_min = punch_min
  2032.                                        
  2033.                                         if nw_min > 24 and nw_min < 55:
  2034.                                             nm_min = 0.5
  2035.                                         elif nw_min >= 55:
  2036.                                             punch_hr = punch_hr + 1
  2037.                                             nm_min = 0.0
  2038.                                         else:
  2039.                                             nm_min = 0.0
  2040.                                         new_hrs = punch_hr + nm_min
  2041.                                         #print "new_hrs..........",new_hrs,type(new_hrs)
  2042.             # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  2043.                                         over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  2044.                                         #print "over_time.............",over_time
  2045.                                         punch_min1 = float(over_time.total_seconds() / 60)
  2046.                                        
  2047.                                         punch_hr1 = float(punch_min1 / 60)
  2048.                                         punch_hr1 = divmod(punch_hr1,1)[0]
  2049.                                         if punch_hr1:
  2050.                                             nw_min1 = punch_min1 - punch_hr1 * 60
  2051.                                         else:
  2052.                                             nw_min1 = punch_min1
  2053.                                        
  2054.                                         if nw_min1 > 25 and nw_min1 < 55:
  2055.                                             nm_min1 = 0.5
  2056.                                         elif nw_min1 >= 55:
  2057.                                             punch_hr1 = punch_hr1 + 1
  2058.                                             nm_min1 = 0.0
  2059.                                         else:
  2060.                                             nm_min1 = 0.0
  2061.                                         new_hrs1 = punch_hr1 + nm_min1
  2062.                                         #print "actual ot new_hrs1.........",new_hrs1
  2063.                                         ot=round(new_hrs1/2,2)
  2064.                                         #print "shown  ot.............",ot
  2065.                                         extra_ot = 0.0
  2066.                                         if ot > 2.0:
  2067.                                             extra_ot= float(float(ot -2.0))
  2068.                                             #print "ot setting to 2.0...extra_ot...........",extra_ot
  2069.                                             ot= 2.0
  2070.                                         # if ot > 2.0 and ot_sum_gaurav<16:
  2071.                                         #     extra_ot= float(float(ot -2.0))
  2072.                                         #     #print "ot setting to 2.0...extra_ot...........",extra_ot
  2073.                                         #     ot= 2.0
  2074.                                         # if ot_sum_gaurav>15.5:
  2075.                                         #     extra_ot= float(float(ot))
  2076.                                         #     ot = 0.0
  2077.                                             #print "ot setting to 2.0...extra_ot...........",extra_ot
  2078.                                         print "33333333333333333333333333##################",ot,extra_ot,new_hrs1,ot_sum_gaurav
  2079.                                         ot_sum_gaurav = ot_sum_gaurav+ot
  2080.     # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  2081.                                         val1=self.float_time_convert_main(ot)
  2082.                                         hr=0
  2083.                                         #print "val1...............",val1
  2084.                                         time1 = 0.0
  2085.                                         if val1 and len(str(val1[1])) == 1:
  2086.                                             hr = 0
  2087.                                             ot_hr = val1[0]
  2088.                                             ot_min = hr
  2089.                                             time1 = str(val1[0]) +'.'+str(hr) + '0'
  2090.                                        
  2091.                                         if val1 and len(str(val1[1])) == 2:
  2092.                                             if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  2093.                                                 hr = 0
  2094.                                             else:
  2095.                                                 hr = 5
  2096.                                             ot_hr = val1[0]
  2097.                                             ot_min = hr
  2098.                                             time1 = str(val1[0]) +'.'+str(hr) + '0'
  2099.                                         ot = time1
  2100.                                         #print "new ot...........",ot
  2101.                                         val2=self.float_time_convert_main(extra_ot)
  2102.                                         hr1=0
  2103.                                         #print "val2...............",val2
  2104.                                         time2 = 0.0
  2105.                                         if val2 and len(str(val2[1])) == 1:
  2106.                                             hr1 = 0
  2107.                                             time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2108.                                        
  2109.                                         if val2 and len(str(val2[1])) == 2:
  2110.                                             if int(val2[1]) >=0 and int(val2[1]) <= 24:
  2111.                                                 hr1 = 0
  2112.             #                                elif val2[1] == 50:
  2113.             #                                    hr1 = 5
  2114.                                             else:
  2115.                                                  hr1 = 5
  2116.                                             time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2117.                                         extra_ot = time2
  2118.                                         #print "new extra_ot..............",extra_ot
  2119.             #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  2120.                                     print "working_main............",working_main
  2121.                                    
  2122.                                     if category == 'worker' or category == 'contractor' :
  2123.                                         if working_main == 'HD' or working_main == 'P':
  2124.                                         #print "going in.........."
  2125.                                             if new_hrs < 4.0:
  2126.                                                 working_main = 'A'
  2127.                                                 #print "working hours are less than 4.0.....",working_main
  2128.                                             elif new_hrs >= 4.0 and new_hrs < 7.5:
  2129.                                                 working_main = 'HD'
  2130.                                                 #late = False
  2131.                                                 #print "working setting HD....",working_main
  2132.                                             elif new_hrs >= 7.5 :
  2133.                                                 working_main = 'P'
  2134.                                            
  2135.                                                                          
  2136.                                             #print "working setting P....",working_main
  2137.                                     if working_main == 'HD' and working_main == 'A':
  2138.                                        working_main = 'A'
  2139.                                    
  2140.                                     print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%===========fjjjjjjjh====",short_leave_late,short_leave,working_main
  2141.                                     if category == 'staff':
  2142.                                         if new_hrs <= 3.0:
  2143.                                             working_main = 'A'
  2144.                                     # print "===================================",working_main
  2145.                                     # error
  2146.                                     if short_leave_late >= 2 and short_leave == True and category == 'staff':
  2147.                                         second_short = True
  2148.                                         working_main = 'HD'
  2149.                                         working_main_second_half = 'A'
  2150.                                         working_main_first_half ='P'
  2151.                                    
  2152.                                     if late_count >= 3 and late==True and category == 'staff' and (working_main=='HD' or working_main=='P'):
  2153.                                         third_late = True
  2154.                                         if unpaid_flag == True:
  2155.                                             working_main = 'HD'
  2156.                                             working_main_first_half = 'A'
  2157.                                             working_main_second_half = 'P'
  2158.                                         else:
  2159.                                             working_main = 'P'
  2160.                                             working_main_first_half = 'L'
  2161.                                             working_main_second_half = 'P'
  2162.                                            
  2163.                                        
  2164.                                    
  2165.                                          
  2166.    
  2167.                                     print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave_late
  2168.                                     if working_main == 'P' and hard_hd:
  2169.                                         working_main = 'HD'
  2170.         #                            if working_main == 'HD' and hard_absent:
  2171.         #                                working_main = 'A'
  2172.                                     #print "working main....",working_main
  2173.                                     if new_hrs1 < 0:
  2174.                                         new_hrs1 = 0
  2175.  
  2176.                                     in_punch_sun = False
  2177.                                     out_punch_sun = False
  2178.                                     if working_main == 'A':
  2179.                                         short_leave = False
  2180.                                         late = False
  2181.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2182.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  2183.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  2184.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  2185.                                         leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  2186.                                         leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  2187.                                         time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  2188.                                         time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  2189.                                         # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  2190.                                         # print "time", time_from
  2191.                            
  2192.                                         cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  2193.                                         leave_search = cr.fetchall()
  2194.                                        
  2195.                                         # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  2196.                                         # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  2197.                                         if leave_search:
  2198.                                             leave_qty = leave_search[0][1]
  2199.                                        
  2200.                                         if leave_search and leave_qty == 0.5:
  2201.                                             working_main_first_half = 'A'
  2202.                                             working_main_second_half= 'L'
  2203.                                             working_main = 'HD'
  2204.                                             in_punch_sun = True
  2205.                                             out_punch_sun = True
  2206.                                         elif leave_search and leave_qty > 0.5:
  2207.                                             working_main_first_half = 'L'
  2208.                                             working_main_second_half= 'L'
  2209.                                             working_main = 'L'
  2210.                                             in_punch_sun = True
  2211.                                             out_punch_sun = True
  2212.                                         else:
  2213.                                             working_main_first_half = 'A'
  2214.                                             working_main_second_half= 'A'
  2215.                                             working_main = 'A'
  2216.                                    
  2217.                                     if working_main == 'P' and third_late==False:
  2218.                                         working_main_first_half = 'P'
  2219.                                         working_main_second_half= 'P'
  2220.                                        
  2221.                                        
  2222.                                     if working_main == 'HD' and third_late==False :
  2223.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2224.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  2225.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  2226.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  2227.                                         leave_date = leave_date + timedelta(hours=03,minutes=00)
  2228.                                         leave_date1 = leave_date + timedelta(hours=23,minutes=00)
  2229.                                         leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  2230.                                         print "time", leave_date
  2231.                                         leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','>=',str(leave_date)),('date_to','<=',str(leave_date1)),('holiday_status_id','!=',9),('employee_id','=',employee_id.id)])
  2232.                                         print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  2233.                                        
  2234.                                         if leave_list:
  2235.                                             leave_qty = self.pool.get('hr.holidays').browse(cr,uid,leave_list[0]).number_of_days_temp
  2236.                                        
  2237.                                        
  2238.                                        
  2239.                                            
  2240.                                         in_punch_time_in = datetime.strptime(in_punch, DATETIME_FORMAT)
  2241.                                         out_punch_time_out = datetime.strptime(out_punch1, DATETIME_FORMAT)
  2242.                                         time_from = datetime.strptime(str(in_punch_time_in),'%Y-%m-%d %H:%M:%S').timetuple()
  2243.                                         time_to = datetime.strptime(str(out_punch_time_out),'%Y-%m-%d %H:%M:%S').timetuple()
  2244.                                         if (time_from.tm_hour >= 8 and time_from.tm_hour<=17) and (time_to.tm_hour<=17):
  2245.                                             if leave_list and leave_qty == 0.5:
  2246.                                                 working_main = 'P'  
  2247.                                                 working_main_first_half = 'P'
  2248.                                                 working_main_second_half= 'L'
  2249.                                             else:
  2250.                                                 working_main_first_half= 'P'
  2251.                                                 working_main_second_half= 'A'
  2252.                                         elif (time_from.tm_hour >= 10 and time_from.tm_hour<=18) and (time_to.tm_hour<=23):
  2253.                                             if leave_list and leave_qty == 0.5:
  2254.                                                 working_main = 'P'
  2255.                                                 working_main_second_half = 'P'
  2256.                                                 working_main_first_half = 'L'
  2257.                                             else:
  2258.                                                 working_main_first_half = 'A'
  2259.                                                 working_main_second_half = 'P'
  2260.                                        
  2261.                                    
  2262.                                        
  2263.                                    
  2264.                                     print "working main..working hour...ot.",working_main,new_hrs,new_hrs1,ot,ot_sum_gaurav
  2265.                                    
  2266.     #                                print "end_time.............",end_time,type(end_time)
  2267.     #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  2268.                                     if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  2269.                                         out_punch2 = out_punch1
  2270.                                     else:
  2271.                                         random_second = random.randrange(1, 59, 1)
  2272.                                         random_min = random.randrange(1, 10, 1)
  2273.                                         if ot_min >= 5 :
  2274.                                             random_min +=  30
  2275.                                         else:
  2276.                                             random_min +=  0
  2277.                                         #print "random_min..........",random_min
  2278.                                         out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  2279.                                         #print "out_punch2.............",str(out_punch2)
  2280.                                         #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  2281.     #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  2282.     #                                print "out_punch2..........",str(out_punch2)
  2283.                                     in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  2284.                                     out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  2285.                                     out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  2286.                                     #print "ot hr ,ot min......",ot_hr, ot_min
  2287.                                    
  2288.                                     if (category == 'worker' or category == 'contractor') and (working_main == 'A' or working_main=='L'):
  2289.                                         extra_ot = 0.0
  2290.                                         ot = 0.0
  2291.                                         new_hrs1 = 0.0
  2292.                                        
  2293.                                        
  2294.                                     if category == 'staff':
  2295.                                         extra_ot = 0.0
  2296.                                         ot = 0.0
  2297.                                     if category != 'staff':
  2298.                                         short_leave = False
  2299.                                         late = False
  2300.             # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  2301.                                     holiday_flag = False
  2302.                                    
  2303.                                     try:
  2304.                                         cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  2305.                                         #print "deleted ...punch card .............",cr.fetchall()
  2306.                                         #print "going................."
  2307.                                         #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  2308.                                         #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  2309.                                        
  2310.                                         if dict['holiday_id']:
  2311.                                             for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  2312.                                                 if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  2313.                                                     holiday_flag = True
  2314.                                         if holiday_flag == True and category == 'staff':
  2315.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2316.                                             start_date = salary_date
  2317.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  2318.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  2319.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  2320.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  2321.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  2322.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2323.                                            
  2324.                                             full_day = cr.fetchall()
  2325.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2326.                                            
  2327.                                             half_day = cr.fetchall()
  2328.                                            
  2329.                                    
  2330.                                            
  2331.                                            
  2332.                                            
  2333.                                             if week_off_first == True:
  2334.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  2335.                                                 leave_first = cr.fetchall()
  2336.                                                 week_off_first = False
  2337.                                            
  2338.                                             if leave_first:
  2339.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  2340.                                                 leave_first =False
  2341.                                             else:    
  2342.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  2343.                                            
  2344.                                             print "========================AAAAAAAAAA==============",int(full_day[0][0]),int((half_day[0][0])/2),total_day_week,full_day[0][0],half_day[0][0]
  2345.                                            
  2346.                                             if total_day_week >=3:
  2347.                                                 working_main ='WO'
  2348.                                                 working_main_first_half = 'WO'
  2349.                                                 working_main_second_half = 'WO'
  2350.                                                 short_leave = False
  2351.                                                 late = False
  2352.                                                 late_count = late_count-1
  2353.                                                 in_punch_sun = True
  2354.                                                 out_punch_sun = True
  2355.                                             else:
  2356.                                                 working_main ='S'
  2357.                                                 working_main_first_half ='S'
  2358.                                                 working_main_second_half = 'S'
  2359.                                                 short_leave = False
  2360.                                                 late = False
  2361.                                                 late_count = late_count-1
  2362.                                                 in_punch_sun = True
  2363.                                                 out_punch_sun = True
  2364.                                    
  2365.                                        
  2366.                                         if holiday_flag == True and (category =='worker' or category=='contractor'):
  2367.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2368.                                             start_date = salary_date
  2369.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  2370.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  2371.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  2372.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  2373.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  2374.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2375.                                            
  2376.                                             full_day = cr.fetchall()
  2377.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2378.                                            
  2379.                                             half_day = cr.fetchall()
  2380.                                            
  2381.                                            
  2382.                                            
  2383.                                            
  2384.                                            
  2385.                                             if week_off_first == True:
  2386.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  2387.                                                 leave_first = cr.fetchall()
  2388.                                                 week_off_first = False
  2389.                                            
  2390.                                             if leave_first:
  2391.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  2392.                                                 leave_first =False
  2393.                                             else:    
  2394.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  2395.                                            
  2396.  
  2397. #                                             print "======================================",start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  2398.                                             if total_day_week >=3:
  2399.                                                 working_main ='WO'
  2400.                                                 working_main_first_half ='WO'
  2401.                                                 working_main_second_half = 'WO'
  2402.        
  2403.                                                 over_time=datetime.strptime(str( datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") + timedelta(hours=5,minutes=30)),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_start),"%Y-%m-%d %H:%M:%S")
  2404.                                                 print "over_time.............",over_time,out_punch1,actual_start
  2405.                                                 punch_min1 = float(over_time.total_seconds() / 60)
  2406.                                                
  2407.                                                 punch_hr1 = float(punch_min1 / 60)
  2408.                                                 punch_hr1 = divmod(punch_hr1,1)[0]
  2409.                                                 if punch_hr1:
  2410.                                                     nw_min1 = punch_min1 - punch_hr1 * 60
  2411.                                                 else:
  2412.                                                     nw_min1 = punch_min1
  2413.                                                
  2414.                                                 if nw_min1 > 25 and nw_min1 < 55:
  2415.                                                     nm_min1 = 0.5
  2416.                                                 elif nw_min1 >= 55:
  2417.                                                     punch_hr1 = punch_hr1 + 1
  2418.                                                     nm_min1 = 0.0
  2419.                                                 else:
  2420.                                                     nm_min1 = 0.0
  2421.                                                 new_hrs1 = punch_hr1 + nm_min1
  2422.                                                 print "actual ot new_hrs1.........",new_hrs1
  2423.                                                 # ot=round(new_hrs1/2,2)
  2424.                                                 #print "shown  ot.............",ot
  2425.                                                 extra_ot= float(float(new_hrs1))
  2426.                                                
  2427.                                                 extra_ot = round(extra_ot/2,2)
  2428.                                                 val2=self.float_time_convert_main(extra_ot)
  2429.                                                 hr1=0
  2430.                                                 #print "val2...............",val2
  2431.                                                 time2 = 0.0
  2432.                                                 if val2 and len(str(val2[1])) == 1:
  2433.                                                     hr1 = 0
  2434.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2435.                                                
  2436.                                                 if val2 and len(str(val2[1])) == 2:
  2437.                                                     if int(val2[1]) >=0 and int(val2[1]) <= 24:
  2438.                                                         hr1 = 0
  2439.                     #                                elif val2[1] == 50:
  2440.                     #                                    hr1 = 5
  2441.                                                     else:
  2442.                                                          hr1 = 5
  2443.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2444.                                                 extra_ot = time2
  2445.                                                 ot = 0.0
  2446.                                                 new_hrs = 0.0
  2447.                                                 in_punch_sun = True
  2448.                                                 out_punch_sun = True
  2449.                                             else:
  2450.                                                 working_main ='S'
  2451.                                                 working_main_first_half ='S'
  2452.                                                 working_main_second_half = 'S'
  2453.                
  2454.  
  2455.                                                 over_time=datetime.strptime(str( datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") + timedelta(hours=5,minutes=30)),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_start),"%Y-%m-%d %H:%M:%S")
  2456.                                                 print "over_time.............",over_time,out_punch1,actual_start
  2457.                                                 punch_min1 = float(over_time.total_seconds() / 60)
  2458.                                                
  2459.                                                 punch_hr1 = float(punch_min1 / 60)
  2460.                                                 punch_hr1 = divmod(punch_hr1,1)[0]
  2461.                                                 if punch_hr1:
  2462.                                                     nw_min1 = punch_min1 - punch_hr1 * 60
  2463.                                                 else:
  2464.                                                     nw_min1 = punch_min1
  2465.                                                
  2466.                                                 if nw_min1 > 25 and nw_min1 < 55:
  2467.                                                     nm_min1 = 0.5
  2468.                                                 elif nw_min1 >= 55:
  2469.                                                     punch_hr1 = punch_hr1 + 1
  2470.                                                     nm_min1 = 0.0
  2471.                                                 else:
  2472.                                                     nm_min1 = 0.0
  2473.                                                 new_hrs1 = punch_hr1 + nm_min1
  2474.                                                 print "actual ot new_hrs1.........",new_hrs1
  2475.                                                 # ot=round(new_hrs1/2,2)
  2476.                                                 #print "shown  ot.............",ot
  2477.                                                 extra_ot= float(float(new_hrs1))
  2478.                                                
  2479.                                                 extra_ot = round(extra_ot/2,2)
  2480.                                                 val2=self.float_time_convert_main(extra_ot)
  2481.                                                 hr1=0
  2482.                                                 #print "val2...............",val2
  2483.                                                 time2 = 0.0
  2484.                                                 if val2 and len(str(val2[1])) == 1:
  2485.                                                     hr1 = 0
  2486.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2487.                                                
  2488.                                                 if val2 and len(str(val2[1])) == 2:
  2489.                                                     if int(val2[1]) >=0 and int(val2[1]) <= 24:
  2490.                                                         hr1 = 0
  2491.                     #                                elif val2[1] == 50:
  2492.                     #                                    hr1 = 5
  2493.                                                     else:
  2494.                                                          hr1 = 5
  2495.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  2496.                                                 extra_ot = time2
  2497.                                                 ot = 0.0
  2498.                                                 new_hrs = 0.0
  2499.  
  2500.                                                 in_punch_sun = True
  2501.                                                 out_punch_sun = True
  2502.                                                 print "Punch hr",punch_hr1,new_hrs1
  2503.                                         if in_punch_sun == True and out_punch_sun== True:
  2504.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  2505.                                                 'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch)})
  2506.                                         else:
  2507.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  2508.                                                 'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  2509.                                                 'out_punch2':str(out_punch2),'in_punch_custom':str(in_punch),'late':late,})
  2510.                                                              
  2511.                                         print "=========================NEW  PUNCH CREATED===========================",created_id
  2512.                                     except Exception,e:
  2513.                                         print "Exception Punch Card Creation...........", e.args
  2514.                                         pass
  2515.  
  2516.                             else:
  2517.                                 cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  2518.                                
  2519.                                 cr.execute(""" select  h_line.leave_date,h_line.national_holiday  from holiday_list_lines  as h_line left join holiday_list as h_list on (h_line.holiday_id = h_list.id)  
  2520.                                where  h_list.month ='"""+str(salary_month.month)+"""' and  h_list.year_id ='"""+str(salary_month.year_id.id)+"""' group by  h_line.leave_date,h_line.national_holiday
  2521.                                """)
  2522.                                
  2523.                                 temp1 = cr.fetchall()
  2524.                                 sunday_list = {}
  2525.                                 flag = False
  2526.                                 for each in temp1:
  2527.                                     sunday_list.update({each[0]:each[1]})
  2528.                    
  2529.                                 for key,val in sunday_list.items():
  2530.                                     print "=========",week_days
  2531.                                     if salary_date == key and val == False and joining_date <= salary_date:
  2532.                                         if termination_date:
  2533.                                             if termination_date >=salary_date:
  2534.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2535.                                                 start_date = salary_date
  2536.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  2537.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  2538.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  2539.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  2540.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  2541.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working = 'L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2542.                                                
  2543.                                                 full_day = cr.fetchall()
  2544.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2545.                                                
  2546.                                                 half_day = cr.fetchall()
  2547.                                                
  2548.                                                
  2549.                                                
  2550.                                                
  2551.                                                
  2552.                                                 if week_off_first == True:
  2553.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  2554.                                                     leave_first = cr.fetchall()
  2555.                                                     week_off_first = False
  2556.                                                
  2557.                                                 if leave_first:
  2558.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  2559.                                                     leave_first =False
  2560.                                                 else:    
  2561.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  2562.                                                
  2563.    
  2564.                                                 print "=====================AAA=================",int(full_day[0][0]),float((half_day[0][0])/2),start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  2565.                                                 if total_day_week >=3:
  2566.                                                     working_main = 'WO'
  2567.                                                     working_main_first_half = 'WO'
  2568.                                                     working_main_second_half = 'WO'
  2569.                                                     print "Sunday======================================="
  2570.                                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2571.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2572.                                                                                             })
  2573.                                                 else:
  2574.                                                     working_main = 'S'
  2575.                                                     working_main_first_half = 'S'
  2576.                                                     working_main_second_half = 'S'
  2577.                                                     print "Sunday======================================="
  2578.                                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2579.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2580.                                                                                             })
  2581.                                                
  2582.                                                    
  2583.  
  2584.                                         else:
  2585.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2586.                                             start_date = salary_date
  2587.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  2588.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  2589.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  2590.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  2591.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  2592.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P'  or working='L' ) and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2593.                                            
  2594.                                             full_day = cr.fetchall()
  2595.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  2596.                                            
  2597.                                             half_day = cr.fetchall()
  2598.                                            
  2599.                                            
  2600.                                            
  2601.                                            
  2602.                                            
  2603.                                             print "=========================",half_day[0][0]/2,int(half_day[0][0])/2
  2604.                                            
  2605.                                             if week_off_first == True:
  2606.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  2607.                                                 leave_first = cr.fetchall()
  2608.                                                 week_off_first = False
  2609.                                             print "========================AAAAAAAAAA==============",leave_first
  2610.                                             if leave_first:
  2611.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  2612.                                                 leave_first =False
  2613.                                             else:    
  2614.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  2615.                                            
  2616.                                             print "========================AAAAAAAAAA==============",leave_first,week_off_first,total_day_week,leave_first
  2617.                                            
  2618.    
  2619.                                             print "=====================AAA=================",int(full_day[0][0]),float((half_day[0][0])/2),start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  2620.                                             if total_day_week >=3:
  2621.                                                 working_main = 'WO'
  2622.                                                 working_main_first_half = 'WO'
  2623.                                                 working_main_second_half = 'WO'
  2624.                                                 print "Sunday======================================="
  2625.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2626.                                                                                         'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2627.                                                                                         })
  2628.                                             else:
  2629.                                                 working_main = 'S'
  2630.                                                 working_main_first_half = 'S'
  2631.                                                 working_main_second_half = 'S'
  2632.                                                 print "Sunday=======================================",total_day_week,end_date
  2633.                                                
  2634.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2635.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2636.                                                                                             })
  2637.                                                
  2638.  
  2639.                                     elif salary_date == key and val == True and joining_date <= salary_date :
  2640.                                         if termination_date :
  2641.                                             if termination_date >= salary_date:
  2642.                                                 working_main = 'H'
  2643.                                                 working_main_first_half = 'H'
  2644.                                                 working_main_second_half = 'H'
  2645.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2646.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2647.                                                     })
  2648.                                         else:
  2649.                                             working_main = 'H'
  2650.                                             working_main_first_half = 'H'
  2651.                                             working_main_second_half = 'H'
  2652.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2653.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2654.                                                 })
  2655.                                                
  2656.                                 if salary_date not in sunday_list.keys() and joining_date <= salary_date :
  2657.                                    
  2658.                                     if termination_date:
  2659.                                         if termination_date >= salary_date:
  2660.                                             leave_qty = 0.0
  2661.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2662.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  2663.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  2664.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  2665.                                             leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  2666.                                             leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  2667.                                             time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  2668.                                             time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  2669.                                             # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  2670.                                             # print "time", time_from
  2671.                                             print "select date_from,number_of_days_temp,date_to from hr_holidays where (date_from <= '"+time_to+"' and date_to >='"+time_from+"') and  employee_id = '"+str(employee_id.id)+"'"
  2672.                                             cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  2673.                                             leave_search = cr.fetchall()
  2674.                                             print "leave_search",leave_search
  2675.                                             # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  2676.                                             # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  2677.                                             if leave_search:
  2678.                                                 leave_qty = leave_search[0][1]
  2679.                                             print "===============Leave",leave_qty
  2680.                                            
  2681.                                             if leave_qty > 0.5:
  2682.                                                 week_days +=1    
  2683.                                                 working_main = 'L'
  2684.                                                 working_main_first_half = 'L'
  2685.                                                 working_main_second_half = 'L'
  2686.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2687.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2688.                                                     })
  2689.                                             elif leave_qty == 0.5:
  2690.                                                 week_days +=1    
  2691.                                                 working_main = 'HD'
  2692.                                                 working_main_first_half = 'A'
  2693.                                                 working_main_second_half = 'P'
  2694.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2695.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2696.                                                     })
  2697.                                             else:
  2698.                                                 working_main = 'A'
  2699.                                                 working_main_first_half = 'A'
  2700.                                                 working_main_second_half = 'A'
  2701.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2702.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2703.                                                     })
  2704.                                     else:
  2705.                                         leave_qty = 0.0
  2706.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  2707.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  2708.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  2709.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  2710.                                         leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  2711.                                         leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  2712.                                         time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  2713.                                         time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  2714.                                         # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  2715.                                         # print "time", time_from
  2716.                                         print "select date_from,number_of_days_temp,date_to from hr_holidays where (date_from <= '"+time_to+"' and date_to >='"+time_from+"') and  employee_id = '"+str(employee_id.id)+"'"
  2717.                                         cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  2718.                                         leave_search = cr.fetchall()
  2719.                                         print "leave_search",leave_search
  2720.                                         # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  2721.                                         # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  2722.                                         if leave_search:
  2723.                                             leave_qty = leave_search[0][1]
  2724.                                         print "===============Leave",leave_qty
  2725.                                        
  2726.                                         if leave_qty  > 0.5:
  2727.                                             week_days +=1    
  2728.                                             working_main = 'L'
  2729.                                             working_main_first_half = 'L'
  2730.                                             working_main_second_half = 'L'
  2731.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2732.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2733.                                                 })
  2734.                                         elif leave_qty == 0.5:
  2735.                                             week_days +=1    
  2736.                                             working_main = 'HD'
  2737.                                             working_main_first_half = 'A'
  2738.                                             working_main_second_half = 'P'
  2739.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2740.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2741.                                                 })
  2742.                                         else:
  2743.                                             working_main = 'A'
  2744.                                             working_main_first_half = 'A'
  2745.                                             working_main_second_half = 'A'
  2746.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  2747.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  2748.                                                 })
  2749.                                        
  2750.                                        
  2751.                                        
  2752.                                 print "IN and Out Pair attendance not found for emp on date......",employee_id,salary_date
  2753.                            
  2754.                    
  2755.                     next_date +=  timedelta(days=1)
  2756. # ++++++++++++++ All EMPLOYEES  FOR A DATE  ++++++++++++++++++++++++++++++
  2757.         elif salary_date:
  2758.             print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%============salary_date==="
  2759.  
  2760.             emp_list=emp_obj.search(cr,uid,[('active','=',True)])  
  2761.             #print "emp_list..............",emp_list  
  2762. #            month1=salary_month.month
  2763. #            print "month......... ",month1
  2764. #            year=salary_month.year_id.name
  2765. #            print "year............",year
  2766. #            if int(month1) in [1,3,5,7,8,10,12]:
  2767. #                month = 31
  2768. #            if int(month1) in [4,6,9,11]:
  2769. #                month = 30
  2770. #            if int(month1) in [2]:
  2771. #                if int(year) % 4 == 0:
  2772. #                    month = 29
  2773. #                else:
  2774. #                    month = 28  
  2775.            
  2776.  
  2777.             if 1 in emp_list:
  2778.                 emp_list.remove(1)
  2779.            
  2780.  
  2781.             if emp_list:
  2782.                 for emp in emp_list:
  2783.                     late_count = 0;
  2784.                     ot_sum_gaurav = 0
  2785.                     employee_id=emp_obj.browse(cr ,uid ,emp )
  2786.                     category = employee_id.category
  2787.                     att_list=att_obj.search(cr,uid,[('employee_id','=',employee_id.id),('day','=',salary_date)])
  2788.                     print "att_list.........",att_list
  2789.                    
  2790. #                    day_from = str(year) + '-' + str(month1) + '-' + '01'
  2791. #                    day_from = datetime.strptime(day_from,"%Y-%m-%d")
  2792. #                    print "day_from..............",day_from
  2793. #                    day_to = str(year) + '-' + str(month1) + '-' + str(month)
  2794. #                    day_to = datetime.strptime(day_to,"%Y-%m-%d")
  2795. #                    print "day_to.....................",day_to
  2796. #                    next_date = day_from.strftime('%Y-%m-%d')
  2797.                     contract_ids = cont_obj.search(cr,uid,[('employee_id','=',employee_id.id)])
  2798.                     if contract_ids:
  2799.                         contract_data = cont_obj.browse(cr, uid, contract_ids)[0]
  2800.                     else:
  2801.                         raise osv.except_osv(_('Warning !'),_("Contract is missing for employee %s." % (employee_id.name)))
  2802.                     #print "contract_data..............",contract_data
  2803. #                    for day in range(0, month):
  2804. #                        print "next_date.............",next_date
  2805.                        
  2806.                     if contract_data:
  2807.                         #salary_date = next_date.strftime('%Y-%m-%d')
  2808.                         category = employee_id.category
  2809.                         timing_main= self.timing_main_func( cr ,uid ,ids ,contract_data.working_hours ,salary_date)
  2810.                         #print "timing_main...........",timing_main
  2811.                         total_punch = "select min(name + interval'5 hour 30 minutes') ,max(name + interval'5 hour 30 minutes') from hr_attendance where name + interval '5 hours 30 minute' >= to_timestamp('"+str(timing_main['start_time'])+"',\
  2812.                        'YYYY-MM-DD HH24:MI:SS')::timestamp - interval '1 hours 30 minute' and name + interval '5 hours 30 minute' < to_timestamp('"+str(timing_main['final_time'])+"','YYYY-MM-DD HH24:MI:SS')::timestamp and employee_id = \
  2813.                        '"+str(employee_id.id)+"' "
  2814.                         cr.execute(total_punch)
  2815.                         total_punch_result = cr.fetchall()
  2816.                        
  2817.                         for data in total_punch_result:  
  2818.                             if len(data)>1 and data[0] != None and data[1] != None and data[0] != data[1]:
  2819.                                 #if total_punch_result and len(total_punch_result[0]) == 2 and total_punch_result[0][0] != None and total_punch_result[0][1] != None and total_punch_result[0][0] != total_punch_result[0][1]:
  2820.                                 in_punch = data[0]
  2821.                                 #print "in_punch....................",in_punch
  2822.                                 out_punch1 = data[1]
  2823.                                 #print "out_punch1..................",out_punch1
  2824.                                 #category =employee_id.category
  2825.                                 in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  2826.                                 #print "in_time.........",in_time
  2827.                                 out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  2828.                                 #print "out_time.........",out_time
  2829.                                 dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  2830.                                
  2831.                                 timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  2832.                                 #print "timing.................",timing
  2833.                                 lunch_mid = late = hard_absent = short_leave = hard_hd = False
  2834.                                 start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  2835.                                 time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  2836.                                 working_main= False
  2837.                                 lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  2838.                                 time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  2839.                                 elunch = lunch + timedelta(hours=4,minutes=30)
  2840. # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  2841.                                 shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  2842.                                 #print "shift............",shift
  2843.                                 start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  2844.                                 hour= int(float(shift['start']))
  2845.                                 min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  2846.                                 if min and len(min) == 1:
  2847.                                     min = str(min) +'0'
  2848.                                 if min and len(min) == 1:
  2849.                                     min = str(min)
  2850.                                 start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  2851.                                 #print "start_time..............",start_time,type(start_time)
  2852.                                 end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  2853.                                 hour= int(float(shift['end']))
  2854.                                 min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  2855.                                 if min and len(min) == 1:
  2856.                                     min = str(min) +'0'
  2857.                                 if min and len(min) == 1:
  2858.                                     min = str(min)
  2859.                                 end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  2860.                                 #print "end_time................",end_time
  2861.                                 if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  2862.                                     actual_start = start_time
  2863.                                 else:
  2864.                                     actual_start = in_punch
  2865.                                 #print "actual_start............",actual_start
  2866.                                 if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  2867.                                     actual_end = end_time
  2868.                                 else:
  2869.                                     actual_end = out_punch1
  2870.                                 #print "actual_end.............",actual_end
  2871. #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  2872.                                 #if category == 'staff':
  2873.                                 if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=30)):
  2874.                                     hard_absent =True
  2875.                                     #print "setting absent1...hard_absent =True......."
  2876.                                     working_main='A'
  2877.                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  2878.                                     #print "setting hd1..........."
  2879.                                     working_main='HD'
  2880.                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  2881.                                     print "setting P1 and late..1230........."
  2882.                                     working_main ='P'
  2883.                                     late_count = late_count+1
  2884.                                     print "+++++++++++++++++++++++++++++==================================",late_count
  2885.  
  2886.                                     late=True
  2887.                                     print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  2888.                                     if late_count>=3 and category=='staff':
  2889.                                         working_main ='HD'
  2890.                                        
  2891.                                        
  2892.                                         leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  2893.                                         if leave_obj1:
  2894.                                            
  2895.                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  2896.                                             self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  2897.                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  2898.                                             leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  2899.                                        
  2900.                                         cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  2901.                                         ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  2902.                                         el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  2903.                                        
  2904.            
  2905.                                         if cl_status[5]['remaining_leaves']>=0.5:
  2906.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  2907.                                             print "Hiii",create_id
  2908.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  2909.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  2910.                                         elif ml_status[2]['remaining_leaves']>=0.5:
  2911.                                             print "Ml"
  2912.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  2913.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  2914.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  2915.                                        
  2916.                                         elif el_status[6]['remaining_leaves']>=0.5:
  2917.                                             print "El"
  2918.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  2919.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  2920.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  2921.                                         else:
  2922.                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  2923.                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  2924.                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  2925.                                     else:
  2926.                                         working_main = 'P'
  2927.                                 else:
  2928.                                     #print "setting P1..........."
  2929.                                     working_main ='P'
  2930.                                 if not in_punch and not out_punch1:
  2931.                                     working_main = "A"
  2932.                                     #continue
  2933.                                 #print "working_main..........",working_main
  2934.         # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  2935.                                 #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  2936.                                 #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  2937.                                 #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  2938.                                 hard_hd = short_leave = False
  2939.                                 if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  2940.                                     if late_count>=3 and category=='staff':
  2941.                                         print "Late Count Reseted$$$$$$$$$$$$$$$$$$$$$$$",late_count                                    
  2942.                                         working_main = 'A'
  2943.                                     else:
  2944.                                         working_main = 'HD'
  2945.                                         hard_hd = True
  2946.                                         #print "hard_hd......setting...",hard_hd
  2947.                                 elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  2948.                                   (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))):
  2949.                                     #print "short leave  true....."
  2950.                                     short_leave = True
  2951.         # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  2952. #                                missing = new_hrs_miss = 0.0
  2953. #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  2954. #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  2955. #                                    missing = value.total_seconds()
  2956. #                                    #print "missing................",missing
  2957. #                                    miss_min = float(missing / 60)
  2958. #                                    print "miss_min............",miss_min
  2959. #                                    miss_hr = float(miss_min / 60)
  2960. #                                    #print "miss_hr.................",miss_hr
  2961. #                                    miss_hr = divmod(miss_hr,1)[0]
  2962. #                                    #print "miss_hr.................",miss_hr
  2963. #                                    if miss_hr:
  2964. #                                        #print "miss_hr...............",miss_hr
  2965. #                                        nw_min = miss_min - miss_hr * 60
  2966. #                                        #print "nw_min...1............",nw_min
  2967. #                                    else:
  2968. #                                        nw_min = miss_min
  2969. #                                        #print "nw_min......2.........",nw_min
  2970. #                                    nw_min = round(nw_min,0)
  2971. #                                    if nw_min > 5 and nw_min <= 35:
  2972. #                                        nm_min = 0.5
  2973. #                                    elif nw_min > 35:
  2974. #                                        miss_hr = miss_hr + 1
  2975. #                                        nm_min = 0.0
  2976. #                                    else:
  2977. #                                        nm_min = 0.0
  2978. #                                    print "miss_hr............",miss_hr
  2979. #                                    print "nm_min..............",nm_min
  2980. #                                    new_hrs_miss = miss_hr + nm_min
  2981. #                                    print "new_hrs_miss................",new_hrs_miss  
  2982.         #                        if new_hrs_miss < 4.0:
  2983.         #                            working_main ="A"
  2984.         #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  2985.         #                            working_main ="HD"
  2986.         #                        elif new_hrs_miss >= 6.0 :
  2987.         #                            working_main ="A"
  2988.         #                        print "working_main............",working_main
  2989.                                     # ++++ START TIME & END TIME +++++++++++++++++++
  2990.                                 st= ed= False
  2991.                                 if in_punch:
  2992.                                     st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  2993.                                 if out_punch1:
  2994.                                     ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  2995.         #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  2996.                                 #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  2997.                                 #print "ed.................",ed
  2998.                                 #if category == 'staff':
  2999.                                 if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  3000.                                  ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  3001.                                     lunch_mid =True
  3002.     #                            if category != 'staff':
  3003.     #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  3004.     #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  3005.     #                                    lunch_mid =True
  3006.                                 #print "lunch_mid...........",lunch_mid
  3007.         # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  3008.                                 new_hrs=0.0
  3009.                                 ot_hr = 0
  3010.                                 ot_min = 0
  3011.                                 if st and ed:
  3012.         # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  3013.        
  3014.                                    
  3015.                                     working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  3016.                                     punch_min = float(working_hour.total_seconds() / 60)
  3017.                                     if lunch_mid:
  3018.                                         punch_min = punch_min - 30.0
  3019.                                     punch_hr = float(punch_min / 60)
  3020.                                     punch_hr = divmod(punch_hr,1)[0]
  3021.                                     if punch_hr:
  3022.                                         nw_min = punch_min - punch_hr * 60
  3023.                                     else:
  3024.                                         nw_min = punch_min
  3025.                                    
  3026.                                     if nw_min > 24 and nw_min < 55:
  3027.                                         nm_min = 0.5
  3028.                                     elif nw_min >= 55:
  3029.                                         punch_hr = punch_hr + 1
  3030.                                         nm_min = 0.0
  3031.                                     else:
  3032.                                         nm_min = 0.0
  3033.                                     new_hrs = punch_hr + nm_min
  3034.                                     #print "new_hrs..........",new_hrs,type(new_hrs)
  3035.         # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  3036.                                     over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  3037.                                     #print "over_time.............",over_time
  3038.                                     punch_min1 = float(over_time.total_seconds() / 60)
  3039.                                    
  3040.                                     punch_hr1 = float(punch_min1 / 60)
  3041.                                     punch_hr1 = divmod(punch_hr1,1)[0]
  3042.                                     if punch_hr1:
  3043.                                         nw_min1 = punch_min1 - punch_hr1 * 60
  3044.                                     else:
  3045.                                         nw_min1 = punch_min1
  3046.                                    
  3047.                                     if nw_min1 > 25 and nw_min1 < 55:
  3048.                                         nm_min1 = 0.5
  3049.                                     elif nw_min1 >= 55:
  3050.                                         punch_hr1 = punch_hr1 + 1
  3051.                                         nm_min1 = 0.0
  3052.                                     else:
  3053.                                         nm_min1 = 0.0
  3054.                                     new_hrs1 = punch_hr1 + nm_min1
  3055.                                     #print "actual ot new_hrs1.........",new_hrs1
  3056.                                     ot=round(new_hrs1/2,2)
  3057.                                     #print "shown  ot.............",ot
  3058.                                     extra_ot = 0.0
  3059.                                     if ot > 2.0 and ot_sum_gaurav<16:
  3060.                                         extra_ot= float(float(ot -2.0))
  3061.                                         #print "ot setting to 2.0...extra_ot...........",extra_ot
  3062.                                         ot= 2.0
  3063.                                     if ot_sum_gaurav>16:
  3064.                                         extra_ot= float(float(ot))
  3065.                                         ot = 0.0
  3066. # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  3067.                                     val1=self.float_time_convert_main(ot)
  3068.                                     hr=0
  3069.                                     #print "val1...............",val1
  3070.                                     time1 = 0.0
  3071.                                     if val1 and len(str(val1[1])) == 1:
  3072.                                         hr = 0
  3073.                                         ot_hr = val1[0]
  3074.                                         ot_min = hr
  3075.                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  3076.                                    
  3077.                                     if val1 and len(str(val1[1])) == 2:
  3078.                                         if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  3079.                                             hr = 0
  3080.                                         else:
  3081.                                             hr = 5
  3082.                                         ot_hr = val1[0]
  3083.                                         ot_min = hr
  3084.                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  3085.                                     ot = time1
  3086.                                     #print "new ot...........",ot
  3087.                                     val2=self.float_time_convert_main(extra_ot)
  3088.                                     hr1=0
  3089.                                     #print "val2...............",val2
  3090.                                     time2 = 0.0
  3091.                                     if val2 and len(str(val2[1])) == 1:
  3092.                                         hr1 = 0
  3093.                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  3094.                                    
  3095.                                     if val2 and len(str(val2[1])) == 2:
  3096.                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  3097.                                             hr1 = 0
  3098.         #                                elif val2[1] == 50:
  3099.         #                                    hr1 = 5
  3100.                                         else:
  3101.                                              hr1 = 5
  3102.                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  3103.                                     extra_ot = time2
  3104.                                     #print "new extra_ot..............",extra_ot
  3105.         #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  3106.                                 print "working_main............",working_main
  3107.                                 if working_main == 'HD' or working_main == 'P':
  3108.                                     #print "going in.........."
  3109.                                     if new_hrs < 4.0:
  3110.                                         working_main = 'A'
  3111.                                         #print "working hours are less than 4.0.....",working_main
  3112.                                     elif new_hrs >= 4.0 and new_hrs < 6.0:
  3113.                                         working_main = 'HD'
  3114.                                         #late = False
  3115.                                         #print "working setting HD....",working_main
  3116.                                     elif new_hrs >= 6.0 :
  3117.                                         working_main = 'P'
  3118.                                 if category == 'worker':
  3119.                                     if working_main == 'HD' or working_main == 'P':
  3120.                                     #print "going in.........."
  3121.                                         if new_hrs < 4.0:
  3122.                                             working_main = 'A'
  3123.                                             #print "working hours are less than 4.0.....",working_main
  3124.                                         elif new_hrs >= 4.0 and new_hrs < 7.5:
  3125.                                             working_main = 'HD'
  3126.                                             #late = False
  3127.                                             #print "working setting HD....",working_main
  3128.                                         elif new_hrs >= 7.5 :
  3129.                                             working_main = 'P'
  3130.                                        
  3131.                                                                      
  3132.                                         #print "working setting P....",working_main
  3133.                                 print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave
  3134.                                 if working_main == 'P' and hard_hd:
  3135.                                     working_main = 'HD'
  3136.     #                            if working_main == 'HD' and hard_absent:
  3137.     #                                working_main = 'A'
  3138.                                 #print "working main....",working_main
  3139.                                 if new_hrs1 < 0:
  3140.                                     new_hrs1 = 0
  3141.                                 if working_main == 'A':
  3142.                                     short_leave = False
  3143.                                     late = False
  3144.                                 print "working main..working hour...ot.",working_main,new_hrs,new_hrs1
  3145. #                                print "end_time.............",end_time,type(end_time)
  3146. #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  3147.                                 if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  3148.                                     out_punch2 = out_punch1
  3149.                                 else:
  3150.                                     random_second = random.randrange(1, 59, 1)
  3151.                                     random_min = random.randrange(1, 10, 1)
  3152.                                     if ot_min >= 5 :
  3153.                                         random_min +=  30
  3154.                                     else:
  3155.                                         random_min +=  0
  3156.                                     #print "random_min..........",random_min
  3157.                                     out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  3158.                                     #print "out_punch2.............",str(out_punch2)
  3159.                                     #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  3160. #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  3161. #                                print "out_punch2..........",str(out_punch2)
  3162.                                 in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  3163.                                 out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  3164.                                 out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  3165.                                 #print "ot hr ,ot min......",ot_hr, ot_min
  3166.                                
  3167.                                 if category == 'staff':
  3168.                                     extra_ot = 0.0
  3169.                                     ot = 0.0
  3170.                                 if category != 'staff':
  3171.                                     short_leave = False
  3172.                                     late = False
  3173.         # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  3174.                                 holiday_flag = False
  3175.                                 if late_count >=3:
  3176.                                     late_count =0
  3177.                                 try:
  3178.                                     cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  3179.                                     #print "deleted ...punch card .............",cr.fetchall()
  3180.                                     #print "going................."
  3181.                                     #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  3182.                                     #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  3183.                                     if dict['holiday_id']:
  3184.                                         for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  3185.                                             if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  3186.                                                 holiday_flag = True
  3187.                                     if holiday_flag :
  3188.                                         working_main ='L'
  3189.                                         short_leave = False
  3190.                                         late = False
  3191.                                     if holiday_flag and (category =='worker' or category=='contractor'):
  3192.                                         print "ooooooooooooooooooooooootttttttttMy Block",
  3193.                                         working_main ='L'
  3194.  
  3195.                                         punch_min1 = float(working_hour.total_seconds() / 60)
  3196.                                    
  3197.                                         # punch_hr1 = float(punch_min1 / 60)
  3198.                                         # punch_hr1 = divmod(punch_hr1,1)[0]
  3199.                                         punch_hr1 = float(punch_min1 / 60)
  3200.                                         punch_hr1 = divmod(punch_hr1,1)[0]
  3201.                                         if punch_hr1:
  3202.                                             nw_min1 = punch_min1 - punch_hr1 * 60
  3203.                                         else:
  3204.                                             nw_min1 = punch_min1
  3205.                                        
  3206.                                         if nw_min1 > 25 and nw_min1 < 55:
  3207.                                             nm_min1 = 0.5
  3208.                                         elif nw_min1 >= 55:
  3209.                                             punch_hr1 = punch_hr1 + 1
  3210.                                             nm_min1 = 0.0
  3211.                                         else:
  3212.                                             nm_min1 = 0.0
  3213.                                         new_hrs1 = punch_hr1 + nm_min1
  3214.                                         extra_ot = new_hrs1
  3215.                                         new_hrs = 0
  3216.                                         ot = 0
  3217.                                         print "Punch hr",punch_hr1,new_hrs1
  3218.  
  3219.                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  3220.                                             'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  3221.                                             'out_punch2':str(out_punch2),'late':late,})
  3222.                                                          
  3223.                                     print "=========================NEW  PUNCH CREATED===========================",created_id
  3224.                                 except Exception,e:
  3225.                                     print "Exception Punch Card Creation...........", e.args
  3226.                                     pass
  3227.                             else:
  3228.                                 print "IN and Out Pair attendance not found for emp on date......",employee_id.name,salary_date
  3229.                        
  3230. #+++++++++++++++++++ ALL EMPLOYEES FOR WHOLE MONTH  +++++++++++++++++    
  3231.         else:
  3232.             #return True
  3233.             week_days = 0
  3234.             week_day = 0
  3235.             cur_obj = self.browse(cr ,uid , ids[0])
  3236.             address_id = cur_obj.address_id
  3237.             category_id = self.browse(cr ,uid , ids[0]).category
  3238.             emp_list=emp_obj.search(cr,uid,[('active','=',True),('category','=',category_id)])
  3239.             # emp_list=emp_obj.search(cr,uid,[('active','=',False),('termination_date','>','2015-03-31'),('joining_date','<','2015-05-01'),('category','=','contractor')])  
  3240.             # emp_list = [34]
  3241.             # emp_list.append(emp_list_false)
  3242.             if 1 in emp_list:
  3243.                 emp_list.remove(1)
  3244.  
  3245.             for emp in emp_obj.search(cr,uid,[('category','=',False)]):
  3246.                 if emp in emp_list:
  3247.                     emp_list.remove(emp)
  3248.                
  3249.             i = 0
  3250.             for each in emp_list:
  3251.                 i = i+1
  3252.                 employee_id = self.pool.get('hr.employee').browse(cr,uid,each)
  3253.                
  3254.                 late_count = 0;
  3255.                
  3256.                 week_off_first = True
  3257.                 short_leave_late = 0
  3258.                 ot_sum_gaurav = 0
  3259.                 month1=salary_month.month
  3260.              
  3261.                
  3262.                 year=salary_month.year_id.name  
  3263.                
  3264.                 if int(month1) in [1,3,5,7,8,10,12]:
  3265.                     month = 31
  3266.                 if int(month1) in [4,6,9,11]:
  3267.                     month = 30
  3268.                 if int(month1) in [2]:
  3269.                     if int(year) % 4 == 0:
  3270.                         month = 29
  3271.                     else:
  3272.                         month = 28
  3273.                 day_from = str(year) + '-' + str(month1) + '-' + '01'
  3274.                 day_from = datetime.strptime(day_from,"%Y-%m-%d")
  3275.                
  3276.                 day_to = str(year) + '-' + str(month1) + '-' + str(month)
  3277.                 day_to = datetime.strptime(day_to,"%Y-%m-%d")
  3278.                 #print "day_to.....................",day_to
  3279.                 next_date = day_from
  3280.                
  3281.                 contract_ids = cont_obj.search(cr,uid,[('employee_id','=',employee_id.id)])
  3282.                 if contract_ids:
  3283.                     contract_data = cont_obj.browse(cr, uid, contract_ids)[0]
  3284.                 else:
  3285.                     raise osv.except_osv(_('Warning !'),_("Contract is missing for employee %s." % (employee_id.name)))
  3286.                 #print "contract_data..............",contract_data
  3287.                 for day in range(0, month):
  3288.                     #print "next_date.............",next_date
  3289.                    
  3290.                     if contract_data:
  3291.                         week_day +=1
  3292.                         termination_date = self.pool.get('hr.employee').browse(cr,uid,employee_id.id).termination_date
  3293.                         joining_date = self.pool.get('hr.employee').browse(cr,uid,employee_id.id).joining_date
  3294.                         salary_date = next_date.strftime('%Y-%m-%d')
  3295.                         print "Employee==========================N0",i,"Employee code",employee_id.name,salary_date
  3296.                         flag = 0
  3297.                         if contract_data.weekly_shedule_id:
  3298.                             for each in contract_data.weekly_shedule_id.attendance_ids:
  3299.                                 flag = 0
  3300.                                 # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  3301.                                 date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  3302.                                 date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  3303.                                 salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  3304.                                
  3305.                                 time_diff = date_to_week - date_from_week
  3306.                                
  3307.                                 diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  3308.                                 diff_day = round(math.ceil(diff_day))
  3309.        
  3310.                                 # print "Diffre========================",diff_day
  3311.                                 next_date_week = date_from_week
  3312.                                 for day in range(0, int(diff_day+1)):
  3313.                                     if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  3314.                                         print "Hello",
  3315.                                         flag=1
  3316.                                         break
  3317.                                     next_date_week +=  timedelta(days=1)
  3318.                                 if flag == 1:
  3319.                                     print "====Each=========",each,salary_date
  3320.                                     timing_main= self.timing_main_weekly_func( cr ,uid ,ids ,each ,salary_date)
  3321.                                     print "==========Custom======",timing_main
  3322.                         else:
  3323.                             timing_main= self.timing_main_func( cr ,uid ,ids ,contract_data.working_hours ,salary_date)
  3324.                             print "hjhjhjhjhjhjhj",timing_main
  3325.                                    
  3326.                        
  3327. #                        print "salary_date.............",salary_date
  3328. #                        att_list=att_obj.search(cr,uid,[('employee_id','=',employee_id.id),('day','=',salary_date)])
  3329. #                        print "att_list..............",att_list
  3330.                         category = employee_id.category
  3331.                        
  3332.                        
  3333.                        
  3334.                         # print "Gaurav====================Working Hours",timing_main
  3335.                         #print "timing_main...........",timing_main
  3336.                         total_punch = "select min(name + interval'5 hour 30 minutes') ,max(name + interval'5 hour 30 minutes') from hr_attendance where name + interval '5 hours 30 minute' >= to_timestamp('"+str(timing_main['start_time'])+"',\
  3337.                        'YYYY-MM-DD HH24:MI:SS')::timestamp - interval '1 hours 30 minute' and name + interval '5 hours 30 minute' < to_timestamp('"+str(timing_main['final_time'])+"','YYYY-MM-DD HH24:MI:SS')::timestamp and employee_id = \
  3338.                        '"+str(employee_id.id)+"' "
  3339.                         cr.execute(total_punch)
  3340.                         total_punch_result = cr.fetchall()
  3341.                         # print "Gaurav====================Working Hours",timing_main,total_punch_result
  3342.                         for data in total_punch_result:
  3343.                              
  3344.                             if len(data)>1 and data[0] != None and data[1] != None and data[0] != data[1] and joining_date <= salary_date:
  3345.                                 #if total_punch_result and len(total_punch_result[0]) == 2 and total_punch_result[0][0] != None and total_punch_result[0][1] != None and total_punch_result[0][0] != total_punch_result[0][1]:
  3346.                                 if termination_date:
  3347.                                     if termination_date>=salary_date:
  3348.                                         in_punch = data[0]
  3349.                                         #print "in_punch....................",in_punch
  3350.                                         out_punch1 = data[1]
  3351.                                         #print "out_punch1..................",out_punch1
  3352.                                         #category =employee_id.category
  3353.                                         in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  3354.                                         #print "in_time.........",in_time
  3355.                                         out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  3356.                                         #print "out_time.........",out_time
  3357.                                         dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  3358.                                        
  3359.                                         timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  3360.                                         #print "timing.................",timing
  3361.                                         lunch_mid = late = hard_absent = short_leave = hard_hd = False
  3362.                                         start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  3363.                                         time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  3364.                                         working_main= False
  3365.                                         working_main_first_half = False
  3366.                                         working_main_second_half = False
  3367.                                         unpaid_flag=False
  3368.                                         third_late = False
  3369.                                         second_short = False
  3370.                                        
  3371.                                         lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  3372.                                         time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  3373.                                         elunch = lunch + timedelta(hours=4,minutes=30)
  3374.         # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  3375.                                        
  3376.                                         if contract_data.weekly_shedule_id:
  3377.                                             for each in contract_data.weekly_shedule_id.attendance_ids:
  3378.                                                 flag = 0
  3379.                                                 # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  3380.                                                 date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  3381.                                                 date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  3382.                                                 salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  3383.                                                
  3384.                                                 time_diff = date_to_week - date_from_week
  3385.                                                
  3386.                                                 diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  3387.                                                 diff_day = round(math.ceil(diff_day))
  3388.                        
  3389.                                                 # print "Diffre========================",diff_day
  3390.                                                 next_date_week = date_from_week
  3391.                                                 for day in range(0, int(diff_day+1)):
  3392.                                                     if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  3393.         #                                                 print "Hello",
  3394.                                                         flag=1
  3395.                                                         break
  3396.                                                     next_date_week +=  timedelta(days=1)
  3397.                                                 if flag == 1:
  3398.         #                                             print "====Each=========",each,salary_date
  3399.                                                     shift = self.working_hours_on_day_weekly(cr, uid, each, salary_date, context)
  3400.                                         else:
  3401.                                             shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  3402.                                        
  3403.                                         # print "shift............",shift,salary_date
  3404.                                        
  3405.                                         start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  3406.                                         hour= int(float(shift['start']))
  3407.                                         min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  3408.                                         if min and len(min) == 1:
  3409.                                             min = str(min) +'0'
  3410.                                         if min and len(min) == 1:
  3411.                                             min = str(min)
  3412.                                         start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  3413.                                         # print "start_time..............",start_time,type(start_time)
  3414.                                         end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  3415.                                         hour= int(float(shift['end']))
  3416.                                         min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  3417.                                         if min and len(min) == 1:
  3418.                                             min = str(min) +'0'
  3419.                                         if min and len(min) == 1:
  3420.                                             min = str(min)
  3421.                                         end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  3422.                                         #print "end_time................",end_time
  3423.                                         if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  3424.                                             actual_start = start_time
  3425.                                         else:
  3426.                                             actual_start = in_punch
  3427.                                         #print "actual_start............",actual_start
  3428.                                         if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  3429.                                             actual_end = end_time
  3430.                                         else:
  3431.                                             actual_end = out_punch1
  3432.                                         #print "actual_end.............",actual_end
  3433.         #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  3434.                                         #if category == 'staff':
  3435.                                         if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=32)):
  3436.                                             hard_absent =True
  3437.                                             #print "setting absent1...hard_absent =True......."
  3438.                                             working_main='A'
  3439.                                         elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  3440.                                             #print "setting hd1..........."
  3441.                                             working_main='HD'
  3442.                                         elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  3443.                                            
  3444.                                             print "setting P1 and late..859.........12",in_punch
  3445.                                            # working_main ='P'
  3446.                                             late=True
  3447.                                             late_count = late_count+1
  3448.                                             print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  3449.                                             holiday_flag_1=False
  3450.                                             if dict['holiday_id']:
  3451.                                                     for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  3452.                                                         if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  3453.                                                             holiday_flag_1 = True
  3454.                                             if late_count>=3 and category=='staff' and holiday_flag_1==False:
  3455.                                                 working_main ='HD'
  3456.                                                
  3457.                                                
  3458.                                                 punch_in_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  3459.                                                 punch_in_hd= punch_in_hd.strftime("%Y-%m-%d 04:00:30")
  3460.                                                 punch_out_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  3461.                                                 punch_out_hd = punch_out_hd.strftime("%Y-%m-%d 08:10:30")
  3462.                                                
  3463.                                                
  3464.                                                
  3465.                                                
  3466.                                                
  3467.                                                
  3468.                                                 leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  3469.                                                 if leave_obj1:
  3470.                                                    
  3471.                                                     self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  3472.                                                     self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  3473.                                                     self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  3474.                                                     leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  3475.                                                
  3476.                                                 cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  3477.                                                 ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  3478.                                                 el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  3479.                                                
  3480.                    
  3481.                                                 if cl_status[5]['remaining_leaves']>=0.5:
  3482.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  3483.                                                     print "Hiii",create_id
  3484.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  3485.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  3486.                                                 elif ml_status[2]['remaining_leaves']>=0.5:
  3487.                                                     print "Ml"
  3488.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  3489.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  3490.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  3491.                                                 elif el_status[6]['remaining_leaves']>=0.5:
  3492.                                                     print "El"
  3493.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  3494.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  3495.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  3496.                                                 else:
  3497.                                                     create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  3498.                                                     a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  3499.                                                     unpaid_flag = True
  3500.                                                     print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  3501.                                             else:
  3502.                                                 working_main = 'P'
  3503.                                         else:
  3504.                                             #print "setting P1..........."
  3505.                                             working_main ='P'
  3506.                                         if not in_punch and not out_punch1:
  3507.                                             working_main = "A"
  3508.                                             #continue
  3509.                                         #print "working_main..........",working_main
  3510.                 # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  3511.                                         #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  3512.                                         #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  3513.                                         #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  3514.                                         hard_hd = short_leave = False
  3515.                                         if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  3516.                                             if late_count>=3 and category=='staff':
  3517.                                                                                    
  3518.                                                 working_main = 'HD'
  3519.                                                
  3520.                                             else:
  3521.                                                 #print "hard hd true....."
  3522.                                                 working_main = 'HD'
  3523.                                                 hard_hd = True
  3524.                                             #print "hard_hd......setting...",hard_hd
  3525.                                         elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  3526.                                           (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=5))):
  3527.                                             #print "short leave  true....."
  3528.                                             short_leave = True
  3529.                                             short_leave_late = short_leave_late + 1
  3530.                 # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  3531.         #                                missing = new_hrs_miss = 0.0
  3532.         #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  3533.         #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  3534.         #                                    missing = value.total_seconds()
  3535.         #                                    #print "missing................",missing
  3536.         #                                    miss_min = float(missing / 60)
  3537.         #                                    print "miss_min............",miss_min
  3538.         #                                    miss_hr = float(miss_min / 60)
  3539.         #                                    #print "miss_hr.................",miss_hr
  3540.         #                                    miss_hr = divmod(miss_hr,1)[0]
  3541.         #                                    #print "miss_hr.................",miss_hr
  3542.         #                                    if miss_hr:
  3543.         #                                        #print "miss_hr...............",miss_hr
  3544.         #                                        nw_min = miss_min - miss_hr * 60
  3545.         #                                        #print "nw_min...1............",nw_min
  3546.         #                                    else:
  3547.         #                                        nw_min = miss_min
  3548.         #                                        #print "nw_min......2.........",nw_min
  3549.         #                                    nw_min = round(nw_min,0)
  3550.         #                                    if nw_min > 5 and nw_min <= 35:
  3551.         #                                        nm_min = 0.5
  3552.         #                                    elif nw_min > 35:
  3553.         #                                        miss_hr = miss_hr + 1
  3554.         #                                        nm_min = 0.0
  3555.         #                                    else:
  3556.         #                                        nm_min = 0.0
  3557.         #                                    print "miss_hr............",miss_hr
  3558.         #                                    print "nm_min..............",nm_min
  3559.         #                                    new_hrs_miss = miss_hr + nm_min
  3560.         #                                    print "new_hrs_miss................",new_hrs_miss  
  3561.                 #                        if new_hrs_miss < 4.0:
  3562.                 #                            working_main ="A"
  3563.                 #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  3564.                 #                            working_main ="HD"
  3565.                 #                        elif new_hrs_miss >= 6.0 :
  3566.                 #                            working_main ="A"
  3567.                 #                        print "working_main............",working_main
  3568.                                             # ++++ START TIME & END TIME +++++++++++++++++++
  3569.                                         st= ed= False
  3570.                                         if in_punch:
  3571.                                             st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  3572.                                         if out_punch1:
  3573.                                             ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  3574.                 #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  3575.                                         #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  3576.                                         #print "ed.................",ed
  3577.                                         #if category == 'staff':
  3578.                                         if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  3579.                                          ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  3580.                                             lunch_mid =True
  3581.             #                            if category != 'staff':
  3582.             #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  3583.             #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  3584.             #                                    lunch_mid =True
  3585.                                         #print "lunch_mid...........",lunch_mid
  3586.                 # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  3587.                                         new_hrs=0.0
  3588.                                         ot_hr = 0
  3589.                                         ot_min = 0
  3590.                                         if st and ed:
  3591.                 # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  3592.                
  3593.                                            
  3594.                                             working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  3595.                                             punch_min = float(working_hour.total_seconds() / 60)
  3596.                                            
  3597.                                             if category == 'worker' and category=='contractor':
  3598.                                                 if lunch_mid:
  3599.                                                     punch_min = punch_min - 30.0
  3600.                                                
  3601.                                             punch_hr = float(punch_min / 60)
  3602.                                             punch_hr = divmod(punch_hr,1)[0]
  3603.                                             if punch_hr:
  3604.                                                 nw_min = punch_min - punch_hr * 60
  3605.                                             else:
  3606.                                                 nw_min = punch_min
  3607.                                            
  3608.                                             if nw_min > 24 and nw_min < 55:
  3609.                                                 nm_min = 0.5
  3610.                                             elif nw_min >= 55:
  3611.                                                 punch_hr = punch_hr + 1
  3612.                                                 nm_min = 0.0
  3613.                                             else:
  3614.                                                 nm_min = 0.0
  3615.                                             new_hrs = punch_hr + nm_min
  3616.                                             #print "new_hrs..........",new_hrs,type(new_hrs)
  3617.                 # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  3618.                                             over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  3619.                                             #print "over_time.............",over_time
  3620.                                             punch_min1 = float(over_time.total_seconds() / 60)
  3621.                                            
  3622.                                             punch_hr1 = float(punch_min1 / 60)
  3623.                                             punch_hr1 = divmod(punch_hr1,1)[0]
  3624.                                             if punch_hr1:
  3625.                                                 nw_min1 = punch_min1 - punch_hr1 * 60
  3626.                                             else:
  3627.                                                 nw_min1 = punch_min1
  3628.                                            
  3629.                                             if nw_min1 > 25 and nw_min1 < 55:
  3630.                                                 nm_min1 = 0.5
  3631.                                             elif nw_min1 >= 55:
  3632.                                                 punch_hr1 = punch_hr1 + 1
  3633.                                                 nm_min1 = 0.0
  3634.                                             else:
  3635.                                                 nm_min1 = 0.0
  3636.                                             new_hrs1 = punch_hr1 + nm_min1
  3637.                                             #print "actual ot new_hrs1.........",new_hrs1
  3638.                                             ot=round(new_hrs1/2,2)
  3639.                                             #print "shown  ot.............",ot
  3640.                                             extra_ot = 0.0
  3641.                                             if ot > 2.0:
  3642.                                                 extra_ot= float(float(ot -2.0))
  3643.                                                 #print "ot setting to 2.0...extra_ot...........",extra_ot
  3644.                                                 ot= 2.0
  3645.                                             # if ot > 2.0 and ot_sum_gaurav<16:
  3646.                                             #     extra_ot= float(float(ot -2.0))
  3647.                                             #     #print "ot setting to 2.0...extra_ot...........",extra_ot
  3648.                                             #     ot= 2.0
  3649.                                             # if ot_sum_gaurav>15.5:
  3650.                                             #     extra_ot= float(float(ot))
  3651.                                             #     ot = 0.0
  3652.                                                 #print "ot setting to 2.0...extra_ot...........",extra_ot
  3653.                                            
  3654.                                             ot_sum_gaurav = ot_sum_gaurav+ot
  3655.         # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  3656.                                             val1=self.float_time_convert_main(ot)
  3657.                                             hr=0
  3658.                                             #print "val1...............",val1
  3659.                                             time1 = 0.0
  3660.                                             if val1 and len(str(val1[1])) == 1:
  3661.                                                 hr = 0
  3662.                                                 ot_hr = val1[0]
  3663.                                                 ot_min = hr
  3664.                                                 time1 = str(val1[0]) +'.'+str(hr) + '0'
  3665.                                            
  3666.                                             if val1 and len(str(val1[1])) == 2:
  3667.                                                 if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  3668.                                                     hr = 0
  3669.                                                 else:
  3670.                                                     hr = 5
  3671.                                                 ot_hr = val1[0]
  3672.                                                 ot_min = hr
  3673.                                                 time1 = str(val1[0]) +'.'+str(hr) + '0'
  3674.                                             ot = time1
  3675.                                             #print "new ot...........",ot
  3676.                                             val2=self.float_time_convert_main(extra_ot)
  3677.                                             hr1=0
  3678.                                             #print "val2...............",val2
  3679.                                             time2 = 0.0
  3680.                                             if val2 and len(str(val2[1])) == 1:
  3681.                                                 hr1 = 0
  3682.                                                 time2 = str(val2[0]) +'.'+str(hr1) + '0'
  3683.                                            
  3684.                                             if val2 and len(str(val2[1])) == 2:
  3685.                                                 if int(val2[1]) >=0 and int(val2[1]) <= 24:
  3686.                                                     hr1 = 0
  3687.                 #                                elif val2[1] == 50:
  3688.                 #                                    hr1 = 5
  3689.                                                 else:
  3690.                                                      hr1 = 5
  3691.                                                 time2 = str(val2[0]) +'.'+str(hr1) + '0'
  3692.                                             extra_ot = time2
  3693.                                             #print "new extra_ot..............",extra_ot
  3694.                 #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  3695.                                        
  3696.                                        
  3697.                                         if category == 'worker' or category == 'contractor' :
  3698.                                             if working_main == 'HD' or working_main == 'P':
  3699.                                             #print "going in.........."
  3700.                                                 if new_hrs < 4.0:
  3701.                                                     working_main = 'A'
  3702.                                                     #print "working hours are less than 4.0.....",working_main
  3703.                                                 elif new_hrs >= 4.0 and new_hrs < 7.5:
  3704.                                                     working_main = 'HD'
  3705.                                                     #late = False
  3706.                                                     #print "working setting HD....",working_main
  3707.                                                 elif new_hrs >= 7.5 :
  3708.                                                     working_main = 'P'
  3709.                                                
  3710.                                                                              
  3711.                                                 #print "working setting P....",working_main
  3712.                                         if working_main == 'HD' and working_main == 'A':
  3713.                                            working_main = 'A'
  3714.                                        
  3715.                                        
  3716.                                         if category == 'staff':
  3717.                                             if new_hrs <= 3.0:
  3718.                                                 working_main = 'A'
  3719.                                         # print "===================================",working_main
  3720.                                         # error
  3721.                                         if short_leave_late >= 2 and short_leave == True and category == 'staff':
  3722.                                             second_short = True
  3723.                                             working_main = 'HD'
  3724.                                             working_main_second_half = 'A'
  3725.                                             working_main_first_half ='P'
  3726.                                        
  3727.                                         if late_count >= 3 and late==True and category == 'staff' and (working_main=='HD' or working_main=='P'):
  3728.                                             third_late = True
  3729.                                             if unpaid_flag == True:
  3730.                                                 working_main = 'HD'
  3731.                                                 working_main_first_half = 'A'
  3732.                                                 working_main_second_half = 'P'
  3733.                                             else:
  3734.                                                 working_main = 'P'
  3735.                                                 working_main_first_half = 'L'
  3736.                                                 working_main_second_half = 'P'
  3737.                                                
  3738.                                            
  3739.                                        
  3740.                                              
  3741.        
  3742.                                         print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave_late
  3743.                                         if working_main == 'P' and hard_hd:
  3744.                                             working_main = 'HD'
  3745.             #                            if working_main == 'HD' and hard_absent:
  3746.             #                                working_main = 'A'
  3747.                                         #print "working main....",working_main
  3748.                                         if new_hrs1 < 0:
  3749.                                             new_hrs1 = 0
  3750.  
  3751.                                         in_punch_sun = False
  3752.                                         out_punch_sun = False
  3753.                                         if working_main == 'A':
  3754.                                             short_leave = False
  3755.                                             late = False
  3756.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  3757.                                             leave_qty = 0.0
  3758.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  3759.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  3760.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  3761.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  3762.                                             leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  3763.                                             leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  3764.                                             time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  3765.                                             time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  3766.                                             # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  3767.                                             # print "time", time_from
  3768.                                
  3769.                                             cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  3770.                                             leave_search = cr.fetchall()
  3771.                                            
  3772.                                             # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  3773.                                             # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  3774.                                             if leave_search:
  3775.                                                 leave_qty = leave_search[0][1]
  3776.                                            
  3777.                                             if leave_search and leave_qty == 0.5:
  3778.                                                 working_main_first_half = 'A'
  3779.                                                 working_main_second_half= 'L'
  3780.                                                 working_main = 'HD'
  3781.                                                 in_punch_sun = True
  3782.                                                 out_punch_sun = True
  3783.                                             elif leave_search and leave_qty > 0.5:
  3784.                                                 working_main_first_half = 'L'
  3785.                                                 working_main_second_half= 'L'
  3786.                                                 working_main = 'L'
  3787.                                                 in_punch_sun = True
  3788.                                                 out_punch_sun = True
  3789.                                             else:
  3790.                                                 working_main_first_half = 'A'
  3791.                                                 working_main_second_half= 'A'
  3792.                                                 working_main = 'A'
  3793.                                        
  3794.                                         if working_main == 'P' and third_late==False:
  3795.                                             working_main_first_half = 'P'
  3796.                                             working_main_second_half= 'P'
  3797.                                            
  3798.                                            
  3799.                                         if working_main == 'HD' and third_late==False:
  3800.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  3801.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  3802.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  3803.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  3804.                                             leave_date = leave_date + timedelta(hours=03,minutes=00)
  3805.                                             leave_date1 = leave_date + timedelta(hours=23,minutes=00)
  3806.                                             leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  3807.                                             print "time", leave_date
  3808.                                             leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','>=',str(leave_date)),('date_to','<=',str(leave_date1)),('holiday_status_id','!=',9),('employee_id','=',employee_id.id)])
  3809.                                             print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  3810.                                            
  3811.                                             if leave_list:
  3812.                                                 leave_qty = self.pool.get('hr.holidays').browse(cr,uid,leave_list[0]).number_of_days_temp
  3813.                                            
  3814.                                            
  3815.                                            
  3816.                                                
  3817.                                             in_punch_time_in = datetime.strptime(in_punch, DATETIME_FORMAT)
  3818.                                             out_punch_time_out = datetime.strptime(out_punch1, DATETIME_FORMAT)
  3819.                                             time_from = datetime.strptime(str(in_punch_time_in),'%Y-%m-%d %H:%M:%S').timetuple()
  3820.                                             time_to = datetime.strptime(str(out_punch_time_out),'%Y-%m-%d %H:%M:%S').timetuple()
  3821.                                             if (time_from.tm_hour >= 8 and time_from.tm_hour<=17) and (time_to.tm_hour<=17):
  3822.                                                 if leave_list and leave_qty == 0.5:
  3823.                                                     working_main = 'P'  
  3824.                                                     working_main_first_half = 'P'
  3825.                                                     working_main_second_half= 'L'
  3826.                                                 else:
  3827.                                                     working_main_first_half= 'P'
  3828.                                                     working_main_second_half= 'A'
  3829.                                             elif (time_from.tm_hour >= 10 and time_from.tm_hour<=18) and (time_to.tm_hour<=23):
  3830.                                                 if leave_list and leave_qty == 0.5:
  3831.                                                     working_main = 'P'
  3832.                                                     working_main_second_half = 'P'
  3833.                                                     working_main_first_half = 'L'
  3834.                                                 else:
  3835.                                                     working_main_first_half = 'A'
  3836.                                                     working_main_second_half = 'P'
  3837.                                            
  3838.                                        
  3839.                                            
  3840.                                        
  3841.                                         print "working main..working hour...ot.",working_main,new_hrs,new_hrs1,ot,ot_sum_gaurav
  3842.                                        
  3843.         #                                print "end_time.............",end_time,type(end_time)
  3844.         #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  3845.                                         if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  3846.                                             out_punch2 = out_punch1
  3847.                                         else:
  3848.                                             random_second = random.randrange(1, 59, 1)
  3849.                                             random_min = random.randrange(1, 10, 1)
  3850.                                             if ot_min >= 5 :
  3851.                                                 random_min +=  30
  3852.                                             else:
  3853.                                                 random_min +=  0
  3854.                                             #print "random_min..........",random_min
  3855.                                             out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  3856.                                             #print "out_punch2.............",str(out_punch2)
  3857.                                             #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  3858.         #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  3859.         #                                print "out_punch2..........",str(out_punch2)
  3860.                                         in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  3861.                                         out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  3862.                                         out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  3863.                                         #print "ot hr ,ot min......",ot_hr, ot_min
  3864.                                        
  3865.                                         if (category == 'worker' or category == 'contractor') and (working_main == 'A' or working_main=='L'):
  3866.                                             extra_ot = 0.0
  3867.                                             ot = 0.0
  3868.                                             new_hrs1 = 0.0
  3869.                                            
  3870.                                            
  3871.                                         if category == 'staff':
  3872.                                             extra_ot = 0.0
  3873.                                             ot = 0.0
  3874.                                         if category != 'staff':
  3875.                                             short_leave = False
  3876.                                             late = False
  3877.                 # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  3878.                                         holiday_flag = False
  3879.                                        
  3880.                                         try:
  3881.                                             cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  3882.                                             #print "deleted ...punch card .............",cr.fetchall()
  3883.                                             #print "going................."
  3884.                                             #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  3885.                                             #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  3886.                                            
  3887.                                             if dict['holiday_id']:
  3888.                                                 for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  3889.                                                     if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  3890.                                                         holiday_flag = True
  3891.                                            
  3892.                                             if holiday_flag == True and category == 'staff':
  3893.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  3894.                                                 start_date = salary_date
  3895.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  3896.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  3897.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  3898.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  3899.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  3900.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  3901.                                                
  3902.                                                 full_day = cr.fetchall()
  3903.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  3904.                                                
  3905.                                                 half_day = cr.fetchall()
  3906.                                                
  3907.                                                 if week_off_first == True:
  3908.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  3909.                                                     leave_first = cr.fetchall()
  3910.                                                     week_off_first =False
  3911.                                                
  3912.                                                 if leave_first:
  3913.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  3914.                                                     leave_first =False
  3915.                                                 else:    
  3916.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  3917.                                                
  3918.  
  3919.                                                 print "========================AAAAAAAAAA==============",int(full_day[0][0]),int((half_day[0][0])/2),total_day_week,full_day[0][0],half_day[0][0]
  3920.                                                
  3921.                                                 if total_day_week >=3:
  3922.                                                     working_main ='WO'
  3923.                                                     working_main_first_half = 'WO'
  3924.                                                     working_main_second_half = 'WO'
  3925.                                                     short_leave = False
  3926.                                                     late = False
  3927.                                                     late_count = late_count-1
  3928.                                                     in_punch_sun = True
  3929.                                                     out_punch_sun = True
  3930.                                                 else:
  3931.                                                     working_main ='S'
  3932.                                                     working_main_first_half ='S'
  3933.                                                     working_main_second_half = 'S'
  3934.                                                     short_leave = False
  3935.                                                     late = False
  3936.                                                     late_count = late_count-1
  3937.                                                     in_punch_sun = True
  3938.                                                     out_punch_sun = True
  3939.                                        
  3940.                                            
  3941.                                             if holiday_flag == True and (category =='worker' or category=='contractor'):
  3942.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  3943.                                                 start_date = salary_date
  3944.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  3945.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  3946.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  3947.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  3948.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  3949.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  3950.                                                
  3951.                                                 full_day = cr.fetchall()
  3952.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  3953.                                                
  3954.                                                 half_day = cr.fetchall()
  3955.                                                
  3956.                                                
  3957.                                                
  3958.                                                
  3959.                                                
  3960.                                                 if week_off_first == True:
  3961.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  3962.                                                     leave_first = cr.fetchall()
  3963.                                                     week_off_first =False
  3964.                                                
  3965.                                                 if leave_first:
  3966.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  3967.                                                     leave_first =False
  3968.                                                 else:    
  3969.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  3970.                                                
  3971.  
  3972.     #                                             print "======================================",start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  3973.                                                 if total_day_week >=3:
  3974.                                                     working_main ='WO'
  3975.                                                     working_main_first_half ='WO'
  3976.                                                     working_main_second_half = 'WO'
  3977.            
  3978.                                                     punch_min1 = float(working_hour.total_seconds() / 60)
  3979.                                                
  3980.                                                     # punch_hr1 = float(punch_min1 / 60)
  3981.                                                     # punch_hr1 = divmod(punch_hr1,1)[0]
  3982.                                                     punch_hr1 = float(punch_min1 / 60)
  3983.                                                     punch_hr1 = divmod(punch_hr1,1)[0]
  3984.                                                     if punch_hr1:
  3985.                                                         nw_min1 = punch_min1 - punch_hr1 * 60
  3986.                                                     else:
  3987.                                                         nw_min1 = punch_min1
  3988.                                                    
  3989.                                                     if nw_min1 > 25 and nw_min1 < 55:
  3990.                                                         nm_min1 = 0.5
  3991.                                                     elif nw_min1 >= 55:
  3992.                                                         punch_hr1 = punch_hr1 + 1
  3993.                                                         nm_min1 = 0.0
  3994.                                                     else:
  3995.                                                         nm_min1 = 0.0
  3996.                                                     new_hrs1 = punch_hr1 + nm_min1
  3997.                                                     extra_ot = new_hrs1
  3998.                                                     new_hrs = 0
  3999.                                                     ot = 0
  4000.                                                    
  4001.                                                     extra_ot = round(extra_ot/2,2)
  4002.                                                     val2=self.float_time_convert_main(extra_ot)
  4003.                                                     hr1=0
  4004.                                                     #print "val2...............",val2
  4005.                                                     time2 = 0.0
  4006.                                                     if val2 and len(str(val2[1])) == 1:
  4007.                                                         hr1 = 0
  4008.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4009.                                                    
  4010.                                                     if val2 and len(str(val2[1])) == 2:
  4011.                                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  4012.                                                             hr1 = 0
  4013.                         #                                elif val2[1] == 50:
  4014.                         #                                    hr1 = 5
  4015.                                                         else:
  4016.                                                              hr1 = 5
  4017.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4018.                                                     extra_ot = time2
  4019.                                                     in_punch_sun = True
  4020.                                                     out_punch_sun = True
  4021.                                                 else:
  4022.                                                     working_main ='S'
  4023.                                                     working_main_first_half ='S'
  4024.                                                     working_main_second_half = 'S'
  4025.                                                     punch_min1 = float(working_hour.total_seconds() / 60)
  4026.                                                
  4027.                                                     # punch_hr1 = float(punch_min1 / 60)
  4028.                                                     # punch_hr1 = divmod(punch_hr1,1)[0]
  4029.                                                     punch_hr1 = float(punch_min1 / 60)
  4030.                                                     punch_hr1 = divmod(punch_hr1,1)[0]
  4031.                                                     if punch_hr1:
  4032.                                                         nw_min1 = punch_min1 - punch_hr1 * 60
  4033.                                                     else:
  4034.                                                         nw_min1 = punch_min1
  4035.                                                    
  4036.                                                     if nw_min1 > 25 and nw_min1 < 55:
  4037.                                                         nm_min1 = 0.5
  4038.                                                     elif nw_min1 >= 55:
  4039.                                                         punch_hr1 = punch_hr1 + 1
  4040.                                                         nm_min1 = 0.0
  4041.                                                     else:
  4042.                                                         nm_min1 = 0.0
  4043.                                                     new_hrs1 = punch_hr1 + nm_min1
  4044.                                                     extra_ot = new_hrs1
  4045.                                                     new_hrs = 0
  4046.                                                     ot = 0
  4047.                                                    
  4048.                                                     extra_ot = round(extra_ot/2,2)
  4049.                                                     val2=self.float_time_convert_main(extra_ot)
  4050.                                                     hr1=0
  4051.                                                     #print "val2...............",val2
  4052.                                                     time2 = 0.0
  4053.                                                     if val2 and len(str(val2[1])) == 1:
  4054.                                                         hr1 = 0
  4055.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4056.                                                    
  4057.                                                     if val2 and len(str(val2[1])) == 2:
  4058.                                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  4059.                                                             hr1 = 0
  4060.                         #                                elif val2[1] == 50:
  4061.                         #                                    hr1 = 5
  4062.                                                         else:
  4063.                                                              hr1 = 5
  4064.                                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4065.                                                     extra_ot = time2
  4066.                                                     in_punch_sun = True
  4067.                                                     out_punch_sun = True
  4068.                                                     print "Punch hr",punch_hr1,new_hrs1
  4069.                                             if in_punch_sun == True and out_punch_sun== True:
  4070.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  4071.                                                     'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch)})
  4072.                                             else:
  4073.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  4074.                                                     'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  4075.                                                     'out_punch2':str(out_punch2),'in_punch_custom':str(in_punch),'late':late,})
  4076.                                                                  
  4077.                                             print "=========================NEW  PUNCH CREATED===========================",created_id
  4078.                                         except Exception,e:
  4079.                                             print "Exception Punch Card Creation...........", e.args
  4080.                                             pass
  4081.  
  4082.                                 else:
  4083.                                     in_punch = data[0]
  4084.                                     #print "in_punch....................",in_punch
  4085.                                     out_punch1 = data[1]
  4086.                                     #print "out_punch1..................",out_punch1
  4087.                                     #category =employee_id.category
  4088.                                     in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  4089.                                     #print "in_time.........",in_time
  4090.                                     out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  4091.                                     #print "out_time.........",out_time
  4092.                                     dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  4093.                                    
  4094.                                     timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  4095.                                     #print "timing.................",timing
  4096.                                     lunch_mid = late = hard_absent = short_leave = hard_hd = False
  4097.                                     start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  4098.                                     time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  4099.                                     working_main= False
  4100.                                     working_main_first_half = False
  4101.                                     working_main_second_half = False
  4102.                                     unpaid_flag=False
  4103.                                     third_late = False
  4104.                                     second_short = False
  4105.                                    
  4106.                                     lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  4107.                                     time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  4108.                                     elunch = lunch + timedelta(hours=4,minutes=30)
  4109.     # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  4110.                                    
  4111.                                     if contract_data.weekly_shedule_id:
  4112.                                         for each in contract_data.weekly_shedule_id.attendance_ids:
  4113.                                             flag = 0
  4114.                                             # print "Gaurav==========123",salary_date,each.dt_from,each.date_to      
  4115.                                             date_from_week = datetime.strptime(each.dt_from,DEFAULT_SERVER_DATE_FORMAT).date()
  4116.                                             date_to_week = datetime.strptime(each.date_to,DEFAULT_SERVER_DATE_FORMAT).date()
  4117.                                             salary_date_week = datetime.strptime(str(salary_date),DEFAULT_SERVER_DATE_FORMAT).date()
  4118.                                            
  4119.                                             time_diff = date_to_week - date_from_week
  4120.                                            
  4121.                                             diff_day = time_diff.days + (float(time_diff.seconds) / 86400)
  4122.                                             diff_day = round(math.ceil(diff_day))
  4123.                    
  4124.                                             # print "Diffre========================",diff_day
  4125.                                             next_date_week = date_from_week
  4126.                                             for day in range(0, int(diff_day+1)):
  4127.                                                 if datetime.strptime((next_date_week.strftime('%Y-%m-%d')),"%Y-%m-%d").date() == salary_date_week:
  4128.     #                                                 print "Hello",
  4129.                                                     flag=1
  4130.                                                     break
  4131.                                                 next_date_week +=  timedelta(days=1)
  4132.                                             if flag == 1:
  4133.     #                                             print "====Each=========",each,salary_date
  4134.                                                 shift = self.working_hours_on_day_weekly(cr, uid, each, salary_date, context)
  4135.                                     else:
  4136.                                         shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  4137.                                    
  4138.                                     # print "shift............",shift,salary_date
  4139.                                    
  4140.                                     start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  4141.                                     hour= int(float(shift['start']))
  4142.                                     min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  4143.                                     if min and len(min) == 1:
  4144.                                         min = str(min) +'0'
  4145.                                     if min and len(min) == 1:
  4146.                                         min = str(min)
  4147.                                     start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  4148.                                     # print "start_time..............",start_time,type(start_time)
  4149.                                     end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  4150.                                     hour= int(float(shift['end']))
  4151.                                     min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  4152.                                     if min and len(min) == 1:
  4153.                                         min = str(min) +'0'
  4154.                                     if min and len(min) == 1:
  4155.                                         min = str(min)
  4156.                                     end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  4157.                                     #print "end_time................",end_time
  4158.                                     if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  4159.                                         actual_start = start_time
  4160.                                     else:
  4161.                                         actual_start = in_punch
  4162.                                     #print "actual_start............",actual_start
  4163.                                     if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  4164.                                         actual_end = end_time
  4165.                                     else:
  4166.                                         actual_end = out_punch1
  4167.                                     #print "actual_end.............",actual_end
  4168.     #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  4169.                                     #if category == 'staff':
  4170.                                     if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=32)):
  4171.                                         hard_absent =True
  4172.                                         #print "setting absent1...hard_absent =True......."
  4173.                                         working_main='A'
  4174.                                     elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2,minutes=1)):
  4175.                                         #print "setting hd1..........."
  4176.                                         working_main='HD'
  4177.                                     elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  4178.                                        
  4179.                                         print "setting P1 and late..859.........12",in_punch
  4180.                                        # working_main ='P'
  4181.                                         late=True
  4182.                                         late_count = late_count+1
  4183.                                        
  4184.                                         holiday_flag_1=False
  4185.                                         if dict['holiday_id']:
  4186.                                                 for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  4187.                                                     if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  4188.                                                         holiday_flag_1 = True
  4189.                                         if late_count>=3 and category=='staff' and holiday_flag_1==False:
  4190.                                             working_main ='HD'
  4191.                                            
  4192.                                            
  4193.                                             punch_in_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  4194.                                             punch_in_hd= punch_in_hd.strftime("%Y-%m-%d 04:00:30")
  4195.                                             punch_out_hd = datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S')
  4196.                                             punch_out_hd = punch_out_hd.strftime("%Y-%m-%d 08:10:30")
  4197.                                            
  4198.                                            
  4199.                                            
  4200.                                            
  4201.                                            
  4202.                                            
  4203.                                             leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  4204.                                             if leave_obj1:
  4205.                                                
  4206.                                                 self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  4207.                                                 self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  4208.                                                 self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  4209.                                                 leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  4210.                                            
  4211.                                             cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  4212.                                             ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  4213.                                             el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  4214.                                            
  4215.                
  4216.                                             if cl_status[5]['remaining_leaves']>=0.5:
  4217.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  4218.                                                 print "Hiii",create_id
  4219.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  4220.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  4221.                                             elif ml_status[2]['remaining_leaves']>=0.5:
  4222.                                                 print "Ml"
  4223.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  4224.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  4225.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  4226.                                             elif el_status[6]['remaining_leaves']>=0.5:
  4227.                                                 print "El"
  4228.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  4229.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  4230.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  4231.                                             else:
  4232.                                                 create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':punch_in_hd,'date_to':punch_out_hd,'number_of_days_temp':0.5,'day_temp':0.5,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  4233.                                                 a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  4234.                                                 unpaid_flag = True
  4235.                                                 print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  4236.                                         else:
  4237.                                             working_main = 'P'
  4238.                                     else:
  4239.                                         #print "setting P1..........."
  4240.                                         working_main ='P'
  4241.                                     if not in_punch and not out_punch1:
  4242.                                         working_main = "A"
  4243.                                         #continue
  4244.                                     #print "working_main..........",working_main
  4245.             # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  4246.                                     #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  4247.                                     #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  4248.                                     #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  4249.                                     hard_hd = short_leave = False
  4250.                                     if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  4251.                                         if late_count>=3 and category=='staff':
  4252.                                                                      
  4253.                                             working_main = 'HD'
  4254.                                            
  4255.                                         else:
  4256.                                             #print "hard hd true....."
  4257.                                             working_main = 'HD'
  4258.                                             hard_hd = True
  4259.                                         #print "hard_hd......setting...",hard_hd
  4260.                                     elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str((datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(hours=2))),'%Y-%m-%d %H:%M:%S') )) and \
  4261.                                       (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=5))):
  4262.                                         #print "short leave  true....."
  4263.                                         short_leave = True
  4264.                                         short_leave_late = short_leave_late + 1
  4265.             # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  4266.     #                                missing = new_hrs_miss = 0.0
  4267.     #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  4268.     #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  4269.     #                                    missing = value.total_seconds()
  4270.     #                                    #print "missing................",missing
  4271.     #                                    miss_min = float(missing / 60)
  4272.     #                                    print "miss_min............",miss_min
  4273.     #                                    miss_hr = float(miss_min / 60)
  4274.     #                                    #print "miss_hr.................",miss_hr
  4275.     #                                    miss_hr = divmod(miss_hr,1)[0]
  4276.     #                                    #print "miss_hr.................",miss_hr
  4277.     #                                    if miss_hr:
  4278.     #                                        #print "miss_hr...............",miss_hr
  4279.     #                                        nw_min = miss_min - miss_hr * 60
  4280.     #                                        #print "nw_min...1............",nw_min
  4281.     #                                    else:
  4282.     #                                        nw_min = miss_min
  4283.     #                                        #print "nw_min......2.........",nw_min
  4284.     #                                    nw_min = round(nw_min,0)
  4285.     #                                    if nw_min > 5 and nw_min <= 35:
  4286.     #                                        nm_min = 0.5
  4287.     #                                    elif nw_min > 35:
  4288.     #                                        miss_hr = miss_hr + 1
  4289.     #                                        nm_min = 0.0
  4290.     #                                    else:
  4291.     #                                        nm_min = 0.0
  4292.     #                                    print "miss_hr............",miss_hr
  4293.     #                                    print "nm_min..............",nm_min
  4294.     #                                    new_hrs_miss = miss_hr + nm_min
  4295.     #                                    print "new_hrs_miss................",new_hrs_miss  
  4296.             #                        if new_hrs_miss < 4.0:
  4297.             #                            working_main ="A"
  4298.             #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  4299.             #                            working_main ="HD"
  4300.             #                        elif new_hrs_miss >= 6.0 :
  4301.             #                            working_main ="A"
  4302.             #                        print "working_main............",working_main
  4303.                                         # ++++ START TIME & END TIME +++++++++++++++++++
  4304.                                     st= ed= False
  4305.                                     if in_punch:
  4306.                                         st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  4307.                                     if out_punch1:
  4308.                                         ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  4309.             #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  4310.                                     #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  4311.                                     #print "ed.................",ed
  4312.                                     #if category == 'staff':
  4313.                                     if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  4314.                                      ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  4315.                                         lunch_mid =True
  4316.         #                            if category != 'staff':
  4317.         #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  4318.         #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  4319.         #                                    lunch_mid =True
  4320.                                     #print "lunch_mid...........",lunch_mid
  4321.             # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  4322.                                     new_hrs=0.0
  4323.                                     ot_hr = 0
  4324.                                     ot_min = 0
  4325.                                     if st and ed:
  4326.             # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  4327.            
  4328.                                        
  4329.                                         working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  4330.                                         punch_min = float(working_hour.total_seconds() / 60)
  4331.                                        
  4332.                                         if category == 'worker' and category=='contractor':
  4333.                                             if lunch_mid:
  4334.                                                 punch_min = punch_min - 30.0
  4335.                                            
  4336.                                         punch_hr = float(punch_min / 60)
  4337.                                         punch_hr = divmod(punch_hr,1)[0]
  4338.                                         if punch_hr:
  4339.                                             nw_min = punch_min - punch_hr * 60
  4340.                                         else:
  4341.                                             nw_min = punch_min
  4342.                                        
  4343.                                         if nw_min > 24 and nw_min < 55:
  4344.                                             nm_min = 0.5
  4345.                                         elif nw_min >= 55:
  4346.                                             punch_hr = punch_hr + 1
  4347.                                             nm_min = 0.0
  4348.                                         else:
  4349.                                             nm_min = 0.0
  4350.                                         new_hrs = punch_hr + nm_min
  4351.                                         #print "new_hrs..........",new_hrs,type(new_hrs)
  4352.             # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  4353.                                         over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  4354.                                         #print "over_time.............",over_time
  4355.                                         punch_min1 = float(over_time.total_seconds() / 60)
  4356.                                        
  4357.                                         punch_hr1 = float(punch_min1 / 60)
  4358.                                         punch_hr1 = divmod(punch_hr1,1)[0]
  4359.                                         if punch_hr1:
  4360.                                             nw_min1 = punch_min1 - punch_hr1 * 60
  4361.                                         else:
  4362.                                             nw_min1 = punch_min1
  4363.                                        
  4364.                                         if nw_min1 > 25 and nw_min1 < 55:
  4365.                                             nm_min1 = 0.5
  4366.                                         elif nw_min1 >= 55:
  4367.                                             punch_hr1 = punch_hr1 + 1
  4368.                                             nm_min1 = 0.0
  4369.                                         else:
  4370.                                             nm_min1 = 0.0
  4371.                                         new_hrs1 = punch_hr1 + nm_min1
  4372.                                         #print "actual ot new_hrs1.........",new_hrs1
  4373.                                         ot=round(new_hrs1/2,2)
  4374.                                         #print "shown  ot.............",ot
  4375.                                         extra_ot = 0.0
  4376.                                         if ot > 2.0:
  4377.                                             extra_ot= float(float(ot -2.0))
  4378.                                             #print "ot setting to 2.0...extra_ot...........",extra_ot
  4379.                                             ot= 2.0
  4380.                                         # if ot > 2.0 and ot_sum_gaurav<16:
  4381.                                         #     extra_ot= float(float(ot -2.0))
  4382.                                         #     #print "ot setting to 2.0...extra_ot...........",extra_ot
  4383.                                         #     ot= 2.0
  4384.                                         # if ot_sum_gaurav>15.5:
  4385.                                         #     extra_ot= float(float(ot))
  4386.                                         #     ot = 0.0
  4387.                                             #print "ot setting to 2.0...extra_ot...........",extra_ot
  4388.                                        
  4389.                                         ot_sum_gaurav = ot_sum_gaurav+ot
  4390.     # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  4391.                                         val1=self.float_time_convert_main(ot)
  4392.                                         hr=0
  4393.                                         #print "val1...............",val1
  4394.                                         time1 = 0.0
  4395.                                         if val1 and len(str(val1[1])) == 1:
  4396.                                             hr = 0
  4397.                                             ot_hr = val1[0]
  4398.                                             ot_min = hr
  4399.                                             time1 = str(val1[0]) +'.'+str(hr) + '0'
  4400.                                        
  4401.                                         if val1 and len(str(val1[1])) == 2:
  4402.                                             if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  4403.                                                 hr = 0
  4404.                                             else:
  4405.                                                 hr = 5
  4406.                                             ot_hr = val1[0]
  4407.                                             ot_min = hr
  4408.                                             time1 = str(val1[0]) +'.'+str(hr) + '0'
  4409.                                         ot = time1
  4410.                                         #print "new ot...........",ot
  4411.                                         val2=self.float_time_convert_main(extra_ot)
  4412.                                         hr1=0
  4413.                                         #print "val2...............",val2
  4414.                                         time2 = 0.0
  4415.                                         if val2 and len(str(val2[1])) == 1:
  4416.                                             hr1 = 0
  4417.                                             time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4418.                                        
  4419.                                         if val2 and len(str(val2[1])) == 2:
  4420.                                             if int(val2[1]) >=0 and int(val2[1]) <= 24:
  4421.                                                 hr1 = 0
  4422.             #                                elif val2[1] == 50:
  4423.             #                                    hr1 = 5
  4424.                                             else:
  4425.                                                  hr1 = 5
  4426.                                             time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4427.                                         extra_ot = time2
  4428.                                         #print "new extra_ot..............",extra_ot
  4429.             #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  4430.                                    
  4431.                                    
  4432.                                     if category == 'worker' or category == 'contractor' :
  4433.                                         if working_main == 'HD' or working_main == 'P':
  4434.                                         #print "going in.........."
  4435.                                             if new_hrs < 4.0:
  4436.                                                 working_main = 'A'
  4437.                                                 #print "working hours are less than 4.0.....",working_main
  4438.                                             elif new_hrs >= 4.0 and new_hrs < 7.5:
  4439.                                                 working_main = 'HD'
  4440.                                                 #late = False
  4441.                                                 #print "working setting HD....",working_main
  4442.                                             elif new_hrs >= 7.5 :
  4443.                                                 working_main = 'P'
  4444.                                            
  4445.                                                                          
  4446.                                             #print "working setting P....",working_main
  4447.                                     if working_main == 'HD' and working_main == 'A':
  4448.                                        working_main = 'A'
  4449.                                    
  4450.                                    
  4451.                                     if category == 'staff':
  4452.                                         if new_hrs <= 3.0:
  4453.                                             working_main = 'A'
  4454.                                     # print "===================================",working_main
  4455.                                     # error
  4456.                                     if short_leave_late >= 2 and short_leave == True and category == 'staff':
  4457.                                         second_short = True
  4458.                                         working_main = 'HD'
  4459.                                         working_main_second_half = 'A'
  4460.                                         working_main_first_half ='P'
  4461.                                    
  4462.                                     if late_count >= 3 and late==True and category == 'staff' and (working_main=='HD' or working_main=='P'):
  4463.                                         third_late = True
  4464.                                         if unpaid_flag == True:
  4465.                                             working_main = 'HD'
  4466.                                             working_main_first_half = 'A'
  4467.                                             working_main_second_half = 'P'
  4468.                                         else:
  4469.                                             working_main = 'P'
  4470.                                             working_main_first_half = 'L'
  4471.                                             working_main_second_half = 'P'
  4472.                                            
  4473.                                        
  4474.                                    
  4475.                                          
  4476.    
  4477.                                    
  4478.                                     if working_main == 'P' and hard_hd:
  4479.                                         working_main = 'HD'
  4480.         #                            if working_main == 'HD' and hard_absent:
  4481.         #                                working_main = 'A'
  4482.                                     #print "working main....",working_main
  4483.                                     if new_hrs1 < 0:
  4484.                                         new_hrs1 = 0
  4485.  
  4486.                                     in_punch_sun = False
  4487.                                     out_punch_sun = False
  4488.                                     if working_main == 'A':
  4489.                                         short_leave = False
  4490.                                         late = False
  4491.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4492.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  4493.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  4494.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  4495.                                         leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  4496.                                         leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  4497.                                         time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  4498.                                         time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  4499.                                         # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  4500.                                         # print "time", time_from
  4501.                            
  4502.                                         cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  4503.                                         leave_search = cr.fetchall()
  4504.                                        
  4505.                                         # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  4506.                                         # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  4507.                                         if leave_search:
  4508.                                             leave_qty = leave_search[0][1]
  4509.                                        
  4510.                                         if leave_search and leave_qty == 0.5:
  4511.                                             working_main_first_half = 'A'
  4512.                                             working_main_second_half= 'L'
  4513.                                             working_main = 'HD'
  4514.                                             in_punch_sun = True
  4515.                                             out_punch_sun = True
  4516.                                         elif leave_search and leave_qty > 0.5:
  4517.                                             working_main_first_half = 'L'
  4518.                                             working_main_second_half= 'L'
  4519.                                             working_main = 'L'
  4520.                                             in_punch_sun = True
  4521.                                             out_punch_sun = True
  4522.                                         else:
  4523.                                             working_main_first_half = 'A'
  4524.                                             working_main_second_half= 'A'
  4525.                                             working_main = 'A'
  4526.                                    
  4527.                                     if working_main == 'P' and third_late==False:
  4528.                                         working_main_first_half = 'P'
  4529.                                         working_main_second_half= 'P'
  4530.                                        
  4531.                                        
  4532.                                     if working_main == 'HD' and third_late==False:
  4533.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4534.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  4535.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  4536.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  4537.                                         leave_date = leave_date + timedelta(hours=03,minutes=00)
  4538.                                         leave_date1 = leave_date + timedelta(hours=23,minutes=00)
  4539.                                         leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  4540.                                         print "time", leave_date
  4541.                                         leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','>=',str(leave_date)),('date_to','<=',str(leave_date1)),('holiday_status_id','!=',9),('employee_id','=',employee_id.id)])
  4542.                                         print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  4543.                                        
  4544.                                         if leave_list:
  4545.                                             leave_qty = self.pool.get('hr.holidays').browse(cr,uid,leave_list[0]).number_of_days_temp
  4546.                                        
  4547.                                        
  4548.                                        
  4549.                                            
  4550.                                         in_punch_time_in = datetime.strptime(in_punch, DATETIME_FORMAT)
  4551.                                         out_punch_time_out = datetime.strptime(out_punch1, DATETIME_FORMAT)
  4552.                                         time_from = datetime.strptime(str(in_punch_time_in),'%Y-%m-%d %H:%M:%S').timetuple()
  4553.                                         time_to = datetime.strptime(str(out_punch_time_out),'%Y-%m-%d %H:%M:%S').timetuple()
  4554.                                         if (time_from.tm_hour >= 8 and time_from.tm_hour<=17) and (time_to.tm_hour<=17):
  4555.                                             if leave_list and leave_qty == 0.5:
  4556.                                                 working_main = 'P'  
  4557.                                                 working_main_first_half = 'P'
  4558.                                                 working_main_second_half= 'L'
  4559.                                             else:
  4560.                                                 working_main_first_half= 'P'
  4561.                                                 working_main_second_half= 'A'
  4562.                                         elif (time_from.tm_hour >= 10 and time_from.tm_hour<=18) and (time_to.tm_hour<=23):
  4563.                                             if leave_list and leave_qty == 0.5:
  4564.                                                 working_main = 'P'
  4565.                                                 working_main_second_half = 'P'
  4566.                                                 working_main_first_half = 'L'
  4567.                                             else:
  4568.                                                 working_main_first_half = 'A'
  4569.                                                 working_main_second_half = 'P'
  4570.                                        
  4571.                                    
  4572.                                        
  4573.                                    
  4574.                                    
  4575.                                    
  4576.     #                                print "end_time.............",end_time,type(end_time)
  4577.     #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  4578.                                     if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  4579.                                         out_punch2 = out_punch1
  4580.                                     else:
  4581.                                         random_second = random.randrange(1, 59, 1)
  4582.                                         random_min = random.randrange(1, 10, 1)
  4583.                                         if ot_min >= 5 :
  4584.                                             random_min +=  30
  4585.                                         else:
  4586.                                             random_min +=  0
  4587.                                         #print "random_min..........",random_min
  4588.                                         out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  4589.                                         #print "out_punch2.............",str(out_punch2)
  4590.                                         #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  4591.     #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  4592.     #                                print "out_punch2..........",str(out_punch2)
  4593.                                     in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  4594.                                     out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  4595.                                     out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  4596.                                     #print "ot hr ,ot min......",ot_hr, ot_min
  4597.                                    
  4598.                                     if (category == 'worker' or category == 'contractor') and (working_main == 'A' or working_main=='L'):
  4599.                                         extra_ot = 0.0
  4600.                                         ot = 0.0
  4601.                                         new_hrs1 = 0.0
  4602.                                        
  4603.                                        
  4604.                                     if category == 'staff':
  4605.                                         extra_ot = 0.0
  4606.                                         ot = 0.0
  4607.                                     if category != 'staff':
  4608.                                         short_leave = False
  4609.                                         late = False
  4610.             # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  4611.                                     holiday_flag = False
  4612.                                    
  4613.                                     try:
  4614.                                         cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  4615.                                         #print "deleted ...punch card .............",cr.fetchall()
  4616.                                         #print "going................."
  4617.                                         #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  4618.                                         #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  4619.                                        
  4620.                                         if dict['holiday_id']:
  4621.                                             for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  4622.                                                 if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  4623.                                                     holiday_flag = True
  4624.                                         if holiday_flag == True and category == 'staff':
  4625.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4626.                                             start_date = salary_date
  4627.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  4628.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  4629.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  4630.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  4631.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  4632.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4633.                                            
  4634.                                             full_day = cr.fetchall()
  4635.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4636.                                            
  4637.                                             half_day = cr.fetchall()
  4638.                                            
  4639.                                    
  4640.                                            
  4641.                                            
  4642.                                            
  4643.                                             if week_off_first == True:
  4644.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  4645.                                                 leave_first = cr.fetchall()
  4646.                                                 week_off_first = False
  4647.                                            
  4648.                                             if leave_first:
  4649.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  4650.                                                 leave_first =False
  4651.                                             else:    
  4652.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  4653.                                            
  4654.                                            
  4655.                                            
  4656.                                             if total_day_week >=3:
  4657.                                                 working_main ='WO'
  4658.                                                 working_main_first_half = 'WO'
  4659.                                                 working_main_second_half = 'WO'
  4660.                                                 short_leave = False
  4661.                                                 late = False
  4662.                                                 late_count = late_count-1
  4663.                                                 in_punch_sun = True
  4664.                                                 out_punch_sun = True
  4665.                                             else:
  4666.                                                 working_main ='S'
  4667.                                                 working_main_first_half ='S'
  4668.                                                 working_main_second_half = 'S'
  4669.                                                 short_leave = False
  4670.                                                 late = False
  4671.                                                 late_count = late_count-1
  4672.                                                 in_punch_sun = True
  4673.                                                 out_punch_sun = True
  4674.                                    
  4675.                                        
  4676.                                         if holiday_flag == True and (category =='worker' or category=='contractor'):
  4677.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4678.                                             start_date = salary_date
  4679.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  4680.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  4681.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  4682.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  4683.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  4684.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working='L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4685.                                            
  4686.                                             full_day = cr.fetchall()
  4687.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4688.                                            
  4689.                                             half_day = cr.fetchall()
  4690.                                            
  4691.                                            
  4692.                                            
  4693.                                            
  4694.                                            
  4695.                                             if week_off_first == True:
  4696.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  4697.                                                 leave_first = cr.fetchall()
  4698.                                                 week_off_first = False
  4699.                                            
  4700.                                             if leave_first:
  4701.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  4702.                                                 leave_first =False
  4703.                                             else:    
  4704.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  4705.                                            
  4706.  
  4707. #                                             print "======================================",start_date,end_date,total_day_week,full_day[0][0],half_day[0][0]
  4708.                                             if total_day_week >=3:
  4709.                                                 working_main ='WO'
  4710.                                                 working_main_first_half ='WO'
  4711.                                                 working_main_second_half = 'WO'
  4712.        
  4713.                                                 over_time=datetime.strptime(str( datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") + timedelta(hours=5,minutes=30)),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_start),"%Y-%m-%d %H:%M:%S")
  4714.                                                 print "over_time.............",over_time,out_punch1,actual_start
  4715.                                                 punch_min1 = float(over_time.total_seconds() / 60)
  4716.                                                
  4717.                                                 punch_hr1 = float(punch_min1 / 60)
  4718.                                                 punch_hr1 = divmod(punch_hr1,1)[0]
  4719.                                                 if punch_hr1:
  4720.                                                     nw_min1 = punch_min1 - punch_hr1 * 60
  4721.                                                 else:
  4722.                                                     nw_min1 = punch_min1
  4723.                                                
  4724.                                                 if nw_min1 > 25 and nw_min1 < 55:
  4725.                                                     nm_min1 = 0.5
  4726.                                                 elif nw_min1 >= 55:
  4727.                                                     punch_hr1 = punch_hr1 + 1
  4728.                                                     nm_min1 = 0.0
  4729.                                                 else:
  4730.                                                     nm_min1 = 0.0
  4731.                                                 new_hrs1 = punch_hr1 + nm_min1
  4732.                                                 print "actual ot new_hrs1.........",new_hrs1
  4733.                                                 # ot=round(new_hrs1/2,2)
  4734.                                                 #print "shown  ot.............",ot
  4735.                                                 extra_ot= float(float(new_hrs1))
  4736.                                                
  4737.                                                 extra_ot = round(extra_ot/2,2)
  4738.                                                 val2=self.float_time_convert_main(extra_ot)
  4739.                                                 hr1=0
  4740.                                                 #print "val2...............",val2
  4741.                                                 time2 = 0.0
  4742.                                                 if val2 and len(str(val2[1])) == 1:
  4743.                                                     hr1 = 0
  4744.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4745.                                                
  4746.                                                 if val2 and len(str(val2[1])) == 2:
  4747.                                                     if int(val2[1]) >=0 and int(val2[1]) <= 24:
  4748.                                                         hr1 = 0
  4749.                     #                                elif val2[1] == 50:
  4750.                     #                                    hr1 = 5
  4751.                                                     else:
  4752.                                                          hr1 = 5
  4753.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4754.                                                 extra_ot = time2
  4755.                                                 ot = 0.0
  4756.                                                 new_hrs = 0.0
  4757.                                                 in_punch_sun = True
  4758.                                                 out_punch_sun = True
  4759.                                             else:
  4760.                                                 working_main ='S'
  4761.                                                 working_main_first_half ='S'
  4762.                                                 working_main_second_half = 'S'
  4763.                
  4764.  
  4765.                                                 over_time=datetime.strptime(str( datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") + timedelta(hours=5,minutes=30)),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_start),"%Y-%m-%d %H:%M:%S")
  4766.                                                 print "over_time.............",over_time,out_punch1,actual_start
  4767.                                                 punch_min1 = float(over_time.total_seconds() / 60)
  4768.                                                
  4769.                                                 punch_hr1 = float(punch_min1 / 60)
  4770.                                                 punch_hr1 = divmod(punch_hr1,1)[0]
  4771.                                                 if punch_hr1:
  4772.                                                     nw_min1 = punch_min1 - punch_hr1 * 60
  4773.                                                 else:
  4774.                                                     nw_min1 = punch_min1
  4775.                                                
  4776.                                                 if nw_min1 > 25 and nw_min1 < 55:
  4777.                                                     nm_min1 = 0.5
  4778.                                                 elif nw_min1 >= 55:
  4779.                                                     punch_hr1 = punch_hr1 + 1
  4780.                                                     nm_min1 = 0.0
  4781.                                                 else:
  4782.                                                     nm_min1 = 0.0
  4783.                                                 new_hrs1 = punch_hr1 + nm_min1
  4784.                                                 print "actual ot new_hrs1.........",new_hrs1
  4785.                                                 # ot=round(new_hrs1/2,2)
  4786.                                                 #print "shown  ot.............",ot
  4787.                                                 extra_ot= float(float(new_hrs1))
  4788.                                                
  4789.                                                 extra_ot = round(extra_ot/2,2)
  4790.                                                 val2=self.float_time_convert_main(extra_ot)
  4791.                                                 hr1=0
  4792.                                                 #print "val2...............",val2
  4793.                                                 time2 = 0.0
  4794.                                                 if val2 and len(str(val2[1])) == 1:
  4795.                                                     hr1 = 0
  4796.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4797.                                                
  4798.                                                 if val2 and len(str(val2[1])) == 2:
  4799.                                                     if int(val2[1]) >=0 and int(val2[1]) <= 24:
  4800.                                                         hr1 = 0
  4801.                     #                                elif val2[1] == 50:
  4802.                     #                                    hr1 = 5
  4803.                                                     else:
  4804.                                                          hr1 = 5
  4805.                                                     time2 = str(val2[0]) +'.'+str(hr1) + '0'
  4806.                                                 extra_ot = time2
  4807.                                                 ot = 0.0
  4808.                                                 new_hrs = 0.0
  4809.  
  4810.                                                 in_punch_sun = True
  4811.                                                 out_punch_sun = True
  4812.                                                 print "Punch hr",punch_hr1,new_hrs1
  4813.                                         if in_punch_sun == True and out_punch_sun== True:
  4814.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  4815.                                                 'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch)})
  4816.                                         else:
  4817.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  4818.                                                 'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  4819.                                                 'out_punch2':str(out_punch2),'in_punch_custom':str(in_punch),'late':late,})
  4820.                                                              
  4821.                                         print "=========================NEW  PUNCH CREATED===========================",created_id
  4822.                                     except Exception,e:
  4823.                                         print "Exception Punch Card Creation...........", e.args
  4824.                                         pass
  4825.  
  4826.                             else:
  4827.                                 cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  4828.                                
  4829.                                 cr.execute(""" select  h_line.leave_date,h_line.national_holiday  from holiday_list_lines  as h_line left join holiday_list as h_list on (h_line.holiday_id = h_list.id)  
  4830.                                where  h_list.month ='"""+str(salary_month.month)+"""' and  h_list.year_id ='"""+str(salary_month.year_id.id)+"""' group by  h_line.leave_date,h_line.national_holiday
  4831.                                """)
  4832.                                
  4833.                                 temp1 = cr.fetchall()
  4834.                                 sunday_list = {}
  4835.                                 flag = False
  4836.                                 for each in temp1:
  4837.                                     sunday_list.update({each[0]:each[1]})
  4838.                    
  4839.                                 for key,val in sunday_list.items():
  4840.                                    
  4841.                                     if salary_date == key and val == False and joining_date <= salary_date:
  4842.                                         if termination_date:
  4843.                                             if termination_date >=salary_date:
  4844.                                                 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4845.                                                 start_date = salary_date
  4846.                                                 start_date = datetime.strptime(start_date,'%Y-%m-%d')
  4847.                                                 start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  4848.                                                 end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  4849.                                                 end_date = end_date - timedelta(hours=144,minutes=00)
  4850.                                                 end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  4851.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P' or working = 'L') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4852.                                                
  4853.                                                 full_day = cr.fetchall()
  4854.                                                 cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4855.                                                
  4856.                                                 half_day = cr.fetchall()
  4857.                                                
  4858.                                                
  4859.                                                
  4860.                                                
  4861.                                                
  4862.                                                 if week_off_first == True:
  4863.                                                     cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  4864.                                                     leave_first = cr.fetchall()
  4865.                                                     week_off_first = False
  4866.                                                
  4867.                                                 if leave_first:
  4868.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  4869.                                                     leave_first =False
  4870.                                                 else:    
  4871.                                                     total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  4872.                                                
  4873.    
  4874.                                                
  4875.                                                 if total_day_week >=3:
  4876.                                                     working_main = 'WO'
  4877.                                                     working_main_first_half = 'WO'
  4878.                                                     working_main_second_half = 'WO'
  4879.                                                    
  4880.                                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4881.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4882.                                                                                             })
  4883.                                                 else:
  4884.                                                     working_main = 'S'
  4885.                                                     working_main_first_half = 'S'
  4886.                                                     working_main_second_half = 'S'
  4887.                                                    
  4888.                                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4889.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4890.                                                                                             })
  4891.                                                
  4892.                                                    
  4893.  
  4894.                                         else:
  4895.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4896.                                             start_date = salary_date
  4897.                                             start_date = datetime.strptime(start_date,'%Y-%m-%d')
  4898.                                             start_date = datetime.strftime(start_date,'%Y-%m-%d %H:%M:%S')
  4899.                                             end_date = datetime.strptime(start_date, DATETIME_FORMAT)
  4900.                                             end_date = end_date - timedelta(hours=144,minutes=00)
  4901.                                             end_date = datetime.strftime(end_date,'%Y-%m-%d %H:%M:%S')
  4902.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='P'  or working='L' ) and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4903.                                            
  4904.                                             full_day = cr.fetchall()
  4905.                                             cr.execute("select count(*) from hr_punch_card where employee_id = '"+str(employee_id.id)+"' and (working='HD') and  in_punch between '"+str(end_date)+"' and '"+str(start_date)+"'")
  4906.                                            
  4907.                                             half_day = cr.fetchall()
  4908.                                            
  4909.                                            
  4910.                                            
  4911.                                            
  4912.                                            
  4913.                                            
  4914.                                            
  4915.                                             if week_off_first == True:
  4916.                                                 cr.execute("select number_of_days_temp from hr_holidays where date_from >= '"+str(end_date)+"' and date_to <= '"+str(start_date)+"'  and employee_id='"+str(employee_id.id)+"';")
  4917.                                                 leave_first = cr.fetchall()
  4918.                                                 week_off_first = False
  4919.                                            
  4920.                                             if leave_first:
  4921.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2) + leave_first[0][0]
  4922.                                                 leave_first =False
  4923.                                             else:    
  4924.                                                 total_day_week = int(full_day[0][0]) + float((half_day[0][0])/2)
  4925.                                            
  4926.                                            
  4927.                                            
  4928.    
  4929.                                            
  4930.                                             if total_day_week >=3:
  4931.                                                 working_main = 'WO'
  4932.                                                 working_main_first_half = 'WO'
  4933.                                                 working_main_second_half = 'WO'
  4934.                                                
  4935.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4936.                                                                                         'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4937.                                                                                         })
  4938.                                             else:
  4939.                                                 working_main = 'S'
  4940.                                                 working_main_first_half = 'S'
  4941.                                                 working_main_second_half = 'S'
  4942.                                                
  4943.                                                
  4944.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4945.                                                                                             'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4946.                                                                                             })
  4947.                                                
  4948.  
  4949.                                     elif salary_date == key and val == True and joining_date <= salary_date :
  4950.                                         if termination_date :
  4951.                                             if termination_date >= salary_date:
  4952.                                                 working_main = 'H'
  4953.                                                 working_main_first_half = 'H'
  4954.                                                 working_main_second_half = 'H'
  4955.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4956.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4957.                                                     })
  4958.                                         else:
  4959.                                             working_main = 'H'
  4960.                                             working_main_first_half = 'H'
  4961.                                             working_main_second_half = 'H'
  4962.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4963.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4964.                                                 })
  4965.                                                
  4966.                                 if salary_date not in sunday_list.keys() and joining_date <= salary_date :
  4967.                                    
  4968.                                     if termination_date:
  4969.                                         if termination_date >= salary_date:
  4970.                                             leave_qty = 0.0
  4971.                                             DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  4972.                                             leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  4973.                                             leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  4974.                                             leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  4975.                                             leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  4976.                                             leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  4977.                                             time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  4978.                                             time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  4979.                                             # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  4980.                                             # print "time", time_from
  4981.                                             print "select date_from,number_of_days_temp,date_to from hr_holidays where (date_from <= '"+time_to+"' and date_to >='"+time_from+"') and  employee_id = '"+str(employee_id.id)+"'"
  4982.                                             cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  4983.                                             leave_search = cr.fetchall()
  4984.                                             print "leave_search",leave_search
  4985.                                             # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  4986.                                             # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  4987.                                             if leave_search:
  4988.                                                 leave_qty = leave_search[0][1]
  4989.                                            
  4990.                                            
  4991.                                             if leave_qty > 0.5:
  4992.                                                 week_days +=1    
  4993.                                                 working_main = 'L'
  4994.                                                 working_main_first_half = 'L'
  4995.                                                 working_main_second_half = 'L'
  4996.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  4997.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  4998.                                                     })
  4999.                                             elif leave_qty == 0.5:
  5000.                                                 week_days +=1    
  5001.                                                 working_main = 'HD'
  5002.                                                 working_main_first_half = 'A'
  5003.                                                 working_main_second_half = 'P'
  5004.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  5005.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  5006.                                                     })
  5007.                                             else:
  5008.                                                 working_main = 'A'
  5009.                                                 working_main_first_half = 'A'
  5010.                                                 working_main_second_half = 'A'
  5011.                                                 created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  5012.                                                     'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  5013.                                                     })
  5014.                                     else:
  5015.                                         leave_qty = 0.0
  5016.                                         DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
  5017.                                         leave_date = datetime.strptime(salary_date,'%Y-%m-%d')
  5018.                                         leave_date = datetime.strftime(leave_date,'%Y-%m-%d %H:%M:%S')
  5019.                                         leave_date = datetime.strptime(leave_date, DATETIME_FORMAT)
  5020.                                         leave_date11 = leave_date + timedelta(hours=23,minutes=50)
  5021.                                         leave_date1 = leave_date + timedelta(hours=01,minutes=50)
  5022.                                         time_from = datetime.strftime(leave_date11,'%Y-%m-%d %H:%M:%S')
  5023.                                         time_to = datetime.strftime(leave_date1,'%Y-%m-%d %H:%M:%S')
  5024.                                         # leave_date = datetime.strptime(str(leave_date), DATETIME_FORMAT)
  5025.                                         # print "time", time_from
  5026.                                         print "select date_from,number_of_days_temp,date_to from hr_holidays where (date_from <= '"+time_to+"' and date_to >='"+time_from+"') and  employee_id = '"+str(employee_id.id)+"'"
  5027.                                         cr.execute("select date_from,number_of_days_temp,date_to from hr_holidays where holiday_status_id in (5,6,2) and state='validate' and (date_from <= '"+time_from+"' and date_to >='"+time_to+"') and  employee_id = '"+str(employee_id.id)+"' ")
  5028.                                         leave_search = cr.fetchall()
  5029.                                         print "leave_search",leave_search
  5030.                                         # leave_list = self.pool.get('hr.holidays').search(cr,uid,[('date_from','<=',str(leave_date)),('date_to','>=',str(leave_date)),('employee_id','=',employee_id.id)])
  5031.                                         # print "hiiiiiiiiiiiiiiiiiiiiiiiiiiiii",leave_list
  5032.                                         if leave_search:
  5033.                                             leave_qty = leave_search[0][1]
  5034.                                        
  5035.                                        
  5036.                                         if leave_qty  > 0.5:
  5037.                                             week_days +=1    
  5038.                                             working_main = 'L'
  5039.                                             working_main_first_half = 'L'
  5040.                                             working_main_second_half = 'L'
  5041.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  5042.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  5043.                                                 })
  5044.                                         elif leave_qty == 0.5:
  5045.                                             week_days +=1    
  5046.                                             working_main = 'HD'
  5047.                                             working_main_first_half = 'A'
  5048.                                             working_main_second_half = 'P'
  5049.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  5050.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  5051.                                                 })
  5052.                                         else:
  5053.                                             working_main = 'A'
  5054.                                             working_main_first_half = 'A'
  5055.                                             working_main_second_half = 'A'
  5056.                                             created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'first_half':working_main_first_half,'second_half':working_main_second_half,
  5057.                                                 'method':'auto','in_punch':str(salary_date),'method':'auto',#'in_time':in_time,'out_time':out_time,
  5058.                                                 })
  5059.                                        
  5060.                                        
  5061.                                        
  5062.                                 print "IN and Out Pair attendance not found for emp on date......",employee_id,salary_date
  5063.                            
  5064.                    
  5065.                     next_date +=  timedelta(days=1)
  5066. #             eror
  5067. #             print "emp_list..............",emp_list  
  5068.            
  5069. #             month1=salary_month.month
  5070. #             print "month......... ",month1
  5071.                
  5072. #             year=salary_month.year_id.name  
  5073. #             print "year............",year
  5074. #             if int(month1) in [1,3,5,7,8,10,12]:
  5075. #                 month = 31
  5076. #             if int(month1) in [4,6,9,11]:
  5077. #                 month = 30
  5078. #             if int(month1) in [2]:
  5079. #                 if int(year) % 4 == 0:
  5080. #                     month = 29
  5081. #                 else:
  5082. #                     month = 28
  5083. #             day_from = str(year) + '-' + str(month1) + '-' + '01'
  5084. #             # day_from = datetime.strptime(day_from,"%Y-%m-%d")
  5085. #             print "day_from..............",day_from
  5086. #             day_to = str(year) + '-' + str(month1) + '-' + str(month)
  5087. #             day_to = datetime.strptime(day_to,"%Y-%m-%d")
  5088. #                 #print "day_to.....................",day_to
  5089. #             day_from = datetime.strptime(day_from,"%Y-%m-%d")
  5090. #             next_date = day_from
  5091.            
  5092.            
  5093. #             for day in range(0, month):
  5094. #                 #print "next_date.............",str(next_date)
  5095. #                 salary_date =  next_date.strftime('%Y-%m-%d')
  5096. #                 if emp_list:
  5097. #                     for emp in emp_list:
  5098. #                         late_count = 0;
  5099. #                         ot_sum_gaurav = 0
  5100. #                         employee_id=emp_obj.browse(cr ,uid ,emp )
  5101. #                         contract_ids = cont_obj.search(cr,uid,[('employee_id','=',employee_id.id)])
  5102.                        
  5103. #                         if contract_ids:
  5104. #                             contract_data = cont_obj.browse(cr, uid, contract_ids)[0]
  5105. #                         else:
  5106. #                             raise osv.except_osv(_('Warning !'),_("Contract is missing for employee %s." % (employee_id.name)))
  5107.                        
  5108. #                         category = employee_id.category
  5109. #                         timing_main= self.timing_main_func( cr ,uid ,ids ,contract_data.working_hours ,salary_date)
  5110. #                         #print "timing_main...........",timing_main
  5111. #                         total_punch = "select min(name + interval'5 hour 30 minutes') ,max(name + interval'5 hour 30 minutes') from hr_attendance where name + interval '5 hours 30 minute' >= to_timestamp('"+str(timing_main['start_time'])+"',\
  5112. #                         'YYYY-MM-DD HH24:MI:SS')::timestamp - interval '1 hours 30 minute' and name + interval '5 hours 30 minute' < to_timestamp('"+str(timing_main['final_time'])+"','YYYY-MM-DD HH24:MI:SS')::timestamp and employee_id = \
  5113. #                         '"+str(employee_id.id)+"' "
  5114. #                         cr.execute(total_punch)
  5115. #                         total_punch_result = cr.fetchall()
  5116.                        
  5117. #                         for data in total_punch_result:  
  5118. #                             if len(data)>1 and data[0] != None and data[1] != None and data[0] != data[1]:
  5119. #                                 #if total_punch_result and len(total_punch_result[0]) == 2 and total_punch_result[0][0] != None and total_punch_result[0][1] != None and total_punch_result[0][0] != total_punch_result[0][1]:
  5120. #                                 in_punch = data[0]
  5121. #                                 #print "in_punch....................",in_punch
  5122. #                                 out_punch1 = data[1]
  5123. #                                 #print "out_punch1..................",out_punch1
  5124. #                                 #category =employee_id.category
  5125. #                                 in_time = float(str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  5126. #                                 #print "in_time.........",in_time
  5127. #                                 out_time = float(str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S").strftime('%H.%M')))
  5128. #                                 #print "out_time.........",out_time
  5129. #                                 dict=self.get_month_year(cr ,uid ,ids ,salary_date)
  5130.                                
  5131. #                                 timing = self.calculate_time(cr, uid, ids, salary_date, in_time or 0.0, out_time or 0.0)
  5132. #                                 #print "timing.................",timing
  5133. #                                 lunch_mid = late = hard_absent = short_leave = hard_hd = False
  5134. #                                 start = datetime.strptime(str(timing_main['start_time']),"%Y-%m-%d %H:%M:%S")
  5135. #                                 time1600 = start.replace(hour=16, minute=00, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  5136. #                                 working_main= False
  5137. #                                 lunch = datetime.strptime(str(timing['start_time']),"%Y-%m-%d %H:%M:%S")
  5138. #                                 time915 = lunch.replace(hour=9, minute=15, second= 00).strftime('%Y-%m-%d %H:%M:%S')
  5139. #                                 elunch = lunch + timedelta(hours=4,minutes=30)
  5140. # # +++++++++++++++   Finds Out the His Shift Timings and His Actual Working Time ++++++++++++++
  5141. #                                 shift = self.working_hours_on_day(cr, uid, contract_data.working_hours, salary_date, context)
  5142. #                                 #print "shift............",shift
  5143. #                                 start_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  5144. #                                 hour= int(float(shift['start']))
  5145. #                                 min = str(str(abs(float(shift['start'])) % 1).split('.')[1])
  5146. #                                 if min and len(min) == 1:
  5147. #                                     min = str(min) +'0'
  5148. #                                 if min and len(min) == 1:
  5149. #                                     min = str(min)
  5150. #                                 start_time = start_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  5151. #                                 #print "start_time..............",start_time,type(start_time)
  5152. #                                 end_time = datetime.strptime(datetime.strptime(salary_date,"%Y-%m-%d").strftime('%Y-%m-%d %H:%M:%S'),"%Y-%m-%d %H:%M:%S")
  5153. #                                 hour= int(float(shift['end']))
  5154. #                                 min = str(str(abs(float(shift['end'])) % 1).split('.')[1])
  5155. #                                 if min and len(min) == 1:
  5156. #                                     min = str(min) +'0'
  5157. #                                 if min and len(min) == 1:
  5158. #                                     min = str(min)
  5159. #                                 end_time = end_time.replace(hour=hour, minute=int(min), second= 00).strftime('%Y-%m-%d %H:%M:%S')
  5160. #                                 #print "end_time................",end_time
  5161. #                                 if datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S") < datetime.strptime(start_time,"%Y-%m-%d %H:%M:%S"):
  5162. #                                     actual_start = start_time
  5163. #                                 else:
  5164. #                                     actual_start = in_punch
  5165. #                                 #print "actual_start............",actual_start
  5166. #                                 if datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S") > datetime.strptime(end_time,"%Y-%m-%d %H:%M:%S"):
  5167. #                                     actual_end = end_time
  5168. #                                 else:
  5169. #                                     actual_end = out_punch1
  5170. #                                 #print "actual_end.............",actual_end
  5171. # #  +++++++++++++++++++++ TO KNOW P , HD , A OR LATE +++++++++++++++++++++++++++++++++
  5172. #                                 #if category == 'staff':
  5173. #                                 if datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') >( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4, minutes=30)):
  5174. #                                     hard_absent =True
  5175. #                                     #print "setting absent1...hard_absent =True......."
  5176. #                                     working_main='A'
  5177. #                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)):
  5178. #                                     #print "setting hd1..........."
  5179. #                                     working_main='HD'
  5180. #                                 elif datetime.strptime(str(in_punch),'%Y-%m-%d %H:%M:%S') > ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes=11)):
  5181. #                                     print "setting P1 and late....1604......."
  5182. #                                     working_main ='P'
  5183. #                                     late_count = late_count+1
  5184. #                                     print "+++++++++++++++++++++++++++++==================================",late_count
  5185. #                                     late=True
  5186. #                                     print "+++++++++++++++++++++++++++++==================================",late_count,in_punch,employee_id.id
  5187. #                                     if late_count>=3 and category=='staff':
  5188. #                                         working_main ='A'
  5189.                                        
  5190.                                        
  5191. #                                         leave_obj1 = self.pool.get('hr.holidays').search(cr,uid,[('employee_id','=',employee_id.id),('date_from','=',in_punch)],context=context)
  5192. #                                         if leave_obj1:
  5193.                                            
  5194. #                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  5195. #                                             self.pool.get('hr.holidays').holidays_refuse(cr,uid,leave_obj1)
  5196. #                                             self.pool.get('hr.holidays').write(cr,uid,leave_obj1,{'state':'confirm','approve_user_id':employee_id.parent_id.id },context=None)
  5197. #                                             leave_obj2 = self.pool.get('hr.holidays').unlink(cr,uid,leave_obj1)
  5198.                                        
  5199. #                                         cl_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [5], employee_id.id, False, context=context)
  5200. #                                         ml_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [2], employee_id.id, False, context=context)
  5201. #                                         el_status = self.pool.get('hr.holidays.status').get_days(cr, uid, [6], employee_id.id, False, context=context)
  5202.                                        
  5203.            
  5204. #                                         if cl_status[5]['remaining_leaves']>=1:
  5205. #                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':5,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':1,'day_temp':1,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  5206. #                                             print "Hiii",create_id
  5207. #                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  5208. #                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  5209. #                                         elif ml_status[2]['remaining_leaves']>=1:
  5210. #                                             print "Ml"
  5211. #                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':2,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':1,'day_temp':1,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  5212. #                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  5213. #                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  5214.                                        
  5215. #                                         elif el_status[6]['remaining_leaves']>=1:
  5216. #                                             print "El"
  5217. #                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':6,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':1,'day_temp':1,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  5218. #                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  5219. #                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a
  5220. #                                         else:
  5221. #                                             create_id = self.pool.get('hr.holidays').create(cr,uid,{'name':'Late Leave','employee_id':employee_id.id,'holiday_status_id':9,'date_from':in_punch,'date_to':out_punch1,'number_of_days_temp':1,'day_temp':1,'manager_id':employee_id.parent_id.id,'res_employee_id':1},context=None)
  5222. #                                             a = self.pool.get('hr.holidays').write(cr,uid,create_id,{'state':'validate','approve_user_id':employee_id.parent_id.id },context=None)
  5223. #                                             print "+++++++++++++++++++++++++++++=========================Leave Created=========",late_count,a    
  5224. #                                     else:
  5225. #                                         working_main = 'P'
  5226. #                                 else:
  5227. #                                     #print "setting P1..........."
  5228. #                                     working_main ='P'
  5229. #                                 if not in_punch and not out_punch1:
  5230. #                                     working_main = "A"
  5231. #                                     #continue
  5232. #                                 #print "working_main..........",working_main
  5233. #         # ++++++++++++++++++++++++++++++++ Check for Employee Short Leave (Goes Before Time) , SETS STRICT HD(hard_hd) +++++++++++++++
  5234. #                                 #print "(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )..",(datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )
  5235. #                                 #print "(datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')......",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  5236. #                                 #print "(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15)).....",(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))
  5237. #                                 hard_hd = short_leave = False
  5238. #                                 if datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') < (datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') ):#- timedelta(hours=5,minutes=30)
  5239. #                                     if late_count>=3 and category=='staff':
  5240. #                                         print "Late Count Reseted$$$$$$$$$$$$$$$$$$$$$$$",late_count                                    
  5241. #                                         working_main = 'A'
  5242. #                                     else:    
  5243. #                                         working_main = 'HD'
  5244. #                                         hard_hd = True
  5245. #                                         #print "hard_hd......setting...",hard_hd
  5246. #                                 elif (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') >= (datetime.strptime(str(time1600),'%Y-%m-%d %H:%M:%S') )) and \
  5247. #                                   (datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S') <= (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') - timedelta(minutes=15))):
  5248. #                                     #print "short leave  true....."
  5249. #                                     short_leave = True
  5250. #         # +++++++++++ Calculating Missing of Employee +++++++++++++++++++++++++++++++
  5251. # #                                missing = new_hrs_miss = 0.0
  5252. # #                                if datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  5253. # #                                    value = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") - datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  5254. # #                                    missing = value.total_seconds()
  5255. # #                                    #print "missing................",missing
  5256. # #                                    miss_min = float(missing / 60)
  5257. # #                                    print "miss_min............",miss_min
  5258. # #                                    miss_hr = float(miss_min / 60)
  5259. # #                                    #print "miss_hr.................",miss_hr
  5260. # #                                    miss_hr = divmod(miss_hr,1)[0]
  5261. # #                                    #print "miss_hr.................",miss_hr
  5262. # #                                    if miss_hr:
  5263. # #                                        #print "miss_hr...............",miss_hr
  5264. # #                                        nw_min = miss_min - miss_hr * 60
  5265. # #                                        #print "nw_min...1............",nw_min
  5266. # #                                    else:
  5267. # #                                        nw_min = miss_min
  5268. # #                                        #print "nw_min......2.........",nw_min
  5269. # #                                    nw_min = round(nw_min,0)
  5270. # #                                    if nw_min > 5 and nw_min <= 35:
  5271. # #                                        nm_min = 0.5
  5272. # #                                    elif nw_min > 35:
  5273. # #                                        miss_hr = miss_hr + 1
  5274. # #                                        nm_min = 0.0
  5275. # #                                    else:
  5276. # #                                        nm_min = 0.0
  5277. # #                                    print "miss_hr............",miss_hr
  5278. # #                                    print "nm_min..............",nm_min
  5279. # #                                    new_hrs_miss = miss_hr + nm_min
  5280. # #                                    print "new_hrs_miss................",new_hrs_miss  
  5281. #         #                        if new_hrs_miss < 4.0:
  5282. #         #                            working_main ="A"
  5283. #         #                        elif new_hrs_miss >=4.0 and new_hrs_miss < 6.0:
  5284. #         #                            working_main ="HD"
  5285. #         #                        elif new_hrs_miss >= 6.0 :
  5286. #         #                            working_main ="A"
  5287. #         #                        print "working_main............",working_main
  5288. #                                     # ++++ START TIME & END TIME +++++++++++++++++++
  5289. #                                 st= ed= False
  5290. #                                 if in_punch:
  5291. #                                     st=str(datetime.strptime(in_punch,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  5292. #                                 if out_punch1:
  5293. #                                     ed=str(datetime.strptime(out_punch1,"%Y-%m-%d %H:%M:%S"))# - timedelta(hours=5,minutes=30))
  5294. #         #+++++++++++++++++ Lunch Lies in between of work ++++++++++++++++++++++
  5295. #                                 #print "st..............",st,type(st) # st.strftime('%Y-%m-%d %H:%M:%S')
  5296. #                                 #print "ed.................",ed
  5297. #                                 #if category == 'staff':
  5298. #                                 if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  5299. #                                  ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  5300. #                                     lunch_mid =True
  5301. #     #                            if category != 'staff':
  5302. #     #                                if st and datetime.strptime(st,'%Y-%m-%d %H:%M:%S') <= ( datetime.strptime(str(start_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=3,minutes=30)) and \
  5303. #     #                                 ed and datetime.strptime(ed,'%Y-%m-%d %H:%M:%S') >= ( datetime.strptime(str(timing_main['start_time']),'%Y-%m-%d %H:%M:%S') + timedelta(hours=4)):
  5304. #     #                                    lunch_mid =True
  5305. #                                 #print "lunch_mid...........",lunch_mid
  5306. #         # ++++++++++++++++++++++ For Working Time Calculation ex 9:00 - 6:00 ++++++ not 8;50 - 6:34 +++
  5307. #                                 new_hrs=0.0
  5308. #                                 ot_hr = 0
  5309. #                                 ot_min = 0
  5310. #                                 if st and ed:
  5311. #         # +++++++++++++++++++ Calculate Working Hours +++++++++++++++++++++++++++++
  5312.        
  5313.                                    
  5314. #                                     working_hour=datetime.strptime(actual_end,"%Y-%m-%d %H:%M:%S") -datetime.strptime(actual_start,"%Y-%m-%d %H:%M:%S")
  5315. #                                     punch_min = float(working_hour.total_seconds() / 60)
  5316. #                                     if lunch_mid:
  5317. #                                         punch_min = punch_min - 30.0
  5318. #                                     punch_hr = float(punch_min / 60)
  5319. #                                     punch_hr = divmod(punch_hr,1)[0]
  5320. #                                     if punch_hr:
  5321. #                                         nw_min = punch_min - punch_hr * 60
  5322. #                                     else:
  5323. #                                         nw_min = punch_min
  5324.                                    
  5325. #                                     if nw_min > 24 and nw_min < 55:
  5326. #                                         nm_min = 0.5
  5327. #                                     elif nw_min >= 55:
  5328. #                                         punch_hr = punch_hr + 1
  5329. #                                         nm_min = 0.0
  5330. #                                     else:
  5331. #                                         nm_min = 0.0
  5332. #                                     new_hrs = punch_hr + nm_min
  5333. #                                     #print "new_hrs..........",new_hrs,type(new_hrs)
  5334. #         # +++++++++++++++++++ Over Time Calculation +++++++++++++++++++++++
  5335. #                                     over_time=datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") -datetime.strptime(str(actual_end),"%Y-%m-%d %H:%M:%S")
  5336. #                                     #print "over_time.............",over_time
  5337. #                                     punch_min1 = float(over_time.total_seconds() / 60)
  5338.                                    
  5339. #                                     punch_hr1 = float(punch_min1 / 60)
  5340. #                                     punch_hr1 = divmod(punch_hr1,1)[0]
  5341. #                                     if punch_hr1:
  5342. #                                         nw_min1 = punch_min1 - punch_hr1 * 60
  5343. #                                     else:
  5344. #                                         nw_min1 = punch_min1
  5345.                                    
  5346. #                                     if nw_min1 > 25 and nw_min1 < 55:
  5347. #                                         nm_min1 = 0.5
  5348. #                                     elif nw_min1 >= 55:
  5349. #                                         punch_hr1 = punch_hr1 + 1
  5350. #                                         nm_min1 = 0.0
  5351. #                                     else:
  5352. #                                         nm_min1 = 0.0
  5353. #                                     new_hrs1 = punch_hr1 + nm_min1
  5354. #                                     #print "actual ot new_hrs1.........",new_hrs1
  5355. #                                     ot=round(new_hrs1/2,2)
  5356. #                                     #print "shown  ot.............",ot
  5357. #                                     extra_ot = 0.0
  5358. #                                     if ot > 2.0:
  5359. #                                         extra_ot= float(float(ot -2.0))
  5360. #                                         #print "ot setting to 2.0...extra_ot...........",extra_ot
  5361. #                                         ot= 2.0
  5362. #                                     # if ot > 2.0 and ot_sum_gaurav<16:
  5363. #                                     #     extra_ot= float(float(ot -2.0))
  5364. #                                     #     #print "ot setting to 2.0...extra_ot...........",extra_ot
  5365. #                                     #     ot= 2.0
  5366. #                                     # if ot_sum_gaurav>16:
  5367. #                                     #     extra_ot= float(float(ot))
  5368. #                                     #     ot = 0.0
  5369. # # +++++++++++++++++++++++++++++ FORMATTING OT  (EX- 1.49 = 1.5) ++++++++++++++++++++++++++++++++++
  5370. #                                     val1=self.float_time_convert_main(ot)
  5371. #                                     hr=0
  5372. #                                     #print "val1...............",val1
  5373. #                                     time1 = 0.0
  5374. #                                     if val1 and len(str(val1[1])) == 1:
  5375. #                                         hr = 0
  5376. #                                         ot_hr = val1[0]
  5377. #                                         ot_min = hr
  5378. #                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  5379.                                    
  5380. #                                     if val1 and len(str(val1[1])) == 2:
  5381. #                                         if int(val1[1]) >= 0 and int(val1[1]) <= 24:
  5382. #                                             hr = 0
  5383. #                                         else:
  5384. #                                             hr = 5
  5385. #                                         ot_hr = val1[0]
  5386. #                                         ot_min = hr
  5387. #                                         time1 = str(val1[0]) +'.'+str(hr) + '0'
  5388. #                                     ot = time1
  5389. #                                     #print "new ot...........",ot
  5390. #                                     val2=self.float_time_convert_main(extra_ot)
  5391. #                                     hr1=0
  5392. #                                     #print "val2...............",val2
  5393. #                                     time2 = 0.0
  5394. #                                     if val2 and len(str(val2[1])) == 1:
  5395. #                                         hr1 = 0
  5396. #                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  5397.                                    
  5398. #                                     if val2 and len(str(val2[1])) == 2:
  5399. #                                         if int(val2[1]) >=0 and int(val2[1]) <= 24:
  5400. #                                             hr1 = 0
  5401. #         #                                elif val2[1] == 50:
  5402. #         #                                    hr1 = 5
  5403. #                                         else:
  5404. #                                              hr1 = 5
  5405. #                                         time2 = str(val2[0]) +'.'+str(hr1) + '0'
  5406. #                                     extra_ot = time2
  5407. #                                     #print "new extra_ot..............",extra_ot
  5408. #         #  +++++++++++++++++ Check for If Worked for Less Than 4 Hours ++++++++++++++++++++++=
  5409. #                                 print "working_main............",working_main
  5410. #                                 if working_main == 'HD' or working_main == 'P':
  5411. #                                     #print "going in.........."
  5412. #                                     if new_hrs < 4.0:
  5413. #                                         working_main = 'A'
  5414. #                                         #print "working hours are less than 4.0.....",working_main
  5415. #                                     elif new_hrs >= 4.0 and new_hrs < 6.0:
  5416. #                                         working_main = 'HD'
  5417. #                                         #late = False
  5418. #                                         #print "working setting HD....",working_main
  5419. #                                     elif new_hrs >= 6.0 :
  5420. #                                         working_main = 'P'
  5421. #                                         #print "working setting P....",working_main
  5422. #                                 print "hard_absent   ,hard_hd ..short_leave.. ",hard_absent ,hard_hd,short_leave
  5423. #                                 if working_main == 'P' and hard_hd:
  5424. #                                     working_main = 'HD'
  5425. #     #                            if working_main == 'HD' and hard_absent:
  5426. #     #                                working_main = 'A'
  5427. #                                 #print "working main....",working_main
  5428. #                                 if new_hrs1 < 0:
  5429. #                                     new_hrs1 = 0
  5430. #                                 if working_main == 'A':
  5431. #                                     short_leave = False
  5432. #                                     late = False
  5433. #                                 print "working main..working hour...ot.",working_main,new_hrs,new_hrs1
  5434. # #                                print "end_time.............",end_time,type(end_time)
  5435. # #                                print "datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S').........",datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S')
  5436. #                                 if (datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(minutes = 10)) > datetime.strptime(str(out_punch1),'%Y-%m-%d %H:%M:%S'):
  5437. #                                     out_punch2 = out_punch1
  5438. #                                 else:
  5439. #                                     random_second = random.randrange(1, 59, 1)
  5440. #                                     random_min = random.randrange(1, 10, 1)
  5441. #                                     if ot_min >= 5 :
  5442. #                                         random_min +=  30
  5443. #                                     else:
  5444. #                                         random_min +=  0
  5445. #                                     #print "random_min..........",random_min
  5446. #                                     out_punch2 = datetime.strptime(str(end_time),"%Y-%m-%d %H:%M:%S") + timedelta(hours=ot_hr,minutes = random_min,seconds=random_second) #- timedelta(hours=5,minutes=30)  
  5447. #                                     #print "out_punch2.............",str(out_punch2)
  5448. #                                     #out_punch2 = str(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  5449. # #                                print"(datetime.strptime(str(end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2)).........",str(datetime.strptime((end_time),'%Y-%m-%d %H:%M:%S') + timedelta(hours=2))
  5450. # #                                print "out_punch2..........",str(out_punch2)
  5451. #                                 in_punch = datetime.strptime(str(in_punch),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  5452. #                                 out_punch1 = datetime.strptime(str(out_punch1),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)
  5453. #                                 out_punch2 = datetime.strptime(str(out_punch2),"%Y-%m-%d %H:%M:%S") - timedelta(hours=5,minutes=30)  
  5454. #                                 #print "ot hr ,ot min......",ot_hr, ot_min
  5455.                                
  5456. #                                 if category == 'staff':
  5457. #                                     extra_ot = 0.0
  5458. #                                     ot = 0.0
  5459. #                                 if category != 'staff':
  5460. #                                     short_leave = False
  5461. #                                     late = False
  5462. #         # +++++++++ Creating New Punch Deleting Previous One +++++++++++++++++++++++++
  5463. #                                 holiday_flag = False
  5464. #                                 if late_count >=3:
  5465. #                                     late_count =0
  5466. #                                 try:
  5467. #                                     cr.execute("delete from hr_punch_card where day='"+str(salary_date)+"' and employee_id = '"+str(employee_id.id)+"' and method='auto'")
  5468. #                                     #print "deleted ...punch card .............",cr.fetchall()
  5469. #                                     #print "going................."
  5470. #                                     #ot_ids= ot_obj.search( cr ,uid ,[('month_id','=',dict['holiday_id'] and dict['holiday_id'][0]),('employee_id','=',employee_id.id)])
  5471. #                                     #punch_ids=punch_obj.search(cr ,uid ,[('month','=',dict['month']),('year_id','=',dict['year_id'] and dict['year_id'][0]),('employee_id','=',employee_id.id)])
  5472. #                                     if dict['holiday_id']:
  5473. #                                         for sun in holiday_obj.browse(cr ,uid ,dict['holiday_id'][0]).holiday_lines:
  5474. #                                             if datetime.strptime(salary_date,"%Y-%m-%d").date() == datetime.strptime(str(sun.leave_date),"%Y-%m-%d").date():
  5475. #                                                 holiday_flag = True
  5476. #                                     if holiday_flag :
  5477. #                                         working_main ='L'
  5478. #                                         short_leave = False
  5479. #                                         late = False
  5480. #                                     if holiday_flag and (category =='worker' or category=='contractor'):
  5481. #                                         print "ooooooooooooooooooooooootttttttttMy Block",
  5482. #                                         working_main ='L'
  5483.  
  5484. #                                         punch_min1 = float(working_hour.total_seconds() / 60)
  5485.                                    
  5486. #                                         # punch_hr1 = float(punch_min1 / 60)
  5487. #                                         # punch_hr1 = divmod(punch_hr1,1)[0]
  5488. #                                         punch_hr1 = float(punch_min1 / 60)
  5489. #                                         punch_hr1 = divmod(punch_hr1,1)[0]
  5490. #                                         if punch_hr1:
  5491. #                                             nw_min1 = punch_min1 - punch_hr1 * 60
  5492. #                                         else:
  5493. #                                             nw_min1 = punch_min1
  5494.                                        
  5495. #                                         if nw_min1 > 25 and nw_min1 < 55:
  5496. #                                             nm_min1 = 0.5
  5497. #                                         elif nw_min1 >= 55:
  5498. #                                             punch_hr1 = punch_hr1 + 1
  5499. #                                             nm_min1 = 0.0
  5500. #                                         else:
  5501. #                                             nm_min1 = 0.0
  5502. #                                         new_hrs1 = punch_hr1 + nm_min1
  5503. #                                         extra_ot = new_hrs1
  5504. #                                         new_hrs = 0
  5505. #                                         ot = 0
  5506. #                                         print "Punch hr",punch_hr1,new_hrs1
  5507. #                                     created_id = punch_obj.create(cr, uid, {'employee_id':employee_id.id,'working':working_main,'working_hour':float(new_hrs),'extra_ot':extra_ot,'new_ot':ot,'ot':new_hrs1,
  5508. #                                             'short_leave':short_leave,'method':'auto','method':'auto','in_punch':str(in_punch),'out_punch1':str(out_punch1),#'in_time':in_time,'out_time':out_time,
  5509. #                                             'out_punch2':str(out_punch2),'late':late,})
  5510.                                                          
  5511. #                                     print "=========================NEW  PUNCH CREATED===========================",created_id
  5512. #                                 except Exception,e:
  5513. #                                     print "Exception Punch Card Creation...........", e.args
  5514. #                                     pass
  5515. #                             else:
  5516. #                                 print "IN and Out Pair attendance not found for emp on date......",employee_id.name,salary_date
  5517. #                 next_date +=  timedelta(days=1)        
  5518.         return False
  5519.  
  5520. hr_calculate_salary()
  5521.  
  5522. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
Add Comment
Please, Sign In to add comment