TopTableMixin.render_total_cost()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 3
1
# Copyright 2013 Mathias WOLFF
2
# This file is part of pyfreebilling.
3
#
4
# pyfreebilling is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# pyfreebilling is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with pyfreebilling.  If not, see <http://www.gnu.org/licenses/>
16
17
from django.utils.translation import ugettext_lazy as _
18
from django.utils.safestring import mark_safe
19
20
import django_tables2 as tables
21
from django_tables2.utils import Accessor as A
22
23
from decimal import Decimal
24
25
26
class TopTableMixin(tables.Table):
27
    #customer__name = tables.Column(verbose_name=u'Customer')
28
    total_sell = tables.Column(
29
        verbose_name=u'Sell', attrs={"td": {"class": "text-align: right"}})
30
    total_cost = tables.Column(verbose_name=u'Margin')
31
    #customer__cb_currency__code = tables.Column(verbose_name=u'Currency')
32
    success_calls = tables.Column(verbose_name=u'Nb Succes Calls')
33
    total_calls = tables.Column(verbose_name=u'Nb Calls')
34
    total_duration = tables.Column()
35
    avg_duration = tables.Column(verbose_name=u'ACD')
36
    max_duration = tables.Column(orderable=False)
37
    min_duration = tables.Column(verbose_name=u'ASR')
38
39
    def __init__(self, *args, **kwargs):
40
        super(TopTableMixin, self).__init__(*args, **kwargs)
41
42
    def render_total_cost(self, value, record):
43
        if record['total_sell'] and record['total_cost']:
44
            return '%.2f' % \
45
                float(record['total_sell'] - record['total_cost'])
46
        else:
47
            return '0'
48
49
    def render_avg_duration(self, value, record):
50
        if record['success_calls'] and record['total_duration']:
51
            value = int(record['total_duration'] / record['success_calls'])
52
            return '%02d:%02d:%02d' % \
53
                reduce(lambda ll, b: divmod(ll[0], b) + ll[1:],
54
                       [(value,), 60, 60])
55
        else:
56
            return 'N/A'
57
58
    def render_total_duration(self, value):
59
        """ return the value in hh:mm:ss """
60
        return '%02d:%02d:%02d' % \
61
            reduce(lambda ll, b: divmod(ll[0], b) + ll[1:],
62
                   [(value,), 60, 60])
63
64
    def render_max_duration(self, value):
65
        """ return the value in hh:mm:ss """
66
        return '%02d:%02d:%02d' % \
67
            reduce(lambda ll, b: divmod(ll[0], b) + ll[1:],
68
                   [(value,), 60, 60])
69
70
    def render_min_duration(self, value, record):
71
        """ return the ASR Value """
72
        if record['success_calls'] and record['total_calls']:
73
            value = Decimal(record['success_calls']) / Decimal(
74
                record['total_calls'])
75
            return '{:.0%}'.format(value)
76
        else:
77
            return 'N/A'
78
79
80
class TopCustTable(TopTableMixin, tables.Table):
81
82
    """ Customers stats table """
83
    customer__name = tables.Column(verbose_name=u'Customer')
84
    customer__cb_currency__code = tables.Column(verbose_name=u'Currency')
85
86
    def render_customer__name(self, value):
87
        """ add a link to destination detail """
88
        return mark_safe("<a href='/extranet/destination_customers_stats/?company=%s'>%s</a>" % (value, value))
89
90
    class Meta:
91
        # bootstrap 2 template from : https://gist.github.com/dyve/5458209
92
        attrs = {"class": "bootstrap-tables2"}
93
        sequence = (
94
            "customer__name", "total_sell", "total_cost", "customer__cb_currency__code",
95
            "total_calls", "success_calls", "min_duration", "total_duration", "avg_duration", "max_duration")
96
97
98
class TopDestCustTable(TopTableMixin, tables.Table):
99
100
    """ Destinations stats table """
101
    destination = tables.Column(verbose_name=u'Destination')
102
103
    def render_destination(self, value):
104
        """ add a link to destination detail """
105
        return mark_safe("<a href='/extranet/customers_stats/?destination=%s'>%s</a>" % (value, value))
106
107
    class Meta:
108
        # bootstrap 2 template from : https://gist.github.com/dyve/5458209
109
        attrs = {"class": "bootstrap-tables2"}
110
        sequence = (
111
            "destination", "total_sell", "total_cost",
112
            "total_calls", "success_calls", "min_duration", "total_duration", "avg_duration", "max_duration")
113
114
115
class TopDestProvTable(TopTableMixin, tables.Table):
116
117
    """ Destinations stats table """
118
    destination = tables.Column(verbose_name=u'Destination')
119
120
    def render_destination(self, value):
121
        """ add a link to destination detail """
122
        return mark_safe("<a href='/extranet/providers_stats/?destination=%s'>%s</a>" % (value, value))
123
124
    class Meta:
125
        # bootstrap 2 template from : https://gist.github.com/dyve/5458209
126
        attrs = {"class": "bootstrap-tables2"}
127
        sequence = (
128
            "destination", "total_sell", "total_cost",
129
            "total_calls", "success_calls", "min_duration", "total_duration", "avg_duration", "max_duration")
130
131
132
class TopProvTable(TopTableMixin, tables.Table):
133
134
    """ Providers stats table """
135
    provider__name = tables.Column(verbose_name=u'Provider')
136
    provider__cb_currency__code = tables.Column(verbose_name=u'Currency')
137
138
    def render_provider__name(self, value):
139
        """ add a link to destination detail """
140
        return mark_safe("<a href='/extranet/destination_providers_stats/?company=%s'>%s</a>" % (value, value))
141
142
    class Meta:
143
        # bootstrap 2 template from : https://gist.github.com/dyve/5458209
144
        attrs = {"class": "bootstrap-tables2"}
145
        sequence = (
146
            "provider__name", "total_sell", "total_cost", "provider__cb_currency__code",
147
            "total_calls", "success_calls", "min_duration", "total_duration", "avg_duration", "max_duration")
148