~sergio-incaser/banking-addons/banking-addons

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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2009 EduSense BV (<http://www.edusense.nl>).
#              (C) 2011 - 2013 Therp BV (<http://therp.nl>).
#            
#    All other contributions are (C) by their respective contributors
#
#    All Rights Reserved
#
#    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/>.
#
##############################################################################

'''
This module shows resemblance to both account_bankimport/bankimport.py,
account/account_bank_statement.py and account_payment(_export). All hail to
the makers. account_bankimport is only referenced for their ideas and the
framework of the filters, which they in their turn seem to have derived
from account_coda.

Modifications are extensive:

1. In relation to account/account_bank_statement.py:
    account.bank.statement is effectively stripped from its account.period
    association, while account.bank.statement.line is extended with the same
    association, thereby reflecting real world usage of bank.statement as a
    list of bank transactions and bank.statement.line as a bank transaction.

2. In relation to account/account_bankimport:
    All filter objects and extensions to res.company are removed. Instead a
    flexible auto-loading and auto-browsing plugin structure is created,
    whereby business logic and encoding logic are strictly separated.
    Both parsers and business logic are rewritten from scratch.

    The association of account.journal with res.company is replaced by an
    association of account.journal with res.partner.bank, thereby allowing
    multiple bank accounts per company and one journal per bank account.

    The imported bank statement file does not result in a single 'bank
    statement', but in a list of bank statements by definition of whatever the
    bank sees as a statement. Every imported bank statement contains at least
    one bank transaction, which is a modded account.bank.statement.line.

3. In relation to account_payment:
    An additional state was inserted between 'open' and 'done', to reflect a
    exported bank orders file which was not reported back through statements.
    The import of statements matches the payments and reconciles them when
    needed, flagging them 'done'. When no export wizards are found, the
    default behavior is to flag the orders as 'sent', not as 'done'.
    Rejected payments from the bank receive on import the status 'rejected'.
'''

from openerp.osv import orm, fields
from openerp.osv.osv import except_osv
from openerp.tools.translate import _
from openerp import netsvc
from openerp.addons.decimal_precision import decimal_precision as dp


class account_banking_account_settings(orm.Model):
    '''Default Journal for Bank Account'''
    _name = 'account.banking.account.settings'
    _description = __doc__
    _columns = {
        'company_id': fields.many2one('res.company', 'Company', select=True,
                                      required=True),
        'partner_bank_id': fields.many2one('res.partner.bank', 'Bank Account',
                                           select=True, required=True),
        'journal_id': fields.many2one('account.journal', 'Journal',
                                      required=True),
        'partner_id': fields.related(
            'company_id', 'partner_id',
            type='many2one', relation='res.partner',
            string='Partner'),
        'default_credit_account_id': fields.many2one(
            'account.account', 'Default credit account', select=True,
            help=('The account to use when an unexpected payment was signaled. '
                  'This can happen when a direct debit payment is cancelled '
                  'by a customer, or when no matching payment can be found. '
                  ' Mind that you can correct movements before confirming them.'
                 ),
            required=True
        ),
        'default_debit_account_id': fields.many2one(
            'account.account', 'Default debit account',
            select=True, required=True,
            help=('The account to use when an unexpected payment is received. '
                  'This can be needed when a customer pays in advance or when '
                  'no matching invoice can be found. Mind that you can correct '
                  'movements before confirming them.'
                 ),
        ),
        'costs_account_id': fields.many2one(
            'account.account', 'Bank Costs Account', select=True,
            help=('The account to use when the bank invoices its own costs. '
                  'Leave it blank to disable automatic invoice generation '
                  'on bank costs.'
                 ),
        ),
        'invoice_journal_id': fields.many2one(
            'account.journal', 'Costs Journal', 
            help=('This is the journal used to create invoices for bank costs.'
                 ),
        ),
        'bank_partner_id': fields.many2one(
            'res.partner', 'Bank Partner',
            help=('The partner to use for bank costs. Banks are not partners '
                  'by default. You will most likely have to create one.'
                 ),
        ),

    }

    def _default_company(self, cr, uid, context=None):
        """
        Return the user's company or the first company found
        in the database
        """
        user = self.pool.get('res.users').read(
            cr, uid, uid, ['company_id'], context=context)
        if user['company_id']:
            return user['company_id'][0]
        return self.pool.get('res.company').search(
            cr, uid, [('parent_id', '=', False)])[0]
    
    def _default_partner_id(self, cr, uid, context=None, company_id=False):
        if not company_id:
            company_id = self._default_company(cr, uid, context=context)
        return self.pool.get('res.company').read(
            cr, uid, company_id, ['partner_id'],
            context=context)['partner_id'][0]

    def _default_journal(self, cr, uid, context=None, company_id=False):
        if not company_id:
            company_id = self._default_company(cr, uid, context=context)
        journal_ids = self.pool.get('account.journal').search(
            cr, uid, [('type', '=', 'bank'), ('company_id', '=', company_id)])
        return journal_ids and journal_ids[0] or False

    def _default_partner_bank_id(
            self, cr, uid, context=None, company_id=False):
        if not company_id:
            company_id = self._default_company(cr, uid, context=context)
        partner_id = self.pool.get('res.company').read(
            cr, uid, company_id, ['partner_id'],
            context=context)['partner_id'][0]
        bank_ids = self.pool.get('res.partner.bank').search(
            cr, uid, [('partner_id', '=', partner_id)], context=context)
        return bank_ids and bank_ids[0] or False

    def _default_debit_account_id(
            self, cr, uid, context=None, company_id=False):
        localcontext = context and context.copy() or {}
        localcontext['force_company'] = (
            company_id or self._default_company(cr, uid, context=context))
        account_def = self.pool.get('ir.property').get(
            cr, uid, 'property_account_receivable',
            'res.partner', context=localcontext)
        return account_def and account_def.id or False

    def _default_credit_account_id(
            self, cr, uid, context=None, company_id=False):
        localcontext = context and context.copy() or {}
        localcontext['force_company'] = (
            company_id or self._default_company(cr, uid, context=context))
        account_def = self.pool.get('ir.property').get(
            cr, uid, 'property_account_payable',
            'res.partner', context=localcontext)
        return account_def and account_def.id or False

    def find(self, cr, uid, journal_id, partner_bank_id=False, context=None):
        domain = [('journal_id', '=', journal_id)]
        if partner_bank_id:
            domain.append(('partner_bank_id', '=', partner_bank_id))
        return self.search(cr, uid, domain, context=context)

    def onchange_partner_bank_id(
            self, cr, uid, ids, partner_bank_id, context=None):
        values = {}
        if partner_bank_id:
            bank = self.pool.get('res.partner.bank').read(
                cr, uid, partner_bank_id, ['journal_id'], context=context)
            if bank['journal_id']:
                values['journal_id'] = bank['journal_id'][0]
        return {'value': values}

    def onchange_company_id (
            self, cr, uid, ids, company_id=False, context=None):
        if not company_id:
            return {}
        result = {
            'partner_id': self._default_partner_id(
                cr, uid, company_id=company_id, context=context),
            'journal_id': self._default_journal(
                cr, uid, company_id=company_id, context=context),
            'default_debit_account_id': self._default_debit_account_id(
                cr, uid, company_id=company_id, context=context),
            'default_credit_account_id': self._default_credit_account_id(
                cr, uid, company_id=company_id, context=context),
            }
        return {'value': result}

    _defaults = {
        'company_id': _default_company,
        'partner_id': _default_partner_id,
        'journal_id': _default_journal,
        'default_debit_account_id': _default_debit_account_id,
        'default_credit_account_id': _default_credit_account_id,
        'partner_bank_id': _default_partner_bank_id,
    }
account_banking_account_settings()


class account_banking_imported_file(orm.Model):
    '''Imported Bank Statements File'''
    _name = 'account.banking.imported.file'
    _description = __doc__
    _rec_name = 'date'
    _columns = {
        'company_id': fields.many2one('res.company', 'Company',
                                      select=True, readonly=True
                                     ),
        'date': fields.datetime('Import Date', readonly=True, select=True,
                                states={'draft': [('readonly', False)]}
                               ),
        'format': fields.char('File Format', size=20, readonly=True,
                              states={'draft': [('readonly', False)]}
                             ),
        'file': fields.binary('Raw Data', readonly=True,
                              states={'draft': [('readonly', False)]}
                             ),
        'file_name': fields.char('File name', size=256),
        'log': fields.text('Import Log', readonly=True,
                           states={'draft': [('readonly', False)]}
                          ),
        'user_id': fields.many2one('res.users', 'Responsible User',
                                   readonly=True, select=True,
                                   states={'draft': [('readonly', False)]}
                                  ),
        'state': fields.selection(
            [('unfinished', 'Unfinished'),
             ('error', 'Error'),
             ('review', 'Review'),
             ('ready', 'Finished'),
            ], 'State', select=True, readonly=True
        ),
        'statement_ids': fields.one2many('account.bank.statement',
                                         'banking_id', 'Statements',
                                         readonly=False,
                                  ),
    }
    _defaults = {
        'date': fields.date.context_today,
        'user_id': lambda self, cr, uid, context: uid,
    }
account_banking_imported_file()


class account_bank_statement(orm.Model):
    '''
    Implement changes to this model for the following features:

    * bank statement lines have their own period_id, derived from
    their effective date. The period and date are propagated to
    the move lines created from each statement line
    * bank statement lines have their own state. When a statement
    is confirmed, all lines are confirmed too. When a statement
    is reopened, lines remain confirmed until reopened individually.
    * upon confirmation of a statement line, the move line is
    created and reconciled according to the matched entry(/ies)
    '''
    _inherit = 'account.bank.statement'

    _columns = {
        'period_id': fields.many2one('account.period', 'Period',
                                     required=False, readonly=True),
        'banking_id': fields.many2one('account.banking.imported.file',
                                     'Imported File', readonly=True,
                                     ),
    }

    _defaults = {
        'period_id': False,
    }

    def _check_company_id(self, cr, uid, ids, context=None):
        """
        Adapt this constraint method from the account module to reflect the
        move of period_id to the statement line: also check the periods of the
        lines. Update the statement period if it does not have one yet.
        Don't call super, because its check is integrated below and
        it will break if a statement does not have any lines yet and
        therefore may not have a period.
        """
        for statement in self.browse(cr, uid, ids, context=context):
            if (statement.period_id and
                    statement.company_id != statement.period_id.company_id):
                return False
            for line in statement.line_ids:
                if (line.period_id and
                        statement.company_id != line.period_id.company_id):
                    return False
                if not statement.period_id:
                    statement.write({'period_id': line.period_id.id})
                    statement.refresh()
        return True
    
    # Redefine the constraint, or it still refer to the original method
    _constraints = [
        (_check_company_id,
         'The journal and period chosen have to belong to the same company.',
         ['journal_id','period_id']),
        ]

    def _get_period(self, cr, uid, date=False, context=None):
        """
        Used in statement line's _defaults, so it is always triggered
        on installation or module upgrade even if there are no records
        without a value. For that reason, we need
        to be tolerant and allow for the situation in which no period
        exists for the current date (i.e. when no date is specified).

        Cannot be used directly as a defaults method due to lp:1296229
        """
        local_ctx = dict(context or {}, account_period_prefer_normal=True)
        try:
            return self.pool.get('account.period').find(
                cr, uid, dt=date, context=local_ctx)[0]
        except except_osv:
            if date:
                raise
        return False

    def _prepare_move(
            self, cr, uid, st_line, st_line_number, context=None):
        """
        Add the statement line's period to the move, overwriting
        the period on the statement
        """
        res = super(account_bank_statement, self)._prepare_move(
            cr, uid, st_line, st_line_number, context=context)
        if context and context.get('period_id'):
            res['period_id'] = context['period_id']
        return res

    def _prepare_move_line_vals(
            self, cr, uid, st_line, move_id, debit, credit, currency_id=False,
            amount_currency=False, account_id=False, analytic_id=False,
            partner_id=False, context=None):
        """
        Add the statement line's period to the move lines, overwriting
        the period on the statement
        """
        res = super(account_bank_statement, self)._prepare_move_line_vals(
            cr, uid, st_line, move_id, debit, credit, currency_id=currency_id,
            amount_currency=amount_currency, account_id=account_id,
            analytic_id=analytic_id, partner_id=partner_id, context=context)
        if context and context.get('period_id'):
            res['period_id'] = context['period_id']
        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 = {}
        account_move_obj = self.pool.get('account.move')
        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)

        # Take period from statement line and write to context
        # this will be picked up by the _prepare_move* methods
        period_id = self._get_period(
            cr, uid, date=st_line.date, context=context)
        localctx = context.copy()
        localctx['period_id'] = period_id

        # Write date & period on the voucher, delegate to account_voucher's
        # override of this method. Then post the related move and return.
        if st_line.voucher_id:
            voucher_pool = self.pool.get('account.voucher')
            voucher_pool.write(
                cr, uid, [st_line.voucher_id.id], {
                    'date': st_line.date,
                    'period_id': period_id,
                }, context=context)

        res = super(account_bank_statement, self).create_move_from_st_line(
            cr, uid, st_line_id, company_currency_id, st_line_number,
            context=localctx)

        st_line.refresh()
        if st_line.voucher_id:
            if not st_line.voucher_id.journal_id.entry_posted:
                account_move_obj.post(
                    cr, uid, [st_line.voucher_id.move_id.id], context={})
        else:
            # Write stored reconcile_id and pay invoices through workflow 
            if st_line.reconcile_id:
                move_ids = [move.id for move in st_line.move_ids]
                torec = account_move_line_obj.search(
                    cr, uid, [
                        ('move_id', 'in', move_ids),
                        ('account_id', '=', st_line.account_id.id)],
                    context=context)
                account_move_line_obj.write(cr, uid, torec, {
                        (st_line.reconcile_id.line_partial_ids and 
                         'reconcile_partial_id' or 'reconcile_id'): 
                        st_line.reconcile_id.id }, context=context)
                for move_line in (st_line.reconcile_id.line_id or []) + (
                    st_line.reconcile_id.line_partial_ids or []):
                    netsvc.LocalService("workflow").trg_trigger(
                        uid, 'account.move.line', move_line.id, cr)
        return res

    def button_confirm_bank(self, cr, uid, ids, context=None):
        """
        Assign journal sequence to statements without a name
        """
        if context is None:
            context = {}
        obj_seq = self.pool.get('ir.sequence')
        if ids and isinstance(ids, (int, long)):
            ids = [ids]
        noname_ids = self.search(
            cr, uid, [('id', 'in', ids),('name', '=', '/')],
            context=context)
        for st in self.browse(cr, uid, noname_ids, context=context):
            if st.journal_id.sequence_id:
                period_id = self._get_period(
                    cr, uid, date=st.date, context=context)
                year = self.pool.get('account.period').browse(
                    cr, uid, period_id, context=context).fiscalyear_id.id
                c = {'fiscalyear_id': year}
                st_number = obj_seq.get_id(
                    cr, uid, st.journal_id.sequence_id.id, context=c)
                self.write(
                    cr, uid, ids, {'name': st_number}, context=context)
        
        return super(account_bank_statement, self).button_confirm_bank(
            cr, uid, ids, context)


class account_voucher(orm.Model):
    _inherit = 'account.voucher'

    def _get_period(self, cr, uid, context=None):
        if context is None:
            context = {}
        if not context.get('period_id') and context.get('move_line_ids'):
            move_line = self.pool.get('account.move.line').browse(
                cr, uid , context.get('move_line_ids')[0], context=context)
            return move_line.period_id.id
        return super(account_voucher, self)._get_period(cr, uid, context)


class account_bank_statement_line(orm.Model):
    '''
    Extension on basic class:
        1. Extra links to account.period and res.partner.bank for tracing and
           matching.
        2. Extra 'trans' field to carry the transaction id of the bank.
        3. Readonly states for most fields except when in draft.
    '''
    _inherit = 'account.bank.statement.line'
    _description = 'Bank Transaction'

    def _get_period(self, cr, uid, date=False, context=None):
        return self.pool['account.bank.statement']._get_period(
            cr, uid, date=date, context=context)

    def _get_period_context(self, cr, uid, context=None):
        """
        Workaround for lp:1296229, context is passed positionally
        """
        return self._get_period(cr, uid, context=context)

    def _get_currency(self, cr, uid, context=None):
        '''
        Get the default currency (required to allow other modules to function,
        which assume currency to be a calculated field and thus optional)
        Remark: this is only a fallback as the real default is in the journal,
        which is inaccessible from within this method.
        '''
        res_users_obj = self.pool.get('res.users')
        return res_users_obj.browse(cr, uid, uid,
                context=context).company_id.currency_id.id

    def _get_invoice_id(self, cr, uid, ids, name, args, context=None):
        res = {}
        for st_line in self.browse(cr, uid, ids, context):
            res[st_line.id] = False
            for move_line in (
                    st_line.reconcile_id and
                    (st_line.reconcile_id.line_id or
                     st_line.reconcile_id.line_partial_ids) or
                    st_line.import_transaction_id and 
                    st_line.import_transaction_id.move_line_id and
                    [st_line.import_transaction_id.move_line_id] or []):
                if move_line.invoice:
                    res[st_line.id] = move_line.invoice.id
                    continue
        return res

    _columns = {
        # Redefines. Todo: refactor away to view attrs
        'amount': fields.float('Amount', readonly=True,
                            digits_compute=dp.get_precision('Account'),
                            states={'draft': [('readonly', False)]}),
        'ref': fields.char('Ref.', size=32, readonly=True,
                            states={'draft': [('readonly', False)]}),
        'name': fields.char('Name', size=64, required=False, readonly=True,
                            states={'draft': [('readonly', False)]}),
        'date': fields.date('Date', required=True, readonly=True,
                            states={'draft': [('readonly', False)]}),

        # New columns
        'trans': fields.char('Bank Transaction ID', size=15, required=False,
                            readonly=True,
                            states={'draft':[('readonly', False)]},
                            ),
        'partner_bank_id': fields.many2one('res.partner.bank', 'Bank Account',
                            required=False, readonly=True,
                            states={'draft':[('readonly', False)]},
                            ),
        'period_id': fields.many2one('account.period', 'Period', required=True,
                            states={'confirmed': [('readonly', True)]}),
        'currency': fields.many2one('res.currency', 'Currency', required=True,
                            states={'confirmed': [('readonly', True)]}),
        'reconcile_id': fields.many2one(
            'account.move.reconcile', 'Reconciliation', readonly=True
            ),
        'invoice_id': fields.function(
            _get_invoice_id, method=True, string='Linked Invoice',
            type='many2one', relation='account.invoice'
            ),
    }

    _defaults = {
        'period_id': _get_period_context,
        'currency': _get_currency,
    }


class invoice(orm.Model):
    '''
    Create other reference types as well.

    Descendant classes can extend this function to add more reference
    types, ie.

    def _get_reference_type(self, cr, uid, context=None):
        return super(my_class, self)._get_reference_type(cr, uid,
            context=context) + [('my_ref', _('My reference')]

    Don't forget to redefine the column "reference_type" as below or
    your method will never be triggered.

    TODO: move 'structured' part to account_banking_payment module
    where it belongs
    '''
    _inherit = 'account.invoice'

    def test_undo_paid(self, cr, uid, ids, context=None):
        """ 
        Called from the workflow. Used to unset paid state on
        invoices that were paid with bank transfers which are being cancelled 
        """
        for invoice in self.read(cr, uid, ids, ['reconciled'], context):
            if invoice['reconciled']:
                return False
        return True

    def _get_reference_type(self, cr, uid, context=None):
        '''
        Return the list of reference types
        '''
        return [('none', _('Free Reference')),
                ('structured', _('Structured Reference')),
               ]

    _columns = {
        'reference_type': fields.selection(_get_reference_type,
                                           'Reference Type', required=True
                                          )
    }

invoice()


class account_move_line(orm.Model):
    _inherit = "account.move.line"

    def get_balance(self, cr, uid, ids, context=None):
        """ 
        Return the balance of any set of move lines.

        Not to be confused with the 'balance' field on this model, which
        returns the account balance that the move line applies to.
        """
        total = 0.0
        if not ids:
            return total
        for line in self.read(
            cr, uid, ids, ['debit', 'credit'], context=context):
            total += (line['debit'] or 0.0) - (line['credit'] or 0.0)
        return total
account_move_line()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: