2017-02-26 22:37:44 +00:00
|
|
|
import datetime
|
2017-02-23 08:22:15 +00:00
|
|
|
from wagtail.wagtailcore import hooks
|
|
|
|
from wagtail.wagtailadmin.site_summary import SummaryItem
|
|
|
|
from longclaw.longclaworders.models import Order
|
|
|
|
from longclaw.longclawproducts.models import Product
|
|
|
|
from longclaw.longclawstats import stats
|
2017-02-26 22:37:44 +00:00
|
|
|
from longclaw.longclawsettings.models import LongclawSettings
|
2017-02-23 08:22:15 +00:00
|
|
|
|
2017-02-26 22:37:44 +00:00
|
|
|
|
|
|
|
class LongclawSummaryItem(SummaryItem):
|
|
|
|
order = 10
|
|
|
|
template = 'longclawstats/summary_item.html'
|
|
|
|
|
|
|
|
def get_context(self):
|
|
|
|
return {
|
|
|
|
'total': 0,
|
|
|
|
'text': '',
|
|
|
|
'url': '',
|
|
|
|
'icon': 'icon-doc-empty-inverse'
|
|
|
|
}
|
|
|
|
|
|
|
|
class OutstandingOrders(LongclawSummaryItem):
|
2017-02-23 08:22:15 +00:00
|
|
|
order = 10
|
|
|
|
def get_context(self):
|
|
|
|
orders = Order.objects.filter(status=Order.SUBMITTED)
|
|
|
|
return {
|
|
|
|
'total': orders.count(),
|
|
|
|
'text': 'Outstanding Orders',
|
2017-02-26 22:37:44 +00:00
|
|
|
'url': '/admin/longclaworders/order/',
|
|
|
|
'icon': 'icon-warning'
|
2017-02-23 08:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:37:44 +00:00
|
|
|
class ProductCount(LongclawSummaryItem):
|
2017-02-23 08:22:15 +00:00
|
|
|
order = 20
|
|
|
|
def get_context(self):
|
|
|
|
return {
|
|
|
|
'total': Product.objects.all().count(),
|
|
|
|
'text': 'Products',
|
2017-02-26 22:37:44 +00:00
|
|
|
'url': '',
|
|
|
|
'icon': 'icon-list-ul'
|
2017-02-23 08:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:37:44 +00:00
|
|
|
class MonthlySales(LongclawSummaryItem):
|
2017-02-23 08:22:15 +00:00
|
|
|
order = 30
|
|
|
|
def get_context(self):
|
2017-02-26 22:37:44 +00:00
|
|
|
settings = LongclawSettings.for_site(self.request.site)
|
2017-02-23 08:22:15 +00:00
|
|
|
sales = stats.sales_for_time_period(*stats.current_month())
|
|
|
|
return {
|
2017-02-26 22:37:44 +00:00
|
|
|
'total': "{}{}".format(settings.currency_html_code,
|
|
|
|
sum(order.total for order in sales)),
|
|
|
|
'text': 'In sales this month',
|
|
|
|
'url': '/admin/longclaworders/order/',
|
|
|
|
'icon': 'icon-tick'
|
2017-02-23 08:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 22:37:44 +00:00
|
|
|
class LongclawStatsPanel(SummaryItem):
|
|
|
|
order = 110
|
|
|
|
template = 'longclawstats/stats_panel.html'
|
|
|
|
def get_context(self):
|
|
|
|
month_start, month_end = stats.current_month()
|
|
|
|
daily_sales = stats.daily_sales(month_start, month_end)
|
|
|
|
labels = [(month_start + datetime.timedelta(days=x)).strftime('%Y-%m-%d')
|
|
|
|
for x in range(0, datetime.datetime.now().day)]
|
|
|
|
daily_income = [0] * len(labels)
|
|
|
|
for k, order_group in daily_sales:
|
|
|
|
i = labels.index(k)
|
|
|
|
daily_income[i] = float(sum(order.total for order in order_group))
|
|
|
|
|
|
|
|
popular_products = stats.sales_by_product(month_start, month_end)[:5]
|
|
|
|
print(popular_products)
|
|
|
|
return {
|
|
|
|
"daily_income": daily_income,
|
|
|
|
"labels": labels,
|
|
|
|
"product_labels": list(popular_products.values_list('title', flat=True)),
|
|
|
|
"sales_volume": list(popular_products.values_list('quantity', flat=True))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-23 08:22:15 +00:00
|
|
|
@hooks.register('construct_homepage_summary_items')
|
|
|
|
def add_longclaw_summary_items(request, items):
|
2017-02-26 22:37:44 +00:00
|
|
|
|
|
|
|
# We are going to replace everything with our own items
|
|
|
|
items[:] = []
|
2017-02-23 08:22:15 +00:00
|
|
|
items.extend([
|
2017-02-26 22:37:44 +00:00
|
|
|
OutstandingOrders(request),
|
2017-02-23 08:22:15 +00:00
|
|
|
ProductCount(request),
|
|
|
|
MonthlySales(request)
|
|
|
|
])
|
2017-02-26 22:37:44 +00:00
|
|
|
|
|
|
|
@hooks.register('construct_homepage_panels')
|
|
|
|
def add_stats_panel(request, panels):
|
|
|
|
return panels.append(LongclawStatsPanel(request))
|