1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 from lxml import etree
25 from openerp.osv import fields, osv
26 from openerp.tools.translate import _
28 class payment_order_create(osv.osv_memory):
30 Create a payment object with lines corresponding to the account move line
31 to pay according to the date and the mode provided by the user.
33 - Small number of non-reconciled move line, payment mode and bank account type,
34 - Big number of partner and bank account.
36 If a type is given, unsuitable account Entry lines are ignored.
39 _name = 'payment.order.create'
40 _description = 'payment.order.create'
42 'duedate': fields.date('Due Date', required=True),
43 'entries': fields.many2many('account.move.line', 'line_pay_rel', 'pay_id', 'line_id', 'Entries')
46 'duedate': lambda *a: time.strftime('%Y-%m-%d'),
49 def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
50 if not context: context = {}
51 res = super(payment_order_create, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=False)
52 if context and 'line_ids' in context:
53 doc = etree.XML(res['arch'])
54 nodes = doc.xpath("//field[@name='entries']")
56 node.set('domain', '[("id", "in", '+ str(context['line_ids'])+')]')
57 res['arch'] = etree.tostring(doc)
60 def create_payment(self, cr, uid, ids, context=None):
61 order_obj = self.pool.get('payment.order')
62 line_obj = self.pool.get('account.move.line')
63 payment_obj = self.pool.get('payment.line')
66 data = self.browse(cr, uid, ids, context=context)[0]
67 line_ids = [entry.id for entry in data.entries]
69 return {'type': 'ir.actions.act_window_close'}
71 payment = order_obj.browse(cr, uid, context['active_id'], context=context)
73 line2bank = line_obj.line2bank(cr, uid, line_ids, t, context)
75 ## Finally populate the current payment with new lines:
76 for line in line_obj.browse(cr, uid, line_ids, context=context):
77 if payment.date_prefered == "now":
78 #no payment date => immediate payment
80 elif payment.date_prefered == 'due':
81 date_to_pay = line.date_maturity
82 elif payment.date_prefered == 'fixed':
83 date_to_pay = payment.date_scheduled
84 payment_obj.create(cr, uid,{
85 'move_line_id': line.id,
86 'amount_currency': line.amount_residual_currency,
87 'bank_id': line2bank.get(line.id),
88 'order_id': payment.id,
89 'partner_id': line.partner_id and line.partner_id.id or False,
90 'communication': line.ref or '/',
91 'state': line.invoice and line.invoice.reference_type != 'none' and 'structured' or 'normal',
93 'currency': (line.invoice and line.invoice.currency_id.id) or line.journal_id.currency.id or line.journal_id.company_id.currency_id.id,
95 return {'type': 'ir.actions.act_window_close'}
97 def search_entries(self, cr, uid, ids, context=None):
98 line_obj = self.pool.get('account.move.line')
99 mod_obj = self.pool.get('ir.model.data')
102 data = self.browse(cr, uid, ids, context=context)[0]
103 search_due_date = data.duedate
104 # payment = self.pool.get('payment.order').browse(cr, uid, context['active_id'], context=context)
106 # Search for move line to pay:
107 domain = [('reconcile_id', '=', False), ('account_id.type', '=', 'payable'), ('credit', '>', 0), ('account_id.reconcile', '=', True)]
108 domain = domain + ['|', ('date_maturity', '<=', search_due_date), ('date_maturity', '=', False)]
109 line_ids = line_obj.search(cr, uid, domain, context=context)
110 context = dict(context, line_ids=line_ids)
111 model_data_ids = mod_obj.search(cr, uid,[('model', '=', 'ir.ui.view'), ('name', '=', 'view_create_payment_order_lines')], context=context)
112 resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
113 return {'name': _('Entry Lines'),
117 'res_model': 'payment.order.create',
118 'views': [(resource_id,'form')],
119 'type': 'ir.actions.act_window',
124 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: