fixed bug:308803
[odoo/odoo.git] / addons / purchase / stock.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24
25 class stock_move(osv.osv):
26     _inherit = 'stock.move'
27     _columns = {
28         'purchase_line_id': fields.many2one('purchase.order.line',
29             'Purchase Order Line', ondelete='set null', select=True,
30             readonly=True),
31     }
32     _defaults = {
33         'purchase_line_id': lambda *a:False
34     }
35 stock_move()
36
37 #
38 # Inherit of picking to add the link to the PO
39 #
40 class stock_picking(osv.osv):
41     _inherit = 'stock.picking'
42     _columns = {
43         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
44             ondelete='set null', select=True, readonly=True),
45     }
46     _defaults = {
47         'purchase_id': lambda *a: False,
48     }
49
50     def _get_comment_invoice(self, cursor, user, picking):
51         if picking.purchase_id and picking.purchase_id.notes:
52             if picking.note:
53                 return picking.note + '\n' + picking.purchase_id.notes
54             else:
55                 return picking.purchase_id.notes
56         return super(stock_picking, self)._get_comment_invoice(cursor, user,
57                 picking)
58
59     def _get_price_unit_invoice(self, cursor, user, move_line, type):
60         if move_line.purchase_line_id:
61             return move_line.purchase_line_id.price_unit
62         return super(stock_picking, self)._get_price_unit_invoice(cursor,
63                 user, move_line, type)
64
65     def _get_discount_invoice(self, cursor, user, move_line):
66         if move_line.purchase_line_id:
67             return 0.0
68         return super(stock_picking, self)._get_discount_invoice(cursor, user,
69                 move_line)
70
71     def _get_taxes_invoice(self, cursor, user, move_line, type):
72         if move_line.purchase_line_id:
73             return [x.id for x in move_line.purchase_line_id.taxes_id]
74         return super(stock_picking, self)._get_taxes_invoice(cursor, user,
75                 move_line, type)
76
77     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
78         if move_line.purchase_line_id:
79             return move_line.purchase_line_id.account_analytic_id.id
80         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
81                 user, picking, move_line)
82
83     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
84         return super(stock_picking, self)._invoice_line_hook(cursor, user,
85                 move_line, invoice_line_id)
86
87     def _invoice_hook(self, cursor, user, picking, invoice_id):
88         purchase_obj = self.pool.get('purchase.order')
89         if picking.purchase_id:
90             purchase_obj.write(cursor, user, [picking.purchase_id.id], {'invoiced':True,
91                 'invoice_id': invoice_id,
92                 })
93         return super(stock_picking, self)._invoice_hook(cursor, user,
94                 picking, invoice_id)
95
96 stock_picking()
97
98 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
99