public function execute() { list($start_date, $end_date, $group_by) = shopReportsSalesAction::getTimeframeParams(); $checkout_flow = new shopCheckoutFlowModel(); $stat = $checkout_flow->getStat($start_date, $end_date); $app_settings_model = new waAppSettingsModel(); $this->view->assign(array('stat' => $stat, 'checkout_flow_changed' => $app_settings_model->get('shop', 'checkout_flow_changed', 0))); }
public function execute() { list($start_date, $end_date, $group_by) = shopReportsSalesAction::getTimeframeParams(); // Init by-day arrays with zeroes. $by_day = array(); // Data for main graph: 'yyyy-mm-dd' => array(...) $sales_by_day = array(); // Total sales data $om = new shopOrderModel(); foreach ($om->getSales($start_date, $end_date, $group_by) as $date => $row) { $sales_by_day[$date] = $row['total']; $by_day[$date] = array('date' => $date, 'total_percent' => 0, 'total' => 0); } // Max profit in a single day $max_day_profit = 0; // Totals for period, in default currency $total = array('profit' => 0, 'purchase' => 0, 'shipping' => 0, 'sales' => 0, 'tax' => 0); // Loop over all days of a period that had at least one order paid, // and gather data into vars listed above. foreach ($om->getProfit($start_date, $end_date, $group_by) as $row) { $sales = ifset($sales_by_day[$row['date']], 0); $profit = $sales - $row['purchase'] - $row['shipping'] - $row['tax']; $max_day_profit = max($max_day_profit, $profit); $by_day[$row['date']]['total'] = $profit; $total['sales'] += $sales; $total['profit'] += $profit; $total['purchase'] += $row['purchase']; $total['shipping'] += $row['shipping']; $total['tax'] += $row['tax']; } // Data for main chart $profit_data = array(); foreach ($by_day as &$d) { $d['total_percent'] = $max_day_profit ? $d['total'] * 100 / ifempty($max_day_profit, 1) : 0; $profit_data[] = array($d['date'], $d['total']); } unset($d); // Data for pie chart $pie_data = array(); $pie_total = $total['shipping'] + $total['profit'] + $total['purchase'] + $total['tax']; if ($pie_total) { $pie_data[] = array(_w('Shipping') . ' (' . round($total['shipping'] * 100 / ifempty($pie_total, 1), 1) . '%)', (double) $total['shipping']); $pie_data[] = array(_w('Profit') . ' (' . round($total['profit'] * 100 / ifempty($pie_total, 1), 1) . '%)', (double) $total['profit']); $pie_data[] = array(_w('Product purchases') . ' (' . round($total['purchase'] * 100 / ifempty($pie_total, 1), 1) . '%)', (double) $total['purchase']); $pie_data[] = array(_w('Tax') . ' (' . round($total['tax'] * 100 / ifempty($pie_total, 1), 1) . '%)', (double) $total['tax']); $pie_data = array($pie_data); } $def_cur = wa()->getConfig()->getCurrency(); $this->view->assign('total', $total); $this->view->assign('by_day', $by_day); $this->view->assign('def_cur', $def_cur); $this->view->assign('group_by', $group_by); $this->view->assign('pie_data', $pie_data); $this->view->assign('profit_data', $profit_data); $this->view->assign('avg_profit', $by_day ? round($total['profit'] / count($by_day), 2) : 0); }
public function execute() { list($start_date, $end_date, $group_by) = shopReportsSalesAction::getTimeframeParams(); $mode = waRequest::request('mode', 'sales'); if ($mode !== 'sales') { $mode = 'profit'; } // Top products $pm = new shopProductModel(); $top_products = $pm->getTop(10, $mode, $start_date, $end_date)->fetchAll('id'); $max_val = 0; $product_total_val = 0; foreach ($top_products as &$p) { $p['profit'] = $p['sales'] - $p['purchase']; $p['val'] = $p[$mode]; $max_val = max($p['val'], $max_val); $product_total_val += $p['val']; } foreach ($top_products as &$p) { $p['val_percent'] = round($p['val'] * 100 / ifempty($max_val, 1)); } unset($p); // Top services $sm = new shopServiceModel(); $top_services = $sm->getTop(10, $start_date, $end_date)->fetchAll('id'); $max_val = 0; $service_total_val = 0; foreach ($top_services as $s) { $max_val = max($s['total'], $max_val); $service_total_val += $s['total']; } foreach ($top_services as &$s) { $s['total_percent'] = round($s['total'] * 100 / ifempty($max_val, 1)); } unset($s); // Total sales or pofit for the period $om = new shopOrderModel(); if ($mode == 'sales') { $total_val = $om->getTotalSales($start_date, $end_date); } else { $total_val = $om->getTotalProfit($start_date, $end_date); } // Total sales by product type $sales_by_type = array(); $tm = new shopTypeModel(); $pie_total = 0; foreach ($tm->getSales($start_date, $end_date) as $row) { $sales_by_type[] = array($row['name'], (double) $row['sales']); $pie_total += $row['sales']; } $sales_by_type[] = array(_w('Services'), $service_total_val); $pie_total += $service_total_val; if ($pie_total) { foreach ($sales_by_type as &$row) { $row[0] .= ' (' . round($row[1] * 100 / ifempty($pie_total, 1), 1) . '%)'; } unset($row); } $def_cur = wa()->getConfig()->getCurrency(); $this->view->assign('mode', $mode); $this->view->assign('def_cur', $def_cur); $this->view->assign('total_val', $total_val); $this->view->assign('top_products', $top_products); $this->view->assign('top_services', $top_services); $this->view->assign('product_total_val', $product_total_val); $this->view->assign('service_total_val', $service_total_val); $this->view->assign('pie_data', array($sales_by_type)); }