~romaindeheele/openobject-addons/extra-trunk

« back to all changes in this revision

Viewing changes to delivery_routes/delivery.py

  • Committer: Yury Tello
  • Date: 2012-06-21 19:40:45 UTC
  • Revision ID: ytello@cubicerp.com-20120621194045-9tdebiglll7s3g2i
[ADD] delivery_routes and l10n_pe_toponyms

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) 2012 Cubic ERP - Teradata SAC. (http://cubicerp.com).
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU Affero General Public License as
 
9
#    published by the Free Software Foundation, either version 3 of the
 
10
#    License, or (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 Affero General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU Affero General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
 
19
#
 
20
##############################################################################
 
21
 
 
22
from osv import fields, osv
 
23
import time
 
24
from tools.translate import _
 
25
import logging
 
26
_logger = logging.getLogger(__name__)
 
27
 
 
28
 
 
29
class delivery_driver(osv.osv):
 
30
    _name='delivery.driver'
 
31
    _columns = {
 
32
            'partner_id': fields.many2one('res.partner','Partner',help='Fill this field if the driver is a outsourcing of the company'),
 
33
            'employee_id': fields.many2one('hr.employee','Employee',help='Fill this if the driver is a employee of the company'),
 
34
            'name': fields.char('Name', size=64, required=True),
 
35
            'carrier_id': fields.many2one('delivery.carrier','Carrier'),
 
36
            'outsourcing': fields.boolean('Outsourcing'),
 
37
            'route_ids': fields.one2many('delivery.route','driver_id','Delivery Routes'),
 
38
        }
 
39
    _defaults = {
 
40
            'outsourcing': False,
 
41
        }
 
42
 
 
43
delivery_driver()
 
44
 
 
45
 
 
46
class delivery_time(osv.osv):
 
47
    _name='delivery.time'
 
48
    _columns = {
 
49
            'sequence': fields.integer('Sequence'),
 
50
            'name': fields.char('Name', size=64, required=True),
 
51
            'start_hour': fields.selection(([(str(x),str(x)) for x in range(0,24)] + [('-','--')]),'Start Hour'),
 
52
            'start_minute': fields.selection(([(str(x*5),str(x*5)) for x in range(0,12)] + [('-','--')]),'Start Minute'),
 
53
            'end_hour': fields.selection(([(str(x),str(x)) for x in range(0,24)] + [('-','--')]),'End Hour'),
 
54
            'end_minute': fields.selection(([(str(x*5),str(x*5)) for x in range(0,12)] + [('-','--')]),'End Minute'),
 
55
            'active': fields.boolean('Active'),
 
56
        }
 
57
    _defaults = {
 
58
            'active': True,
 
59
        }
 
60
 
 
61
delivery_time()
 
62
 
 
63
 
 
64
class delivery_carrier(osv.osv):
 
65
    _name = "delivery.carrier"
 
66
    _inherit = "delivery.carrier"
 
67
 
 
68
    _columns = {
 
69
            'driver_ids' : fields.one2many('delivery.driver','carrier_id','Delivery Drivers'),
 
70
        }
 
71
 
 
72
delivery_carrier()
 
73
 
 
74
#TODO: Add vehicle class (usually from maintenance module)
 
75
class delivery_route(osv.osv):
 
76
    
 
77
    def create(self, cr, user, vals, context=None):
 
78
        if ('name' not in vals) or (vals.get('name')=='/'):
 
79
            seq_obj_name =  'delivery.route'
 
80
            vals['name'] = self.pool.get('ir.sequence').get(cr, user, seq_obj_name)
 
81
        new_id = super(delivery_route, self).create(cr, user, vals, context)
 
82
        return new_id
 
83
    
 
84
    _name='delivery.route'
 
85
    _columns = {
 
86
            'name': fields.char('Reference', size=64, required=True, select=True),
 
87
            'date': fields.date('Date', required=True, select=True),
 
88
            'time_id': fields.many2one('delivery.time','Delivery Time', select=True),
 
89
            'driver_id': fields.many2one('delivery.driver','Delivery Driver', required=True),
 
90
            'state': fields.selection([
 
91
                                ('draft','Draft'),
 
92
                                ('departure','Departure'),
 
93
                                ('arrive','Arrive'),
 
94
                                ('done', 'Done'),
 
95
                                ('cancel','Cancel')],'State',readonly=True),
 
96
            'line_ids': fields.one2many('delivery.route.line','route_id','Lines',required=True),
 
97
            'departure_date': fields.datetime('Departure Date'),
 
98
            'arrive_date': fields.datetime('Arrive Date'),
 
99
        }
 
100
    _defaults = {
 
101
            'state': 'draft',
 
102
            'name': '/'
 
103
        }
 
104
        
 
105
    def action_draft(self, cr, uid, ids, context=None):
 
106
        line_obj = self.pool.get('delivery.route.line')
 
107
        for route in self.browse(cr,uid,ids,context=context):
 
108
            for line in route.line_ids:
 
109
                line_obj.action_draft(cr,uid,[line.id],context=context)
 
110
            
 
111
        self.write(cr, uid, ids, {'state': 'draft'}, context=context)
 
112
        return True
 
113
        
 
114
    def action_departure(self, cr, uid, ids, context=None):
 
115
        line_obj = self.pool.get('delivery.route.line')
 
116
        for route in self.browse(cr,uid,ids,context=context):
 
117
            for line in route.line_ids:
 
118
                line_obj.action_delivered(cr,uid,[line.id],context=context)
 
119
            
 
120
        self.write(cr, uid, ids, {'state': 'departure','departure_date':time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
 
121
        return True
 
122
        
 
123
    def action_arrive(self, cr, uid, ids, context=None):
 
124
        self.write(cr, uid, ids, {'state': 'arrive','arrive_date':time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
 
125
        return True
 
126
        
 
127
    def action_done(self, cr, uid, ids, context=None):
 
128
        line_obj = self.pool.get('delivery.route.line')
 
129
        for route in self.browse(cr,uid,ids,context=context):
 
130
            for line in route.line_ids:
 
131
                if line.state in ('draft','delivered'):
 
132
                    raise osv.except_osv(_('Error'), _("The lines of delivery route mustn't be draft or delivered"))
 
133
        self.write(cr, uid, ids, {'state': 'done'}, context=context)
 
134
        return True
 
135
        
 
136
    def action_cancel(self, cr, uid, ids, context=None):
 
137
        line_obj = self.pool.get('delivery.route.line')
 
138
        for route in self.browse(cr,uid,ids,context=context):
 
139
            for line in route.line_ids:
 
140
                line_obj.action_cancel(cr,uid,[line.id],context=context)
 
141
            
 
142
        self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
 
143
        return True
 
144
        
 
145
delivery_route()
 
146
 
 
147
 
 
148
class delivery_route_line(osv.osv):
 
149
    _name='delivery.route.line'
 
150
    _columns = {
 
151
            'sequence': fields.integer('Sequence'),
 
152
            'route_id': fields.many2one('delivery.route','Delivery Route', required=True, ondelete="cascade"),
 
153
            'picking_id': fields.many2one('stock.picking','Picking', required=True,select=True),
 
154
            'address_id': fields.related('picking_id','address_id',type='many2one',relation='res.partner.address',string='Delivery Address'),
 
155
            'state': fields.selection([
 
156
                                ('draft','Draft'),
 
157
                                ('delivered','Delivered'),
 
158
                                ('received','Received'),
 
159
                                ('returned','Returned'),
 
160
                                ('cancel','Cancel')],'State',readonly=True),
 
161
            'visit_date': fields.datetime('Visit Date',states={'delivered': [('required', True)],
 
162
                                                                'received':[('readonly',True)],
 
163
                                                                'returned':[('readonly',True)],}),
 
164
            'visit_note': fields.char('Visit Note',size=256,
 
165
                                states={'delivered': [('required', True)],
 
166
                                                                'received':[('readonly',True)],
 
167
                                                                'returned':[('readonly',True)],},
 
168
                                help="Fill field with the name of the person that receive the delivery or the reason for doesn't receive it."),
 
169
            'note': fields.text('Notes'),
 
170
        }
 
171
    _defaults = {
 
172
            'state': 'draft',
 
173
        }
 
174
    _order = 'sequence'
 
175
 
 
176
    def unlink(self, cr, uid, ids, context=None):
 
177
        for o in self.browse(cr, uid, ids, context=context):
 
178
            if o.state not in ('draft', 'cancel'):
 
179
                raise osv.except_osv(_('Invalid action !'), 
 
180
                        _('Cannot delete Delivery Route Line(s) which are already received, returned or delivered !'))
 
181
        return super(delivery_route_line, self).unlink(cr, uid, ids, context=context)
 
182
 
 
183
    def action_draft(self, cr, uid, ids, context=None):
 
184
        self.write(cr, uid, ids, {'state': 'draft'}, context=context)
 
185
        return True
 
186
 
 
187
    def action_delivered_do_line(self, cr, uid, line, context=None):
 
188
        self.pool.get('stock.picking').write(cr,uid,[line.picking_id.id],{'delivered':True},context=context)
 
189
        return True
 
190
        
 
191
    def action_delivered(self, cr, uid, ids, context=None):
 
192
        picking_obj = self.pool.get('stock.picking')
 
193
        for line in self.browse(cr,uid,ids,context=context):
 
194
            if line.picking_id.delivered:
 
195
                raise osv.except_osv(_('Error'), _('The picking %s (origin:%s) was delivered in other delivery route'%(line.picking_id.name,line.picking_id.origin)))
 
196
            if line.picking_id.state not in ('done'):
 
197
                raise osv.except_osv(_('Error'), _('The picking %s (origin:%s) must be in done state'%(line.picking_id.name,line.picking_id.origin)))
 
198
            self.action_delivered_do_line(cr, uid, line, context=context)
 
199
            
 
200
        self.write(cr, uid, ids, {'state': 'delivered'}, context=context)
 
201
        return True
 
202
        
 
203
    def action_received(self, cr, uid, ids, context=None):
 
204
        self.write(cr, uid, ids, {'state': 'received'}, context=context)
 
205
        return True
 
206
        
 
207
    def action_returned(self, cr, uid, ids, context=None):
 
208
        self.write(cr, uid, ids, {'state': 'returned'}, context=context)
 
209
        return True
 
210
        
 
211
    def action_cancel_do_line(self, cr, uid, line, context=None):
 
212
        self.pool.get('stock.picking').write(cr,uid,line.picking_id.id,{'delivered':False},context=context)
 
213
        return True
 
214
        
 
215
    def action_cancel(self, cr, uid, ids, context=None):
 
216
        for line in self.browse(cr,uid,ids,context=context):
 
217
            self.action_cancel_do_line(cr, uid, line, context=context)
 
218
            
 
219
        self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
 
220
        return True
 
221
 
 
222
delivery_route_line()
 
223
 
 
224
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: