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) = self::getTimeframeParams();
     // Total sales for period, in default currency
     $total_sales = array('returning_customers' => 0, 'new_customers' => 0, 'total' => 0);
     // Total number of paid orders for the period
     $total_orders = array('returning_customers' => 0, 'new_customers' => 0, 'total' => 0);
     // Max total sales in a single day
     $max_day_sales = 0;
     // Loop over all days of a period that had at least one order paid,
     // and gather data into vars listed above.
     $om = new shopOrderModel();
     $sales_by_day = $om->getSales($start_date, $end_date, $group_by);
     foreach ($sales_by_day as $row) {
         $max_day_sales = max($max_day_sales, (double) $row['total']);
         $total_orders['new_customers'] += (int) $row['customer_first_count'];
         $total_sales['new_customers'] += (double) $row['customer_first_total'];
         $total_orders['total'] += (int) $row['count'];
         $total_sales['total'] += (double) $row['total'];
     }
     $total_sales['returning_customers'] += $total_sales['total'] - $total_sales['new_customers'];
     $total_orders['returning_customers'] += $total_orders['total'] - $total_orders['new_customers'];
     // Data for main chart
     $sales_data = array();
     foreach ($sales_by_day as &$d) {
         $d['total_percent'] = $max_day_sales ? $d['total'] * 100 / ifempty($max_day_sales, 1) : 0;
         $sales_data[] = array($d['date'], $d['total']);
     }
     unset($d);
     $def_cur = wa()->getConfig()->getCurrency();
     $this->view->assign('sales_by_day', $sales_by_day);
     $this->view->assign('sales_data', $sales_data);
     $this->view->assign('group_by', $group_by);
     $this->view->assign('def_cur', $def_cur);
     $this->view->assign('stat', array('total_formatted' => waCurrency::format('%{s}', $total_sales['total'], $def_cur), 'percent_returning' => round($total_sales['returning_customers'] * 100 / ifempty($total_sales['total'], 1), 1), 'percent_new' => round($total_sales['new_customers'] * 100 / ifempty($total_sales['total'], 1), 1), 'avg_total_formatted' => waCurrency::format('%{s}', round($total_sales['total'] / ifempty($total_orders['total'], 1), 1), $def_cur), 'avg_total_new_formatted' => waCurrency::format('%{s}', round($total_sales['new_customers'] / ifempty($total_orders['new_customers'], 1), 2), $def_cur), 'avg_total_returning_formatted' => waCurrency::format('%{s}', round($total_sales['returning_customers'] / ifempty($total_orders['returning_customers'], 1), 2), $def_cur), 'avg_total_daily_formatted' => waCurrency::format('%{s}', $sales_by_day ? round($total_sales['total'] / count($sales_by_day), 2) : 0, $def_cur)));
 }