From 1a2b4f4de7e833bf0e7cb63416ef1a4040d594e6 Mon Sep 17 00:00:00 2001 From: rsi-odoo Date: Tue, 21 Oct 2014 18:02:03 +0530 Subject: [PATCH] [IMP] hr_holidays: Convert Leaves Summary xsl report in qweb report --- addons/hr_holidays/__openerp__.py | 2 + .../hr_holidays/data/report_paperformat.xml | 20 ++ addons/hr_holidays/hr_holidays_report.xml | 22 +- .../hr_holidays/report/holidays_summary.xsl | 259 -------------- .../report/holidays_summary_report.py | 329 ++++++------------ .../hr_holidays/report/hr_custom_default.xsl | 36 -- addons/hr_holidays/report/hr_custom_rml.xsl | 37 -- .../views/report_hr_holidays_summary.xml | 82 +++++ .../wizard/hr_holidays_summary_department.py | 8 +- .../wizard/hr_holidays_summary_employees.py | 8 +- addons/report/views/layouts.xml | 2 + 11 files changed, 228 insertions(+), 577 deletions(-) create mode 100644 addons/hr_holidays/data/report_paperformat.xml delete mode 100644 addons/hr_holidays/report/holidays_summary.xsl delete mode 100644 addons/hr_holidays/report/hr_custom_default.xsl delete mode 100644 addons/hr_holidays/report/hr_custom_rml.xsl create mode 100644 addons/hr_holidays/views/report_hr_holidays_summary.xml diff --git a/addons/hr_holidays/__openerp__.py b/addons/hr_holidays/__openerp__.py index 4f1b2e2265d..60740c7e963 100644 --- a/addons/hr_holidays/__openerp__.py +++ b/addons/hr_holidays/__openerp__.py @@ -46,12 +46,14 @@ A synchronization with an internal agenda (Meetings of the CRM module) is also p """, 'depends': ['hr', 'calendar', 'resource'], 'data': [ + 'data/report_paperformat.xml', 'security/ir.model.access.csv', 'security/ir_rule.xml', 'hr_holidays_workflow.xml', 'hr_holidays_view.xml', 'hr_holidays_data.xml', 'hr_holidays_report.xml', + 'views/report_hr_holidays_summary.xml', 'report/hr_holidays_report_view.xml', 'report/available_holidays_view.xml', 'wizard/hr_holidays_summary_department_view.xml', diff --git a/addons/hr_holidays/data/report_paperformat.xml b/addons/hr_holidays/data/report_paperformat.xml new file mode 100644 index 00000000000..22dc0ea7ae4 --- /dev/null +++ b/addons/hr_holidays/data/report_paperformat.xml @@ -0,0 +1,20 @@ + + + + + Holidays Summary + + custom + 297 + 210 + Landscape + 30 + 23 + 5 + 5 + + 20 + 90 + + + \ No newline at end of file diff --git a/addons/hr_holidays/hr_holidays_report.xml b/addons/hr_holidays/hr_holidays_report.xml index 14d4cb99562..421e84e2cea 100644 --- a/addons/hr_holidays/hr_holidays_report.xml +++ b/addons/hr_holidays/hr_holidays_report.xml @@ -2,14 +2,18 @@ - + + + + + - - + \ No newline at end of file diff --git a/addons/hr_holidays/report/holidays_summary.xsl b/addons/hr_holidays/report/holidays_summary.xsl deleted file mode 100644 index 4d83cd9cdc7..00000000000 --- a/addons/hr_holidays/report/holidays_summary.xsl +++ /dev/null @@ -1,259 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 0, - - - - 0, - - - - - - - - - - - - - - - lightgrey - - - ,0 - - - - ,-1 - - - - - - - - - - - - - - - - , - - - - - , - - - - - - - - - - - - lightgrey - - 0, - - - - 0, - - - - - - - - - - - - - - - - 13.7cm,13.7cm - - - header - - - - - - - - - - - - - - - - Leaves Summary - - Analyze from to of the leaves. - - - 0.7cm,5.0cm - - - products - - - Month - - - - - - - - - - - month - - - - - - - - - - - - - employee - Departments and Employees - - - - digits - - - - Sum - - - - - - - - - - - - dept - - - normal - - - - - - - - - digits - - - - - - - sum - - - - - - - - legend - - - Color - Leave Type - - - - - - - digits - - - - - normal - - - - - - - - diff --git a/addons/hr_holidays/report/holidays_summary_report.py b/addons/hr_holidays/report/holidays_summary_report.py index 1c3fc6dee32..a32fdf8c971 100644 --- a/addons/hr_holidays/report/holidays_summary_report.py +++ b/addons/hr_holidays/report/holidays_summary_report.py @@ -19,236 +19,117 @@ # ############################################################################## -import datetime -import time - -import openerp -from openerp.osv import fields, osv -from openerp.report.interface import report_rml -from openerp.report.interface import toxml - -from openerp.report import report_sxw -from openerp.tools import ustr +from datetime import datetime, timedelta +from dateutil.relativedelta import relativedelta +from openerp.osv import osv from openerp.tools.translate import _ -from openerp.tools import to_xml +from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT -def lengthmonth(year, month): - if month == 2 and ((year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0))): - return 29 - return [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] +class HrHolidaySummaryReport(osv.AbstractModel): + _name = 'report.hr_holidays.report_holidayssummary' -def strToDate(dt): - if dt: - dt_date=datetime.date(int(dt[0:4]),int(dt[5:7]),int(dt[8:10])) - return dt_date - else: - return + def _get_header_info(self, start_date, holiday_type): + return { + 'start_date': datetime.strptime(start_date, DEFAULT_SERVER_DATE_FORMAT).strftime('%Y-%m-%d'), + 'end_date': (datetime.strptime(start_date, DEFAULT_SERVER_DATE_FORMAT) + relativedelta(days=59)).strftime('%Y-%m-%d'), + 'holiday_type': 'Confirmed and Approved' if holiday_type == 'both' else holiday_type + } + def _get_day(self, start_date): + res = [] + start_date = datetime.strptime(start_date, DEFAULT_SERVER_DATE_FORMAT) + for x in range(0, 60): + color = '#ababab' if start_date.strftime('%a') == 'Sat' or start_date.strftime('%a') == 'Sun' else '' + res.append({'day_str': start_date.strftime('%a'), 'day': start_date.day , 'color': color}) + start_date = start_date + relativedelta(days=1) + return res -def emp_create_xml(self, cr, uid, dept, holiday_type, row_id, empid, name, som, eom): - display={} - if dept==0: - count=0 - registry = openerp.registry(cr.dbname) - holidays_ids = registry['hr.holidays'].search(cr, uid, [('employee_id','in',[empid,False]), ('type', '=', 'remove')]) - ids_date = registry['hr.holidays'].read(cr, uid, holidays_ids, ['date_from','date_to','holiday_status_id','state']) + def _get_months(self, start_date): + # it works for geting month name between two dates. + res = [] + start_date = datetime.strptime(start_date, DEFAULT_SERVER_DATE_FORMAT) + end_date = start_date + relativedelta(days=59) + while start_date <= end_date: + last_date = start_date + relativedelta(day=1, months=+1, days=-1) + if last_date > end_date: + last_date = end_date + month_days = (last_date - start_date).days + 1 + res.append({'month_name': start_date.strftime('%B'), 'days': month_days}) + start_date += relativedelta(day=1, months=+1) + return res - for index in range(1,61): - diff=index-1 - current=som+datetime.timedelta(diff) + def _get_leaves_summary(self, cr, uid, ids, start_date, empid, holiday_type, context=None): + res = [] + count = 0 + start_date = datetime.strptime(start_date, DEFAULT_SERVER_DATE_FORMAT) + end_date = start_date + relativedelta(days=59) + for index in range(0, 60): + current = start_date + timedelta(index) + res.append({'day': current.day, 'color': ''}) + if current.strftime('%a') == 'Sat' or current.strftime('%a') == 'Sun': + res[index]['color'] = '#ababab' + # count and get leave summary details. + holidays_obj = self.pool['hr.holidays'] + holiday_type = ['confirm','validate'] if holiday_type == 'both' else ['confirm'] if holiday_type == 'Confirmed' else ['validate'] + holidays_ids = holidays_obj.search(cr, uid, [('employee_id', '=', empid), ('state', 'in', holiday_type), ('type', '=', 'remove'), ('date_from', '<=', str(end_date)), ('date_to', '>=', str(start_date))], context=context) + for holiday in holidays_obj.browse(cr, uid, holidays_ids, context=context): + date_from = datetime.strptime(holiday.date_from, DEFAULT_SERVER_DATETIME_FORMAT) + date_to = datetime.strptime(holiday.date_to, DEFAULT_SERVER_DATETIME_FORMAT) + for index in range(0, ((date_to - date_from).days + 1)): + if date_from >= start_date and date_from <= end_date: + res[(date_from-start_date).days]['color'] = holiday.holiday_status_id.color_name + count+=1 + date_from += timedelta(1) + self.sum = count + return res - for item in ids_date: - if current >= strToDate(item['date_from']) and current <= strToDate(item['date_to']): - if item['state'] in holiday_type: - display[index]=item['holiday_status_id'][0] - count=count +1 - else: - display[index]=' ' - break - else: - display[index]=' ' - else: - for index in range(1,61): - display[index]=' ' - count='' - - data_xml=['' % (row_id,x,display[x]) for x in range(1,len(display)+1) ] - - # Computing the xml - xml = ''' - %s - - - ''' % (data_xml,row_id,dept, ustr(toxml(name)),count) + def _get_data_from_report(self, cr, uid, ids, data, context=None): + res = [] + emp_obj = self.pool['hr.employee'] + department_obj = self.pool['hr.department'] + if 'depts' in data: + for department in department_obj.browse(cr, uid, data['depts'], context=context): + res.append({'dept' : department.name, 'data': [], 'color': self._get_day(data['date_from'])}) + employee_ids = emp_obj.search(cr, uid, [('department_id', '=', department.id)], context=context) + employees = emp_obj.browse(cr, uid, employee_ids, context=context) + for emp in employees: + res[len(res)-1]['data'].append({ + 'emp': emp.name, + 'display': self._get_leaves_summary(cr, uid, ids, data['date_from'], emp.id, data['holiday_type'], context=context), + 'sum': self.sum + }) + elif 'emp' in data: + employees = emp_obj.browse(cr, uid, data['emp'], context=context) + res.append({'data':[]}) + for emp in employees: + res[0]['data'].append({ + 'emp': emp.name, + 'display': self._get_leaves_summary(cr, uid, ids, data['date_from'], emp.id, data['holiday_type'], context=context), + 'sum': self.sum + }) + return res - return xml + def _get_holidays_status(self, cr, uid, ids, context=None): + res = [] + holiday_obj = self.pool['hr.holidays.status'] + holiday_ids = holiday_obj.search(cr, uid, [], context=context) + holiday_datas = holiday_obj.browse(cr, uid, holiday_ids, context=context) + for holiday in holiday_datas: + res.append({'color': holiday.color_name, 'name': holiday.name}) + return res -class report_custom(report_rml): - def create_xml(self, cr, uid, ids, data, context): - registry = openerp.registry(cr.dbname) - obj_dept = registry['hr.department'] - obj_emp = registry['hr.employee'] - depts=[] - emp_id={} - rpt_obj = registry['hr.holidays'] - rml_obj=report_sxw.rml_parse(cr, uid, rpt_obj._name,context) - cr.execute("SELECT name FROM res_company") - res=cr.fetchone()[0] - date_xml=[] - date_today=time.strftime('%Y-%m-%d %H:%M:%S') - date_xml +=['' % (to_xml(res),date_today)] - - cr.execute("SELECT id, name, color_name FROM hr_holidays_status ORDER BY id") - legend=cr.fetchall() - today=datetime.datetime.today() - - first_date=data['form']['date_from'] - som = strToDate(first_date) - eom = som+datetime.timedelta(59) - day_diff=eom-som - - name = '' - if len(data['form'].get('emp', ())) == 1: - name = obj_emp.read(cr, uid, data['form']['emp'][0], ['name'])['name'] - - if data['form']['holiday_type']!='both': - type=data['form']['holiday_type'] - if data['form']['holiday_type']=='Confirmed': - holiday_type=('confirm') - else: - holiday_type=('validate') - else: - type="Confirmed and Approved" - holiday_type=('confirm','validate') - date_xml.append('%s\n'% (str(rml_obj.formatLang(som.strftime("%Y-%m-%d"),date=True)))) - date_xml.append('%s\n' %(str(rml_obj.formatLang(eom.strftime("%Y-%m-%d"),date=True)))) - date_xml.append('%s'%(type)) - date_xml.append('%s'%(name)) - -# date_xml=[] - for l in range(0,len(legend)): - date_xml += ['' % (l+1,legend[l][0],_(legend[l][1]),legend[l][2])] - date_xml += ['' % (ustr(som.strftime('%B')), som.year),''] - - cell=1 - if day_diff.days>=30: - date_xml += ['' % (x, _(som.replace(day=x).strftime('%a')),x-som.day+1) for x in range(som.day, lengthmonth(som.year, som.month)+1)] - else: - if day_diff.days>=(lengthmonth(som.year, som.month)-som.day): - date_xml += ['' % (x, _(som.replace(day=x).strftime('%a')),x-som.day+1) for x in range(som.day, lengthmonth(som.year, som.month)+1)] - else: - date_xml += ['' % (x, _(som.replace(day=x).strftime('%a')),x-som.day+1) for x in range(som.day, eom.day+1)] - - cell=x-som.day+1 - day_diff1=day_diff.days-cell+1 - - width_dict={} - month_dict={} - - i=1 - j=1 - year=som.year - month=som.month - month_dict[j]=som.strftime('%B') - width_dict[j]=cell - - while day_diff1>0: - if month+i<=12: - if day_diff1 > lengthmonth(year,i+month): # Not on 30 else you have problems when entering 01-01-2009 for example - som1=datetime.date(year,month+i,1) - date_xml += ['' % (x, _(som1.replace(day=x).strftime('%a')),cell+x) for x in range(1, lengthmonth(year,i+month)+1)] - i=i+1 - j=j+1 - month_dict[j]=som1.strftime('%B') - cell=cell+x - width_dict[j]=x - - else: - som1=datetime.date(year,month+i,1) - date_xml += ['' % (x, _(som1.replace(day=x).strftime('%a')),cell+x) for x in range(1, eom.day+1)] - i=i+1 - j=j+1 - month_dict[j]=som1.strftime('%B') - cell=cell+x - width_dict[j]=x - - day_diff1=day_diff1-x - else: - years=year+1 - year=years - month=0 - i=1 - if day_diff1>=30: - som1=datetime.date(years,i,1) - date_xml += ['' % (x, _(som1.replace(day=x).strftime('%a')),cell+x) for x in range(1, lengthmonth(years,i)+1)] - i=i+1 - j=j+1 - month_dict[j]=som1.strftime('%B') - cell=cell+x - width_dict[j]=x - - else: - som1=datetime.date(years,i,1) - i=i+1 - j=j+1 - month_dict[j]=som1.strftime('%B') - date_xml += ['' % (x, _(som1.replace(day=x).strftime('%a')),cell+x) for x in range(1, eom.day+1)] - cell=cell+x - width_dict[j]=x - - day_diff1=day_diff1-x - - date_xml.append('') - date_xml.append('3.5cm%s,0.4cm\n' % (',0.4cm' * (60))) - date_xml = ''.join(date_xml) - - st='3.5cm' - for m in range(1,len(width_dict)+1): - st+=',' + str(0.4 *width_dict[m])+'cm' - st+=',0.4cm\n' - - months_xml =['' % (x, _(month_dict[x])) for x in range(1,len(month_dict)+1) ] - months_xml.append(st) - - emp_xml='' - row_id=1 - - if data['model'] == 'hr.employee': - for items in obj_emp.read(cr, uid, data['form']['emp'], ['id', 'name']): - emp_xml += emp_create_xml(self, cr, uid, 0, holiday_type, row_id, items['id'], items['name'], som, eom) - row_id = row_id +1 - - elif data['model']=='ir.ui.menu': - for dept in obj_dept.browse(cr, uid, data['form']['depts'], context=context): - emp_ids = obj_emp.search(cr, uid, [('department_id', '=', dept.id)], context=context) - if emp_ids==[]: - continue - dept_done=0 - for item in obj_emp.read(cr, uid, emp_ids, ['id', 'name']): - if dept_done==0: - emp_xml += emp_create_xml(self, cr, uid, 1, holiday_type, row_id, dept.id, dept.name, som, eom) - row_id = row_id +1 - dept_done=1 - emp_xml += emp_create_xml(self, cr, uid, 0, holiday_type, row_id, item['id'], item['name'], som, eom) - row_id = row_id +1 - - header_xml = ''' -
- %s - %s -
- ''' % (str(rml_obj.formatLang(time.strftime("%Y-%m-%d"),date=True))+' ' + str(time.strftime("%H:%M")),to_xml(registry['res.users'].browse(cr,uid,uid).company_id.name)) - - # Computing the xml - xml=''' - - %s - %s - %s - %s - - ''' % (header_xml,months_xml,date_xml, ustr(emp_xml)) - - return xml - -report_custom('report.holidays.summary', 'hr.holidays', '', 'addons/hr_holidays/report/holidays_summary.xsl') + def render_html(self, cr, uid, ids, data=None, context=None): + report_obj = self.pool['report'] + holidays_report = report_obj._get_report_from_name(cr, uid, 'hr_holidays.report_holidayssummary') + selected_records = self.pool['hr.holidays'].browse(cr, uid, ids, context=context) + docargs = { + 'doc_ids': ids, + 'doc_model': holidays_report.model, + 'docs': selected_records, + 'get_header_info': self._get_header_info(data['form']['date_from'], data['form']['holiday_type']), + 'get_day': self._get_day(data['form']['date_from']), + 'get_months': self._get_months(data['form']['date_from']), + 'get_data_from_report': self._get_data_from_report(cr, uid, ids, data['form'], context=context), + 'get_holidays_status': self._get_holidays_status(cr, uid, ids, context=context), + } + return report_obj.render(cr, uid, ids, 'hr_holidays.report_holidayssummary', docargs, context=context) \ No newline at end of file diff --git a/addons/hr_holidays/report/hr_custom_default.xsl b/addons/hr_holidays/report/hr_custom_default.xsl deleted file mode 100644 index 2eda3a08f4b..00000000000 --- a/addons/hr_holidays/report/hr_custom_default.xsl +++ /dev/null @@ -1,36 +0,0 @@ - - - - - - - - - - - - - 1.3cm 19.3cm 28.5cm 19.3cm - - - - - - - - - - - - 1.3cm 19.3cm 28.5cm 19.3cm - - - - - - - - - - - diff --git a/addons/hr_holidays/report/hr_custom_rml.xsl b/addons/hr_holidays/report/hr_custom_rml.xsl deleted file mode 100644 index f57c04aa097..00000000000 --- a/addons/hr_holidays/report/hr_custom_rml.xsl +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/addons/hr_holidays/views/report_hr_holidays_summary.xml b/addons/hr_holidays/views/report_hr_holidays_summary.xml new file mode 100644 index 00000000000..1096331c06d --- /dev/null +++ b/addons/hr_holidays/views/report_hr_holidays_summary.xml @@ -0,0 +1,82 @@ + + + + + + diff --git a/addons/hr_holidays/wizard/hr_holidays_summary_department.py b/addons/hr_holidays/wizard/hr_holidays_summary_department.py index 5fb4745c941..9400b00c117 100644 --- a/addons/hr_holidays/wizard/hr_holidays_summary_department.py +++ b/addons/hr_holidays/wizard/hr_holidays_summary_department.py @@ -45,11 +45,7 @@ class hr_holidays_summary_dept(osv.osv_memory): raise UserError(_('You have to select at least one Department. And try again.')) datas = { 'ids': [], - 'model': 'ir.ui.menu', + 'model': 'hr.department', 'form': data } - return { - 'type': 'ir.actions.report.xml', - 'report_name': 'holidays.summary', - 'datas': datas, - } + return self.pool['report'].get_action(cr, uid, data['depts'], 'hr_holidays.report_holidayssummary', data=datas, context=context) \ No newline at end of file diff --git a/addons/hr_holidays/wizard/hr_holidays_summary_employees.py b/addons/hr_holidays/wizard/hr_holidays_summary_employees.py index a157dfb8db1..3bf123c3038 100644 --- a/addons/hr_holidays/wizard/hr_holidays_summary_employees.py +++ b/addons/hr_holidays/wizard/hr_holidays_summary_employees.py @@ -38,14 +38,10 @@ class hr_holidays_summary_employee(osv.osv_memory): def print_report(self, cr, uid, ids, context=None): data = self.read(cr, uid, ids, context=context)[0] - data['emp'] = context['active_ids'] + data['emp'] = context.get('active_ids',[]) datas = { 'ids': [], 'model': 'hr.employee', 'form': data } - return { - 'type': 'ir.actions.report.xml', - 'report_name': 'holidays.summary', - 'datas': datas, - } + return self.pool['report'].get_action(cr, uid, data['emp'], 'hr_holidays.report_holidayssummary', data=datas, context=context) \ No newline at end of file diff --git a/addons/report/views/layouts.xml b/addons/report/views/layouts.xml index a632dff661a..d68c3ef0211 100644 --- a/addons/report/views/layouts.xml +++ b/addons/report/views/layouts.xml @@ -13,6 +13,7 @@ + @@ -149,6 +150,7 @@ +