+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# etudesetchantiersidf module for OpenERP, Custom module for Étude et
+# Chantiers île-de-France
+# Copyright (C) 2014-2018 etudesetchantiersidf
+# (<http://etudesetchantiersiledefrance.unarec.org/>)
+#
+# This file is a part of etudesetchantiersidf
+#
+# etudesetchantiersidf is free software: you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or (at
+# your option) any later version.
+#
+# etudesetchantiersidf is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+import StringIO
+import cStringIO
+import base64
+import csv
+import codecs
+import calendar
+
+import openerp.exceptions
+from openerp.osv import osv
+from openerp.tools.translate import _
+from openerp.addons.account_export_csv.wizard.account_export_csv import AccountUnicodeWriter
+
+class AccountUnicodeWriter(AccountUnicodeWriter):
+
+ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
+ # Redirect output to a queue
+ self.queue = cStringIO.StringIO()
+ # created a writer with Excel formating settings
+
+ self.writer = csv.writer(self.queue, dialect=dialect,
+ quoting=csv.QUOTE_NONE, **kwds)
+ self.stream = f
+ self.encoder = codecs.getincrementalencoder(encoding)()
+
+
+class AccountCSVExport(osv.osv_memory):
+ _inherit = "account.csv.export"
+
+ def action_manual_export_ccmx(self, cr, uid, ids, context=None):
+ this = self.browse(cr, uid, ids)[0]
+ rows = self.get_data(cr, uid, ids, "ccmx", context)
+ file_data = StringIO.StringIO()
+ try:
+ writer = AccountUnicodeWriter(file_data)
+ writer.writerows(rows)
+ file_value = file_data.getvalue()
+ self.write(cr, uid, ids,
+ {'data': base64.encodestring(file_value)},
+ context=context)
+ finally:
+ file_data.close()
+ return {
+ 'type': 'ir.actions.act_window',
+ 'res_model': 'account.csv.export',
+ 'view_mode': 'form',
+ 'view_type': 'form',
+ 'res_id': this.id,
+ 'views': [(False, 'form')],
+ 'target': 'new',
+ }
+
+ def _get_header_ccmx(self, cr, uid, ids, context=None):
+ """docstring for _get_header_ccmx"""
+ return [_(u'ID'),
+ _(u'DATE'),
+ _(u'JOURNAL'),
+ _(u'COMPTE'),
+ _(u'LIBELLE'),
+ _(u'REFERENCE'),
+ _(u'DEBIT'),
+ _(u'CREDIT'),
+ _(u'ANALYTIQUE'),
+ ]
+
+ def _get_rows_ccmx(self, cr, uid, ids,
+ fiscalyear_id,
+ period_range_ids,
+ journal_ids,
+ account_ids,
+ context=None):
+ fiscalyear_obj = self.pool.get('account.fiscalyear')
+ fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context)
+ fiscalyear_code = fiscalyear_obj.browse(cr, uid, fiscalyear_id, context=context).code
+ test = 0
+ if len(period_range_ids) != 1:
+ raise openerp.exceptions.Warning(_('You must select ONE period for this export.'))
+ return []
+ else:
+ test += 1
+ if len(journal_ids) != 1:
+ raise openerp.exceptions.Warning(_('You must select ONE journal for this export.'))
+ return []
+ else:
+ test += 1
+ if test == 2:
+ period_obj = self.pool.get('account.period')
+ period_code = period_obj.browse(cr, uid, period_range_ids[0], context=context).code
+ period_id = period_obj.browse(cr, uid, period_range_ids[0], context=context).id
+ mandy = period_code.split("/")
+ nbday = calendar.monthrange(int(mandy[1]), int(mandy[0]))[1]
+ ref_date = "".join((str(nbday), mandy[0], mandy[1][2:4]))
+ journal_obj = self.pool.get('account.journal')
+ journal = journal_obj.browse(cr, uid, journal_ids[0], context=context)
+ journal_id = journal.id
+ try:
+ city = journal.name.split(" - ")[1]
+ libelle = " ".join((journal.name, period_code))
+ reference = " ".join((journal.code[:2], period_code))
+ except IndexError:
+ raise openerp.exceptions.Warning(_('The selected journal is not available for this export type.'))
+ return []
+
+ req = """
+ select '' as id,
+ '%(ref_date)s' as date,
+ j.code as journal,
+ ac.code,
+ '%(libelle)s' as libelle,
+ '%(reference)s' as reference,
+ sum(debit) as sum_debit,
+ sum(credit) as sum_credit
+ from
+ account_move_line as aml,
+ account_account as ac,
+ account_journal as j
+ where
+ aml.account_id = ac.id
+ and aml.journal_id = j.id
+ and period_id = %(period_id)d
+ and journal_id = %(journal_id)d
+ group by
+ j.id,
+ ac.id,
+ ac.code,
+ ac.name
+ order by ac.code
+ """ % {
+ 'ref_date': ref_date,
+ 'libelle': libelle,
+ 'reference': reference,
+ 'fiscalyear_id': fiscalyear_id,
+ 'period_id': period_id,
+ 'journal_id': journal_id,
+ }
+ cr.execute(req)
+ res = cr.fetchall()
+ rows = []
+ for line in res:
+ journal = line[2]
+ code = line[3]
+ debit = line[6]
+ credit = line[7]
+ analytic = journal[2:]
+ "Add analytic account for 6 and 7 classes accounts"
+ if code[:1] in ('6', '7'):
+ line = line + (analytic,)
+ else:
+ line = line + ('',)
+ "Split lines whith debit AND credit amount"
+ if debit and credit:
+ dline = list(line)
+ dline[7] = 0
+ rows.append(dline)
+ cline = list(line)
+ cline[6] = 0
+ rows.append(cline)
+ else:
+ rows.append(list(line))
+ return rows
+
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: