~wowas18/openeam/online

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2013 CodUP (<http://codup.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
import calendar

from openerp.osv import fields, osv

class mro_order(osv.osv):
    _inherit = 'mro.order'
    
    MAINTENANCE_TYPE_SELECTION = [
        ('bm', 'Breakdown'),
        ('cm', 'Corrective'),
        ('pm', 'Preventive')
    ]
    
    _columns = {
        'maintenance_type': fields.selection(MAINTENANCE_TYPE_SELECTION, 'Maintenance Type', required=True, readonly=True, states={'draft': [('readonly', False)]}),
    }
    
    def find_step(self, start, end, tmin, tmax):
        M = round(2*(end - start)/(tmin + tmax),0)
        if M != 0:
            step = (end - start)/M
            if step < tmin:
                M = M - 1
                if M != 0:
                    step = (end - start)/M
                if step < tmin or step > tmax: step = tmin
            elif step > tmax:
                M = M + 1
                step = (end - start)/M
                if step < tmin or step > tmax: step = tmax
        else: step = tmin
        return step
    
    def replan_pm(self, cr, uid, context=None):
        rule_obj = self.pool.get('mro.pm.rule')
        meter_obj = self.pool.get('mro.pm.meter')
        ids = rule_obj.search(cr, uid, [])
        for rule in rule_obj.browse(cr,uid,ids,context=context):
            tasks = rule.pm_rules_line_ids
            if rule.meter_id.state != 'reading' or not len(tasks):
                continue
            tasks.sort(lambda y,x: cmp(x.meter_interval_id.interval_max, y.meter_interval_id.interval_max))
            K = 3600.0*24
            hf = len(tasks)-1
            lf = 0
            task_ids = []
            Ci = []
            Imin = []
            Imax = []
            Si = []
            Dmin = []
            Dmax = []
            Dopt = []
            for task in tasks:
                task_ids.append(task.task_id.id)
                order_ids = self.search(cr, uid, 
                    [('asset_id', '=', rule.asset_id.id),
                    ('state', 'not in', ('draft','cancel')),
                    ('maintenance_type', '=', 'pm'),
                    ('task_id', 'in', task_ids)],
                    limit=1, order='date_execution desc')
                if len(order_ids) > 0:
                    date = self.browse(cr, uid, order_ids[0], context=context).date_execution
                    Ci.append(K*meter_obj.get_reading(cr, uid, rule.meter_id.id, date))
                else: Ci.append(0)
                Imin.append(K*task.meter_interval_id.interval_min)
                Imax.append(K*task.meter_interval_id.interval_max)
                Si.append(0)
                Dmin.append(0)
                Dmax.append(0)
                Dopt.append(0)
            C = K*rule.meter_id.total_value
            Dc = 1.0*calendar.timegm(time.strptime(rule.meter_id.date, "%Y-%m-%d"))
            N = rule.meter_id.utilization
            Hp = 3600.0*24*31*rule.horizon
            Dn = 1.0*calendar.timegm(time.strptime(time.strftime('%Y-%m-%d',time.gmtime()),"%Y-%m-%d"))
            Si[lf] = Imin[lf]
            for i in range(hf):
                Si[i+1] = self.find_step(Ci[i+1], Ci[i] + Si[i], Imin[i+1], Imax[i+1])
            for i in range(hf+1):
                Dmin[i] = Dc + (Imin[i] - C + Ci[i])/N
                Dmax[i] = Dmin[i] + (Imax[i] - Imin[i])/N
                Dopt[i] = Dc + (Si[i] - C + Ci[i])/N
            Dp = Dopt[hf]
            for i in range(hf):
                if Dp > Dmax[i]: Dp = Dmax[i]
            if Dp<Dn: Dp=Dn
            Cp = C + (Dp - Dc)*N
            delta = Cp - Ci[hf]
            order_ids = self.search(cr, uid, 
                [('asset_id', '=', rule.asset_id.id),
                ('state', '=', 'draft'),
                ('maintenance_type', '=', 'pm'),
                ('task_id', 'in', task_ids)],
                order='date_execution')
            for order in self.browse(cr, uid, order_ids, context=context):
                Tp = time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Dp))
                values = {
                    'date_planned':Tp,
                    'date_scheduled':Tp,
                    'date_execution':Tp,
                    'origin': rule.name,
                    'state': 'draft',
                    'maintenance_type': 'pm',
                    'asset_id': rule.asset_id.id,
                }
                task = tasks[hf].task_id
                Ci[hf] = Cp
                Si[hf] = self.find_step(Ci[hf], Ci[hf-1] + Si[hf-1], Imin[hf], Imax[hf])
                for i in range(hf):
                    if Dmin[i] < Dp + (Si[hf]-Imax[i]+Imin[i])/N:
                        task = tasks[i].task_id
                        for j in range(hf-i):
                            Ci[i+j] = Cp
                        for j in range(hf-i):
                            Si[i+j] = self.find_step(Ci[i+j], Ci[i+j-1] + Si[i+j-1], Imin[i+j], Imax[i+j])
                            Dmin[i+j] = Dp + Imin[i+j]/N
                            Dmax[i+j] = Dp + Imax[i+j]/N
                            Dopt[i+j] = Dp + Si[i+j]/N
                        break
                Si[hf] = self.find_step(Ci[hf], Ci[hf-1] + Si[hf-1], Imin[hf], Imax[hf])
                Dmin[hf] = Dp + Imin[hf]/N
                Dmax[hf] = Dp + Imax[hf]/N
                Dopt[hf] = Dp + Si[hf]/N
                values['task_id'] = task.id
                values['description'] = task.name
                values['tools_description'] = task.tools_description
                values['labor_description'] = task.labor_description
                values['operations_description'] = task.operations_description
                values['documentation_description'] = task.documentation_description
                parts_lines = [[2,line.id] for line in order.parts_lines]
                for line in task.parts_lines:
                    parts_lines.append([0,0,{
                        'name': line.name,
                        'parts_id': line.parts_id.id,
                        'parts_qty': line.parts_qty,
                        'parts_uom': line.parts_uom.id,
                        }])
                values['parts_lines'] = parts_lines
                self.write(cr, uid, [order.id], values)
                Dp = Dopt[hf]
                for i in range(hf):
                    if Dp > Dmax[i]: Dp = Dmax[i]
                Co = Cp
                Cp = C + (Dp - Dc)*N
                delta = Cp - Co
            Dhp = Dn + Hp
            while Dp < Dhp:
                Tp = time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Dp))
                values = {
                    'date_planned':Tp,
                    'date_scheduled':Tp,
                    'date_execution':Tp,
                    'origin': rule.name,
                    'state': 'draft',
                    'maintenance_type': 'pm',
                    'asset_id': rule.asset_id.id,
                }
                task = tasks[hf].task_id
                Ci[hf] = Cp
                Si[hf] = self.find_step(Ci[hf], Ci[hf-1] + Si[hf-1], Imin[hf], Imax[hf])
                for i in range(hf):
                    if Dmin[i] < Dp + (Si[hf]-Imax[i]+Imin[i])/N:
                        task = tasks[i].task_id
                        for j in range(hf-i):
                            Ci[i+j] = Cp
                        for j in range(hf-i):
                            Si[i+j] = self.find_step(Ci[i+j], Ci[i+j-1] + Si[i+j-1], Imin[i+j], Imax[i+j])
                            Dmin[i+j] = Dp + Imin[i+j]/N
                            Dmax[i+j] = Dp + Imax[i+j]/N
                            Dopt[i+j] = Dp + Si[i+j]/N
                        break
                Si[hf] = self.find_step(Ci[hf], Ci[hf-1] + Si[hf-1], Imin[hf], Imax[hf])
                Dmin[hf] = Dp + Imin[hf]/N
                Dmax[hf] = Dp + Imax[hf]/N
                Dopt[hf] = Dp + Si[hf]/N
                values['task_id'] = task.id
                values['description'] = task.name
                values['tools_description'] = task.tools_description
                values['labor_description'] = task.labor_description
                values['operations_description'] = task.operations_description
                values['documentation_description'] = task.documentation_description
                parts_lines = []
                for line in task.parts_lines:
                    parts_lines.append([0,0,{
                        'name': line.name,
                        'parts_id': line.parts_id.id,
                        'parts_qty': line.parts_qty,
                        'parts_uom': line.parts_uom.id,
                        }])
                values['parts_lines'] = parts_lines
                self.create(cr, uid, values)
                Dp = Dopt[hf]
                for i in range(hf):
                    if Dp > Dmax[i]: Dp = Dmax[i]
                Co = Cp
                Cp = C + (Dp - Dc)*N
                delta = Cp - Co
        return True
    
    
class mro_task(osv.osv):
    _inherit = 'mro.task'
    
    MAINTENANCE_TYPE_SELECTION = [
        ('cm', 'Corrective'),
        ('pm', 'Preventive')
    ]
    
    _columns = {
        'maintenance_type': fields.selection(MAINTENANCE_TYPE_SELECTION, 'Maintenance Type', required=True),
    }

    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: