Example #1
0
def analyticsPinsets(request, pinset_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id = 1)
    Pin = get_object_or_404(Pinset, id = pinset_id)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(initial=
        {'start_date': datetime.date(year = today.year, month=today.month , day=1),
         'end_date': datetime.date(year = today.year, month=today.month , day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days = 1)
    end_date = datetime.date(year=today.year, month=today.month, day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    start_date_month = start_date
    end_date_month = end_date
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month , day=1)- timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST': # If the form has been submitted...
            form = forms.getDate(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    sql = "SELECT tarifica_call.id,\
        SUM(tarifica_call.cost) AS cost,\
        tarifica_call.dialed_number,\
        SUM(tarifica_call.duration) AS duration, \
        COUNT(dialed_number) AS times_dialed\
        FROM tarifica_call\
        WHERE date > %s AND date < %s AND pinset_number = %s \
        GROUP BY dialed_number ORDER BY SUM(cost) DESC"
    cursor.execute(sql,[start_date,end_date, Pin.pinset_number])
    top_calls = dictfetchall(cursor)[:10]
    data = []
    for n in top_calls :
        data.append([n['dialed_number'], n['cost']])
    cursor.execute('SELECT tarifica_call.id, tarifica_call.cost, tarifica_call.dialed_number, tarifica_call.duration, \
        tarifica_destinationname.name, tarifica_destinationgroup.destination_country AS country, tarifica_call.date AS dat,\
        tarifica_call.date AS time FROM tarifica_call LEFT JOIN tarifica_destinationgroup\
        ON tarifica_call.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date > %s AND date < %s AND pinset_number = %s ORDER BY dat',
        [start_date,end_date, Pin.pinset_number])
    all_calls = dictfetchall(cursor)
    for cost in all_calls:
        cost['dat'] = cost['dat'].strftime('%d %B %Y')
        cost['time'] = cost['time'].strftime('%H:%M:%S')
    year_data = getBarChartInfoByPinForYear(cursor, Pin.id)
    return render(request, 'tarifica/pinsets/analyticsPinsets.html', {
              'user_info' : user_info,
              'all_calls' : all_calls,
              'top_calls' : top_calls,
              'last_month' : last_month,
              'custom' : custom,
              'form': form,
              'pinset' : Pin,
              'data' : json.dumps(data),
              'year_data' : json.dumps(year_data),
              'start_date': start_date,
              'end_date': end_date - datetime.timedelta(days=1),
              })
Example #2
0
def detailPinsets(request, pinset_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id = 1)
    Pin = get_object_or_404(Pinset, id = pinset_id)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(initial=
        {'start_date': datetime.date(year = today.year, month=today.month , day=1),
         'end_date': datetime.date(year = today.year, month=today.month , day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days = 1)
    end_date = datetime.date(year=today.year, month=today.month, day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month , day=1)- timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST': # If the form has been submitted...
            form = forms.getDate(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    cursor.execute('SELECT tarifica_pinsetdestinationdetail.id, SUM(tarifica_pinsetdestinationdetail.cost) AS cost,\
        tarifica_destinationgroup.id AS destid, tarifica_destinationname.name AS destname, \
        tarifica_destinationgroup.destination_country \
        FROM tarifica_pinsetdestinationdetail LEFT JOIN tarifica_destinationgroup \
        ON tarifica_pinsetdestinationdetail.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date >= %s AND date <= %s AND pinset_id = %s GROUP BY destination_group_id \
        ORDER BY cost DESC',
        [start_date,end_date, Pin.id])
    destinations = dictfetchall(cursor)
    for d in destinations: 
        d['destination_country'] = Country(d['destination_country'])
    #print destinations

    cursor.execute('SELECT tarifica_call.id, tarifica_call.cost, tarifica_call.dialed_number, tarifica_call.duration,\
        tarifica_destinationname.name, tarifica_destinationgroup.destination_country AS country, tarifica_call.date AS dat,\
        tarifica_call.date AS time FROM tarifica_call LEFT JOIN tarifica_destinationgroup\
        ON tarifica_call.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date > %s AND date < %s AND pinset_number = %s ORDER BY dat',
        [start_date,end_date, Pin.pinset_number])
    all_calls = dictfetchall(cursor)
    average = 0
    n = 0
    for cost in all_calls:
        average += cost['cost']
        n += 1
        cost['dat'] = cost['dat'].strftime('%d %B %Y')
        cost['time'] = cost['time'].strftime('%H:%M:%S')
    if n: average = average/n
    data = getBarChartInfoByLocale(cursor, Pin.id)
    day_data = getBarChartInfoByPinForMonth(cursor, Pin.id, start_date, end_date)
    return render(request, 'tarifica/pinsets/detailPinsets.html', {
              'user_info' : user_info,
              'destinations' : destinations,
              'all_calls' : all_calls,
              'average' : average,
              'last_month' : last_month,
              'custom' : custom,
              'form': form,
              'pinset' : Pin,
              'data' : json.dumps(data),
              'day_data' : json.dumps(day_data),
              'start_date': start_date,
              'end_date': end_date - datetime.timedelta(days=1),
              })
Example #3
0
def generalPinsets(request, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id = 1)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(initial=
        {'start_date': datetime.date(year = today.year, month=today.month , day=1),
         'end_date': datetime.date(year = today.year, month=today.month , day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days = 1)
    end_date = datetime.date(year=today.year, month=today.month, day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month , day=1)- timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST': # If the form has been submitted...
            form = forms.getDate(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    cursor.execute(
        'SELECT tarifica_pinsetdailydetail.id, SUM(tarifica_pinsetdailydetail.cost) AS cost, \
        tarifica_pinset.pinset_number, tarifica_pinsetdailydetail.pinset_id AS pinid \
        FROM tarifica_pinsetdailydetail LEFT JOIN tarifica_pinset\
        ON tarifica_pinsetdailydetail.pinset_id = tarifica_pinset.id \
        WHERE date > %s AND date < %s GROUP BY pinset_id ORDER BY SUM(cost) DESC',
        [start_date,end_date])
    pinsets = dictfetchall(cursor)[:5]
    #for e in pinsets : print e
    #cursor.execute(
        #'SELECT tarifica_pinsetdailydetail.id, SUM(tarifica_pinsetdailydetail.cost) AS cost, \
        #SUM(tarifica_pinsetdailydetail.total_calls) AS calls , \
        #SUM(tarifica_pinsetdailydetail.total_seconds) AS seconds, tarifica_pinset.pinset_number, \
        #tarifica_pinsetdailydetail.pinset_id AS pinid \
        #FROM tarifica_pinsetdailydetail LEFT JOIN tarifica_pinset\
        #ON tarifica_pinsetdailydetail.pinset_id = tarifica_pinset.id \
        #WHERE date > %s AND date < %s GROUP BY pinset_id ORDER BY SUM(cost) DESC',
        #[start_date,end_date])
    #all_pinsets = dictfetchall(cursor)
    #for a in all_Pinsets: print a

    all_pinsets_objects = Pinset.objects.all()
    sql = 'SELECT SUM(tarifica_pinsetdailydetail.cost) AS cost, \
    SUM(tarifica_pinsetdailydetail.total_calls) AS calls , \
    SUM(tarifica_pinsetdailydetail.total_seconds) AS seconds \
    FROM tarifica_pinsetdailydetail \
    WHERE date > %s AND date < %s AND tarifica_pinsetdailydetail.pinset_id = %s'
    all_pinsets = []
    for e in all_pinsets_objects:
        e_data = {
            'pinset': e,
            'cost': 0,
            'calls': 0,
            'seconds': 0,
        }

        cursor.execute(sql, [start_date,end_date, e.id])
        data = dictfetchall(cursor)[0]
        if data['cost'] is None:
            data['cost'] = 0
        if data['calls'] is None:
            data['calls'] = 0
        if data['seconds'] is None:
            data['seconds'] = 0
        e_data['cost'] = data['cost']
        e_data['calls'] = data['calls']
        e_data['seconds'] = data['seconds']
        all_pinsets.append(e_data)

    #Sorting pinsets by cost:
    all_pinsets = sorted(all_pinsets, key=lambda pinset: pinset['cost'], reverse=True)

    average = 0
    n = 0
    for a in all_pinsets:
        average += a['cost']
        n += 1
    if n: average = average/n

    thisMonth = 'July'
    lastMonth = 'June'
    lastTwoMonths = 'May'

    data = getBarChartInfoByPin(cursor)

    return render(request, 'tarifica/pinsets/generalPinsets.html', {
        'user_info' : user_info,
        'pinsets' : pinsets,
        'all_pinsets' : all_pinsets,
        'average' : average,
        'last_month' : last_month,
        'custom' : custom,
        'form': form,
        'thisMonth': thisMonth,
        'lastMonth': lastMonth,
        'lastTwoMonths': lastTwoMonths,
        'data' : json.dumps(data),
        'start_date': start_date,
        'end_date': end_date - datetime.timedelta(days=1),
    })
Example #4
0
def generalPinsets(request, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id=1)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(
        initial={
            'start_date':
            datetime.date(year=today.year, month=today.month, day=1),
            'end_date':
            datetime.date(year=today.year, month=today.month, day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days=1)
    end_date = datetime.date(year=today.year, month=today.month,
                             day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month,
                              day=1) - timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST':  # If the form has been submitted...
            form = forms.getDate(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    cursor.execute(
        'SELECT tarifica_pinsetdailydetail.id, SUM(tarifica_pinsetdailydetail.cost) AS cost, \
        tarifica_pinset.pinset_number, tarifica_pinsetdailydetail.pinset_id AS pinid \
        FROM tarifica_pinsetdailydetail LEFT JOIN tarifica_pinset\
        ON tarifica_pinsetdailydetail.pinset_id = tarifica_pinset.id \
        WHERE date > %s AND date < %s GROUP BY pinset_id ORDER BY SUM(cost) DESC',
        [start_date, end_date])
    pinsets = dictfetchall(cursor)[:5]
    #for e in pinsets : print e
    #cursor.execute(
    #'SELECT tarifica_pinsetdailydetail.id, SUM(tarifica_pinsetdailydetail.cost) AS cost, \
    #SUM(tarifica_pinsetdailydetail.total_calls) AS calls , \
    #SUM(tarifica_pinsetdailydetail.total_seconds) AS seconds, tarifica_pinset.pinset_number, \
    #tarifica_pinsetdailydetail.pinset_id AS pinid \
    #FROM tarifica_pinsetdailydetail LEFT JOIN tarifica_pinset\
    #ON tarifica_pinsetdailydetail.pinset_id = tarifica_pinset.id \
    #WHERE date > %s AND date < %s GROUP BY pinset_id ORDER BY SUM(cost) DESC',
    #[start_date,end_date])
    #all_pinsets = dictfetchall(cursor)
    #for a in all_Pinsets: print a

    all_pinsets_objects = Pinset.objects.all()
    sql = 'SELECT SUM(tarifica_pinsetdailydetail.cost) AS cost, \
    SUM(tarifica_pinsetdailydetail.total_calls) AS calls , \
    SUM(tarifica_pinsetdailydetail.total_seconds) AS seconds \
    FROM tarifica_pinsetdailydetail \
    WHERE date > %s AND date < %s AND tarifica_pinsetdailydetail.pinset_id = %s'

    all_pinsets = []
    for e in all_pinsets_objects:
        e_data = {
            'pinset': e,
            'cost': 0,
            'calls': 0,
            'seconds': 0,
        }

        cursor.execute(sql, [start_date, end_date, e.id])
        data = dictfetchall(cursor)[0]
        if data['cost'] is None:
            data['cost'] = 0
        if data['calls'] is None:
            data['calls'] = 0
        if data['seconds'] is None:
            data['seconds'] = 0
        e_data['cost'] = data['cost']
        e_data['calls'] = data['calls']
        e_data['seconds'] = data['seconds']
        all_pinsets.append(e_data)

    #Sorting pinsets by cost:
    all_pinsets = sorted(all_pinsets,
                         key=lambda pinset: pinset['cost'],
                         reverse=True)

    average = 0
    n = 0
    for a in all_pinsets:
        average += a['cost']
        n += 1
    if n: average = average / n

    thisMonth = 'July'
    lastMonth = 'June'
    lastTwoMonths = 'May'

    data = getBarChartInfoByPin(cursor)

    return render(
        request, 'tarifica/pinsets/generalPinsets.html', {
            'user_info': user_info,
            'pinsets': pinsets,
            'all_pinsets': all_pinsets,
            'average': average,
            'last_month': last_month,
            'custom': custom,
            'form': form,
            'thisMonth': thisMonth,
            'lastMonth': lastMonth,
            'lastTwoMonths': lastTwoMonths,
            'data': json.dumps(data),
            'start_date': start_date,
            'end_date': end_date - datetime.timedelta(days=1),
        })
Example #5
0
def analyticsPinsets(request, pinset_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id=1)
    Pin = get_object_or_404(Pinset, id=pinset_id)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(
        initial={
            'start_date':
            datetime.date(year=today.year, month=today.month, day=1),
            'end_date':
            datetime.date(year=today.year, month=today.month, day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days=1)
    end_date = datetime.date(year=today.year, month=today.month,
                             day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    start_date_month = start_date
    end_date_month = end_date
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month,
                              day=1) - timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST':  # If the form has been submitted...
            form = forms.getDate(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    sql = "SELECT tarifica_call.id,\
        SUM(tarifica_call.cost) AS cost,\
        tarifica_call.dialed_number,\
        SUM(tarifica_call.duration) AS duration, \
        COUNT(dialed_number) AS times_dialed\
        FROM tarifica_call\
        WHERE date > %s AND date < %s AND pinset_number = %s \
        GROUP BY dialed_number ORDER BY SUM(cost) DESC"

    cursor.execute(sql, [start_date, end_date, Pin.pinset_number])
    top_calls = dictfetchall(cursor)[:10]
    data = []
    for n in top_calls:
        data.append([n['dialed_number'], n['cost']])
    cursor.execute(
        'SELECT tarifica_call.id, tarifica_call.cost, tarifica_call.dialed_number, tarifica_call.duration, \
        tarifica_destinationname.name, tarifica_destinationgroup.destination_country AS country, tarifica_call.date AS dat,\
        tarifica_call.date AS time FROM tarifica_call LEFT JOIN tarifica_destinationgroup\
        ON tarifica_call.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date > %s AND date < %s AND pinset_number = %s ORDER BY dat',
        [start_date, end_date, Pin.pinset_number])
    all_calls = dictfetchall(cursor)
    for cost in all_calls:
        cost['dat'] = cost['dat'].strftime('%d %B %Y')
        cost['time'] = cost['time'].strftime('%H:%M:%S')
    year_data = getBarChartInfoByPinForYear(cursor, Pin.id)
    return render(
        request, 'tarifica/pinsets/analyticsPinsets.html', {
            'user_info': user_info,
            'all_calls': all_calls,
            'top_calls': top_calls,
            'last_month': last_month,
            'custom': custom,
            'form': form,
            'pinset': Pin,
            'data': json.dumps(data),
            'year_data': json.dumps(year_data),
            'start_date': start_date,
            'end_date': end_date - datetime.timedelta(days=1),
        })
Example #6
0
def detailPinsets(request, pinset_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id=1)
    Pin = get_object_or_404(Pinset, id=pinset_id)
    cursor = connection.cursor()
    today = datetime.datetime.now()
    form = forms.getDate(
        initial={
            'start_date':
            datetime.date(year=today.year, month=today.month, day=1),
            'end_date':
            datetime.date(year=today.year, month=today.month, day=today.day),
        })
    last_month = False
    custom = False
    timedelta = datetime.timedelta(days=1)
    end_date = datetime.date(year=today.year, month=today.month,
                             day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1)
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month,
                              day=1) - timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1)
    elif period_id == "custom":
        if request.method == 'POST':  # If the form has been submitted...
            form = forms.getDate(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                start_date = form.cleaned_data['start_date']
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True
    #print start_date.isoformat()
    #print end_date.isoformat()
    cursor.execute(
        'SELECT tarifica_pinsetdestinationdetail.id, SUM(tarifica_pinsetdestinationdetail.cost) AS cost,\
        tarifica_destinationgroup.id AS destid, tarifica_destinationname.name AS destname, \
        tarifica_destinationgroup.destination_country \
        FROM tarifica_pinsetdestinationdetail LEFT JOIN tarifica_destinationgroup \
        ON tarifica_pinsetdestinationdetail.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date >= %s AND date <= %s AND pinset_id = %s GROUP BY destination_group_id \
        ORDER BY cost DESC', [start_date, end_date, Pin.id])
    destinations = dictfetchall(cursor)
    for d in destinations:
        d['destination_country'] = Country(d['destination_country'])
    #print destinations

    cursor.execute(
        'SELECT tarifica_call.id, tarifica_call.cost, tarifica_call.dialed_number, tarifica_call.duration,\
        tarifica_destinationname.name, tarifica_destinationgroup.destination_country AS country, tarifica_call.date AS dat,\
        tarifica_call.date AS time FROM tarifica_call LEFT JOIN tarifica_destinationgroup\
        ON tarifica_call.destination_group_id = tarifica_destinationgroup.id \
        LEFT JOIN tarifica_destinationname ON tarifica_destinationgroup.destination_name_id = tarifica_destinationname.id \
        WHERE date > %s AND date < %s AND pinset_number = %s ORDER BY dat',
        [start_date, end_date, Pin.pinset_number])
    all_calls = dictfetchall(cursor)
    average = 0
    n = 0
    for cost in all_calls:
        average += cost['cost']
        n += 1
        cost['dat'] = cost['dat'].strftime('%d %B %Y')
        cost['time'] = cost['time'].strftime('%H:%M:%S')
    if n: average = average / n
    data = getBarChartInfoByLocale(cursor, Pin.id)
    day_data = getBarChartInfoByPinForMonth(cursor, Pin.id, start_date,
                                            end_date)
    return render(
        request, 'tarifica/pinsets/detailPinsets.html', {
            'user_info': user_info,
            'destinations': destinations,
            'all_calls': all_calls,
            'average': average,
            'last_month': last_month,
            'custom': custom,
            'form': form,
            'pinset': Pin,
            'data': json.dumps(data),
            'day_data': json.dumps(day_data),
            'start_date': start_date,
            'end_date': end_date - datetime.timedelta(days=1),
        })
Example #7
0
def general(request, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    # Required for getting this month's, last month's and custom start and end dates
    user_info = get_object_or_404(UserInformation, id = 1)
    today = datetime.datetime.now()
    form = forms.getDate(initial=
        {'start_date': datetime.date(year = today.year, month=today.month , day=1),
         'end_date': datetime.date(year = today.year, month=today.month , day=today.day),
        }
    )
    timedelta = datetime.timedelta(days = 1)
    custom = False
    last_month = False
    end_date = datetime.date(year=today.year, month=today.month, day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1) - timedelta
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month , day=1)- timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1) - timedelta
    elif period_id == "custom":
        if request.method == 'POST': # If the form has been submitted...
            form = forms.getDate(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                start_date = form.cleaned_data['start_date'] - timedelta
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True

    providers = Provider.objects.filter(is_configured = True)
    averageMonthlyCost = 0
    providersData = []
    providersGraphs = []
    providersGraphsTicks = []
    ticksAssigned = False
    this_month_total_usage = 0
    total_trunks_month_usage = 0
    for p in providers:
        # Obtenemos los periodos de cobro:
        billingPeriods = getBillingPeriods(p)        
        graph_data = []
        for b in billingPeriods:
            if not ticksAssigned:
                providersGraphsTicks.append(b['date_start'].strftime('%b')+" - "+b['date_end'].strftime('%b'))

            averageMonthlyCost += b['total_cost']
            #Agregamos tambien la informacion pa las graphs
            graph_data.append(b['total_cost'])
        ticksAssigned = True

        providersGraphs.append({
            'provider_id': p.id,
            'provider_name': p.name,
            'data': graph_data
        })

        average_cost = averageMonthlyCost / len(billingPeriods)
        this_month_total_usage = getTrunkCurrentIntervalData(p, start_date, end_date)
        print this_month_total_usage
        #print this_month_total_usage
        total_trunks_month_usage += this_month_total_usage['total_cost']
        providersData.append({
            'provider': p, 
            'average_cost': average_cost,
            'this_month_total_usage' : this_month_total_usage,
            'next_month_end_date': p.period_end - 1
        })

    destinationInfo = getAllProvidersDestinationCDR(start_date, end_date)
    #print destinationInfo
    destinationGraph = []
    for d in destinationInfo:
        destinationGraph.append([d['destination_name'], d['total_cost']])

    return render(request, 'tarifica/trunks/trunks.html', {
        'user_info' : user_info,
        'form' : form,
        'custom' : custom,
        'last_month' : last_month,
        'total_trunks_month_usage' : total_trunks_month_usage,
        'providers' : providersData,
        'destinationGraph' : json.dumps(destinationGraph),
        'providersGraphs' : json.dumps(providersGraphs),
        'providersGraphsTicks' : json.dumps(providersGraphsTicks),
        'start_date': start_date + datetime.timedelta(days=1),
        'end_date': end_date - datetime.timedelta(days=1),
    })
Example #8
0
def getTrunk(request, provider_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id = 1)
    provider = get_object_or_404(Provider, id = provider_id)

    # Required for getting this month's, last month's and custom start and end dates
    today = datetime.datetime.now()
    form = forms.getDate(initial=
        {'start_date': datetime.date(year = today.year, month=today.month , day=1),
         'end_date': datetime.date(year = today.year, month=today.month , day=today.day),
        }
    )
    timedelta = datetime.timedelta(days = 1)
    custom = False
    last_month = False
    end_date = datetime.date(year=today.year, month=today.month, day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month, day=1) - timedelta
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month , day=1)- timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month, day=1) - timedelta
    elif period_id == "custom":
        if request.method == 'POST': # If the form has been submitted...
            form = forms.getDate(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                start_date = form.cleaned_data['start_date'] - timedelta
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True

    cost_graph = []
    minutes_graph = []
    billing_period_cost = 0

    # Obtenemos los periodos de cobro:
    billingPeriods = getBillingPeriods(provider)        
    graph_data = []
    for b in billingPeriods:
        minutes = int(b['total_seconds'])
        cost = float(b['total_cost'])
        billing_period_cost += b['total_cost']
        cost_graph.append([
            b['date_start'].strftime('%b')+" - "+b['date_end'].strftime('%b'),
            cost 
        ])
        minutes_graph.append([
            b['date_start'].strftime('%b')+" - "+b['date_end'].strftime('%b'),
            minutes
        ])
    
    this_month_total_usage = getTrunkCurrentIntervalData(provider, start_date, end_date)
    average_monthly_cost = billing_period_cost / len(billingPeriods)

    destinationInfo = getProviderDestinationCDR(provider.id, start_date, end_date)
    destinationGraph = []
    for d in destinationInfo:
        destinationGraph.append([d['destination_name'], d['total_cost']])

    return render(request, 'tarifica/trunks/trunksGet.html', {
        'user_info' : user_info,
        'provider': provider,
        'form' : form,
        'custom' : custom,
        'last_month' : last_month,
        'average_monthly_cost': average_monthly_cost,
        'this_month_total_usage': this_month_total_usage,
        'period_id': period_id,
        'start_date': start_date.strftime("%Y-%m-%d"),
        'end_date': end_date.strftime("%Y-%m-%d"),
        'billingPeriods' : billingPeriods,
        'destinationGraph' : json.dumps(destinationGraph),
        'cost_graph' : json.dumps(cost_graph),
        'minutes_graph' : json.dumps(minutes_graph),
    })
Example #9
0
def general(request, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    # Required for getting this month's, last month's and custom start and end dates
    user_info = get_object_or_404(UserInformation, id=1)
    today = datetime.datetime.now()
    form = forms.getDate(
        initial={
            'start_date':
            datetime.date(year=today.year, month=today.month, day=1),
            'end_date':
            datetime.date(year=today.year, month=today.month, day=today.day),
        })
    timedelta = datetime.timedelta(days=1)
    custom = False
    last_month = False
    end_date = datetime.date(year=today.year, month=today.month,
                             day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month,
                               day=1) - timedelta
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month,
                              day=1) - timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month,
                                   day=1) - timedelta
    elif period_id == "custom":
        if request.method == 'POST':  # If the form has been submitted...
            form = forms.getDate(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                start_date = form.cleaned_data['start_date'] - timedelta
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True

    providers = Provider.objects.filter(is_configured=True)
    averageMonthlyCost = 0
    providersData = []
    providersGraphs = []
    providersGraphsTicks = []
    ticksAssigned = False
    this_month_total_usage = 0
    total_trunks_month_usage = 0
    for p in providers:
        # Obtenemos los periodos de cobro:
        billingPeriods = getBillingPeriods(p)
        graph_data = []
        for b in billingPeriods:
            if not ticksAssigned:
                providersGraphsTicks.append(b['date_start'].strftime('%b') +
                                            " - " +
                                            b['date_end'].strftime('%b'))

            averageMonthlyCost += b['total_cost']
            #Agregamos tambien la informacion pa las graphs
            graph_data.append(b['total_cost'])
        ticksAssigned = True

        providersGraphs.append({
            'provider_id': p.id,
            'provider_name': p.name,
            'data': graph_data
        })

        average_cost = averageMonthlyCost / len(billingPeriods)
        this_month_total_usage = getTrunkCurrentIntervalData(
            p, start_date, end_date)
        print this_month_total_usage
        #print this_month_total_usage
        total_trunks_month_usage += this_month_total_usage['total_cost']
        providersData.append({
            'provider': p,
            'average_cost': average_cost,
            'this_month_total_usage': this_month_total_usage,
            'next_month_end_date': p.period_end - 1
        })

    destinationInfo = getAllProvidersDestinationCDR(start_date, end_date)
    #print destinationInfo
    destinationGraph = []
    for d in destinationInfo:
        destinationGraph.append([d['destination_name'], d['total_cost']])

    return render(
        request, 'tarifica/trunks/trunks.html', {
            'user_info': user_info,
            'form': form,
            'custom': custom,
            'last_month': last_month,
            'total_trunks_month_usage': total_trunks_month_usage,
            'providers': providersData,
            'destinationGraph': json.dumps(destinationGraph),
            'providersGraphs': json.dumps(providersGraphs),
            'providersGraphsTicks': json.dumps(providersGraphsTicks),
            'start_date': start_date + datetime.timedelta(days=1),
            'end_date': end_date - datetime.timedelta(days=1),
        })
Example #10
0
def getTrunk(request, provider_id, period_id="thisMonth"):
    #Syncing extensions, pinsets and trunks
    syncAsteriskInformation()

    user_info = get_object_or_404(UserInformation, id=1)
    provider = get_object_or_404(Provider, id=provider_id)

    # Required for getting this month's, last month's and custom start and end dates
    today = datetime.datetime.now()
    form = forms.getDate(
        initial={
            'start_date':
            datetime.date(year=today.year, month=today.month, day=1),
            'end_date':
            datetime.date(year=today.year, month=today.month, day=today.day),
        })
    timedelta = datetime.timedelta(days=1)
    custom = False
    last_month = False
    end_date = datetime.date(year=today.year, month=today.month,
                             day=today.day) + timedelta
    start_date = datetime.date(year=today.year, month=today.month,
                               day=1) - timedelta
    if period_id == "lastMonth":
        t = datetime.datetime(year=today.year, month=today.month,
                              day=1) - timedelta
        last_month = True
        end_date = datetime.date(year=today.year, month=today.month, day=1)
        start_date = datetime.date(year=t.year, month=t.month,
                                   day=1) - timedelta
    elif period_id == "custom":
        if request.method == 'POST':  # If the form has been submitted...
            form = forms.getDate(request.POST)  # A form bound to the POST data
            if form.is_valid():  # All validation rules pass
                start_date = form.cleaned_data['start_date'] - timedelta
                end_date = form.cleaned_data['end_date'] + timedelta
        custom = True

    cost_graph = []
    minutes_graph = []
    billing_period_cost = 0

    # Obtenemos los periodos de cobro:
    billingPeriods = getBillingPeriods(provider)
    graph_data = []
    for b in billingPeriods:
        minutes = int(b['total_seconds'])
        cost = float(b['total_cost'])
        billing_period_cost += b['total_cost']
        cost_graph.append([
            b['date_start'].strftime('%b') + " - " +
            b['date_end'].strftime('%b'), cost
        ])
        minutes_graph.append([
            b['date_start'].strftime('%b') + " - " +
            b['date_end'].strftime('%b'), minutes
        ])

    this_month_total_usage = getTrunkCurrentIntervalData(
        provider, start_date, end_date)
    average_monthly_cost = billing_period_cost / len(billingPeriods)

    destinationInfo = getProviderDestinationCDR(provider.id, start_date,
                                                end_date)
    destinationGraph = []
    for d in destinationInfo:
        destinationGraph.append([d['destination_name'], d['total_cost']])

    return render(
        request, 'tarifica/trunks/trunksGet.html', {
            'user_info': user_info,
            'provider': provider,
            'form': form,
            'custom': custom,
            'last_month': last_month,
            'average_monthly_cost': average_monthly_cost,
            'this_month_total_usage': this_month_total_usage,
            'period_id': period_id,
            'start_date': start_date.strftime("%Y-%m-%d"),
            'end_date': end_date.strftime("%Y-%m-%d"),
            'billingPeriods': billingPeriods,
            'destinationGraph': json.dumps(destinationGraph),
            'cost_graph': json.dumps(cost_graph),
            'minutes_graph': json.dumps(minutes_graph),
        })