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

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
###############################################################################
#
#   account_statement_one_move for OpenERP
#   Copyright (C) 2013-TODAY Akretion <http://www.akretion.com>.
#   @author S├ębastien BEAU <sebastien.beau@akretion.com>
#
#   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/>.
#
###############################################################################

from openerp.osv import fields, orm, osv


class AccountStatementProfile(orm.Model):
    _inherit = "account.statement.profile"   
    _columns = {
        'one_move': fields.boolean(
            'Group Journal Items',
            help="Only one Journal Entry will be generated on the "
                 "validation of the bank statement."),
        'split_transfer_line': fields.boolean(
            'Split Transfer Line',
            help="Two transfer lines will be automatically generated : one "
                 "for the refunds and one for the payments.")
    }

class account_bank_statement(orm.Model):
    _inherit = "account.bank.statement"

    def _prepare_move_line_vals(self, cr, uid, st_line, *args, **kwargs):
        res = super(account_bank_statement, self)._prepare_move_line_vals(cr, uid, st_line,
                                                                          *args, **kwargs)
        period_id = self._get_period(cr, uid, st_line.statement_id.date,
                                     context=kwargs.get('context'))
        if st_line.statement_id.profile_id.one_move:
            res.update({
                'period_id': period_id,
                'date': st_line.statement_id.date,
                'name': st_line.ref,
                })
        return res


        return res

    def _prepare_move(self, cr, uid, st_line, st_line_number, context=None):
        res = super(account_bank_statement, self).\
                _prepare_move(cr, uid, st_line, st_line_number, context=context)
        res.update({
            'ref': st_line.statement_id.name,
            'name': st_line.statement_id.name,
            'date': st_line.statement_id.date,
            })
        return res


    def create_move_from_st_line(self, cr, uid, st_line_id, company_currency_id,
                                 st_line_number, context=None):
        if context is None:
            context = {}
        context['from_parent_object'] = True #For compability with module account_constraints
        account_move_obj = self.pool.get('account.move')
        account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
        st_line = account_bank_statement_line_obj.browse(cr, uid, st_line_id,
                                                         context=context)
        st = st_line.statement_id

        if st.profile_id.one_move:
            if not context.get('move_id'):
                move_vals = self._prepare_move(cr, uid, st_line, st_line_number, context=context)
                context['move_id'] = account_move_obj.create(cr, uid, move_vals, context=context)
            self.create_move_line_from_st_line(cr, uid, context['move_id'], 
                                              st_line_id, company_currency_id,
                                              context=context)
            return context['move_id']
        else:
            return super(account_bank_statement, self).create_move_from_st_line(cr, uid, st_line_id, 
                                                                                company_currency_id, 
                                                                                st_line_number,
                                                                                context=context)
    
    def create_move_line_from_st_line(self, cr, uid, move_id, st_line_id,
                                     company_currency_id, context=None):
        """Create the account move line from the statement line.
           
           :param int/long move_id: ID of the account.move
           :param int/long st_line_id: ID of the account.bank.statement.line to create the move line from.
           :param int/long company_currency_id: ID of the res.currency of the company
           :return: ID of the account.move created
        """
        if context is None:
            context = {}
        res_currency_obj = self.pool.get('res.currency')
        account_move_line_obj = self.pool.get('account.move.line')
        account_bank_statement_line_obj = self.pool.get('account.bank.statement.line')
        st_line = account_bank_statement_line_obj.browse(cr, uid, st_line_id, context=context)
        st = st_line.statement_id

        context.update({'date': st_line.date})
        acc_cur = ((st_line.amount<=0) and st.journal_id.default_debit_account_id) or st_line.account_id

        context.update({
                'res.currency.compute.account': acc_cur,
            })
        amount = res_currency_obj.compute(cr, uid, st.currency.id,
                                          company_currency_id, 
                                          st_line.amount, 
                                          context=context)

        bank_move_vals = self._prepare_bank_move_line(cr, uid, st_line, move_id, amount,
            company_currency_id, context=context)
        return account_move_line_obj.create(cr, uid, bank_move_vals, context=context)

    def _valid_move(self, cr, uid, move_id, context=None):
        move_obj = self.pool.get('account.move')
        move = move_obj.browse(cr, uid, move_id, context=context)
        move_obj.post(cr, uid, [move_id], context=context)
        return True


    def _prepare_transfer_move_line_vals(self, cr, uid, st, name, amount, move_id, context=None):
        """
            Prepare the dict of values to create the transfer move lines.
        """
        account_id = st.profile_id.journal_id.default_debit_account_id.id
        partner_id = st.profile_id.partner_id and profile.partner_id.id or False
        if amount < 0.0:
            debit = 0.0
            credit = -amount
        else:
            debit = amount
            credit = 0.0
        vals = {
            'name': name,
            'date': st.date,
            'partner_id': partner_id,
            'statement_id': st.id,
            'account_id': account_id,
            'ref': name,
            'move_id': move_id,
            'credit': credit,
            'debit': debit,
            'journal_id': st.journal_id.id,
            'period_id': st.period_id.id,
        }
        return vals


    def create_move_transfer_lines(self, cr, uid, move, st, context=None):
        move_line_obj = self.pool.get('account.move.line')
        move_id = move.id
        refund = 0.0
        payment = 0.0
        transfer_lines = []
        transfer_line_ids = []
        #Calculate the part of the refund amount and the payment amount
        for move_line in move.line_id:
            refund -= move_line.debit
            payment += move_line.credit
        #Create 2 Transfer lines or One global tranfer line
        if st.profile_id.split_transfer_line:
            if refund:
                transfer_lines.append(['Refund Transfer', refund])
            if payment:
                transfer_lines.append(['Payment Transfer', payment])
        else:
            amount = payment + refund
            if amount:
                transfer_lines.append(['Transfer', amount])
        for transfer_line in transfer_lines:
            vals = self._prepare_transfer_move_line_vals(cr, uid, st, 
                                                         transfer_line[0],
                                                         transfer_line[1], 
                                                         move_id,
                                                         context=context)
            transfer_line_ids.append(move_line_obj.create(cr, uid, vals, context=context))
        return transfer_line_ids

           
    def button_confirm_bank(self, cr, uid, ids, context=None):
        st_line_obj = self.pool.get('account.bank.statement.line')
        move_obj = self.pool.get('account.move')
        if context is None:
            context = {}
        for st in self.browse(cr, uid, ids, context=context):
            super(account_bank_statement, self).button_confirm_bank(cr, uid, ids,
                                                                    context=context)
            if st.profile_id.one_move and context.get('move_id', False):
                move_id = context['move_id']
                move = move_obj.browse(cr, uid, move_id, context=context)
                transfe_line_ids = self.create_move_transfer_lines(cr, uid, move, st, context=context)
                self._valid_move(cr, uid, move_id, context=context)
                lines_ids = [x.id for x in st.line_ids]
                st_line_obj.write(cr, uid, lines_ids,
                        {'move_ids': [(4, move_id, False)]},
                        context=context)
        return True

    def button_cancel(self, cr, uid, ids, context=None):
        done = []
        for st in self.browse(cr, uid, ids, context=context):
            if st.profile_id.one_move and st.line_ids:
                for move in st.line_ids[0].move_ids:
                    if move.state != 'draft':
                        move.button_cancel(context=context)
                    move.unlink(context=context)
                st.write({'state':'draft'}, context=context)
            else:
                super(account_bank_statement, self).button_cancel(cr, uid, ids, 
                                                                 context=context)
        return True