/**
  * Creates a new Comercios model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Comercios();
     if ($model->load(Yii::$app->request->post())) {
         $cantProd = count($model->productos);
         $productos = ArrayHelper::map(Productos::find()->all(), 'idProd', 'nombre');
         /*se crea el stock inicial para el comercio*/
         for ($i = 0; $i < $cantProd; $i++) {
             $id = $model->productos[$i];
             $modelStock = new Stock();
             $modelStock->nombreComercio = $model->nombre;
             $modelStock->cantidadEnStock = 0;
             $modelStock->nombreProducto = $productos[$id];
             $modelStock->save();
         }
         $address = urlencode($model->direccion);
         $url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
         $response = file_get_contents($url);
         $json = json_decode($response, true);
         if ($json['status'] == 'ZERO_RESULTS') {
             return array();
         }
         $model->latitud = json_encode($json['results'][0]['geometry']['location']['lat']);
         $model->longitud = json_encode($json['results'][0]['geometry']['location']['lng']);
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->idComercio]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates a new Stock model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Stock();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Stock::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'cantidad' => $this->cantidad, 'id_producto' => $this->id_producto, 'id_ruta_diaria_com' => $this->id_ruta_diaria_com]);
     return $dataProvider;
 }
Ejemplo n.º 4
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = StockModel::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'trade' => $this->trade, 'pricechange' => $this->pricechange, 'changepercent' => $this->changepercent, 'buy' => $this->buy, 'sell' => $this->sell, 'settlement' => $this->settlement, 'open' => $this->open, 'high' => $this->high, 'low' => $this->low, 'volume' => $this->volume, 'amount' => $this->amount, 'pb' => $this->pb, 'mktcap' => $this->mktcap, 'nmc' => $this->nmc, 'turnoverratio' => $this->turnoverratio, 'ticktime' => $this->ticktime, 'updated_at' => $this->updated_at, 'created_at' => $this->created_at]);
     $query->andFilterWhere(['like', 'symbol', $this->symbol])->andFilterWhere(['like', 'code', $this->code])->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Ejemplo n.º 5
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Stock::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['idStock' => $this->idStock, 'cantidadEnStock' => $this->cantidadEnStock]);
     $query->andFilterWhere(['like', 'nombreComercio', $this->nombreComercio])->andFilterWhere(['like', 'nombreProducto', $this->nombreProducto]);
     return $dataProvider;
 }
 /**
         Devuelve un numero de 0 a 100 que refleja la actividad del Relevador en esta ruta.
      **/
 public function getCompletitudRecorrido()
 {
     if ($this->getRutaDiariaComercios()->count() > 0) {
         $cantidadComercios = $this->getRutaDiariaComercios()->count();
         $rdComercios = $this->getRutaDiariaComercios()->all();
         $comerciosRecorridos = 0;
         foreach ($rdComercios as $rdComercio) {
             if (Stock::find()->where(['id_ruta_diaria_com' => $rdComercio->id])->count() > 0) {
                 $comerciosRecorridos++;
             }
         }
         return $comerciosRecorridos * 100 / $cantidadComercios;
     } else {
         return 0;
     }
 }
 public function findStockComercio($id)
 {
     if ($models = Stock::find()->where(['id_ruta_diaria_com' => $id])->all() !== null) {
         return $models;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionStock($id)
 {
     $idStockDeComercio = array();
     $arrayNombresProductos = array();
     $cantStockDeComercio = array();
     $conjuntoComercios = ArrayHelper::toArray(Comercios::find()->all());
     $conjuntoStock = ArrayHelper::toArray(Stock::find()->all());
     foreach ($conjuntoComercios as $value) {
         if ($value['idComercio'] == $id) {
             $nomComercio = $value['nombre'];
         }
     }
     foreach ($conjuntoStock as $value) {
         if ($value['nombreComercio'] == $nomComercio) {
             array_push($arrayNombresProductos, $value['nombreProducto']);
             //obtengo los nombres de los productos del stock de ese comercio
             array_push($idStockDeComercio, $value['idStock']);
             array_push($cantStockDeComercio, $value['cantidadEnStock']);
         }
     }
     return $this->render('stock', ['comerciostock' => $arrayNombresProductos, 'nombreComercio' => $nomComercio, 'idStockDeComercio' => $idStockDeComercio, 'cantStockDeComercio' => $cantStockDeComercio]);
 }
 /**
  * Finds the Stock model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Stock the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Stock::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Ejemplo n.º 10
0
 /**
  * 同步当前股票信息
  */
 public function actionSyncStock()
 {
     $sleep = 5;
     while (true) {
         try {
             $hourm = intval(date('Hm'));
             if ($hourm > 900 && $hourm < 1135 || $hourm > 1300 && $hourm < 1510) {
                 $pici = time();
                 $url_arr = ['http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sz_a&symbol=&_s_r_a=page&time=' . time(), 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10000&sort=changepercent&asc=0&node=sh_a&symbol=&_s_r_a=page'];
                 foreach ($url_arr as $url) {
                     $return_arr = Common::reqUrl($url, [], true, 30, true);
                     if ($return_arr['content']) {
                         $arr = Common::ext_json_decode(mb_convert_encoding($return_arr['content'], "utf-8", "gbk"), true);
                         if ($arr) {
                             foreach ($arr as $row) {
                                 $Stock = Stock::find()->where(['symbol' => $row['symbol']])->one();
                                 if ($Stock) {
                                     $Stock->scenario = 'update';
                                 } else {
                                     $Stock = new Stock(['scenario' => 'create']);
                                     $Stock->updated_at = time();
                                 }
                                 $save_arr = [];
                                 $save_arr['Stock'] = $row;
                                 if ($Stock->id && $row['trade'] != $Stock->trade) {
                                     $StockLog = new StockLog(['scenario' => 'create']);
                                     $StockLog->stock_id = $Stock->id;
                                     $StockLog->pici = $pici;
                                     $StockLog->setAttributes($Stock->attributes);
                                     $StockLog->id = '';
                                     $StockLog->save();
                                 }
                                 if ($Stock->load($save_arr) && $Stock->save()) {
                                 }
                             }
                         }
                     }
                 }
             }
             sleep($sleep);
         } catch (Exception $e) {
         }
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStocks()
 {
     return $this->hasMany(Stock::className(), ['id_producto' => 'id']);
 }
 public function actionStock()
 {
     $params = Yii::$app->getRequest()->post();
     $user = User::findIdentityByAccessToken($params['muli_token']);
     $valid = false;
     //return $params;
     $arrayStock = $params['stock'];
     foreach ($arrayStock as $stockEnv) {
         $rutaDiaria = RutaDiaria::find()->where(['id_usuario' => $user->id])->andWhere(['fecha' => date('Y-m-d')])->one();
         $rutaDiariaComercio = RutaDiariaComercio::find()->where(['id_comercio' => $params['id_comercio']])->andWhere(['id_ruta_diaria' => $rutaDiaria->id])->one();
         $stockIdProd = Stock::find()->where(['id_producto' => $stockEnv['id_producto']])->andWhere(['id_ruta_diaria_com' => $rutaDiariaComercio->id])->one();
         if ($stockIdProd != null) {
             $stock = $stockIdProd;
             $stock->setAttribute('cantidad', $stockEnv['cant']);
         } else {
             $stock = new Stock();
             $stock->setAttribute('id_ruta_diaria_com', $rutaDiariaComercio->id);
             $stock->setAttribute('id_producto', $stockEnv['id_producto']);
             $stock->setAttribute('cantidad', $stockEnv['cant']);
         }
         if ($stock->cantidad != 0) {
             if ($stock->validate()) {
                 if ($stockIdProd != null) {
                     //es un update del Stock, podemos haber realizado ya el calculo de la venta.
                     $rutaDiariaComercioFecha = RutaDiariaComercio::find()->where('ruta_diaria_comercio.id !=' . $rutaDiariaComercio->id)->andWhere('ruta_diaria_comercio.id_ruta_diaria!=' . $rutaDiariaComercio->id_ruta_diaria)->andWhere(['ruta_diaria_comercio.id_comercio' => $rutaDiariaComercio->id_comercio])->joinWith(['idRutaDiaria' => function ($query) {
                         //$query->where('MAX(ruta_diaria.fecha)');
                         $query->groupBy('ruta_diaria.id')->having('MAX(ruta_diaria.fecha)')->orderBy('fecha DESC');
                     }])->one();
                     $stockAnterior = Stock::find()->where(['id_producto' => $stock->id_producto])->andWhere(['id_ruta_diaria_com' => $rutaDiariaComercioFecha->id])->one();
                     $pedidoAnterior = Pedido::find()->where(['id_producto' => $stock->id_producto])->andWhere(['id_ruta_diaria_com' => $rutaDiariaComercioFecha->id])->one();
                     if ($stockAnterior != null) {
                         $vendidos = $stockAnterior->cantidad - $stock->cantidad;
                         if ($pedidoAnterior != null) {
                             $vendidos = $vendidos + $pedidoAnterior->cantidad;
                         }
                         $compraProducto = ComercioProducto::find()->where(['id_comercio' => $params['id_comercio']])->andWhere(['id_producto' => $stock->id_producto])->andWhere(['fecha' => date('Y-m-d')])->one();
                         $compraProducto->setAttribute('vendidos', $vendidos);
                         $compraProducto->save();
                     }
                 } else {
                     $rutaDiariaComercioFecha = RutaDiariaComercio::find()->where('ruta_diaria_comercio.id !=' . $rutaDiariaComercio->id)->andWhere('ruta_diaria_comercio.id_ruta_diaria!=' . $rutaDiariaComercio->id_ruta_diaria)->andWhere(['ruta_diaria_comercio.id_comercio' => $rutaDiariaComercio->id_comercio])->joinWith(['idRutaDiaria' => function ($query) {
                         //$query->where('MAX(ruta_diaria.fecha)');
                         $query->groupBy('ruta_diaria.id')->having('MAX(ruta_diaria.fecha)')->orderBy('fecha DESC');
                     }])->one();
                     if ($rutaDiariaComercioFecha != null) {
                         //return "existe?:".$stock->id_producto;
                         $stockAnterior = Stock::find()->where(['id_producto' => $stock->id_producto])->andWhere(['id_ruta_diaria_com' => $rutaDiariaComercioFecha->id])->one();
                         $pedidoAnterior = Pedido::find()->where(['id_producto' => $stock->id_producto])->andWhere(['id_ruta_diaria_com' => $rutaDiariaComercioFecha->id])->one();
                         if ($stockAnterior != null) {
                             $vendidos = $stockAnterior->cantidad - $stock->cantidad;
                             if ($pedidoAnterior != null) {
                                 $vendidos = $vendidos + $pedidoAnterior->cantidad;
                             }
                             $compraProducto = new ComercioProducto();
                             $compraProducto->setAttribute('vendidos', $vendidos);
                             $compraProducto->setAttribute('id_comercio', $params['id_comercio']);
                             $compraProducto->setAttribute('id_producto', $stock->id_producto);
                             $compraProducto->setAttribute('fecha', date('Y-m-d'));
                             $compraProducto->save();
                         }
                     }
                 }
                 if ($stock->save()) {
                     $valid = true;
                 } else {
                     $valid = false;
                 }
             } else {
                 throw new BadRequestHttpException(Yii::t('mobile', 'Failed to save orders data...'));
             }
         } else {
             if ($stock->validate()) {
                 $valid = true;
             } else {
                 throw new BadRequestHttpException(Yii::t('mobile', 'Failed to save orders data...'));
             }
         }
     }
     return $valid;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStocks()
 {
     return $this->hasMany(Stock::className(), ['id_ruta_diaria_com' => 'id']);
 }