From 57791c2d4ea2bd10d8aa8c97f4428bb554978695 Mon Sep 17 00:00:00 2001 From: Federico Cruz Date: Thu, 23 Oct 2014 19:59:12 -0500 Subject: [PATCH 1/2] Set correct company on invoices when working on multicompany environment Now when working on multy company environmets the server action creates invioices with accounts belong to correct company based on contract company. --- .../account_analytic_analysis_recurring.py | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/contract/account_analytic_analysis_recurring.py b/contract/account_analytic_analysis_recurring.py index dfc25e8e..e05c8f7f 100644 --- a/contract/account_analytic_analysis_recurring.py +++ b/contract/account_analytic_analysis_recurring.py @@ -93,7 +93,7 @@ class account_analytic_invoice_line(osv.osv): 'uom_id': uom_id or res.uom_id.id or False, 'price_unit': res.list_price or 0.0}) if res.description: - result['name'] += '\n'+res.description + result['name'] += '\n' + res.description res_final = {'value': result} if result['uom_id'] != res.uom_id.id: @@ -164,7 +164,7 @@ class account_analytic_account(osv.osv): raise osv.except_osv( _('Error!'), _('Please define a sale journal for the company "%s".') % - (contract.company_id.name or '', )) + (contract.company_id.name or '',)) partner_payment_term = contract.partner_id.property_payment_term.id inv_data = { 'reference': contract.code or False, @@ -230,20 +230,41 @@ class account_analytic_account(osv.osv): contract.recurring_next_date or current_date, "%Y-%m-%d") interval = contract.recurring_interval if contract.recurring_rule_type == 'daily': - old_date = next_date-relativedelta(days=+interval) - new_date = next_date+relativedelta(days=+interval) + old_date = next_date - relativedelta(days=+interval) + new_date = next_date + relativedelta(days=+interval) elif contract.recurring_rule_type == 'weekly': - old_date = next_date-relativedelta(weeks=+interval) - new_date = next_date+relativedelta(weeks=+interval) + old_date = next_date - relativedelta(weeks=+interval) + new_date = next_date + relativedelta(weeks=+interval) else: - old_date = next_date+relativedelta(months=+interval) - new_date = next_date+relativedelta(months=+interval) + old_date = next_date + relativedelta(months=+interval) + new_date = next_date + relativedelta(months=+interval) context['old_date'] = old_date context['next_date'] = datetime.datetime.strptime( contract.recurring_next_date or current_date, "%Y-%m-%d") - self._prepare_invoice( - cr, uid, contract, context=context) + # Force company for correct evaluate domain access rules + context['force_company'] = contract.company_id.id + + # Create new cursor for handle multi company environments + from openerp import pooler + db, pool = pooler.get_db_and_pool(cr.dbname) + cursor = db.cursor() + try: + this = pool.get('account.analytic.account') + # Need to reload contract on new cursor for prevent + # ORM optimizations use same company for load all + # partner properties + contract = this.browse( + cursor, uid, contract.id, context=context + ) + this._prepare_invoice( + cursor, uid, contract, context=context + ) + cursor.commit() # commit results + except Exception: + cursor.rollback() # error, rollback everything + finally: + cursor.close() # always close cursor self.write( cr, uid, [contract.id], From f25bd906bf57c2df59d094c5bc918fcf2555b1ff Mon Sep 17 00:00:00 2001 From: Federico Cruz Date: Tue, 28 Oct 2014 19:10:40 -0600 Subject: [PATCH 2/2] Set correct company on invoices when working on multicompany environment In the last commit we used a new cursor for this problem, but in this time we make a browse for each contract like Pedro Baeza proposed --- .../account_analytic_analysis_recurring.py | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/contract/account_analytic_analysis_recurring.py b/contract/account_analytic_analysis_recurring.py index 23e52833..fc195803 100644 --- a/contract/account_analytic_analysis_recurring.py +++ b/contract/account_analytic_analysis_recurring.py @@ -243,30 +243,14 @@ class AccountAnalyticAccount(orm.Model): context['next_date'] = new_date # Force company for correct evaluate domain access rules context['force_company'] = contract.company_id.id - - # Create new cursor for handle multi company environments - from openerp import pooler - db, pool = pooler.get_db_and_pool(cr.dbname) - cursor = db.cursor() - try: - this = pool.get('account.analytic.account') - # Need to reload contract on new cursor for prevent - # ORM optimizations use same company for load all - # partner properties - contract = this.browse( - cursor, uid, contract.id, context=context - ) - this._prepare_invoice( - cursor, uid, contract, context=context - ) - cursor.commit() # commit results - except Exception: - cursor.rollback() # error, rollback everything - finally: - cursor.close() # always close cursor - + # Re-read contract with correct company + contract = self.browse(cr, uid, contract.id, context=context) + self._prepare_invoice( + cr, uid, contract, context=context + ) self.write( cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, - context=context) + context=context + ) return True