2008-07-23 14:41:47 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-06-16 11:00:21 +00:00
|
|
|
# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
|
|
|
|
#
|
|
|
|
# $Id$
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
import wizard
|
|
|
|
import netsvc
|
|
|
|
import pooler
|
2008-06-16 13:48:57 +00:00
|
|
|
import time
|
2008-07-08 08:13:12 +00:00
|
|
|
from tools.translate import _
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
pay_form = '''<?xml version="1.0"?>
|
|
|
|
<form string="Pay invoice">
|
2008-07-22 15:11:28 +00:00
|
|
|
<field name="amount"/>
|
|
|
|
<newline/>
|
|
|
|
<field name="name"/>
|
|
|
|
<field name="date"/>
|
|
|
|
<field name="journal_id"/>
|
|
|
|
<field name="period_id"/>
|
2006-12-07 13:41:40 +00:00
|
|
|
</form>'''
|
|
|
|
|
|
|
|
pay_fields = {
|
2008-07-22 15:11:28 +00:00
|
|
|
'amount': {'string': 'Amount paid', 'type':'float', 'required':True},
|
|
|
|
'name': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
|
|
|
|
'date': {'string': 'Payment date', 'type':'date', 'required':True, 'default':lambda *args: time.strftime('%Y-%m-%d')},
|
|
|
|
'journal_id': {'string': 'Journal', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
|
|
|
|
'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
|
2006-12-07 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _pay_and_reconcile(self, cr, uid, data, context):
|
2008-07-22 15:11:28 +00:00
|
|
|
form = data['form']
|
|
|
|
period_id = form.get('period_id', False)
|
|
|
|
journal_id = form.get('journal_id', False)
|
|
|
|
writeoff_account_id = form.get('writeoff_acc_id', False)
|
|
|
|
writeoff_journal_id = form.get('writeoff_journal_id', False)
|
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
cur_obj = pool.get('res.currency')
|
|
|
|
amount = form['amount']
|
2008-06-16 13:48:57 +00:00
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
|
|
|
|
journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
|
|
|
|
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
|
|
|
|
ctx = {'date':data['form']['date']}
|
|
|
|
amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
|
2008-06-16 13:48:57 +00:00
|
|
|
|
2008-08-25 15:41:01 +00:00
|
|
|
|
|
|
|
# Take the choosen date
|
|
|
|
if form.has_key('comment'):
|
|
|
|
context={'date_p':form['date'],'comment':form['comment']}
|
|
|
|
else:
|
|
|
|
context={'date_p':form['date'],'comment':False}
|
|
|
|
|
2008-07-22 15:11:28 +00:00
|
|
|
acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
|
|
|
|
if not acc_id:
|
|
|
|
raise wizard.except_wizard(_('Error !'), _('Your journal must have a default credit and debit account.'))
|
|
|
|
pool.get('account.invoice').pay_and_reconcile(cr, uid, [data['id']],
|
|
|
|
amount, acc_id, period_id, journal_id, writeoff_account_id,
|
|
|
|
period_id, writeoff_journal_id, context, data['form']['name'])
|
|
|
|
return {}
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def _wo_check(self, cr, uid, data, context):
|
2008-07-22 15:11:28 +00:00
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
|
|
|
|
journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
|
2008-08-27 14:15:58 +00:00
|
|
|
if invoice.company_id.currency_id.id <> invoice.currency_id.id:
|
|
|
|
return 'addendum'
|
|
|
|
if journal.currency and (journal.currency.id <> invoice.currency_id.id):
|
2008-07-22 15:11:28 +00:00
|
|
|
return 'addendum'
|
|
|
|
if pool.get('res.currency').is_zero(cr, uid, invoice.currency_id,
|
|
|
|
(data['form']['amount'] - invoice.amount_total)):
|
|
|
|
return 'reconcile'
|
|
|
|
return 'addendum'
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
_transaction_add_form = '''<?xml version="1.0"?>
|
|
|
|
<form string="Information addendum">
|
2008-07-22 15:11:28 +00:00
|
|
|
<separator string="Write-Off Move" colspan="4"/>
|
|
|
|
<field name="writeoff_acc_id"/>
|
|
|
|
<field name="writeoff_journal_id"/>
|
2008-08-25 15:41:01 +00:00
|
|
|
<field name="comment"/>
|
2006-12-07 13:41:40 +00:00
|
|
|
</form>'''
|
|
|
|
|
|
|
|
_transaction_add_fields = {
|
2008-07-22 15:11:28 +00:00
|
|
|
'writeoff_acc_id': {'string':'Write-Off account', 'type':'many2one', 'relation':'account.account', 'required':True},
|
|
|
|
'writeoff_journal_id': {'string': 'Write-Off journal', 'type': 'many2one', 'relation':'account.journal', 'required':True},
|
2008-08-25 15:41:01 +00:00
|
|
|
'comment': {'string': 'Entry Name', 'type':'char', 'size': 64, 'required':True},
|
2006-12-07 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def _get_value_addendum(self, cr, uid, data, context={}):
|
2008-07-22 15:11:28 +00:00
|
|
|
return {}
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def _get_period(self, cr, uid, data, context={}):
|
2008-07-22 15:11:28 +00:00
|
|
|
pool = pooler.get_pool(cr.dbname)
|
|
|
|
ids = pool.get('account.period').find(cr, uid, context=context)
|
|
|
|
period_id = False
|
|
|
|
if len(ids):
|
|
|
|
period_id = ids[0]
|
|
|
|
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
|
|
|
|
if invoice.state == 'draft':
|
|
|
|
raise wizard.except_wizard(_('Error !'), _('Can not pay draft invoice.'))
|
|
|
|
return {
|
|
|
|
'period_id': period_id,
|
|
|
|
'amount': invoice.amount_total,
|
|
|
|
'date': time.strftime('%Y-%m-%d')
|
|
|
|
}
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
class wizard_pay_invoice(wizard.interface):
|
2008-07-22 15:11:28 +00:00
|
|
|
states = {
|
|
|
|
'init': {
|
|
|
|
'actions': [_get_period],
|
|
|
|
'result': {'type':'form', 'arch':pay_form, 'fields':pay_fields, 'state':[('end','Cancel'),('reconcile','Partial Payment'),('writeoff_check','Full Payment')]}
|
|
|
|
},
|
|
|
|
'writeoff_check': {
|
|
|
|
'actions': [],
|
|
|
|
'result' : {'type': 'choice', 'next_state': _wo_check }
|
|
|
|
},
|
|
|
|
'addendum': {
|
|
|
|
'actions': [_get_value_addendum],
|
|
|
|
'result': {'type': 'form', 'arch':_transaction_add_form, 'fields':_transaction_add_fields, 'state':[('end','Cancel'),('reconcile','Pay and reconcile')]}
|
|
|
|
},
|
|
|
|
'reconcile': {
|
|
|
|
'actions': [_pay_and_reconcile],
|
|
|
|
'result': {'type':'state', 'state':'end'}
|
|
|
|
}
|
|
|
|
}
|
2006-12-07 13:41:40 +00:00
|
|
|
wizard_pay_invoice('account.invoice.pay')
|
|
|
|
|
2008-07-23 14:41:47 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|