[IMP]: auction,delivery: fields_list variable has been changed to fields
[odoo/odoo.git] / addons / delivery / stock.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 netsvc
23 from osv import fields,osv
24 from tools.translate import _
25 import tools
26
27 import decimal_precision as dp
28
29 # Overloaded stock_picking to manage carriers :
30 class stock_picking(osv.osv):
31     _name = "stock.picking"
32     _description = "Picking list"
33     _inherit = 'stock.picking'
34
35     def _cal_weight(self, cr, uid, ids, name, args, context=None):
36         res = {}
37         uom_obj = self.pool.get('product.uom')
38         for picking in self.browse(cr, uid, ids, context):
39             total_weight = 0.00
40             for move in picking.move_lines:
41                 total_weight += move.weight
42             res[picking.id] = total_weight
43         return res
44
45
46     def _get_picking_line(self, cr, uid, ids, context=None):
47         result = {}
48         for line in self.pool.get('stock.move').browse(cr, uid, ids, context=context):
49             result[line.picking_id.id] = True
50         return result.keys()
51     
52     _columns = {
53         'carrier_id':fields.many2one("delivery.carrier","Carrier"),
54         'volume': fields.float('Volume'),
55         'weight': fields.function(_cal_weight, method=True, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'),
56                   store={
57                  'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
58                  'stock.move': (_get_picking_line, ['product_id','product_uos_qty'], 20),
59                  }),
60         }
61
62     def action_invoice_create(self, cursor, user, ids, journal_id=False,
63             group=False, type='out_invoice', context=None):
64         invoice_obj = self.pool.get('account.invoice')
65         picking_obj = self.pool.get('stock.picking')
66         carrier_obj = self.pool.get('delivery.carrier')
67         grid_obj = self.pool.get('delivery.grid')
68         invoice_line_obj = self.pool.get('account.invoice.line')
69
70         result = super(stock_picking, self).action_invoice_create(cursor, user,
71                 ids, journal_id=journal_id, group=group, type=type,
72                 context=context)
73
74         picking_ids = result.keys()
75         invoice_ids = result.values()
76
77         invoices = {}
78         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
79                 context=context):
80             invoices[invoice.id] = invoice
81
82         for picking in picking_obj.browse(cursor, user, picking_ids,
83                 context=context):
84             if not picking.carrier_id:
85                 continue
86             grid_id = carrier_obj.grid_get(cursor, user, [picking.carrier_id.id],
87                     picking.address_id.id, context=context)
88             if not grid_id:
89                 raise osv.except_osv(_('Warning'),
90                         _('The carrier %s (id: %d) has no delivery grid!') \
91                                 % (picking.carrier_id.name,
92                                     picking.carrier_id.id))
93             invoice = invoices[result[picking.id]]
94             price = grid_obj.get_price_from_picking(cursor, user, grid_id,
95                     invoice.amount_untaxed, picking.weight, picking.volume,
96                     context=context)
97             account_id = picking.carrier_id.product_id.product_tmpl_id\
98                     .property_account_income.id
99             if not account_id:
100                 account_id = picking.carrier_id.product_id.categ_id\
101                         .property_account_income_categ.id
102
103             taxes = picking.carrier_id.product_id.taxes_id
104
105             partner_id=picking.address_id.partner_id and picking.address_id.partner_id.id or False
106             taxes_ids = [x.id for x in picking.carrier_id.product_id.taxes_id]
107             if partner_id:
108                 partner = picking.address_id.partner_id
109                 account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, partner.property_account_position, account_id)
110                 taxes_ids = self.pool.get('account.fiscal.position').map_tax(cursor, user, partner.property_account_position, taxes)
111
112             invoice_line_obj.create(cursor, user, {
113                 'name': picking.carrier_id.name,
114                 'invoice_id': invoice.id,
115                 'uos_id': picking.carrier_id.product_id.uos_id.id,
116                 'product_id': picking.carrier_id.product_id.id,
117                 'account_id': account_id,
118                 'price_unit': price,
119                 'quantity': 1,
120                 'invoice_line_tax_id': [(6, 0,taxes_ids)],
121             })
122         return result
123
124 stock_picking()
125
126 class stock_move(osv.osv):
127     _inherit = 'stock.move'
128
129     def _cal_move_weight(self, cr, uid, ids, name, args, context=None):
130         res = {}
131         uom_obj = self.pool.get('product.uom')
132         for move in self.browse(cr, uid, ids, context):
133             weight = 0.00
134             if move.product_id.weight > 0.00:
135                 converted_qty = move.product_qty
136                 
137                 if move.product_uom.id <> move.product_id.uom_id.id:
138                     converted_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, move.product_id.uom_id.id)
139
140                 weight = (converted_qty * move.product_id.weight)
141             res[move.id] = weight
142         return res
143     
144     _columns = {
145         'weight': fields.function(_cal_move_weight, method=True, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'),
146                   store={
147                  'stock.move': (lambda self, cr, uid, ids, c={}: ids, ['product_id', 'product_qty', 'product_uom'], 20),
148                  }),
149         }
150
151 stock_move()
152
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
154