summaryrefslogtreecommitdiff
path: root/ohrms_loan/models/hr_payroll.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 17:14:58 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 17:14:58 +0700
commit1ca3b3df3421961caec3b747a364071c80f5c7da (patch)
tree6778a1f0f3f9b4c6e26d6d87ccde16e24da6c9d6 /ohrms_loan/models/hr_payroll.py
parentb57188be371d36d96caac4b8d65a40745c0e972c (diff)
initial commit
Diffstat (limited to 'ohrms_loan/models/hr_payroll.py')
-rw-r--r--ohrms_loan/models/hr_payroll.py79
1 files changed, 79 insertions, 0 deletions
diff --git a/ohrms_loan/models/hr_payroll.py b/ohrms_loan/models/hr_payroll.py
new file mode 100644
index 0000000..7e2b645
--- /dev/null
+++ b/ohrms_loan/models/hr_payroll.py
@@ -0,0 +1,79 @@
+# -*- coding: utf-8 -*-
+import time
+import babel
+from odoo import models, fields, api, tools, _
+from datetime import datetime
+
+
+class HrPayslipInput(models.Model):
+ _inherit = 'hr.payslip.input'
+
+ loan_line_id = fields.Many2one('hr.loan.line', string="Loan Installment", help="Loan installment")
+
+
+class HrPayslip(models.Model):
+ _inherit = 'hr.payslip'
+
+ @api.onchange('employee_id', 'date_from', 'date_to')
+ def onchange_employee(self):
+ if (not self.employee_id) or (not self.date_from) or (not self.date_to):
+ return
+
+ employee = self.employee_id
+ date_from = self.date_from
+ date_to = self.date_to
+ contract_ids = []
+
+ ttyme = datetime.fromtimestamp(time.mktime(time.strptime(str(date_from), "%Y-%m-%d")))
+ locale = self.env.context.get('lang') or 'en_US'
+ self.name = _('Salary Slip of %s for %s') % (
+ employee.name, tools.ustr(babel.dates.format_date(date=ttyme, format='MMMM-y', locale=locale)))
+ self.company_id = employee.company_id
+
+ if not self.env.context.get('contract') or not self.contract_id:
+ contract_ids = self.get_contract(employee, date_from, date_to)
+ if not contract_ids:
+ return
+ self.contract_id = self.env['hr.contract'].browse(contract_ids[0])
+
+ if not self.contract_id.struct_id:
+ return
+ self.struct_id = self.contract_id.struct_id
+
+ # computation of the salary input
+ contracts = self.env['hr.contract'].browse(contract_ids)
+ worked_days_line_ids = self.get_worked_day_lines(contracts, date_from, date_to)
+ worked_days_lines = self.worked_days_line_ids.browse([])
+ for r in worked_days_line_ids:
+ worked_days_lines += worked_days_lines.new(r)
+ self.worked_days_line_ids = worked_days_lines
+ if contracts:
+ input_line_ids = self.get_inputs(contracts, date_from, date_to)
+ input_lines = self.input_line_ids.browse([])
+ for r in input_line_ids:
+ input_lines += input_lines.new(r)
+ self.input_line_ids = input_lines
+ return
+
+ def get_inputs(self, contract_ids, date_from, date_to):
+ """This Compute the other inputs to employee payslip.
+ """
+ res = super(HrPayslip, self).get_inputs(contract_ids, date_from, date_to)
+ contract_obj = self.env['hr.contract']
+ emp_id = contract_obj.browse(contract_ids[0].id).employee_id
+ lon_obj = self.env['hr.loan'].search([('employee_id', '=', emp_id.id), ('state', '=', 'approve')])
+ for loan in lon_obj:
+ for loan_line in loan.loan_lines:
+ if date_from <= loan_line.date <= date_to and not loan_line.paid:
+ for result in res:
+ if result.get('code') == 'LO':
+ result['amount'] = loan_line.amount
+ result['loan_line_id'] = loan_line.id
+ return res
+
+ def action_payslip_done(self):
+ for line in self.input_line_ids:
+ if line.loan_line_id:
+ line.loan_line_id.paid = True
+ line.loan_line_id.loan_id._compute_loan_amount()
+ return super(HrPayslip, self).action_payslip_done()