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 ##############################################################################
22 from openerp.osv import fields,osv
23 from openerp.tools.translate import _
25 import openerp.addons.decimal_precision as dp
27 # Overloaded stock_picking to manage carriers :
28 class stock_picking(osv.osv):
29 _inherit = 'stock.picking'
31 def _cal_weight(self, cr, uid, ids, name, args, context=None):
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
37 for move in picking.move_lines:
38 total_weight += move.weight
39 total_weight_net += move.weight_net
42 'weight': total_weight,
43 'weight_net': total_weight_net,
48 def _get_picking_line(self, cr, uid, ids, context=None):
50 for line in self.pool.get('stock.move').browse(cr, uid, ids, context=context):
51 result[line.picking_id.id] = True
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',
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),
62 'weight_net': fields.function(_cal_weight, type='float', string='Net Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
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),
67 'carrier_tracking_ref': fields.char('Carrier Tracking Ref', size=32),
68 'number_of_packages': fields.integer('Number of Packages'),
69 'weight_uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True,readonly="1",help="Unit of measurement for Weight",),
72 def _prepare_shipping_invoice_line(self, cr, uid, picking, invoice, context=None):
73 """Prepare the invoice line to add to the shipping costs to the shipping's
76 :param browse_record picking: the stock picking being invoiced
77 :param browse_record invoice: the stock picking's invoice
78 :return: dict containing the values to create the invoice line,
79 or None to create nothing
81 carrier_obj = self.pool.get('delivery.carrier')
82 grid_obj = self.pool.get('delivery.grid')
83 if not picking.carrier_id or \
84 any(inv_line.product_id.id == picking.carrier_id.product_id.id
85 for inv_line in invoice.invoice_line):
87 grid_id = carrier_obj.grid_get(cr, uid, [picking.carrier_id.id],
88 picking.partner_id.id, context=context)
90 raise osv.except_osv(_('Warning!'),
91 _('The carrier %s (id: %d) has no delivery grid!') \
92 % (picking.carrier_id.name,
93 picking.carrier_id.id))
94 price = grid_obj.get_price_from_picking(cr, uid, grid_id,
95 invoice.amount_untaxed, picking.weight, picking.volume,
97 account_id = picking.carrier_id.product_id.property_account_income.id
99 account_id = picking.carrier_id.product_id.categ_id\
100 .property_account_income_categ.id
102 taxes = picking.carrier_id.product_id.taxes_id
103 partner = picking.partner_id or False
105 account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, partner.property_account_position, account_id)
106 taxes_ids = self.pool.get('account.fiscal.position').map_tax(cr, uid, partner.property_account_position, taxes)
108 taxes_ids = [x.id for x in taxes]
111 'name': picking.carrier_id.name,
112 'invoice_id': invoice.id,
113 'uos_id': picking.carrier_id.product_id.uos_id.id,
114 'product_id': picking.carrier_id.product_id.id,
115 'account_id': account_id,
118 'invoice_line_tax_id': [(6, 0, taxes_ids)],
121 def action_invoice_create(self, cr, uid, ids, journal_id=False,
122 group=False, type='out_invoice', context=None):
123 invoice_obj = self.pool.get('account.invoice')
124 picking_obj = self.pool.get('stock.picking')
125 invoice_line_obj = self.pool.get('account.invoice.line')
126 result = super(stock_picking, self).action_invoice_create(cr, uid,
127 ids, journal_id=journal_id, group=group, type=type,
129 for picking in picking_obj.browse(cr, uid, result.keys(), context=context):
130 invoice = invoice_obj.browse(cr, uid, result[picking.id], context=context)
131 invoice_line = self._prepare_shipping_invoice_line(cr, uid, picking, invoice, context=context)
133 invoice_line_obj.create(cr, uid, invoice_line)
134 invoice_obj.button_compute(cr, uid, [invoice.id], context=context)
136 def _get_default_uom(self,cr,uid,c):
137 uom_categ, uom_categ_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_categ_kgm')
138 return self.pool.get('product.uom').search(cr, uid, [('category_id', '=', uom_categ_id),('factor','=',1)])[0]
140 'weight_uom_id': lambda self,cr,uid,c: self._get_default_uom(cr,uid,c)
144 class stock_move(osv.osv):
145 _inherit = 'stock.move'
147 def _cal_move_weight(self, cr, uid, ids, name, args, context=None):
149 uom_obj = self.pool.get('product.uom')
150 for move in self.browse(cr, uid, ids, context=context):
151 weight = weight_net = 0.00
152 if move.product_id.weight > 0.00:
153 converted_qty = move.product_qty
155 if move.product_uom.id <> move.product_id.uom_id.id:
156 converted_qty = uom_obj._compute_qty(cr, uid, move.product_uom.id, move.product_qty, move.product_id.uom_id.id)
158 weight = (converted_qty * move.product_id.weight)
160 if move.product_id.weight_net > 0.00:
161 weight_net = (converted_qty * move.product_id.weight_net)
165 'weight_net': weight_net,
170 'weight': fields.function(_cal_move_weight, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_move_weight',
172 'stock.move': (lambda self, cr, uid, ids, c=None: ids, ['product_id', 'product_qty', 'product_uom'], 20),
174 'weight_net': fields.function(_cal_move_weight, type='float', string='Net weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_move_weight',
176 'stock.move': (lambda self, cr, uid, ids, c=None: ids, ['product_id', 'product_qty', 'product_uom'], 20),
178 'weight_uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True,readonly="1",help="Unit of Measure (Unit of Measure) is the unit of measurement for Weight",),
180 def _get_default_uom(self,cr,uid,c):
181 uom_categ, uom_categ_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'product', 'product_uom_categ_kgm')
182 return self.pool.get('product.uom').search(cr, uid, [('category_id', '=', uom_categ_id),('factor','=',1)])[0]
184 'weight_uom_id': lambda self,cr,uid,c: self._get_default_uom(cr,uid,c)
187 # Redefinition of the new fields in order to update the model stock.picking.out in the orm
188 # FIXME: this is a temporary workaround because of a framework bug (ref: lp996816). It should be removed as soon as
191 # TODO in trunk: Remove the duplication below using a mixin class!
193 class stock_picking_out(osv.osv):
194 _inherit = 'stock.picking.out'
196 def _cal_weight(self, cr, uid, ids, name, args, context=None):
197 return self.pool.get('stock.picking')._cal_weight(cr, uid, ids, name, args, context=context)
200 def _get_picking_line(self, cr, uid, ids, context=None):
201 return self.pool.get('stock.picking')._get_picking_line(cr, uid, ids, context=context)
204 'carrier_id':fields.many2one("delivery.carrier","Carrier"),
205 'volume': fields.float('Volume'),
206 'weight': fields.function(_cal_weight, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
208 'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
209 'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
211 'weight_net': fields.function(_cal_weight, type='float', string='Net Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
213 'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
214 'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
216 'carrier_tracking_ref': fields.char('Carrier Tracking Ref', size=32),
217 'number_of_packages': fields.integer('Number of Packages'),
218 'weight_uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True,readonly="1",help="Unit of measurement for Weight",),
221 class stock_picking_in(osv.osv):
222 _inherit = 'stock.picking.in'
224 def _cal_weight(self, cr, uid, ids, name, args, context=None):
225 return self.pool.get('stock.picking')._cal_weight(cr, uid, ids, name, args, context=context)
227 def _get_picking_line(self, cr, uid, ids, context=None):
228 return self.pool.get('stock.picking')._get_picking_line(cr, uid, ids, context=context)
231 'carrier_id':fields.many2one("delivery.carrier","Carrier"),
232 'volume': fields.float('Volume'),
233 'weight': fields.function(_cal_weight, type='float', string='Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
235 'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
236 'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
238 'weight_net': fields.function(_cal_weight, type='float', string='Net Weight', digits_compute= dp.get_precision('Stock Weight'), multi='_cal_weight',
240 'stock.picking': (lambda self, cr, uid, ids, c={}: ids, ['move_lines'], 20),
241 'stock.move': (_get_picking_line, ['product_id','product_qty','product_uom','product_uos_qty'], 20),
243 'carrier_tracking_ref': fields.char('Carrier Tracking Ref', size=32),
244 'number_of_packages': fields.integer('Number of Packages'),
245 'weight_uom_id': fields.many2one('product.uom', 'Unit of Measure', required=True,readonly="1",help="Unit of measurement for Weight",),
248 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: