Fix detection of MIME type in message_parse()
[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 openerp.osv import fields, osv
26 from openerp.tools.translate import _
27
28 class payment_order_create(osv.osv_memory):
29     """
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.
32     Hypothesis:
33     - Small number of non-reconciled move line, payment mode and bank account type,
34     - Big number of partner and bank account.
35
36     If a type is given, unsuitable account Entry lines are ignored.
37     """
38
39     _name = 'payment.order.create'
40     _description = 'payment.order.create'
41     _columns = {
42         'duedate': fields.date('Due Date', required=True),
43         'entries': fields.many2many('account.move.line', 'line_pay_rel', 'pay_id', 'line_id', 'Entries')
44     }
45     _defaults = {
46          'duedate': lambda *a: time.strftime('%Y-%m-%d'),
47     }
48
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']")
55             for node in nodes:
56                 node.set('domain', '[("id", "in", '+ str(context['line_ids'])+')]')
57             res['arch'] = etree.tostring(doc)
58         return res
59
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')
64         if context is None:
65             context = {}
66         data = self.browse(cr, uid, ids, context=context)[0]
67         line_ids = [entry.id for entry in data.entries]
68         if not line_ids:
69             return {'type': 'ir.actions.act_window_close'}
70
71         payment = order_obj.browse(cr, uid, context['active_id'], context=context)
72         t = None
73         line2bank = line_obj.line2bank(cr, uid, line_ids, t, context)
74
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
79                 date_to_pay = False
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_to_pay,
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',
92                     'date': date_to_pay,
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,
94                 }, context=context)
95         return {'type': 'ir.actions.act_window_close'}
96
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')
100         if context is None:
101             context = {}
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)
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': _('Entry 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: