1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
23 from tools.translate import _
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):
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
39 @return: A dictionary which of fields with values.
42 if context is None: context = {}
46 #TODO: merge with sale.py/make_invoice
47 def make_invoice(order, lines):
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
65 'type': 'out_invoice',
66 'reference': "P%dSO%d" % (order.partner_id.id, order.id),
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
77 inv_id = self.pool.get('account.invoice').create(cr, uid, inv)
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,
90 invoices[line.order_id.id].append((line, lid))
91 sales_order_line_obj.write(cr, uid, [line.id],
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))
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:
107 wf_service.trg_validate(uid, 'sale.order', line.order_id.id, 'all_lines', cr)
108 sales_order_obj.write(cr, uid, [line.order_id.id], {'state': 'progress'})
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!'))
113 return {'type': 'ir.actions.act_window_close'}
115 sale_order_line_make_invoice()
117 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: