Skip to content
Snippets Groups Projects
Commit 78daf747 authored by Viral Rami's avatar Viral Rami
Browse files

[MOD,ADD] account_pay_invoice converted to osv memory wizard.

bzr revid: vir@tinyerp.com-20100416134524-vhpr0931mfl2di3a
parent ce1e63bd
Branches
Tags
No related merge requests found
......@@ -67,6 +67,7 @@ module named account_voucherss
'wizard/account_change_currency_view.xml',
'wizard/account_validate_move_view.xml',
'wizard/account_statement_from_invoice_view.xml',
'wizard/account_pay_invoice_view.xml',
'project/wizard/project_account_analytic_line_view.xml',
'account_view.xml',
'account_end_fy.xml',
......
......@@ -16,7 +16,7 @@
<wizard id="wizard_invoice_pay" model="account.invoice" name="account.invoice.pay" string="Pay invoice" groups="base.group_user"/>
<!-- wizard id="wizard_invoice_pay" model="account.invoice" name="account.invoice.pay" string="Pay invoice" groups="base.group_user"/-->
<!-- close year, period, journal -->
<!-- <wizard id="wizard_fiscalyear_close" menu="False" model="account.fiscalyear" name="account.fiscalyear.close" string="Generate Fiscal Year Opening Entries"/>-->
......
......@@ -652,6 +652,7 @@ class account_invoice(osv.osv):
def compute_invoice_totals(self, cr, uid, inv, company_currency, ref, invoice_move_lines):
total = 0
total_currency = 0
cur_obj = self.pool.get('res.currency')
for i in invoice_move_lines:
if inv.currency_id.id != company_currency:
i['currency_id'] = inv.currency_id.id
......
......@@ -25,7 +25,7 @@ import account_move_line_unreconcile_select
import wizard_reconcile
import wizard_unreconcile
import account_invoice_refund
import wizard_pay_invoice
import account_pay_invoice
import account_move_journal
import account_journal_select
import account_move_bank_reconcile
......
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import time
from osv import fields, osv
from tools.translate import _
import decimal_precision as dp
class account_invoice_pay_writeoff(osv.osv_memory):
"""
Opens the write off amount pay form.
"""
_name = "account.invoice.pay.writeoff"
_description = "Pay Invoice "
_columns = {
'writeoff_acc_id': fields.many2one('account.account', 'Write-Off account', required=True),
'writeoff_journal_id': fields.many2one('account.journal', 'Write-Off journal', required=True),
'comment': fields.char('Comment', size=64, required=True),
'analytic_id': fields.many2one('account.analytic.account','Analytic Account'),
}
_defaults = {
'comment': 'Write-Off',
}
def pay_and_reconcile_writeoff(self, cr, uid, ids, context=None):
data = self.read(cr, uid, ids,context=context)[0]
context.update({'write_off':data})
self.pool.get('account.invoice.pay').pay_and_reconcile(cr, uid, ids, context=context)
return {}
account_invoice_pay_writeoff()
class account_invoice_pay(osv.osv_memory):
"""
Generate pay invoice wizard, user can make partial or full payment for invoice.
"""
_name = "account.invoice.pay"
_description = "Pay Invoice "
_columns = {
'amount': fields.float('Amount paid', required=True, digits_compute = dp.get_precision('Account')),
'name': fields.char('Entry Name', size=64, required=True),
'date': fields.date('Date payment', required=True),
'journal_id': fields.many2one('account.journal', 'Journal/Payment Mode', required=True),
'period_id': fields.many2one('account.period', 'Period', required=True),
}
def view_init(self, cr, uid, ids, context=None):
invoice = self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context=context)
if invoice.state in ['draft', 'proforma2', 'cancel']:
raise osv.except_osv(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
pass
def _get_period(self, cr, uid, context=None):
ids = self.pool.get('account.period').find(cr, uid, context=context)
period_id = False
if len(ids):
period_id = ids[0]
return period_id
def _get_amount(self, cr, uid, context=None):
return self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context=context).residual
_defaults = {
'date': lambda *a: time.strftime('%Y-%m-%d'),
'period_id': _get_period,
'amount': _get_amount,
}
def wo_check(self, cr, uid, ids, context=None):
cur_obj = self.pool.get('res.currency')
mod_obj = self.pool.get('ir.model.data')
if context is None:
context = {}
data = self.read(cr, uid, ids,context=context)[0]
invoice = self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context)
journal = self.pool.get('account.journal').browse(cr, uid, data['journal_id'], context)
# Here we need that:
# The invoice total amount in company's currency <> paid amount in company currency
# (according to the correct day rate, invoicing rate and payment rate are may be different)
# => Ask to a write-off of the difference. This could happen even if both amount are equal,
# because if the currency rate
# Get the amount in company currency for the invoice (according to move lines)
inv_amount_company_currency = 0
for aml in invoice.move_id.line_id:
if aml.account_id.id == invoice.account_id.id or aml.account_id.type in ('receivable', 'payable'):
inv_amount_company_currency += aml.debit
inv_amount_company_currency -= aml.credit
inv_amount_company_currency = abs(inv_amount_company_currency)
# Get the current amount paid in company currency
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
ctx = {'date':data['date']}
amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['amount'], round=True, context=ctx)
else:
amount_paid = data['amount']
# Get the old payment if there are some
if invoice.payment_ids:
debit=credit=0.0
for payment in invoice.payment_ids:
debit+=payment.debit
credit+=payment.credit
amount_paid+=abs(debit-credit)
# Test if there is a difference according to currency rouding setting
if self.pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
(amount_paid - inv_amount_company_currency)):
return self.pay_and_reconcile(cr, uid, ids, context=context)
else:
model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','view_account_invoice_pay_writeoff')], context=context)
resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
return {
'name': _('Information addendum'),
'context': context,
'view_type': 'form',
'view_mode': 'form',
'res_model': 'account.invoice.pay.writeoff',
'views': [(resource_id,'form')],
'type': 'ir.actions.act_window',
'target': 'new',
}
def pay_and_reconcile(self, cr, uid, ids, context=None):
cur_obj = self.pool.get('res.currency')
if context is None:
context = {}
data = self.read(cr, uid, ids,context=context)[0]
writeoff_account_id = False
writeoff_journal_id = False
comment = False
if 'write_off' in context and context['write_off'] :
writeoff_account_id = context['write_off']['writeoff_acc_id']
writeoff_journal_id = context['write_off']['writeoff_journal_id']
comment = context['write_off']['comment']
amount = data['amount']
invoice = self.pool.get('account.invoice').browse(cr, uid, context['active_id'], context)
journal = self.pool.get('account.journal').browse(cr, uid, data['journal_id'], context)
# Compute the amount in company's currency, with the journal currency (which is equal to payment currency)
# when it is needed : If payment currency (according to selected journal.currency) is <> from company currency
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
ctx = {'date':data['date']}
amount = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, amount, context=ctx)
currency_id = journal.currency.id
# Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
context.update({'amount_currency':data['amount'],'currency_id':currency_id})
if invoice.company_id.currency_id.id<>invoice.currency_id.id:
ctx = {'date':data['date']}
amount = cur_obj.compute(cr, uid, invoice.currency_id.id, invoice.company_id.currency_id.id, amount, context=ctx)
currency_id = invoice.currency_id.id
# Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
context.update({'amount_currency':data['amount'],'currency_id':currency_id})
# Take the choosen date
if comment:
context.update({'date_p':data['date'],'comment':comment})
else:
context.update({'date_p':data['date'],'comment':False})
acc_id = journal.default_credit_account_id and journal.default_credit_account_id.id
if not acc_id:
raise osv.except_osv(_('Error !'), _('Your journal must have a default credit and debit account.'))
self.pool.get('account.invoice').pay_and_reconcile(cr, uid, [context['active_id']],
amount, acc_id, data['period_id'], data['journal_id'], writeoff_account_id,
data['period_id'], writeoff_journal_id, context, data['name'])
return {}
account_invoice_pay()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="view_account_invoice_pay" model="ir.ui.view">
<field name="name">account.invoice.pay.form</field>
<field name="model">account.invoice.pay</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Pay invoice">
<group colspan="4" >
<field name="amount"/>
<newline/>
<field name="name"/>
<field name="date"/>
<field name="journal_id"/>
<field name="period_id"/>
</group>
<group colspan="4" col="6">
<label string ="" colspan="2"/>
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-execute" string="Partial Payment" name="pay_and_reconcile" type="object"/>
<button icon="gtk-execute" string="Full Payment" name="wo_check" type="object"/>
</group>
</form>
</field>
</record>
<act_window name="Pay Invoice"
res_model="account.invoice.pay"
src_model="account.invoice"
view_mode="form"
target="new"
key2="client_action_multi"
id="action_view_account_invoice_pay"/>
<record id="view_account_invoice_pay_writeoff" model="ir.ui.view">
<field name="name">account.invoice.pay.writeoff.form</field>
<field name="model">account.invoice.pay.writeoff</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Information addendum">
<group colspan="4" >
<separator string="Write-Off Move" colspan="4"/>
<field name="writeoff_journal_id"/>
<field name="writeoff_acc_id" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
<field name="comment"/>
<separator string="Analytic" colspan="4"/>
<field name="analytic_id"/>
</group>
<group colspan="4" col="6">
<label string ="" colspan="2"/>
<button icon="gtk-cancel" special="cancel" string="Cancel"/>
<button icon="gtk-execute" string="Pay and reconcile" name="pay_and_reconcile_writeoff" type="object"/>
</group>
</form>
</field>
</record>
</data>
</openerp>
\ No newline at end of file
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import wizard
import netsvc
import pooler
import time
from tools.translate import _
import tools
import decimal_precision as dp
pay_form = '''<?xml version="1.0"?>
<form string="Pay invoice">
<field name="amount"/>
<newline/>
<field name="name"/>
<field name="date"/>
<field name="journal_id"/>
<field name="period_id"/>
</form>'''
pay_fields = {
'amount': {'string': 'Amount paid', 'type':'float', 'required':True, 'digits': (16,dp.get_precision('Account'))},
'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/Payment Mode', 'type': 'many2one', 'relation':'account.journal', 'required':True, 'domain':[('type','=','cash')]},
'period_id': {'string': 'Period', 'type': 'many2one', 'relation':'account.period', 'required':True},
}
def _pay_and_reconcile(self, cr, uid, data, context):
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']
context['analytic_id'] = form.get('analytic_id', False)
invoice = pool.get('account.invoice').browse(cr, uid, data['id'], context)
journal = pool.get('account.journal').browse(cr, uid, data['form']['journal_id'], context)
# Compute the amount in company's currency, with the journal currency (which is equal to payment currency)
# when it is needed : If payment currency (according to selected journal.currency) is <> from company currency
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)
currency_id = journal.currency.id
# Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
context.update({'amount_currency':form['amount'],'currency_id':currency_id})
if invoice.company_id.currency_id.id<>invoice.currency_id.id:
ctx = {'date':data['form']['date']}
amount = cur_obj.compute(cr, uid, invoice.currency_id.id, invoice.company_id.currency_id.id, amount, context=ctx)
currency_id = invoice.currency_id.id
# Put the paid amount in currency, and the currency, in the context if currency is different from company's currency
context.update({'amount_currency':form['amount'],'currency_id':currency_id})
# Take the choosen date
if form.has_key('comment'):
context.update({'date_p':form['date'],'comment':form['comment']})
else:
context.update({'date_p':form['date'],'comment':False})
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 {}
def _wo_check(self, cr, uid, data, context):
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)
cur_obj = pool.get('res.currency')
# Here we need that:
# The invoice total amount in company's currency <> paid amount in company currency
# (according to the correct day rate, invoicing rate and payment rate are may be different)
# => Ask to a write-off of the difference. This could happen even if both amount are equal,
# because if the currency rate
# Get the amount in company currency for the invoice (according to move lines)
inv_amount_company_currency = 0
for aml in invoice.move_id.line_id:
if aml.account_id.id == invoice.account_id.id or aml.account_id.type in ('receivable', 'payable'):
inv_amount_company_currency += aml.debit
inv_amount_company_currency -= aml.credit
inv_amount_company_currency = abs(inv_amount_company_currency)
# Get the current amount paid in company currency
if journal.currency and invoice.company_id.currency_id.id<>journal.currency.id:
ctx = {'date':data['form']['date']}
amount_paid = cur_obj.compute(cr, uid, journal.currency.id, invoice.company_id.currency_id.id, data['form']['amount'], round=True, context=ctx)
else:
amount_paid = data['form']['amount']
# Get the old payment if there are some
if invoice.payment_ids:
debit=credit=0.0
for payment in invoice.payment_ids:
debit+=payment.debit
credit+=payment.credit
amount_paid+=abs(debit-credit)
# Test if there is a difference according to currency rouding setting
if pool.get('res.currency').is_zero(cr, uid, invoice.company_id.currency_id,
(amount_paid - inv_amount_company_currency)):
return 'reconcile'
return 'addendum'
_transaction_add_form = '''<?xml version="1.0"?>
<form string="Information addendum">
<separator string="Write-Off Move" colspan="4"/>
<field name="writeoff_journal_id"/>
<field name="writeoff_acc_id" domain="[('type','&lt;&gt;','view'),('type','&lt;&gt;','consolidation')]"/>
<field name="comment"/>
<separator string="Analytic" colspan="4"/>
<field name="analytic_id"/>
</form>'''
_transaction_add_fields = {
'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},
'comment': {'string': 'Comment', 'type':'char', 'size': 64 , 'required':True},
'analytic_id': {'string':'Analytic Account', 'type': 'many2one', 'relation':'account.analytic.account'},
}
def _get_value_addendum(self, cr, uid, data, context={}):
return {'comment': _('Write-Off')}
def _get_period(self, cr, uid, data, context={}):
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 in ['draft', 'proforma2', 'cancel']:
raise wizard.except_wizard(_('Error !'), _('Can not pay draft/proforma/cancel invoice.'))
return {
'period_id': period_id,
'amount': invoice.residual,
'date': time.strftime('%Y-%m-%d')
}
class wizard_pay_invoice(wizard.interface):
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'}
}
}
wizard_pay_invoice('account.invoice.pay')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment