~vauxoo/addons-vauxoo/6.0-trunk

« back to all changes in this revision

Viewing changes to hr_payslip_paid/model/hr_payroll.py

  • Committer: Jose Morales
  • Date: 2014-07-28 20:00:11 UTC
  • mfrom: (543.7.551 7.0-addons-vauxoo)
  • Revision ID: jose@vauxoo.com-20140728200011-csytovehrzwp24lr
[FORWARD PORT] 7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: 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
#
 
26
from openerp.osv import fields, osv
 
27
from openerp import netsvc
 
28
 
 
29
class hr_payslip(osv.osv):
 
30
    _inherit = 'hr.payslip'
 
31
    
 
32
    def _compute_lines(self, cr, uid, ids, name, args, context=None):
 
33
        result = {}
 
34
        for payslip in self.browse(cr, uid, ids, context=context):
 
35
            src = []
 
36
            lines = []
 
37
            if payslip.move_id:
 
38
                for p in payslip.move_id.line_id:
 
39
                    temp_lines = []
 
40
                    if p.reconcile_id:
 
41
                        temp_lines = map(lambda x: x.id, p.reconcile_id.line_id)
 
42
                    elif p.reconcile_partial_id:
 
43
                        temp_lines = map(lambda x: x.id, p.reconcile_partial_id.line_partial_ids)
 
44
                    lines += [x for x in temp_lines if x not in lines]
 
45
                    src.append(p.id)
 
46
 
 
47
            lines = filter(lambda x: x not in src, lines)
 
48
            result[payslip.id] = lines
 
49
        return result
 
50
    
 
51
    def _reconciled(self, cr, uid, ids, name, args, context=None):
 
52
        res = {}
 
53
        wf_service = netsvc.LocalService("workflow")
 
54
        for pay in self.browse(cr, uid, ids, context=context):
 
55
            res[pay.id] = self.test_paid(cr, uid, [pay.id])
 
56
            if not res[pay.id] and pay.state == 'paid':
 
57
                wf_service.trg_validate(uid, 'hr.payslip', pay.id, 'open_test', cr)
 
58
        return res
 
59
    
 
60
    def action_done_paid(self, cr, uid, ids, context=None):
 
61
        return self.write(cr, uid, ids, {'state': 'done'}, context=context)    
 
62
            
 
63
    def _get_payslip_from_line(self, cr, uid, ids, context=None):
 
64
        move = {}
 
65
        for line in self.pool.get('account.move.line').browse(cr, uid, ids, context=context):
 
66
            if line.reconcile_partial_id:
 
67
                for line2 in line.reconcile_partial_id.line_partial_ids:
 
68
                    move[line2.move_id.id] = True
 
69
            if line.reconcile_id:
 
70
                for line2 in line.reconcile_id.line_id:
 
71
                    move[line2.move_id.id] = True
 
72
        payslip_ids = []
 
73
        if move:
 
74
            payslip_ids = self.pool.get('hr.payslip').search(cr, uid, [('move_id', 'in', move.keys())], context=context)
 
75
        return payslip_ids
 
76
        
 
77
    def _get_payslip_from_reconcile(self, cr, uid, ids, context=None):
 
78
        move = {}
 
79
        for r in self.pool.get('account.move.reconcile').browse(cr, uid, ids, context=context):
 
80
            for line in r.line_partial_ids:
 
81
                move[line.move_id.id] = True
 
82
            for line in r.line_id:
 
83
                move[line.move_id.id] = True
 
84
 
 
85
        payslip_ids = []
 
86
        if move:
 
87
            payslip_ids = self.pool.get('hr.payslip').search(cr, uid, [('move_id', 'in', move.keys())], context=context)
 
88
        return payslip_ids
 
89
 
 
90
    _columns = {
 
91
        'state': fields.selection([
 
92
                    ('draft', 'Draft'),
 
93
                    ('verify', 'Waiting'),
 
94
                    ('done', 'Done'),
 
95
                    ('cancel', 'Rejected'),
 
96
                    ('paid', 'Paid'),
 
97
                ], 'Status', select=True, readonly=True,
 
98
                    help='* When the payslip is created the status is \'Draft\'.\
 
99
                    \n* If the payslip is under verification, the status is \'Waiting\'. \
 
100
                    \n* If the payslip is confirmed then status is set to \'Done\'.\
 
101
                    \n* When user cancel payslip the status is \'Rejected\'.\
 
102
                    \n* When the payment is done the status id \'Paid\'.'),
 
103
        'payment_ids': fields.function(_compute_lines, relation='account.move.line',
 
104
            type="many2many", string='Payments'),
 
105
        'reconciled': fields.function(_reconciled, string='Paid/Reconciled', type='boolean',
 
106
            store={
 
107
                'hr.payslip': (lambda self, cr, uid, ids, c={}: ids, [], 50),
 
108
                'account.move.line': (_get_payslip_from_line, None, 50),
 
109
                'account.move.reconcile': (_get_payslip_from_reconcile, None, 50),},
 
110
            help="It indicates that the payslip has been paid and the journal entry of the payslip has been reconciled with one or several journal entries of payment."),
 
111
        }
 
112
        
 
113
    def move_line_id_payment_get(self, cr, uid, ids, *args):
 
114
        if not ids: return []
 
115
        result = self.move_line_id_payment_gets(cr, uid, ids, *args)
 
116
        return result.get(ids[0], [])
 
117
 
 
118
    def move_line_id_payment_gets(self, cr, uid, ids, *args):
 
119
        res = {}
 
120
        if not ids: return res
 
121
        payslip_bwr = self.browse(cr, uid, ids[0])
 
122
        account_ids = []
 
123
        for x in payslip_bwr.details_by_salary_rule_category:
 
124
            if x.salary_rule_id.account_credit.id:
 
125
                account_ids.append(x.salary_rule_id.account_credit.id)
 
126
        if not account_ids: return res
 
127
        cr.execute('SELECT i.id, l.id '\
 
128
                   'FROM account_move_line l '\
 
129
                   'LEFT JOIN hr_payslip i ON (i.move_id=l.move_id) '\
 
130
                   'WHERE i.id IN %s '\
 
131
                   'AND l.account_id=%s',
 
132
                   (tuple(ids), account_ids[0]))
 
133
        for r in cr.fetchall():
 
134
            res.setdefault(r[0], [])
 
135
            res[r[0]].append( r[1] )
 
136
        return res
 
137
        
 
138
    def test_paid(self, cr, uid, ids, *args):
 
139
        res = self.move_line_id_payment_get(cr, uid, ids)
 
140
        if not res:
 
141
            return False
 
142
        ok = True
 
143
        for id in res:
 
144
            cr.execute('select reconcile_id from account_move_line where id=%s', (id,))
 
145
            ok = ok and  bool(cr.fetchone()[0])
 
146
        return ok
 
147
        
 
148
    def done_paid(self, cr, uid, ids, context=None):
 
149
        """
 
150
        This function is called by a webservice for update the payslip that were created 
 
151
        before to added the state paid.
 
152
        """
 
153
        wf_service = netsvc.LocalService("workflow")
 
154
        for id_ in ids:
 
155
            wf_service.trg_write(uid, 'hr.payslip', id_, cr)
 
156
        return True