[IMP] Account payment & l10_nch: Remove bank reconcile object reference and use vouch...
[odoo/odoo.git] / addons / account_payment / wizard / account_payment_order.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21 import time
22 from lxml import etree
23
24 from osv import osv, fields
25
26 class payment_order_create(osv.osv_memory):
27     """
28     Create a payment object with lines corresponding to the account move line
29     to pay according to the date and the mode provided by the user.
30     Hypothesis:
31     - Small number of non-reconcilied move line , payment mode and bank account type,
32     - Big number of partner and bank account.
33
34     If a type is given, unsuitable account Entry lines are ignored.
35     """
36
37     _name = 'payment.order.create'
38     _description = 'payment.order.create'
39     _columns = {
40         'duedate': fields.date('Due Date', required=True),
41         'entries': fields.many2many('account.move.line', 'line_pay_rel', 'pay_id', 'line_id', 'Entries')
42                 }
43     _defaults = {
44          'duedate': time.strftime('%Y-%m-%d'),
45                  }
46
47     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
48         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)
49         if context and 'line_ids' in context:
50             view_obj = etree.XML(res['arch'])
51             child = view_obj.getchildren()[0]
52             domain = '[("id", "in", '+ str(context['line_ids'])+')]'
53             field = etree.Element('field', attrib={'domain': domain, 'name':'entries', 'colspan':'4', 'height':'300', 'width':'800', 'nolabel':"1"})
54             child.addprevious(field)
55             res['arch'] = etree.tostring(view_obj)
56         return res
57
58     def create_payment(self, cr, uid, ids, context=None):
59         order_obj = self.pool.get('payment.order')
60         line_obj = self.pool.get('account.move.line')
61         payment_obj = self.pool.get('payment.line')
62         if context is None:
63             context = {}
64         data = self.read(cr, uid, ids, [])[0]
65         line_ids = data['entries']
66         if not line_ids: return {}
67
68         payment = order_obj.browse(cr, uid, context['active_id'], context=context)
69         t = None
70         line2bank = line_obj.line2bank(cr, uid, line_ids, t, context)
71
72         ## Finally populate the current payment with new lines:
73         for line in line_obj.browse(cr, uid, line_ids, context=context):
74             if payment.date_prefered == "now":
75                 #no payment date => immediate payment
76                 date_to_pay = False
77             elif payment.date_prefered == 'due':
78                 date_to_pay = line.date_maturity
79             elif payment.date_prefered == 'fixed':
80                 date_to_pay = payment.date_scheduled
81             payment_obj.create(cr, uid,{
82                 'move_line_id': line.id,
83                 'amount_currency': line.amount_to_pay,
84                 'bank_id': line2bank.get(line.id),
85                 'order_id': payment.id,
86                 'partner_id': line.partner_id and line.partner_id.id or False,
87                 'communication': line.ref or '/',
88                 'date': date_to_pay,
89                 'currency': line.invoice and line.invoice.currency_id.id or False,
90                 }, context=context)
91         return {}
92
93     def search_entries(self, cr, uid, ids, context=None):
94         order_obj = self.pool.get('payment.order')
95         line_obj = self.pool.get('account.move.line')
96         mod_obj = self.pool.get('ir.model.data')
97         if context is None:
98             context = {}
99         data = self.read(cr, uid, ids, [], context=context)[0]
100         search_due_date = data['duedate']
101         payment = order_obj.browse(cr, uid, context['active_id'], context=context)
102         ctx = ''
103         if payment.mode:
104             ctx = '''context="{'journal_id': %d}"''' % payment.mode.journal.id
105
106         # Search for move line to pay:
107         domain = [('reconcile_id', '=', False),('account_id.type', '=', 'payable'),('amount_to_pay', '>', 0)]
108         domain = domain + ['|',('date_maturity','<=',search_due_date),('date_maturity','=',False)]
109         line_ids = line_obj.search(cr, uid, domain, context=context)
110         context.update({'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': ('Entrie Lines'),
114                 'context': context,
115                 'view_type': 'form',
116                 'view_mode': 'form',
117                 'res_model': 'payment.order.create',
118                 'views': [(resource_id,'form')],
119                 'type': 'ir.actions.act_window',
120                 'target': 'new',
121                 }
122
123 payment_order_create()
124
125 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: