~mathieu-julius/extra-service-job/trunk-1

« back to all changes in this revision

Viewing changes to service_job/partner_contract.py

  • Committer: Mathieu VATEL
  • Date: 2012-10-17 16:19:26 UTC
  • Revision ID: mathieu@julius.fr-20121017161926-qwtbi8hmp6u32ag9
[RMV] - remove none use file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
#################################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2011 Julius Network Solutions SARL <contact@julius.fr>
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU General Public License as published by
9
 
#    the Free Software Foundation, either version 3 of the License, or
10
 
#    (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
#################################################################################
21
 
 
22
 
import datetime
23
 
from osv import fields, osv
24
 
from tools.translate import _
25
 
 
26
 
class partner_contract(osv.osv):
27
 
    _inherit = "contract.contract"
28
 
    _description = "Parnter Contract"
29
 
    
30
 
    """This function computes the month prices and worked hours for a contract"""
31
 
    def _package_compute(self, cr, uid, ids, name, args, context=None):
32
 
        res={}
33
 
        for contract in self.browse(cr,uid,ids, context=context):
34
 
            
35
 
            full_hours_worked=0
36
 
            duration = 0
37
 
            shared = 0
38
 
            presence = 0
39
 
            
40
 
            """If the contract is linked with another, the program takes the linked contract
41
 
            usual hours to compute shared hours."""
42
 
            if contract.contract_linked :
43
 
                usual_hours_link = contract.contract_linked.usual_hours_ids
44
 
            else :
45
 
                usual_hours_link=None
46
 
           
47
 
            for usual_hour in contract.usual_hours_ids :
48
 
                """for each usual hour in the contract, the program computes its duration, the responsible
49
 
                presence hours and the time shared between the current usual hour and the corresponding one
50
 
                in the linked contract."""
51
 
                temp = self._usual_hour(usual_hour,usual_hours_link)
52
 
                duration += temp['duration']
53
 
                shared += temp['shared']
54
 
                presence += usual_hour.responsible_hours
55
 
            
56
 
            """The times are working times are balanced with the responsible hours and shared hours rates."""
57
 
            if contract.contract_linked :
58
 
                full_hours_worked = duration - (1 - float(contract.contract_linked_rate) / 100) * shared
59
 
            else :
60
 
                full_hours_worked = duration -1.0 / 3 * presence
61
 
                
62
 
            monthly_package = (float(full_hours_worked) * float(contract.worked_weeks)/ 12)
63
 
            monthly_fee = monthly_package*contract.net_rate
64
 
            res[contract.id] = {
65
 
               'monthly_fee': monthly_fee,
66
 
               'monthly_package': monthly_package,
67
 
               'hours_per_week': full_hours_worked,
68
 
               }
69
 
        return res    
70
 
    
71
 
    def onchange_linked_contract(self, cr, uid, ids, worked_weeks, total_weeks, net_rate,name, contract_linked, contract_linked_rate):
72
 
        self.modif_linked_contract(cr, uid, ids, contract_linked, contract_linked_rate)
73
 
        return self.onchange_compute(cr, uid, ids, worked_weeks, total_weeks, net_rate, name, contract_linked, contract_linked_rate)
74
 
 
75
 
    def onchange_linked_contract_rate(self, cr, uid, ids, worked_weeks, total_weeks, net_rate,name, contract_linked, contract_linked_rate):
76
 
        for id in ids:
77
 
            cr.execute('''update partner_contract set contract_linked_rate=100-%s where contract_linked = %s''' % (contract_linked_rate,id))
78
 
        return self.onchange_compute(cr, uid, ids, worked_weeks, total_weeks, net_rate, name, contract_linked, contract_linked_rate)
79
 
    
80
 
    """This function returns for an usual_hours A and a list of usual_hour, the duration of A and the time shared between A and the list"""
81
 
    def _usual_hour(self,usual_hour,susual_hours):
82
 
    
83
 
        begin = usual_hour.hour_from
84
 
        end = usual_hour.hour_to
85
 
        day_of_week = usual_hour.day_of_week
86
 
        shared = 0
87
 
        presence = usual_hour.responsible_hours
88
 
        duration = end-begin
89
 
        
90
 
        if susual_hours :
91
 
            for susual_hour in susual_hours :
92
 
                if susual_hour.day_of_week == day_of_week and susual_hour.hour_from <= end and susual_hour.hour_to>= begin :
93
 
                    shared += min(susual_hour.hour_to,end)-max(susual_hour.hour_from,begin)
94
 
                    presence=0
95
 
        
96
 
        return {'shared' : shared, 'duration' : duration, 'presence' : presence}
97
 
    
98
 
    def _get_period_ids(self, cr, uid, ids, field_name, arg, context={}):
99
 
        result = {}
100
 
        period_obj = self.pool.get('service.period')
101
 
        for record in self.browse(cr, uid, ids, context):
102
 
            read = self.read(cr, uid, [record.id],['partner_id'])[0]
103
 
            if read['partner_id']:
104
 
                period_ids = period_obj.search(cr, uid, [
105
 
                                                         ('partner_id.id','=',read['partner_id'][0]),
106
 
                                                         ('type','=','partner'),
107
 
                                                         ])
108
 
                result[record.id] = period_ids
109
 
            else:
110
 
                result[record.id] = []
111
 
 
112
 
        return result
113
 
    
114
 
    _columns = {
115
 
        'user_id': fields.many2one('res.users', 'Manager'),
116
 
        'gross_rate':fields.float('Gross rate'),
117
 
        'net_rate':fields.float('Net rate'),
118
 
        'worked_weeks':fields.integer('Number of worked weeks', size=2),
119
 
        'sale_order_id': fields.many2one('sale.order', 'Quotation'),
120
 
        'monthly': fields.boolean('Monthly'),
121
 
        'employee_id': fields.many2many('hr.employee', 'worker_contract_rel', 'contract_id', 'employee_id', 'Workers'),
122
 
        'agency' : fields.many2one('res.partner.address', 'Agency'),
123
 
        'main_contract' : fields.boolean('Main contract'),
124
 
        'period_ids' : fields.function(_get_period_ids, method=True, type='many2many', relation="service.period", string="Periods"),
125
 
    }
126
 
 
127
 
    _defaults = {
128
 
        'user_id': lambda obj, cr, uid, context: uid,
129
 
        'active': lambda *a: True
130
 
    }
131
 
 
132
 
partner_contract()
133
 
 
134
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:heures effectives / semaine x Nbre semaines travaillees / an / 12