From 6f256d41dbffd50e0f99f31c61f0bbf51144f33e Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Tue, 27 Nov 2012 13:07:37 +0100 Subject: [PATCH] [FIX] account_analytic_contract, analytic_contract_hr_expense, hr_timesheet_invoice: fixed the computation of *to_invoice fields bzr revid: qdp-launchpad@openerp.com-20121127120737-5xeed6lobynqf10l --- .../account_analytic_analysis.py | 20 ++- .../analytic_contract_hr_expense.py | 51 +++--- .../hr_timesheet_invoice.py | 140 +++++++++++++++- .../wizard/hr_timesheet_invoice_create.py | 158 +----------------- 4 files changed, 174 insertions(+), 195 deletions(-) diff --git a/addons/account_analytic_analysis/account_analytic_analysis.py b/addons/account_analytic_analysis/account_analytic_analysis.py index 06e2fd2bb18..466db63e61e 100644 --- a/addons/account_analytic_analysis/account_analytic_analysis.py +++ b/addons/account_analytic_analysis/account_analytic_analysis.py @@ -75,15 +75,21 @@ class account_analytic_account(osv.osv): res[id][f] = 0.0 res2 = {} for account in accounts: - cr.execute("SELECT product_id, user_id, to_invoice, sum(unit_amount), product_uom_id, name " \ - "FROM account_analytic_line as line " \ - "WHERE account_id = %s " \ - "AND invoice_id is NULL AND to_invoice IS NOT NULL " \ - "GROUP BY product_id, user_id, to_invoice, product_uom_id, name", (account.id,)) + cr.execute(""" + SELECT product_id, sum(amount), user_id, to_invoice, sum(unit_amount), product_uom_id, line.name + FROM account_analytic_line line + LEFT JOIN account_analytic_journal journal ON (journal.id = line.journal_id) + WHERE account_id = %s + AND journal.type != 'purchase' + AND invoice_id IS NULL + AND to_invoice IS NOT NULL + GROUP BY product_id, user_id, to_invoice, product_uom_id, line.name""", (account.id,)) res[account.id][f] = 0.0 - for product_id, user_id, factor_id, qty, uom, line_name in cr.fetchall(): - price = self.pool.get('account.analytic.line')._get_invoice_price(cr, uid, account, product_id, user_id, qty, context) + for product_id, price, user_id, factor_id, qty, uom, line_name in cr.fetchall(): + price = -price + if product_id: + price = self.pool.get('account.analytic.line')._get_invoice_price(cr, uid, account, product_id, user_id, qty, context) factor = self.pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context=context) res[account.id][f] += price * qty * (100-factor.factor or 0.0) / 100.0 diff --git a/addons/analytic_contract_hr_expense/analytic_contract_hr_expense.py b/addons/analytic_contract_hr_expense/analytic_contract_hr_expense.py index 35177ee3a66..2b5584d4660 100644 --- a/addons/analytic_contract_hr_expense/analytic_contract_hr_expense.py +++ b/addons/analytic_contract_hr_expense/analytic_contract_hr_expense.py @@ -65,39 +65,26 @@ class account_analytic_account(osv.osv): def _expense_to_invoice_calc(self, cr, uid, ids, name, arg, context=None): res = {} - res_final = {} - child_ids = tuple(ids) #We don't want consolidation for each of these fields because those complex computation is resource-greedy. - for i in child_ids: - res[i] = 0.0 - if not child_ids: - return res + #We don't want consolidation for each of these fields because those complex computation is resource-greedy. + for account in self.pool.get('account.analytic.account').browse(cr, uid, ids, context=context): + cr.execute(""" + SELECT product_id, sum(amount), user_id, to_invoice, sum(unit_amount), product_uom_id, line.name + FROM account_analytic_line line + LEFT JOIN account_analytic_journal journal ON (journal.id = line.journal_id) + WHERE account_id = %s + AND journal.type = 'purchase' + AND invoice_id IS NULL + AND to_invoice IS NOT NULL + GROUP BY product_id, user_id, to_invoice, product_uom_id, line.name""", (account.id,)) - if child_ids: - cr.execute("""SELECT account_analytic_account.id, \ - COALESCE(SUM (product_template.list_price * \ - account_analytic_line.unit_amount * \ - ((100-hr_timesheet_invoice_factor.factor)/100)), 0.0) \ - AS ca_to_invoice \ - FROM product_template \ - JOIN product_product \ - ON product_template.id = product_product.product_tmpl_id \ - JOIN account_analytic_line \ - ON account_analytic_line.product_id = product_product.id \ - JOIN account_analytic_journal \ - ON account_analytic_line.journal_id = account_analytic_journal.id \ - JOIN account_analytic_account \ - ON account_analytic_account.id = account_analytic_line.account_id \ - JOIN hr_timesheet_invoice_factor \ - ON hr_timesheet_invoice_factor.id = account_analytic_account.to_invoice \ - WHERE account_analytic_account.id IN %s \ - AND account_analytic_line.invoice_id IS NULL \ - AND account_analytic_line.to_invoice IS NOT NULL \ - AND account_analytic_journal.type = 'purchase' \ - GROUP BY account_analytic_account.id;""",(child_ids,)) - for account_id, sum in cr.fetchall(): - res[account_id] = sum - res_final = res - return res_final + res[account.id] = 0.0 + for product_id, price, user_id, factor_id, qty, uom, line_name in cr.fetchall(): + price = -price + if product_id: + price = self.pool.get('account.analytic.line')._get_invoice_price(cr, uid, account, product_id, user_id, qty, context) + factor = self.pool.get('hr_timesheet_invoice.factor').browse(cr, uid, factor_id, context=context) + res[account.id] += price * qty * (100 - factor.factor or 0.0) / 100.0 + return res def _expense_invoiced_calc(self, cr, uid, ids, name, arg, context=None): lines_obj = self.pool.get('account.analytic.line') diff --git a/addons/hr_timesheet_invoice/hr_timesheet_invoice.py b/addons/hr_timesheet_invoice/hr_timesheet_invoice.py index a24208d6ce0..be23faea804 100644 --- a/addons/hr_timesheet_invoice/hr_timesheet_invoice.py +++ b/addons/hr_timesheet_invoice/hr_timesheet_invoice.py @@ -19,8 +19,9 @@ # ############################################################################## -from osv import fields, osv +import time +from osv import fields, osv from tools.translate import _ class hr_timesheet_invoice_factor(osv.osv): @@ -172,6 +173,143 @@ class account_analytic_line(osv.osv): return super(account_analytic_line, self).copy(cursor, user, obj_id, default, context=context) + def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context = {}): + pro_price_obj = self.pool.get('product.pricelist') + if account.pricelist_id: + pl = account.pricelist_id.id + price = pro_price_obj.price_get(cr,uid,[pl], product_id, qty or 1.0, account.partner_id.id, context=context)[pl] + else: + price = 0.0 + return price + + def invoice_cost_create(self, cr, uid, ids, data=None, context=None): + analytic_account_obj = self.pool.get('account.analytic.account') + account_payment_term_obj = self.pool.get('account.payment.term') + invoice_obj = self.pool.get('account.invoice') + product_obj = self.pool.get('product.product') + invoice_factor_obj = self.pool.get('hr_timesheet_invoice.factor') + fiscal_pos_obj = self.pool.get('account.fiscal.position') + product_uom_obj = self.pool.get('product.uom') + invoice_line_obj = self.pool.get('account.invoice.line') + invoices = [] + if context is None: + context = {} + if data is None: + data = {} + + journal_types = {} + for line in self.pool.get('account.analytic.line').browse(cr, uid, ids, context=context): + if line.journal_id.type not in journal_types: + journal_types[line.journal_id.type] = set() + journal_types[line.journal_id.type].add(line.account_id.id) + for journal_type, account_ids in journal_types.items(): + for account in analytic_account_obj.browse(cr, uid, list(account_ids), context=context): + partner = account.partner_id + if (not partner) or not (account.pricelist_id): + raise osv.except_osv(_('Analytic Account incomplete !'), + _('Please fill in the Partner or Customer and Sale Pricelist fields in the Analytic Account:\n%s.') % (account.name,)) + + date_due = False + if partner.property_payment_term: + pterm_list= account_payment_term_obj.compute(cr, uid, + partner.property_payment_term.id, value=1, + date_ref=time.strftime('%Y-%m-%d')) + if pterm_list: + pterm_list = [line[0] for line in pterm_list] + pterm_list.sort() + date_due = pterm_list[-1] + + curr_invoice = { + 'name': time.strftime('%d/%m/%Y') + ' - '+account.name, + 'partner_id': account.partner_id.id, + 'company_id': account.company_id.id, + 'payment_term': partner.property_payment_term.id or False, + 'account_id': partner.property_account_receivable.id, + 'currency_id': account.pricelist_id.currency_id.id, + 'date_due': date_due, + 'fiscal_position': account.partner_id.property_account_position.id + } + + context2 = context.copy() + context2['lang'] = partner.lang + # set company_id in context, so the correct default journal will be selected + context2['force_company'] = curr_invoice['company_id'] + # set force_company in context so the correct product properties are selected (eg. income account) + context2['company_id'] = curr_invoice['company_id'] + + last_invoice = invoice_obj.create(cr, uid, curr_invoice, context=context2) + invoices.append(last_invoice) + + cr.execute("""SELECT product_id, user_id, to_invoice, sum(unit_amount), product_uom_id + FROM account_analytic_line as line LEFT JOIN account_analytic_journal journal ON (line.journal_id = journal.id) + WHERE account_id = %s + AND id IN %s AND journal.type IN %s AND to_invoice IS NOT NULL + GROUP BY product_id, user_id, to_invoice, product_uom_id""", (account.id, tuple(ids), journal_type)) + + for product_id, user_id, factor_id, qty, uom in cr.fetchall(): + if data.get('product'): + product_id = data['product'][0] + product = product_obj.browse(cr, uid, product_id, context=context2) + if not product: + raise osv.except_osv(_('Error!'), _('There is no product defined. Please select one or force the product through the wizard.')) + factor = invoice_factor_obj.browse(cr, uid, factor_id, context=context2) + factor_name = product_obj.name_get(cr, uid, [product_id], context=context2)[0][1] + if factor.customer_name: + factor_name += ' - ' + factor.customer_name + + ctx = context.copy() + ctx.update({'uom':uom}) + + price = self._get_invoice_price(cr, uid, account, product_id, user_id, qty, ctx) + + general_account = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ + if not general_account: + raise osv.except_osv(_("Configuration Error!"), _("Please define income account for product '%s'.") % product.name) + taxes = product.taxes_id or general_account.tax_ids + tax = fiscal_pos_obj.map_tax(cr, uid, account.partner_id.property_account_position, taxes) + 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, + 'invoice_line_tax_id': [(6,0,tax)], + 'uos_id': uom, + 'account_id': general_account.id, + 'account_analytic_id': account.id, + } + + # + # Compute for lines + # + cr.execute("SELECT * FROM account_analytic_line WHERE account_id = %s and id IN %s AND product_id=%s and to_invoice=%s ORDER BY account_analytic_line.date", (account.id, tuple(ids), product_id, factor_id)) + + line_ids = cr.dictfetchall() + note = [] + for line in line_ids: + # set invoice_line_note + details = [] + if data.get('date', False): + details.append(line['date']) + if data.get('time', False): + if line['product_uom_id']: + details.append("%s %s" % (line['unit_amount'], product_uom_obj.browse(cr, uid, [line['product_uom_id']],context2)[0].name)) + else: + details.append("%s" % (line['unit_amount'], )) + if data.get('name', False): + details.append(line['name']) + note.append(u' - '.join(map(lambda x: unicode(x) or '',details))) + + if note: + curr_line['name'] += "\n" + ("\n".join(map(lambda x: unicode(x) or '',note))) + invoice_line_obj.create(cr, uid, curr_line, context=context) + cr.execute("update account_analytic_line set invoice_id=%s WHERE account_id = %s and id IN %s", (last_invoice, account.id, tuple(ids))) + + invoice_obj.button_reset_taxes(cr, uid, [last_invoice], context) + return invoices + account_analytic_line() 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 3fd7cca9b3b..0a8fd36f469 100644 --- a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py +++ b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py @@ -19,163 +19,10 @@ # ############################################################################## -import time from osv import osv, fields from tools.translate import _ -## Create an invoice based on selected timesheet lines -# - -class account_analytic_line(osv.osv): - _inherit = "account.analytic.line" - def _get_invoice_price(self, cr, uid, account, product_id, user_id, qty, context = {}): - pro_price_obj = self.pool.get('product.pricelist') - if account.pricelist_id: - pl = account.pricelist_id.id - price = pro_price_obj.price_get(cr,uid,[pl], product_id, qty or 1.0, account.partner_id.id, context=context)[pl] - else: - price = 0.0 - return price - - - # - # data = { - # 'date': boolean - # 'time': boolean - # 'name': boolean - # 'price': boolean - # 'product': many2one id - # } - def invoice_cost_create(self, cr, uid, ids, data=None, context=None): - analytic_account_obj = self.pool.get('account.analytic.account') - account_payment_term_obj = self.pool.get('account.payment.term') - invoice_obj = self.pool.get('account.invoice') - product_obj = self.pool.get('product.product') - invoice_factor_obj = self.pool.get('hr_timesheet_invoice.factor') - fiscal_pos_obj = self.pool.get('account.fiscal.position') - product_uom_obj = self.pool.get('product.uom') - invoice_line_obj = self.pool.get('account.invoice.line') - invoices = [] - if context is None: - context = {} - if data is None: - data = {} - - account_ids = [line.account_id.id for line in self.pool.get('account.analytic.line').browse(cr, uid, ids, context=context)] - for account in analytic_account_obj.browse(cr, uid, account_ids, context=context): - partner = account.partner_id - if (not partner) or not (account.pricelist_id): - raise osv.except_osv(_('Analytic Account incomplete !'), - _('Please fill in the Partner or Customer and Sale Pricelist fields in the Analytic Account:\n%s.') % (account.name,)) - - - - date_due = False - if partner.property_payment_term: - pterm_list= account_payment_term_obj.compute(cr, uid, - partner.property_payment_term.id, value=1, - date_ref=time.strftime('%Y-%m-%d')) - if pterm_list: - pterm_list = [line[0] for line in pterm_list] - pterm_list.sort() - date_due = pterm_list[-1] - - curr_invoice = { - 'name': time.strftime('%d/%m/%Y')+' - '+account.name, - 'partner_id': account.partner_id.id, - 'company_id': account.company_id.id, - 'payment_term': partner.property_payment_term.id or False, - 'account_id': partner.property_account_receivable.id, - 'currency_id': account.pricelist_id.currency_id.id, - 'date_due': date_due, - 'fiscal_position': account.partner_id.property_account_position.id - } - - context2 = context.copy() - context2['lang'] = partner.lang - # set company_id in context, so the correct default journal will be selected - context2['force_company'] = curr_invoice['company_id'] - # set force_company in context so the correct product properties are selected (eg. income account) - context2['company_id'] = curr_invoice['company_id'] - - last_invoice = invoice_obj.create(cr, uid, curr_invoice, context=context2) - invoices.append(last_invoice) - - cr.execute("SELECT product_id, user_id, to_invoice, sum(unit_amount), product_uom_id " \ - "FROM account_analytic_line as line " \ - "WHERE account_id = %s " \ - "AND id IN %s AND to_invoice IS NOT NULL " \ - "GROUP BY product_id, user_id, to_invoice, product_uom_id", (account.id, tuple(ids),)) - - for product_id, user_id, factor_id, qty, uom in cr.fetchall(): - if data.get('product'): - product_id = data['product'][0] - product = product_obj.browse(cr, uid, product_id, context=context2) - if not product: - raise osv.except_osv(_('Error!'), _('There is no product defined. Please select one or force the product through the wizard.')) - factor = invoice_factor_obj.browse(cr, uid, factor_id, context=context2) - factor_name = product_obj.name_get(cr, uid, [product_id], context=context2)[0][1] - if factor.customer_name: - factor_name += ' - ' + factor.customer_name - - ctx = context.copy() - ctx.update({'uom':uom}) - - price = self._get_invoice_price(cr, uid, account, product_id, user_id, qty, ctx) - - general_account = product.product_tmpl_id.property_account_income or product.categ_id.property_account_income_categ - if not general_account: - raise osv.except_osv(_("Configuration Error!"), _("Please define income account for product '%s'.") % product.name) - taxes = product.taxes_id or general_account.tax_ids - tax = fiscal_pos_obj.map_tax(cr, uid, account.partner_id.property_account_position, taxes) - 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, - 'invoice_line_tax_id': [(6,0,tax)], - 'uos_id': uom, - 'account_id': general_account.id, - 'account_analytic_id': account.id, - } - - # - # Compute for lines - # - cr.execute("SELECT * FROM account_analytic_line WHERE account_id = %s and id IN %s AND product_id=%s and to_invoice=%s ORDER BY account_analytic_line.date", (account.id, tuple(ids), product_id, factor_id)) - - line_ids = cr.dictfetchall() - note = [] - for line in line_ids: - # set invoice_line_note - details = [] - if data.get('date', False): - details.append(line['date']) - if data.get('time', False): - if line['product_uom_id']: - details.append("%s %s" % (line['unit_amount'], product_uom_obj.browse(cr, uid, [line['product_uom_id']],context2)[0].name)) - else: - details.append("%s" % (line['unit_amount'], )) - if data.get('name', False): - details.append(line['name']) - note.append(u' - '.join(map(lambda x: unicode(x) or '',details))) - - if note: - curr_line['name'] += "\n" + ("\n".join(map(lambda x: unicode(x) or '',note))) - invoice_line_obj.create(cr, uid, curr_line, context=context) - cr.execute("update account_analytic_line set invoice_id=%s WHERE account_id = %s and id IN %s", (last_invoice, account.id, tuple(ids))) - - invoice_obj.button_reset_taxes(cr, uid, [last_invoice], context) - return invoices - -# -# TODO: check unit of measure !!! -# - class hr_timesheet_invoice_create(osv.osv_memory): _name = 'hr.timesheet.invoice.create' @@ -189,8 +36,8 @@ class hr_timesheet_invoice_create(osv.osv_memory): } _defaults = { - 'date': lambda *args: 1, - 'name': lambda *args: 1 + 'date': 1, + 'name': 1, } def view_init(self, cr, uid, fields, context=None): @@ -210,6 +57,7 @@ class hr_timesheet_invoice_create(osv.osv_memory): def do_create(self, cr, uid, ids, context=None): data = self.read(cr, uid, ids, [], context=context)[0] + # Create an invoice based on selected timesheet lines invs = self.pool.get('account.analytic.line').invoice_cost_create(cr, uid, context['active_ids'], data, context=context) mod_obj = self.pool.get('ir.model.data') act_obj = self.pool.get('ir.actions.act_window')