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