hr_payroll.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # coding: utf-8
  2. #
  3. # Module Writen to OpenERP, Open Source Management Solution
  4. #
  5. # Copyright (c) 2014 Vauxoo - http://www.vauxoo.com/
  6. # All Rights Reserved.
  7. # info Vauxoo (info@vauxoo.com)
  8. #
  9. # Coded by: Luis Torres (luis_t@vauxoo.com)
  10. #
  11. #
  12. # This program is free software: you can redistribute it and/or modify
  13. # it under the terms of the GNU Affero General Public License as
  14. # published by the Free Software Foundation, either version 3 of the
  15. # License, or (at your option) any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU Affero General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU Affero General Public License
  23. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. #
  25. from openerp import models, fields, api, _
  26. class HrPayslip(models.Model):
  27. _inherit = 'hr.payslip'
  28. @api.one
  29. @api.depends(
  30. 'move_id.line_id.account_id',
  31. 'move_id.line_id.reconcile_id')
  32. def _compute_payments(self):
  33. lines = []
  34. if self.move_id:
  35. lines = []
  36. for p_line_id in self.move_id.line_id:
  37. temp_lines = []
  38. if p_line_id.reconcile_id:
  39. temp_lines = [
  40. line.id for line in p_line_id.reconcile_id.line_id
  41. if line != p_line_id]
  42. elif p_line_id.reconcile_partial_id:
  43. temp_lines = [
  44. line.id for line in
  45. p_line_id.reconcile_partial_id.line_partial_ids
  46. if line != p_line_id]
  47. lines += [x for x in temp_lines if x not in lines]
  48. self.payment_ids = lines
  49. @api.one
  50. @api.depends(
  51. 'move_id.line_id.account_id',
  52. 'move_id.line_id.reconcile_id')
  53. def _compute_reconciled(self):
  54. self.reconciled = self.test_paid()
  55. state = fields.Selection(
  56. selection_add=[('paid', _('Pagado'))],
  57. help="* When the payslip is created the status is 'Draft'.\
  58. \n* If the payslip is under verification, the status is 'Waiting'.\
  59. \n* If the payslip is confirmed then status is set to 'Done'.\
  60. \n* When user cancel payslip the status is 'Rejected'.\
  61. \n* When the payment is done the status id 'Paid'.")
  62. payment_ids = fields.Many2many(
  63. 'account.move.line', string='Payments', compute='_compute_payments')
  64. reconciled = fields.Boolean(
  65. string='Paid/Reconciled', store=True, readonly=True,
  66. compute='_compute_reconciled', help="It indicates that the payslip has"
  67. " been paid and the journal entry of the payslip has been reconciled "
  68. "with one or several journal entries of payment.")
  69. @api.multi
  70. def move_line_id_payment_get(self):
  71. # return the move line ids with the same account as the payroll self
  72. res = {}
  73. if not self.id:
  74. return res
  75. account_ids = []
  76. for det in self.details_by_salary_rule_category:
  77. if det.salary_rule_id.account_credit.id:
  78. account = det.salary_rule_id.account_credit
  79. if account.type == 'payable' and account.reconcile:
  80. account_ids.append(account.id)
  81. if not account_ids:
  82. return res
  83. query = '''SELECT i.id, l.id
  84. FROM account_move_line l
  85. LEFT JOIN hr_payslip i ON (i.move_id=l.move_id)
  86. WHERE i.id IN %s
  87. AND l.account_id=%s'''
  88. self._cr.execute(query, (tuple(self.ids), account_ids[0]))
  89. for result in self._cr.fetchall():
  90. res.setdefault(result[0], [])
  91. res[result[0]].append(result[1])
  92. return res.get(self.id, [])
  93. @api.multi
  94. def test_paid(self):
  95. # check whether all corresponding account move lines are reconciled
  96. line_ids = self.move_line_id_payment_get()
  97. if not line_ids:
  98. return False
  99. query = "SELECT reconcile_id FROM account_move_line WHERE id IN %s"
  100. self._cr.execute(query, (tuple(line_ids),))
  101. return all(row[0] for row in self._cr.fetchall())