2007-08-06 09:45:52 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Copyright (c) 2005-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
|
|
|
|
#
|
|
|
|
# WARNING: This program as such is intended to be used by professional
|
|
|
|
# programmers who take the whole responsability of assessing all potential
|
|
|
|
# consequences resulting from its eventual inadequacies and bugs
|
|
|
|
# End users who are looking for a ready-to-use solution with commercial
|
|
|
|
# garantees and support are strongly adviced to contract a Free Software
|
|
|
|
# Service Company
|
|
|
|
#
|
|
|
|
# This program is Free Software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields
|
|
|
|
from osv import osv
|
|
|
|
import time
|
|
|
|
import netsvc
|
|
|
|
class payment_type(osv.osv):
|
|
|
|
_name= 'payment.type'
|
|
|
|
_description= 'Payment type'
|
|
|
|
_columns= {
|
|
|
|
'name': fields.char('Name', size=64, required=True),
|
2007-08-10 11:49:54 +00:00
|
|
|
'code': fields.char('Code', size=64, required=True),
|
2007-08-06 09:45:52 +00:00
|
|
|
'suitable_bank_types': fields.many2many('res.partner.bank.type',
|
|
|
|
'bank_type_payment_type_rel',
|
|
|
|
'pay_type_id','bank_type_id',
|
|
|
|
'Suitable bank types')
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
payment_type()
|
|
|
|
|
|
|
|
|
|
|
|
class payment_mode(osv.osv):
|
|
|
|
_name= 'payment.mode'
|
|
|
|
_description= 'Payment mode'
|
|
|
|
_columns= {
|
|
|
|
'name': fields.char('Name', size=64, required=True),
|
2007-08-07 12:02:12 +00:00
|
|
|
'bank_id': fields.many2one('res.partner.bank',"Bank account",required=True),
|
2007-08-06 09:45:52 +00:00
|
|
|
'journal': fields.many2one('account.journal','Journal',required=True,domain=[('type','=','cash')]),
|
|
|
|
'type': fields.many2one('payment.type','Payment type',required=True),
|
2007-08-10 11:49:54 +00:00
|
|
|
'account': fields.many2one('account.account','Default payable account',domain=[('type','=','payable')],required=True),
|
2007-08-06 09:45:52 +00:00
|
|
|
}
|
|
|
|
|
2007-08-10 11:49:54 +00:00
|
|
|
def suitable_bank_types(self,cr,uid,payment_code= 'manual',context={}):
|
|
|
|
""" Return the codes of the bank type that are suitable for the given payment type code"""
|
2007-08-06 09:45:52 +00:00
|
|
|
cr.execute(""" select t.code
|
|
|
|
from res_partner_bank_type t
|
|
|
|
join bank_type_payment_type_rel r on (r.bank_type_id = t.id)
|
|
|
|
join payment_type pt on (r.pay_type_id = pt.id)
|
2007-08-10 11:49:54 +00:00
|
|
|
where pt.code = %s """, [payment_code])
|
2007-08-06 09:45:52 +00:00
|
|
|
return [x[0] for x in cr.fetchall()]
|
|
|
|
|
|
|
|
payment_mode()
|
|
|
|
|
|
|
|
|
|
|
|
class payment_order(osv.osv):
|
|
|
|
_name = 'payment.order'
|
|
|
|
_description = 'Payment Order'
|
|
|
|
_rec_name = 'date'
|
|
|
|
|
2007-08-10 11:49:54 +00:00
|
|
|
def get_wizard(self,type):
|
|
|
|
logger = netsvc.Logger()
|
|
|
|
logger.notifyChannel("warning", netsvc.LOG_WARNING, "No wizard found for the payment type '%s'."%type)
|
|
|
|
return None
|
|
|
|
|
2007-08-06 09:45:52 +00:00
|
|
|
def _total(self, cr, uid, ids, name, args, context={}):
|
|
|
|
if not ids: return {}
|
2007-08-23 14:07:04 +00:00
|
|
|
cr.execute("""SELECT o.id, coalesce(sum(l.amount),0)
|
|
|
|
FROM payment_order o
|
|
|
|
LEFT JOIN payment_line l
|
|
|
|
ON (o.id = l.order_id)
|
|
|
|
WHERE o.id in (%s)
|
|
|
|
GROUP BY o.id""" % ','.join(map(str,ids)))
|
2007-08-06 09:45:52 +00:00
|
|
|
return dict(cr.fetchall())
|
|
|
|
|
|
|
|
def nb_line(self, cr, uid, ids, name, args, context={}):
|
|
|
|
if not ids: return {}
|
|
|
|
res= {}.fromkeys(ids,0)
|
2007-08-23 14:07:04 +00:00
|
|
|
cr.execute("""SELECT "order_id", count(*)
|
|
|
|
FROM payment_line
|
|
|
|
WHERE "order_id" in (%s)
|
|
|
|
GROUP BY "order_id" """ % ','.join(map(str,ids)))
|
2007-08-06 09:45:52 +00:00
|
|
|
res.update(dict(cr.fetchall()))
|
|
|
|
return res
|
|
|
|
|
|
|
|
_columns = {
|
2007-08-08 19:55:39 +00:00
|
|
|
'date_planned': fields.date('Scheduled date if fixed'),
|
2007-08-06 09:45:52 +00:00
|
|
|
'reference': fields.char('Reference',size=128),
|
2007-08-10 11:49:54 +00:00
|
|
|
'mode': fields.many2one('payment.mode','Payment mode', select=True),
|
2007-08-06 09:45:52 +00:00
|
|
|
'state': fields.selection([('draft', 'Draft'),('open','Open'),
|
|
|
|
('cancel','Cancelled'),('done','Done')], 'State', select=True),
|
2007-08-07 12:02:12 +00:00
|
|
|
'line_ids': fields.one2many('payment.line','order_id','Payment lines'),
|
2007-08-06 09:45:52 +00:00
|
|
|
'total': fields.function(_total, string="Total", method=True, type='float'),
|
|
|
|
'user_id': fields.many2one('res.users','User',required=True),
|
|
|
|
'nb_line': fields.function(nb_line,string='Number of payment',method=True, type='integer'),
|
|
|
|
'date_prefered': fields.selection([('now','Directly'),('due','Due date'),('fixed','Fixed date')],"Prefered date",required=True),
|
|
|
|
'date_created': fields.date('Creation date',readonly=True),
|
|
|
|
'date_done': fields.date('Execution date',readonly=True),
|
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
|
|
|
'user_id': lambda self,cr,uid,context: uid,
|
|
|
|
'state': lambda *a: 'draft',
|
|
|
|
'date_prefered': lambda *a: 'due',
|
|
|
|
'date_created': lambda *a: time.strftime('%Y-%m-%d'),
|
|
|
|
}
|
|
|
|
|
|
|
|
def set_to_draft(self, cr, uid, ids, *args):
|
|
|
|
self.write(cr, uid, ids, {'state':'draft'})
|
|
|
|
wf_service = netsvc.LocalService("workflow")
|
|
|
|
for id in ids:
|
|
|
|
wf_service.trg_create(uid, 'payment.order', id, cr)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def action_open(self, cr, uid, ids, *args):
|
|
|
|
for order in self.read(cr,uid,ids,['reference']):
|
|
|
|
if not order['reference']:
|
|
|
|
reference = self.pool.get('ir.sequence').get(cr, uid, 'payment.order')
|
|
|
|
self.write(cr,uid,order['id'],{'reference':reference})
|
|
|
|
return True
|
|
|
|
|
2007-08-10 11:49:54 +00:00
|
|
|
def set_done(self, cr, uid, id, *args):
|
|
|
|
self.write(cr,uid,id,{'date_done':time.strftime('%Y-%m-%d'),
|
|
|
|
'state':'done',})
|
|
|
|
wf_service = netsvc.LocalService("workflow")
|
|
|
|
wf_service.trg_validate(uid, 'payment.order', id, 'done', cr)
|
|
|
|
|
2007-08-06 09:45:52 +00:00
|
|
|
return True
|
|
|
|
|
2007-08-10 11:49:54 +00:00
|
|
|
|
2007-08-06 09:45:52 +00:00
|
|
|
payment_order()
|
|
|
|
|
|
|
|
class payment_line(osv.osv):
|
|
|
|
_name = 'payment.line'
|
|
|
|
_description = 'Payment Line'
|
2007-08-07 12:02:12 +00:00
|
|
|
_rec_name = 'move_line_id'
|
2007-08-06 09:45:52 +00:00
|
|
|
|
|
|
|
def partner_payable(self, cr, uid, ids, name, args, context={}):
|
|
|
|
if not ids: return {}
|
2007-08-07 12:02:12 +00:00
|
|
|
partners= self.read(cr, uid, ids, ['partner_id'], context)
|
|
|
|
partners= dict(map(lambda x: (x['id'],x['partner_id']),partners))
|
2007-08-10 11:49:54 +00:00
|
|
|
debit= self.pool.get('res.partner')._debit_get(cr, uid,
|
|
|
|
partners.values(), name,
|
|
|
|
args, context)
|
2007-08-06 09:45:52 +00:00
|
|
|
for i in partners:
|
|
|
|
partners[i]= debit[partners[i]]
|
|
|
|
return partners
|
|
|
|
|
|
|
|
def translate(self,orig):
|
|
|
|
return {"to_pay":"credit",
|
|
|
|
"due_date":"date_maturity",
|
|
|
|
"reference":"ref"}.get(orig,orig)
|
|
|
|
|
|
|
|
def select_by_name(self, cr, uid, ids, name, args, context={}):
|
|
|
|
if not ids: return {}
|
|
|
|
cr.execute("""SELECT pl.id, ml.%s
|
|
|
|
from account_move_line ml
|
2007-08-07 12:02:12 +00:00
|
|
|
inner join payment_line pl on (ml.id= pl.move_line_id)
|
2007-08-06 09:45:52 +00:00
|
|
|
where pl.id in (%s)"""% (self.translate(name),','.join(map(str,ids))) )
|
|
|
|
return dict(cr.fetchall())
|
|
|
|
|
|
|
|
|
|
|
|
_columns = {
|
2007-08-07 12:02:12 +00:00
|
|
|
'move_line_id': fields.many2one('account.move.line','Entry line',required=True),
|
2007-08-06 09:45:52 +00:00
|
|
|
'amount': fields.float('Payment Amount', digits=(16,2), required=True),
|
2007-08-07 12:02:12 +00:00
|
|
|
'bank_id': fields.many2one('res.partner.bank','Bank account'),
|
|
|
|
'order_id': fields.many2one('payment.order','Order', ondelete='cascade', select=True),
|
|
|
|
'partner_id': fields.function(select_by_name, string="Partner", method=True, type='many2one', obj='res.partner'),
|
2007-08-06 09:45:52 +00:00
|
|
|
'to_pay': fields.function(select_by_name, string="To pay", method=True, type='float'),
|
|
|
|
'due_date': fields.function(select_by_name, string="Due date", method=True, type='date'),
|
|
|
|
'date_created': fields.function(select_by_name, string="Creation date", method=True, type='date'),
|
|
|
|
'reference': fields.function(select_by_name, string="Ref", method=True, type='char'),
|
|
|
|
'partner_payable': fields.function(partner_payable, string="Partner payable", method=True, type='float'),
|
|
|
|
}
|
2007-08-10 11:49:54 +00:00
|
|
|
def onchange_move_line(self, cr, uid, id, move_line_id, payment_type,context={}):
|
2007-08-07 12:02:12 +00:00
|
|
|
if not move_line_id:
|
2007-08-06 09:45:52 +00:00
|
|
|
return {}
|
2007-08-07 12:02:12 +00:00
|
|
|
line=self.pool.get('account.move.line').browse(cr,uid,move_line_id)
|
2007-08-06 09:45:52 +00:00
|
|
|
return {'value': {'amount': line.amount_to_pay,
|
2007-08-23 14:07:04 +00:00
|
|
|
'to_pay': line.amount_to_pay,
|
|
|
|
'partner_id': line.partner_id.id,
|
|
|
|
'reference': line.ref,
|
|
|
|
'date_created': line.date_created,
|
|
|
|
'bank_id': self.pool.get('account.move.line').line2bank(cr, uid,
|
|
|
|
[move_line_id],
|
|
|
|
payment_type or 'manual',context)[move_line_id]
|
|
|
|
}}
|
2007-08-06 09:45:52 +00:00
|
|
|
|
|
|
|
payment_line()
|