967f05ba7dbb17e40aa3f400d08996fa4e17aa58
[burette/etudesetchantiersidf.git] / wizard / account_export_csv.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 # etudesetchantiersidf module for OpenERP, Custom module for Étude et
5 # Chantiers île-de-France
6 # Copyright (C) 2014-2018 etudesetchantiersidf
7 # (<http://etudesetchantiersiledefrance.unarec.org/>)
8 #
9 # This file is a part of etudesetchantiersidf
10 #
11 # etudesetchantiersidf is free software: you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License as published
13 # by the Free Software Foundation, either version 3 of the License, or (at
14 # your option) any later version.
15 #
16 # etudesetchantiersidf is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #
24 ##############################################################################
25
26 import StringIO
27 import cStringIO
28 import base64
29 import csv
30 import codecs
31 import calendar
32
33 import openerp.exceptions
34 from openerp.osv import osv
35 from openerp.tools.translate import _
36 from openerp.addons.account_export_csv.wizard.account_export_csv import AccountUnicodeWriter
37
38 class AccountUnicodeWriter(AccountUnicodeWriter):
39
40 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
41 # Redirect output to a queue
42 self.queue = cStringIO.StringIO()
43 # created a writer with Excel formating settings
44
45 self.writer = csv.writer(self.queue, dialect=dialect,
46 quoting=csv.QUOTE_NONE, **kwds)
47 self.stream = f
48 self.encoder = codecs.getincrementalencoder(encoding)()
49
50
51 class AccountCSVExport(osv.osv_memory):
52 _inherit = "account.csv.export"
53
54 def action_manual_export_ccmx(self, cr, uid, ids, context=None):
55 this = self.browse(cr, uid, ids)[0]
56 rows = self.get_data(cr, uid, ids, "ccmx", context)
57 file_data = StringIO.StringIO()
58 try:
59 writer = AccountUnicodeWriter(file_data)
60 writer.writerows(rows)
61 file_value = file_data.getvalue()
62 self.write(cr, uid, ids,
63 {'data': base64.encodestring(file_value)},
64 context=context)
65 finally:
66 file_data.close()
67 return {
68 'type': 'ir.actions.act_window',
69 'res_model': 'account.csv.export',
70 'view_mode': 'form',
71 'view_type': 'form',
72 'res_id': this.id,
73 'views': [(False, 'form')],
74 'target': 'new',
75 }
76
77 def _get_header_ccmx(self, cr, uid, ids, context=None):
78 """docstring for _get_header_ccmx"""
79 return [_(u'ID'),
80 _(u'DATE'),
81 _(u'JOURNAL'),
82 _(u'COMPTE'),
83 _(u'LIBELLE'),
84 _(u'REFERENCE'),
85 _(u'DEBIT'),
86 _(u'CREDIT'),
87 _(u'ANALYTIQUE'),
88 ]
89
90 def _get_rows_ccmx(self, cr, uid, ids,
91 fiscalyear_id,
92 period_range_ids,
93 journal_ids,
94 account_ids,
95 context=None):
96 fiscalyear_obj = self.pool.get('account.fiscalyear')
97 fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context)
98 fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context).code
99 test = 0
100 if len(period_range_ids) != 1:
101 raise openerp.exceptions.Warning(_('You must select ONE period for this export.'))
102 return []
103 else:
104 test += 1
105 if len(journal_ids) != 1:
106 raise openerp.exceptions.Warning(_('You must select ONE journal for this export.'))
107 return []
108 else:
109 test += 1
110 if test == 2:
111 period_obj = self.pool.get('account.period')
112 period_code = period_obj.browse(cr, uid, period_range_ids[0], context=context).code
113 period_id = period_obj.browse(cr, uid, period_range_ids[0], context=context).id
114 mandy = period_code.split("/")
115 nbday = calendar.monthrange(int(mandy[1]), int(mandy[0]))[1]
116 ref_date = "".join((str(nbday), mandy[0], mandy[1][2:4]))
117 journal_obj = self.pool.get('account.journal')
118 journal = journal_obj.browse(cr, uid, journal_ids[0], context=context)
119 journal_id = journal.id
120 try:
121 city = journal.name.split(" - ")[1]
122 libelle = " ".join((journal.name, period_code))
123 reference = " ".join((journal.code[:2], period_code))
124 except IndexError:
125 raise openerp.exceptions.Warning(_('The selected journal is not available for this export type.'))
126 return []
127
128 req = """
129 select '' as id,
130 '%(ref_date)s' as date,
131 j.code as journal,
132 ac.code,
133 '%(libelle)s' as libelle,
134 '%(reference)s' as reference,
135 sum(debit) as sum_debit,
136 sum(credit) as sum_credit
137 from
138 account_move_line as aml,
139 account_account as ac,
140 account_journal as j
141 where
142 aml.account_id = ac.id
143 and aml.journal_id = j.id
144 and period_id = %(period_id)d
145 and journal_id = %(journal_id)d
146 group by
147 j.id,
148 ac.id,
149 ac.code,
150 ac.name
151 order by ac.code
152 """ % {
153 'ref_date': ref_date,
154 'libelle': libelle,
155 'reference': reference,
156 'fiscalyear_id': fiscalyear_id,
157 'period_id': period_id,
158 'journal_id': journal_id,
159 }
160 cr.execute(req)
161 res = cr.fetchall()
162 rows = []
163 for line in res:
164 journal = line[2]
165 code = line[3]
166 debit = line[6]
167 credit = line[7]
168 analytic = journal[2:]
169 "Add analytic account for 6 and 7 classes accounts"
170 if code[:1] in ('6', '7'):
171 line = line + (analytic,)
172 else:
173 line = line + ('',)
174 "Split lines whith debit AND credit amount"
175 if debit and credit:
176 dline = list(line)
177 dline[7] = 0
178 rows.append(dline)
179 cline = list(line)
180 cline[6] = 0
181 rows.append(cline)
182 else:
183 rows.append(list(line))
184 return rows
185
186 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: