From bf99648acc80cf7c1ee320ce23f95527b3e9d7c8 Mon Sep 17 00:00:00 2001 From: "Quentin (OpenERP)" Date: Thu, 17 Nov 2011 15:51:49 +0100 Subject: [PATCH] [FIX] account_followup: fixed the multi company and multi currency behavior bzr revid: qdp-launchpad@openerp.com-20111117145149-yuwk52cyy3jou3iw --- addons/account_followup/account_followup.py | 2 +- .../report/account_followup_print.py | 45 +++++++++---------- .../report/account_followup_print.rml | 8 ++-- .../report/account_followup_report.py | 2 +- .../test/account_followup.yml | 1 + .../wizard/account_followup_print.py | 24 ++++++---- .../wizard/account_followup_print_view.xml | 5 +-- 7 files changed, 45 insertions(+), 42 deletions(-) diff --git a/addons/account_followup/account_followup.py b/addons/account_followup/account_followup.py index 8bc364ad306..a81535ad70c 100644 --- a/addons/account_followup/account_followup.py +++ b/addons/account_followup/account_followup.py @@ -28,7 +28,7 @@ class followup(osv.osv): 'name': fields.char('Name', size=64, required=True), 'description': fields.text('Description'), 'followup_line': fields.one2many('account_followup.followup.line', 'followup_id', 'Follow-Up'), - 'company_id': fields.many2one('res.company', 'Company'), + 'company_id': fields.many2one('res.company', 'Company', required=True), } _defaults = { 'company_id': lambda s, cr, uid, c: s.pool.get('res.company')._company_default_get(cr, uid, 'account_followup.followup', context=c), diff --git a/addons/account_followup/report/account_followup_print.py b/addons/account_followup/report/account_followup_print.py index 1d471d00ec2..f2cf476e674 100644 --- a/addons/account_followup/report/account_followup_print.py +++ b/addons/account_followup/report/account_followup_print.py @@ -35,45 +35,44 @@ class report_rappel(report_sxw.rml_parse): 'get_text': self._get_text }) - def _ids_to_objects(self, partner_ids): + def _ids_to_objects(self, ids): pool = pooler.get_pool(self.cr.dbname) - all_partners = [] - for partner in pool.get('res.partner').browse(self.cr, self.uid, partner_ids): - if partner not in all_partners: - all_partners.append(partner) - return all_partners + all_lines = [] + for line in pool.get('account_followup.stat.by.partner').browse(self.cr, self.uid, ids): + if line not in all_lines: + all_lines.append(line) + return all_lines - def _adr_get(self, partner, type): + def _adr_get(self, stat_line, type): res_partner = pooler.get_pool(self.cr.dbname).get('res.partner') res_partner_address = pooler.get_pool(self.cr.dbname).get('res.partner.address') - adr = res_partner.address_get(self.cr, self.uid, [partner.id], [type])[type] + adr = res_partner.address_get(self.cr, self.uid, [stat_line.partner_id.id], [type])[type] return adr and res_partner_address.read(self.cr, self.uid, [adr]) or [{}] - def _lines_get(self, partner): + def _lines_get(self, stat_by_partner_line): pool = pooler.get_pool(self.cr.dbname) moveline_obj = pool.get('account.move.line') company_obj = pool.get('res.company') obj_currency = pool.get('res.currency') - #FIXME: search on company accounting entries only movelines = moveline_obj.search(self.cr, self.uid, - [('partner_id', '=', partner.id), + [('partner_id', '=', stat_by_partner_line.partner_id.id), ('account_id.type', '=', 'receivable'), - ('reconcile_id', '=', False), ('state', '<>', 'draft')]) + ('reconcile_id', '=', False), ('state', '<>', 'draft'),('company_id','=', stat_by_partner_line.company_id.id)]) movelines = moveline_obj.browse(self.cr, self.uid, movelines) base_currency = movelines[0].company_id.currency_id final_res = [] line_cur = {base_currency.id: {'line': []}} for line in movelines: - if line.currency_id and (not line.currency_id.id in line_cur): - line_cur[line.currency_id.id] = {'line': []} - currency = line.currency_id or line.company_id.currency_id + if line.account_id.currency_id and (not line.account_id.currency_id.id in line_cur): + line_cur[line.account_id.currency_id.id] = {'line': []} + currency = line.account_id.currency_id or line.company_id.currency_id line_data = { 'name': line.move_id.name, 'ref': line.ref, 'date':line.date, 'date_maturity': line.date_maturity, - 'balance': line.currency_id and line.amount_currency or (line.debit - line.credit), + 'balance': currency.id <> line.company_id.currency_id.id and line.amount_currency or (line.debit - line.credit), 'blocked': line.blocked, 'currency_id': currency.symbol or currency.name, } @@ -85,7 +84,7 @@ class report_rappel(report_sxw.rml_parse): return final_res - def _get_text(self, partner, followup_id, context=None): + def _get_text(self, stat_line, followup_id, context=None): if context is None: context = {} fp_obj = pooler.get_pool(self.cr.dbname).get('account_followup.followup') @@ -96,10 +95,10 @@ class report_rappel(report_sxw.rml_parse): li_delay.sort(reverse=True) text = "" a = {} - partner_line = pooler.get_pool(self.cr.dbname).get('account.move.line').search(self.cr, self.uid, [('partner_id','=',partner.id),('reconcile_id','=',False)]) + partner_line_ids = pooler.get_pool(self.cr.dbname).get('account.move.line').search(self.cr, self.uid, [('partner_id','=',stat_line.partner_id.id),('reconcile_id','=',False),('company_id','=',stat_line.company_id.id)]) partner_delay = [] - context.update({'lang': partner.lang}) - for i in pooler.get_pool(self.cr.dbname).get('account.move.line').browse(self.cr, self.uid, partner_line, context): + context.update({'lang': stat_line.partner_id.lang}) + for i in pooler.get_pool(self.cr.dbname).get('account.move.line').browse(self.cr, self.uid, partner_line_ids, context): for delay in li_delay: if i.followup_line_id and str(i.followup_line_id.delay)==str(delay): text = i.followup_line_id.description @@ -108,16 +107,16 @@ class report_rappel(report_sxw.rml_parse): text = partner_delay and a[max(partner_delay)] or '' if text: text = text % { - 'partner_name': partner.name, + 'partner_name': stat_line.partner_id.name, 'date': time.strftime('%Y-%m-%d'), - 'company_name': fp_obj.browse(self.cr, self.uid, followup_id).company_id.name, + 'company_name': stat_line.company_id.name, 'user_signature': pooler.get_pool(self.cr.dbname).get('res.users').browse(self.cr, self.uid, self.uid, context).signature or '', } return text report_sxw.report_sxw('report.account_followup.followup.print', - 'res.partner', 'addons/account_followup/report/account_followup_print.rml', + 'account_followup.stat.by.partner', 'addons/account_followup/report/account_followup_print.rml', parser=report_rappel) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/account_followup/report/account_followup_print.rml b/addons/account_followup/report/account_followup_print.rml index 812f0aa0e50..92eda92e9ee 100644 --- a/addons/account_followup/report/account_followup_print.rml +++ b/addons/account_followup/report/account_followup_print.rml @@ -124,7 +124,7 @@ [[ repeatIn(ids_to_objects(data['form']['partner_ids']),'o') ]] - [[ setLang(o.lang) ]] + [[ setLang(o.partner_id.lang) ]] @@ -132,7 +132,7 @@ [[ repeatIn(adr_get(o,'invoice'),'a' )]] - [[ o.name or '' ]] + [[ o.partner_id.name or '' ]] [[ a['street'] or '']] [[ a['street2'] or removeParentNode('para') ]] [[ a['zip'] or '' ]] [[ a['city'] or '' ]] @@ -141,7 +141,7 @@ - VAT: [[ o.vat or removeParentNode('para') ]] + VAT: [[ o.partner_id.vat or removeParentNode('para') ]] @@ -150,7 +150,7 @@ Document : Customer account statement Date : [[ formatLang(data['form']['date'],date=True) ]] - Customer Ref : [[ o.ref or '' ]] + Customer Ref : [[ o.partner_id.ref or '' ]] diff --git a/addons/account_followup/report/account_followup_report.py b/addons/account_followup/report/account_followup_report.py index 25fc37691f4..c2a842cfd4c 100644 --- a/addons/account_followup/report/account_followup_report.py +++ b/addons/account_followup/report/account_followup_report.py @@ -68,7 +68,7 @@ class account_followup_stat(osv.osv): cr.execute(""" create or replace view account_followup_stat as ( SELECT - l.partner_id as id, + l.id as id, l.partner_id AS partner_id, min(l.date) AS date_move, max(l.date) AS date_move_last, diff --git a/addons/account_followup/test/account_followup.yml b/addons/account_followup/test/account_followup.yml index b43ca32d200..2650a40a7ad 100644 --- a/addons/account_followup/test/account_followup.yml +++ b/addons/account_followup/test/account_followup.yml @@ -105,4 +105,5 @@ self.do_print(cr, uid, [ref("account_followup_print_all_0")], {"lang": 'en_US', "active_model": "ir.ui.menu", "active_ids": [ref("account_followup.account_followup_print_menu")], "tz": False, "date": time.strftime('%Y-%m-%d'), "followup_id": ref("account_followup_followup_testfollowups0"), "active_id": ref("account_followup.account_followup_print_menu"), + "company_id": ref('base.main_company'), }) diff --git a/addons/account_followup/wizard/account_followup_print.py b/addons/account_followup/wizard/account_followup_print.py index 3fe404134ae..ea1083f26ff 100644 --- a/addons/account_followup/wizard/account_followup_print.py +++ b/addons/account_followup/wizard/account_followup_print.py @@ -31,7 +31,7 @@ class account_followup_print(osv.osv_memory): _description = 'Print Followup & Send Mail to Customers' _columns = { 'date': fields.date('Follow-up Sending Date', required=True, help="This field allow you to select a forecast date to plan your follow-ups"), - 'followup_id': fields.many2one('account_followup.followup', 'Follow-up', required=True) + 'followup_id': fields.many2one('account_followup.followup', 'Follow-up', required=True), } def _get_followup(self, cr, uid, context=None): @@ -51,7 +51,7 @@ class account_followup_print(osv.osv_memory): data = self.browse(cr, uid, ids, context=context)[0] model_data_ids = mod_obj.search(cr, uid, [('model','=','ir.ui.view'),('name','=','view_account_followup_print_all')], context=context) resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id'] - context.update({'followup_id': data.followup_id.id, 'date':data.date}) + context.update({'followup_id': data.followup_id.id, 'date': data.date, 'company_id': data.followup_id.company_id.id}) return { 'name': _('Select Partners'), 'view_type': 'form', @@ -87,10 +87,13 @@ class account_followup_stat_by_partner(osv.osv): def init(self, cr): tools.drop_view_if_exists(cr, 'account_followup_stat_by_partner') + # Here we don't have other choice but to create a virtual ID based on the concatenation + # of the partner_id and the company_id. An assumption that the number of companies will + # not reach 10 000 records is made, what should be enough for a time. cr.execute(""" create or replace view account_followup_stat_by_partner as ( SELECT - l.partner_id AS id, + l.partner_id * 10000 + l.company_id as id, l.partner_id AS partner_id, min(l.date) AS date_move, max(l.date) AS date_move_last, @@ -148,6 +151,8 @@ class account_followup_print_all(osv.osv_memory): context = {} if ids: data = self.browse(cr, uid, ids, context=context)[0] + company_id = 'company_id' in context and context['company_id'] or data.company_id.id + cr.execute( "SELECT l.partner_id, l.followup_line_id,l.date_maturity, l.date, l.id "\ "FROM account_move_line AS l "\ @@ -159,7 +164,8 @@ class account_followup_print_all(osv.osv_memory): "AND (l.partner_id is NOT NULL) "\ "AND (a.active) "\ "AND (l.debit > 0) "\ - "ORDER BY l.date") + "AND (l.company_id = %s) "\ + "ORDER BY l.date", (company_id,)) move_lines = cr.fetchall() old = None fups = {} @@ -189,16 +195,16 @@ class account_followup_print_all(osv.osv_memory): continue if followup_line_id not in fups: continue + stat_line_id = partner_id * 10000 + company_id if date_maturity: if date_maturity <= fups[followup_line_id][0].strftime('%Y-%m-%d'): if partner_id not in partner_list: - partner_list.append(partner_id) + partner_list.append(stat_line_id) to_update[str(id)]= {'level': fups[followup_line_id][1], 'partner_id': partner_id} elif date and date <= fups[followup_line_id][0].strftime('%Y-%m-%d'): if partner_id not in partner_list: - partner_list.append(partner_id) - to_update[str(id)]= {'level': fups[followup_line_id][1], 'partner_id': partner_id} - + partner_list.append(stat_line_id) + to_update[str(id)]= {'level': fups[followup_line_id][1], 'partner_id': stat_line_id} return {'partner_ids': partner_list, 'to_update': to_update} def do_mail(self ,cr, uid, ids, context=None): @@ -225,7 +231,7 @@ class account_followup_print_all(osv.osv_memory): partners.append(line.partner_id) dict_lines[line.partner_id.id] =line for partner in partners: - ids_lines = move_obj.search(cr,uid,[('partner_id','=',partner.id),('reconcile_id','=',False),('account_id.type','in',['receivable'])]) + ids_lines = move_obj.search(cr,uid,[('partner_id','=',partner.id),('reconcile_id','=',False),('account_id.type','in',['receivable']),('company_id','=',context.get('company_id', False))]) data_lines = move_obj.browse(cr, uid, ids_lines, context=context) followup_data = dict_lines[partner.id] dest = False diff --git a/addons/account_followup/wizard/account_followup_print_view.xml b/addons/account_followup/wizard/account_followup_print_view.xml index 9d74060b19a..ce178d9fa24 100644 --- a/addons/account_followup/wizard/account_followup_print_view.xml +++ b/addons/account_followup/wizard/account_followup_print_view.xml @@ -13,7 +13,6 @@ - @@ -81,7 +80,7 @@ - + @@ -137,7 +136,5 @@ form new - -