1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (c) 2012 Cubic ERP - Teradata SAC. (http://cubicerp.com).
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.
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.
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/>.
20
##############################################################################
22
from osv import fields, osv
24
from tools.translate import _
26
_logger = logging.getLogger(__name__)
29
class delivery_driver(osv.osv):
30
_name='delivery.driver'
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'),
46
class delivery_time(osv.osv):
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'),
64
class delivery_carrier(osv.osv):
65
_name = "delivery.carrier"
66
_inherit = "delivery.carrier"
69
'driver_ids' : fields.one2many('delivery.driver','carrier_id','Delivery Drivers'),
74
#TODO: Add vehicle class (usually from maintenance module)
75
class delivery_route(osv.osv):
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)
84
_name='delivery.route'
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([
92
('departure','Departure'),
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'),
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)
111
self.write(cr, uid, ids, {'state': 'draft'}, context=context)
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)
120
self.write(cr, uid, ids, {'state': 'departure','departure_date':time.strftime('%Y-%m-%d %H:%M:%S')}, context=context)
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)
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)
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)
142
self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
148
class delivery_route_line(osv.osv):
149
_name='delivery.route.line'
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([
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'),
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)
183
def action_draft(self, cr, uid, ids, context=None):
184
self.write(cr, uid, ids, {'state': 'draft'}, context=context)
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)
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)
200
self.write(cr, uid, ids, {'state': 'delivered'}, context=context)
203
def action_received(self, cr, uid, ids, context=None):
204
self.write(cr, uid, ids, {'state': 'received'}, context=context)
207
def action_returned(self, cr, uid, ids, context=None):
208
self.write(cr, uid, ids, {'state': 'returned'}, context=context)
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)
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)
219
self.write(cr, uid, ids, {'state': 'cancel'}, context=context)
222
delivery_route_line()
224
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: