#
# etudesetchantiersidf module for OpenERP, Custom module for Étude et
# Chantiers île-de-France
-# Copyright (C) 2014-2018 etudesetchantiersidf
+# Copyright (C) 2014-2022 etudesetchantiersidf
# (<http://etudesetchantiersiledefrance.unarec.org/>)
#
# This file is a part of etudesetchantiersidf
import csv
import codecs
import calendar
+import datetime
+import string
-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):
class AccountCSVExport(osv.osv_memory):
_inherit = "account.csv.export"
- def action_manual_export_ccmx(self, cr, uid, ids, context=None):
+ def action_manual_export_sage(self, cr, uid, ids, context=None):
this = self.browse(cr, uid, ids)[0]
- rows = self.get_data(cr, uid, ids, "ccmx", context)
+ rows = self.get_data(cr, uid, ids, "sage", context)
file_data = StringIO.StringIO()
try:
writer = AccountUnicodeWriter(file_data)
'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_header_sage(self, cr, uid, ids, context=None):
+ """Return header for SAGE export"""
+ return [_(u'Code du journal'),
+ _(u'N° de pièce'),
+ _(u'Date'),
+ _(u'Compte Général'),
+ _(u'Compte Tiers'),
+ _(u'Général/analytique'),
+ _(u'Compte Analytique'),
+ _(u'Nom du client'),
+ _(u'Débit'),
+ _(u'Crédit'),
]
- 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 []
+ def format_row_sage(self, dico, numero_piece, side):
+ """Return a formatted row for SAGE export"""
+ if side == "D":
+ debit = dico['debit']
+ credit = 0
+ if side == "C":
+ debit = 0
+ credit = dico['credit']
+ return [
+ dico['journal_code'],
+ numero_piece,
+ dico['date'],
+ dico['account_code'],
+ dico['compte_tiers'],
+ dico['general_analytic'],
+ dico['analytic_account'],
+ dico['client_name'],
+ debit,
+ credit,
+ ]
- 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,
+ def _get_rows_sage(self, cr, uid, ids,
+ fiscalyear_id,
+ period_range_ids,
+ journal_ids,
+ account_ids=None,
+ context=None):
+ period_obj = self.pool.get('account.period')
+ journal_obj = self.pool.get('account.journal')
+ account_obj = self.pool.get('account.account')
+ account_type_obj = self.pool.get('account.account.type')
+ numero_piece = 0
+ prev_period = 0
+ prev_journal = 0
+ cr.execute("""
+ select
+ aml.period_id,
+ aml.journal_id as journal_id,
+ aml.account_id as account_id,
+ sum(aml.debit) as sum_debit,
+ sum(aml.credit) as sum_credit
+ from
+ account_move_line as aml
+ where
+ aml.period_id in %(period_ids)s and
+ aml.journal_id in %(journal_ids)s and
+ aml.account_id in %(account_ids)s
+ group by
+ period_id,
+ journal_id,
+ account_id
+ order by
+ period_id,
+ journal_id,
+ account_id
+ """,
+ {
+ 'period_ids': tuple(period_range_ids),
+ 'journal_ids': tuple(journal_ids),
+ 'account_ids': tuple(account_ids),
}
- 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
+ )
+ res = cr.fetchall()
+ rows = []
+ for line in res:
+ dico = {}
+ journal = journal_obj.browse(cr, uid, line[1], context=context)
+ dico['journal_code'] = journal.code[0:2]
+ period = period_obj.browse(cr, uid, line[0], context=context)
+ if prev_journal != journal or prev_period != period:
+ numero_piece += 1
+ year = int(period.code[3:7])
+ month = int(period.code[0:2])
+ day = calendar.monthrange(year, month)[1]
+ dico['date'] = datetime.datetime(year, month, day).strftime(
+ "%d/%m/%Y")
+ account = account_obj.browse(cr, uid, line[2], context=context)
+ dico['account_code'] = account.code
+ preformatted_client_name = journal.name.split(" - ", 1)[1].upper().replace(' ', '')
+ for char in string.punctuation:
+ preformatted_client_name = preformatted_client_name.replace(
+ char, '')
+ dico['client_name'] = preformatted_client_name
+ if account.type in ("receivable", "payable"):
+ dico['compte_tiers'] = dico['client_name']
+ else:
+ dico['compte_tiers'] = ""
+ dico['general_analytic'] = "G"
+ dico['analytic_account'] = ""
+ dico['debit'] = line[3]
+ dico['credit'] = line[4]
+ if dico['debit']:
+ row = self.format_row_sage(dico, numero_piece, "D")
+ rows.append(row)
+ if dico['credit']:
+ row = self.format_row_sage(dico, numero_piece, "C")
+ rows.append(row)
+ account_type = account_type_obj.browse(
+ cr, uid, account.user_type.id, context=context)
+ if account_type.code in ("expense", "income"):
+ dico['general_analytic'] = "A"
+ dico['analytic_account'] = journal.code[3:]
+ if dico['debit']:
+ row = self.format_row_sage(dico, numero_piece, "D")
+ rows.append(row)
+ if dico['credit']:
+ row = self.format_row_sage(dico, numero_piece, "C")
+ rows.append(row)
+
+ prev_journal = journal
+ prev_period = period
+ return rows
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: