2
# -*- coding: utf-8 -*-
3
##############################################################################
5
# OpenERP, Open Source Management Solution
6
# Copyright (C) 2012 TeMPO Consulting, MSF. All Rights Reserved
7
# Developer: Olivier DOSSMANN
9
# This program is free software: you can redistribute it and/or modify
10
# it under the terms of the GNU Affero General Public License as
11
# published by the Free Software Foundation, either version 3 of the
12
# License, or (at your option) any later version.
14
# This program is distributed in the hope that it will be useful,
15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU Affero General Public License for more details.
19
# You should have received a copy of the GNU Affero General Public License
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
##############################################################################
25
from osv import fields
27
from base64 import decodestring
28
from tempfile import NamedTemporaryFile
30
from tools.misc import ustr
31
from tools.translate import _
35
class hq_entries_import_wizard(osv.osv_memory):
36
_name = 'hq.entries.import'
37
_description = 'HQ Entries Import Wizard'
40
'file': fields.binary(string="File", filters="*.csv", required=True),
43
def parse_date(self, date):
45
pdate = time.strptime(date, '%d/%m/%y')
47
pdate = time.strptime(date, '%d/%m/%Y')
48
return time.strftime('%Y-%m-%d', pdate)
50
def update_hq_entries(self, cr, uid, line):
52
Import hq entry regarding all elements given in "line"
54
# Seems that some line could be empty
55
if line.count('') == 12:
57
for x in xrange(0,12-len(line)):
62
'user_validated': False,
65
description, reference, date, document_date, account_description, third_party, booking_amount, booking_currency, \
66
cost_center, funding_pool, free1, free2 = line
68
raise osv.except_osv(_('Error'), _('Unknown format.'))
69
### TO USE IF DATE HAVE some JAN or MAR or OCT instead of 01 ####
70
### Set locale 'C' because of period
71
## locale.setlocale(locale.LC_ALL, 'C')
74
raise osv.except_osv(_('Warning'), _('A date is missing!'))
76
line_date = self.parse_date(date)
78
raise osv.except_osv(_('Error'), _('Wrong format for date: %s: %s') % (date, e))
79
period_ids = self.pool.get('account.period').get_period_from_date(cr, uid, line_date)
81
raise osv.except_osv(_('Warning'), _('No open period found for given date: %s') % (line_date,))
82
if len(period_ids) > 1:
83
raise osv.except_osv(_('Warning'), _('More than one period found for given date: %s') % (line_date,))
84
period_id = period_ids[0]
85
vals.update({'period_id': period_id, 'date': line_date})
88
dd = self.parse_date(document_date)
89
vals.update({'document_date': dd})
91
raise osv.except_osv(_('Error'), _('Wrong format for date: %s: %s') % (document_date, e))
93
if account_description:
94
account_data = account_description.split(' ')
95
account_code = account_data and account_data[0] or False
97
raise osv.except_osv(_('Error'), _('No account code found!'))
98
account_ids = self.pool.get('account.account').search(cr, uid, [('code', '=', account_code)])
100
raise osv.except_osv(_('Error'), _('Account code %s doesn\'t exist!') % (account_code,))
101
vals.update({'account_id': account_ids[0], 'account_id_first_value': account_ids[0]})
102
# Retrieve Cost Center and Funding Pool
104
cc_id = self.pool.get('account.analytic.account').search(cr, uid, [('code', '=', cost_center)])
106
raise osv.except_osv(_('Error'), _('Cost Center %s doesn\'t exist!') % (cost_center,))
110
cc_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution', 'analytic_account_project_dummy')[1]
114
fp_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'analytic_distribution', 'analytic_account_msf_private_funds')[1]
117
vals.update({'cost_center_id': cc_id, 'analytic_id': fp_id, 'cost_center_id_first_value': cc_id, 'analytic_id_first_value': fp_id,})
120
vals.update({'name': description})
123
vals.update({'ref': reference})
126
vals.update({'partner_txt': third_party})
129
currency_ids = self.pool.get('res.currency').search(cr, uid, [('name', '=', booking_currency), ('active', 'in', [False, True])])
131
raise osv.except_osv(_('Error'), _('This currency was not found or is not active: %s') % (booking_currency,))
132
if currency_ids and currency_ids[0]:
133
vals.update({'currency_id': currency_ids[0],})
136
vals.update({'amount': booking_amount,})
138
res = self.pool.get('hq.entries').create(cr, uid, vals)
143
def button_validate(self, cr, uid, ids, context=None):
145
Take a CSV file and fetch some informations for HQ Entries
151
# Verify that an HQ journal exists
152
journal_ids = self.pool.get('account.journal').search(cr, uid, [('type', '=', 'hq')])
154
raise osv.except_osv(_('Error'), _('You cannot import HQ entries because no HQ Journal exists.'))
156
# Prepare some values
157
file_ext_separator = '.'
159
message = _("HQ Entries import failed.")
165
# Browse all given wizard
166
for wiz in self.browse(cr, uid, ids):
168
fileobj = NamedTemporaryFile('w+')
169
fileobj.write(decodestring(wiz.file))
170
# now we determine the file format
174
reader = csv.reader(fileobj, delimiter=',', quotechar='"')
177
raise osv.except_osv(_('Error'), _('Problem to read given file.'))
181
# Omit first line that contains columns ' name
188
update = self.update_hq_entries(cr, uid, line)
190
except osv.except_osv, e:
191
errors.append('Line %s, %s'%(nbline, e.value))
195
message = _("HQ Entries import successful")
196
context.update({'message': message})
200
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'msf_homere_interface', 'payroll_import_error')
202
view_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'msf_homere_interface', 'payroll_import_confirmation')
203
view_id = view_id and view_id[1] or False
205
# This is to redirect to HQ Entries Tree View
206
context.update({'from': 'hq_entries_import'})
209
res_id = self.pool.get('hr.payroll.import.confirmation').create(cr, uid, {'created': created, 'total': processed, 'state': 'hq', 'errors':"\n".join(errors), 'nberrors': len(errors)})
212
'name': 'HQ Entries Import Confirmation',
213
'type': 'ir.actions.act_window',
214
'res_model': 'hr.payroll.import.confirmation',
217
'view_id': [view_id],
223
hq_entries_import_wizard()
225
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: