[FIX] /web/login restore request.uid in case of authentication failure
[odoo/odoo.git] / addons / account_voucher / wizard / account_statement_from_invoice.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
24 from openerp.osv import fields, osv
25 from openerp.tools.translate import _
26
27 class account_statement_from_invoice_lines(osv.osv_memory):
28     """
29     Generate Entries by Statement from Invoices
30     """
31     _name = "account.statement.from.invoice.lines"
32     _description = "Entries by Statement from Invoices"
33     _columns = {
34         'line_ids': fields.many2many('account.move.line', 'account_move_line_relation', 'move_id', 'line_id', 'Invoices'),
35     }
36
37     def populate_statement(self, cr, uid, ids, context=None):
38         if context is None:
39             context = {}
40         statement_id = context.get('statement_id', False)
41         if not statement_id:
42             return {'type': 'ir.actions.act_window_close'}
43         data =  self.read(cr, uid, ids, context=context)[0]
44         line_ids = data['line_ids']
45         if not line_ids:
46             return {'type': 'ir.actions.act_window_close'}
47
48         line_obj = self.pool.get('account.move.line')
49         statement_obj = self.pool.get('account.bank.statement')
50         statement_line_obj = self.pool.get('account.bank.statement.line')
51         currency_obj = self.pool.get('res.currency')
52         voucher_obj = self.pool.get('account.voucher')
53         voucher_line_obj = self.pool.get('account.voucher.line')
54         line_date = time.strftime('%Y-%m-%d')
55         statement = statement_obj.browse(cr, uid, statement_id, context=context)
56
57         # for each selected move lines
58         for line in line_obj.browse(cr, uid, line_ids, context=context):
59             voucher_res = {}
60             ctx = context.copy()
61             #  take the date for computation of currency => use payment date
62             ctx['date'] = line_date
63             amount = 0.0
64
65             if line.debit > 0:
66                 amount = line.debit
67             elif line.credit > 0:
68                 amount = -line.credit
69
70             if line.amount_currency:
71                 amount = currency_obj.compute(cr, uid, line.currency_id.id,
72                     statement.currency.id, line.amount_currency, context=ctx)
73             elif (line.invoice and line.invoice.currency_id.id <> statement.currency.id):
74                 amount = currency_obj.compute(cr, uid, line.invoice.currency_id.id,
75                     statement.currency.id, amount, context=ctx)
76
77             context.update({'move_line_ids': [line.id],
78                             'invoice_id': line.invoice.id})
79             type = 'general'
80             ttype = amount < 0 and 'payment' or 'receipt'
81             sign = 1
82             if line.journal_id.type in ('sale', 'sale_refund'):
83                 type = 'customer'
84                 ttype = 'receipt'
85             elif line.journal_id.type in ('purchase', 'purhcase_refund'):
86                 type = 'supplier'
87                 ttype = 'payment'
88                 sign = -1
89             result = voucher_obj.onchange_partner_id(cr, uid, [], partner_id=line.partner_id.id, journal_id=statement.journal_id.id, amount=sign*amount, currency_id= statement.currency.id, ttype=ttype, date=line_date, context=context)
90             voucher_res = { 'type': ttype,
91                             'name': line.name,
92                             'partner_id': line.partner_id.id,
93                             'journal_id': statement.journal_id.id,
94                             'account_id': result['value'].get('account_id', statement.journal_id.default_credit_account_id.id),
95                             'company_id': statement.company_id.id,
96                             'currency_id': statement.currency.id,
97                             'date': statement.date,
98                             'amount': sign*amount,
99                             'payment_rate': result['value']['payment_rate'],
100                             'payment_rate_currency_id': result['value']['payment_rate_currency_id'],
101                             'period_id':statement.period_id.id}
102             voucher_id = voucher_obj.create(cr, uid, voucher_res, context=context)
103
104             voucher_line_dict =  {}
105             for line_dict in result['value']['line_cr_ids'] + result['value']['line_dr_ids']:
106                 move_line = line_obj.browse(cr, uid, line_dict['move_line_id'], context)
107                 if line.move_id.id == move_line.move_id.id:
108                     voucher_line_dict = line_dict
109
110             if voucher_line_dict:
111                 voucher_line_dict.update({'voucher_id': voucher_id})
112                 voucher_line_obj.create(cr, uid, voucher_line_dict, context=context)
113             statement_line_obj.create(cr, uid, {
114                 'name': line.name or '?',
115                 'amount': amount,
116                 'type': type,
117                 'partner_id': line.partner_id.id,
118                 'account_id': line.account_id.id,
119                 'statement_id': statement_id,
120                 'ref': line.ref,
121                 'voucher_id': voucher_id,
122                 'date': statement.date,
123             }, context=context)
124         return {'type': 'ir.actions.act_window_close'}
125
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: