[FIX] sale: when invoicing on lines, set order in progress when all lines invoiced
[odoo/odoo.git] / addons / sale / wizard / sale_line_invoice.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 openerp.osv import osv, fields
23 from openerp.tools.translate import _
24 from openerp import netsvc
25
26 class sale_order_line_make_invoice(osv.osv_memory):
27     _name = "sale.order.line.make.invoice"
28     _description = "Sale OrderLine Make_invoice"
29     
30     def make_invoices(self, cr, uid, ids, context=None):
31         """
32              To make invoices.
33
34              @param self: The object pointer.
35              @param cr: A database cursor
36              @param uid: ID of the user currently logged in
37              @param ids: the ID or list of IDs
38              @param context: A standard dictionary
39
40              @return: A dictionary which of fields with values.
41
42         """
43         if context is None: context = {}
44         res = False
45         invoices = {}
46
47     #TODO: merge with sale.py/make_invoice
48         def make_invoice(order, lines):
49             """
50                  To make invoices.
51
52                  @param order:
53                  @param lines:
54
55                  @return:
56
57             """
58             a = order.partner_id.property_account_receivable.id
59             if order.partner_id and order.partner_id.property_payment_term.id:
60                 pay_term = order.partner_id.property_payment_term.id
61             else:
62                 pay_term = False
63             inv = {
64                 'name': order.client_order_ref or '',
65                 'origin': order.name,
66                 'type': 'out_invoice',
67                 'reference': "P%dSO%d" % (order.partner_id.id, order.id),
68                 'account_id': a,
69                 'partner_id': order.partner_invoice_id.id,
70                 'invoice_line': [(6, 0, lines)],
71                 'currency_id' : order.pricelist_id.currency_id.id,
72                 'comment': order.note,
73                 'payment_term': pay_term,
74                 'fiscal_position': order.fiscal_position.id or order.partner_id.property_account_position.id,
75                 'user_id': order.user_id and order.user_id.id or False,
76                 'company_id': order.company_id and order.company_id.id or False,
77                 'date_invoice': fields.date.today(),
78             }
79             inv_id = self.pool.get('account.invoice').create(cr, uid, inv)
80             return inv_id
81
82         sales_order_line_obj = self.pool.get('sale.order.line')
83         sales_order_obj = self.pool.get('sale.order')
84         wf_service = netsvc.LocalService('workflow')
85         for line in sales_order_line_obj.browse(cr, uid, context.get('active_ids', []), context=context):
86             if (not line.invoiced) and (line.state not in ('draft', 'cancel')):
87                 if not line.order_id in invoices:
88                     invoices[line.order_id] = []
89                 line_id = sales_order_line_obj.invoice_line_create(cr, uid, [line.id])
90                 for lid in line_id:
91                     invoices[line.order_id].append(lid)
92         for order, il in invoices.items():
93             res = make_invoice(order, il)
94             cr.execute('INSERT INTO sale_order_invoice_rel \
95                     (order_id,invoice_id) values (%s,%s)', (order.id, res))
96             flag = True
97             data_sale = sales_order_obj.browse(cr, uid, order.id, context=context)
98             for line in data_sale.order_line:
99                 if not line.invoiced:
100                     flag = False
101                     break
102             if flag:
103                 line.order_id.write({'state': 'progress'})
104                 wf_service.trg_validate(uid, 'sale.order', order.id, 'all_lines', cr)
105
106         if not invoices:
107             raise osv.except_osv(_('Warning!'), _('Invoice cannot be created for this Sales Order Line due to one of the following reasons:\n1.The state of this sales order line is either "draft" or "cancel"!\n2.The Sales Order Line is Invoiced!'))
108         if context.get('open_invoices', False):
109             return self.open_invoices(cr, uid, ids, res, context=context)
110         return {'type': 'ir.actions.act_window_close'}
111
112     def open_invoices(self, cr, uid, ids, invoice_ids, context=None):
113         """ open a view on one of the given invoice_ids """
114         ir_model_data = self.pool.get('ir.model.data')
115         form_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_form')
116         form_id = form_res and form_res[1] or False
117         tree_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_tree')
118         tree_id = tree_res and tree_res[1] or False
119
120         return {
121             'name': _('Invoice'),
122             'view_type': 'form',
123             'view_mode': 'form,tree',
124             'res_model': 'account.invoice',
125             'res_id': invoice_ids,
126             'view_id': False,
127             'views': [(form_id, 'form'), (tree_id, 'tree')],
128             'context': {'type': 'out_invoice'},
129             'type': 'ir.actions.act_window',
130         }
131
132 sale_order_line_make_invoice()
133
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: