[FIX] account_followup: print the overdue report on partner defined in account_follow...
authorQuentin (OpenERP) <qdp-launchpad@openerp.com>
Thu, 3 Jan 2013 09:48:18 +0000 (10:48 +0100)
committerQuentin (OpenERP) <qdp-launchpad@openerp.com>
Thu, 3 Jan 2013 09:48:18 +0000 (10:48 +0100)
bzr revid: qdp-launchpad@openerp.com-20130103094818-4e73d2clqkqai5xh

addons/account_followup/account_followup.py
addons/account_followup/report/account_followup_print.py

index 9590777..9821030 100644 (file)
@@ -280,18 +280,29 @@ class res_partner(osv.osv):
 
     def do_button_print(self, cr, uid, ids, context=None):
         assert(len(ids) == 1)
+        company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
+        #search if the partner has accounting entries to print. If not, it may not be present in the
+        #psql view the report is based on, so we need to stop the user here.
+        if not self.pool.get('account.move.line').search(cr, uid, [
+                                                                   ('partner_id', '=', ids[0]),
+                                                                   ('account_id.type', '=', 'receivable'),
+                                                                   ('reconcile_id', '=', False),
+                                                                   ('state', '!=', 'draft'),
+                                                                   ('company_id', '=', company_id),
+                                                                  ], context=context):
+            raise osv.except_osv(_('Error!'),_("The partner does not have any accounting entries to print in the overdue report for the current company."))
         self.message_post(cr, uid, [ids[0]], body=_('Printed overdue payments report'), context=context)
-        data['partner_ids'] = [ids[0] * 10000 + company_id]
-        datas = {
-             'ids': [],
-             'model': 'account_followup.followup',
-             'form': data
+        #build the id of this partner in the psql view. Could be replaced by a search with [('company_id', '=', company_id),('partner_id', '=', ids[0])]
+        wizard_partner_ids = [ids[0] * 10000 + company_id]
+        followup_ids = self.pool.get('account_followup.followup').search(cr, uid, [('company_id', '=', company_id)], context=context)
+        if not followup_ids:
+            raise osv.except_osv(_('Error!'),_("There is no followup plan defined for the current company."))
+        data = {
+            'date': fields.date.today(),
+            'followup_id': followup_ids[0],
         }
-        return {
-            'type': 'ir.actions.report.xml',
-            'report_name': 'account_followup.followup.print',
-            'datas': datas,
-            }
+        #call the print overdue report on this partner
+        return self.do_partner_print(cr, uid, wizard_partner_ids, data, context=context)
 
     def _get_amounts_and_date(self, cr, uid, ids, name, arg, context=None):
         '''
index 0343fe0..16d88d6 100644 (file)
@@ -79,24 +79,29 @@ class report_rappel(report_sxw.rml_parse):
     def _get_text(self, stat_line, followup_id, context=None):
         if context is None:
             context = {}
+        context.update({'lang': stat_line.partner_id.lang})
         fp_obj = pooler.get_pool(self.cr.dbname).get('account_followup.followup')
-        fp_line = fp_obj.browse(self.cr, self.uid, followup_id).followup_line
+        fp_line = fp_obj.browse(self.cr, self.uid, followup_id, context=context).followup_line
+        if not fp_line:
+            raise osv.except_osv(_('Error!'),_("The followup plan defined for the current company does not have any followup action."))
+        #the default text will be the first fp_line in the sequence with a description.
+        default_text = ''
         li_delay = []
         for line in fp_line:
+            if not default_text and line.description:
+                default_text = line.description
             li_delay.append(line.delay)
         li_delay.sort(reverse=True)
-        text = ""
         a = {}
-        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),('blocked','=',False)])
-        partner_delay = []
-        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
-                    a[delay] = text
-                    partner_delay.append(delay)
-        text = partner_delay and a[max(partner_delay)] or ''
+        #look into the lines of the partner that already have a followup level, and take the description of the higher level for which it is available
+        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),('blocked','=',False),('state','!=','draft'),('debit','!=',False),('account_id.type','=','receivable'),('followup_line_id','!=',False)])
+        partner_max_delay = 0
+        partner_max_text = ''
+        for i in pooler.get_pool(self.cr.dbname).get('account.move.line').browse(self.cr, self.uid, partner_line_ids, context=context):
+            if i.followup_line_id.delay > partner_max_delay and i.followup_line_id.description:
+                partner_max_delay = i.followup_line_id.delay
+                partner_max_text = i.followup_line_id.description
+        text = partner_max_delay and partner_max_text or default_text
         if text:
             text = text % {
                 'partner_name': stat_line.partner_id.name,