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