Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Copyright 2019 Le Filament (<http://www.le-filament.com>)
# License AGPL-3 or later (http://www.gnu.org/licenses/agpl.html).
import csv
import re
from datetime import datetime
from io import BytesIO, StringIO
from odoo import models, fields, api, http
from odoo.addons.web.controllers.main import serialize_exception
from odoo.addons.web.controllers.main import content_disposition
from odoo.fields import Date
from odoo.http import request
from odoo.tools import pycompat
from odoo.tools.misc import xlwt
HEADER_DEFAULT = [
'date',
'journal',
'compte',
'debit',
'credit',
'libelle',
'piece',
'echeance',
'ref_piece'
]
HEADER_DEFAULT_CEGID = [
'journal',
'date',
'compte',
'auxiliaire',
'ref_piece',
'libelle',
'debit',
'credit',
]
HEADER_DEFAULT_CEGID_FILE = [
'Journal',
'Date',
'Général',
'Auxiliaire',
'Référence',
'Libellé',
'Débit',
'Crédit',
]
class AccountDatasExportWizard(models.TransientModel):
_name = "invoice.line.export"
date_start = fields.Date('Date de début', required=True)
date_end = fields.Date('Date de fin',
required=True,
default=datetime.today().strftime('%Y-%m-%d'))
export_format = fields.Selection([('csv', 'CSV'), ('xls', 'Excel')],
'Format', default="csv")
journal = fields.Selection([('ventes', 'Ventes'),
('achats', 'Achats')],
'Journal', default='ventes')
@api.multi
def get_data_file(self):
nom_outil_compta = self.env['ir.config_parameter'].sudo().get_param(
'account.nom_outil_compta')
return {
'type': 'ir.actions.act_url',
'url':
'/web/export_journal?format=%s&journal=%s&nom_outil_compta=%s'
% (self.export_format, self.journal, nom_outil_compta) +
'&date_start=%s&date_end=%s'
% (self.date_start, self.date_end),
'target': 'new',
}
class AccountDatasExport(http.Controller):
def export_csv(self, lignes_export, columns, filename_, nom_outil_compta):
fp = StringIO()
export_file = csv.writer(fp, delimiter=';', quoting=csv.QUOTE_ALL)
row = []
# Add header line only if outil = cegid
if nom_outil_compta == 'cegid':
header_file = HEADER_DEFAULT_CEGID_FILE
for head in header_file:
row.append(head)
export_file.writerow(row)
for line in lignes_export:
row = []
for column in columns:
if isinstance(line[column], str):
try:
value = line[column]
except UnicodeError:
pass
else:
if ((column == 'date') or (column == 'echeance')
and line[column]):
value = Date.to_date(line[column]).strftime('%d/%m/%Y')
elif (column == 'credit') or (column == 'debit'):
value = str(line[column]).replace('.', ',')
else:
value = line[column]
row.append(value)
export_file.writerow(row)
fp.seek(0)
data = fp.read()
fp.close()
filename = filename_ + '.csv'
csvhttpheaders = [
('Content-Type', 'text/csv;charset=utf8'),
('Content-Disposition', content_disposition(filename)),
]
return request.make_response(data, headers=csvhttpheaders)
def export_xls(self, lignes_export, columns, filename_, nom_outil_compta):
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet(filename_)
if nom_outil_compta == 'cegid':
header_file = HEADER_DEFAULT_CEGID_FILE
else:
header_file = columns
for i, fieldname in enumerate(header_file):
worksheet.write(0, i, fieldname)
worksheet.col(i).width = 8000 # around 220 pixels
base_style = xlwt.easyxf('align: wrap yes')
for row_index, line in enumerate(lignes_export):
for cell_index, column in enumerate(columns):
cell_style = base_style
if ((column == 'date') or (column == 'echeance')
and line[column]):
cell_value = Date.to_date(line[column]).strftime(
'%d/%m/%Y')
elif (column == 'credit') or (column == 'debit'):
cell_value = line[column]
elif isinstance(line[column], pycompat.string_types):
cell_value = re.sub("\r", " ", line[column])
else:
cell_value = line[column]
worksheet.write(row_index + 1, cell_index,
cell_value, cell_style)
fp = BytesIO()
workbook.save(fp)
fp.seek(0)
data = fp.read()
fp.close()
filename = filename_ + '.xls'
xlshttpheaders = [
('Content-Type', 'text/csv;charset=utf8'),
('Content-Disposition', content_disposition(filename)),
]
return request.make_response(data, headers=xlshttpheaders)
def datas_export_ventes(self, format, date_start, date_end,
nom_outil_compta):
if nom_outil_compta == 'ibiza':
columns = HEADER_DEFAULT
journal_value = 'VT'
elif nom_outil_compta == 'cegid':
columns = HEADER_DEFAULT_CEGID
journal_value = 'VEN'
else:
columns = HEADER_DEFAULT
journal_value = 'VT'
# requete
request.cr.execute("""
SELECT l.date,
%s AS journal,
a.code AS compte,
'' AS auxiliaire,
l.debit,
l.credit,
(CASE WHEN l.name = '/' OR l.name = ''
THEN p.name
ELSE CONCAT('Facture ',i.number,' - ',l.name) END) AS libelle,
i.number AS piece,
l.date_maturity AS echeance,
i.number AS ref_piece
FROM account_move_line AS l
LEFT JOIN account_invoice AS i ON l.invoice_id = i.id
LEFT JOIN account_account AS a ON l.account_id = a.id
LEFT JOIN res_partner AS p ON l.partner_id = p.id
WHERE l.journal_id = 1 AND l.date >= %s AND l.date <= %s
ORDER BY l.date, l.move_id, a.code DESC;
""", (journal_value, date_start, date_end))
lignes_export = request.cr.dictfetchall()
company_name = request.env['res.company'].search([('id', '=', 1)]).name
filename_ = (company_name.title().replace(' ', '')
+ 'JournalVentes_' + date_start.replace('-', '')
+ '_' + date_end.replace('-', ''))
if format == 'csv':
return self.export_csv(
lignes_export, columns, filename_, nom_outil_compta)
return self.export_xls(
lignes_export, columns, filename_, nom_outil_compta)
def datas_export_achats(self, format, date_start, date_end,
nom_outil_compta):
if nom_outil_compta == 'ibiza':
columns = HEADER_DEFAULT
journal_value = 'HA'
elif nom_outil_compta == 'cegid':
columns = HEADER_DEFAULT_CEGID
journal_value = 'ACH'
else:
columns = HEADER_DEFAULT
journal_value = 'HA'
request.cr.execute("""
SELECT l.date,
%s AS journal,
a.code AS compte,
'' AS auxiliaire,
l.debit,
l.credit,
(CASE WHEN l.name = '/' OR ''
THEN p.name
ELSE CONCAT('Facture ',i.number,' - ',l.name) END) AS libelle,
i.number AS piece,
i.date_due AS echeance,
i.reference AS ref_piece
FROM account_move_line AS l
LEFT JOIN account_invoice AS i ON l.invoice_id = i.id
LEFT JOIN account_account AS a ON l.account_id = a.id
LEFT JOIN res_partner AS p ON l.partner_id = p.id
WHERE l.journal_id = 2 AND i.type='in_invoice'
AND l.date >= %s AND l.date <= %s
ORDER BY l.date, l.move_id, a.code DESC;
""", (journal_value, date_start, date_end))
lignes_export = request.cr.dictfetchall()
company_name = request.env['res.company'].search([('id', '=', 1)]).name
filename_ = (company_name.title().replace(' ', '')
+ 'JournalAchats_' + date_start.replace('-', '')
+ '_' + date_end.replace('-', ''))
if format == 'csv':
return self.export_csv(
lignes_export, columns, filename_, nom_outil_compta)
return self.export_xls(
lignes_export, columns, filename_, nom_outil_compta)
@http.route('/web/export_journal/', type='http', auth="user")
@serialize_exception
def datas_export(self, format, journal, nom_outil_compta,
date_start, date_end, **kw):
if journal == 'ventes':
return self.datas_export_ventes(format, date_start, date_end,
nom_outil_compta)
elif journal == 'achats':
return self.datas_export_achats(format, date_start, date_end,
nom_outil_compta)