Fix invoice on picking to be more modular and works also from purchase
[odoo/odoo.git] / addons / purchase / stock.py
1 ##############################################################################
2 #
3 # Copyright (c) 2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #                    Fabien Pinckaers <fp@tiny.Be>
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from osv import osv, fields
30
31 class stock_move(osv.osv):
32         _inherit = 'stock.move'
33         _columns = {
34                 'purchase_line_id': fields.many2one('purchase.order.line', 'Purchase Order Line', ondelete='set null', select=True),
35         }
36         _defaults = {
37                 'purchase_line_id': lambda *a:False
38         }
39 stock_move()
40
41 #
42 # Inherit of picking to add the link to the PO
43 #
44 class stock_picking(osv.osv):
45         _inherit = 'stock.picking'
46         _columns = {
47                 'purchase_id': fields.many2one('purchase.order', 'Purchase Order', ondelete='set null', select=True),
48         }
49         _defaults = {
50                 'purchase_id': lambda *a: False,
51         }
52
53         def _get_comment_invoice(self, cursor, user, picking):
54                 if picking.purchase_id and picking.purchase_id.notes:
55                         if picking.note:
56                                 return picking.note + '\n' + picking.purchase_id.notes
57                         else:
58                                 return picking.purchase_id.notes
59                 return super(stock_picking, self)._get_comment_invoice(cursor, user,
60                                 picking)
61
62         def _get_price_unit_invoice(self, cursor, user, move_line, type):
63                 if move_line.purchase_line_id:
64                         return move_line.purchase_line_id.price_unit
65                 return super(stock_picking, self)._get_price_unit_invoice(cursor,
66                                 user, move_line, type)
67
68         def _get_discount_invoice(self, cursor, user, move_line):
69                 if move_line.purchase_line_id:
70                         return 0.0
71                 return super(stock_picking, self)._get_discount_invoice(cursor, user,
72                                 move_line)
73
74         def _get_taxes_invoice(self, cursor, user, move_line, type):
75                 if move_line.purchase_line_id:
76                         return [x.id for x in move_line.purchase_line_id.taxes_id]
77                 return super(stock_picking, self)._get_taxes_invoice(cursor, user,
78                                 move_line, type)
79
80         def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
81                 if move_line.purchase_line_id:
82                         return move_line.purchase_line_id.account_analytic_id.id
83                 return super(stock_picking, self)._get_account_analytic_invoice(cursor,
84                                 user, picking, move_line)
85
86         def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
87                 return super(stock_picking, self)._invoice_line_hook(cursor, user,
88                                 move_line, invoice_line_id)
89
90         def _invoice_hook(self, cursor, user, picking, invoice_id):
91                 purchase_obj = self.pool.get('purchase.order')
92                 if picking.purchase_id:
93                         purchase_obj.write(cursor, user, [picking.purchase_id.id], {
94                                 'invoice_id': invoice_id,
95                                 })
96                 return super(stock_picking, self)._invoice_hook(cursor, user,
97                                 picking, invoice_id)
98
99 stock_picking()