mege
[odoo/odoo.git] / addons / purchase / 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 osv, fields
23
24 class stock_move(osv.osv):
25     _inherit = 'stock.move'
26     _columns = {
27         'purchase_line_id': fields.many2one('purchase.order.line',
28             'Purchase Order Line', ondelete='set null', select=True,
29             readonly=True),
30     }
31     _defaults = {
32         'purchase_line_id': lambda *a:False
33     }
34 stock_move()
35
36 #
37 # Inherit of picking to add the link to the PO
38 #
39 class stock_picking(osv.osv):
40     _inherit = 'stock.picking'
41     _columns = {
42         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
43             ondelete='set null', select=True, readonly=True),
44     }
45     _defaults = {
46         'purchase_id': lambda *a: False,
47     }
48
49     def get_currency_id(self, cursor, user, picking):
50         if picking.purchase_id:
51             return picking.purchase_id.pricelist_id.currency_id.id
52         else:
53             return super(stock_picking, self).get_currency_id(cursor, user, picking)
54
55     def _get_comment_invoice(self, cursor, user, picking):
56         if picking.purchase_id and picking.purchase_id.notes:
57             if picking.note:
58                 return picking.note + '\n' + picking.purchase_id.notes
59             else:
60                 return picking.purchase_id.notes
61         return super(stock_picking, self)._get_comment_invoice(cursor, user,
62                 picking)
63
64     def _get_price_unit_invoice(self, cursor, user, move_line, type):
65         if move_line.purchase_line_id:
66             return move_line.purchase_line_id.price_unit
67         return super(stock_picking, self)._get_price_unit_invoice(cursor,
68                 user, move_line, type)
69
70     def _get_discount_invoice(self, cursor, user, move_line):
71         if move_line.purchase_line_id:
72             return 0.0
73         return super(stock_picking, self)._get_discount_invoice(cursor, user,
74                 move_line)
75
76     def _get_taxes_invoice(self, cursor, user, move_line, type):
77         if move_line.purchase_line_id:
78             return [x.id for x in move_line.purchase_line_id.taxes_id]
79         return super(stock_picking, self)._get_taxes_invoice(cursor, user,
80                 move_line, type)
81
82     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
83         if move_line.purchase_line_id:
84             return move_line.purchase_line_id.account_analytic_id.id
85         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
86                 user, picking, move_line)
87
88     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
89         return super(stock_picking, self)._invoice_line_hook(cursor, user,
90                 move_line, invoice_line_id)
91
92     def _invoice_hook(self, cursor, user, picking, invoice_id):
93         purchase_obj = self.pool.get('purchase.order')
94         if picking.purchase_id:
95             purchase_obj.write(cursor, user, [picking.purchase_id.id], {'invoice_id': invoice_id,
96                 })
97         return super(stock_picking, self)._invoice_hook(cursor, user,
98                 picking, invoice_id)
99
100 stock_picking()
101
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
103