[PYTHON] ~permit single selection of perdiod and journal and code tuning
[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-2022 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 import datetime
33
34 import openerp.exceptions
35 from openerp.osv import osv
36 from openerp.tools.translate import _
37 from openerp.addons.account_export_csv.wizard.account_export_csv import AccountUnicodeWriter
38
39
40 class AccountUnicodeWriter(AccountUnicodeWriter):
41
42 def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
43 # Redirect output to a queue
44 self.queue = cStringIO.StringIO()
45 # created a writer with Excel formating settings
46
47 self.writer = csv.writer(self.queue, dialect=dialect,
48 quoting=csv.QUOTE_NONE, **kwds)
49 self.stream = f
50 self.encoder = codecs.getincrementalencoder(encoding)()
51
52
53 class AccountCSVExport(osv.osv_memory):
54 _inherit = "account.csv.export"
55
56 def action_manual_export_sage(self, cr, uid, ids, context=None):
57 this = self.browse(cr, uid, ids)[0]
58 rows = self.get_data(cr, uid, ids, "sage", context)
59 file_data = StringIO.StringIO()
60 try:
61 writer = AccountUnicodeWriter(file_data)
62 writer.writerows(rows)
63 file_value = file_data.getvalue()
64 self.write(cr, uid, ids,
65 {'data': base64.encodestring(file_value)},
66 context=context)
67 finally:
68 file_data.close()
69 return {
70 'type': 'ir.actions.act_window',
71 'res_model': 'account.csv.export',
72 'view_mode': 'form',
73 'view_type': 'form',
74 'res_id': this.id,
75 'views': [(False, 'form')],
76 'target': 'new',
77 }
78
79 def _get_header_sage(self, cr, uid, ids, context=None):
80 """docstring for _get_header_sage"""
81 return [_(u'Code du journal'),
82 _(u'N° de pièce'),
83 _(u'Date'),
84 _(u'Compte Général'),
85 _(u'Compte Tiers'),
86 _(u'Général/analytique'),
87 _(u'Compte Analytique'),
88 _(u'Nom du client'),
89 _(u'Débit'),
90 _(u'Crédit'),
91 ]
92
93 def format_row_sage(self, dico, numero_piece, side):
94 """docstring for format_row_sage"""
95 if side == "D":
96 debit = dico['debit']
97 credit = 0
98 if side == "C":
99 debit = 0
100 credit = dico['credit']
101 return [
102 dico['journal_code'],
103 numero_piece,
104 dico['date'],
105 dico['account_code'],
106 dico['compte_tiers'],
107 dico['general_analytic'],
108 dico['analytic_account'],
109 dico['client_name'],
110 debit,
111 credit,
112 ]
113
114 def _get_rows_sage(self, cr, uid, ids,
115 fiscalyear_id,
116 period_range_ids,
117 journal_ids,
118 account_ids=None,
119 context=None):
120 fiscalyear_obj = self.pool.get('account.fiscalyear')
121 fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context)
122 fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context).code
123
124 period_obj = self.pool.get('account.period')
125 journal_obj = self.pool.get('account.journal')
126 account_obj = self.pool.get('account.account')
127 numero_piece = 0
128 prev_period = 0
129 prev_journal = 0
130 cr.execute("""
131 select
132 aml.period_id,
133 aml.journal_id as journal_id,
134 aml.account_id as account_id,
135 sum(aml.debit) as sum_debit,
136 sum(aml.credit) as sum_credit
137 from
138 account_move_line as aml
139 where
140 aml.period_id in %(period_ids)s and
141 aml.journal_id in %(journal_ids)s and
142 aml.account_id in %(account_ids)s
143 group by
144 period_id,
145 journal_id,
146 account_id
147 order by
148 period_id,
149 journal_id,
150 account_id
151 """,
152 {
153 'period_ids': tuple(period_range_ids),
154 'journal_ids': tuple(journal_ids),
155 'account_ids': tuple(account_ids),
156 }
157 )
158 res = cr.fetchall()
159 rows = []
160 for line in res:
161 dico = {}
162 journal = journal_obj.browse(cr, uid, line[1], context=context)
163 dico['journal_code'] = journal.code[0:2]
164 period = period_obj.browse(cr, uid, line[0], context=context)
165 if prev_journal != journal or prev_period != period:
166 numero_piece += 1
167 year = int(period.code[3:7])
168 month = int(period.code[0:2])
169 day = calendar.monthrange(year, month)[1]
170 dico['date'] = datetime.datetime(year, month, day).strftime("%d/%m/%Y")
171 account = account_obj.browse(cr, uid, line[2], context=context)
172 dico['account_code'] = account.code
173 dico['client_name'] = journal.name.split(" - ", 1)[1].upper()
174 if account.type in ("receivable", "payable"):
175 dico['compte_tiers'] = dico['client_name']
176 else:
177 dico['compte_tiers'] = ""
178 dico['general_analytic'] = "G"
179 dico['analytic_account'] = ""
180 dico['debit'] = line[3]
181 dico['credit'] = line[4]
182 if dico['debit'] == dico['credit']:
183 pass
184 else:
185 if dico['debit']:
186 row = self.format_row_sage(dico, numero_piece, "D")
187 rows.append(row)
188 if dico['credit']:
189 row = self.format_row_sage(dico, numero_piece, "C")
190 rows.append(row)
191 if account.type == "other":
192 dico['general_analytic'] = "A"
193 dico['analytic_account'] = journal.code[3:]
194 if dico['debit']:
195 row = self.format_row_sage(dico, numero_piece, "D")
196 rows.append(row)
197 if dico['credit']:
198 row = self.format_row_sage(dico, numero_piece, "C")
199 rows.append(row)
200
201 prev_journal = journal
202 prev_period = period
203 return rows
204
205 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: