Skip to content

Commit 7e4a4a0

Browse files
authored
Add monthly report for hr_leave (#128)
1 parent 99f80dc commit 7e4a4a0

7 files changed

Lines changed: 157 additions & 2 deletions

File tree

hr_holidays_webform/__manifest__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
"data": [
1616
"security/ir.model.access.csv",
1717
"data/leave_requests_mail_template.xml",
18+
"data/leave_report_mail_template.xml",
1819
"data/leave_requests_report_mail_template.xml",
1920
"data/cron_send_monthly_report.xml",
21+
"data/cron_send_monthly_leave_report.xml",
2022
"views/report.xml",
2123
"views/report_hr_leave_request_template.xml",
24+
"views/report_hr_leave_template.xml",
25+
"views/report_hr_leave.xml",
2226
"views/hr_leave_request.xml",
2327
"views/hr_leave.xml",
2428
"views/hr_leave_request_wizard.xml",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="ir_cron_hr_leave_monthly_report" model="ir.cron">
4+
<field name="name">Leave Monthly Report</field>
5+
<field name="model_id" ref="model_hr_leave" />
6+
<field name="state">code</field>
7+
<field name="code">model.cron_generate_monthly_report()</field>
8+
<field name="interval_number">1</field>
9+
<field name="interval_type">months</field>
10+
<field name="numbercall">-1</field>
11+
<field name="active">True</field>
12+
</record>
13+
</odoo>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<odoo noupdate="1">
2+
<record id="mail_template_hr_leave_monthly_report" model="mail.template">
3+
<field name="name">Leave Monthly Report</field>
4+
<field name="model_id" ref="model_hr_leave" />
5+
<field name="subject">Monthly Leave Report</field>
6+
<field name="email_to">manager@example.com</field>
7+
<field name="body_html">
8+
<![CDATA[
9+
<p>Hello,</p>
10+
<p>Please find attached the monthly Leave report.</p>
11+
<p>Best regards,<br/>Odoo System</p>
12+
]]>
13+
</field>
14+
</record>
15+
</odoo>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
1+
import base64
2+
import logging
3+
4+
from dateutil.relativedelta import relativedelta
5+
16
from odoo import fields, models
27

8+
logger = logging.getLogger(__name__)
9+
310

411
class HrLeave(models.Model):
512
_inherit = "hr.leave"
613

714
leave_request_id = fields.Many2one("hr.leave.request", string="Leave Request")
15+
16+
def cron_generate_monthly_report(self):
17+
today = fields.Date.today()
18+
first_of_this_month = today.replace(day=1)
19+
first_of_last_month = first_of_this_month - relativedelta(months=1)
20+
last_of_last_month = first_of_this_month - relativedelta(days=1)
21+
default_leave_type = (
22+
self.env["ir.config_parameter"]
23+
.sudo()
24+
.get_param("hr_leave_request.default_leave_type")
25+
)
26+
records = self.search(
27+
[
28+
("date_from", ">=", first_of_last_month),
29+
("date_to", "<=", last_of_last_month),
30+
(
31+
"holiday_status_id",
32+
"=",
33+
int(default_leave_type) if default_leave_type else False,
34+
),
35+
("number_of_days", ">", 0),
36+
]
37+
)
38+
39+
if not records:
40+
return
41+
42+
pdf_content, _ = self.env.ref(
43+
"hr_holidays_webform.report_hr_leave_pdf"
44+
)._render_qweb_pdf(records.ids)
45+
46+
attachment = self.env["ir.attachment"].create(
47+
{
48+
"name": "Monthly_Leave_Report.pdf",
49+
"type": "binary",
50+
"datas": base64.b64encode(pdf_content),
51+
"res_model": "hr.leave",
52+
"res_id": records[0].id,
53+
"mimetype": "application/pdf",
54+
}
55+
)
56+
template = self.env.ref(
57+
"hr_holidays_webform.mail_template_hr_leave_monthly_report"
58+
)
59+
if template:
60+
# force_attachment prevents Odoo from regenerating attachments
61+
# defined in the template
62+
template.send_mail(
63+
records[0].id,
64+
force_send=True,
65+
email_values={"attachment_ids": [(6, 0, [attachment.id])]},
66+
)

hr_holidays_webform/models/hr_leave_request.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ def cron_generate_monthly_report(self):
157157
last_of_last_month = first_of_this_month - relativedelta(days=1)
158158
records = self.search(
159159
[
160-
("create_date", ">=", first_of_last_month),
161-
("create_date", "<=", last_of_last_month),
160+
("start_date", ">=", first_of_last_month),
161+
("end_date", "<=", last_of_last_month),
162162
]
163163
)
164164

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<odoo>
2+
<record id="report_hr_leave_pdf" model="ir.actions.report">
3+
<field name="name">Leave Report</field>
4+
<field name="model">hr.leave</field>
5+
<field name="report_type">qweb-pdf</field>
6+
<field name="report_name">hr_holidays_webform.report_hr_leave_template</field>
7+
<field name="report_file">hr_holidays_webform.report_hr_leave_template</field>
8+
<field name="print_report_name">'Leave_Report_' + object.name</field>
9+
<field name="binding_model_id" ref="model_hr_leave" />
10+
<field name="binding_type">report</field>
11+
</record>
12+
</odoo>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<odoo>
2+
<template id="report_hr_leave_template">
3+
<t t-call="web.html_container">
4+
<div class="page">
5+
<h2>Leave Report</h2>
6+
<table
7+
class="table table-sm o_main_table"
8+
style="width:100%; border:1px solid black; border-collapse: collapse;"
9+
>
10+
<thead>
11+
<tr>
12+
<th style="border:1px solid black; padding:4px;">
13+
Employee
14+
</th>
15+
<th style="border:1px solid black; padding:4px;">
16+
Start Date
17+
</th>
18+
<th style="border:1px solid black; padding:4px;">
19+
End Date
20+
</th>
21+
<th style="border:1px solid black; padding:4px;">
22+
Number of Days
23+
</th>
24+
</tr>
25+
</thead>
26+
<tbody>
27+
<t t-foreach="docs" t-as="o">
28+
<tr>
29+
<td
30+
style="border:1px solid black; padding:4px;"
31+
t-esc="o.employee_id.name"
32+
/>
33+
<td
34+
style="border:1px solid black; padding:4px;"
35+
t-esc="o.date_from"
36+
/>
37+
<td
38+
style="border:1px solid black; padding:4px;"
39+
t-esc="o.date_to"
40+
/>
41+
<td
42+
style="border:1px solid black; padding:4px;"
43+
t-esc="o.number_of_days"
44+
/>
45+
</tr>
46+
</t>
47+
</tbody>
48+
</table>
49+
</div>
50+
</t>
51+
</template>
52+
</odoo>

0 commit comments

Comments
 (0)