access_rights
[odoo/odoo.git] / addons / delivery / stock.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
5 #
6 # $Id$
7 #
8 # WARNING: This program as such is intended to be used by professional
9 # programmers who take the whole responsability of assessing all potential
10 # consequences resulting from its eventual inadequacies and bugs
11 # End users who are looking for a ready-to-use solution with commercial
12 # garantees and support are strongly adviced to contract a Free Software
13 # Service Company
14 #
15 # This program is Free Software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License
17 # as published by the Free Software Foundation; either version 2
18 # of the License, or (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28 ###############################################################################
29
30 import netsvc
31 from osv import fields,osv
32 from tools.translate import _
33
34 # Overloaded stock_picking to manage carriers :
35 class stock_picking(osv.osv):
36     _name = "stock.picking"
37     _description = "Picking list"
38     _inherit = 'stock.picking'
39     _columns = {
40         'carrier_id':fields.many2one("delivery.carrier","Carrier"),
41         'volume': fields.float('Volume'),
42         'weight': fields.float('Weight'),
43     }
44
45     def action_invoice_create(self, cursor, user, ids, journal_id=False,
46             group=False, type='out_invoice', context=None):
47         invoice_obj = self.pool.get('account.invoice')
48         picking_obj = self.pool.get('stock.picking')
49         carrier_obj = self.pool.get('delivery.carrier')
50         grid_obj = self.pool.get('delivery.grid')
51         invoice_line_obj = self.pool.get('account.invoice.line')
52
53         result = super(stock_picking, self).action_invoice_create(cursor, user,
54                 ids, journal_id=journal_id, group=group, type=type,
55                 context=context)
56
57         picking_ids = result.keys()
58         invoice_ids = result.values()
59
60         invoices = {}
61         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
62                 context=context):
63             invoices[invoice.id] = invoice
64
65         for picking in picking_obj.browse(cursor, user, picking_ids,
66                 context=context):
67             if not picking.carrier_id:
68                 continue
69             grid_id = carrier_obj.grid_get(cursor, user, [picking.carrier_id.id],
70                     picking.address_id.id, context=context)
71             if not grid_id:
72                 raise osv.except_osv(_('Warning'),
73                         _('The carrier %s (id: %d) has no delivery grid!') \
74                                 % (picking.carrier_id.name,
75                                     picking.carrier_id.id))
76             invoice = invoices[result[picking.id]]
77             price = grid_obj.get_price_from_picking(cursor, user, grid_id,
78                     invoice.amount_untaxed, picking.weight, picking.volume,
79                     context=context)
80             account_id = picking.carrier_id.product_id.product_tmpl_id\
81                     .property_account_income.id
82             if not account_id:
83                 account_id = picking.carrier_id.product_id.categ_id\
84                         .property_account_income_categ.id
85
86             taxes = picking.carrier_id.product_id.taxes_id
87
88             partner_id=picking.address_id.partner_id and picking.address_id.partner_id.id or False
89             taxes_ids = [x.id for x in picking.carrier_id.product_id.taxes_id]
90             if partner_id:
91                 partner = picking.address_id.partner_id
92                 account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, partner, account_id)
93                 taxes_ids = self.pool.get('account.fiscal.position').map_tax(cursor, user, partner, taxes)
94
95             invoice_line_obj.create(cursor, user, {
96                 'name': picking.carrier_id.name,
97                 'invoice_id': invoice.id,
98                 'uos_id': picking.carrier_id.product_id.uos_id.id,
99                 'product_id': picking.carrier_id.product_id.id,
100                 'account_id': account_id,
101                 'price_unit': price,
102                 'quantity': 1,
103                 'invoice_line_tax_id': [(6, 0,taxes_ids)],
104             })
105         return result
106
107 stock_picking()
108
109 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
110