ACCOUNT: add ref to analytic line, HR_TIMESHEET_INVOICE: add final invoice
bzr revid: ced-9422c619a669a2e335ee6cee8cd7304b49cbd65c
This commit is contained in:
parent
61a6a96814
commit
13bc7d36b1
|
@ -48,6 +48,7 @@ class account_analytic_line(osv.osv):
|
|||
'journal_id' : fields.many2one('account.analytic.journal', 'Analytic journal', required=True, ondelete='cascade', select=True),
|
||||
'code' : fields.char('Code', size=8),
|
||||
'user_id' : fields.many2one('res.users', 'User',),
|
||||
'ref': fields.char('Ref.', size=32),
|
||||
}
|
||||
|
||||
_defaults = {
|
||||
|
|
|
@ -372,8 +372,7 @@ class account_invoice(osv.osv):
|
|||
number = self.pool.get('ir.sequence').get(cr, uid, 'account.invoice.'+invtype)
|
||||
cr.execute('UPDATE account_invoice SET number=%s WHERE id=%d', (number, id))
|
||||
cr.execute('UPDATE account_move_line SET ref=%s WHERE move_id=%d and ref is null', (number, move_id))
|
||||
cr.execute('UPDATE account_analytic_line SET name=%s||account_analytic_line.name FROM account_move_line WHERE account_move_line.move_id=%d AND account_analytic_line.move_id=account_move_line.id', (number+' - ', move_id))
|
||||
print "move_id:", move_id
|
||||
cr.execute('UPDATE account_analytic_line SET ref=%s FROM account_move_line WHERE account_move_line.move_id=%d AND account_analytic_line.move_id=account_move_line.id', (number, move_id))
|
||||
return True
|
||||
|
||||
def action_cancel(self, cr, uid, ids, *args):
|
||||
|
|
|
@ -107,6 +107,7 @@
|
|||
<field name="general_account_id" select="1"/>
|
||||
<field name="move_id" select="1"/>
|
||||
<field name="unit_amount" select="1"/>
|
||||
<field name="ref" select="1"/>
|
||||
<newline/>
|
||||
<field name="product_id" select="1"/>
|
||||
<field name="product_uom_id" select="1"/>
|
||||
|
@ -126,6 +127,7 @@
|
|||
<field name="account_id" domain="[('type','=','normal')]"/>
|
||||
<field name="general_account_id"/>
|
||||
<field name="journal_id"/>
|
||||
<field name="ref"/>
|
||||
</tree>
|
||||
</field>
|
||||
</record>
|
||||
|
|
|
@ -6,6 +6,11 @@
|
|||
id="hr_timesheet_invoice_create"
|
||||
model="account.analytic.line"
|
||||
name="hr.timesheet.invoice.create"/>
|
||||
<wizard
|
||||
string="Final invoices"
|
||||
id="hr_timesheet_final_invoice_create"
|
||||
model="account.analytic.account"
|
||||
name="hr.timesheet.final.invoice.create"/>
|
||||
|
||||
</data>
|
||||
</terp>
|
||||
|
|
|
@ -29,3 +29,4 @@
|
|||
|
||||
import hr_timesheet_invoice_create
|
||||
import account_analytic_profit
|
||||
import hr_timesheet_final_invoice_create
|
||||
|
|
|
@ -0,0 +1,248 @@
|
|||
##############################################################################
|
||||
#
|
||||
# Copyright (c) 2004-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
|
||||
#
|
||||
# $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
|
||||
#
|
||||
# WARNING: This program as such is intended to be used by professional
|
||||
# programmers who take the whole responsability of assessing all potential
|
||||
# consequences resulting from its eventual inadequacies and bugs
|
||||
# End users who are looking for a ready-to-use solution with commercial
|
||||
# garantees and support are strongly adviced to contract a Free Software
|
||||
# Service Company
|
||||
#
|
||||
# This program is Free Software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
import wizard
|
||||
import pooler
|
||||
import time
|
||||
|
||||
#
|
||||
# Create an final invoice based on selected timesheet lines
|
||||
#
|
||||
|
||||
#
|
||||
# TODO: check unit of measure !!!
|
||||
#
|
||||
class final_invoice_create(wizard.interface):
|
||||
def _get_defaults(self, cr, uid, data, context):
|
||||
if not data['ids']:
|
||||
return {}
|
||||
account = pooler.get_pool(cr.dbname).get('account.analytic.account').browse(cr, uid, data['ids'], context)[0]
|
||||
return {'use_amount_max': bool(account.amount_max)}
|
||||
|
||||
def _do_create(self, cr, uid, data, context):
|
||||
pool = pooler.get_pool(cr.dbname)
|
||||
account_ids = data['ids']
|
||||
invoices = []
|
||||
for account in pool.get('account.analytic.account').browse(cr, uid, account_ids, context):
|
||||
partner = account.partner_id
|
||||
amount_total=0.0
|
||||
if (not partner) or not (account.pricelist_id):
|
||||
raise wizard.except_wizard('Analytic account incomplete', 'Please fill in the partner and pricelist field in the analytic account:\n%s' % (account.name,))
|
||||
|
||||
curr_invoice = {
|
||||
'name': time.strftime('%D')+' - '+account.name,
|
||||
'partner_id': account.partner_id.id,
|
||||
'address_contact_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['contact'])['contact'],
|
||||
'address_invoice_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['invoice'])['invoice'],
|
||||
'payment_term': (partner.property_payment_term and partner.property_payment_term[0]) or False,
|
||||
'account_id': partner.property_account_receivable[0],
|
||||
'currency_id': account.pricelist_id.currency_id.id,
|
||||
'project_id': account.id
|
||||
}
|
||||
last_invoice = pool.get('account.invoice').create(cr, uid, curr_invoice)
|
||||
invoices.append(last_invoice)
|
||||
|
||||
context2=context.copy()
|
||||
context2['lang'] = partner.lang
|
||||
cr.execute("SELECT product_id,to_invoice,sum(unit_amount) FROM account_analytic_line as line WHERE account_id = %d AND to_invoice IS NOT NULL GROUP BY product_id,to_invoice" % (account.id,))
|
||||
for product_id,factor_id,qty in cr.fetchall():
|
||||
product = pool.get('product.product').browse(cr, uid, product_id, context2)
|
||||
factor_name = ''
|
||||
factor = pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context2)
|
||||
if factor.customer_name:
|
||||
factor_name = product.name+' - '+factor.customer_name
|
||||
else:
|
||||
factor_name = product.name
|
||||
if account.pricelist_id:
|
||||
pl = account.pricelist_id.id
|
||||
price = pool.get('product.pricelist').price_get(cr,uid,[pl], product_id, qty or 1.0, account.partner_id.id)[pl]
|
||||
else:
|
||||
price = 0.0
|
||||
|
||||
taxes = product.taxes_id
|
||||
taxep = account.partner_id.property_account_tax
|
||||
if not taxep:
|
||||
tax = [x.id for x in taxes or []]
|
||||
else:
|
||||
tax = [taxep[0]]
|
||||
tp = pool.get('account.tax').browse(cr, uid, taxep[0])
|
||||
for t in taxes:
|
||||
if not t.tax_group==tp.tax_group:
|
||||
tax.append(t.id)
|
||||
|
||||
account_id = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ
|
||||
|
||||
curr_line = {
|
||||
'price_unit': price,
|
||||
'quantity': qty,
|
||||
'discount':factor.factor,
|
||||
'invoice_line_tax_id': [(6,0,tax )],
|
||||
'invoice_id': last_invoice,
|
||||
'name': factor_name,
|
||||
'product_id': product_id,
|
||||
'uos_id': product.uom_id.id,
|
||||
'account_id': account_id[0],
|
||||
}
|
||||
|
||||
amount_total += round((price * ( 1.0 - (factor.factor or 0.0)/100.0)), 2) * qty
|
||||
|
||||
#
|
||||
# Compute for lines
|
||||
#
|
||||
cr.execute("SELECT * FROM account_analytic_line WHERE account_id = %d AND product_id=%d and to_invoice=%d" % (account.id, product_id, factor_id))
|
||||
line_ids = cr.dictfetchall()
|
||||
note = []
|
||||
for line in line_ids:
|
||||
# set invoice_line_note
|
||||
details = []
|
||||
if data['form']['date']:
|
||||
details.append(line['date'])
|
||||
if data['form']['time']:
|
||||
details.append("%s %s" % (line['unit_amount'], pool.get('product.uom').browse(cr, uid, [line['product_uom_id']])[0].name))
|
||||
if data['form']['name']:
|
||||
details.append(line['name'])
|
||||
#if data['form']['price']:
|
||||
# details.append(abs(line['amount']))
|
||||
note.append(' - '.join(map(str,details)))
|
||||
|
||||
curr_line['note'] = "\n".join(map(str,note))
|
||||
pool.get('account.invoice.line').create(cr, uid, curr_line)
|
||||
cr.execute("update account_analytic_line set invoice_id=%d WHERE account_id = %d and invoice_id is null" % (last_invoice,account.id,))
|
||||
|
||||
cr.execute("SELECT line.product_id, sum(line.amount), line.account_id, line.product_uom_id, move_line.ref FROM account_analytic_line as line, account_move_line as move_line WHERE line.account_id = %d AND line.move_id IS NOT NULL AND move_line.id = line.move_id GROUP BY line.product_id, line.account_id, line.product_uom_id, move_line.ref" % (account.id))
|
||||
for product_id, amount, account_id, product_uom_id, ref in cr.fetchall():
|
||||
product = pool.get('product.product').browse(cr, uid, product_id, context2)
|
||||
|
||||
if product:
|
||||
taxes = product.taxes_id
|
||||
else:
|
||||
taxes = []
|
||||
taxep = account.partner_id.property_account_tax
|
||||
if not taxep:
|
||||
tax = [x.id for x in taxes or []]
|
||||
else:
|
||||
tax = [taxep[0]]
|
||||
tp = pool.get('account.tax').browse(cr, uid, taxep[0])
|
||||
for t in taxes:
|
||||
if not t.tax_group==tp.tax_group:
|
||||
tax.append(t.id)
|
||||
|
||||
curr_line = {
|
||||
'price_unit': -amount,
|
||||
'quantity': 1.0,
|
||||
'discount': 0.0,
|
||||
'invoice_line_tax_id': [(6,0,tax)],
|
||||
'invoice_id': last_invoice,
|
||||
'name': ref+(product and ' - '+product.name or ''),
|
||||
'product_id': product_id,
|
||||
'uos_id': product_uom_id,
|
||||
'account_id': account_id,
|
||||
}
|
||||
pool.get('account.invoice.line').create(cr, uid, curr_line)
|
||||
|
||||
if data['form']['use_amount_max']:
|
||||
if abs(account.amount_max - amount_total) > data['form']['balance_amount'] :
|
||||
if not data['form']['balance_product']:
|
||||
raise wizard.except_wizard('Balance product needed', 'Please fill a Balance product in the wizard')
|
||||
product = pool.get('product.product').browse(cr, uid, data['form']['balance_product'], context2)
|
||||
|
||||
taxes = product.taxes_id
|
||||
taxep = account.partner_id.property_account_tax
|
||||
if not taxep:
|
||||
tax = [x.id for x in taxes or []]
|
||||
else:
|
||||
tax = [taxep[0]]
|
||||
tp = pool.get('account.tax').browse(cr, uid, taxep[0])
|
||||
for t in taxes:
|
||||
if not t.tax_group==tp.tax_group:
|
||||
tax.append(t.id)
|
||||
|
||||
account_id = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ
|
||||
|
||||
curr_line = {
|
||||
'price_unit': account.amount_max - amount_total,
|
||||
'quantity': 1.0,
|
||||
'discount': 0.0,
|
||||
'invoice_line_tax_id': [(6,0,tax)],
|
||||
'invoice_id': last_invoice,
|
||||
'name': product.name,
|
||||
'product_id': product_id,
|
||||
'uos_id': product.uom_id.id,
|
||||
'account_id': account_id[0],
|
||||
}
|
||||
pool.get('account.invoice.line').create(cr, uid, curr_line)
|
||||
if account.amount_max < amount_total:
|
||||
pool.get('account.invoice').write(cr, uid, [last_invoice], {'type': 'out_refund',})
|
||||
|
||||
return {
|
||||
'domain': "[('id','in', ["+','.join(map(str,invoices))+"])]",
|
||||
'name': 'Invoices',
|
||||
'view_type': 'form',
|
||||
'view_mode': 'tree,form',
|
||||
'res_model': 'account.invoice',
|
||||
'view_id': False,
|
||||
'context': "{'type':'out_invoice'}",
|
||||
'type': 'ir.actions.act_window'
|
||||
}
|
||||
|
||||
|
||||
_create_form = """<?xml version="1.0"?>
|
||||
<form title="Final invoice for analytic account">
|
||||
<separator string="Do you want details for each line of the invoices ?" colspan="4"/>
|
||||
<field name="date"/>
|
||||
<field name="time"/>
|
||||
<field name="name"/>
|
||||
<field name="price"/>
|
||||
<separator string="Invoice Balance amount" colspan="4"/>
|
||||
<field name="use_amount_max"/>
|
||||
<field name="balance_amount"/>
|
||||
<field name="balance_product"/>
|
||||
</form>"""
|
||||
|
||||
_create_fields = {
|
||||
'date': {'string':'Date', 'type':'boolean'},
|
||||
'time': {'string':'Time spent', 'type':'boolean'},
|
||||
'name': {'string':'Name of entry', 'type':'boolean'},
|
||||
'price': {'string':'Cost', 'type':'boolean'},
|
||||
'use_amount_max': {'string':'Use Max. Invoice Price', 'type':'boolean'},
|
||||
'balance_amount': {'string':'Balance amount', 'type': 'float'},
|
||||
'balance_product': {'string':'Balance product', 'type': 'many2one', 'relation':'product.product'},
|
||||
}
|
||||
|
||||
states = {
|
||||
'init' : {
|
||||
'actions' : [_get_defaults],
|
||||
'result' : {'type':'form', 'arch':_create_form, 'fields':_create_fields, 'state': [('end','Cancel'),('create','Create invoices')]},
|
||||
},
|
||||
'create' : {
|
||||
'actions' : [],
|
||||
'result' : {'type':'action', 'action':_do_create, 'state':'end'},
|
||||
},
|
||||
}
|
||||
final_invoice_create('hr.timesheet.final.invoice.create')
|
|
@ -1,6 +1,6 @@
|
|||
##############################################################################
|
||||
#
|
||||
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
|
||||
# Copyright (c) 2004-2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
|
||||
#
|
||||
# $Id: account.py 1005 2005-07-25 08:41:42Z nicoe $
|
||||
#
|
||||
|
@ -46,15 +46,6 @@ class invoice_create(wizard.interface):
|
|||
account_ids = cr.fetchall()
|
||||
return {'accounts': [x[0] for x in account_ids]}
|
||||
|
||||
#
|
||||
# Sould use method from the partner object
|
||||
#
|
||||
def _get_partner_address_id(self, cr, uid, partner_id, type='default'):
|
||||
id = pooler.get_pool(cr.dbname).get('res.partner.address').search(cr, uid, [('partner_id','=',partner_id),('type','=',type)])
|
||||
if not id:
|
||||
return pooler.get_pool(cr.dbname).get('res.partner.address').search(cr, uid, [('partner_id','=',partner_id),('type','=','default')])[0]
|
||||
return id[0]
|
||||
|
||||
def _do_create(self, cr, uid, data, context):
|
||||
pool = pooler.get_pool(cr.dbname)
|
||||
account_ids = data['form']['accounts'][0][2]
|
||||
|
@ -67,8 +58,8 @@ class invoice_create(wizard.interface):
|
|||
curr_invoice = {
|
||||
'name': time.strftime('%D')+' - '+account.name,
|
||||
'partner_id': account.partner_id.id,
|
||||
'address_contact_id': self._get_partner_address_id(cr, uid, account.partner_id.id, 'contact'),
|
||||
'address_invoice_id': self._get_partner_address_id(cr, uid, account.partner_id.id, 'invoice'),
|
||||
'address_contact_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['contact'])['contact'],
|
||||
'address_invoice_id': pool.get('res.partner').address_get(cr, uid, [account.partner_id.id], adr_pref=['invoice'])['invoice'],
|
||||
'payment_term': (partner.property_payment_term and partner.property_payment_term[0]) or False,
|
||||
'account_id': partner.property_account_receivable[0],
|
||||
'currency_id': account.pricelist_id.currency_id.id,
|
||||
|
@ -105,6 +96,8 @@ class invoice_create(wizard.interface):
|
|||
if not t.tax_group==tp.tax_group:
|
||||
tax.append(t.id)
|
||||
|
||||
account_id = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ
|
||||
|
||||
curr_line = {
|
||||
'price_unit': price,
|
||||
'quantity': qty,
|
||||
|
@ -112,8 +105,10 @@ class invoice_create(wizard.interface):
|
|||
'invoice_line_tax_id': [(6,0,tax )],
|
||||
'invoice_id': last_invoice,
|
||||
'name': factor_name,
|
||||
'product_id': product_id,
|
||||
'invoice_line_tax_id': [(6,0,tax)]
|
||||
'product_id': data['form']['product'] or product_id,
|
||||
'invoice_line_tax_id': [(6,0,tax)],
|
||||
'uos_id': product.uom_id.id,
|
||||
'account_id': account_id[0],
|
||||
}
|
||||
|
||||
#
|
||||
|
@ -123,9 +118,6 @@ class invoice_create(wizard.interface):
|
|||
line_ids = cr.dictfetchall()
|
||||
note = []
|
||||
for line in line_ids:
|
||||
curr_line['uos_id'] = line['product_uom_id']
|
||||
curr_line['account_id'] = line['general_account_id']
|
||||
|
||||
# set invoice_line_note
|
||||
details = []
|
||||
if data['form']['date']:
|
||||
|
@ -163,6 +155,8 @@ class invoice_create(wizard.interface):
|
|||
<field name="price"/>
|
||||
<separator string="Choose accounts you want to invoice" colspan="4"/>
|
||||
<field name="accounts" colspan="4"/>
|
||||
<separator string="Choose a product for intermediary invoice" colspan="4"/>
|
||||
<field name="product"/>
|
||||
</form>"""
|
||||
|
||||
_create_fields = {
|
||||
|
@ -171,6 +165,7 @@ class invoice_create(wizard.interface):
|
|||
'time': {'string':'Time spent', 'type':'boolean'},
|
||||
'name': {'string':'Name of entry', 'type':'boolean'},
|
||||
'price': {'string':'Cost', 'type':'boolean'},
|
||||
'product': {'string':'Product', 'type':'many2one', 'relation': 'product.product'},
|
||||
}
|
||||
|
||||
states = {
|
||||
|
|
Loading…
Reference in New Issue