|
@@ 1215-1354 (lines=140) @@
|
| 1212 |
|
return response |
| 1213 |
|
|
| 1214 |
|
|
| 1215 |
|
class CostSummaryAdmin(admin.ModelAdmin): |
| 1216 |
|
change_list_template = 'admin/cost_summary_change_list.html' |
| 1217 |
|
date_hierarchy = 'date__date' |
| 1218 |
|
search_fields = ('^provider__name',) |
| 1219 |
|
|
| 1220 |
|
# orderable |
| 1221 |
|
|
| 1222 |
|
list_filter = ( |
| 1223 |
|
'destination', |
| 1224 |
|
'provider__name', |
| 1225 |
|
) |
| 1226 |
|
|
| 1227 |
|
def has_add_permission(self, request, obj=None): |
| 1228 |
|
return False |
| 1229 |
|
|
| 1230 |
|
def has_delete_permission(self, request, obj=None): |
| 1231 |
|
return False |
| 1232 |
|
|
| 1233 |
|
def changelist_view(self, request, extra_context=None): |
| 1234 |
|
response = super(CostSummaryAdmin, self).changelist_view( |
| 1235 |
|
request, |
| 1236 |
|
extra_context=extra_context, |
| 1237 |
|
) |
| 1238 |
|
|
| 1239 |
|
def get_next_in_date_hierarchy(request, date_hierarchy): |
| 1240 |
|
if date_hierarchy + '__day' in request.GET: |
| 1241 |
|
return 'day' |
| 1242 |
|
|
| 1243 |
|
if date_hierarchy + '__month' in request.GET: |
| 1244 |
|
return 'day' |
| 1245 |
|
|
| 1246 |
|
if date_hierarchy + '__year' in request.GET: |
| 1247 |
|
return 'week' |
| 1248 |
|
|
| 1249 |
|
return 'month' |
| 1250 |
|
|
| 1251 |
|
try: |
| 1252 |
|
qs = response.context_data['cl'].queryset |
| 1253 |
|
except (AttributeError, KeyError): |
| 1254 |
|
return response |
| 1255 |
|
|
| 1256 |
|
metrics = { |
| 1257 |
|
'total': Coalesce(Count('id'), V(0)), # not OK |
| 1258 |
|
'total_buy': Coalesce(Sum('total_cost'), V(0)), |
| 1259 |
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
| 1260 |
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
| 1261 |
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
| 1262 |
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
| 1263 |
|
'max_duration': Coalesce(Max('max_duration'), V(0)), |
| 1264 |
|
'min_duration': Coalesce(Min('min_duration'), V(0)), |
| 1265 |
|
} |
| 1266 |
|
|
| 1267 |
|
summary_stats = qs\ |
| 1268 |
|
.values('provider__name')\ |
| 1269 |
|
.annotate(**metrics)\ |
| 1270 |
|
.order_by('-total_buy') |
| 1271 |
|
|
| 1272 |
|
response.context_data['summary'] = list(summary_stats) |
| 1273 |
|
|
| 1274 |
|
# Calculate total line |
| 1275 |
|
summary_totals = dict( |
| 1276 |
|
qs.aggregate(**metrics) |
| 1277 |
|
) |
| 1278 |
|
response.context_data['summary_total'] = summary_totals |
| 1279 |
|
|
| 1280 |
|
period = get_next_in_date_hierarchy( |
| 1281 |
|
request, |
| 1282 |
|
self.date_hierarchy, |
| 1283 |
|
) |
| 1284 |
|
response.context_data['period'] = period |
| 1285 |
|
|
| 1286 |
|
# generate period data for graph |
| 1287 |
|
metrics2 = { |
| 1288 |
|
'total_buy': Coalesce(Sum('total_cost'), V(0)), |
| 1289 |
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
| 1290 |
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
| 1291 |
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
| 1292 |
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
| 1293 |
|
} |
| 1294 |
|
|
| 1295 |
|
summary_over_time = qs.annotate( |
| 1296 |
|
period=Trunc( |
| 1297 |
|
'date__date', |
| 1298 |
|
period, |
| 1299 |
|
output_field=DateTimeField() |
| 1300 |
|
) |
| 1301 |
|
).values('period').annotate(**metrics2).order_by('period') |
| 1302 |
|
|
| 1303 |
|
# generate data for graph |
| 1304 |
|
|
| 1305 |
|
# revenue repartition / 5 customers |
| 1306 |
|
chart_label = [] |
| 1307 |
|
chart_data = [] |
| 1308 |
|
top5sales = 0 |
| 1309 |
|
if len(summary_stats) < 5: |
| 1310 |
|
max_cust = len(summary_stats) |
| 1311 |
|
else: |
| 1312 |
|
max_cust = 5 |
| 1313 |
|
for i in range(max_cust): |
| 1314 |
|
chart_label.append(summary_stats[i]['provider__name']) |
| 1315 |
|
chart_data.append(int(summary_stats[i]['total_buy'])) |
| 1316 |
|
top5sales = top5sales + int(summary_stats[i]['total_buy']) |
| 1317 |
|
|
| 1318 |
|
# Add others to graphs |
| 1319 |
|
chart_label.append(_(u'others')) |
| 1320 |
|
chart_data.append(int(summary_totals['total_buy']) - top5sales) |
| 1321 |
|
|
| 1322 |
|
# transform unicode text to strings |
| 1323 |
|
response.context_data['chart_label'] = map(str, chart_label) |
| 1324 |
|
# Example of datas ["Africa", "Asia", "Europe", "Latin America", "North America"] |
| 1325 |
|
response.context_data['chart_data'] = chart_data |
| 1326 |
|
# Example of datas [2478,5267,734,784,433] |
| 1327 |
|
# response.context_data['chart_color'] = ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"] |
| 1328 |
|
|
| 1329 |
|
# timeseries stats |
| 1330 |
|
callvolume_data = [] |
| 1331 |
|
successcallvolume_data = [] |
| 1332 |
|
timeserie_data = [] |
| 1333 |
|
revenue_data = [] |
| 1334 |
|
margin_data = [] |
| 1335 |
|
for j in range(len(summary_over_time)): |
| 1336 |
|
callvolume_data.append(int(summary_over_time[j]['total_calls'])) |
| 1337 |
|
successcallvolume_data.append(int(summary_over_time[j]['success_calls'])) |
| 1338 |
|
revenue_data.append(int(summary_over_time[j]['total_buy'])) |
| 1339 |
|
margin_data.append(int(summary_over_time[j]['margin'])) |
| 1340 |
|
if period == 'day': |
| 1341 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%m-%d')) |
| 1342 |
|
elif period == 'week': |
| 1343 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%W')) |
| 1344 |
|
else: |
| 1345 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%b')) |
| 1346 |
|
|
| 1347 |
|
response.context_data['callvolume_data'] = callvolume_data |
| 1348 |
|
response.context_data['successcallvolume_data'] = successcallvolume_data |
| 1349 |
|
response.context_data['revenue_data'] = revenue_data |
| 1350 |
|
response.context_data['margin_data'] = margin_data |
| 1351 |
|
response.context_data['timeserie_data'] = timeserie_data |
| 1352 |
|
response.context_data['summary_over_time'] = period |
| 1353 |
|
|
| 1354 |
|
return response |
| 1355 |
|
|
| 1356 |
|
|
| 1357 |
|
#---------------------------------------- |
|
@@ 1075-1212 (lines=138) @@
|
| 1072 |
|
return False |
| 1073 |
|
|
| 1074 |
|
|
| 1075 |
|
class SaleSummaryAdmin(admin.ModelAdmin): |
| 1076 |
|
change_list_template = 'admin/sale_summary_change_list.html' |
| 1077 |
|
date_hierarchy = 'date__date' |
| 1078 |
|
search_fields = ('^customer__name',) |
| 1079 |
|
|
| 1080 |
|
# orderable |
| 1081 |
|
|
| 1082 |
|
list_filter = ( |
| 1083 |
|
'destination', |
| 1084 |
|
'customer__name', |
| 1085 |
|
) |
| 1086 |
|
|
| 1087 |
|
def has_add_permission(self, request, obj=None): |
| 1088 |
|
return False |
| 1089 |
|
|
| 1090 |
|
def has_delete_permission(self, request, obj=None): |
| 1091 |
|
return False |
| 1092 |
|
|
| 1093 |
|
def changelist_view(self, request, extra_context=None): |
| 1094 |
|
response = super(SaleSummaryAdmin, self).changelist_view( |
| 1095 |
|
request, |
| 1096 |
|
extra_context=extra_context, |
| 1097 |
|
) |
| 1098 |
|
|
| 1099 |
|
def get_next_in_date_hierarchy(request, date_hierarchy): |
| 1100 |
|
if date_hierarchy + '__day' in request.GET: |
| 1101 |
|
return 'day' |
| 1102 |
|
|
| 1103 |
|
if date_hierarchy + '__month' in request.GET: |
| 1104 |
|
return 'day' |
| 1105 |
|
|
| 1106 |
|
if date_hierarchy + '__year' in request.GET: |
| 1107 |
|
return 'week' |
| 1108 |
|
|
| 1109 |
|
return 'month' |
| 1110 |
|
|
| 1111 |
|
try: |
| 1112 |
|
qs = response.context_data['cl'].queryset |
| 1113 |
|
except (AttributeError, KeyError): |
| 1114 |
|
return response |
| 1115 |
|
|
| 1116 |
|
metrics = { |
| 1117 |
|
'total': Coalesce(Count('id'), V(0)), # not OK |
| 1118 |
|
'total_sales': Coalesce(Sum('total_sell'), V(0)), |
| 1119 |
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
| 1120 |
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
| 1121 |
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
| 1122 |
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
| 1123 |
|
'max_duration': Coalesce(Max('max_duration'), V(0)), |
| 1124 |
|
'min_duration': Coalesce(Min('min_duration'), V(0)), |
| 1125 |
|
} |
| 1126 |
|
|
| 1127 |
|
summary_stats = qs\ |
| 1128 |
|
.values('customer__name')\ |
| 1129 |
|
.annotate(**metrics)\ |
| 1130 |
|
.order_by('-total_sales') |
| 1131 |
|
|
| 1132 |
|
response.context_data['summary'] = list(summary_stats) |
| 1133 |
|
|
| 1134 |
|
summary_totals = dict( |
| 1135 |
|
qs.aggregate(**metrics) |
| 1136 |
|
) |
| 1137 |
|
response.context_data['summary_total'] = summary_totals |
| 1138 |
|
|
| 1139 |
|
period = get_next_in_date_hierarchy( |
| 1140 |
|
request, |
| 1141 |
|
self.date_hierarchy, |
| 1142 |
|
) |
| 1143 |
|
response.context_data['period'] = period |
| 1144 |
|
|
| 1145 |
|
metrics2 = { |
| 1146 |
|
'total_sales': Coalesce(Sum('total_sell'), V(0)), |
| 1147 |
|
'margin': Coalesce(Sum('total_sell'), V(0)) - Coalesce(Sum('total_cost'), V(0)), |
| 1148 |
|
'total_calls': Coalesce(Sum('total_calls'), V(0)), |
| 1149 |
|
'success_calls': Coalesce(Sum('success_calls'), V(0)), |
| 1150 |
|
'total_duration': Coalesce(Sum('total_duration'), V(0)), |
| 1151 |
|
} |
| 1152 |
|
|
| 1153 |
|
summary_over_time = qs.annotate( |
| 1154 |
|
period=Trunc( |
| 1155 |
|
'date__date', |
| 1156 |
|
period, |
| 1157 |
|
output_field=DateTimeField() |
| 1158 |
|
) |
| 1159 |
|
).values('period').annotate(**metrics2).order_by('period') |
| 1160 |
|
|
| 1161 |
|
# generate data for graph |
| 1162 |
|
|
| 1163 |
|
# revenue repartition / 5 customers |
| 1164 |
|
chart_label = [] |
| 1165 |
|
chart_data = [] |
| 1166 |
|
top5sales = 0 |
| 1167 |
|
if len(summary_stats) < 5: |
| 1168 |
|
max_cust = len(summary_stats) |
| 1169 |
|
else: |
| 1170 |
|
max_cust = 5 |
| 1171 |
|
for i in range(max_cust): |
| 1172 |
|
chart_label.append(summary_stats[i]['customer__name']) |
| 1173 |
|
chart_data.append(int(summary_stats[i]['total_sales'])) |
| 1174 |
|
top5sales = top5sales + int(summary_stats[i]['total_sales']) |
| 1175 |
|
|
| 1176 |
|
# Add others to graphs |
| 1177 |
|
chart_label.append(_(u'others')) |
| 1178 |
|
chart_data.append(int(summary_totals['total_sales']) - top5sales) |
| 1179 |
|
|
| 1180 |
|
# transform unicode text to strings |
| 1181 |
|
response.context_data['chart_label'] = map(str, chart_label) |
| 1182 |
|
# Example of datas ["Africa", "Asia", "Europe", "Latin America", "North America"] |
| 1183 |
|
response.context_data['chart_data'] = chart_data |
| 1184 |
|
# Example of datas [2478,5267,734,784,433] |
| 1185 |
|
# response.context_data['chart_color'] = ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"] |
| 1186 |
|
|
| 1187 |
|
# timeseries stats |
| 1188 |
|
callvolume_data = [] |
| 1189 |
|
successcallvolume_data = [] |
| 1190 |
|
timeserie_data = [] |
| 1191 |
|
revenue_data = [] |
| 1192 |
|
margin_data = [] |
| 1193 |
|
for j in range(len(summary_over_time)): |
| 1194 |
|
callvolume_data.append(int(summary_over_time[j]['total_calls'])) |
| 1195 |
|
successcallvolume_data.append(int(summary_over_time[j]['success_calls'])) |
| 1196 |
|
revenue_data.append(int(summary_over_time[j]['total_sales'])) |
| 1197 |
|
margin_data.append(int(summary_over_time[j]['margin'])) |
| 1198 |
|
if period == 'day': |
| 1199 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%m-%d')) |
| 1200 |
|
elif period == 'week': |
| 1201 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%W')) |
| 1202 |
|
else: |
| 1203 |
|
timeserie_data.append(summary_over_time[j]['period'].strftime('%Y-%b')) |
| 1204 |
|
|
| 1205 |
|
response.context_data['callvolume_data'] = callvolume_data |
| 1206 |
|
response.context_data['successcallvolume_data'] = successcallvolume_data |
| 1207 |
|
response.context_data['revenue_data'] = revenue_data |
| 1208 |
|
response.context_data['margin_data'] = margin_data |
| 1209 |
|
response.context_data['timeserie_data'] = timeserie_data |
| 1210 |
|
response.context_data['summary_over_time'] = period |
| 1211 |
|
|
| 1212 |
|
return response |
| 1213 |
|
|
| 1214 |
|
|
| 1215 |
|
class CostSummaryAdmin(admin.ModelAdmin): |