report/render: fixes from xrg branch
[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 osv import fields, osv
25 from 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
39         statement_id = context.get('statement_id', False)
40         if not statement_id:
41             return {}
42         data =  self.read(cr, uid, ids, context=context)[0]
43         line_ids = data['line_ids']
44         if not line_ids:
45             return {}
46
47         line_obj = self.pool.get('account.move.line')
48         statement_obj = self.pool.get('account.bank.statement')
49         statement_line_obj = self.pool.get('account.bank.statement.line')
50         currency_obj = self.pool.get('res.currency')
51         voucher_obj = self.pool.get('account.voucher')
52         voucher_line_obj = self.pool.get('account.voucher.line')
53         line_date = time.strftime('%Y-%m-%d')
54         statement = statement_obj.browse(cr, uid, statement_id, context=context)
55
56         # for each selected move lines
57         for line in line_obj.browse(cr, uid, line_ids, context=context):
58             voucher_res = {}
59             ctx = context.copy()
60             #  take the date for computation of currency => use payment date
61             ctx['date'] = line_date
62             amount = 0.0
63
64             if line.debit > 0:
65                 amount = line.debit
66             elif line.credit > 0:
67                 amount = -line.credit
68
69             if line.amount_currency:
70                 amount = currency_obj.compute(cr, uid, line.currency_id.id,
71                     statement.currency.id, line.amount_currency, context=ctx)
72             elif (line.invoice and line.invoice.currency_id.id <> statement.currency.id):
73                 amount = currency_obj.compute(cr, uid, line.invoice.currency_id.id,
74                     statement.currency.id, amount, context=ctx)
75
76             context.update({'move_line_ids': [line.id]})
77             result = voucher_obj.onchange_partner_id(cr, uid, [], partner_id=line.partner_id.id, journal_id=statement.journal_id.id, price=abs(amount), currency_id= statement.currency.id, ttype=(amount < 0 and 'payment' or 'receipt'), context=context)
78             voucher_res = { 'type':(amount < 0 and 'payment' or 'receipt'),
79                             'name': line.name,
80                             'partner_id': line.partner_id.id,
81                             'journal_id': statement.journal_id.id,
82                             'account_id': result.get('account_id', statement.journal_id.default_credit_account_id.id), # improve me: statement.journal_id.default_credit_account_id.id
83                             'company_id':statement.company_id.id,
84                             'currency_id':statement.currency.id,
85                             'date':line.date,
86                             'amount':abs(amount),
87                             'period_id':statement.period_id.id}
88             voucher_id = voucher_obj.create(cr, uid, voucher_res, context=context)
89
90             voucher_line_dict =  {}
91             if result['value']['line_ids']:
92                 for line_dict in result['value']['line_ids']:
93                     move_line = line_obj.browse(cr, uid, line_dict['move_line_id'], context)
94                     if line.move_id.id == move_line.move_id.id:
95                         voucher_line_dict = line_dict
96
97             if voucher_line_dict:
98                 voucher_line_dict.update({'voucher_id': voucher_id})
99                 voucher_line_obj.create(cr, uid, voucher_line_dict, context=context)
100             if line.journal_id.type == 'sale':
101                 type = 'customer'
102             elif line.journal_id.type == 'purchase':
103                 type = 'supplier'
104             else:
105                 type = 'general'
106             statement_line_obj.create(cr, uid, {
107                 'name': line.name or '?',
108                 'amount': amount,
109                 'type': type,
110                 'partner_id': line.partner_id.id,
111                 'account_id': line.account_id.id,
112                 'statement_id': statement_id,
113                 'ref': line.ref,
114                 'voucher_id': voucher_id,
115                 'date': time.strftime('%Y-%m-%d'), #time.strftime('%Y-%m-%d'), #line.date_maturity or,
116             }, context=context)
117         return {}
118
119 account_statement_from_invoice_lines()
120
121 class account_statement_from_invoice(osv.osv_memory):
122     """
123     Generate Entries by Statement from Invoices
124     """
125     _name = "account.statement.from.invoice"
126     _description = "Entries by Statement from Invoices"
127     _columns = {
128         'date': fields.date('Date payment',required=True),
129         'journal_ids': fields.many2many('account.journal', 'account_journal_relation', 'account_id', 'journal_id', 'Journal'),
130         'line_ids': fields.many2many('account.move.line', 'account_move_line_relation', 'move_id', 'line_id', 'Invoices'),
131     }
132     _defaults = {
133         'date': lambda *a: time.strftime('%Y-%m-%d'),
134     }
135
136     def search_invoices(self, cr, uid, ids, context=None):
137
138         line_obj = self.pool.get('account.move.line')
139         statement_obj = self.pool.get('account.bank.statement')
140         journal_obj = self.pool.get('account.journal')
141         mod_obj = self.pool.get('ir.model.data')
142         statement_id = 'statement_id' in context and context['statement_id']
143
144         data =  self.read(cr, uid, ids, context=context)[0]
145         statement = statement_obj.browse(cr, uid, statement_id, context=context)
146         args_move_line = []
147         repeated_move_line_ids = []
148         # Creating a group that is unique for importing move lines(move lines, once imported into statement lines, should not appear again)
149         for st_line in statement.line_ids:
150             args_move_line = []
151             args_move_line.append(('name', '=', st_line.name))
152             args_move_line.append(('ref', '=', st_line.ref))
153             if st_line.partner_id:
154                 args_move_line.append(('partner_id', '=', st_line.partner_id.id))
155             args_move_line.append(('account_id', '=', st_line.account_id.id))
156
157             move_line_id = line_obj.search(cr, uid, args_move_line, context=context)
158             if move_line_id:
159                 repeated_move_line_ids += move_line_id
160
161         journal_ids = data['journal_ids']
162         if journal_ids == []:
163             journal_ids = journal_obj.search(cr, uid, [('type', 'in', ('sale', 'cash', 'purchase'))], context=context)
164
165         args = [
166             ('reconcile_id', '=', False),
167             ('journal_id', 'in', journal_ids),
168             ('account_id.reconcile', '=', True)]
169
170         if repeated_move_line_ids:
171             args.append(('id', 'not in', repeated_move_line_ids))
172
173         line_ids = line_obj.search(cr, uid, args,
174             context=context)
175
176         model_data_ids = mod_obj.search(cr, uid, [('model', '=', 'ir.ui.view'), ('name', '=', 'view_account_statement_from_invoice_lines')], context=context)
177         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
178         return {
179             'domain': "[('id','in', ["+','.join([str(x) for x in line_ids])+"])]",
180             'name': _('Import Entries'),
181             'context': context,
182             'view_type': 'form',
183             'view_mode': 'form',
184             'res_model': 'account.statement.from.invoice.lines',
185             'views': [(resource_id,'form')],
186             'type': 'ir.actions.act_window',
187             'target': 'new',
188         }
189
190 account_statement_from_invoice()
191 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: