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',
32 'relation': 'account.move.line',},
35 'duedate': {'string':'Due Date', 'type':'date','required':True, 'default': lambda *a: time.strftime('%Y-%m-%d'),},
37 arch_duedate='''<?xml version="1.0"?>
38 <form string="Search Payment lines">
39 <field name="duedate" />
43 def search_entries(self, cr, uid, data, context):
44 search_due_date=data['form']['duedate']
46 pool = pooler.get_pool(cr.dbname)
47 order_obj = pool.get('payment.order')
48 line_obj = pool.get('account.move.line')
50 payment = order_obj.browse(cr, uid, data['id'],
54 ctx = '''context="{'journal_id': %d}"''' % payment.mode.journal.id
56 # Search for move line to pay:
57 domain = [('reconcile_id', '=', False),('account_id.type', '=', 'payable'),('amount_to_pay', '>', 0)]
58 domain = domain + ['|',('date_maturity','<=',search_due_date),('date_maturity','=',False)]
59 line_ids = line_obj.search(cr, uid, domain, context=context)
60 FORM.string = '''<?xml version="1.0"?>
61 <form string="Populate Payment:">
62 <field name="entries" colspan="4" height="300" width="800" nolabel="1"
63 domain="[('id', 'in', [%s])]" %s/>
64 </form>''' % (','.join([str(x) for x in line_ids]), ctx)
67 def create_payment(self, cr, uid, data, context):
68 line_ids= data['form']['entries'][0][2]
69 if not line_ids: return {}
71 pool= pooler.get_pool(cr.dbname)
72 order_obj = pool.get('payment.order')
73 line_obj = pool.get('account.move.line')
75 payment = order_obj.browse(cr, uid, data['id'],
77 t = payment.mode and payment.mode.type.id or None
78 line2bank = pool.get('account.move.line').line2bank(cr, uid,
81 ## Finally populate the current payment with new lines:
82 for line in line_obj.browse(cr, uid, line_ids, context=context):
83 if payment.date_prefered == "now":
84 #no payment date => immediate payment
86 elif payment.date_prefered == 'due':
87 date_to_pay = line.date_maturity
88 elif payment.date_prefered == 'fixed':
89 date_to_pay = payment.date_planned
90 pool.get('payment.line').create(cr,uid,{
91 'move_line_id': line.id,
92 'amount_currency': line.amount_to_pay,
93 'bank_id': line2bank.get(line.id),
94 'order_id': payment.id,
95 'partner_id': line.partner_id and line.partner_id.id or False,
96 'communication': line.ref or '/',
98 'currency': line.invoice and line.invoice.currency_id.id or False,
103 class wizard_payment_order(wizard.interface):
105 Create a payment object with lines corresponding to the account move line
106 to pay according to the date and the mode provided by the user.
108 - Small number of non-reconcilied move line , payment mode and bank account type,
109 - Big number of partner and bank account.
111 If a type is given, unsuitable account move lines are ignored.
119 'arch': arch_duedate,
120 'fields':field_duedate,
123 ('search','_Search', '', True)
129 'actions': [search_entries],
136 ('create','_Add to payment order', '', True)
144 'action': create_payment,
149 wizard_payment_order('populate_payment')
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: