Modularize the selection of the partner to invoice when invoicing from the picking.
[odoo/odoo.git] / addons / sale / 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         'sale_line_id': fields.many2one('sale.order.line', 'Sales Order Line', ondelete='set null', select=True, readonly=True),
28     }
29
30     def _prepare_chained_picking(self, cr, uid, picking_name, picking, picking_type, moves_todo, context=None):
31         values = super(stock_move, self)._prepare_chained_picking(cr, uid, picking_name, picking, picking_type, moves_todo, context=context)
32         if picking.sale_id:
33             values['sale_id'] = picking.sale_id.id
34         return values
35
36 class stock_picking(osv.osv):
37     _inherit = 'stock.picking'
38     _columns = {
39         'sale_id': fields.many2one('sale.order', 'Sales Order', ondelete='set null', select=True),
40     }
41     _defaults = {
42         'sale_id': False
43     }
44
45     def get_currency_id(self, cursor, user, picking):
46         if picking.sale_id:
47             return picking.sale_id.pricelist_id.currency_id.id
48         else:
49             return super(stock_picking, self).get_currency_id(cursor, user, picking)
50
51     def _get_partner_to_invoice(self, cr, uid, picking, context=None):
52         if picking.sale_id:
53             return picking.sale_id.partner_id
54         return super(stock_picking, self)._get_partner_to_invoice(cr, uid, picking, context=context)
55
56     def _get_payment_term(self, cursor, user, picking):
57         if picking.sale_id and picking.sale_id.payment_term:
58             return picking.sale_id.payment_term.id
59         return super(stock_picking, self)._get_payment_term(cursor, user, picking)
60
61     def _get_address_invoice(self, cursor, user, picking):
62         res = {}
63         if picking.sale_id:
64             res['contact'] = picking.sale_id.partner_order_id.id
65             res['invoice'] = picking.sale_id.partner_invoice_id.id
66             return res
67         return super(stock_picking, self)._get_address_invoice(cursor, user, picking)
68
69     def _get_comment_invoice(self, cursor, user, picking):
70         if picking.note or (picking.sale_id and picking.sale_id.note):
71             return picking.note or picking.sale_id.note
72         return super(stock_picking, self)._get_comment_invoice(cursor, user, picking)
73
74     def _get_price_unit_invoice(self, cursor, user, move_line, type):
75         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
76             uom_id = move_line.product_id.uom_id.id
77             uos_id = move_line.product_id.uos_id and move_line.product_id.uos_id.id or False
78             price = move_line.sale_line_id.price_unit
79             coeff = move_line.product_id.uos_coeff
80             if uom_id != uos_id  and coeff != 0:
81                 price_unit = price / coeff
82                 return price_unit
83             return move_line.sale_line_id.price_unit
84         return super(stock_picking, self)._get_price_unit_invoice(cursor, user, move_line, type)
85
86     def _get_discount_invoice(self, cursor, user, move_line):
87         if move_line.sale_line_id:
88             return move_line.sale_line_id.discount
89         return super(stock_picking, self)._get_discount_invoice(cursor, user, move_line)
90
91     def _get_taxes_invoice(self, cursor, user, move_line, type):
92         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
93             return [x.id for x in move_line.sale_line_id.tax_id]
94         return super(stock_picking, self)._get_taxes_invoice(cursor, user, move_line, type)
95
96     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
97         if picking.sale_id:
98             return picking.sale_id.project_id.id
99         return super(stock_picking, self)._get_account_analytic_invoice(cursor, user, picking, move_line)
100
101     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
102         sale_line_obj = self.pool.get('sale.order.line')
103         invoice_line_obj = self.pool.get('account.invoice.line')
104         if move_line.sale_line_id:
105             sale_line_obj.write(cursor, user, [move_line.sale_line_id.id],
106                                     {
107                                         'invoiced': True,
108                                         'invoice_lines': [(4, invoice_line_id)],
109                                     })
110             invoice_line_obj.write(cursor, user, [invoice_line_id], {'note':  move_line.sale_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         sale_obj = self.pool.get('sale.order')
115         if picking.sale_id:
116             sale_obj.write(cursor, user, [picking.sale_id.id], {
117                 'invoice_ids': [(4, invoice_id)],
118                 })
119         return super(stock_picking, self)._invoice_hook(cursor, user, picking, invoice_id)
120
121     def action_invoice_create(self, cursor, user, ids, journal_id=False,
122             group=False, type='out_invoice', context=None):
123         if context is None:
124             context = {}
125         invoice_obj = self.pool.get('account.invoice')
126         picking_obj = self.pool.get('stock.picking')
127         invoice_line_obj = self.pool.get('account.invoice.line')
128         fiscal_position_obj = self.pool.get('account.fiscal.position')
129         order_line_obj = self.pool.get('sale.order.line')
130
131         result = super(stock_picking, self).action_invoice_create(cursor, user,
132                 ids, journal_id=journal_id, group=group, type=type,
133                 context=context)
134         picking_ids = result.keys()
135         invoice_ids = result.values()
136         invoices = {}
137         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
138                 context=context):
139             invoices[invoice.id] = invoice
140
141         for picking in picking_obj.browse(cursor, user, picking_ids,
142                 context=context):
143             if not picking.sale_id or picking.backorder_id:
144                 continue
145             sale_lines = picking.sale_id.order_line
146             invoice_created = invoices[result[picking.id]]
147             if picking.sale_id.user_id:
148                 invoice_obj.write(cursor, user, [invoice_created.id], {'user_id': picking.sale_id.user_id.id}, context=context)
149
150             if picking.sale_id.fiscal_position:
151                 invoice_obj.write(cursor, user, [invoice_created.id], {'fiscal_position': picking.sale_id.fiscal_position.id}, context=context)
152             if picking.sale_id.client_order_ref:
153                 inv_name = picking.sale_id.client_order_ref + " : " + invoice_created.name
154                 invoice_obj.write(cursor, user, [invoice_created.id], {'name': inv_name}, context=context)
155             for sale_line in sale_lines:
156                 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
157                     if not type:
158                         type = context.get('inv_type', False)
159                     if group:
160                         name = picking.name + '-' + sale_line.name
161                     else:
162                         name = sale_line.name
163                     if type in ('out_invoice', 'out_refund'):
164                         account_id = sale_line.product_id.product_tmpl_id.\
165                                 property_account_income.id
166                         if not account_id:
167                             account_id = sale_line.product_id.categ_id.\
168                                     property_account_income_categ.id
169                     else:
170                         account_id = sale_line.product_id.product_tmpl_id.\
171                                 property_account_expense.id
172                         if not account_id:
173                             account_id = sale_line.product_id.categ_id.\
174                                     property_account_expense_categ.id
175                     price_unit = sale_line.price_unit
176                     discount = sale_line.discount
177                     tax_ids = sale_line.tax_id
178                     tax_ids = map(lambda x: x.id, tax_ids)
179
180                     account_analytic_id = self._get_account_analytic_invoice(cursor,
181                             user, picking, sale_line)
182
183                     account_id = fiscal_position_obj.map_account(cursor, user, picking.sale_id.partner_id.property_account_position, account_id)
184                     invoice = invoices[result[picking.id]]
185                     invoice_line_id = invoice_line_obj.create(cursor, user, {
186                         'name': name,
187                         'invoice_id': invoice.id,
188                         'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
189                         'product_id': sale_line.product_id.id,
190                         'account_id': account_id,
191                         'price_unit': price_unit,
192                         'discount': discount,
193                         'quantity': sale_line.product_uos_qty,
194                         'invoice_line_tax_id': [(6, 0, tax_ids)],
195                         'account_analytic_id': account_analytic_id,
196                         'notes':sale_line.notes
197                     }, context=context)
198                     order_line_obj.write(cursor, user, [sale_line.id], {'invoiced': True,
199                         'invoice_lines': [(6, 0, [invoice_line_id])],
200                     })
201         return result
202
203 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: