Esempio n. 1
0
 function actionSpent()
 {
     if ($user = Yii::$app->getRequest()->getQueryParam('user')) {
         if (!$user) {
             return 'Доступ запрещн!';
         }
         if (Yii::$app->getRequest()->getQueryParam('request')) {
             // return Yii::$app->getRequest()->getQueryParam('request');
             $shop = Shop::find()->where(['name' => Yii::$app->getRequest()->getQueryParam('request')])->one();
             if ($shop) {
                 //return var_dump($shop->id);
                 $shop_spent_sum = Bought::find()->select(['id, spent, shop_id, COUNT(*) as cnt, SUM(spent) as sum'])->where(['user_id' => 8, 'shop_id' => $shop->id])->andWhere('id > 28')->one();
                 $shop_last_spent = Bought::find()->where(['user_id' => 8, 'shop_id' => $shop->id])->andWhere('id > 28')->orderBy('id DESC')->limit(30)->all();
                 //return var_dump($shop_spent);
                 return $this->renderPartial('spent_shop', ['spent_sum' => $shop_spent_sum, 'spents' => $shop_last_spent]);
             }
             $product = Products::find()->where(['name' => Yii::$app->getRequest()->getQueryParam('request')])->one();
             if ($product) {
                 //return var_dump($shop->id);
                 $product_spent = Bought::find()->select(['id, spent, shop_id, COUNT(*) as cnt, SUM(spent) as sum'])->where(['user_id' => 8, 'product_id' => $product->id])->andWhere('id > 28')->one();
                 $product_last_spent = Bought::find()->where(['user_id' => 8, 'product_id' => $product->id])->andWhere('id > 28')->orderBy('id DESC')->limit(30)->all();
                 //return var_dump($product_last_spent);
                 return $this->renderPartial('spent_product', ['spent' => $product_spent, 'spents' => $product_last_spent, 'product' => $product->name]);
             }
             $product = Products::find()->where(['name' => Yii::$app->getRequest()->getQueryParam('request')])->one();
             if ($product) {
                 //return var_dump($shop->id);
                 $product_spent = Bought::find()->select(['id, spent, shop_id, COUNT(*) as cnt, SUM(spent) as sum'])->where(['user_id' => 8, 'product_id' => $product->id])->andWhere('id > 28')->one();
                 $product_last_spent = Bought::find()->where(['user_id' => 8, 'product_id' => $product->id])->andWhere('id > 28')->orderBy('id DESC')->limit(30)->all();
                 //return var_dump($product_last_spent);
                 return $this->renderPartial('spent_product', ['spent' => $product_spent, 'spents' => $product_last_spent, 'product' => $product->name]);
             }
             $cat = Categories::find()->where(['name' => Yii::$app->getRequest()->getQueryParam('request')])->one();
             if ($cat) {
                 $sum = 0;
                 $spent_prods = [];
                 $prods = Products::find()->where(['cat_id' => $cat->id])->all();
                 //return var_dump($prods);
                 foreach ($prods as $prod) {
                     $cat_products_ids = implode(',', ArrayHelper::map(Products::find()->where(['cat_id' => $prod->cat->id])->all(), 'id', 'id'));
                     $sum = Bought::find()->select('SUM(spent) as sum')->where('product_id IN (' . $cat_products_ids . ')')->andWhere('id > 28')->scalar();
                     $spent_prods[$prod->name] = Bought::find()->select('SUM(spent) as sum')->where(['user_id' => 8, 'product_id' => $prod->id])->andWhere('id > 28')->scalar();
                 }
                 arsort($spent_prods);
                 //return var_dump($spent_prods);
                 return $this->renderPartial('spent_cat', ['sum' => $sum, 'spent_prods' => $spent_prods, 'cat' => $cat->name]);
             }
         }
         //return 'ok';
         try {
             $spents = Bought::find()->select(['id, spent, product_id, COUNT(*) as cnt, SUM(spent) as sum'])->where(['user_id' => 8])->andWhere('id > 28')->groupBy('product_id')->orderBy('sum DESC')->limit(20)->all();
             $shop_spents = Bought::find()->select(['id, spent, shop_id, COUNT(*) as cnt, SUM(spent) as sum'])->where(['user_id' => 8])->andWhere('id > 28')->groupBy('shop_id')->orderBy('sum DESC')->limit(20)->all();
             $prod_boughts = [];
             $prods = Products::find()->select(['id, cat_id, COUNT(*) as cnt'])->groupBy('cat_id')->all();
             //return var_dump($prods);
             foreach ($prods as $prod) {
                 $cat_products_ids = implode(',', ArrayHelper::map(Products::find()->where(['cat_id' => $prod->cat->id])->all(), 'id', 'id'));
                 $prod_boughts[$prod->cat->name] = Bought::find()->select('SUM(spent) as sum')->where('product_id IN (' . $cat_products_ids . ')')->andWhere('id > 28')->scalar();
             }
             arsort($prod_boughts);
             //return var_dump($prod_boughts);
         } catch (\ErrorException $e) {
             return $e->getMessage();
         }
         return $this->renderPartial('spent', ['spents' => $spents, 'shop_spents' => $shop_spents, 'prod_boughts' => $prod_boughts]);
     }
 }
Esempio n. 2
0
 /**
  * Снимок дня
  * @throws \Exception
  */
 public function actionSnapshot()
 {
     //$data = $this->cut_content($this->get_page("http://redday.ru/moon/"),'maintext', '/sun/sunrise.asp');
     //var_dump($data); exit;
     $start_time = strtotime('now 00:00:00', time() + 7 * 60 * 60);
     $snapshot = new Snapshot();
     $today_mark = DiaryActs::find()->select('SUM(mark)')->where("user_id = 8 and time > " . $start_time)->scalar();
     if ($today_mark) {
         $snapshot->oz = $today_mark;
     } else {
         $snapshot->weight = 0;
     }
     $snapshot->doll = Helper::currencyAdapter(1, 11);
     $snapshot->euro = Helper::currencyAdapter(1, 12);
     if (DiaryRecDayParams::find()->where(['day_param_id' => 1])->orderBy('id DESC')->one()) {
         $snapshot->weight = (double) DiaryRecDayParams::find()->where(['day_param_id' => 1])->orderBy('id DESC')->one()->value;
     } else {
         $snapshot->weight = 0;
     }
     $mish_mark = DiaryActs::find()->select('SUM(mark)')->where("time > {$start_time} and user_id = 11")->scalar();
     if ($mish_mark) {
         $snapshot->mish_oz = $mish_mark;
     } else {
         $snapshot->mish_oz = 0;
     }
     $today_acts = implode(',', ArrayHelper::map(DiaryActs::find()->where("time > {$start_time} and user_id = 8 and model_id = 1")->all(), 'id', 'id'));
     $snapshot->kkal = DiaryAte::find()->select('SUM(kkal)')->where("act_id  IN (" . $today_acts . ")")->scalar();
     $not_curr_sum = Incomes::find()->select('SUM(money)')->where("income_id  IN (1,2,7,10)")->scalar();
     $dollar = Incomes::find()->select('SUM(money)')->where("income_id  = 8")->scalar();
     $euro = Incomes::find()->select('SUM(money)')->where("income_id  = 9")->scalar();
     $snapshot->useful_bal = (int) ($not_curr_sum + Helper::currencyAdapter($dollar, 11) + Helper::currencyAdapter($euro, 12));
     $snapshot->sun_rise = date_sunrise(time(), SUNFUNCS_RET_STRING, 55, 82, 90, 7);
     $snapshot->sun_set = date_sunset(time(), SUNFUNCS_RET_STRING, 55, 82, 90, 7);
     $today_acts_bought = implode(',', ArrayHelper::map(DiaryActs::find()->where("time > {$start_time} and user_id = 8 and model_id = 3")->all(), 'id', 'id'));
     $bought_today = [];
     $sum_spent = 0;
     if ($today_acts_bought) {
         try {
             //return var_dump($bought_today);
             $snapshot->spent = Bought::find()->select('SUM(spent)')->where("act_id  IN (" . $today_acts_bought . ")")->scalar();
         } catch (\ErrorException $e) {
             return $e->getMessage();
         }
         // return var_dump($sum_spent );
     }
     if ($snapshot->save()) {
         $act = new DiaryActs();
         $act->model_id = 12;
         $act->user_id = 8;
         $klavaro_balls = 0;
         $today_acts_of_day_params = implode(',', ArrayHelper::map(DiaryActs::find()->where("time > {$start_time} and user_id = 8 and model_id = 4")->all(), 'id', 'id'));
         if ($today_acts_of_day_params) {
             $klavaro_eng_today = DiaryRecDayParams::find()->where("act_id  IN (" . $today_acts_of_day_params . ") and day_param_id = 22")->one();
             $klavaro_ru_today = DiaryRecDayParams::find()->where("act_id  IN (" . $today_acts_of_day_params . ") and day_param_id = 23")->one();
         }
         if (isset($klavaro_eng_today) && $klavaro_eng_today->value > 95) {
             $klavaro_balls = 1;
         }
         if (isset($klavaro_ru_today) && $klavaro_ru_today->value > 95) {
             $klavaro_balls += 1;
         }
         //$klavaro_balls = ( isset($klavaro_eng_today) ? $klavaro_eng_today->value : 0 ) + ( isset($klavaro_ru_today) ? $klavaro_eng_today->value : 0 );
         //echo $klavaro_balls;
         if (DiaryRecDayParams::find()->where(['day_param_id' => 18])->orderBy('id DESC')->one()) {
             $act->mark = (int) round(DiaryRecDayParams::find()->where(['day_param_id' => 18])->orderBy('id DESC')->one()->value / 2000) + $klavaro_balls;
         } else {
             $act->mark = 0;
         }
         $snapshot->oz += $act->mark;
         $snapshot->update();
         // var_dump($act);
         if ($act->save(false)) {
             echo 'ok';
         }
     }
     //var_dump($snapshot);
 }