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 ##############################################################################
22 from openerp.osv import osv, fields
23 from openerp.tools.translate import _
24 from openerp import netsvc
26 class sale_order_line_make_invoice(osv.osv_memory):
27 _name = "sale.order.line.make.invoice"
28 _description = "Sale OrderLine Make_invoice"
30 def make_invoices(self, cr, uid, ids, context=None):
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
40 @return: A dictionary which of fields with values.
43 if context is None: context = {}
47 #TODO: merge with sale.py/make_invoice
48 def make_invoice(order, lines):
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
66 'type': 'out_invoice',
67 'reference': "P%dSO%d" % (order.partner_id.id, order.id),
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(),
79 inv_id = self.pool.get('account.invoice').create(cr, uid, inv)
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])
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))
97 sales_order_obj.message_post(cr, uid, [order.id], body=_("Invoice created"), context=context)
98 data_sale = sales_order_obj.browse(cr, uid, order.id, context=context)
99 for line in data_sale.order_line:
100 if not line.invoiced:
104 wf_service.trg_validate(uid, 'sale.order', order.id, 'manual_invoice', cr)
105 sales_order_obj.write(cr, uid, [order.id], {'state': 'progress'})
108 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!'))
109 if context.get('open_invoices', False):
110 return self.open_invoices(cr, uid, ids, res, context=context)
111 return {'type': 'ir.actions.act_window_close'}
113 def open_invoices(self, cr, uid, ids, invoice_ids, context=None):
114 """ open a view on one of the given invoice_ids """
115 ir_model_data = self.pool.get('ir.model.data')
116 form_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_form')
117 form_id = form_res and form_res[1] or False
118 tree_res = ir_model_data.get_object_reference(cr, uid, 'account', 'invoice_tree')
119 tree_id = tree_res and tree_res[1] or False
122 'name': _('Invoice'),
124 'view_mode': 'form,tree',
125 'res_model': 'account.invoice',
126 'res_id': invoice_ids,
128 'views': [(form_id, 'form'), (tree_id, 'tree')],
129 'context': {'type': 'out_invoice'},
130 'type': 'ir.actions.act_window',
134 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: