~camptocamp/banking-addons/vre-preserve-manual-partial-reconcile

« back to all changes in this revision

Viewing changes to account_statement_one_move/statement.py

[IMP] Add transfer lines automatically to balance the bank statement

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
###############################################################################
22
22
 
23
23
from openerp.osv import fields, orm, osv
 
24
from openerp.tools.translate import _
 
25
 
24
26
 
25
27
 
26
28
class AccountStatementProfile(orm.Model):
30
32
            'Group Journal Items',
31
33
            help="Only one Journal Entry will be generated on the "
32
34
                 "validation of the bank statement."),
 
35
        'split_transfer_line': fields.boolean(
 
36
            'Split Transfer Line',
 
37
            help="Two transfer lines will be automatically generated : one "
 
38
                 "for the refunds and one for the payments.")
 
39
    }
33
40
 
34
41
 
35
42
class account_bank_statement(orm.Model):
124
131
        move_obj.post(cr, uid, [move_id], context=context)
125
132
        return True
126
133
 
127
 
           
 
134
 
 
135
    def create_statement_payment_line(self, cr, uid, st, payment_amount, context=None):
 
136
        st_line_obj = self.pool.get('account.bank.statement.line')
 
137
        payment_account_id = st.profile_id.journal_id.default_credit_account_id.id
 
138
        partner_id = st.profile_id.partner_id and profile.partner_id.id or False
 
139
        vals = {
 
140
            'name': _('Payment Transfer'),
 
141
            'date': st.date,
 
142
            'amount': -payment_amount,
 
143
            'partner_id': partner_id,
 
144
            'type': 'general',
 
145
            'statement_id': st.id,
 
146
            'account_id': payment_account_id,
 
147
            'ref': _('Transfer'),
 
148
            'already_completed': True
 
149
                }
 
150
        payment_line_id = st_line_obj.create(cr, uid, vals, context=context)
 
151
        return payment_line_id
 
152
 
 
153
 
 
154
    def create_statement_refund_line(self, cr, uid, st, refund_amount, context=None):
 
155
        st_line_obj = self.pool.get('account.bank.statement.line')
 
156
        refund_account_id = st.profile_id.journal_id.default_credit_account_id.id
 
157
        partner_id = st.profile_id.partner_id and profile.partner_id.id or False
 
158
        vals = {
 
159
            'name': _('Refund Transfer'),
 
160
            'date': st.date,
 
161
            'amount': -refund_amount,
 
162
            'partner_id': partner_id,
 
163
            'type': 'general',
 
164
            'statement_id': st.id,
 
165
            'account_id': refund_account_id,
 
166
            'ref': _('Transfer'),
 
167
            'already_completed': True
 
168
                }
 
169
        refund_line_id = st_line_obj.create(cr, uid, vals, context=context)
 
170
        return refund_line_id
 
171
 
 
172
 
 
173
    def prepare_statement_transfer_lines(self, cr, uid, st, context=None):
 
174
        refund_amount = 0.0
 
175
        payment_amount = 0.0
 
176
        transfer_line_ids = []
 
177
        #Calculate the part of the refund amount and the payment amount
 
178
        for st_line in st.line_ids:
 
179
            if st_line.amount < 0.0:
 
180
                refund_amount += st_line.amount
 
181
            else:
 
182
                payment_amount += st_line.amount
 
183
        #Create 2 Transfer lines or One global tranfer line
 
184
        if st.profile_id.split_transfer_line:
 
185
            transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st,
 
186
                                                               refund_amount,
 
187
                                                               context=context))
 
188
            transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st,
 
189
                                                                 payment_amount,
 
190
                                                                 context=context))
 
191
        else:
 
192
            global_amount = refund_amount + payment_amount
 
193
            #The global transfer line can be a refund or a payment transfer
 
194
            if global_amount < 0.00:
 
195
                transfer_line_ids.append(self.create_statement_refund_line(cr, uid, st,
 
196
                                                                   global_amount,
 
197
                                                                   context=context))
 
198
            else:
 
199
                transfer_line_ids.append(self.create_statement_payment_line(cr, uid, st,
 
200
                                                                     global_amount,
 
201
                                                                     context=context))
 
202
        return transfer_line_ids
 
203
            
 
204
 
 
205
 
128
206
    def button_confirm_bank(self, cr, uid, ids, context=None):
129
207
        st_line_obj = self.pool.get('account.bank.statement.line')
130
208
        if context is None:
131
209
            context = {}
132
210
        for st in self.browse(cr, uid, ids, context=context):
 
211
            if st.profile_id.one_move:
 
212
                refund_line_ids = self.prepare_statement_transfer_lines(cr, uid, st, 
 
213
                                                                        context=context)
133
214
            super(account_bank_statement, self).button_confirm_bank(cr, uid, ids,
134
215
                                                                    context=context)
135
216
            if st.profile_id.one_move: