Launchpad automatic translations update.
[odoo/odoo.git] / addons / account_coda / account_coda.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    
6 #    Copyright (c) 2011 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 decimal_precision as dp
25 import netsvc
26 from tools.translate import _
27 logger=netsvc.Logger()
28
29 class coda_bank_account(osv.osv):
30     _name= 'coda.bank.account'
31     _description= 'CODA Bank Account Configuration'
32
33     def _check_currency(self, cr, uid, ids, context=None):
34         obj_cba = self.browse(cr, uid, ids[0], context=context)
35         if (obj_cba.state == 'normal') and obj_cba.journal and (obj_cba.currency != obj_cba.journal.currency):
36             return False
37         return True
38
39     _columns = {
40         'name': fields.char('Name', size=64, required=True),
41         'bank_id': fields.many2one('res.partner.bank', 'Bank Account', required=True, 
42             help='Bank Account Number.\nThe CODA import function will find its CODA processing parameters on this number.'),
43         'description1': fields.char('Primary Account Description', size=35,
44             help='The Primary or Secondary Account Description should match the corresponding Account Description in the CODA file.'),
45         'description2': fields.char('Secondary Account Description', size=35,
46             help='The Primary or Secondary Account Description should match the corresponding Account Description in the CODA file.'),
47         'state': fields.selection([
48             ('normal', 'Normal'),
49             ('info', 'Info')], 
50             'Type', required=True, select=1,
51             help='No Bank Statements will be generated for CODA Bank Statements from Bank Accounts of type \'Info\'.'),
52         'journal': fields.many2one('account.journal', 'Journal', 
53             domain=[('type', '=', 'bank')], 
54             states={'normal':[('required',True)],'info':[('required',False)]},
55             help='Bank Journal for the Bank Statement'),
56         'currency': fields.many2one('res.currency', 'Currency', required=True,
57             help='The currency of the CODA Bank Statement'),  
58         'coda_st_naming': fields.char('Bank Statement Naming Policy', size=64,
59             help="Define the rules to create the name of the Bank Statements generated by the CODA processing." \
60                  "\nE.g. %(code)s%(y)s/%(paper)s"     
61                  "\n\nVariables:" \
62                  "\nBank Journal Code: %(code)s" \
63                  "\nCurrent Year with Century: %(year)s" \
64                  "\nCurrent Year without Century: %(y)s" \
65                  "\nCODA sequence number: %(coda)s" \
66                  "\nPaper Statement sequence number: %(paper)s"),
67         'def_payable': fields.many2one('account.account', 'Default Payable Account', domain=[('type', '=', 'payable')], required=True,
68             help= 'Set here the payable account that will be used, by default, if the partner is not found.'),
69         'def_receivable': fields.many2one('account.account', 'Default Receivable Account', domain=[('type', '=', 'receivable')], required=True,
70             help= 'Set here the receivable account that will be used, by default, if the partner is not found.',),
71         'awaiting_account': fields.many2one('account.account', 'Default Account for Unrecognized Movement', domain=[('type', '!=', 'view')], required=True,
72             help= 'Set here the default account that will be used if the partner cannot be unambiguously identified.'),
73         'transfer_account': fields.many2one('account.account', 'Default Internal Transfer Account', domain=[('code', 'like', '58%'), ('type', '!=', 'view')], required=True,
74             help= 'Set here the default account that will be used for internal transfer between own bank accounts (e.g. transfer between current and deposit bank accounts).'),
75         'find_bbacom': fields.boolean('Lookup Invoice', required=True, help='Partner lookup via the \'BBA\' Structured Communication field of the Invoice.'),
76         'find_partner': fields.boolean('Lookup Partner', required=True, help='Partner lookup via Bank Account Number.'),
77         'active': fields.boolean('Active', help='If the active field is set to False, it will allow you to hide the Bank Account without removing it.'),
78         'company_id': fields.many2one('res.company', 'Company', required=True),
79     }
80     _defaults = {
81         'currency': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
82         'state': 'normal',
83         'coda_st_naming': '%(code)s/%(y)s/%(coda)s',
84         'active': True,   
85         'find_bbacom': True,                         
86         'find_partner': True,                         
87         'company_id': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.id,
88     }
89     _sql_constraints = [
90         ('account_uniq_1', 'unique (bank_id, description1, currency)', 'The combination of Bank Account, Account Description and Currency must be unique !'),
91         ('account_uniq_2', 'unique (bank_id, description2, currency)', 'The combination of Bank Account, Account Description and Currency must be unique !'),
92     ]
93     _constraints = [
94         (_check_currency, '\n\nConfiguration Error! \nThe Bank Account Currency should match the Journal Currency !', ['currency', 'journal']),
95     ]
96     _order = 'name'
97
98     def name_get(self, cr, uid, ids, context=None):
99         res = []
100         if not len(ids):
101             return res
102         for id in self.browse(cr, uid, ids, context=context):
103             res.append((id.id, (id.bank_id.iban or id.bank_id.acc_number) + ' (' + id.currency.name + ')' + \
104                 (id.description1 and (' - ' + id.description1) or '')))
105         return res
106
107     def copy(self, cr, uid, id, default=None, context=None):
108         cba = self.browse(cr, uid, id, context=context)
109         if not default:
110             default = {}
111         default = default.copy()
112         default.update({'journal_id': None})     
113         default['description1'] = cba['description1'] or ''
114         default['description2'] = cba['description2'] or ''
115         default['name'] = (cba['name'] or '') + ' (copy)'
116         default['state'] = cba['state']        
117         return super(coda_bank_account, self).copy(cr, uid, id, default, context) 
118
119     def onchange_state(self, cr, uid, ids, state):
120         return state =='info' and {'value': {'journal': None}} or {}
121
122 coda_bank_account()
123
124 class account_coda(osv.osv):
125     _name = 'account.coda'
126     _description = 'Object to store CODA Data Files'
127     _order = 'coda_creation_date desc'
128     _columns = {
129         'name': fields.char('CODA Filename',size=128, readonly=True),
130         'coda_data': fields.binary('CODA File', readonly=True),
131         'statement_ids': fields.one2many('coda.bank.statement','coda_id','Generated CODA Bank Statements', readonly=True),
132         'note': fields.text('Import Log', readonly=True),
133         'coda_creation_date': fields.date('CODA Creation Date', readonly=True, select=True),
134         'date': fields.date('Import Date', readonly=True, select=True),
135         'user_id': fields.many2one('res.users','User', readonly=True, select=True),
136         'company_id': fields.many2one('res.company', 'Company', readonly=True)
137     }
138     _defaults = {
139         'date': fields.date.context_today,
140         'user_id': lambda self,cr,uid,context: uid,
141         'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'account.coda', context=c),
142     }        
143     _sql_constraints = [
144         ('coda_uniq', 'unique (name, coda_creation_date)', 'This CODA has already been imported !')
145     ]  
146
147     def unlink(self, cr, uid, ids, context=None):
148         if context is None:
149             context = {}
150         context.update({'coda_unlink': True})
151         coda_st_obj = self.pool.get('coda.bank.statement')
152         bank_st_obj = self.pool.get('account.bank.statement')
153         for coda in self.browse(cr, uid, ids, context=context):
154             for coda_statement in coda.statement_ids:                 
155                 if not context.get('coda_statement_unlink', False):
156                     if coda_st_obj.exists(cr, uid, coda_statement.id, context=context):
157                         coda_st_obj.unlink(cr, uid, [coda_statement.id], context=context)    
158                 if not context.get('bank_statement_unlink', False):
159                     if coda_st_obj.exists(cr, uid, coda_statement.id, context=context) and (coda_statement.type == 'normal') and bank_st_obj.exists(cr, uid, coda_statement.statement_id.id, context=context):
160                         bank_st_obj.unlink(cr, uid, [coda_statement.statement_id.id], context=context)                   
161         context.update({'coda_unlink': False})
162         return super(account_coda, self).unlink(cr, uid, ids, context=context)
163   
164 account_coda()
165
166 class account_coda_trans_type(osv.osv):  
167     _name = 'account.coda.trans.type'
168     _description = 'CODA transaction type'
169     _rec_name = 'type' 
170     _columns = {
171         'type': fields.char('Transaction Type', size=1, required=True),
172         'parent_id': fields.many2one('account.coda.trans.type', 'Parent'),
173         'description': fields.text('Description', translate=True),
174     }
175 account_coda_trans_type()
176
177 class account_coda_trans_code(osv.osv):  
178     _name = 'account.coda.trans.code'
179     _description = 'CODA transaction code'
180     _rec_name = 'code' 
181     _columns = {
182         'code': fields.char('Code', size=2, required=True, select=1),
183         'type': fields.selection([
184                 ('code', 'Transaction Code'),
185                 ('family', 'Transaction Family')], 
186                 'Type', required=True, select=1), 
187         'parent_id': fields.many2one('account.coda.trans.code', 'Family', select=1),
188         'description': fields.char('Description', size=128, translate=True, select=2),
189         'comment': fields.text('Comment', translate=True),
190     }
191 account_coda_trans_code()
192
193 class account_coda_trans_category(osv.osv):  
194     _name = 'account.coda.trans.category'
195     _description = 'CODA transaction category'
196     _rec_name = 'category' 
197     _columns = {
198         'category': fields.char('Transaction Category', size=3, required=True),
199         'description': fields.char('Description', size=256, translate=True),
200     }
201 account_coda_trans_category()
202
203 class account_coda_comm_type(osv.osv):  
204     _name = 'account.coda.comm.type'
205     _description = 'CODA structured communication type'
206     _rec_name = 'code' 
207     _columns = {
208         'code': fields.char('Structured Communication Type', size=3, required=True, select=1),
209         'description': fields.char('Description', size=128, translate=True),
210     }
211     _sql_constraints = [
212         ('code_uniq', 'unique (code)','The Structured Communication Code must be unique !')
213         ]
214 account_coda_comm_type()
215
216 class coda_bank_statement(osv.osv):
217     _name = 'coda.bank.statement' 
218     _description = 'CODA Bank Statement'    
219     
220     def _default_journal_id(self, cr, uid, context={}):
221         if context.get('journal_id', False):
222             return context['journal_id']
223         return False
224
225     def _end_balance(self, cursor, user, ids, name, attr, context=None):
226         res = {}
227         statements = self.browse(cursor, user, ids, context=context)
228         for statement in statements:
229             res[statement.id] = statement.balance_start
230             for line in statement.line_ids:
231                     res[statement.id] += line.amount
232         for r in res:
233             res[r] = round(res[r], 2)
234         return res
235
236     def _get_period(self, cr, uid, context={}):
237         periods = self.pool.get('account.period').find(cr, uid)
238         if periods:
239             return periods[0]
240         else:
241             return False
242
243     _order = 'date desc'
244     _columns = {
245         'name': fields.char('Name', size=64, required=True, readonly=True),
246         'date': fields.date('Date', required=True, readonly=True),
247         'coda_id': fields.many2one('account.coda', 'CODA Data File', ondelete='cascade'),
248         'type': fields.selection([
249             ('normal', 'Normal'),
250             ('info', 'Info')], 
251             'Type', required=True, readonly=True,
252             help='No Bank Statements are associated with CODA Bank Statements of type \'Info\'.'),
253         'statement_id': fields.many2one('account.bank.statement', 'Associated Bank Statement'),        
254         'journal_id': fields.many2one('account.journal', 'Journal', readonly=True, domain=[('type', '=', 'bank')]),
255         'coda_bank_account_id': fields.many2one('coda.bank.account', 'Bank Account', readonly=True),        
256         'period_id': fields.many2one('account.period', 'Period', required=True, readonly=True),
257         'balance_start': fields.float('Starting Balance', digits_compute=dp.get_precision('Account'), readonly=True),
258         'balance_end_real': fields.float('Ending Balance', digits_compute=dp.get_precision('Account'), readonly=True),
259         'balance_end': fields.function(_end_balance, method=True, store=True, string='Balance'),        
260         'line_ids': fields.one2many('coda.bank.statement.line',
261             'statement_id', 'CODA Bank Statement lines', readonly=True),
262         'currency': fields.many2one('res.currency', 'Currency', required=True, readonly=True,
263             help='The currency of the CODA Bank Statement'),
264         'company_id': fields.related('journal_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),
265     }
266     _defaults = {
267         'type': 'normal',        
268         'currency': lambda self,cr,uid,c: self.pool.get('res.users').browse(cr, uid, uid, c).company_id.currency_id.id,
269         'journal_id': _default_journal_id,
270         'period_id': _get_period,
271     }
272
273     def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
274         if context is None: 
275             context = {}
276         res = super(coda_bank_statement, self).search(cr, uid, args=args, offset=offset, limit=limit, order=order,
277                 context=context, count=count)
278         if context.get('bank_statement', False) and not res:
279             raise osv.except_osv('Warning', _('No CODA Bank Statement found for this Bank Statement!'))
280         return res
281
282     def unlink(self, cr, uid, ids, context=None):
283         if context is None:
284             context = {}
285         context.update({'coda_statement_unlink': True})
286         coda_obj = self.pool.get('account.coda')
287         bank_st_obj = self.pool.get('account.bank.statement')
288         
289         # find all CODA bank statements that are associated with the selected CODA bank statements via a common CODA file
290         new_ids = []       
291         for coda_statement in self.browse(cr, uid, ids, context=context):
292             if coda_obj.exists(cr, uid, coda_statement.coda_id.id, context=context):
293                 new_ids += [x.id for x in coda_obj.browse(cr, uid, coda_statement.coda_id.id, context=context).statement_ids]
294
295         # unlink CODA banks statements as well as associated bank statements and CODA files               
296         for coda_statement in self.browse(cr, uid, new_ids, context=context):
297             if coda_statement.statement_id.state == 'confirm': 
298                 raise osv.except_osv(_('Invalid action !'),
299                     _("Cannot delete CODA Bank Statement '%s' of Journal '%s'." \
300                       "\nThe associated Bank Statement has already been confirmed !" \
301                       "\nPlease undo this action first!") \
302                       % (coda_statement.name, coda_statement.journal_id.name))
303             else:
304                 if not context.get('coda_unlink', False):
305                     if coda_statement.coda_id and coda_obj.exists(cr, uid, coda_statement.coda_id.id, context=context):
306                         coda_obj.unlink(cr, uid, [coda_statement.coda_id.id], context=context)
307                 if not context.get('bank_statement_unlink', False):
308                     if coda_statement.statement_id and bank_st_obj.exists(cr, uid, coda_statement.statement_id.id, context=context):
309                         bank_st_obj.unlink(cr, uid, [coda_statement.statement_id.id], context=context)    
310
311         context.update({'coda_statement_unlink': False})
312         return super(coda_bank_statement, self).unlink(cr, uid, new_ids, context=context)
313  
314 coda_bank_statement()
315
316 class account_bank_statement(osv.osv):
317     _inherit = 'account.bank.statement'
318     _columns = {
319         'coda_statement_id': fields.many2one('coda.bank.statement', 'Associated CODA Bank Statement'),
320     }
321     
322     def unlink(self, cr, uid, ids, context=None):
323         if context is None:
324             context = {}
325         context.update({'bank_statement_unlink': True})
326         coda_obj = self.pool.get('account.coda')
327         coda_st_obj = self.pool.get('coda.bank.statement')
328
329         # find all statements that are associated with the selected bank statements via a common CODA file
330         ids_plus = []       
331         for statement in self.browse(cr, uid, ids, context=context):
332             if statement.coda_statement_id:
333                 for x in coda_obj.browse(cr, uid, statement.coda_statement_id.coda_id.id, context=context).statement_ids:
334                     if x.type == 'normal':
335                         ids_plus += [x.statement_id.id]
336                 
337         # unlink banks statements as well as associated CODA bank statements and CODA files
338         for statement in self.browse(cr, uid, ids_plus, context=context):       
339             if not context.get('coda_statement_unlink', False):
340                 if statement.coda_statement_id and coda_st_obj.exists(cr, uid, statement.coda_statement_id.id, context=context):
341                     coda_st_obj.unlink(cr, uid, [statement.coda_statement_id.id], context=context)
342             if not context.get('coda_unlink', False):
343                 if statement.coda_statement_id \
344                     and coda_st_obj.exists(cr, uid, statement.coda_statement_id.id, context=context) \
345                     and statement.coda_statement_id.coda_id \
346                     and coda_obj.exists(cr, uid, statement.coda_statement_id.coda_id.id, context=context):
347                         coda_obj.unlink(cr, uid, [statement.coda_statement_id.coda_id.id], context=context)
348
349         context.update({'bank_statement_unlink': False})
350         new_ids = list(set(ids + ids_plus))
351         return super(account_bank_statement, self).unlink(cr, uid, new_ids, context=context)
352          
353 account_bank_statement()
354
355 class coda_bank_statement_line(osv.osv):
356     _name = 'coda.bank.statement.line'     
357     _order = 'sequence'   
358     _description = 'CODA Bank Statement Line'
359     _columns = {
360         'name': fields.char('Communication', size=268, required=True),
361         'sequence': fields.integer('Sequence'),
362         'date': fields.date('Entry Date', required=True),
363         'val_date': fields.date('Valuta Date'),        
364         'account_id': fields.many2one('account.account','Account'),     # remove required=True
365         'type': fields.selection([
366             ('supplier','Supplier'),
367             ('customer','Customer'),
368             ('general','General'),
369             ('globalisation','Globalisation'),            
370             ('information','Information'),    
371             ('communication','Free Communication'),          
372             ], 'Type', required=True),
373         'globalisation_level': fields.integer('Globalisation Level', 
374                 help="The value which is mentioned (1 to 9), specifies the hierarchy level"
375                      " of the globalisation of which this record is the first."
376                      "\nThe same code will be repeated at the end of the globalisation."),
377         'globalisation_amount': fields.float('Globalisation Amount', digits_compute=dp.get_precision('Account')),      
378         'globalisation_id': fields.many2one('account.bank.statement.line.global', 'Globalisation ID', readonly=True,
379             help="Code to identify transactions belonging to the same globalisation level within a batch payment"),                                                     
380         'amount': fields.float('Amount', digits_compute=dp.get_precision('Account')),
381         'partner_id': fields.many2one('res.partner', 'Partner'),
382         'counterparty_name': fields.char('Counterparty Name', size=35),
383         'counterparty_bic': fields.char('Counterparty BIC', size=11),                     
384         'counterparty_number': fields.char('Counterparty Number', size=34),   
385         'counterparty_currency': fields.char('Counterparty Currency', size=3), 
386         'statement_id': fields.many2one('coda.bank.statement', 'CODA Bank Statement',
387             select=True, required=True, ondelete='cascade'),
388         'coda_bank_account_id': fields.related('statement_id', 'coda_bank_account_id', type='many2one', relation='coda.bank.account', string='Bank Account', store=True, readonly=True),
389         'ref': fields.char('Reference', size=32),
390         'note': fields.text('Notes'),
391         'company_id': fields.related('statement_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True),        
392     }
393
394     def unlink(self, cr, uid, ids, context=None):
395         if context is None:
396             context = {}
397         if context.get('block_statement_line_delete', False):
398             raise osv.except_osv('Warning', _('Delete operation not allowed !'))
399         return super(account_bank_statement_line, self).unlink(cr, uid, ids, context=context)
400
401 coda_bank_statement_line()       
402
403 class account_bank_statement_line_global(osv.osv):
404     _inherit = 'account.bank.statement.line.global'
405     _columns = {
406         'coda_statement_line_ids': fields.one2many('coda.bank.statement.line', 'globalisation_id', 'CODA Bank Statement Lines', readonly=True),
407     }
408 account_bank_statement_line_global()
409
410 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: