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