def add_report_element(request):
    values = json.loads(request.body.decode('utf-8'))
    for (index, elem) in enumerate(values['selectedOutlets']):
        outlet = Element.objects.filter(id=elem).first()
        if (outlet is None):
            return HttpResponse("La sortie d'eau concernée par ce rapport n'a pas été trouvée", status=400)
        if (not has_access(outlet, request)):
            return HttpResponse("Vous n'avez pas les droits sur cet élément de réseau", status=403)
        active = values['isActive']
        if active:
            hour_activity = values['inputHours']
            day_activity = values['inputDays']
            if ((not is_int(hour_activity)) or (not is_int(day_activity))):
                return HttpResponse('Impossible, certains champs devraient être des entiers', status=400)
            data = (values['details'][index]['perCubic'] != 'none')
            if data:
                meters_distr = values['details'][index]['cubic']
                value_meter = values['details'][index]['perCubic']
                recette = values['details'][index]['bill']
                if ((not is_float(meters_distr)) or (not is_float(value_meter)) or (not is_float(recette))):
                    return HttpResponse('Impossible, certains champs devraient être des entiers', status=400)
                report_line = Report(water_outlet=outlet, was_active=active, has_data=data, hours_active=hour_activity, days_active=day_activity, quantity_distributed=meters_distr, price=value_meter, recette=recette)
                if (outlet.type == ElementType.INDIVIDUAL.name):
                    consumer = Consumer.objects.filter(water_outlet=outlet).first()
                    if (consumer is not None):
                        amount = (int(meters_distr) * int(value_meter))
                        creation = date.today()
                        expiration = (creation + relativedelta(months=1))
                        invoice = Invoice(consumer=consumer, water_outlet=outlet, creation=creation, expiration=expiration, amount=amount)
                        invoice.save()
            else:
                report_line = Report(water_outlet=outlet, was_active=active, has_data=data, hours_active=hour_activity, days_active=day_activity)
                if (outlet.type == ElementType.INDIVIDUAL.name):
                    consumer = Consumer.objects.filter(water_outlet=outlet).first()
                    if (consumer is not None):
                        amount = outlet.zone.indiv_base_price
                        creation = date.today()
                        expiration = (creation + relativedelta(months=1))
                        invoice = Invoice(consumer=consumer, water_outlet=outlet, creation=creation, expiration=expiration, amount=amount)
                        invoice.save()
        else:
            report_line = Report(water_outlet=outlet, was_active=active)
        log_element(report_line, request)
    return HttpResponse(status=200)
def add_report_element(request):
    values = json.loads(request.body.decode('utf-8'))
    for (index, elem) in enumerate(values['selectedOutlets']):
        outlet = Element.objects.filter(id=elem).first()
        if (outlet is None):
            return HttpResponse("La sortie d'eau concernée par ce rapport n'a pas été trouvée", status=400)
        if (not has_access(outlet, request)):
            return HttpResponse("Vous n'avez pas les droits sur cet élément de réseau", status=403)
        active = values['isActive']
        if active:
            hour_activity = values['inputHours']
            day_activity = values['inputDays']
            if ((not is_int(hour_activity)) or (not is_int(day_activity))):
                return HttpResponse('Impossible, certains champs devraient être des entiers', status=400)
            data = (values['details'][index]['perCubic'] != 'none')
            if data:
                meters_distr = values['details'][index]['cubic']
                value_meter = values['details'][index]['perCubic']
                recette = values['details'][index]['bill']
                if ((not is_float(meters_distr)) or (not is_float(value_meter)) or (not is_float(recette))):
                    return HttpResponse('Impossible, certains champs devraient être des entiers', status=400)
                report_line = Report(water_outlet=outlet, was_active=active, has_data=data, hours_active=hour_activity, days_active=day_activity, quantity_distributed=meters_distr, price=value_meter, recette=recette)
                if (outlet.type == ElementType.INDIVIDUAL.name):
                    consumer = Consumer.objects.filter(water_outlet=outlet).first()
                    if (consumer is not None):
                        amount = (int(meters_distr) * int(value_meter))
                        creation = date.today()
                        expiration = (creation + relativedelta(months=1))
                        invoice = Invoice(consumer=consumer, water_outlet=outlet, creation=creation, expiration=expiration, amount=amount)
                        invoice.save()
            else:
                report_line = Report(water_outlet=outlet, was_active=active, has_data=data, hours_active=hour_activity, days_active=day_activity)
                if (outlet.type == ElementType.INDIVIDUAL.name):
                    consumer = Consumer.objects.filter(water_outlet=outlet).first()
                    if (consumer is not None):
                        amount = outlet.zone.indiv_base_price
                        creation = date.today()
                        expiration = (creation + relativedelta(months=1))
                        invoice = Invoice(consumer=consumer, water_outlet=outlet, creation=creation, expiration=expiration, amount=amount)
                        invoice.save()
        else:
            report_line = Report(water_outlet=outlet, was_active=active)
        log_element(report_line, request)
    return HttpResponse(status=200)