[IMP] Small improvements
[odoo/odoo.git] / addons / delivery / sale.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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 import time
23 from openerp.osv import fields,osv
24 from openerp.tools.translate import _
25
26 class sale_order_line(osv.osv):
27     _inherit = 'sale.order.line'
28     _columns = {
29         'is_delivery':fields.boolean("Is a Delivery"),
30     }
31     _defaults = {
32         'is_delivery': False
33     }
34
35 class sale_order(osv.osv):
36     _inherit = 'sale.order'
37     _columns = {
38         'carrier_id':fields.many2one("delivery.carrier", "Delivery Method", help="Complete this field if you plan to invoice the shipping based on picking."),
39     }
40
41     def onchange_partner_id(self, cr, uid, ids, part, context=None):
42         result = super(sale_order, self).onchange_partner_id(cr, uid, ids, part, context=context)
43         if part:
44             dtype = self.pool.get('res.partner').browse(cr, uid, part, context=context).property_delivery_carrier.id
45             result['value']['carrier_id'] = dtype
46         return result
47
48     def _prepare_order_picking(self, cr, uid, order, context=None):
49         result = super(sale_order, self)._prepare_order_picking(cr, uid, order, context=context)
50         result.update(carrier_id=order.carrier_id.id)
51         return result
52
53     def _delivery_unset(self, cr, uid, order, context=None):
54         for line in order.order_line:
55             if line.is_delivery:
56                 self.pool.get('sale.order.line').unlink(cr, uid, [line.id], context=context)
57         return True
58
59     def delivery_set(self, cr, uid, ids, context=None):
60         order_obj = self.pool.get('sale.order')
61         line_obj = self.pool.get('sale.order.line')
62         grid_obj = self.pool.get('delivery.grid')
63         carrier_obj = self.pool.get('delivery.carrier')
64         acc_fp_obj = self.pool.get('account.fiscal.position')
65         for order in self.browse(cr, uid, ids, context=context):
66             self._delivery_unset(cr, uid, order, context=context)
67         for order in self.browse(cr, uid, ids, context=context):
68             grid_id = carrier_obj.grid_get(cr, uid, [order.carrier_id.id], order.partner_shipping_id.id)
69             if not grid_id:
70                 raise osv.except_osv(_('No Grid Available!'), _('No grid matching for this carrier!'))
71
72             if not order.state in ('draft'):
73                 raise osv.except_osv(_('Order not in Draft State!'), _('The order state have to be draft to add delivery lines.'))
74
75             grid = grid_obj.browse(cr, uid, grid_id, context=context)
76
77             taxes = grid.carrier_id.product_id.taxes_id
78             fpos = order.fiscal_position or False
79             taxes_ids = acc_fp_obj.map_tax(cr, uid, fpos, taxes)
80             #create the sale order line
81             line_obj.create(cr, uid, {
82                 'order_id': order.id,
83                 'name': grid.carrier_id.name,
84                 'product_uom_qty': 1,
85                 'product_uom': grid.carrier_id.product_id.uom_id.id,
86                 'product_id': grid.carrier_id.product_id.id,
87                 'price_unit': grid_obj.get_price(cr, uid, grid.id, order, time.strftime('%Y-%m-%d'), context),
88                 'tax_id': [(6,0,taxes_ids)],
89                 'type': 'make_to_stock',
90                 'is_delivery': True
91             })
92         #remove the value of the carrier_id field on the sale order
93         return self.write(cr, uid, ids, {'carrier_id': False}, context=context)
94         #return {'type': 'ir.actions.act_window_close'} action reload?
95
96
97 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
98