1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 ##############################################################################
23 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
27 class sale_order_line(osv.Model):
28 _inherit = 'sale.order.line'
31 'is_delivery': fields.boolean("Is a Delivery"),
39 class sale_order(osv.Model):
40 _inherit = 'sale.order'
42 'carrier_id': fields.many2one(
43 "delivery.carrier", string="Delivery Method",
44 help="Complete this field if you plan to invoice the shipping based on picking."),
47 def onchange_partner_id(self, cr, uid, ids, part, context=None):
48 result = super(sale_order, self).onchange_partner_id(cr, uid, ids, part, context=context)
50 dtype = self.pool.get('res.partner').browse(cr, uid, part, context=context).property_delivery_carrier.id
51 # TDE NOTE: not sure the aded 'if dtype' is valid
53 result['value']['carrier_id'] = dtype
56 def _prepare_order_picking(self, cr, uid, order, context=None):
57 result = super(sale_order, self)._prepare_order_picking(cr, uid, order, context=context)
58 result.update(carrier_id=order.carrier_id.id)
61 def _delivery_unset(self, cr, uid, ids, context=None):
62 sale_obj = self.pool['sale.order.line']
63 line_ids = sale_obj.search(cr, uid, [('order_id', 'in', ids), ('is_delivery', '=', True)],context=context)
64 sale_obj.unlink(cr, uid, line_ids, context=context)
66 def delivery_set(self, cr, uid, ids, context=None):
67 line_obj = self.pool.get('sale.order.line')
68 grid_obj = self.pool.get('delivery.grid')
69 carrier_obj = self.pool.get('delivery.carrier')
70 acc_fp_obj = self.pool.get('account.fiscal.position')
71 self._delivery_unset(cr, uid, ids, context=context)
72 for order in self.browse(cr, uid, ids, context=context):
73 grid_id = carrier_obj.grid_get(cr, uid, [order.carrier_id.id], order.partner_shipping_id.id)
75 raise osv.except_osv(_('No Grid Available!'), _('No grid matching for this carrier!'))
77 if order.state != 'draft':
78 raise osv.except_osv(_('Order not in Draft State!'), _('The order state have to be draft to add delivery lines.'))
80 grid = grid_obj.browse(cr, uid, grid_id, context=context)
82 taxes = grid.carrier_id.product_id.taxes_id
83 fpos = order.fiscal_position or False
84 taxes_ids = acc_fp_obj.map_tax(cr, uid, fpos, taxes)
85 #create the sale order line
86 line_obj.create(cr, uid, {
88 'name': grid.carrier_id.name,
90 'product_uom': grid.carrier_id.product_id.uom_id.id,
91 'product_id': grid.carrier_id.product_id.id,
92 'price_unit': grid_obj.get_price(cr, uid, grid.id, order, time.strftime('%Y-%m-%d'), context),
93 'tax_id': [(6, 0, taxes_ids)],