Inheritance: extends yii\db\ActiveRecord
 public function actionIndex()
 {
     $last15days = [];
     $last6Month = [];
     $numDataOrder = [];
     // 订单生成数据
     $numDataVolume = [];
     // 营业额数据
     $numDataCompleted = [];
     // 订单完成数据
     $numDataVolumeMonth = [];
     // 每月营业额
     $today = strtotime("00:00:00");
     $todayEnd = strtotime("23:59:59");
     for ($i = 0; $i < 15; $i++) {
         $timestrap = strtotime('-' . $i . ' days', $today);
         $timestrapEnd = strtotime('-' . $i . ' days', $todayEnd);
         $where = ['and', ['store_id' => Yii::$app->user->identity->store_id], ['>=', 'created_at', $timestrap], ['<=', 'created_at', $timestrapEnd]];
         array_unshift($last15days, date('m/d', $timestrap));
         array_unshift($numDataOrder, Order::find()->where($where)->count());
         $data = OrderVolume::find()->select(['sum(volume) AS volume', 'count(*) AS count'])->where($where)->asArray()->one();
         array_unshift($numDataVolume, $data['volume']);
         array_unshift($numDataCompleted, $data['count']);
     }
     for ($i = 0; $i < 6; $i++) {
         $timestrap = strtotime("first day of -{$i} month", $today);
         $timestrapEnd = strtotime("last day of -{$i} month", $todayEnd);
         $where = ['and', ['store_id' => Yii::$app->user->identity->store_id], ['>=', 'created_at', $timestrap], ['<=', 'created_at', $timestrapEnd]];
         array_unshift($last6Month, date('Y/m', $timestrap));
         array_unshift($numDataVolumeMonth, OrderVolume::find()->where($where)->sum('volume'));
     }
     $data2 = OrderVolume::find()->select(['sum(volume) AS volume', 'count(*) AS count'])->where(['store_id' => Yii::$app->user->identity->store_id])->asArray()->one();
     return $this->render('index', ['model' => Yii::$app->user->identity->store, 'last15days' => $last15days, 'last6Month' => $last6Month, 'numDataOrder' => $numDataOrder, 'numDataVolume' => $numDataVolume, 'numDataCompleted' => $numDataCompleted, 'numDataVolumeMonth' => $numDataVolumeMonth, 'countOrder' => Order::getCountByStoreId(Yii::$app->user->identity->store_id), 'countCompleted' => $data2['count'], 'sumVolume' => $data2['volume']]);
 }
Example #2
0
 public function complete()
 {
     $this->beforeComplete();
     $transaction = Yii::$app->db->beginTransaction();
     try {
         $this->status = self::STATUS_COMPLETED;
         $this->clearCancelledMsg();
         if (!$this->save(false)) {
             throw new \Exception('订单错误!');
         }
         $orderVolume = new OrderVolume();
         $orderVolume->volume = $this->real_fee;
         $orderVolume->cost = $this->cost;
         $orderVolume->profit = bcsub($this->real_fee, $this->cost, 4);
         $orderVolume->order_id = $this->id;
         $orderVolume->payment = $this->payment;
         $orderVolume->user_id = $this->user_id;
         $orderVolume->store_id = $this->store_id;
         if (!$orderVolume->save(false)) {
             throw new \Exception('记录交易错误!');
         }
         $transaction->commit();
         $this->afterComplete();
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         return false;
     }
 }