1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# -*- coding: utf-8 -*-
import time
from odoo import models, api, fields
from odoo.exceptions import UserError
class HrLoanAcc(models.Model):
_inherit = 'hr.loan'
employee_account_id = fields.Many2one('account.account', string="Loan Account")
treasury_account_id = fields.Many2one('account.account', string="Treasury Account")
journal_id = fields.Many2one('account.journal', string="Journal")
state = fields.Selection([
('draft', 'Draft'),
('waiting_approval_1', 'Submitted'),
('waiting_approval_2', 'Waiting Approval'),
('approve', 'Approved'),
('refuse', 'Refused'),
('cancel', 'Canceled'),
], string="State", default='draft', track_visibility='onchange', copy=False, )
def action_approve(self):
"""This create account move for request.
"""
loan_approve = self.env['ir.config_parameter'].sudo().get_param('account.loan_approve')
contract_obj = self.env['hr.contract'].search([('employee_id', '=', self.employee_id.id)])
if not contract_obj:
raise UserError('You must Define a contract for employee')
if not self.loan_lines:
raise UserError('You must compute installment before Approved')
if loan_approve:
self.write({'state': 'waiting_approval_2'})
else:
if not self.employee_account_id or not self.treasury_account_id or not self.journal_id:
raise UserError("You must enter employee account & Treasury account and journal to approve ")
if not self.loan_lines:
raise UserError('You must compute Loan Request before Approved')
timenow = time.strftime('%Y-%m-%d')
for loan in self:
amount = loan.loan_amount
loan_name = loan.employee_id.name
reference = loan.name
journal_id = loan.journal_id.id
debit_account_id = loan.treasury_account_id.id
credit_account_id = loan.employee_account_id.id
debit_vals = {
'name': loan_name,
'account_id': debit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount > 0.0 and amount or 0.0,
'credit': amount < 0.0 and -amount or 0.0,
'loan_id': loan.id,
}
credit_vals = {
'name': loan_name,
'account_id': credit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount < 0.0 and -amount or 0.0,
'credit': amount > 0.0 and amount or 0.0,
'loan_id': loan.id,
}
vals = {
'name': 'Loan For' + ' ' + loan_name,
'narration': loan_name,
'ref': reference,
'journal_id': journal_id,
'date': timenow,
'line_ids': [(0, 0, debit_vals), (0, 0, credit_vals)]
}
move = self.env['account.move'].create(vals)
move.post()
self.write({'state': 'approve'})
return True
def action_double_approve(self):
"""This create account move for request in case of double approval.
"""
if not self.employee_account_id or not self.treasury_account_id or not self.journal_id:
raise UserError("You must enter employee account & Treasury account and journal to approve ")
if not self.loan_lines:
raise UserError('You must compute Loan Request before Approved')
timenow = time.strftime('%Y-%m-%d')
for loan in self:
amount = loan.loan_amount
loan_name = loan.employee_id.name
reference = loan.name
journal_id = loan.journal_id.id
debit_account_id = loan.treasury_account_id.id
credit_account_id = loan.employee_account_id.id
debit_vals = {
'name': loan_name,
'account_id': debit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount > 0.0 and amount or 0.0,
'credit': amount < 0.0 and -amount or 0.0,
'loan_id': loan.id,
}
credit_vals = {
'name': loan_name,
'account_id': credit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount < 0.0 and -amount or 0.0,
'credit': amount > 0.0 and amount or 0.0,
'loan_id': loan.id,
}
vals = {
'name': 'Loan For' + ' ' + loan_name,
'narration': loan_name,
'ref': reference,
'journal_id': journal_id,
'date': timenow,
'line_ids': [(0, 0, debit_vals), (0, 0, credit_vals)]
}
move = self.env['account.move'].create(vals)
move.post()
self.write({'state': 'approve'})
return True
class HrLoanLineAcc(models.Model):
_inherit = "hr.loan.line"
def action_paid_amount(self):
"""This create the account move line for payment of each installment.
"""
timenow = time.strftime('%Y-%m-%d')
for line in self:
if line.loan_id.state != 'approve':
raise UserError("Loan Request must be approved")
amount = line.amount
loan_name = line.employee_id.name
reference = line.loan_id.name
journal_id = line.loan_id.journal_id.id
debit_account_id = line.loan_id.employee_account_id.id
credit_account_id = line.loan_id.treasury_account_id.id
debit_vals = {
'name': loan_name,
'account_id': debit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount > 0.0 and amount or 0.0,
'credit': amount < 0.0 and -amount or 0.0,
}
credit_vals = {
'name': loan_name,
'account_id': credit_account_id,
'journal_id': journal_id,
'date': timenow,
'debit': amount < 0.0 and -amount or 0.0,
'credit': amount > 0.0 and amount or 0.0,
}
vals = {
'name': 'Loan For' + ' ' + loan_name,
'narration': loan_name,
'ref': reference,
'journal_id': journal_id,
'date': timenow,
'line_ids': [(0, 0, debit_vals), (0, 0, credit_vals)]
}
move = self.env['account.move'].create(vals)
move.post()
return True
class HrPayslipAcc(models.Model):
_inherit = 'hr.payslip'
def action_payslip_done(self):
for line in self.input_line_ids:
if line.loan_line_id:
line.loan_line_id.action_paid_amount()
return super(HrPayslipAcc, self).action_payslip_done()
|