[PYTHON][VIEW] ~wip
[burette/etudesetchantiersidf.git] / wizard / account_export_csv.py
index 0052b91..2bd51d5 100644 (file)
@@ -3,7 +3,7 @@
 #
 #    etudesetchantiersidf module for OpenERP, Custom module for Étude et
 #    Chantiers île-de-France
-#    Copyright (C) 2014-2019 etudesetchantiersidf
+#    Copyright (C) 2014-2022 etudesetchantiersidf
 #    (<http://etudesetchantiersiledefrance.unarec.org/>)
 #
 #    This file is a part of etudesetchantiersidf
@@ -29,12 +29,14 @@ import base64
 import csv
 import codecs
 import calendar
+import datetime
 
 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):
@@ -51,9 +53,9 @@ class AccountUnicodeWriter(AccountUnicodeWriter):
 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)
@@ -74,113 +76,149 @@ class AccountCSVExport(osv.osv_memory):
             '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):
+        """docstring for _get_header_sage"""
+        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):
+    def _get_rows_sage(self, cr, uid, ids,
+                       fiscalyear_id,
+                       period_range_ids,
+                       journal_ids,
+                       account_ids=None,
+                       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
+
+        period_obj = self.pool.get('account.period')
+        journal_obj = self.pool.get('account.journal')
+        account_obj = self.pool.get('account.account')
+        numero_piece = 0
+        prev_period = 0
+        prev_journal = 0
+        req = """
+            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 = journal_obj.browse(cr, uid, line[1], context=context)
+            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]
+            date = datetime.datetime(year, month, day).strftime("%d/%m/%Y")
+            account = account_obj.browse(cr, uid, line[2], context=context)
+            account_code = account.code
+            client_name = journal.name.split(" - ", 1)[1].upper()
+            if account.type in ("receivable", "payable"):
+                compte_tiers = client_name
+            else:
+                compte_tiers = ""
+            general_analytic = "G"
+            analytic_account = ""
+            debit = line[3]
+            credit = line[4]
+            if debit == credit:
+                pass
+            else:
+                if debit:
+                    rows.append([
+                        journal_code,
+                        numero_piece,
+                        date,
+                        account_code,
+                        compte_tiers,
+                        general_analytic,
+                        analytic_account,
+                        client_name,
+                        debit,
+                        0,
+                        ])
+                if credit:
+                    rows.append([
+                        journal_code,
+                        numero_piece,
+                        date,
+                        account_code,
+                        compte_tiers,
+                        general_analytic,
+                        analytic_account,
+                        client_name,
+                        0,
+                        credit,
+                        ])
+                if account.type == "other":
+                    general_analytic = "A"
+                    analytic_account = journal.code[3:]
+                    if debit:
+                        rows.append([
+                            journal_code,
+                            numero_piece,
+                            date,
+                            account_code,
+                            compte_tiers,
+                            general_analytic,
+                            analytic_account,
+                            client_name,
+                            debit,
+                            0,
+                            ])
+                    if credit:
+                        rows.append([
+                            journal_code,
+                            numero_piece,
+                            date,
+                            account_code,
+                            compte_tiers,
+                            general_analytic,
+                            analytic_account,
+                            client_name,
+                            0,
+                            credit,
+                            ])
+
+
+
+            prev_journal = journal
+            prev_period = period
+        return rows
 
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: