~camptocamp/openerp-swiss-localization/7.0-pending-merge

« back to all changes in this revision

Viewing changes to l10n_ch_payment_slip/invoice.py

  • Committer: nicolas.bessi at camptocamp
  • Author(s): romain.deheele at camptocamp
  • Date: 2013-09-23 07:15:24 UTC
  • mfrom: (177.1.11 multi-payment-bvr)
  • Revision ID: nicolas.bessi@camptocamp.com-20130923071524-sv1b3qa46ocug3dr
[MRG]release 7.2 adds support of multi payments term BVR.
Adds a new report that will allows to print BVR from invoice using account.move.line So if there is many payment terms many BVR will be print per invoice.
The previous report is kept but deprecated.
A new field is addes on account.move.line transaction_ref.
Changes to manage bvr_reference and transaction_ref depending on payment terms are automatic

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from openerp.tools import mod10r
24
24
 
25
25
 
 
26
class AccountMoveLine(Model):
 
27
 
 
28
    _inherit = "account.move.line"
 
29
 
 
30
    _compile_get_ref = re.compile('[^0-9]')
 
31
 
 
32
    _columns = {
 
33
        'transaction_ref': fields.char('Transaction Ref.', size=128),
 
34
    }
 
35
 
 
36
    def init(self, cr):
 
37
        cr.execute('UPDATE account_move_line SET transaction_ref = ref'
 
38
                   '  WHERE transaction_ref IS NULL'
 
39
                   '   AND ref IS NOT NULL')
 
40
        return True
 
41
 
 
42
    def get_bvr_ref(self, cursor, uid, move_line_id, context=None):
 
43
        """Retrieve ESR/BVR reference from move line in order to print it"""
 
44
        res = ''
 
45
        if isinstance(move_line_id, (tuple, list)):
 
46
            assert len(move_line_id) == 1, "Only 1 ID expected"
 
47
            move_line_id = move_line_id[0]
 
48
        move_line = self.browse(cursor, uid, move_line_id, context=context)
 
49
        ## We check if the type is bvr, if not we return false
 
50
        if move_line.invoice.partner_bank_id.state != 'bvr':
 
51
            return ''
 
52
        ##
 
53
        if move_line.invoice.partner_bank_id.bvr_adherent_num:
 
54
            res = move_line.invoice.partner_bank_id.bvr_adherent_num
 
55
        move_number = ''
 
56
        if move_line.invoice.number:
 
57
            move_number = self._compile_get_ref.sub('', str(move_line.invoice.number) + str(move_line_id))
 
58
        return mod10r(res + move_number.rjust(26 - len(res), '0'))
 
59
 
 
60
 
26
61
class AccountInvoice(Model):
27
62
    """Inherit account.invoice in order to add bvr
28
63
    printing functionnalites. BVR is a Swiss payment vector"""
40
75
 
41
76
    def _compute_full_bvr_name(self, cursor, uid, ids, field_names, arg, context=None):
42
77
        res = {}
 
78
        move_line_obj = self.pool.get('account.move.line')
 
79
        account_obj = self.pool.get('account.account')
 
80
        tier_account_id = account_obj.search(cursor, uid, [('type', 'in', ['receivable', 'payable'])])
43
81
        for inv in self.browse(cursor, uid, ids, context=context):
44
 
            res[inv.id] = self._space(inv.get_bvr_ref())
 
82
            move_lines = move_line_obj.search(cursor, uid, [('move_id', '=', inv.move_id.id),
 
83
                                                            ('account_id', 'in', tier_account_id)])
 
84
            if move_lines:
 
85
                if len(move_lines) == 1:
 
86
                    res[inv.id] = self._space(inv.get_bvr_ref())
 
87
                else:
 
88
                    refs = []
 
89
                    for move_line in move_line_obj.browse(cursor, uid, move_lines, context=context):
 
90
                        refs.append(self._space(move_line.get_bvr_ref()))
 
91
                    res[inv.id] = ' ; '.join(refs)
45
92
        return res
46
93
 
47
94
    _columns = {
76
123
        """Spaces * 5.
77
124
 
78
125
        Example:
79
 
            >>> self._space('123456789012345')
 
126
            self._space('123456789012345')
80
127
            '12 34567 89012 345'
81
128
        """
82
129
        return ''.join([' '[(i - 2) % nbrspc:] + c for i, c in enumerate(nbr)])
83
130
 
84
 
    def action_number(self, cursor, uid, ids, context=None):
85
 
        res = super(AccountInvoice, self).action_number(cursor, uid, ids, context=context)
86
 
        for inv in self.browse(cursor, uid, ids, context=context):
87
 
            if inv.type != 'out_invoice' or inv.partner_bank_id.state != 'bvr':
 
131
    def _update_ref_on_account_analytic_line(self, cr, uid, ref, move_id, context=None):
 
132
        cr.execute('UPDATE account_analytic_line SET ref=%s'
 
133
                   '   FROM account_move_line '
 
134
                   ' WHERE account_move_line.move_id = %s '
 
135
                   '   AND account_analytic_line.move_id = account_move_line.id',
 
136
                   (ref, move_id))
 
137
        return True
 
138
 
 
139
    def action_number(self, cr, uid, ids, context=None):
 
140
        res = super(AccountInvoice, self).action_number(cr, uid, ids, context=context)
 
141
        move_line_obj = self.pool.get('account.move.line')
 
142
        account_obj = self.pool.get('account.account')
 
143
        tier_account_id = account_obj.search(cr, uid, [('type', 'in', ['receivable', 'payable'])])
 
144
 
 
145
        for inv in self.browse(cr, uid, ids, context=context):
 
146
            if inv.type != 'out_invoice' and inv.partner_bank_id.state != 'bvr':
88
147
                continue
89
 
            ref = inv.get_bvr_ref()
90
 
            move_id = inv.move_id
91
 
            if move_id:
92
 
                cursor.execute('UPDATE account_move SET ref=%s'
93
 
                               '  WHERE id=%s',
94
 
                               (ref, move_id.id))
95
 
                cursor.execute('UPDATE account_move_line SET ref=%s'
96
 
                               '  WHERE move_id=%s',
97
 
                               (ref, move_id.id))
98
 
                cursor.execute('UPDATE account_analytic_line SET ref=%s'
99
 
                               '   FROM account_move_line '
100
 
                               ' WHERE account_move_line.move_id = %s '
101
 
                               '   AND account_analytic_line.move_id = account_move_line.id',
102
 
                               (ref, move_id.id))
 
148
            move_lines = move_line_obj.search(cr, uid, [('move_id', '=', inv.move_id.id),
 
149
                                                        ('account_id', 'in', tier_account_id)])
 
150
            # We keep this branch for compatibility with single BVR report.
 
151
            # This should be cleaned when porting to V8
 
152
            if move_lines:
 
153
                if len(move_lines) == 1:
 
154
                    ref = inv.get_bvr_ref()
 
155
                    move_id = inv.move_id
 
156
                    if move_id:
 
157
                        cr.execute('UPDATE account_move_line SET transaction_ref=%s'
 
158
                                   '  WHERE move_id=%s',
 
159
                                   (ref, move_id.id))
 
160
                        self._update_ref_on_account_analytic_line(cr, uid, ref, move_id.id)
 
161
                else:
 
162
                    for move_line in move_line_obj.browse(cr, uid, move_lines, context=context):
 
163
                        ref = move_line.get_bvr_ref()
 
164
                        if ref:
 
165
                            cr.execute('UPDATE account_move_line SET transaction_ref=%s'
 
166
                                       '  WHERE id=%s',
 
167
                                       (ref, move_line.id))
 
168
                            self._update_ref_on_account_analytic_line(cr, uid, ref,
 
169
                                                                      move_line.move_id.id)
103
170
        return res
104
171
 
105
172
    def copy(self, cursor, uid, inv_id, default=None, context=None):