1 ##############################################################################
3 # Copyright (c) 2004-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ##############################################################################
35 # Create an invoice based on selected timesheet lines
39 # TODO: check unit of measure !!!
41 class invoice_create(wizard.interface):
42 def _get_accounts(self, cr, uid, data, context):
43 if not len(data['ids']):
45 cr.execute("SELECT distinct(account_id) from account_analytic_line where id IN (%s)"% (','.join(map(str,data['ids'])),))
46 account_ids = cr.fetchall()
47 return {'accounts': [x[0] for x in account_ids]}
49 def _do_create(self, cr, uid, data, context):
50 pool = pooler.get_pool(cr.dbname)
51 account_ids = data['form']['accounts'][0][2]
53 for account in pool.get('account.analytic.account').browse(cr, uid, account_ids, context):
54 partner = account.partner_id
55 if (not partner) or not (account.pricelist_id):
56 raise wizard.except_wizard('Analytic account incomplete', 'Please fill in the partner and pricelist field in the analytic account:\n%s' % (account.name,))
59 'name': time.strftime('%D')+' - '+account.name,
60 'partner_id': account.partner_id.id,
61 'address_contact_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['contact'])['contact'],
62 'address_invoice_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['invoice'])['invoice'],
63 'payment_term': (partner.property_payment_term and partner.property_payment_term[0]) or False,
64 'account_id': partner.property_account_receivable[0],
65 'currency_id': account.pricelist_id.currency_id.id,
67 last_invoice = pool.get('account.invoice').create(cr, uid, curr_invoice)
68 invoices.append(last_invoice)
70 context2=context.copy()
71 context2['lang'] = partner.lang
72 cr.execute("SELECT product_id,to_invoice,sum(unit_amount) FROM account_analytic_line as line WHERE account_id = %d and id IN (%s) GROUP BY product_id,to_invoice" % (account.id, ','.join(map(str,data['ids']))))
73 for product_id,factor_id,qty in cr.fetchall():
74 product = pool.get('product.product').browse(cr, uid, product_id, context2)
76 raise wizard.except_wizard('Error', 'At least on line have no product !')
78 factor = pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context2)
79 if factor.customer_name:
80 factor_name = product.name+' - '+factor.customer_name
82 factor_name = product.name
83 if account.pricelist_id:
84 pl = account.pricelist_id.id
85 price = pool.get('product.pricelist').price_get(cr,uid,[pl], product_id, qty or 1.0, account.partner_id.id)[pl]
89 taxes = product.taxes_id
90 taxep = account.partner_id.property_account_tax
92 tax = [x.id for x in taxes or []]
95 tp = pool.get('account.tax').browse(cr, uid, taxep[0])
97 if not t.tax_group==tp.tax_group:
100 account_id = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ
105 'discount':factor.factor,
106 'invoice_line_tax_id': [(6,0,tax )],
107 'invoice_id': last_invoice,
109 'product_id': data['form']['product'] or product_id,
110 'invoice_line_tax_id': [(6,0,tax)],
111 'uos_id': product.uom_id.id,
112 'account_id': account_id[0],
113 'account_analytic_id': account.id,
119 cr.execute("SELECT * FROM account_analytic_line WHERE account_id = %d and id IN (%s) AND product_id=%d and to_invoice=%d" % (account.id, ','.join(map(str,data['ids'])), product_id, factor_id))
120 line_ids = cr.dictfetchall()
122 for line in line_ids:
123 # set invoice_line_note
125 if data['form']['date']:
126 details.append(line['date'])
127 if data['form']['time']:
128 details.append("%s %s" % (line['unit_amount'], pool.get('product.uom').browse(cr, uid, [line['product_uom_id']])[0].name))
129 if data['form']['name']:
130 details.append(line['name'])
131 #if data['form']['price']:
132 # details.append(abs(line['amount']))
133 note.append(' - '.join(map(str,details)))
135 curr_line['note'] = "\n".join(map(str,note))
136 pool.get('account.invoice.line').create(cr, uid, curr_line)
137 cr.execute("update account_analytic_line set invoice_id=%d WHERE account_id = %d and id IN (%s)" % (last_invoice,account.id, ','.join(map(str,data['ids']))))
140 'domain': "[('id','in', ["+','.join(map(str,invoices))+"])]",
143 'view_mode': 'tree,form',
144 'res_model': 'account.invoice',
146 'context': "{'type':'out_invoice'}",
147 'type': 'ir.actions.act_window'
151 _create_form = """<?xml version="1.0"?>
152 <form title="Invoice on analytic entries">
153 <separator string="Do you want details for each line of the invoices ?" colspan="4"/>
157 <field name="price"/>
158 <separator string="Choose accounts you want to invoice" colspan="4"/>
159 <field name="accounts" colspan="4"/>
160 <separator string="Choose a product for intermediary invoice" colspan="4"/>
161 <field name="product"/>
165 'accounts': {'string':'Analytic Accounts', 'type':'many2many', 'required':'true', 'relation':'account.analytic.account'},
166 'date': {'string':'Date', 'type':'boolean'},
167 'time': {'string':'Time spent', 'type':'boolean'},
168 'name': {'string':'Name of entry', 'type':'boolean'},
169 'price': {'string':'Cost', 'type':'boolean'},
170 'product': {'string':'Product', 'type':'many2one', 'relation': 'product.product'},
175 'actions' : [_get_accounts],
176 'result' : {'type':'form', 'arch':_create_form, 'fields':_create_fields, 'state': [('end','Cancel'),('create','Create invoices')]},
180 'result' : {'type':'action', 'action':_do_create, 'state':'end'},
183 invoice_create('hr.timesheet.invoice.create')