1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
24 from osv import fields, osv
28 from mx.DateTime import RelativeDateTime
29 from tools import config
31 class ir_sequence_type(osv.osv):
32 _inherit = "ir.sequence.type"
34 'name': fields.char('Sequence Name',size=128, required=True),
35 'code': fields.char('Sequence Code',size=128, required=True),
39 class account_voucher(osv.osv):
40 def _get_period(self, cr, uid, context):
41 periods = self.pool.get('account.period').find(cr, uid)
47 def _get_type(self, cr, uid, context={}):
48 type = context.get('type', 'rec_voucher')
51 def _get_reference_type(self, cursor, user, context=None):
52 return [('none', 'Free Reference')]
54 def _get_journal(self, cr, uid, context):
55 type_inv = 'rec_voucher'
57 if type(context) == type(''):
59 elif type(context) == type({}):
60 type_inv = context.get('type', 'rec_voucher')
63 'rec_voucher': 'cash',
64 'bank_rec_voucher': 'cash',
65 'pay_voucher': 'cash',
66 'bank_pay_voucher': 'cash',
67 'cont_voucher': 'cash',
68 'journal_sale_vou': 'sale',
69 'journal_pur_voucher': 'purchase',
70 'journal_voucher':'expanse'
73 journal_obj = self.pool.get('account.journal')
74 ttype = type2journal.get(type_inv, 'cash')
75 res = journal_obj.search(cr, uid, [('type', '=', ttype)], limit=1)
82 def _get_currency(self, cr, uid, context):
83 user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, [uid])[0]
85 return user.company_id.currency_id.id
87 return pooler.get_pool(cr.dbname).get('res.currency').search(cr, uid, [('rate','=',1.0)])[0]
89 _name = 'account.voucher'
90 _description = 'Accounting Voucher'
93 'name':fields.char('Name', size=256, required=True, readonly=True, states={'draft':[('readonly',False)]}),
94 'type': fields.selection([
95 ('pay_voucher','Cash Payment Voucher'),
96 ('bank_pay_voucher','Bank Payment Voucher'),
97 ('rec_voucher','Cash Receipt Voucher'),
98 ('bank_rec_voucher','Bank Receipt Voucher'),
99 ('cont_voucher','Contra Voucher'),
100 ('journal_sale_vou','Journal Sale Voucher'),
101 ('journal_pur_voucher','Journal Purchase Voucher'),
102 ('journal_voucher','Journal Voucher'),
103 ],'Type', readonly=True, select=True , size=128),
104 'date':fields.date('Date', readonly=True, states={'draft':[('readonly',False)]}),
105 'journal_id':fields.many2one('account.journal', 'Journal', required=True, readonly=True, states={'draft':[('readonly',False)]}),
106 'account_id':fields.many2one('account.account', 'Account', required=True, readonly=True, states={'draft':[('readonly',False)]}),
107 'payment_ids':fields.one2many('account.voucher.line','voucher_id','Voucher Lines', readonly=False, states={'proforma':[('readonly',True)]}),
108 'period_id': fields.many2one('account.period', 'Period', required=True, states={'posted':[('readonly',True)]}),
109 'narration':fields.text('Narration', readonly=True, states={'draft':[('readonly',False)]}, required=True),
110 'currency_id': fields.many2one('res.currency', 'Currency', required=True, readonly=True, states={'draft':[('readonly',False)]}),
111 'company_id': fields.many2one('res.company', 'Company', required=True),
112 'state':fields.selection(
114 ('proforma','Pro-forma'),
119 help=' * The \'Draft\' state is used when a user is encoding a new and unconfirmed Voucher. \
120 \n* The \'Pro-forma\' when voucher is in Pro-forma state,voucher does not have an voucher number. \
121 \n* The \'Posted\' state is used when user create voucher,a voucher number is generated and voucher entries are created in account \
122 \n* The \'Cancelled\' state is used when user cancel voucher.'),
123 'amount':fields.float('Amount', readonly=True),
124 'number':fields.char('Number', size=32, readonly=True),
125 'reference': fields.char('Voucher Reference', size=64),
126 'reference_type': fields.selection(_get_reference_type, 'Reference Type',
128 'move_id':fields.many2one('account.move', 'Account Entry'),
129 'move_ids':fields.many2many('account.move.line', 'voucher_id', 'account_id', 'rel_account_move', 'Real Entry'),
130 'partner_id':fields.many2one('res.partner', 'Partner', readonly=True, states={'draft':[('readonly',False)]})
134 'state': lambda *a: 'draft',
135 'date' : lambda *a: time.strftime('%Y-%m-%d'),
136 'period_id': _get_period,
138 'reference_type': lambda *a: 'none',
139 'journal_id':_get_journal,
140 'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'account.voucher', context=c),
141 'currency_id': _get_currency,
144 def _get_analityc_lines(self, cr, uid, id):
145 inv = self.browse(cr, uid, [id])[0]
146 cur_obj = self.pool.get('res.currency')
148 def onchange_account(self, cr, uid, ids, account_id):
150 return {'value':{'amount':False}}
151 account = self.pool.get('account.account').browse(cr,uid,account_id)
152 balance=account.balance
153 return {'value':{'amount':balance}}
155 def onchange_journal(self, cr, uid, ids, journal_id, type):
157 return {'value':{'account_id':False}}
158 journal = self.pool.get('account.journal')
159 if journal_id and (type in ('rec_voucher','bank_rec_voucher','journal_pur_voucher','journal_voucher')):
160 account_id = journal.browse(cr, uid, journal_id).default_debit_account_id
161 return {'value':{'account_id':account_id.id}}
162 elif journal_id and (type in ('pay_voucher','bank_pay_voucher','journal_sale_vou')) :
163 account_id = journal.browse(cr, uid, journal_id).default_credit_account_id
164 return {'value':{'account_id':account_id.id}}
166 account_id = journal.browse(cr, uid, journal_id).default_credit_account_id
167 return {'value':{'account_id':account_id.id}}
169 def open_voucher(self, cr, uid, ids, context={}):
170 obj=self.pool.get('account.voucher').browse(cr,uid,ids)
172 for i in obj[0].payment_ids:
175 self.write(cr,uid,ids,{'amount':total})
176 self.write(cr, uid, ids, {'state':'proforma'})
178 raise osv.except_osv('Invalid action !', 'You can not post to Pro-Forma a voucher with Total amount = 0')
181 def proforma_voucher(self, cr, uid, ids, context={}):
182 self.action_number(cr, uid, ids)
183 self.action_move_line_create(cr, uid, ids)
184 self.write(cr, uid, ids, {'state':'posted'})
187 def cancel_voucher(self,cr,uid,ids,context={}):
188 self.action_cancel(cr, uid, ids)
189 self.write(cr, uid, ids, {'state':'cancel'})
192 def action_cancel_draft(self, cr, uid, ids, *args):
193 self.write(cr, uid, ids, {'state':'draft'})
196 def unlink(self, cr, uid, ids, context={}):
197 vouchers = self.read(cr, uid, ids, ['state'])
200 if t['state'] in ('draft', 'cancel'):
201 unlink_ids.append(t['id'])
203 raise osv.except_osv('Invalid action !', 'Cannot delete Voucher(s) which are already opened or paid !')
204 osv.osv.unlink(self, cr, uid, unlink_ids)
207 def _get_analytic_lines(self, cr, uid, id):
208 inv = self.browse(cr, uid, [id])[0]
209 cur_obj = self.pool.get('res.currency')
211 company_currency = inv.company_id.currency_id.id
212 if inv.type in ('rec_voucher'):
217 iml = self.pool.get('account.voucher.line').move_line_get(cr, uid, inv.id)
220 if il['account_analytic_id']:
221 if inv.type in ('pay_voucher', 'rec_voucher','cont_voucher','bank_pay_voucher','bank_rec_voucher','journal_sale_vou','journal_pur_voucher'):
224 ref = self._convert_ref(cr, uid, inv.number)
226 il['analytic_lines'] = [(0, 0, {
229 'account_id': il['account_analytic_id'],
230 'amount': inv['amount'] * sign,
231 'general_account_id': il['account_id'] or False,
232 'journal_id': self.pool.get('account.voucher').browse(cr, uid, id).journal_id.analytic_journal_id.id or False,
237 def action_move_line_create(self, cr, uid, ids, *args):
238 for inv in self.browse(cr, uid, ids):
241 company_currency = inv.company_id.currency_id.id
243 line_ids = self.read(cr, uid, [inv.id], ['payment_ids'])[0]['payment_ids']
244 ils = self.pool.get('account.voucher.line').read(cr, uid, line_ids)
246 iml = self._get_analytic_lines(cr, uid, inv.id)
248 diff_currency_p = inv.currency_id.id <> company_currency
251 if inv.type in ('pay_voucher', 'journal_voucher', 'rec_voucher','cont_voucher','bank_pay_voucher','bank_rec_voucher','journal_sale_vou','journal_pur_voucher'):
254 ref = self._convert_ref(cr, uid, inv.number)
261 partner_id=i['partner_id']
262 acc_id = i['account_id']
263 if inv.currency_id.id != company_currency:
264 i['currency_id'] = inv.currency_id.id
265 i['amount_currency'] = i['amount']
267 i['amount_currency'] = False
268 i['currency_id'] = False
269 if inv.type in ('rec_voucher','bank_rec_voucher','journal_pur_voucher','journal_voucher'):
271 total_currency += i['amount_currency'] or i['amount']
272 i['amount'] = - i['amount']
275 total_currency -= i['amount_currency'] or i['amount']
277 name = inv['name'] or '/'
283 'amount': total or False,
284 'account_id': acc_id,
285 'amount_currency': diff_currency_p \
286 and total_currency or False,
287 'currency_id': diff_currency_p \
288 and inv.currency_id.id or False,
290 'partner_id':partner_id or False,
296 line = map(lambda x:(0,0,self.line_get_convert(cr, uid, x,date, context={})) ,iml)
297 an_journal_id=inv.journal_id.analytic_journal_id.id
298 journal_id = inv.journal_id.id
300 journal = self.pool.get('account.journal').browse(cr, uid, journal_id)
301 if journal.sequence_id:
302 name = self.pool.get('ir.sequence').get_id(cr, uid, journal.sequence_id.id)
306 'journal_id': journal_id,
307 'voucher_type':inv.type,
308 'narration' : inv.narration
311 move['period_id'] = inv.period_id.id
313 i[2]['period_id'] = inv.period_id.id
314 move_id = self.pool.get('account.move').create(cr, uid, move)
318 #create the first line our self
323 'account_id': inv.account_id.id or False,
325 'journal_id':journal_id ,
326 'period_id':inv.period_id.id,
331 if inv.type in ('rec_voucher', 'bank_rec_voucher', 'journal_pur_voucher', 'journal_voucher'):
332 move_line['debit'] = inv.amount
334 move_line['credit'] = inv.amount * (-1)
335 self.pool.get('account.move.line').create(cr, uid, move_line)
337 for line in inv.payment_ids:
343 'account_id':line.account_id.id or False,
345 'journal_id':journal_id ,
346 'period_id':inv.period_id.id,
347 'partner_id':line.partner_id.id or False,
352 if line.type == 'dr':
353 move_line['debit'] = line.amount or False
355 elif line.type == 'cr':
356 move_line['credit'] = line.amount or False
357 amount=line.amount * (-1)
359 ml_id=self.pool.get('account.move.line').create(cr, uid, move_line)
362 line.name=inv.narration
366 if line.account_analytic_id:
371 'account_id':line.account_analytic_id.id or False,
373 'journal_id':an_journal_id ,
374 'general_account_id':line.account_id.id,
377 self.pool.get('account.analytic.line').create(cr,uid,an_line)
379 self.write(cr, uid, [inv.id], {'move_id': move_id})
380 obj=self.pool.get('account.move').browse(cr, uid, move_id)
382 for line in obj.line_id :
383 cr.execute('insert into voucher_id (account_id,rel_account_move) values (%d, %d)',(int(ids[0]),int(line.id)))
388 def line_get_convert(self, cr, uid, x, date, context={}):
392 'date_maturity': x.get('date_maturity', False),
393 'partner_id':x.get('partner_id',False),
394 'name':x['name'][:64],
395 'debit':x['amount']>0 and x['amount'],
396 'credit':x['amount']<0 and -x['amount'],
397 'account_id':x['account_id'],
398 'analytic_lines':x.get('analytic_lines', []),
399 'amount_currency':x.get('amount_currency', False),
400 'currency_id':x.get('currency_id', False),
401 'tax_code_id': x.get('tax_code_id', False),
402 'tax_amount': x.get('tax_amount', False),
403 'ref':x.get('ref',False)
405 def _convert_ref(self, cr, uid, ref):
406 return (ref or '').replace('/','')
408 def action_number(self, cr, uid, ids, *args):
409 cr.execute('SELECT id, type, number, move_id, reference ' \
410 'FROM account_voucher ' \
411 'WHERE id =ANY(%s)',(ids,))
412 for (id, invtype, number, move_id, reference) in cr.fetchall():
414 number = self.pool.get('ir.sequence').get(cr, uid, invtype)
416 if type in ('pay_voucher', 'journal_voucher', 'rec_voucher','cont_voucher','bank_pay_voucher','bank_rec_voucher','journal_sale_vou','journal_pur_voucher'):
419 ref = self._convert_ref(cr, uid, number)
421 cr.execute('UPDATE account_voucher SET number=%s ' \
422 'WHERE id=%s', (number, id))
423 cr.execute('UPDATE account_move_line SET ref=%s ' \
424 'WHERE move_id=%s AND (ref is null OR ref = \'\')',
426 cr.execute('UPDATE account_analytic_line SET ref=%s ' \
427 'FROM account_move_line ' \
428 'WHERE account_move_line.move_id = %s ' \
429 'AND account_analytic_line.move_id = account_move_line.id',
433 def name_get(self, cr, uid, ids, context={}):
437 'pay_voucher': 'CPV: ',
438 'rec_voucher': 'CRV: ',
439 'cont_voucher': 'CV: ',
440 'bank_pay_voucher': 'BPV: ',
441 'bank_rec_voucher': 'BRV: ',
442 'journal_sale_vou': 'JSV: ',
443 'journal_pur_voucher': 'JPV: ',
444 'journal_voucher':'JV'
446 return [(r['id'], types[r['type']]+(r['number'] or '')+' '+(r['name'] or '')) for r in self.read(cr, uid, ids, ['type', 'number', 'name'], context, load='_classic_write')]
448 def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
455 ids = self.search(cr, user, [('number','=',name)]+ args, limit=limit, context=context)
457 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
458 return self.name_get(cr, user, ids, context)
460 def copy(self, cr, uid, id, default=None, context=None):
463 default = default.copy()
464 default.update({'state':'draft', 'number':False, 'move_id':False, 'move_ids':False, 'payment_ids':False})
465 if 'date' not in default:
466 default['date'] = time.strftime('%Y-%m-%d')
467 return super(account_voucher, self).copy(cr, uid, id, default, context)
469 def action_cancel(self, cr, uid, ids, *args):
470 account_move_obj = self.pool.get('account.move')
471 voucher = self.read(cr, uid, ids, ['move_id'])
474 account_move_obj.button_cancel(cr, uid, [i['move_id'][0]])
475 # delete the move this invoice was pointing to
476 # Note that the corresponding move_lines and move_reconciles
477 # will be automatically deleted too
478 account_move_obj.unlink(cr, uid, [i['move_id'][0]])
479 self.write(cr, uid, ids, {'state':'cancel', 'move_id':False})
484 class account_voucher_line(osv.osv):
485 _name = 'account.voucher.line'
486 _description = 'Voucher Line'
488 'voucher_id':fields.many2one('account.voucher', 'Voucher'),
489 'name':fields.char('Description', size=256, required=True),
490 'account_id':fields.many2one('account.account','Account', required=True),
491 'partner_id': fields.many2one('res.partner', 'Partner', change_default=True),
492 'amount':fields.float('Amount'),
493 'type':fields.selection([('dr','Debit'),('cr','Credit')], 'Type'),
494 'ref':fields.char('Reference', size=32),
495 'account_analytic_id': fields.many2one('account.analytic.account', 'Analytic Account')
498 'type': lambda *a: 'cr'
501 def move_line_get(self, cr, uid, voucher_id, context={}):
504 cur_obj = self.pool.get('res.currency')
505 inv = self.pool.get('account.voucher').browse(cr, uid, voucher_id)
506 company_currency = inv.company_id.currency_id.id
507 cur = inv.currency_id
509 for line in inv.payment_ids:
510 res.append(self.move_line_get_item(cr, uid, line, context))
513 def onchange_partner(self, cr, uid, ids, partner_id, ttype ,type1):
515 return {'value' : {'account_id' : False, 'type' : False ,'amount':False}}
516 obj = self.pool.get('res.partner')
518 if type1 in ('rec_voucher','bank_rec_voucher', 'journal_voucher'):
519 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
520 balance = obj.browse(cr,uid,partner_id).credit
522 elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
523 account_id = obj.browse(cr, uid, partner_id).property_account_payable
524 balance = obj.browse(cr,uid,partner_id).debit
526 elif type1 in ('journal_sale_vou') :
527 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
528 balance = obj.browse(cr,uid,partner_id).credit
530 elif type1 in ('journal_pur_voucher') :
531 account_id = obj.browse(cr, uid, partner_id).property_account_payable
532 balance = obj.browse(cr,uid,partner_id).debit
536 'value' : {'account_id' : account_id.id, 'type' : ttype, 'amount':balance}
539 def onchange_amount(self, cr, uid, ids,partner_id,amount, type,type1):
541 return {'value' : {}}
544 obj = self.pool.get('res.partner')
545 if type1 in ('rec_voucher', 'bank_rec_voucher', 'journal_voucher'):
547 account_id = obj.browse(cr, uid, partner_id).property_account_payable
550 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
553 elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
555 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
558 account_id = obj.browse(cr, uid, partner_id).property_account_payable
561 elif type1 in ('journal_sale_vou') :
563 account_id = obj.browse(cr, uid, partner_id).property_account_payable
566 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
569 elif type1 in ('journal_pur_voucher') :
571 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
574 account_id = obj.browse(cr, uid, partner_id).property_account_payable
577 if type1 in ('rec_voucher', 'bank_rec_voucher', 'journal_voucher'):
585 elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
593 elif type1 in ('journal_sale_vou') :
601 elif type1 in ('journal_pur_voucher') :
610 'value' : { 'type' : type , 'amount':amount}
613 def onchange_type(self, cr, uid, ids,partner_id,amount,type,type1):
616 obj = self.pool.get('res.partner')
618 if type1 in ('rec_voucher','bank_rec_voucher', 'journal_voucher'):
620 account_id = obj.browse(cr, uid, partner_id).property_account_payable
623 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
626 elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
628 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
631 account_id = obj.browse(cr, uid, partner_id).property_account_payable
634 elif type1 in ('journal_sale_vou') :
636 account_id = obj.browse(cr, uid, partner_id).property_account_payable
639 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
642 elif type1 in ('journal_pur_voucher') :
644 account_id = obj.browse(cr, uid, partner_id).property_account_receivable
647 account_id = obj.browse(cr, uid, partner_id).property_account_payable
650 if type1 in ('rec_voucher','bank_rec_voucher', 'journal_voucher'):
658 elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
666 elif type1 in ('journal_sale_vou') :
674 elif type1 in ('journal_pur_voucher') :
683 'value' : {'type' : type , 'amount':total}
686 def move_line_get_item(self, cr, uid, line, context={}):
689 'name': line.name[:64],
690 'amount':line.amount,
691 'account_id':line.account_id.id,
692 'partner_id':line.partner_id.id or False ,
693 'account_analytic_id':line.account_analytic_id.id or False,
697 account_voucher_line()