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 purchase_line_invoice(osv.osv_memory):
28 """ To create invoice for purchase order line"""
30 _name = 'purchase.order.line_invoice'
31 _description = 'Purchase Order Line Make Invoice'
33 def makeInvoices(self, cr, uid, ids, context=None):
36 To get Purchase Order line and create Invoice
37 @param self: The object pointer.
38 @param cr: A database cursor
39 @param uid: ID of the user currently logged in
40 @param context: A standard dictionary
41 @return : retrun view of Invoice
47 record_ids = context.get('active_ids',[])
51 invoice_obj=self.pool.get('account.invoice')
52 purchase_line_obj=self.pool.get('purchase.order.line')
53 property_obj=self.pool.get('ir.property')
54 account_fiscal_obj=self.pool.get('account.fiscal.position')
55 invoice_line_obj=self.pool.get('account.invoice.line')
56 account_jrnl_obj=self.pool.get('account.journal')
58 def multiple_order_invoice_notes(orders):
61 notes += "%s \n" % order.notes
66 def make_invoice_by_partner(partner, orders, lines_ids):
68 create a new invoice for one supplier
69 @param partner : The object partner
70 @param orders : The set of orders to add in the invoice
71 @param lines : The list of line's id
73 name = orders and orders[0].name or ''
74 journal_id = account_jrnl_obj.search(cr, uid, [('type', '=', 'purchase')], context=None)
75 journal_id = journal_id and journal_id[0] or False
76 a = partner.property_account_payable.id
77 if partner and partner.property_payment_term.id:
78 pay_term = partner.property_payment_term.id
85 'journal_id':journal_id,
86 'reference' : partner.ref,
88 'partner_id': partner.id,
89 'invoice_line': [(6,0,lines_ids)],
90 'currency_id' : orders[0].pricelist_id.currency_id.id,
91 'comment': multiple_order_invoice_notes(orders),
92 'payment_term': pay_term,
93 'fiscal_position': partner.property_account_position.id
95 inv_id = invoice_obj.create(cr, uid, inv)
97 order.write({'invoice_ids': [(4, inv_id)]})
100 for line in purchase_line_obj.browse(cr,uid,record_ids):
101 if (not line.invoiced) and (line.state not in ('draft','cancel')):
102 if not line.partner_id.id in invoices:
103 invoices[line.partner_id.id] = []
105 a = line.product_id.product_tmpl_id.property_account_expense.id
107 a = line.product_id.categ_id.property_account_expense_categ.id
109 raise osv.except_osv(_('Error !'),
110 _('There is no expense account defined ' \
111 'for this product: "%s" (id:%d)') % \
112 (line.product_id.name, line.product_id.id,))
114 a = property_obj.get(cr, uid,
115 'property_account_expense_categ', 'product.category',
117 fpos = line.order_id.fiscal_position or False
118 a = account_fiscal_obj.map_account(cr, uid, fpos, a)
119 inv_id = invoice_line_obj.create(cr, uid, {
121 'origin': line.order_id.name,
123 'price_unit': line.price_unit,
124 'quantity': line.product_qty,
125 'uos_id': line.product_uom.id,
126 'product_id': line.product_id.id or False,
127 'invoice_line_tax_id': [(6, 0, [x.id for x in line.taxes_id])],
129 'account_analytic_id': line.account_analytic_id and line.account_analytic_id.id or False,
131 purchase_line_obj.write(cr, uid, [line.id], {'invoiced': True, 'invoice_lines': [(4, inv_id)]})
132 invoices[line.partner_id.id].append((line,inv_id))
135 for result in invoices.values():
136 il = map(lambda x: x[1], result)
137 orders = list(set(map(lambda x : x[0].order_id, result)))
139 res.append(make_invoice_by_partner(orders[0].partner_id, orders, il))
142 'domain': "[('id','in', ["+','.join(map(str,res))+"])]",
143 'name': _('Supplier Invoices'),
145 'view_mode': 'tree,form',
146 'res_model': 'account.invoice',
148 'context': "{'type':'in_invoice', 'journal_type': 'purchase'}",
149 'type': 'ir.actions.act_window'
151 purchase_line_invoice()
154 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: