[IMP]:purchase:Incoming Shipment: add a field warehouse below the company (the field...
[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
32 stock_move()
33
34 #
35 # Inherit of picking to add the link to the PO
36 #
37 class stock_picking(osv.osv):
38     _inherit = 'stock.picking'
39     _columns = {
40         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
41             ondelete='set null', select=True),
42         
43     }
44     _defaults = {
45         'purchase_id': False,
46     }
47
48     def _get_partner_to_invoice(self, cr, uid, picking, context=None):
49         """ Inherit the original function of the 'stock' module
50             We select the partner of the sale order as the partner of the customer invoice
51         """
52         if picking.purchase_id:
53             return picking.purchase_id.partner_id
54         return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
55
56     def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
57         """ Inherit the original function of the 'stock' module in order to override some
58             values if the picking has been generated by a purchase order
59         """
60         invoice_vals = super(stock_picking, self)._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
61         if picking.purchase_id:
62             invoice_vals['fiscal_position'] = picking.purchase_id.fiscal_position.id
63         return invoice_vals
64
65     def get_currency_id(self, cursor, user, picking):
66         if picking.purchase_id:
67             return picking.purchase_id.pricelist_id.currency_id.id
68         else:
69             return super(stock_picking, self).get_currency_id(cursor, user, picking)
70
71     def _get_comment_invoice(self, cursor, user, picking):
72         if picking.purchase_id and picking.purchase_id.notes:
73             if picking.note:
74                 return picking.note + '\n' + picking.purchase_id.notes
75             else:
76                 return picking.purchase_id.notes
77         return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
78
79     def _get_price_unit_invoice(self, cursor, user, move_line, type):
80         if move_line.purchase_line_id:
81             if move_line.purchase_line_id.order_id.invoice_method == 'picking':
82                 return move_line.price_unit
83             else:
84                 return move_line.purchase_line_id.price_unit
85         return super(stock_picking, self)._get_price_unit_invoice(cursor, user, move_line, type)
86
87     def _get_discount_invoice(self, cursor, user, move_line):
88         if move_line.purchase_line_id:
89             return 0.0
90         return super(stock_picking, self)._get_discount_invoice(cursor, user, move_line)
91
92     def _get_taxes_invoice(self, cursor, user, move_line, type):
93         if move_line.purchase_line_id:
94             return [x.id for x in move_line.purchase_line_id.taxes_id]
95         return super(stock_picking, self)._get_taxes_invoice(cursor, user, move_line, type)
96
97     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
98         if picking.purchase_id and move_line.purchase_line_id:
99             return move_line.purchase_line_id.account_analytic_id.id
100         return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
101
102     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
103         if move_line.purchase_line_id:
104             invoice_line_obj = self.pool.get('account.invoice.line')
105             purchase_line_obj = self.pool.get('purchase.order.line') 
106             purchase_line_obj.write(cursor, user, [move_line.purchase_line_id.id], {
107                 'invoiced': True,
108                 'invoice_lines': [(4, invoice_line_id)],
109             })
110             invoice_line_obj.write(cursor, user, [invoice_line_id], {'note':  move_line.purchase_line_id.notes,})
111         return super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id)
112
113     def _invoice_hook(self, cursor, user, picking, invoice_id):
114         purchase_obj = self.pool.get('purchase.order')
115         if picking.purchase_id:
116             purchase_obj.write(cursor, user, [picking.purchase_id.id], {'invoice_ids': [(4, invoice_id)]})
117         return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
118
119 class stock_partial_picking(osv.osv_memory):
120     _inherit = 'stock.partial.picking'
121
122     # Overridden to inject the purchase price as true 'cost price' when processing
123     # incoming pickings.
124     def _product_cost_for_average_update(self, cr, uid, move):
125         if move.picking_id.purchase_id:
126             return {'cost': move.purchase_line_id.price_unit,
127                     'currency': move.picking_id.purchase_id.pricelist_id.currency_id.id}
128         return super(stock_partial_picking, self)._product_cost_for_average_update(cr, uid, move)
129
130 # Redefinition of the new field in order to update the model stock.picking.in in the orm
131 # FIXME: this is a temporary workaround because of a framework bug (ref: lp996816). It should be removed as soon as
132 #        the bug is fixed
133 class stock_picking_in(osv.osv):
134     _inherit = 'stock.picking.in'
135     _columns = {
136         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
137             ondelete='set null', select=True),
138         'warehouse_id': fields.related('purchase_id', 'warehouse_id', type='many2one', relation='stock.warehouse', string='Warehouse'),
139     }
140 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: