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