Skip to content

Commit

Permalink
[IMP] Refactorize action_done
Browse files Browse the repository at this point in the history
  • Loading branch information
cvinh committed Feb 15, 2024
1 parent 85b7ec7 commit 560edf1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 60 deletions.
4 changes: 3 additions & 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:9c9b1be50d9942288d682bac171b52d2fb61bd6904723f05ae3c833bdd20bdfc
!! source digest: sha256:5bdfba50457cc2a2920d5ffdf3c32cd43ab3533339bedf79c110b47f3ef95a15
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand Down Expand Up @@ -68,6 +68,8 @@ Contributors

* Odoo SA <[email protected]>
* Saran Lim. <[email protected]>
* Moana FABER <[email protected]>
* Cyril VINH-TUNG <[email protected]>

Maintainers
~~~~~~~~~~~
Expand Down
131 changes: 73 additions & 58 deletions payroll_account/models/hr_payroll_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,15 @@ def onchange_contract(self):
def action_payslip_cancel(self):
for payslip in self:
if not payslip.move_id.journal_id.restrict_mode_hash_table:
# TODO : cancel all the grouped payslips
payslip.move_id.with_context(force_delete=True).button_cancel()
payslip.move_id.with_context(force_delete=True).unlink()
else:
payslip.move_id._reverse_moves()
payslip.move_id = False
return super(HrPayslip, self).action_payslip_cancel()

def action_payslip_done(self):
# 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)

def _check_can_merge(self, move_date, move_journal):
# Check that payslips can be accounted together
for slip in self:
# Check date
Expand All @@ -128,14 +112,50 @@ def action_payslip_done(self):
% (slip.number)
)

# Initialize account move and account move lines
move = self.env["account.move"].create({})
move_lines = []
def _merge_move_lines(self, move_lines, line_ids):
for line in line_ids:
del line[2]["partner_id"]
account_in_move = False

Check warning on line 118 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L117-L118

Added lines #L117 - L118 were not covered by tests
for move_line in move_lines:
if (
line[2]["account_id"] == move_line[2]["account_id"]
and line[2]["analytic_distribution"]
== move_line[2]["analytic_distribution"]
and line[2]["debit"] == move_line[2]["debit"] == 0
):
account_in_move = True
move_line[2]["credit"] += line[2]["credit"]
break

Check warning on line 128 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L126-L128

Added lines #L126 - L128 were not covered by tests
if (
line[2]["account_id"] == move_line[2]["account_id"]
and line[2]["analytic_distribution"]
== move_line[2]["analytic_distribution"]
and line[2]["credit"] == move_line[2]["credit"] == 0
):
account_in_move = True
move_line[2]["debit"] += line[2]["debit"]
break

Check warning on line 137 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L135-L137

Added lines #L135 - L137 were not covered by tests
if not account_in_move:
move_lines.append(line)
return move_lines

Check warning on line 140 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L139-L140

Added lines #L139 - L140 were not covered by tests

def action_payslip_done(self):
# Check if we can merge payslips in accounting
if len(self) > 1 and self.env.company.action_group_payslips:
# Define account move general information based on the first slip
move_date = self[0].date or self[0].date_to
move_journal = self[0].journal_id.id
self._check_can_merge(move_date, move_journal)

Check warning on line 148 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L146-L148

Added lines #L146 - L148 were not covered by tests

# Initialize account move and account move lines
move = False
move_lines = []

Check warning on line 152 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L151-L152

Added lines #L151 - L152 were not covered by tests
# 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
)
Expand Down Expand Up @@ -307,48 +327,43 @@ def action_payslip_done(self):
)
line_ids.append(adjust_debit)
if len(line_ids) > 0:
if len(self) == 1 or self.env.company.action_group_payslips is not True:
move_lines = line_ids
first_slip = False
if len(self) == 1 or not self.env.company.action_group_payslips:
# We do a single account move
name = _("Payslip of %s") % (slip.employee_id.name)
move_dict = {
"narration": name,
"ref": slip.number,
"journal_id": slip.journal_id.id,
"date": date,
}
move_dict["line_ids"] = line_ids
move_slip = self.env["account.move"].create(move_dict)
slip.write({"move_id": move_slip.id})
move_slip.action_post()

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)
# We want to merge account entries in a single account move
if not move:
move = self.env["account.move"].create({})
move_lines = self._merge_move_lines(move_lines, line_ids)
slip.write({"move_id": move.id})

Check warning on line 349 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L347-L349

Added lines #L347 - L349 were not covered by tests
else:
logger.warning(
f"Payslip {slip.number} did not generate any account move lines"
)
# 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()
res = super(HrPayslip, slip).action_payslip_done()

# Add account move lines into the global account move
if len(self) > 1 and self.env.company.action_group_payslips:
move_dict = {

Check warning on line 358 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L358

Added line #L358 was not covered by tests
"narration": _("Payslips of %s") % (move_date),
"journal_id": move_journal,
"date": move_date,
}
move_dict["line_ids"] = move_lines
move.write(move_dict)
move.action_post()

Check warning on line 365 in payroll_account/models/hr_payroll_account.py

View check run for this annotation

Codecov / codecov/patch

payroll_account/models/hr_payroll_account.py#L363-L365

Added lines #L363 - L365 were not covered by tests
return res


class HrSalaryRule(models.Model):
Expand Down
2 changes: 2 additions & 0 deletions payroll_account/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* Odoo SA <[email protected]>
* Saran Lim. <[email protected]>
* Moana FABER <[email protected]>
* Cyril VINH-TUNG <[email protected]>
4 changes: 3 additions & 1 deletion payroll_account/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,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:9c9b1be50d9942288d682bac171b52d2fb61bd6904723f05ae3c833bdd20bdfc
!! source digest: sha256:5bdfba50457cc2a2920d5ffdf3c32cd43ab3533339bedf79c110b47f3ef95a15
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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 Expand Up @@ -413,6 +413,8 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Odoo SA &lt;<a class="reference external" href="mailto:info&#64;odoo.com">info&#64;odoo.com</a>&gt;</li>
<li>Saran Lim. &lt;<a class="reference external" href="mailto:saranl&#64;ecosoft.co.th">saranl&#64;ecosoft.co.th</a>&gt;</li>
<li>Moana FABER &lt;<a class="reference external" href="mailto:moana.faber&#64;hotmail.com">moana.faber&#64;hotmail.com</a>&gt;</li>
<li>Cyril VINH-TUNG &lt;<a class="reference external" href="mailto:cyril&#64;invitu.com">cyril&#64;invitu.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down

0 comments on commit 560edf1

Please sign in to comment.