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_name(orders):
61 name += "-%d" % order.id
64 def multiple_order_invoice_reference(partner, orders):
65 reference = "P%dPO" % partner.id
67 reference += "-%d" % order.id
70 def multiple_order_invoice_notes(orders):
73 notes += "%s \n" % order.notes
78 def make_invoice_by_partner(partner, orders, lines_ids):
80 create a new invoice for one supplier
81 @param partner : The object partner
82 @param orders : The set of orders to add in the invoice
83 @param lines : The list of line's id
85 journal_id = account_jrnl_obj.search(cr, uid, [('type', '=', 'purchase')], context=None)
86 journal_id = journal_id and journal_id[0] or False
87 a = partner.property_account_payable.id
88 if partner and partner.property_payment_term.id:
89 pay_term = partner.property_payment_term.id
93 'name': multiple_order_invoice_name(orders),
94 'origin': multiple_order_invoice_name(orders),
96 'journal_id':journal_id,
97 'reference': multiple_order_invoice_reference(partner, orders),
99 'partner_id': partner.id,
100 'address_invoice_id': orders[0].partner_address_id.id,
101 'address_contact_id': orders[0].partner_address_id.id,
102 'invoice_line': [(6,0,lines_ids)],
103 'currency_id' : orders[0].pricelist_id.currency_id.id,
104 'comment': multiple_order_invoice_notes(orders),
105 'payment_term': pay_term,
106 'fiscal_position': partner.property_account_position.id
108 inv_id = invoice_obj.create(cr, uid, inv)
110 order.write({'invoice_ids': [(4, inv_id)]})
113 for line in purchase_line_obj.browse(cr,uid,record_ids):
114 if (not line.invoiced) and (line.state not in ('draft','cancel')):
115 if not line.partner_id.id in invoices:
116 invoices[line.partner_id.id] = []
118 a = line.product_id.product_tmpl_id.property_account_expense.id
120 a = line.product_id.categ_id.property_account_expense_categ.id
122 raise osv.except_osv(_('Error !'),
123 _('There is no expense account defined ' \
124 'for this product: "%s" (id:%d)') % \
125 (line.product_id.name, line.product_id.id,))
127 a = property_obj.get(cr, uid,
128 'property_account_expense_categ', 'product.category',
130 fpos = line.order_id.fiscal_position or False
131 a = account_fiscal_obj.map_account(cr, uid, fpos, a)
132 inv_id = invoice_line_obj.create(cr, uid, {
134 'origin': line.order_id.name,
136 'price_unit': line.price_unit,
137 'quantity': line.product_qty,
138 'uos_id': line.product_uom.id,
139 'product_id': line.product_id.id or False,
140 'invoice_line_tax_id': [(6, 0, [x.id for x in line.taxes_id])],
142 'account_analytic_id': line.account_analytic_id and line.account_analytic_id.id or False,
144 purchase_line_obj.write(cr, uid, [line.id], {'invoiced': True, 'invoice_lines': [(4, inv_id)]})
145 invoices[line.partner_id.id].append((line,inv_id))
148 for result in invoices.values():
149 il = map(lambda x: x[1], result)
150 orders = list(set(map(lambda x : x[0].order_id, result)))
152 res.append(make_invoice_by_partner(orders[0].partner_id, orders, il))
155 'domain': "[('id','in', ["+','.join(map(str,res))+"])]",
156 'name': _('Supplier Invoices'),
158 'view_mode': 'tree,form',
159 'res_model': 'account.invoice',
161 'context': "{'type':'in_invoice', 'journal_type': 'purchase'}",
162 'type': 'ir.actions.act_window'
164 purchase_line_invoice()
167 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: