~camptocamp/openerp-swiss-localization/payment-slip-evolution-dunning-fees-support-nbi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
##############################################################################
#
#    Author: Romain Deheele. Copyright Camptocamp SA
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import re
import time

from openerp import addons
from openerp.report import report_sxw
from openerp.osv import orm

from openerp.tools import mod10r
from openerp.tools.translate import _
from ..invoice import AccountInvoice


class L10nCHReportWebkitHtmlMulti(report_sxw.rml_parse):
    """Report that output single BVR from invoice.
    This report is deprectated and will be merged
    with multi payment term BVR report when porting to V8"""

    def __init__(self, cr, uid, name, context):
        super(L10nCHReportWebkitHtmlMulti, self).__init__(cr, uid, name,
                                                          context=context)
        self.localcontext.update({
            'time': time,
            'cr': cr,
            'uid': uid,
            'user': self.pool['res.users'].browse(cr, uid, uid,
                                                  context=context),
            'mod10r': mod10r,
            '_space': AccountInvoice._space,
            '_get_ref': self._get_ref,
            'comma_me': self.comma_me,
            'police_absolute_path': self.police_absolute_path,
            'bvr_absolute_path': self.bvr_absolute_path,
            'headheight': self.headheight,
            'amount': self.amount,
        })

    _compile_get_ref = re.compile('[^0-9]')
    _compile_comma_me = re.compile("^(-?\d+)(\d{3})")
    _compile_check_bvr = re.compile('[0-9][0-9]-[0-9]{3,6}-[0-9]')
    _compile_check_bvr_add_num = re.compile('[0-9]*$')

    def set_context(self, objects, data, ids, report_type=None):
        ml_ids = self.get_obj_reference(ids)
        self._check(ml_ids)
        objects = self.pool['account.move.line'].browse(self.cr, self.uid,
                                                        ml_ids)

        return super(L10nCHReportWebkitHtmlMulti, self).set_context(
            objects,
            data,
            ids,
            report_type=report_type
        )

    def get_obj_reference(self, ids, context=None):
        cursor, uid = self.cr, self.uid
        move_line_obj = self.pool['account.move.line']
        account_obj = self.pool['account.account']
        invoice_obj = self.pool['account.invoice']
        inv = invoice_obj.browse(cursor, uid, ids[0], context=context)
        tier_account_ids = account_obj.search(
            cursor, uid,
            [('type', 'in', ['receivable', 'payable'])],
            context=context)
        move_line_ids = move_line_obj.search(
            cursor, uid,
            [('move_id', '=', inv.move_id.id),
             ('account_id', 'in', tier_account_ids)],
            context=context)
        return move_line_ids

    def amount(self, move, rtype=None):
        ml_model = self.pool['account.move.line']
        return ml_model._get_bvr_amount(self.cr, self.uid, move, rtype=rtype)

    def _get_ref(self, move):
        """Get BVR reference using move related to invoice"""
        model = self.pool['account.move.line']
        return model.get_bvr_ref(self.cr, self.uid, move.id)

    def police_absolute_path(self, inner_path):
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource('l10n_ch_payment_slip', 'report', inner_path)
        return path

    def bvr_absolute_path(self):
        """Will get the ocrb police absolute path"""
        path = addons.get_module_resource('l10n_ch_payment_slip', 'report', 'bvr1.jpg')
        return path

    def headheight(self):
        report_id = self.pool['ir.actions.report.xml'].search(self.cr, self.uid,
                                                              [('name', '=', 'BVR invoice')])[0]
        report = self.pool['ir.actions.report.xml'].browse(self.cr, self.uid, report_id)
        return report.webkit_header.margin_top

    def comma_me(self, amount):
        """Fast swiss number formatting"""
        if isinstance(amount, float):
            amount = str('%.2f' % amount)
        else:
            amount = str(amount)
        orig = amount
        new = self._compile_comma_me.sub("\g<1>'\g<2>", amount)
        if orig == new:
            return new
        else:
            return self.comma_me(new)

    def _check_invoice(self, invoice_ids):
        """Check if the invoice is ready to be printed"""
        if not invoice_ids:
            invoice_ids = []
        cursor = self.cr
        invoice_obj = self.pool['account.invoice']
        ids = invoice_ids
        for invoice in invoice_obj.browse(cursor, self.uid, ids):
            invoice_name = "%s %s" % (invoice.name, invoice.number)
            if not invoice.number:
                raise orm.except_orm(
                    _('UserError'),
                    _('Your invoice should be validated to generate a BVR reference.')
                )
            if not invoice.partner_bank_id:
                raise orm.except_orm(
                    _('UserError'),
                    _('No bank specified on invoice:\n%s' % (invoice_name))
                )
            if not self._compile_check_bvr.match(
                    invoice.partner_bank_id.get_account_number() or ''):
                raise orm.except_orm(
                    _('UserError'),
                    _(('Your bank BVR number should be of the form 0X-XXX-X! '
                       'Please check your company '
                       'information for the invoice:\n%s') % (invoice_name))
                )
            adherent_num = invoice.partner_bank_id.bvr_adherent_num
            if invoice.partner_bank_id.bvr_adherent_num \
                    and not self._compile_check_bvr_add_num.match(adherent_num):
                raise orm.except_orm(
                    _('UserError'),
                    _(('Your bank BVR adherent number must contain only '
                       'digits!\nPlease check your company '
                       'information for the invoice:\n%s') % (invoice_name))
                )
        return ''

    def _check(self, move_ids):
        cursor = self.cr
        move_line_obj = self.pool['account.move.line']
        if not move_ids:
            raise orm.except_orm(
                _('UserError'),
                _('Your invoice should be validated to generate BVR reference.')
            )
        invoice_id = move_line_obj.read(cursor, self.uid, move_ids[0],
                                        ['invoice'])['invoice'][0]
        if invoice_id:
            self._check_invoice([invoice_id])

report_sxw.report_sxw('report.invoice_bvr_webkit_multi',
                      'account.invoice',
                      'l10n_ch_payment_slip/report/multi_bvr.mako',
                      parser=L10nCHReportWebkitHtmlMulti)