fixed bug:308803
[odoo/odoo.git] / addons / sale / stock.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import osv, fields
24
25 class stock_move(osv.osv):
26     _inherit = 'stock.move'
27     _columns = {
28         'sale_line_id': fields.many2one('sale.order.line', 'Sale Order Line', ondelete='set null', select=True, readonly=True),
29     }
30     _defaults = {
31         'sale_line_id': lambda *a:False
32     }
33 stock_move()
34
35 class stock_picking(osv.osv):
36     _inherit = 'stock.picking'
37     _columns = {
38         'sale_id': fields.many2one('sale.order', 'Sale Order', ondelete='set null', select=True, readonly=True),
39     }
40     _defaults = {
41         'sale_id': lambda *a: False
42     }
43
44     def get_currency_id(self, cursor, user, picking):
45         if picking.sale_id:
46             return picking.sale_id.pricelist_id.currency_id.id
47         return False
48
49     def _get_payment_term(self, cursor, user, picking):
50         res = {}
51         if picking.sale_id and picking.sale_id.payment_term:
52             return picking.sale_id.payment_term.id
53         return super(stock_picking, self)._get_payment_term(cursor,
54                 user, picking)
55
56     def _get_address_invoice(self, cursor, user, picking):
57         res = {}
58         if picking.sale_id:
59             res['contact'] = picking.sale_id.partner_order_id.id
60             res['invoice'] = picking.sale_id.partner_invoice_id.id
61             return res
62         return super(stock_picking, self)._get_address_invoice(cursor,
63                 user, picking)
64
65     def _get_comment_invoice(self, cursor, user, picking):
66         if picking.sale_id and picking.sale_id.note:
67             if picking.note:
68                 return picking.note + '\n' + picking.sale_id.note
69             else:
70                 return picking.sale_id.note
71         return super(stock_picking, self)._get_comment_invoice(cursor, user,
72                 picking)
73
74     def _get_price_unit_invoice(self, cursor, user, move_line, type):
75         if move_line.sale_line_id:
76             return move_line.sale_line_id.price_unit
77         return super(stock_picking, self)._get_price_unit_invoice(cursor,
78                 user, move_line, type)
79
80     def _get_discount_invoice(self, cursor, user, move_line):
81         if move_line.sale_line_id:
82             return move_line.sale_line_id.discount
83         return super(stock_picking, self)._get_discount_invoice(cursor, user,
84                 move_line)
85
86     def _get_taxes_invoice(self, cursor, user, move_line, type):
87         if move_line.sale_line_id:
88             return [x.id for x in move_line.sale_line_id.tax_id]
89         return super(stock_picking, self)._get_taxes_invoice(cursor, user,
90                 move_line, type)
91
92     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
93         if picking.sale_id:
94             return picking.sale_id.project_id.id
95         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
96                 user, picking, move_line)
97
98     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
99         sale_line_obj = self.pool.get('sale.order.line')
100         if move_line.sale_line_id:
101             sale_line_obj.write(cursor, user, [move_line.sale_line_id.id], {'invoiced':True,
102                 'invoice_lines': [(4, invoice_line_id)],
103                 })
104         return super(stock_picking, self)._invoice_line_hook(cursor, user,
105                 move_line, invoice_line_id)
106
107     def _invoice_hook(self, cursor, user, picking, invoice_id):
108         sale_obj = self.pool.get('sale.order')
109         if picking.sale_id:
110             sale_obj.write(cursor, user, [picking.sale_id.id], {
111                 'invoice_ids': [(4, invoice_id)],
112                 })
113         return super(stock_picking, self)._invoice_hook(cursor, user,
114                 picking, invoice_id)
115
116     def action_invoice_create(self, cursor, user, ids, journal_id=False,
117             group=False, type='out_invoice', context=None):
118         invoice_obj = self.pool.get('account.invoice')
119         picking_obj = self.pool.get('stock.picking')
120         invoice_line_obj = self.pool.get('account.invoice.line')
121
122         result = super(stock_picking, self).action_invoice_create(cursor, user,
123                 ids, journal_id=journal_id, group=group, type=type,
124                 context=context)
125
126         picking_ids = result.keys()
127         invoice_ids = result.values()
128
129         invoices = {}
130         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
131                 context=context):
132             invoices[invoice.id] = invoice
133
134         for picking in picking_obj.browse(cursor, user, picking_ids,
135                 context=context):
136
137             if not picking.sale_id:
138                 continue
139             sale_lines = picking.sale_id.order_line
140             for sale_line in sale_lines:
141                 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
142                     if group:
143                         name = picking.name + '-' + sale_line.name
144                     else:
145                         name = sale_line.name
146                     if type in ('out_invoice', 'out_refund'):
147                         account_id = sale_line.product_id.product_tmpl_id.\
148                                 property_account_income.id
149                         if not account_id:
150                             account_id = sale_line.product_id.categ_id.\
151                                     property_account_income_categ.id
152                     else:
153                         account_id = sale_line.product_id.product_tmpl_id.\
154                                 property_account_expense.id
155                         if not account_id:
156                             account_id = sale_line.product_id.categ_id.\
157                                     property_account_expense_categ.id
158                     price_unit = sale_line.price_unit
159                     discount = sale_line.discount
160                     tax_ids = sale_line.tax_id
161
162                     account_analytic_id = self._get_account_analytic_invoice(cursor,
163                             user, picking, sale_line)
164
165                     account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, picking.sale_id.partner_id, account_id)
166                     invoice = invoices[result[picking.id]]
167                     invoice_line_id = invoice_line_obj.create(cursor, user, {
168                         'name': name,
169                         'invoice_id': invoice.id,
170                         'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
171                         'product_id': sale_line.product_id.id,
172                         'account_id': account_id,
173                         'price_unit': price_unit,
174                         'discount': discount,
175                         'quantity': sale_line.product_uos_qty,
176                         'invoice_line_tax_id': [(6, 0, tax_ids)],
177                         'account_analytic_id': account_analytic_id,
178                         }, context=context)
179                     self.pool.get('sale.order.line').write(cursor, user, [sale_line.id], {'invoiced':True,
180                         'invoice_lines': [(6, 0, [invoice_line_id])],
181                         })
182
183         return result
184
185
186 stock_picking()
187
188
189
190 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
191