[IMP] Changed all module categories, limited number of categories
[odoo/odoo.git] / addons / purchase / wizard / purchase_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
25
26 class purchase_line_invoice(osv.osv_memory):
27
28     """ To create invoice for purchase order line"""
29
30     _name = 'purchase.order.line_invoice'
31     _description = 'Purchase Order Line Make Invoice'
32
33     def makeInvoices(self, cr, uid, ids, context=None):
34
35         """
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
42         """
43
44         if context is None:
45             context={}
46
47         record_ids =  context.get('active_ids',[])
48         if record_ids:
49             res = False
50             invoices = {}
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')
57
58             def multiple_order_invoice_name(orders):
59                 name = "PO";
60                 for order in orders:
61                     name += "-%d" % order.id
62                 return name
63
64             def multiple_order_invoice_reference(partner, orders):
65                 reference = "P%dPO" % partner.id
66                 for order in orders:
67                     reference += "-%d" % order.id
68                 return reference
69
70             def multiple_order_invoice_notes(orders):
71                 notes = ""
72                 for order in orders:
73                     notes += "%s \n" % order.notes
74                 return notes
75
76
77
78             def make_invoice_by_partner(partner, orders, lines_ids):
79                 """
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
84                 """
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
90                 else:
91                     pay_term = False
92                 inv = {
93                     'name': multiple_order_invoice_name(orders),
94                     'origin': multiple_order_invoice_name(orders),
95                     'type': 'in_invoice',
96                     'journal_id':journal_id,
97                     'reference': multiple_order_invoice_reference(partner, orders),
98                     'account_id': a,
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
107                 }
108                 inv_id = invoice_obj.create(cr, uid, inv)
109                 for order in orders:
110                     order.write({'invoice_ids': [(4, inv_id)]})
111                 return inv_id
112
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] = []
117                     if line.product_id:
118                         a = line.product_id.product_tmpl_id.property_account_expense.id
119                         if not a:
120                             a = line.product_id.categ_id.property_account_expense_categ.id
121                         if not a:
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,))
126                     else:
127                         a = property_obj.get(cr, uid,
128                                 'property_account_expense_categ', 'product.category',
129                                 context=context).id
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, {
133                         'name': line.name,
134                         'origin': line.order_id.name,
135                         'account_id': a,
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])],
141                         'note': line.notes,
142                         'account_analytic_id': line.account_analytic_id and line.account_analytic_id.id or False,
143                     })
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))
146
147             res = []
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)))
151
152                 res.append(make_invoice_by_partner(orders[0].partner_id, orders, il))
153
154         return {
155             'domain': "[('id','in', ["+','.join(map(str,res))+"])]",
156             'name': _('Supplier Invoices'),
157             'view_type': 'form',
158             'view_mode': 'tree,form',
159             'res_model': 'account.invoice',
160             'view_id': False,
161             'context': "{'type':'in_invoice', 'journal_type': 'purchase'}",
162             'type': 'ir.actions.act_window'
163         }
164 purchase_line_invoice()
165
166
167 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
168