示例#1
0
 protected function assignReportsData(shopProduct $product)
 {
     $order_model = new shopOrderModel();
     $sales_total = $order_model->getTotalSalesByProduct($product['id'], $product['currency']);
     $this->view->assign('sales', $sales_total);
     $profit = $sales_total['total'];
     $rows = $order_model->getSalesByProduct($product['id']);
     $date = strtotime(date('Y-m-d') . " -30 day");
     $sales_data = array();
     $i = 0;
     while ($date < time()) {
         $date = date('Y-m-d', $date);
         $sales_data[] = array($i++, isset($rows[$date]) ? (double) $rows[$date] : 0);
         $date = strtotime($date . " +1 day");
     }
     $this->view->assign('sales_plot_data', array($sales_data));
     if (count($product['skus']) > 1) {
         $sku_sales_data = array();
         $rows = $order_model->getTotalSkuSalesByProduct($product['id'], $product['currency']);
         foreach ($rows as $sku_id => $v) {
             $sku_sales_data[] = array($product['skus'][$sku_id]['name'], (double) $v['total']);
             if (!(double) $v['purchase']) {
                 $profit = false;
             } elseif ($profit) {
                 $profit -= $v['purchase'];
             }
         }
         $this->view->assign('sku_plot_data', array($sku_sales_data));
     } else {
         if ((double) $sales_total['purchase']) {
             $profit -= $sales_total['purchase'];
         } else {
             $profit = false;
         }
     }
     if ($profit) {
         $this->view->assign('profit', $profit);
     }
     $runout = array();
     $sales_rate = 0;
     if ($sales_total['quantity'] >= 3) {
         // < 3 means not enough data for proper statistic
         $sales_rate = $sales_total['quantity'] / 30;
         $runout = $product->getRunout($sales_rate);
     }
     $this->view->assign('runout', $runout);
     $this->view->assign('sales_rate', $sales_rate);
     $stocks_log_model = new shopProductStocksLogModel();
     $stocks_log = $stocks_log_model->getList('*,stock_name,sku_name,product_name', array('where' => array('product_id' => $product->id), 'limit' => 5, 'order' => 'datetime DESC'));
     $this->view->assign('stocks_log', $stocks_log);
 }