[MERGE] Sync with trunk
[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 osv import osv
23 from tools.translate import _
24 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     def make_invoices(self, cr, uid, ids, context=None):
30         """
31              To make invoices.
32
33              @param self: The object pointer.
34              @param cr: A database cursor
35              @param uid: ID of the user currently logged in
36              @param ids: the ID or list of IDs
37              @param context: A standard dictionary
38
39              @return: A dictionary which of fields with values.
40
41         """
42         if context is None: context = {}
43         res = False
44         invoices = {}
45
46     #TODO: merge with sale.py/make_invoice
47         def make_invoice(order, lines):
48             """
49                  To make invoices.
50
51                  @param order:
52                  @param lines:
53
54                  @return:
55
56             """
57             a = order.partner_id.property_account_receivable.id
58             if order.partner_id and order.partner_id.property_payment_term.id:
59                 pay_term = order.partner_id.property_payment_term.id
60             else:
61                 pay_term = False
62             inv = {
63                 'name': order.name,
64                 'origin': order.name,
65                 'type': 'out_invoice',
66                 'reference': "P%dSO%d" % (order.partner_id.id, order.id),
67                 'account_id': a,
68                 'partner_id': order.partner_id.id,
69                 'invoice_line': [(6, 0, lines)],
70                 'currency_id' : order.pricelist_id.currency_id.id,
71                 'comment': order.note,
72                 'payment_term': pay_term,
73                 'fiscal_position': order.fiscal_position.id or order.partner_id.property_account_position.id,
74                 'user_id': order.user_id and order.user_id.id or False,
75                 'company_id': order.company_id and order.company_id.id or False
76             }
77             inv_id = self.pool.get('account.invoice').create(cr, uid, inv)
78             return inv_id
79
80         sales_order_line_obj = self.pool.get('sale.order.line')
81         sales_order_obj = self.pool.get('sale.order')
82         wf_service = netsvc.LocalService('workflow')
83         for line in sales_order_line_obj.browse(cr, uid, context.get('active_ids', []), context=context):
84             if (not line.invoiced) and (line.state not in ('draft', 'cancel')):
85                 if not line.order_id.id in invoices:
86                     invoices[line.order_id.id] = []
87                 line_id = sales_order_line_obj.invoice_line_create(cr, uid,
88                         [line.id])
89                 for lid in line_id:
90                     invoices[line.order_id.id].append((line, lid))
91                 sales_order_line_obj.write(cr, uid, [line.id],
92                         {'invoiced': True})
93         for result in invoices.values():
94             order = result[0][0].order_id
95             il = map(lambda x: x[1], result)
96             res = make_invoice(order, il)
97             cr.execute('INSERT INTO sale_order_invoice_rel \
98                     (order_id,invoice_id) values (%s,%s)', (order.id, res))
99
100             flag = True
101             data_sale = sales_order_obj.browse(cr, uid, line.order_id.id, context=context)
102             for line in data_sale.order_line:
103                 if not line.invoiced:
104                     flag = False
105                     break
106             if flag:
107                 wf_service.trg_validate(uid, 'sale.order', line.order_id.id, 'manual_invoice', cr)
108                 sales_order_obj.write(cr, uid, [line.order_id.id], {'state': 'progress'})
109
110         if not invoices:
111             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!'))
112
113         return {'type': 'ir.actions.act_window_close'}
114
115 sale_order_line_make_invoice()
116
117 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: