1
# -*- encoding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
##############################################################################
24
from tools.misc import UpdateableStr
28
FORM = UpdateableStr()
31
'entries': {'string':'Entries', 'type':'many2many', 'relation': 'account.move.line',},
34
'duedate': {'string':'Due Date', 'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d'),},
36
arch_duedate='''<?xml version="1.0"?>
37
<form string="Search Payment lines">
38
<field name="duedate" />
42
def _get_date(self, cr, uid, data, context):
43
pool = pooler.get_pool(cr.dbname)
44
data['form']['duedate'] = pool.get('payment.cheque').browse(cr, uid, data['id'], context=context).date
47
def search_entries(self, cr, uid, data, context):
48
search_due_date=data['form']['duedate']
50
pool = pooler.get_pool(cr.dbname)
51
chk_obj = pool.get('payment.cheque')
52
line_obj = pool.get('account.move.line')
54
cheque = chk_obj.browse(cr, uid, data['id'],
58
ctx = '''context="{'journal_id': %d}"''' % cheque.mode.journal.id
60
# Search for move line to pay:
61
domain = [('reconcile_id', '=', False),('account_id.type', 'in', ['payable','receivable']),('partner_id','=',cheque.partner_id.id),
62
('journal_id.type','!=','cash')]
63
#('amount_to_pay', '>', 0)
64
domain = domain + ['|',('date_maturity','<',search_due_date),('date_maturity','=',False)]
65
line_ids = line_obj.search(cr, uid, domain, context=context)
66
FORM.string = '''<?xml version="1.0"?>
67
<form string="Populate Payment:">
68
<field name="entries" colspan="4" height="300" width="1000" nolabel="1"
69
domain="[('id', 'in', [%s])]" %s/>
70
</form>''' % (','.join([str(x) for x in line_ids]), ctx)
73
def create_payment(self, cr, uid, data, context):
74
line_ids= data['form']['entries'][0][2]
75
if not line_ids: return {}
76
pool= pooler.get_pool(cr.dbname)
77
order_obj = pool.get('payment.cheque')
78
bnk_obj = pool.get('res.partner.bank')
79
line_obj = pool.get('account.move.line')
80
cur_obj = pool.get('res.currency')
81
payment = order_obj.browse(cr, uid, data['id'], context=context)
82
t = payment.mode and payment.mode.type.id or None
83
line2bank = pool.get('account.move.line').line2bank(cr, uid,
85
chk_name = payment.concept or ''
86
## Finally populate the current payment with new lines:
88
for line in line_obj.browse(cr, uid, line_ids, context=context):
91
chk_name += line.ref or '/'
92
if line.reconcile_partial_id:
93
partial_rec = [x.id for x in line.reconcile_partial_id.line_partial_ids]
94
partial_rec.remove(line.id)
95
order_obj.write(cr, uid, data['id'], {'concept': chk_name, 'partial_line_id': map(lambda x: (4,x,False), partial_rec)})
96
for line in line_obj.browse(cr, uid, line_ids, context=context):
97
amount_currency, pay_rate, rate = 0, 1, 1
98
date_to_pay = payment.date
99
currency = line.currency_id.id or (line.invoice and line.invoice.currency_id.id) or line.account_id.company_id.currency_id.id
100
company_currency = line.account_id.company_id.currency_id.id
101
journal_currency = payment.mode.journal.currency.id
102
calc_amount = amount_currency = line.amount_to_pay
103
calc_currency = company_currency
104
if currency == journal_currency != company_currency:
105
amount_currency = abs((line.invoice and (line.credit > 0 and -line.invoice.residual or line.invoice.residual)) \
106
or (line.amount_to_pay and (line.credit > 0 and -line.amount_to_pay or line.amount_to_pay)) \
107
or line.amount_currency or (line.debit - line.credit) or 0.0)
108
pay_rate = cur_obj._current_rate(cr, uid, [company_currency], 'rate', arg=None, context={'date':date_to_pay})[company_currency]
109
calc_amount = amount_currency
110
calc_currency = currency
111
elif currency != company_currency:
112
amount_currency = abs((line.invoice and (line.credit > 0 and -line.invoice.residual or line.invoice.residual)) \
113
or (line.amount_to_pay and (line.credit > 0 and -line.amount_to_pay or line.amount_to_pay)) \
114
or line.amount_currency or (line.debit - line.credit) or 0.0)
115
pay_rate = cur_obj._current_rate(cr, uid, [company_currency], 'rate', arg=None, context={'date':date_to_pay})[company_currency]
116
rate = abs((line.debit - line.credit) / amount_currency)
117
calc_amount = cur_obj.compute(cr, uid, currency, company_currency, amount_currency, context={'date': date_to_pay})
118
pool.get('payment.line').create(cr,uid,{
119
'move_line_id': line.id,
120
'calc_amount': calc_amount or 0.0,
121
'amount_currency': amount_currency or 0.0,
122
'currency': currency,
123
'calc_currency': calc_currency,
124
'bank_id': line2bank.get(line.id),
125
'cheque_id': payment.id,
126
'partner_id': line.partner_id and line.partner_id.id or False,
127
'communication': line.ref or '/',
130
'pay_rate': pay_rate,
131
'account_id': line.account_id.id,
132
'company_currency': company_currency,
136
class wizard_invoice_import(wizard.interface):
140
'actions': [_get_date],
143
'arch': arch_duedate,
144
'fields':field_duedate,
147
('search','_Search', '', True)
153
'actions': [search_entries],
160
('create','_Add to payment order', '', True)
168
'action': create_payment,
173
wizard_invoice_import('import_invoice')
177
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: