Launchpad automatic translations update.
[odoo/odoo.git] / addons / sale_stock / 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 openerp.osv import fields, osv
23
24 class stock_move(osv.osv):
25     _inherit = 'stock.move'
26     _columns = {
27         'sale_line_id': fields.many2one('sale.order.line', 'Sales Order Line', ondelete='set null', select=True, readonly=True),
28     }
29
30     def _prepare_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
31         values = super(stock_move, self)._prepare_chained_picking(cr, uid, picking_name, picking, picking_type, moves_todo, context=context)
32         if picking.sale_id:
33             values['sale_id'] = picking.sale_id.id
34         return values
35
36 class stock_picking(osv.osv):
37     _inherit = 'stock.picking'
38     _columns = {
39         'sale_id': fields.many2one('sale.order', 'Sales Order', ondelete='set null', select=True),
40     }
41     _defaults = {
42         'sale_id': False
43     }
44
45     def get_currency_id(self, cursor, user, picking):
46         if picking.sale_id:
47             return picking.sale_id.pricelist_id.currency_id.id
48         else:
49             return super(stock_picking, self).get_currency_id(cursor, user, picking)
50
51     def _get_partner_to_invoice(self, cr, uid, picking, context=None):
52         """ Inherit the original function of the 'stock' module
53             We select the partner of the sales order as the partner of the customer invoice
54         """
55         if picking.sale_id:
56             return picking.sale_id.partner_id
57         return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
58
59     def _get_comment_invoice(self, cursor, user, picking):
60         if picking.note or (picking.sale_id and picking.sale_id.note):
61             return picking.note or picking.sale_id.note
62         return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
63
64     def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
65         """ Inherit the original function of the 'stock' module in order to override some
66             values if the picking has been generated by a sales order
67         """
68         invoice_vals = super(stock_picking, self)._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
69         if picking.sale_id:
70             invoice_vals['fiscal_position'] = picking.sale_id.fiscal_position.id
71             invoice_vals['payment_term'] = picking.sale_id.payment_term.id
72             invoice_vals['user_id'] = picking.sale_id.user_id.id
73             invoice_vals['name'] = picking.sale_id.client_order_ref or ''
74         return invoice_vals
75
76     def _prepare_invoice_line(self, cr, uid, group, picking, move_line, invoice_id, invoice_vals, context=None):
77         invoice_vals = super(stock_picking, self)._prepare_invoice_line(cr, uid, group, picking, move_line, invoice_id, invoice_vals, context=context)
78         if picking.sale_id:
79             if move_line.sale_line_id:
80                 invoice_vals['account_analytic_id'] = self._get_account_analytic_invoice(cr, uid, picking, move_line)
81         return invoice_vals
82
83     def _get_price_unit_invoice(self, cursor, user, move_line, type):
84         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
85             uom_id = move_line.product_id.uom_id.id
86             uos_id = move_line.product_id.uos_id and move_line.product_id.uos_id.id or False
87             price = move_line.sale_line_id.price_unit
88             coeff = move_line.product_id.uos_coeff
89             if uom_id != uos_id  and coeff != 0:
90                 price_unit = price / coeff
91                 return price_unit
92             return move_line.sale_line_id.price_unit
93         return super(stock_picking, self)._get_price_unit_invoice(cursor, user, move_line, type)
94
95     def _get_discount_invoice(self, cursor, user, move_line):
96         if move_line.sale_line_id:
97             return move_line.sale_line_id.discount
98         return super(stock_picking, self)._get_discount_invoice(cursor, user, move_line)
99
100     def _get_taxes_invoice(self, cursor, user, move_line, type):
101         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
102             return [x.id for x in move_line.sale_line_id.tax_id]
103         return super(stock_picking, self)._get_taxes_invoice(cursor, user, move_line, type)
104
105     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
106         if picking.sale_id:
107             return picking.sale_id.project_id.id
108         return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
109
110     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
111         if move_line.sale_line_id:
112             move_line.sale_line_id.write({'invoice_lines': [(4, invoice_line_id)]})
113         return super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id)
114
115     def _invoice_hook(self, cursor, user, picking, invoice_id):
116         sale_obj = self.pool.get('sale.order')
117         if picking.sale_id:
118             sale_obj.write(cursor, user, [picking.sale_id.id], {
119                 'invoice_ids': [(4, invoice_id)],
120                 })
121         return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
122
123 # Redefinition of the new field in order to update the model stock.picking.out in the orm
124 # FIXME: this is a temporary workaround because of a framework bug (ref: lp996816). It should be removed as soon as
125 #        the bug is fixed
126 class stock_picking_out(osv.osv):
127     _inherit = 'stock.picking.out'
128     _columns = {
129         'sale_id': fields.many2one('sale.order', 'Sale Order',
130             ondelete='set null', select=True),
131     }
132 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: