[FIX] Account : Chart of taxes should open move lines respecting the parent+children...
[odoo/odoo.git] / addons / sale / stock.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 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),
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         else:
48             return super(stock_picking, self).get_currency_id(cursor, user, picking)
49
50     def _get_payment_term(self, cursor, user, picking):
51         res = {}
52         if picking.sale_id and picking.sale_id.payment_term:
53             return picking.sale_id.payment_term.id
54         return super(stock_picking, self)._get_payment_term(cursor,
55                 user, picking)
56
57     def _get_address_invoice(self, cursor, user, picking):
58         res = {}
59         if picking.sale_id:
60             res['contact'] = picking.sale_id.partner_order_id.id
61             res['invoice'] = picking.sale_id.partner_invoice_id.id
62             return res
63         return super(stock_picking, self)._get_address_invoice(cursor,
64                 user, picking)
65
66     def _get_comment_invoice(self, cursor, user, picking):
67         if picking.note or (picking.sale_id and picking.sale_id.note):
68             return picking.note or picking.sale_id.note
69         return super(stock_picking, self)._get_comment_invoice(cursor, user,
70                 picking)
71
72     def _get_price_unit_invoice(self, cursor, user, move_line, type):
73         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
74             return move_line.sale_line_id.price_unit
75         return super(stock_picking, self)._get_price_unit_invoice(cursor,
76                 user, move_line, type)
77
78     def _get_discount_invoice(self, cursor, user, move_line):
79         if move_line.sale_line_id:
80             return move_line.sale_line_id.discount
81         return super(stock_picking, self)._get_discount_invoice(cursor, user,
82                 move_line)
83
84     def _get_taxes_invoice(self, cursor, user, move_line, type):
85         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
86             return [x.id for x in move_line.sale_line_id.tax_id]
87         return super(stock_picking, self)._get_taxes_invoice(cursor, user,
88                 move_line, type)
89
90     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
91         if picking.sale_id:
92             return picking.sale_id.project_id.id
93         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
94                 user, picking, move_line)
95
96     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
97         sale_line_obj = self.pool.get('sale.order.line')
98         if move_line.sale_line_id:
99             sale_line_obj.write(cursor, user, [move_line.sale_line_id.id], {'invoiced':True,
100                 'invoice_lines': [(4, invoice_line_id)],
101                 })
102         return super(stock_picking, self)._invoice_line_hook(cursor, user,
103                 move_line, invoice_line_id)
104
105     def _invoice_hook(self, cursor, user, picking, invoice_id):
106         sale_obj = self.pool.get('sale.order')
107         if picking.sale_id:
108             sale_obj.write(cursor, user, [picking.sale_id.id], {
109                 'invoice_ids': [(4, invoice_id)],
110                 })
111         return super(stock_picking, self)._invoice_hook(cursor, user,
112                 picking, invoice_id)
113
114     def action_invoice_create(self, cursor, user, ids, journal_id=False,
115             group=False, type='out_invoice', context=None):
116         invoice_obj = self.pool.get('account.invoice')
117         picking_obj = self.pool.get('stock.picking')
118         invoice_line_obj = self.pool.get('account.invoice.line')
119
120         result = super(stock_picking, self).action_invoice_create(cursor, user,
121                 ids, journal_id=journal_id, group=group, type=type,
122                 context=context)
123
124         picking_ids = result.keys()
125         invoice_ids = result.values()
126
127         invoices = {}
128         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
129                 context=context):
130             invoices[invoice.id] = invoice
131             
132         for picking in picking_obj.browse(cursor, user, picking_ids,
133                 context=context):
134
135             if not picking.sale_id:
136                 continue
137             sale_lines = picking.sale_id.order_line
138             invoice_created = invoices[result[picking.id]]
139
140             for inv in invoice_obj.browse(cursor, user, [invoice_created.id], context=context):
141                 if (not inv.fiscal_position) and picking.sale_id.fiscal_position:
142                     invoice_obj.write(cursor, user, [inv.id], {'fiscal_position': picking.sale_id.fiscal_position.id}, context=context)
143             
144             if picking.sale_id.client_order_ref:
145                 inv_name = picking.sale_id.client_order_ref + " : " + invoice_created.name
146                 invoice_obj.write(cursor, user, [invoice_created.id], {'name': inv_name}, context=context)
147             
148             for sale_line in sale_lines:
149                 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
150                     if group:
151                         name = picking.name + '-' + sale_line.name
152                     else:
153                         name = sale_line.name
154                     if type in ('out_invoice', 'out_refund'):
155                         account_id = sale_line.product_id.product_tmpl_id.\
156                                 property_account_income.id
157                         if not account_id:
158                             account_id = sale_line.product_id.categ_id.\
159                                     property_account_income_categ.id
160                     else:
161                         account_id = sale_line.product_id.product_tmpl_id.\
162                                 property_account_expense.id
163                         if not account_id:
164                             account_id = sale_line.product_id.categ_id.\
165                                     property_account_expense_categ.id
166                     price_unit = sale_line.price_unit
167                     discount = sale_line.discount
168                     tax_ids = sale_line.tax_id
169                     tax_ids = map(lambda x: x.id, tax_ids)
170
171                     account_analytic_id = self._get_account_analytic_invoice(cursor,
172                             user, picking, sale_line)
173
174                     account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, picking.sale_id.partner_id.property_account_position, account_id)
175                     invoice = invoices[result[picking.id]]
176                     invoice_line_id = invoice_line_obj.create(cursor, user, {
177                         'name': name,
178                         'invoice_id': invoice.id,
179                         'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
180                         'product_id': sale_line.product_id.id,
181                         'account_id': account_id,
182                         'price_unit': price_unit,
183                         'discount': discount,
184                         'quantity': sale_line.product_uos_qty,
185                         'invoice_line_tax_id': [(6, 0, tax_ids)],
186                         'account_analytic_id': account_analytic_id,
187                         }, context=context)
188                     self.pool.get('sale.order.line').write(cursor, user, [sale_line.id], {'invoiced':True,
189                         'invoice_lines': [(6, 0, [invoice_line_id])],
190                         })
191
192         return result
193
194     def action_cancel(self, cr, uid, ids, context={}):
195         res = super(stock_picking, self).action_cancel(cr, uid, ids, context=context)
196         for pick in self.browse(cr, uid, ids, context):
197             call_ship_end = True
198             if pick.sale_id:
199                 for picks in pick.sale_id.picking_ids:
200                     if picks.state not in ('done','cancel'):
201                         call_ship_end = False
202                         break
203                 if call_ship_end:
204                     self.pool.get('sale.order').action_ship_end(cr, uid, [pick.sale_id.id], context)    
205         return res
206 stock_picking()
207
208
209
210 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
211