Skip to content

Commit

Permalink
[IMP] hr_period_create_timesheet: add a cron to automatically create …
Browse files Browse the repository at this point in the history
…the timesheets for future periods
  • Loading branch information
AaronHForgeFlow committed Jul 9, 2024
1 parent 8e8edb5 commit 52df3c4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion hr_period_create_timesheet/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ HR period create timesheet
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:015f8be83c740ccb37b80f8adf1060a8544297cdb5dc50178c394d441a628cd2
!! source digest: sha256:81e2a29a70c3c038721b45c78c57dfdfcf651eebd84c26efe2d52c5ecf5527c7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
1 change: 1 addition & 0 deletions hr_period_create_timesheet/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "AGPL-3",
"data": [
"security/ir.model.access.csv",
"data/ir_cron.xml",
"wizards/hr_period_create_timesheet_view.xml",
],
"installable": True,
Expand Down
14 changes: 14 additions & 0 deletions hr_period_create_timesheet/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="ir_cron_hr_payroll_period" model="ir.cron">
<field name='name'>Create Timesheets for Future Periods</field>
<field name='interval_number'>1</field>
<field name='interval_type'>weeks</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="code">model.create_timesheets_on_future_periods()</field>
<field name="state">code</field>
<field name="user_id" ref="base.user_root" />
<field name="model_id" ref="model_hr_period_create_timesheet" />
</record>
</odoo>
36 changes: 18 additions & 18 deletions hr_period_create_timesheet/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>HR period create timesheet</title>
<style type="text/css">

/*
:Author: David Goodger ([email protected])
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $
:Copyright: This stylesheet has been placed in the public domain.

Default cascading style sheet for the HTML output of Docutils.

See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/

Expand Down Expand Up @@ -367,26 +367,26 @@ <h1 class="title">HR period create timesheet</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:015f8be83c740ccb37b80f8adf1060a8544297cdb5dc50178c394d441a628cd2
!! source digest: sha256:81e2a29a70c3c038721b45c78c57dfdfcf651eebd84c26efe2d52c5ecf5527c7
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/timesheet/tree/15.0/hr_period_create_timesheet"><img alt="OCA/timesheet" src="https://img.shields.io/badge/github-OCA%2Ftimesheet-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/timesheet-15-0/timesheet-15-0-hr_period_create_timesheet"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runboat.odoo-community.org/builds?repo=OCA/timesheet&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/timesheet/tree/15.0/hr_period_create_timesheet"><img alt="OCA/timesheet" src="https://img.shields.io/badge/github-OCA%2Ftimesheet-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/timesheet-15-0/timesheet-15-0-hr_period_create_timesheet"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/timesheet&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module prepare the timesheets for all the periods created for all
employees.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#usage" id="id1">Usage</a></li>
<li><a class="reference internal" href="#bug-tracker" id="id2">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id3">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id4">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id5">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id6">Maintainers</a></li>
<li><a class="reference internal" href="#usage" id="toc-entry-1">Usage</a></li>
<li><a class="reference internal" href="#bug-tracker" id="toc-entry-2">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="toc-entry-3">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-4">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-5">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-6">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#id1">Usage</a></h1>
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>To use this module, you need to:</p>
<ul class="simple">
<li>Go to Payroll &gt; Payroll Periods and select the periods you want to create the
Expand All @@ -397,31 +397,31 @@ <h1><a class="toc-backref" href="#id1">Usage</a></h1>
</ul>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/timesheet/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/timesheet/issues/new?body=module:%20hr_period_create_timesheet%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#id3">Credits</a></h1>
<h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#id4">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<ul class="simple">
<li>Eficent</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Serpent Consulting Services Pvt. Ltd. &lt;<a class="reference external" href="mailto:support&#64;serpentcs.com">support&#64;serpentcs.com</a>&gt;</li>
<li>Aaron Henriquez &lt;<a class="reference external" href="mailto:ahenriquez&#64;forgeflow.com">ahenriquez&#64;forgeflow.com</a>&gt;</li>
<li>Jasmin Solanki &lt;<a class="reference external" href="mailto:jasmin.solanki&#64;forgeflow.com">jasmin.solanki&#64;forgeflow.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#id6">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>OCA, or the Odoo Community Association, is a nonprofit organization whose
Expand Down
25 changes: 25 additions & 0 deletions hr_period_create_timesheet/wizards/hr_period_create_timesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,28 @@ def compute(self):
"context": False,
"type": "ir.actions.act_window",
}

def create_timesheets_on_future_periods(self):
timesheet_obj = self.env["hr_timesheet.sheet"]
today = fields.Date.today()
periods = self.env["hr.period"].search([("date_end", ">=", today)])
employees = self.env["hr.employee"].search([("user_id", "!=", False)])
if not periods or not employees:
return
# Create a dictionary to store existing timesheets per employee
existing_timesheets = {}
timesheet_domain = [
("employee_id", "in", employees.ids),
("date_start", "<=", max(periods.mapped("date_end"))),
("date_end", ">=", min(periods.mapped("date_start"))),
]
for timesheet in timesheet_obj.search(timesheet_domain):
key = (timesheet.employee_id.id, timesheet.date_start, timesheet.date_end)
existing_timesheets[key] = timesheet

for hr_period in periods:
for employee in employees:
key = (employee.id, hr_period.date_start, hr_period.date_end)
if key not in existing_timesheets:
ts_data = self._prepare_timesheet(employee, hr_period)
timesheet_obj.create(ts_data)

0 comments on commit 52df3c4

Please sign in to comment.