[MERGE] lp:~xrg/openobject-addons/trunk-patch18
[odoo/odoo.git] / addons / account / wizard / account_use_model.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_use_model(osv.osv_memory):
27
28     _name = 'account.use.model'
29     _description = 'Use model'
30     _columns = {
31         'model': fields.many2many('account.model', 'account_use_model_relation', 'account_id', 'model_id', 'Account Model'),
32     }
33
34     def view_init(self, cr , uid , fields_list, context=None):
35         account_model_obj = self.pool.get('account.model')
36         if context is None:
37             context = {}
38         if context.get('active_ids',False):
39             data_model = account_model_obj.browse(cr, uid, context['active_ids'])
40             for model in data_model:
41                 for line in model.lines_id:
42                     if line.date_maturity == 'partner':
43                         if not line.partner_id:
44                             raise osv.except_osv(_('Error !'), _("Maturity date of entry line generated by model line '%s' is based on partner payment term!"\
45                                                                     "\nPlease define partner on it!")%line.name)
46         pass
47
48     def create_entries(self, cr, uid, ids, context=None):
49         account_model_obj = self.pool.get('account.model')
50         account_period_obj = self.pool.get('account.period')
51         account_move_obj = self.pool.get('account.move')
52         account_move_line_obj = self.pool.get('account.move.line')
53         pt_obj = self.pool.get('account.payment.term')
54         mod_obj = self.pool.get('ir.model.data')
55         if context is None:
56             context = {}
57         move_ids = []
58         entry = {}
59         data =  self.read(cr, uid, ids, context=context)[0]
60         record_id = context and context.get('model_line', False) or False
61         if record_id:
62             data_model = account_model_obj.browse(cr, uid, data['model'], context=context)
63         else:
64             data_model = account_model_obj.browse(cr, uid, context['active_ids'], context=context)
65         for model in data_model:
66             entry['name'] = model.name%{'year':time.strftime('%Y'), 'month':time.strftime('%m'), 'date':time.strftime('%d')}
67             period_id = account_period_obj.find(cr, uid, context=context)
68             if not period_id:
69                 raise osv.except_osv(_('No period found !'), _('Unable to find a valid period !'))
70             period_id = period_id[0]
71             move_id = account_move_obj.create(cr, uid, {
72                 'ref': entry['name'],
73                 'period_id': period_id,
74                 'journal_id': model.journal_id.id,
75             })
76             move_ids.append(move_id)
77             for line in model.lines_id:
78                 analytic_account_id = False
79                 if line.analytic_account_id:
80                     if not model.journal_id.analytic_journal_id:
81                         raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (model.journal_id.name,))
82                     analytic_account_id = line.analytic_account_id.id
83                 val = {
84                     'move_id': move_id,
85                     'journal_id': model.journal_id.id,
86                     'period_id': period_id,
87                     'analytic_account_id': analytic_account_id
88                 }
89                 date_maturity = time.strftime('%Y-%m-%d')
90                 if line.date_maturity == 'partner' and line.partner_id and line.partner_id.property_payment_term:
91                     payment_term_id = line.partner_id.property_payment_term.id
92                     pterm_list = pt_obj.compute(cr, uid, payment_term_id, value=1, date_ref=date_maturity)
93                     if pterm_list:
94                         pterm_list = [l[0] for l in pterm_list]
95                         pterm_list.sort()
96                         date_maturity = pterm_list[-1]
97                 val.update({
98                     'name': line.name,
99                     'quantity': line.quantity,
100                     'debit': line.debit,
101                     'credit': line.credit,
102                     'account_id': line.account_id.id,
103                     'move_id': move_id,
104                     'partner_id': line.partner_id.id,
105                     'date': time.strftime('%Y-%m-%d'),
106                     'date_maturity': date_maturity
107                 })
108                 c = context.copy()
109                 c.update({'journal_id': model.journal_id.id,'period_id': period_id})
110                 id_line = account_move_line_obj.create(cr, uid, val, context=c)
111
112         context.update({'move_ids':move_ids})
113         model_data_ids = mod_obj.search(cr, uid,[('model','=','ir.ui.view'),('name','=','view_move_form')], context=context)
114         resource_id = mod_obj.read(cr, uid, model_data_ids, fields=['res_id'], context=context)[0]['res_id']
115         return {
116             'domain': "[('id','in', ["+','.join(map(str,context['move_ids']))+"])]",
117             'name': 'Entries',
118             'view_type': 'form',
119             'view_mode': 'tree,form',
120             'res_model': 'account.move',
121             'views': [(False,'tree'),(resource_id,'form')],
122             'type': 'ir.actions.act_window',
123         }
124
125 account_use_model()
126
127 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: