[IMP]
[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, 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         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.sale_id and picking.sale_id.note:
68             if picking.note:
69                 return picking.note + '\n' + picking.sale_id.note
70             else:
71                 return picking.sale_id.note
72         return super(stock_picking, self)._get_comment_invoice(cursor, user,
73                 picking)
74
75     def _get_price_unit_invoice(self, cursor, user, move_line, type):
76         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
77             return move_line.sale_line_id.price_unit
78         return super(stock_picking, self)._get_price_unit_invoice(cursor,
79                 user, move_line, type)
80
81     def _get_discount_invoice(self, cursor, user, move_line):
82         if move_line.sale_line_id:
83             return move_line.sale_line_id.discount
84         return super(stock_picking, self)._get_discount_invoice(cursor, user,
85                 move_line)
86
87     def _get_taxes_invoice(self, cursor, user, move_line, type):
88         if move_line.sale_line_id and move_line.sale_line_id.product_id.id == move_line.product_id.id:
89             return [x.id for x in move_line.sale_line_id.tax_id]
90         return super(stock_picking, self)._get_taxes_invoice(cursor, user,
91                 move_line, type)
92
93     def _get_account_analytic_invoice(self, cursor, user, picking, move_line):
94         if picking.sale_id:
95             return picking.sale_id.project_id.id
96         return super(stock_picking, self)._get_account_analytic_invoice(cursor,
97                 user, picking, move_line)
98
99     def _invoice_line_hook(self, cursor, user, move_line, invoice_line_id):
100         sale_line_obj = self.pool.get('sale.order.line')
101         if move_line.sale_line_id:
102             sale_line_obj.write(cursor, user, [move_line.sale_line_id.id], {'invoiced':True,
103                 'invoice_lines': [(4, invoice_line_id)],
104                 })
105         return super(stock_picking, self)._invoice_line_hook(cursor, user,
106                 move_line, invoice_line_id)
107
108     def _invoice_hook(self, cursor, user, picking, invoice_id):
109         sale_obj = self.pool.get('sale.order')
110         if picking.sale_id:
111             sale_obj.write(cursor, user, [picking.sale_id.id], {
112                 'invoice_ids': [(4, invoice_id)],
113                 })
114         return super(stock_picking, self)._invoice_hook(cursor, user,
115                 picking, invoice_id)
116
117     def action_invoice_create(self, cursor, user, ids, journal_id=False,
118             group=False, type='out_invoice', context=None):
119         invoice_obj = self.pool.get('account.invoice')
120         picking_obj = self.pool.get('stock.picking')
121         invoice_line_obj = self.pool.get('account.invoice.line')
122
123         result = super(stock_picking, self).action_invoice_create(cursor, user,
124                 ids, journal_id=journal_id, group=group, type=type,
125                 context=context)
126
127         picking_ids = result.keys()
128         invoice_ids = result.values()
129
130         invoices = {}
131         for invoice in invoice_obj.browse(cursor, user, invoice_ids,
132                 context=context):
133             invoices[invoice.id] = invoice
134
135         for picking in picking_obj.browse(cursor, user, picking_ids,
136                 context=context):
137
138             if not picking.sale_id:
139                 continue
140             sale_lines = picking.sale_id.order_line
141             for sale_line in sale_lines:
142                 if sale_line.product_id.type == 'service' and sale_line.invoiced == False:
143                     if group:
144                         name = picking.name + '-' + sale_line.name
145                     else:
146                         name = sale_line.name
147                     if type in ('out_invoice', 'out_refund'):
148                         account_id = sale_line.product_id.product_tmpl_id.\
149                                 property_account_income.id
150                         if not account_id:
151                             account_id = sale_line.product_id.categ_id.\
152                                     property_account_income_categ.id
153                     else:
154                         account_id = sale_line.product_id.product_tmpl_id.\
155                                 property_account_expense.id
156                         if not account_id:
157                             account_id = sale_line.product_id.categ_id.\
158                                     property_account_expense_categ.id
159                     price_unit = sale_line.price_unit
160                     discount = sale_line.discount
161                     tax_ids = sale_line.tax_id
162                     tax_ids = map(lambda x: x.id, tax_ids)
163
164                     account_analytic_id = self._get_account_analytic_invoice(cursor,
165                             user, picking, sale_line)
166
167                     account_id = self.pool.get('account.fiscal.position').map_account(cursor, user, picking.sale_id.partner_id.property_account_position, account_id)
168                     invoice = invoices[result[picking.id]]
169                     invoice_line_id = invoice_line_obj.create(cursor, user, {
170                         'name': name,
171                         'invoice_id': invoice.id,
172                         'uos_id': sale_line.product_uos.id or sale_line.product_uom.id,
173                         'product_id': sale_line.product_id.id,
174                         'account_id': account_id,
175                         'price_unit': price_unit,
176                         'discount': discount,
177                         'quantity': sale_line.product_uos_qty,
178                         'invoice_line_tax_id': [(6, 0, tax_ids)],
179                         'account_analytic_id': account_analytic_id,
180                         }, context=context)
181                     self.pool.get('sale.order.line').write(cursor, user, [sale_line.id], {'invoiced':True,
182                         'invoice_lines': [(6, 0, [invoice_line_id])],
183                         })
184
185         return result
186
187
188 stock_picking()
189
190
191
192 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
193