[REF] purchase: search view of purchase order and form view of merge order wizard
[odoo/odoo.git] / addons / account_voucher / voucher.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 import netsvc
24 from osv import fields, osv
25 import ir
26 import pooler
27 import mx.DateTime
28 from mx.DateTime import RelativeDateTime
29 from tools import config
30
31 class ir_sequence_type(osv.osv):
32     _inherit = "ir.sequence.type"
33     _columns = {
34         'name': fields.char('Sequence Name',size=128, required=True),
35         'code': fields.char('Sequence Code',size=128, required=True),
36     }
37 ir_sequence_type()
38
39 class account_voucher(osv.osv):
40     def _get_period(self, cr, uid, context):
41         periods = self.pool.get('account.period').find(cr, uid)
42         if periods:
43             return periods[0]
44         else:
45             return False
46
47     def _get_type(self, cr, uid, context={}):
48         type = context.get('type', 'rec_voucher')
49         return type
50
51     def _get_reference_type(self, cursor, user, context=None):
52         return [('none', 'Free Reference')]
53
54     def _get_journal(self, cr, uid, context):
55         type_inv = 'rec_voucher'
56
57         if type(context) == type(''):
58             type_inv = context
59         elif type(context) == type({}):
60             type_inv = context.get('type', 'rec_voucher')
61
62         type2journal = {
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'
71         }
72
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)
76
77         if res:
78             return res[0]
79         else:
80             return False
81
82     def _get_currency(self, cr, uid, context):
83         user = pooler.get_pool(cr.dbname).get('res.users').browse(cr, uid, [uid])[0]
84         if user.company_id:
85             return user.company_id.currency_id.id
86         else:
87             return pooler.get_pool(cr.dbname).get('res.currency').search(cr, uid, [('rate','=',1.0)])[0]
88
89     _name = 'account.voucher'
90     _description = 'Accounting Voucher'
91     _order = "number"
92     _columns = {
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(
113                     [('draft','Draft'),
114                      ('proforma','Pro-forma'),
115                      ('posted','Posted'),
116                      ('cancel','Cancel')
117                     ], 'State',
118                     readonly=True,
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',
127             required=True),
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)]})
131     }
132
133     _defaults = {
134         'state': lambda *a: 'draft',
135         'date' : lambda *a: time.strftime('%Y-%m-%d'),
136         'period_id': _get_period,
137         'type': _get_type,
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,
142     }
143
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')
147
148     def onchange_account(self, cr, uid, ids, account_id):
149         if not 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}}
154
155     def onchange_journal(self, cr, uid, ids, journal_id, type):
156         if not journal_id:
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}}
165         else:
166             account_id = journal.browse(cr, uid, journal_id).default_credit_account_id
167             return {'value':{'account_id':account_id.id}}
168
169     def open_voucher(self, cr, uid, ids, context={}):
170         obj=self.pool.get('account.voucher').browse(cr,uid,ids)
171         total=0
172         for i in obj[0].payment_ids:
173             total+=i.amount
174         if total!=0:
175             self.write(cr,uid,ids,{'amount':total})
176             self.write(cr, uid, ids, {'state':'proforma'})
177         else:
178             raise osv.except_osv('Invalid action !', 'You can not post to Pro-Forma a voucher with Total amount = 0')
179         return True
180
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'})
185         return True
186
187     def cancel_voucher(self,cr,uid,ids,context={}):
188         self.action_cancel(cr, uid, ids)
189         self.write(cr, uid, ids, {'state':'cancel'})
190         return True
191
192     def action_cancel_draft(self, cr, uid, ids, *args):
193         self.write(cr, uid, ids, {'state':'draft'})
194         return True
195
196     def unlink(self, cr, uid, ids, context={}):
197         vouchers = self.read(cr, uid, ids, ['state'])
198         unlink_ids = []
199         for t in vouchers:
200             if t['state'] in ('draft', 'cancel'):
201                 unlink_ids.append(t['id'])
202             else:
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)
205         return True
206
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')
210
211         company_currency = inv.company_id.currency_id.id
212         if inv.type in ('rec_voucher'):
213             sign = 1
214         else:
215             sign = -1
216
217         iml = self.pool.get('account.voucher.line').move_line_get(cr, uid, inv.id)
218
219         for il in iml:
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'):
222                     ref = inv.reference
223                 else:
224                     ref = self._convert_ref(cr, uid, inv.number)
225
226                 il['analytic_lines'] = [(0, 0, {
227                     'name': il['name'],
228                     'date': inv['date'],
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,
233                     'ref': ref,
234                 })]
235         return iml
236
237     def action_move_line_create(self, cr, uid, ids, *args):
238         for inv in self.browse(cr, uid, ids):
239             if inv.move_id:
240                 continue
241             company_currency = inv.company_id.currency_id.id
242
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)
245
246             iml = self._get_analytic_lines(cr, uid, inv.id)
247
248             diff_currency_p = inv.currency_id.id <> company_currency
249
250             total = 0
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'):
252                 ref = inv.reference
253             else:
254                 ref = self._convert_ref(cr, uid, inv.number)
255
256             acc_id = None
257             date = inv.date
258             total_currency = 0
259             acc_id = None
260             for i in iml:
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']
266                 else:
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'):
270                     total += i['amount']
271                     total_currency += i['amount_currency'] or i['amount']
272                     i['amount'] = - i['amount']
273                 else:
274                     total -= i['amount']
275                     total_currency -= i['amount_currency'] or i['amount']
276
277             name = inv['name'] or '/'
278             totlines = False
279
280             iml.append({
281                 'type': 'dest',
282                 'name': name,
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,
289                 'ref': ref,
290                 'partner_id':partner_id or False,
291             })
292
293             date = inv.date
294             inv.amount=total
295
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
299
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)
303
304             move = {
305                 'name': name,
306                 'journal_id': journal_id,
307                 'voucher_type':inv.type,
308                 'narration' : inv.narration
309             }
310             if inv.period_id:
311                 move['period_id'] = inv.period_id.id
312                 for i in line:
313                     i[2]['period_id'] = inv.period_id.id
314             move_id = self.pool.get('account.move').create(cr, uid, move)
315             ref = move['name']
316             amount=0.0
317
318             #create the first line our self
319             move_line = {
320                 'name': inv.name,
321                 'debit': False,
322                 'credit':False,
323                 'account_id': inv.account_id.id or False,
324                 'move_id':move_id ,
325                 'journal_id':journal_id ,
326                 'period_id':inv.period_id.id,
327                 'partner_id': False,
328                 'ref': ref,
329                 'date': inv.date
330             }
331             if inv.type in ('rec_voucher', 'bank_rec_voucher', 'journal_pur_voucher', 'journal_voucher'):
332                 move_line['debit'] = inv.amount
333             else:
334                 move_line['credit'] = inv.amount * (-1)
335             self.pool.get('account.move.line').create(cr, uid, move_line)
336
337             for line in inv.payment_ids:
338
339                 move_line = {
340                     'name':line.name,
341                      'debit':False,
342                      'credit':False,
343                      'account_id':line.account_id.id or False,
344                      'move_id':move_id ,
345                      'journal_id':journal_id ,
346                      'period_id':inv.period_id.id,
347                      'partner_id':line.partner_id.id or False,
348                      'ref':ref,
349                      'date':inv.date
350                  }
351
352                 if line.type == 'dr':
353                     move_line['debit'] = line.amount or False
354                     amount=line.amount
355                 elif line.type == 'cr':
356                     move_line['credit'] = line.amount or False
357                     amount=line.amount * (-1)
358
359                 ml_id=self.pool.get('account.move.line').create(cr, uid, move_line)
360
361                 if inv.narration:
362                     line.name=inv.narration
363                 else:
364                     line.name=line.name
365
366                 if line.account_analytic_id:
367                     an_line = {
368                          'name':line.name,
369                          'date':inv.date,
370                          'amount':amount,
371                          'account_id':line.account_analytic_id.id or False,
372                          'move_id':ml_id,
373                          'journal_id':an_journal_id ,
374                          'general_account_id':line.account_id.id,
375                          'ref':ref
376                      }
377                     self.pool.get('account.analytic.line').create(cr,uid,an_line)
378
379             self.write(cr, uid, [inv.id], {'move_id': move_id})
380             obj=self.pool.get('account.move').browse(cr, uid, move_id)
381
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)))
384
385         return True
386
387
388     def line_get_convert(self, cr, uid, x, date, context={}):
389
390         return {
391             'date':date,
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)
404         }
405     def _convert_ref(self, cr, uid, ref):
406         return (ref or '').replace('/','')
407
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():
413             if not number:
414                 number = self.pool.get('ir.sequence').get(cr, uid, invtype)
415
416                 if type in ('pay_voucher', 'journal_voucher', 'rec_voucher','cont_voucher','bank_pay_voucher','bank_rec_voucher','journal_sale_vou','journal_pur_voucher'):
417                     ref = reference
418                 else:
419                     ref = self._convert_ref(cr, uid, number)
420
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 = \'\')',
425                         (ref, move_id))
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',
430                             (ref, move_id))
431         return True
432
433     def name_get(self, cr, uid, ids, context={}):
434         if not len(ids):
435             return []
436         types = {
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'
445         }
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')]
447
448     def name_search(self, cr, user, name, args=None, operator='ilike', context=None, limit=100):
449         if not args:
450             args=[]
451         if not context:
452             context={}
453         ids = []
454         if name:
455             ids = self.search(cr, user, [('number','=',name)]+ args, limit=limit, context=context)
456         if not ids:
457             ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
458         return self.name_get(cr, user, ids, context)
459
460     def copy(self, cr, uid, id, default=None, context=None):
461         if default is None:
462             default = {}
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)
468
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'])
472         for i in voucher:
473             if i['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})
480         return True
481
482 account_voucher()
483
484 class account_voucher_line(osv.osv):
485     _name = 'account.voucher.line'
486     _description = 'Voucher Line'
487     _columns = {
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')
496     }
497     _defaults = {
498         'type': lambda *a: 'cr'
499     }
500
501     def move_line_get(self, cr, uid, voucher_id, context={}):
502         res = []
503
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
508
509         for line in inv.payment_ids:
510             res.append(self.move_line_get_item(cr, uid, line, context))
511         return res
512
513     def onchange_partner(self, cr, uid, ids, partner_id, ttype ,type1):
514         if not partner_id:
515             return {'value' : {'account_id' : False, 'type' : False ,'amount':False}}
516         obj = self.pool.get('res.partner')
517         account_id = False
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
521             ttype = 'cr'
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
525             ttype = 'dr'
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
529             ttype = 'dr'
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
533             ttype = 'cr'
534
535         return {
536             'value' : {'account_id' : account_id.id, 'type' : ttype, 'amount':balance}
537         }
538
539     def onchange_amount(self, cr, uid, ids,partner_id,amount, type,type1):
540         if not amount:
541             return {'value' : {}}
542         if partner_id:
543
544             obj = self.pool.get('res.partner')
545             if type1 in ('rec_voucher', 'bank_rec_voucher', 'journal_voucher'):
546                 if amount < 0 :
547                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
548                     type = 'dr'
549                 else:
550                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
551                     type = 'cr'
552
553             elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
554                 if amount < 0 :
555                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
556                     type = 'cr'
557                 else:
558                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
559                     type = 'dr'
560
561             elif type1 in ('journal_sale_vou') :
562                 if amount < 0 :
563                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
564                     type = 'cr'
565                 else:
566                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
567                     type = 'dr'
568
569             elif type1 in ('journal_pur_voucher') :
570                 if amount< 0 :
571                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
572                     type = 'dr'
573                 else:
574                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
575                     type = 'cr'
576         else:
577             if type1 in ('rec_voucher', 'bank_rec_voucher', 'journal_voucher'):
578                 if amount < 0 :
579
580                     type = 'dr'
581                 else:
582
583                     type = 'cr'
584
585             elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
586                 if amount < 0 :
587
588                     type = 'cr'
589                 else:
590
591                     type = 'dr'
592
593             elif type1 in ('journal_sale_vou') :
594                 if amount < 0 :
595
596                     type = 'cr'
597                 else:
598
599                     type = 'dr'
600
601             elif type1 in ('journal_pur_voucher') :
602                 if amount< 0 :
603
604                     type = 'dr'
605                 else:
606
607                     type = 'cr'
608
609         return {
610             'value' : { 'type' : type , 'amount':amount}
611         }
612
613     def onchange_type(self, cr, uid, ids,partner_id,amount,type,type1):
614         if partner_id:
615
616             obj = self.pool.get('res.partner')
617
618             if type1 in ('rec_voucher','bank_rec_voucher', 'journal_voucher'):
619                 if type == 'dr' :
620                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
621                     total=amount*(-1)
622                 else:
623                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
624                     total=amount*(-1)
625
626             elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
627                 if type == 'cr' :
628                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
629                     total=amount*(-1)
630                 else:
631                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
632                     total=amount*(-1)
633
634             elif type1 in ('journal_sale_vou') :
635                 if type == 'cr' :
636                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
637                     total=amount*(-1)
638                 else:
639                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
640                     total=amount*(-1)
641
642             elif type1 in ('journal_pur_voucher') :
643                 if type == 'dr' :
644                     account_id = obj.browse(cr, uid, partner_id).property_account_receivable
645                     total=amount*(-1)
646                 else:
647                     account_id = obj.browse(cr, uid, partner_id).property_account_payable
648                     total=amount*(-1)
649         else:
650             if type1 in ('rec_voucher','bank_rec_voucher', 'journal_voucher'):
651                 if type == 'dr' :
652
653                     total=amount*(-1)
654                 else:
655
656                     total=amount*(-1)
657
658             elif type1 in ('pay_voucher','bank_pay_voucher','cont_voucher') :
659                 if type == 'cr' :
660
661                     total=amount*(-1)
662                 else:
663
664                     total=amount*(-1)
665
666             elif type1 in ('journal_sale_vou') :
667                 if type == 'cr' :
668
669                     total=amount*(-1)
670                 else:
671
672                     total=amount*(-1)
673
674             elif type1 in ('journal_pur_voucher') :
675                 if type == 'dr' :
676
677                     total=amount*(-1)
678                 else:
679
680                     total=amount*(-1)
681
682         return {
683             'value' : {'type' : type , 'amount':total}
684         }
685
686     def move_line_get_item(self, cr, uid, line, context={}):
687         return {
688             'type':'src',
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,
694             'ref' : line.ref
695         }
696
697 account_voucher_line()
698
699
700
701
702
703