def dashboard(): current_user = g.user now = datetime.datetime.now() store_id = get_or_set_store_id() orders_stats = dict() orders_stats['today_orders_count'] = Order.count_orders_by_date_and_store(datetime.date.today(), store_id) orders_with_status = Order.find_orders_status_from_date_and_store(datetime.date.today().replace(day=1), store_id) orders_stats['current_month_new_count'] = len( [x for x in orders_with_status if x.status == 'new']) orders_stats['current_month_delivered_count'] = Order.count_delivered_orders_between_date_and_store(store_id, get_first_day_of_month( now).date(), now.date()) customer_stats = dict() today_receptions = Reception.find_all_of_today_in_store(store_id) completed_reception = filter(lambda rx: rx.status == 'completed', today_receptions) customer_stats['total'] = len(today_receptions) if today_receptions else 0 customer_stats['complete'] = len(completed_reception) if completed_reception else 0 customer_stats['instore'] = customer_stats['total'] - customer_stats['complete'] customer_stats['new'] = len([x for x in today_receptions if x.rx_type == 'new']) customer_stats['appt_new'] = len([x for x in today_receptions if x.rx_type == 'appt_new']) customer_stats['appt'] = len([x for x in today_receptions if x.rx_type == 'appt']) customer_stats['other'] = len([x for x in today_receptions if x.rx_type == 'other']) today_appointments = Appointment.find_all_by_date_in_store(datetime.date.today(), store_id) appt_stats = dict() appt_stats['instore'] = len([x for x in today_appointments if x.status != 'cancelled' and x.type == 'instore']) appt_stats['open_instore'] = len([x for x in today_appointments if x.status == 'opened' and x.type == 'instore']) appt_stats['followup'] = len([x for x in today_appointments if x.type == 'followup']) appt_stats['open_followup'] = len([x for x in today_appointments if x.status == 'opened' and x.type == 'followup']) sales = User.get_all_sales_by_store_from_cache(long(store_id)) # make sure the status is correct SalesStatus.reset_all_sales_status(store_id) sales_status = SalesStatus.get_all_sales_status(store_id) total_counts, reception_counts = calc_reception_counts(today_receptions, sales) for sale in sales: sale.status = sales_status.get(str(sale.id), 'free') set_status_label_and_style(sale) sale.rx_count = reception_counts.get(sale.id, {'total': 0, 'new': 0, 'appt_new': 0, 'appt': 0, 'other': 0}) # receptions_per_sale = filter(lambda rx: rx.sales_id == sale.id, today_receptions) # sale.rx_count = len(receptions_per_sale) if receptions_per_sale else 0 # total_rx_count += sale.rx_count sales.sort(key=lambda sale: sale.status) today_orders = Order.find_all_created_today_by_store(store_id) today_orders_count = len(today_orders) if today_orders else 0 current_endpoint = 'user.dashboard' rx_base_url, rx_url_params = get_filter_link('receptions.receptions', now, now, current_endpoint) order_base_url, order_url_params = get_filter_link('orders.orders', now, now, current_endpoint) monthly_order_url_params = order_url_params.copy() monthly_order_url_params.update({'start_date': datetime.date.today().replace(day=1).strftime(DATE_FORMAT)}) monthly_delivered_order_url_params = monthly_order_url_params.copy() monthly_delivered_order_url_params['status_filter'] = 'delivered' monthly_delivered_order_url_params['history'] = 'y' appt_base_url, appt_url_params = get_filter_link('appointments.appts', now, now, current_endpoint) # tbc_orders = Order.find_all_in_status_by_store(store_id, confirmed=0, status='new') return render_template('user/salesmanager/dashboard.html', selected_menu=USER_RT_DATA, orders_stats=orders_stats, appt_stats=appt_stats, customer_stats=customer_stats, sales=sales, today_orders=today_orders, total_count=total_counts, today_orders_count=today_orders_count, sales_receptions=reception_counts, rx_base_url=rx_base_url, rx_url_params=rx_url_params, order_base_url=order_base_url, order_url_params=order_url_params, monthly_order_url_params=monthly_order_url_params, monthly_delivered_order_url_params=monthly_delivered_order_url_params, appt_base_url=appt_base_url, appt_url_params=appt_url_params)
def stats_summary(): now = datetime.datetime.now() today = datetime.date.today() first_day_of_currrent_month = datetime.date.today().replace(day=1) store_id = get_or_set_store_id() current_endpoint = 'mmgmt.stats_summary' orders_stats = dict() orders_stats['today_orders_count'] = Order.count_orders_by_date_and_store( today, store_id) orders_stats[ 'today_delivered_count'] = Order.count_delivered_orders_between_date_and_store( store_id, today, today) orders_stats[ 'current_month_delivered_count'] = Order.count_delivered_orders_between_date_and_store( store_id, first_day_of_currrent_month, today) orders_stats['undelivered_count'] = Order.count_all_new_orders_by_store( store_id) # inv_status = dict() # inv_status['total_inv'] = ? rx_stats = dict() from application.models.reception import Reception today_receptions = Reception.find_all_of_today_in_store(store_id) rx_stats['rx_new'] = len( [rx for rx in today_receptions if rx.rx_type == 'new']) rx_stats['rx_appt_new'] = len( [rx for rx in today_receptions if rx.rx_type == 'appt_new']) rx_stats['rx_appt'] = len( [rx for rx in today_receptions if rx.rx_type == 'appt']) rx_stats['rx_other'] = len( [rx for rx in today_receptions if rx.rx_type == 'other']) rx_stats['rx_total'] = len(today_receptions) rx_stats['rx_instore'] = len( [rx for rx in today_receptions if rx.status != 'completed']) rx_base_url, rx_url_params = get_filter_link('receptions.receptions', now, now, current_endpoint) order_base_url, order_url_params = get_filter_link('orders.orders', now, now, current_endpoint) monthly_delivered_order_url_params = order_url_params.copy() monthly_delivered_order_url_params.update({ 'start_date': datetime.date.today().replace(day=1).strftime(DATE_FORMAT) }) monthly_delivered_order_url_params['status_filter'] = 'delivered' monthly_delivered_order_url_params['history'] = 'y' all_new_order_url_params = order_url_params.copy() all_new_order_url_params['start_date'] = '' all_new_order_url_params['end_date'] = '' all_new_order_url_params['status_filter'] = 'new' return render_template( 'mmgmt/summary.html', today_str=format_date_zh(datetime.date.today()), orders_stats=orders_stats, rx_stats=rx_stats, rx_base_url=rx_base_url, rx_url_params=rx_url_params, order_base_url=order_base_url, order_url_params=order_url_params, monthly_delivered_order_url_params=monthly_delivered_order_url_params, all_new_order_url_params=all_new_order_url_params)