Skip to content

Commit

Permalink
[IMP] Allow companies to group payslips in accounting entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ana0m authored and cvinh committed Feb 15, 2024
1 parent 5983d0b commit 85b7ec7
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 17 deletions.
2 changes: 1 addition & 1 deletion payroll_account/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Payroll Accounting
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:653a0c1054563da937bc111ee0541f4208dd379ec0f37ba7d7d8f4fe03216a84
!! source digest: sha256:9c9b1be50d9942288d682bac171b52d2fb61bd6904723f05ae3c833bdd20bdfc
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down
5 changes: 4 additions & 1 deletion payroll_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"summary": "Manage your payroll to accounting",
"author": "Odoo SA, Odoo Community Association (OCA)",
"depends": ["payroll", "account"],
"data": ["views/hr_payroll_account_views.xml"],
"data": [
"views/hr_payroll_account_views.xml",
"views/res_config_settings_views.xml",
],
"demo": ["demo/hr_payroll_account_demo.xml"],
"maintainers": ["appstogrow", "nimarosa"],
}
2 changes: 2 additions & 0 deletions payroll_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import hr_payroll_account
from . import res_config_settings
from . import company
12 changes: 12 additions & 0 deletions payroll_account/models/company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

action_group_payslips = fields.Boolean(
string="Group payslips in accounting",
help="Allow companies to group payslips in accounting",
)
95 changes: 81 additions & 14 deletions payroll_account/models/hr_payroll_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,58 @@ def action_payslip_cancel(self):
return super(HrPayslip, self).action_payslip_cancel()

def action_payslip_done(self):
res = super(HrPayslip, self).action_payslip_done()
# Define account move general information based on the first slip
first_slip = self[0]

move_date = first_slip.date or first_slip.date_to
move_journal = first_slip.journal_id.id

move_dict = {
"journal_id": move_journal,
"date": move_date,
}

if len(self) == 1:
move_dict["narration"] = _("Payslip of %s") % (first_slip.employee_id.name)
move_dict["ref"] = first_slip.number
else:
move_dict["narration"] = _("Payslips of %s") % (move_date)

# Check that payslips can be accounted together
for slip in self:
# Check date
date = slip.date or slip.date_to
if date != move_date:
raise UserError(
_(
"Only payslips with the same date can be accounted together."
" The payslip '%s' has a different date!"
)
% (slip.number)
)
# Check journal
journal = slip.journal_id.id
if journal != move_journal:
raise UserError(
_(
"Only payslips with the same salary journal can be accounted together."
" The payslip '%s' has a different salary journal!"
)
% (slip.number)
)

# Initialize account move and account move lines
move = self.env["account.move"].create({})
move_lines = []
# Compute account move lines
for slip in self:
line_ids = []
debit_sum = 0.0
credit_sum = 0.0
date = slip.date or slip.date_to
currency = (
slip.company_id.currency_id or slip.journal_id.company_id.currency_id
)

name = _("Payslip of %s") % (slip.employee_id.name)
move_dict = {
"narration": name,
"ref": slip.number,
"journal_id": slip.journal_id.id,
"date": date,
}
for line in slip.line_ids:
amount = currency.round(slip.credit_note and -line.total or line.total)
if currency.is_zero(amount):
Expand Down Expand Up @@ -273,15 +307,48 @@ def action_payslip_done(self):
)
line_ids.append(adjust_debit)
if len(line_ids) > 0:
move_dict["line_ids"] = line_ids
move = self.env["account.move"].create(move_dict)
slip.write({"move_id": move.id, "date": date})
move.action_post()
if len(self) == 1 or self.env.company.action_group_payslips is not True:
move_lines = line_ids
first_slip = False
else:
for line_id in line_ids:
del line_id[2]["partner_id"]
account_in_move = False
for move_line in move_lines:
if (
line_id[2]["account_id"] == move_line[2]["account_id"]
and line_id[2]["analytic_distribution"]
== move_line[2]["analytic_distribution"]
and line_id[2]["debit"] == move_line[2]["debit"] == 0
):
account_in_move = True
move_line[2]["credit"] += line_id[2]["credit"]
break
if (
line_id[2]["account_id"] == move_line[2]["account_id"]
and line_id[2]["analytic_distribution"]
== move_line[2]["analytic_distribution"]
and line_id[2]["credit"] == move_line[2]["credit"] == 0
):
account_in_move = True
move_line[2]["debit"] += line_id[2]["debit"]
break
if not account_in_move:
move_lines.append(line_id)
else:
logger.warning(
f"Payslip {slip.number} did not generate any account move lines"
)
return res
# Link payslip with the account move
slip.write({"move_id": move.id, "date": date})
# Change payslip's state to "Done"
super(HrPayslip, slip).action_payslip_done()

# Add account move lines in the account move
move_dict["line_ids"] = move_lines
move.write(move_dict)
# Post the account move
return move.action_post()


class HrSalaryRule(models.Model):
Expand Down
15 changes: 15 additions & 0 deletions payroll_account/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

action_group_payslips = fields.Boolean(
string="Group payslips in accounting",
related="company_id.action_group_payslips",
help="Allow companies to group payslips in accounting",
readonly=False,
store=True,
)
3 changes: 2 additions & 1 deletion payroll_account/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?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>
Expand Down Expand Up @@ -366,7 +367,7 @@ <h1 class="title">Payroll Accounting</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:653a0c1054563da937bc111ee0541f4208dd379ec0f37ba7d7d8f4fe03216a84
!! source digest: sha256:9c9b1be50d9942288d682bac171b52d2fb61bd6904723f05ae3c833bdd20bdfc
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/payroll/tree/16.0/payroll_account"><img alt="OCA/payroll" src="https://img.shields.io/badge/github-OCA%2Fpayroll-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/payroll-16-0/payroll-16-0-payroll_account"><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/payroll&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Generic Payroll system Integrated with Accounting.</p>
Expand Down
30 changes: 30 additions & 0 deletions payroll_account/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="res_config_settings_view_form" model="ir.ui.view">
<field
name="name"
>res.config.settings.view.form.inherit.hr.payroll.account</field>
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="payroll.res_config_settings_view_form" />
<field name="arch" type="xml">
<xpath expr="//div[@id='payroll_accountant']" position="after">
<div class="row mt16 o_settings_container" id="action_group_payslips">
<div class="col-lg-6 col-12 o_setting_box">
<div class="o_setting_left_pane">
<field name="action_group_payslips" />
</div>
<div class="o_setting_right_pane">
<label
for="action_group_payslips"
string="Group payslips in accounting"
/>
<div class="text-muted">
Allow companies to group payslips in accounting
</div>
</div>
</div>
</div>
</xpath>
</field>
</record>
</odoo>
1 change: 1 addition & 0 deletions payroll_account/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import hr_payroll_payslips_by_employees
from . import hr_payslip_change_state
35 changes: 35 additions & 0 deletions payroll_account/wizard/hr_payslip_change_state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models
from odoo.exceptions import UserError
from odoo.tools.translate import _


class HrPayslipChangeState(models.TransientModel):

_inherit = "hr.payslip.change.state"

def change_state_confirm(self):
company_id = self.env.company.id
config_settings = self.env["res.config.settings"].search(
[("company_id", "=", company_id)], limit=1
)
action_group_payslips = config_settings.action_group_payslips

if action_group_payslips and self.state == "done":
record_ids = self.env.context.get("active_ids", False)
payslip_obj = self.env["hr.payslip"]
records = payslip_obj.browse(record_ids)
for rec in records:
if rec.state not in ("verify", "draft"):
raise UserError(
_(
"Only payslips in states verify or draft"
" can be confirmed, the payslip %(nm)s is in "
"%(st)s state"
)
% {"nm": rec.name, "st": rec.state}
)
records.action_payslip_done()
else:
return super(HrPayslipChangeState, self).change_state_confirm()

0 comments on commit 85b7ec7

Please sign in to comment.