[FIX] purchase: keep PO currency on picking
[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 openerp.osv import fields, osv
23 from openerp.tools.translate import _
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
33 stock_move()
34
35 #
36 # Inherit of picking to add the link to the PO
37 #
38 class stock_picking(osv.osv):
39     _inherit = 'stock.picking'
40     _columns = {
41         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
42             ondelete='set null', select=True),
43     }
44
45     _defaults = {
46         'purchase_id': False,
47     }
48
49     def _get_partner_to_invoice(self, cr, uid, picking, context=None):
50         """ Inherit the original function of the 'stock' module
51             We select the partner of the sale order as the partner of the customer invoice
52         """
53         if picking.purchase_id:
54             return picking.purchase_id.partner_id
55         return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
56
57     def _prepare_invoice(self, cr, uid, picking, partner, inv_type, journal_id, context=None):
58         """ Inherit the original function of the 'stock' module in order to override some
59             values if the picking has been generated by a purchase order
60         """
61         invoice_vals = super(stock_picking, self)._prepare_invoice(cr, uid, picking, partner, inv_type, journal_id, context=context)
62         if picking.purchase_id:
63             invoice_vals['fiscal_position'] = picking.purchase_id.fiscal_position.id
64             invoice_vals['payment_term'] = picking.purchase_id.payment_term_id.id
65             # Fill the date_due on the invoice, for usability purposes.
66             # Note that when an invoice with a payment term is validated, the
67             # date_due is always recomputed from the invoice date and the payment
68             # term.
69             if picking.purchase_id.payment_term_id and context.get('date_inv'):
70                 invoice_vals['date_due'] = self.pool.get('account.invoice').onchange_payment_term_date_invoice(cr, uid, [], picking.purchase_id.payment_term_id.id, context.get('date_inv'))['value'].get('date_due')
71         return invoice_vals
72
73     def get_currency_id(self, cursor, user, picking):
74         if picking.purchase_id:
75             return picking.purchase_id.pricelist_id.currency_id.id
76         else:
77             return super(stock_picking, self).get_currency_id(cursor, user, picking)
78
79     def _get_comment_invoice(self, cursor, user, picking):
80         if picking.purchase_id and picking.purchase_id.notes:
81             if picking.note:
82                 return picking.note + '\n' + picking.purchase_id.notes
83             else:
84                 return picking.purchase_id.notes
85         return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
86
87     def _get_price_unit_invoice(self, cursor, user, move_line, type):
88         if move_line.purchase_line_id:
89             if move_line.purchase_line_id.order_id.invoice_method == 'picking':
90                 return move_line.price_unit
91             else:
92                 return move_line.purchase_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.purchase_line_id:
97             return 0.0
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.purchase_line_id:
102             return [x.id for x in move_line.purchase_line_id.taxes_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.purchase_id and move_line.purchase_line_id:
107             return move_line.purchase_line_id.account_analytic_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.purchase_line_id:
112             invoice_line_obj = self.pool.get('account.invoice.line')
113             purchase_line_obj = self.pool.get('purchase.order.line') 
114             purchase_line_obj.write(cursor, user, [move_line.purchase_line_id.id], {
115                 'invoice_lines': [(4, invoice_line_id)],
116             })
117         return super(stock_picking, self)._invoice_line_hook(cursor, user, move_line, invoice_line_id)
118
119     def _invoice_hook(self, cursor, user, picking, invoice_id):
120         purchase_obj = self.pool.get('purchase.order')
121         if picking.purchase_id:
122             purchase_obj.write(cursor, user, [picking.purchase_id.id], {'invoice_ids': [(4, invoice_id)]})
123         return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
124
125 class stock_partial_picking(osv.osv_memory):
126     _inherit = 'stock.partial.picking'
127
128     # Overridden to inject the purchase price as true 'cost price' when processing
129     # incoming pickings.
130     def _product_cost_for_average_update(self, cr, uid, move):
131         purchase_line = move.purchase_line_id
132         if move.picking_id.purchase_id and purchase_line:
133             if any([x.invoice_id.state not in ('draft', 'cancel') for x in purchase_line.invoice_lines]):
134                 # use price set on validated invoices
135                 cost = move.price_unit
136                 for inv_line in purchase_line.invoice_lines:
137                     if inv_line.invoice_id.state not in ('draft', 'cancel'):
138                         inv_currency = inv_line.invoice_id.currency_id.id
139                         company_currency = inv_line.invoice_id.company_id.currency_id.id
140                         cost = self.pool.get('res.currency').compute(cr, uid, inv_currency, company_currency, inv_line.price_unit, round=False, context={'date': inv_line.invoice_id.date_invoice})
141                         return {'cost': cost, 'currency': company_currency}
142             else:
143                 # use price set on the purchase order
144                 pur_currency = purchase_line.order_id.currency_id.id
145                 company_currency = purchase_line.company_id.currency_id.id
146                 cost = self.pool.get('res.currency').compute(cr, uid, pur_currency, company_currency, purchase_line.price_unit, round=False, context={'date': purchase_line.date_order})
147                 return {'cost': cost, 'currency': company_currency}
148         return super(stock_partial_picking, self)._product_cost_for_average_update(cr, uid, move)
149
150     def _partial_move_for(self, cr, uid, move, context=None):
151         partial_move = super(stock_partial_picking, self)._partial_move_for(cr, uid, move, context=context)
152         if move.picking_id.purchase_id and move.purchase_line_id:
153             pur_currency = move.purchase_line_id.order_id.currency_id.id
154             partial_move.update({
155                 'currency': pur_currency,
156                 'cost': move.purchase_line_id.price_unit
157             })
158         return partial_move
159
160     def __get_help_text(self, cursor, user, picking_id, context=None):
161         picking = self.pool.get('stock.picking').browse(cursor, user, picking_id, context=context)
162         if picking.purchase_id:
163             text = _("The proposed cost is made based on %s")
164             value = _("Purchase Order %s") % picking.purchase_id.name
165             if picking.purchase_id.pricelist_id.currency_id != picking.purchase_id.company_id.currency_id:
166                 value += _(" (currency rate of purchase order taken)")
167             if any([x.state not in ('draft', 'cancel') for x in picking.purchase_id.invoice_ids]):
168                 value = _("Invoices made on Purchase Order %s") % (picking.purchase_id.name)
169                 if picking.purchase_id.pricelist_id.currency_id != picking.purchase_id.company_id.currency_id:
170                     value += _(" (currency rate of invoices taken)")
171             return text % value
172         return super(stock_partial_picking, self).__get_help_text(cursor, user, picking_id, context=context)
173
174 # Redefinition of the new field in order to update the model stock.picking.in in the orm
175 # FIXME: this is a temporary workaround because of a framework bug (ref: lp996816). It should be removed as soon as
176 #        the bug is fixed
177 class stock_picking_in(osv.osv):
178     _inherit = 'stock.picking.in'
179     _columns = {
180         'purchase_id': fields.many2one('purchase.order', 'Purchase Order',
181             ondelete='set null', select=True),
182         'warehouse_id': fields.related('purchase_id', 'warehouse_id', type='many2one', relation='stock.warehouse', string='Destination Warehouse', readonly=True),
183     }
184 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: