[MERGE] lp881356
[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 from osv import fields,osv
23 from tools.translate import _
24
25 import decimal_precision as dp
26
27 # Overloaded stock_picking to manage carriers :
28 class stock_picking(osv.osv):
29     _inherit = 'stock.picking'
30
31     def _cal_weight(self, cr, uid, ids, name, args, context=None):
32         res = {}
33         uom_obj = self.pool.get('product.uom')
34         for picking in self.browse(cr, uid, ids, context=context):
35             total_weight = total_weight_net = 0.00
36
37             for move in picking.move_lines:
38                 total_weight += move.weight
39                 total_weight_net += move.weight_net
40
41             res[picking.id] = {
42                                 'weight': total_weight,
43                                 'weight_net': total_weight_net,
44                               }
45         return res
46
47
48     def _get_picking_line(self, cr, uid, ids, context=None):
49         result = {}
50         for line in self.pool.get('stock.move').browse(cr, uid, ids, context=context):
51             result[line.picking_id.id] = True
52         return result.keys()
53     
54     _columns = {
55         'carrier_id':fields.many2one("delivery.carrier","Carrier"),
56         'volume': fields.float('Volume'),
57         'weight': fields.function(_cal_weight, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
58                   store={
59                  'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
60                  'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
61                  }),
62         'weight_net': fields.function(_cal_weight, type='float', string='Net Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
63                   store={
64                  'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
65                  'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
66                  }),
67         'carrier_tracking_ref': fields.char('Carrier Tracking Ref', size=32),
68         'number_of_packages': fields.integer('Number of Packages'),
69         }
70
71     def _prepare_shipping_invoice_line(self, cr, uid, picking, invoice, context=None):
72         """Prepare the invoice line to add to the shipping costs to the shipping's
73            invoice.
74
75             :param browse_record picking: the stock picking being invoiced
76             :param browse_record invoice: the stock picking's invoice
77             :return: dict containing the values to create the invoice line,
78                      or None to create nothing
79         """
80         carrier_obj = self.pool.get('delivery.carrier')
81         grid_obj = self.pool.get('delivery.grid')
82         if not picking.carrier_id or \
83             any(inv_line.product_id.id == picking.carrier_id.product_id.id
84                 for inv_line in invoice.invoice_line):
85             return None
86         grid_id = carrier_obj.grid_get(cr, uid, [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         price = grid_obj.get_price_from_picking(cr, uid, grid_id,
94                 invoice.amount_untaxed, picking.weight, picking.volume,
95                 context=context)
96         account_id = picking.carrier_id.product_id.property_account_income.id
97         if not account_id:
98             account_id = picking.carrier_id.product_id.categ_id\
99                     .property_account_income_categ.id
100
101         taxes = picking.carrier_id.product_id.taxes_id
102         partner = picking.address_id.partner_id or False
103         if partner:
104             account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, partner.property_account_position, account_id)
105             taxes_ids = self.pool.get('account.fiscal.position').map_tax(cr, uid, partner.property_account_position, taxes)
106         else:
107             taxes_ids = [x.id for x in taxes]
108
109         return {
110             'name': picking.carrier_id.name,
111             'invoice_id': invoice.id,
112             'uos_id': picking.carrier_id.product_id.uos_id.id,
113             'product_id': picking.carrier_id.product_id.id,
114             'account_id': account_id,
115             'price_unit': price,
116             'quantity': 1,
117             'invoice_line_tax_id': [(6, 0, taxes_ids)],
118         }
119
120     def action_invoice_create(self, cr, uid, ids, journal_id=False,
121             group=False, type='out_invoice', context=None):
122         invoice_obj = self.pool.get('account.invoice')
123         picking_obj = self.pool.get('stock.picking')
124         invoice_line_obj = self.pool.get('account.invoice.line') 
125         result = super(stock_picking, self).action_invoice_create(cr, uid,
126                 ids, journal_id=journal_id, group=group, type=type,
127                 context=context)
128         for picking in picking_obj.browse(cr, uid, result.keys(), context=context):
129             invoice = invoice_obj.browse(cr, uid, result[picking.id], context=context)
130             invoice_line = self._prepare_shipping_invoice_line(cr, uid, picking, invoice, context=context)
131             if invoice_line:
132                 invoice_line_obj.create(cr, uid, invoice_line)
133                 invoice_obj.button_compute(cr, uid, [invoice.id], context=context)
134         return result
135
136 stock_picking()
137
138 class stock_move(osv.osv):
139     _inherit = 'stock.move'
140
141     def _cal_move_weight(self, cr, uid, ids, name, args, context=None):
142         res = {}
143         uom_obj = self.pool.get('product.uom')
144         for move in self.browse(cr, uid, ids, context=context):
145             weight = weight_net = 0.00
146             if move.product_id.weight > 0.00:
147                 converted_qty = move.product_qty
148
149                 if move.product_uom.id <> move.product_id.uom_id.id:
150                     converted_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, move.product_id.uom_id.id)
151
152                 weight = (converted_qty * move.product_id.weight)
153
154                 if move.product_id.weight_net > 0.00:
155                     weight_net = (converted_qty * move.product_id.weight_net)
156
157             res[move.id] =  {
158                             'weight': weight,
159                             'weight_net': weight_net,
160                             }
161         return res
162
163     _columns = {
164         'weight': fields.function(_cal_move_weight, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_move_weight',
165                   store={
166                  'stock.move': (lambda self, cr, uid, ids, c=None: ids, ['product_id', 'product_qty', 'product_uom'], 20),
167                  }),
168         'weight_net': fields.function(_cal_move_weight, type='float', string='Net weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_move_weight',
169                   store={
170                  'stock.move': (lambda self, cr, uid, ids, c=None: ids, ['product_id', 'product_qty', 'product_uom'], 20),
171                  }),
172         }
173
174 stock_move()
175
176 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
177