~taktik/openobject-addons/hui-extra-6.1

« back to all changes in this revision

Viewing changes to account_cashflow_invoice/invoice.py

  • Committer: root
  • Date: 2013-01-25 09:57:08 UTC
  • Revision ID: root@oerp61-20130125095708-oh7e3hxduhionhyy
update noviat accounting modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    
 
6
#    Copyright (c) 2012 Noviat nv/sa (www.noviat.be). All rights reserved.
 
7
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from osv import osv, fields
 
24
import re
 
25
from tools.translate import _
 
26
import logging
 
27
_logger = logging.getLogger(__name__)
 
28
 
 
29
class account_invoice(osv.osv):
 
30
    _inherit = 'account.invoice'
 
31
    
 
32
    _columns = {
 
33
        'cashflow_code_id': fields.many2one('account.cashflow.code', 'Cash Flow Code', domain=[('type', '=', 'provision')],
 
34
            readonly=True, states={'draft':[('readonly',False)]},
 
35
            help="Specify Cash Flow Code in order to override the Cash Flow Code Assignment Rules."),
 
36
        'cashflow_journal_id': fields.many2one('account.journal', 'Payment Method', 
 
37
            context={'journal_type': False}, domain=[('type', 'in', ['bank','cash'])],            
 
38
            readonly=True, states={'draft':[('readonly',False)]},
 
39
            help="Specify the financial journal for the payment of this invoice or credit note."),
 
40
    }    
 
41
 
 
42
    def action_cfc_create(self, cr, uid, ids, *args):
 
43
        cfpline_obj = self.pool.get('account.cashflow.provision.line')
 
44
        for inv in self.browse(cr, uid, ids):
 
45
            if inv.type in ['out_invoice', 'in_refund']:
 
46
                if inv.currency_id != inv.company_id.currency_id:    # To DO : adapt for multi-currency support
 
47
                    continue
 
48
                cfpline_amount = inv.amount_total
 
49
                cfpline_journal_id = inv.cashflow_journal_id and inv.cashflow_journal_id.id
 
50
                cfpline_company_id = inv.journal_id.company_id.id
 
51
                cfpline_partner_id = inv.partner_id.id
 
52
                cfpline_note = _('Provision for') + ' ' + (inv.type == 'out_invoice' and _('Customer Invoice') or _('Supplier Refund')) + ' ' + inv.internal_number
 
53
                cfpline_account_id = inv.account_id.id
 
54
                cfpline_type = 'customer'
 
55
                cfpline_date = inv.date_due or inv.date_invoice    
 
56
                communication =  inv.reference or ''
 
57
                cfpline_cfc_id = False
 
58
                if inv.cashflow_code_id:
 
59
                    cfpline_cfc_id = inv.cashflow_code_id.id
 
60
                else:
 
61
                    # assign Cash Flow Code via rules engine
 
62
                    kwargs = {
 
63
                        'journal_id': cfpline_journal_id,
 
64
                        'company_id': cfpline_company_id,
 
65
                        'account_id': cfpline_account_id,
 
66
                        'partner_id': cfpline_partner_id,
 
67
                        'sign': cfpline_amount >= 0 and 'debit' or 'credit',
 
68
                    }
 
69
                    #if context.get('extra_fields', None):
 
70
                    #    kwargs['extra_fields'] = context['extra_fields']
 
71
                    cfc_id = self.pool.get('account.cashflow.rule').cfc_id_get(cr, uid, **kwargs)
 
72
                    if cfc_id:
 
73
                        cfpline_cfc_id = self.pool.get('account.cashflow.code').browse(cr, uid, cfc_id).twin_id.id
 
74
                if not cfpline_cfc_id:
 
75
                    continue
 
76
                
 
77
                cfpline_obj.create(cr, uid, {
 
78
                    'description': (inv.type == 'out_invoice' and _('Customer Invoice') or _('Customer Refund')) + ' ' + inv.internal_number,
 
79
                    'cashflow_code_id': cfpline_cfc_id,
 
80
                    'state': 'confirm',
 
81
                    'note': cfpline_note,
 
82
                    'origin': inv._name + ',' + str(inv.id),
 
83
                    'journal_id': cfpline_journal_id,
 
84
                    'val_date': cfpline_date,  
 
85
                    'amount': cfpline_amount,
 
86
                    'partner_id': cfpline_partner_id,
 
87
                    'name': communication,
 
88
                    'company_id': cfpline_company_id,
 
89
                    'type': cfpline_type,
 
90
                    'account_id': cfpline_account_id,              
 
91
                    })
 
92
 
 
93
        return True
 
94
 
 
95
    def action_cancel(self, cr, uid, ids, *args):
 
96
        super(account_invoice, self).action_cancel(cr, uid, ids, *args)
 
97
        cfpline_obj = self.pool.get('account.cashflow.provision.line')
 
98
        for inv in self.browse(cr, uid, ids):
 
99
            domain = [ ('origin', '=', inv._name + ',' + str(inv.id))]
 
100
            cfpline_ids = cfpline_obj.search(cr, uid, domain)
 
101
            if len(cfpline_ids) > 1:
 
102
                raise osv.except_osv(_('Warning'), _('Cash Flow Provision Line ambiguity, cf. lines %s') % cfpline_ids)
 
103
            elif len(cfpline_ids) == 1:
 
104
                cfpline_obj.write(cr, uid, [cfpline_ids[0]], {'state': 'draft'})            
 
105
                cfpline_obj.unlink(cr, uid, [cfpline_ids[0]])        
 
106
        return True
 
107
 
 
108
account_invoice()
 
 
b'\\ No newline at end of file'