This question has been flagged
3 Replies
6983 Views

I want to print days in a month horizontally in a report . if it is January it should be 31 ,if it is February it should be 28... How can be print it in a report.

Which report package(open office report,webkit report, Aero report...) is easier to print this requirement ?.

Avatar
Discard

what have you done for this ? I can do but first send me your exact requirement in word file at jain.atul43@gmail.com

Author

I want to print the days in month horizontally using any report package.

Author

Like a report printed from Reporting -> Human resources -> Reports -> Employee timesheet..

Author

Now can't open any mails from here so kindly please help me through this forum..

Author

hi I tried to make a report by refering the above report. the error displayed while print the report."object with reference: action - action"

Best Answer

from report import report_sxw import datetime class class_name(report_sxw.rml_parse): def __init__(self, cr, uid, name, context): super(bulletin, self).__init__(cr, uid, name, context) self.localcontext.update({ 'time_day': self.get_days, }

def get_days(self): today = datetime.date.today() first = datetime.date(day=1, month=today.month, year=today.year) lastMonth = first - datetime.timedelta(days=1) return lastMonth.strftime("%d")

image description

Avatar
Discard
Best Answer

Dear, Ashmsh.M

as you mentioned yourself, examples for your requirement are the HR Timesheet / HR holiday reports.
Those are created using XSL:RML transformation with code generated XML templates. And that's what I used too.
To get the number of days of a month OpenERP uses this very helpful, small function:

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]

If you prefer any other Report Designer you should add the information of the amount of days to the object you want to print, and then loop over it.
Somehow like:

[[ repeatIn(object.day_list, 'days', 'td' ]]

Hope that will help a little.

Avatar
Discard
Author

Hi am new to python , i have no idea about how to implement this code in my report/ how to link my report to the python function. will u plese give an example.

Please review addons\hr_holidays\report\holidays_summary.xsl and

addons\hr_holidays\report\holidays_summary_report.py

as an example.

Besides: medhabibs solution for getting the last day of a month also works very well.

Author

ok thank i will refer

Author

As a python beigner which files shall I create for printing this report... eg: files like .py file,.xml or .xsl file.. etc.please mention.

Author

I check the related xsl and py, xml files in hr_timesheet/report and hr_timesheet/wizard... and I am confused with this. How to implement it in our custom report.

Author

hi., René Schuster please refer my py and xml files posted as answer. By installing it as a module , A menu appears , while click on it a pop up form appears in that for i select month, year in the next line i select the employee . Then click on the below print button. openerp warning will be displayed..

Author

hi., René Schuster I just generate a report (xsl) , in this first row contain month name and its dates ,Second row contain employee name in the first coloumn and next empty coloumns, I need to fill the empty coloumn with employee login-time from the attendance. shall post the .PY and .XSL file

Author

posted the py and .xsl file

Author Best Answer

Doing this by refering hr_timesheet.

import datetime
from openerp.report.interface import report_rml
from openerp.report.interface import toxml
import time
from openerp import pooler
from openerp.tools.translate import _
from openerp.report import report_sxw
from openerp.tools import ustr


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]

def emp_create_xml(cr, id, som, eom, emp):
    # Computing the attendence by analytical account
    cr.execute(
        "select line.date, (unit_amount / unit.factor) as amount "\
        "from account_analytic_line as line, hr_analytic_timesheet as hr, "\
        "product_uom as unit "\
        "where hr.line_id=line.id "\
        "and product_uom_id = unit.id "\
        "and line.user_id=%s and line.date >= %s and line.date < %s "
        "order by line.date",
        (id, som.strftime('%Y-%m-%d'), eom.strftime('%Y-%m-%d')))

    # Sum by day
    month = {}
    for presence in cr.dictfetchall():
        day = int(presence['date'][-2:])
        month[day] = month.get(day, 0.0) + presence['amount']

    xml = '''
    <time-element date="%s">
        <amount>%.2f</amount>
    </time-element>
    '''
    time_xml = ([xml % (day, amount) for day, amount in month.iteritems()])

    # Computing the xml
    xml = '''
    <employee id="%d" name="%s">
    %s
    </employee>
    ''' % (id, toxml(emp), '\n'.join(time_xml))
    return xml

class report_custom(report_rml):

    def get_month_name(self, cr, uid, month, context=None):
        _months = {1:_("January"), 2:_("February"), 3:_("March"), 4:_("April"), 5:_("May"), 6:_("June"), 7:_("July"), 8:_("August"), 9:_("September"), 10:_("October"), 11:_("November"), 12:_("December")}
        return _months[month]

    def get_weekday_name(self, cr, uid, weekday, context=None):
        _weekdays = {1:_('Mon'), 2:_('Tue'), 3:_('Wed'), 4:_('Thu'), 5:_('Fri'), 6:_('Sat'), 7:_('Sun')}
        return _weekdays[weekday]
    def _last_sign(self, cr, uid, ids, name, args, context=None):
        result = {}
        if not ids:
            return result
        for id in ids:
            result[id] = False
            cr.execute("""select max(name) as name
                        from hr_attendance
                        where action in ('sign_in', 'sign_out') and employee_id = %s""",(id,))
            for res in cr.fetchall():
                result[id] = res[0]
        return result
    def create_xml(self, cr, uid, ids, data, context):

        # Computing the dates (start of month: som, and end of month: eom)
        som = datetime.date(data['form']['year'], data['form']['month'], 1)
        eom = som + datetime.timedelta(lengthmonth(som.year, som.month))
        date_xml = ['<date month="%s" year="%d" />' % (self.get_month_name(cr, uid, som.month, context=context), som.year), '<days>']
        date_xml += ['<day number="%d" name="%s" weekday="%d" />' % (x, self.get_weekday_name(cr, uid, som.replace(day=x).weekday()+1, context=context), som.replace(day=x).weekday()+1) for x in range(1, lengthmonth(som.year, som.month)+1)]
        date_xml.append('</days>')
        date_xml.append('<cols>2.5cm%s,2cm</cols>\n' % (',0.7cm' * lengthmonth(som.year, som.month)))

        emp_xml=''
        emp_obj = pooler.get_pool(cr.dbname).get('hr.employee')        
        for id in data['form']['employee_ids']:
            user = emp_obj.browse(cr, uid, id).user_id.id
            empl_name = emp_obj.browse(cr, uid, id).name
            if user:
                emp_xml += emp_create_xml(cr, user, som, eom, empl_name)
        # Computing the xml
        #Without this, report don't show non-ascii characters (TO CHECK)
        date_xml = '\n'.join(date_xml)
        rpt_obj = pooler.get_pool(cr.dbname).get('hr.attendance')
        rml_obj=report_sxw.rml_parse(cr, uid, rpt_obj._name,context)
        header_xml = '''
        <header>
        <date>%s</date>
        <company>#####@@@######</company>
        </header>
        '''  

        xml='''<?xml version="1.0" encoding="UTF-8" ?>
        <report>
        %s
        %s
        %s
        </report>
        ''' % (header_xml,date_xml, ustr(emp_xml))
        return xml

report_custom('report.hr.analytical.timesheet_users3', 'hr.attendance', '', 'addons/atten_em/report/users_timesheet_new.xsl')

In mycorresponding .xsl file .

<xsl:stylesheet version="1.0" xmlns:xsl="&lt;a href=" http:="" www.w3.org="" 1999="" xsl="" transform"="">http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:import href="custom_default.xsl"/>
<xsl:import href="custom_rml.xsl"/>

<xsl:template match="/">
    <xsl:call-template name="rml" />
</xsl:template>


<xsl:template name="stylesheet">
            <paraStyle name="normal" fontName="Helvetica" fontSize="6" alignment="center" />
            <paraStyle name="normal-title" fontName="Helvetica" fontSize="6" />
            <paraStyle name="title" fontName="Helvetica" fontSize="18" alignment="center" />
            <paraStyle name="employee" fontName="Helvetica-Oblique" fontSize="10" textColor="blue" />
            <paraStyle name="glande" textColor="red" fontSize="7" fontName="Helvetica"/>
            <paraStyle name="normal_people" textColor="green" fontSize="7" fontName="Helvetica"/>
            <paraStyle name="esclave" textColor="purple" fontSize="7" fontName="Helvetica"/>
            <blockTableStyle id="month">
                <blockAlignment value="CENTER" start="1,0" stop="-1,-1" />
                <blockFont name="Helvetica" size="8" start="0,0" stop="-1,1"/>
                <blockFont name="Helvetica" size="6" start="0,2" stop="-2,-2"/>
                <blockFont name="Helvetica-BoldOblique" size="8" start="0,-1" stop="-1,-1"/>
                <blockBackground colorName="#AAAAAA" start="1,0" stop="-2,1"/>
                <xsl:for-each select="/report/days/day[@weekday=6 or @weekday=7]">
                    <xsl:variable name="col" select="attribute::number" />
                    <blockBackground>
                        <xsl:attribute name="colorName">lightgrey</xsl:attribute>
                        <xsl:attribute name="start">
                            <xsl:value-of select="$col" />
                            <xsl:text>,0</xsl:text>
                        </xsl:attribute>
                        <xsl:attribute name="stop">
                            <xsl:value-of select="$col" />
                            <xsl:text>,-1</xsl:text>
                        </xsl:attribute>
                    </blockBackground>
                </xsl:for-each>
                <lineStyle kind="LINEABOVE" colorName="black" start="0,0" stop="-1,-1" />
                <lineStyle kind="LINEBEFORE" colorName="black" start="0,0" stop="-1,-1"/>
                <lineStyle kind="LINEAFTER" colorName="black" start="-1,0" stop="-1,-1"/>
                <lineStyle kind="LINEBELOW" colorName="black" start="0,-1" stop="-1,-1"/>
                <blockValign value="TOP"/>
            </blockTableStyle>
</xsl:template>

<xsl:template name="story">
    <spacer length="1cm" />
    <para style="title" t="1">Employees attendanceSS</para>
    <spacer length="1.5cm" />
    <blockTable>
        <xsl:attribute name="style">month</xsl:attribute>
        <xsl:attribute name="colWidths"><xsl:value-of select="report/cols" /></xsl:attribute>
        <tr>
            <td><xsl:value-of select="report/date/attribute::year" /></td>
            <xsl:for-each select="report/days/day">
                <td>
                    <xsl:value-of select="attribute::name" />
                </td>
            </xsl:for-each>
            <td></td>
        </tr>
        <tr>
                <td><xsl:value-of select="report/date/attribute::month" /></td> 
            <xsl:for-each select="report/days/day">
                <td>
                    <xsl:value-of select="attribute::number" />
                </td> 
                </xsl:for-each> 
            <!--<td t="1">Total</td>-->
        </tr>
        <xsl:apply-templates select="report/employee"/>
        <xsl:for-each select="report/employee">
            <xsl:variable name="id" select="attribute::id"/>
            <tr>
                <td><xsl:value-of select="attribute::name"/></td>
                   <xsl:for-each select="//report/days/day">        <!--select="//report/days/day"-->
                <!--    <xsl:variable name="result" select="attribute::id" />-->
                                       </xsl:for-each>
                <xsl:for-each select="//report/result/name">
                <td><xsl:variable name="result" select="attribute::id"/></td>   
                </xsl:for-each>
                <!--<td>
                    <xsl:value-of select="//report/sign_in"/>
                </td> -->
            </tr>
         </xsl:for-each>
        <!--<tr>
            <td t="1">Total</td>
            <xsl:for-each select="report/days/day">
                <xsl:variable name="today" select="attribute::number"/>
                <td><xsl:value-of select="format-number(sum(//time-element[@date=$today]),'##.##')"/></td>
            </xsl:for-each>
            <td><xsl:value-of select="format-number(sum(//time-element),'##.##')"/></td>
        </tr>-->
    </blockTable>
</xsl:template>

</xsl:stylesheet>

Avatar
Discard
Author

while click on the print button the errror appears "AttributeError: 'analytical_timesheet_daily' object has no attribute 'print_report'".

Remove _inherit = 'hr.attendance'

Author

removed inherit = 'hr.attendance'.. By refering the hr_timesheet module , Next i added the corresponding files to my report folder .