views.py 14.4 KB
Newer Older
Administrator committed
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
# coding: utf-8
"""Root main page."""

from outils.common_imports import *
from outils.for_view_imports import *

from django.views.generic import View

from django.views.decorators.csrf import csrf_exempt

from .common import OdooAPI

from .forms import OdooEntityFieldsForm
from .forms import ExportComptaForm
from outils.lib.compta import *
from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook


def test_compta(request):
    generate_quadratus_compatible_file('bidon')
    return HttpResponse('ok')


def index(request):
    u"""Page de présentation."""
    template = loader.get_template('root.html')
    context = {'form': '', 'title': 'Outils La Cagette'}
    response = HttpResponse(template.render(context, request))
    return response

@csrf_exempt
def log_js_error(request):
    """TODO : Prevent DOS by filling the log file."""
    try:
        paris_tz = pytz.timezone('Europe/Paris')
        file = open('outils/js_errors.log', 'a')

        line = [str(datetime.datetime.now(tz=paris_tz)),
                request.POST.get('module'),
                request.META.get('HTTP_USER_AGENT', ''),
                request.POST.get('error')]
        file.write("\t".join(line) + "\n")
        file.close()
    except Exception as e:
        coop_logger.error("log_js_error : %s", str(e))

    return HttpResponse('ok')


def data(request, fn):
    """Return file octet stream, located in data folder.
    Useful when frontend webserver has not direct access to django filesystem
    """
    from django.views.static import serve
    import os.path
    filepath = 'data/'+ fn
    return serve(request, os.path.basename(filepath), os.path.dirname(filepath))

def entity_example(request):
    # 536521 : dernier id accessible pour account.move.line !!
    entity = request.GET.get("entity", "")
    fields = request.GET.get("fields", [])
    id = request.GET.get("id", 0)
    api = OdooAPI()
    cond = []
    if id != '' and int(id) > 0:
        cond.append(['id', '=', int(id)])
    example = api.search_read(entity, cond, fields.split('|'), 20, 0, 'id DESC')
    # commenté à cause de shift.template : AttributeError: \'shift.template\' object has no attribute \'_compute_seats\'
    #example = api.search_read(entity, cond, [], 20)
    # example = api.search_read('shift.template.registration.line', [['registration_id','=',1850]], [], 20)
    response = JsonResponse({'example': example})
    return response

class FieldsView(View):
    u"""Permet de visualiser, pour chaque objet les champs associés."""

    def get(self, request, *args, **kwargs):
        u"""Affichage du formulaire pour récupérer le nom de l'objet"""
        template = loader.get_template('common/fields.html')
        context = {'form': OdooEntityFieldsForm(),
                   'title': 'Attributs accessibles'}

        return HttpResponse(template.render(context, request))

    def post(self, request, *args, **kwargs):
        u"""Nous allons retourner les attributs Odoo correspondants."""
        entity = request.POST.get("entity", "")
        fields = []
        if len(entity) > 1:
            api = OdooAPI()
            fields = api.get_entity_fields(entity)
        template = loader.get_template('common/entity_fields.html')
        context = {'fields': fields, 'entity': entity,
                   'title': 'Odoo Entity fields'}

        return HttpResponse(template.render(context, request))

class ExportCompta(View):
    u"""Traitement du fichier obtenu par export compta d'Odoo."""



    def get(self, request, *args, **kwargs):
        u"""Affichage du formulaire pour récupérer le fichier CSV Odoo."""
        if request.GET.get('phase', None) is None:
            template = loader.get_template('outils/export_compta.html')

            context = {'form': ExportComptaForm(),
                       'title': 'Export compta'}
            if hasattr(settings, 'EXPORT_COMPTA_FORMAT'):
                context['final_format'] = settings.EXPORT_COMPTA_FORMAT

            response = HttpResponse(template.render(context, request))
        else:
            val = None
            try:
                export_id = (int)(request.GET.get('export_id'))

                if request.GET.get('phase') == '1':
                    val = generate_account_export_report(export_id)
                if request.GET.get('phase') == '2':
                    final_format = request.GET.get('final_format', 'csv')
                    val = get_odoo_account_export_report(export_id, final_format)

                response = JsonResponse({'response': val})
            except Exception as e:
                val = str(e)
                response = JsonResponse({'error': val})
        return response



    def post(self, request, *args, **kwargs):
        u"""Month and year are given: a file is generated to fit with Arithmetic software or Quadra"""

        response = generate_odoo_export_file(request.POST.get('from'), request.POST.get('to'))

        return JsonResponse({'response': response})


class ExportPOS(View):
    u"""Export lié POS d'Odoo."""

    def get(self, request, *args, **kwargs):
        u"""Affichage du formulaire pour récupérer le fichier d'Export Odoo."""
        template = loader.get_template('outils/export_pos.html')
        context = {'title': 'Export POS'}

        return HttpResponse(template.render(context, request))

    def __ca_sessions_ng(self, mois):
154
        import re
Administrator committed
155 156 157 158 159 160 161 162 163 164 165 166
        debut = time.time()
        api = OdooAPI()
        res = api.execute('lacagette.pos_payments_export', 'get_pos_payments', {'month' : mois})
        if not ('sessions' in res):
            return HttpResponse('Aucune session trouvée')
        tf_ym = "%Y-%m-%d %H:%M:%S"
        req_month = time.strptime(mois, "%Y-%m")
        kept_sessions_id = []
        totals = {}
        details_lines = []
        for s in res['sessions']:
            if 'min' in s['mm_dates']:
167 168 169 170 171 172 173
                """
                 s['mm_dates']['min'] and s['mm_dates']['max'] could be formatted with milliseconds
                 i.e 2020-12-12 12:38:58.136 (rescue Session)
                 Thus, .xxx has to be removed
                """
                s['mm_dates']['min'] = re.sub(r'\.[0-9]+', '', s['mm_dates']['min'])
                s['mm_dates']['max'] = re.sub(r'\.[0-9]+', '', s['mm_dates']['max'])
Administrator committed
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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                min_date = time.strptime(s['mm_dates']['min'], tf_ym)
                max_date = time.strptime(s['mm_dates']['max'], tf_ym)

                if min_date.tm_mon == max_date.tm_mon and min_date.tm_mon == req_month.tm_mon:
                    y = str(min_date.tm_year)
                    m = min_date.tm_mon
                    d = min_date.tm_mday
                    if m < 10:
                        m = '0' + str(m)
                    else:
                        m = str(m)
                    if d < 10:
                        d = '0' + str(d)
                    else:
                        d = str(d)
                    kept_sessions_id.append(s['id'])
                    key = y + '-' + m + '-' + d
                    if not (key in totals):
                        totals[key] = {'CB': 0, 'CSH': 0, 'CHQ': 0, 'TOTAL': 0}
                    sub_total = 0
                    cb = chq = csh = 0
                    for p in s['payments']:
                        if 'CSH' in p['name']:
                            csh = round(p['total_amount'], 2)
                        elif 'CHEQ' in p['name']:
                            chq = round(p['total_amount'], 2)
                        elif 'CB' in p['name']:
                            cb = round(p['total_amount'], 2)
                        sub_total += round(p['total_amount'], 2)
                    totals[key]['CB'] += cb
                    totals[key]['CSH'] += csh
                    totals[key]['CHQ'] += chq
                    totals[key]['TOTAL'] += round(sub_total, 2)
                    details_lines.append([mois, s['mm_dates']['min'], s['mm_dates']['min'], s['caisse'], s['name'],
                                         cb, csh, chq, sub_total])
        wb = Workbook()
        ws1 = wb.create_sheet("Totaux " + mois, 0)
        ws2 = wb.create_sheet("Détails " + mois, 1)
        ws1.append(['date', 'CB', 'CSH', 'CHQ', 'Total'])
        for day in sorted(totals):
            cb = totals[day]['CB']
            csh = totals[day]['CSH']
            chq = totals[day]['CHQ']
            total = totals[day]['TOTAL']
            ws1.append([day, cb, csh, chq, total])
        ws2.append(['mois', 'min_date', 'max_date', 'Caisse', 'session', 'CB', 'CSH','CHQ', 'total'])
        for row in details_lines:
            ws2.append(row)
        wb_name = 'export_sessions__' + mois + '.xlsx'
        # wb.save('/tmp/' + wb_name)
        response = HttpResponse(content=save_virtual_workbook(wb),
                                content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename=' + wb_name
        return response

    def __ca_sessions(self, mois):
        api = OdooAPI()

        cond = [['stop_at', 'like', mois], ['state', '=', 'closed']]
        fields = ['stop_at', 'statement_ids', 'display_name', 'total_amount', 'order_ids', 'config_id']
        sessions = api.search_read('pos.session', cond, fields, 100000)
        statement_ids = []
        order_ids = []
        for s in sessions:
            statement_ids += s['statement_ids']
            order_ids += s['order_ids']
            s['min_date'] = '2970-01-01 00:00:00'
            s['max_date'] = '1970-01-01 00:00:00'
        # Verifions d'abord que toutes les dates de vente d'une session sont
        # toutes du même jour

        cond = [['order_id', 'in', order_ids]]
        fields = ['create_date', 'order_id']
        pol = api.search_read('pos.order.line', cond, fields, 100000)
        tf = "%Y-%m-%d %H:%M:%S"
        for l in pol:
            l_date = time.strptime(l['create_date'], tf)
            for s in sessions:
                s_min_date = time.strptime(s['min_date'], tf)
                s_max_date = time.strptime(s['max_date'], tf)
                for oi in s['order_ids']:
                    if (oi == l['order_id'][0]):
                        if (l_date < s_min_date):
                            s['min_date'] = l['create_date']
                        if (l_date > s_max_date):
                            s['max_date'] = l['create_date']

        cond = [['statement_id', 'in', statement_ids]]
        fields = ['pos_statement_id', 'statement_id', 'amount']
        payments = api.search_read('account.bank.statement.line', cond, fields, 100000)
        payments_statements = {}
        for p in payments:
            if not (p['statement_id'][0] in payments_statements):
                payments_statements[p['statement_id'][0]] = {'nom': p['statement_id'][1], 'total' : 0.00}
            payments_statements[p['statement_id'][0]]['total'] += round(p['amount'],2)
        res = []
        for s in sessions:
            line = s
            for p in s['statement_ids']:
                try:
                    line[payments_statements[p]['nom']] =\
                        round(payments_statements[p]['total'], 2)
                except:
                    # line['erreur_statement_id'] = p
                    # Aucun reglement avec le moyen de paiement
                    pass
            del s['statement_ids']
            line['cheval'] = False
            try:
                if (time.strptime(s['min_date'], "%Y-%m-%d") !=
                   time.strptime(s['max_date'], "%Y-%m-%d")):
                    line['cheval'] = True
            except:
                pass
            res.append(line)
        wb = Workbook()
        ws1 = wb.create_sheet("Totaux " + mois, 0)
        ws2 = wb.create_sheet("Détails " + mois, 1)
        ws2.append(['mois', 'min_date', 'max_date', 'Caisse', 'session', 'CB', 'CSH', 'CHQ', 'total'])

        totals = {}
        for r in res:
            if (r['total_amount'] > 0):
                # d = time.strptime(r['min_date'], tf)
                # date = str(d.tm_mday) + '-' + str(d.tm_mon) + '-' + str(d.tm_year)
                date, hours = r['min_date'].split(' ')
                total = round(r['total_amount'], 2)
                cb = csh = chq = 0
                caisse = r['config_id'][1]

                for key in r.keys():
                    if ('CB' in key):
                        cb = r[key]
                    if ('CSH' in key):
                        csh = r[key]
                    if ('CHEQ' in key):
                        chq = r[key]
                if not (date in totals):
                    totals[date] = {'CB': cb, 'CSH': csh, 'CHQ': chq, 'TOTAL': total}
                else:
                    totals[date]['CB'] += cb
                    totals[date]['CSH'] += csh
                    totals[date]['CHQ'] += chq
                    totals[date]['TOTAL'] += total
                line = [mois, r['min_date'], r['max_date'], caisse, r['display_name'],
                        cb, csh, chq, total]
                # writer.writerow(line)
                ws2.append(line)
        ws1.append(['date', 'CB', 'CSH', 'CHQ', 'Total'])

        for day in sorted(totals):
            cb = totals[day]['CB']
            csh = totals[day]['CSH']
            chq = totals[day]['CHQ']
            total = totals[day]['TOTAL']
            ws1.append([day, cb, csh, chq, total])
        wb_name = 'export_sessions__' + mois + '.xlsx'
        # wb.save('/tmp/' + wb_name)
        response = HttpResponse(content=save_virtual_workbook(wb),
                                content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
        response['Content-Disposition'] = 'attachment; filename=' + wb_name
        return response

    def post(self, request, *args, **kwargs):
        u"""Traitement formulaire."""
        month = request.POST.get("month", "")
        response = HttpResponse("Impossible de récupérer le fichier.")
        if len(month) == 0:
            import datetime
            month = datetime.date.today().strftime("%Y-%m")
        else:
            # verifions la validité du mois
            d_elts = month.split('-')
            if len(d_elts) == 2:
                try:
                    y = int(d_elts[0])
                    m = int(d_elts[1])
                    if (y < 100 * m):
                        month = None
                except:
                    month = None
            else:
                month = None

        if not (month is None):
            response = self.__ca_sessions_ng(month)
        return response