From 33bd32903cdb454d9c8993b2f5944ecccd1c05e0 Mon Sep 17 00:00:00 2001 From: "mra (Open ERP)" Date: Thu, 22 Apr 2010 16:04:40 +0530 Subject: [PATCH] [ADD] hr_timesheet_invoice: Create invoice for analytic lines=> osv memory bzr revid: mra@tinyerp.com-20100422103440-960ki0xp1lwygzb1 --- addons/hr_timesheet_invoice/__openerp__.py | 1 + .../hr_timesheet_invoice_wizard.xml | 4 +- .../wizard/hr_timesheet_invoice_create.py | 134 ++++++++---------- .../hr_timesheet_invoice_create_view.xml | 57 ++++++++ 4 files changed, 117 insertions(+), 79 deletions(-) create mode 100644 addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create_view.xml diff --git a/addons/hr_timesheet_invoice/__openerp__.py b/addons/hr_timesheet_invoice/__openerp__.py index c41ede0ffe8..90933e2ecda 100644 --- a/addons/hr_timesheet_invoice/__openerp__.py +++ b/addons/hr_timesheet_invoice/__openerp__.py @@ -41,6 +41,7 @@ reports, eso.""", 'report/hr_timesheet_invoice_report_view.xml', 'wizard/hr_timesheet_invoice_analytic_cost_ledger_view.xml', 'wizard/hr_timesheet_analytic_profit_view.xml', + 'wizard/hr_timesheet_invoice_create_view.xml', ], 'demo_xml': ['hr_timesheet_invoice_demo.xml'], 'installable': True, diff --git a/addons/hr_timesheet_invoice/hr_timesheet_invoice_wizard.xml b/addons/hr_timesheet_invoice/hr_timesheet_invoice_wizard.xml index 350f30c780d..b18b8c0893e 100644 --- a/addons/hr_timesheet_invoice/hr_timesheet_invoice_wizard.xml +++ b/addons/hr_timesheet_invoice/hr_timesheet_invoice_wizard.xml @@ -1,8 +1,8 @@ - + - + diff --git a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py index 7e5edca5474..89ff5e92697 100644 --- a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py +++ b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py @@ -19,52 +19,69 @@ # ############################################################################## -import wizard -import pooler import time + +from osv import osv, fields from tools.translate import _ -# -# Create an invoice based on selected timesheet lines +## Create an invoice based on selected timesheet lines # # # TODO: check unit of measure !!! # -class invoice_create(wizard.interface): - def _get_accounts(self, cr, uid, data, context): - if not len(data['ids']): +class hr_timesheet_invoice_create(osv.osv_memory): + + _name = 'hr.timesheet.invoice.create' + _description = 'Create invoice from timesheet' + _columns = { + 'accounts': fields.many2many('account.analytic.account', 'invoice_id', 'account_id', 'Analytic Accounts', required=True), + 'date': fields.boolean('Date', help='The real date of each work will be displayed on the invoice'), + 'time': fields.boolean('Time spent', help='The time of each work done will be displayed on the invoice'), + 'name': fields.boolean('Name of entry', help='The detail of each work done will be displayed on the invoice'), + 'price': fields.boolean('Cost', help='The cost of each work done will be displayed on the invoice. You probably don\'t want to check this'), + 'product': fields.many2one('product.product', 'Product', help='Complete this field only if you want to force to use a specific product. Keep empty to use the real product that comes from the cost.'), + } + + def _get_accounts(self, cr, uid, context=None): + if context is None: + context = {} + if not len(context['active_ids']): return {} #Checking whether the analytic line is invoiced or not - pool = pooler.get_pool(cr.dbname) - analytic_line_obj = pool.get('account.analytic.line').browse(cr, uid, data['ids'], context) + analytic_line_obj = self.pool.get('account.analytic.line').browse(cr, uid, context['active_ids'], context) for obj_acc in analytic_line_obj: if obj_acc.invoice_id and obj_acc.invoice_id.state !='cancel': - raise wizard.except_wizard(_('Warning'),_('The analytic entry "%s" is already invoiced!')%(obj_acc.name,)) - - cr.execute("SELECT distinct(account_id) from account_analytic_line where id =ANY(%s)",(data['ids'],)) - account_ids = cr.fetchall() - return {'accounts': [x[0] for x in account_ids]} + raise osv.except_osv(_('Warning'),_('The analytic entry "%s" is already invoiced!')%(obj_acc.name,)) + + cr.execute("SELECT distinct(account_id) from account_analytic_line where id =ANY(%s)",(context['active_ids'],)) + account_ids = cr.fetchall() + return [x[0] for x in account_ids] + + _defaults = { + 'accounts': _get_accounts + } + + def do_create(self, cr, uid, ids, context=None): + mod_obj = self.pool.get('ir.model.data') + analytic_account_obj = self.pool.get('account.analytic.account') + res_partner_obj = self.pool.get('res.partner') + account_payment_term_obj = self.pool.get('account.payment.term') + invoices = [] - def _do_create(self, cr, uid, data, context): - pool = pooler.get_pool(cr.dbname) - mod_obj = pool.get('ir.model.data') result = mod_obj._get_id(cr, uid, 'account', 'view_account_invoice_filter') res = mod_obj.read(cr, uid, result, ['res_id']) - analytic_account_obj = pool.get('account.analytic.account') - res_partner_obj = pool.get('res.partner') - account_payment_term_obj = pool.get('account.payment.term') - account_ids = data['form']['accounts'][0][2] - invoices = [] + data = self.read(cr, uid, ids, [], context)[0] + account_ids = data['accounts'] for account in analytic_account_obj.browse(cr, uid, account_ids, context): partner = account.partner_id if (not partner) or not (account.pricelist_id): - raise wizard.except_wizard(_('Analytic Account incomplete'), + raise osv.except_osv(_('Analytic Account incomplete'), _('Please fill in the Associate Partner and Sale Pricelist fields in the Analytic Account:\n%s') % (account.name,)) if not partner.address: - raise wizard.except_wizard(_('Partner incomplete'), + raise osv.except_osv(_('Partner incomplete'), _('Please fill in the Address field in the Partner: %s.') % (partner.name,)) date_due = False @@ -90,7 +107,7 @@ class invoice_create(wizard.interface): 'date_due': date_due, 'fiscal_position': account.partner_id.property_account_position.id } - last_invoice = pool.get('account.invoice').create(cr, uid, curr_invoice) + last_invoice = self.pool.get('account.invoice').create(cr, uid, curr_invoice) invoices.append(last_invoice) context2=context.copy() @@ -99,26 +116,26 @@ class invoice_create(wizard.interface): "FROM account_analytic_line as line " \ "WHERE account_id = %s " \ "AND id =ANY(%s) AND to_invoice IS NOT NULL " \ - "GROUP BY product_id,to_invoice", (account.id,data['ids'],)) + "GROUP BY product_id,to_invoice", (account.id,context['active_ids'],)) for product_id,factor_id,qty in cr.fetchall(): product = pool.get('product.product').browse(cr, uid, product_id, context2) if not product: - raise wizard.except_wizard(_('Error'), _('At least one line has no product !')) + raise osv.except_osv(_('Error'), _('At least one line has no product !')) factor_name = '' factor = pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context2) - if not data['form']['product']: + if not data['product']: if factor.customer_name: factor_name = product.name+' - '+factor.customer_name else: factor_name = product.name else: - factor_name = pool.get('product.product').name_get(cr, uid, [data['form']['product']], context=context)[0][1] + factor_name = pool.get('product.product').name_get(cr, uid, [data['product']], context=context)[0][1] if account.pricelist_id: pl = account.pricelist_id.id - price = pool.get('product.pricelist').price_get(cr,uid,[pl], data['form']['product'] or product_id, qty or 1.0, account.partner_id.id)[pl] + price = pool.get('product.pricelist').price_get(cr,uid,[pl], data['product'] or product_id, qty or 1.0, account.partner_id.id)[pl] else: price = 0.0 @@ -132,7 +149,7 @@ class invoice_create(wizard.interface): 'invoice_line_tax_id': [(6,0,tax )], 'invoice_id': last_invoice, 'name': factor_name, - 'product_id': data['form']['product'] or product_id, + 'product_id': data['product'] or product_id, 'invoice_line_tax_id': [(6,0,tax)], 'uos_id': product.uom_id.id, 'account_id': account_id, @@ -149,27 +166,27 @@ class invoice_create(wizard.interface): for line in line_ids: # set invoice_line_note details = [] - if data['form']['date']: + if data['date']: details.append(line['date']) - if data['form']['time']: + if data['time']: if line['product_uom_id']: details.append("%s %s" % (line['unit_amount'], pool.get('product.uom').browse(cr, uid, [line['product_uom_id']])[0].name)) else: details.append("%s" % (line['unit_amount'], )) - if data['form']['name']: + if data['name']: details.append(line['name']) - #if data['form']['price']: + #if data['price']: # details.append(abs(line['amount'])) note.append(u' - '.join(map(lambda x: unicode(x) or '',details))) curr_line['note'] = "\n".join(map(lambda x: unicode(x) or '',note)) pool.get('account.invoice.line').create(cr, uid, curr_line) - cr.execute("update account_analytic_line set invoice_id=%s WHERE account_id = %s and id =ANY(%s)" ,(last_invoice,account.id,data['ids'])) + cr.execute("update account_analytic_line set invoice_id=%s WHERE account_id = %s and id =ANY(%s)" ,(last_invoice, account.id,data['ids'])) - pool.get('account.invoice').button_reset_taxes(cr, uid, [last_invoice], context) + self.pool.get('account.invoice').button_reset_taxes(cr, uid, [last_invoice], context) - mod_obj = pooler.get_pool(cr.dbname).get('ir.model.data') - act_obj = pooler.get_pool(cr.dbname).get('ir.actions.act_window') + mod_obj = self.pool.get('ir.model.data') + act_obj = self.pool.get('ir.actions.act_window') mod_id = mod_obj.search(cr, uid, [('name', '=', 'action_invoice_tree1')])[0] res_id = mod_obj.read(cr, uid, mod_id, ['res_id'])['res_id'] @@ -190,45 +207,8 @@ class invoice_create(wizard.interface): # 'search_view_id': res['res_id'] # } - _create_form = """ -
- - - - - - - - - - - - - - - -
""" - _create_fields = { - 'accounts': {'string':'Analytic Accounts', 'type':'many2many', 'required':'true', 'relation':'account.analytic.account'}, - 'date': {'string':'Date', 'type':'boolean', 'help':'The real date of each work will be displayed on the invoice'}, - 'time': {'string':'Time spent', 'type':'boolean', 'help':'The time of each work done will be displayed on the invoice'}, - 'name': {'string':'Name of entry', 'type':'boolean', 'help':'The detail of each work done will be displayed on the invoice'}, - 'price': {'string':'Cost', 'type':'boolean', 'help':'The cost of each work done will be displayed on the invoice. You probably don\'t want to check this.'}, - 'product': {'string':'Product', 'type':'many2one', 'relation': 'product.product', 'help':"Complete this field only if you want to force to use a specific product. Keep empty to use the real product that comes from the cost."}, - } - - states = { - 'init' : { - 'actions' : [_get_accounts], - 'result' : {'type':'form', 'arch':_create_form, 'fields':_create_fields, 'state': [('end','Cancel', 'gtk-cancel'),('create','Create Invoices', 'gtk-ok')]}, - }, - 'create' : { - 'actions' : [], - 'result' : {'type':'action', 'action':_do_create, 'state':'end'}, - }, - } -invoice_create('hr.timesheet.invoice.create') +hr_timesheet_invoice_create() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create_view.xml b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create_view.xml new file mode 100644 index 00000000000..7ddf3bc99a7 --- /dev/null +++ b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create_view.xml @@ -0,0 +1,57 @@ + + + + + + hr.timesheet.invoice.create.form + hr.timesheet.invoice.create + form + +
+ + + + + + + + + + + + + + + + + + + +