# -*- coding: utf-8 -*-

from openerp import pooler
from openerp.report import report_sxw
from openerp.tools.translate import _
from osv import osv

from report_webkit.webkit_report import WebKitParser


class payment_notice_parser(report_sxw.rml_parse):

    def __init__(self, cr, uid, name, context):
        super(payment_notice_parser, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'debit_credit': self.get_debit_credit,
            'format_amount': self.format_amount,
            'message': self.get_message,
        })

    def get_debit_credit(self, br):
        return _('Debit') if br.type == 'debit' else _('Credit')

    def format_amount(self, amount, br):
        # little check
        if not amount:
            return '0.00'
        # shortcut
        position = br.currency_id.position
        symbol = br.currency_id.symbol
        # currency after
        if position == 'after':
            return '%s %s' % (amount, symbol)
        # currency before
        if position == 'before':
            return '%s %s' % (symbol, amount)
        return amount.strip()

    def get_message(self, this_br):
       company = this_br.company_id
       if not company:
           return ''
       iban = this_br.partner_bank_id.acc_number
       date = this_br.date
       if this_br.state == 'posted':
           return company.message_voucher_validate.replace('$iban', iban).replace('$date', str(date))
       return ''


class payment_notice_report(WebKitParser):
    def remove_previous(self, cr, uid, ids, context=None):
        # get attachement model
        ir_att_osv = pooler.get_pool(cr.dbname).get('ir.attachment')
        # previous ids
        data_ids = ir_att_osv.search(
            cr,
            uid,
            [('res_model', '=', 'account.voucher'),
             ('res_id', 'in', ids)],
            context=context)
        # remove previous items
        ir_att_osv.unlink(cr, uid, data_ids)

    def create(self, cr, uid, ids, datas, context=None):
        # remove previous items
        self.remove_previous(cr, uid, ids, context=context)
        # call parent
        return super(payment_notice_report, self).create(cr, uid, ids, datas, context)


payment_notice_report('report.account_streamline.payment_notice',
               'account.voucher',
               'addons/account_streamline/report'
               '/payment_notice.mako',
               parser=payment_notice_parser)

@@ -373,32 +373,103 @@
 ontext=context)%0A
+        self.__check_vouchers(cr, uid, context%5B'active_ids'%5D, context)%0A
         self.loc
@@ -640,16 +640,580 @@
     %7D)%0A%0A
+    def __check_vouchers(self, cr, uid, ids, context=None):%0A        %22%22%22 This function check if the message for payment%0A        is set in the company settings and raise in the other case.%0A        %22%22%22%0A        voucher_osv = self.pool.get('account.voucher')%0A        voucher_br = voucher_osv.browse(cr, uid, ids%5B0%5D, context)%0A        if not voucher_br.company_id.message_voucher_validate:%0A            raise osv.except_osv(%0A                _('Error'),%0A                _('Please set the message for payments'%0A                  ' in your company settings.')%0A            )%0A%0A
     def 

Fix #22 - Check if message for payment is set, raise if not