/**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['id_comercio', 'id_producto'], 'required'], [['id_comercio', 'id_producto'], 'integer'], 'repetidoValidator' => ['id_comercio', function ($attribute) {
         if (ComercioProductosRelacionados::find()->where(['id_comercio' => $this->id_comercio])->andWhere(['id_producto' => $this->id_producto])->count() > 0) {
             $this->addError($attribute, \Yii::t('core', 'Some of the products added are already related with this store '));
         }
     }]];
 }
 private function findModel($id)
 {
     $listaComercioProductos = ComercioProductosRelacionados::find()->where(['id_comercio' => $id])->all();
     $listaProductos = [];
     foreach ($listaComercioProductos as $comercioProductos) {
         $producto = Producto::find()->where(['id' => $comercioProductos->id_producto])->one();
         $listaProductos[] = $producto;
     }
     return $listaProductos;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ComercioProductosRelacionados::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_comercio' => $this->id_comercio, 'id_producto' => $this->id_producto]);
     return $dataProvider;
 }
 /**
  * Creates a new ComercioProductosRelacionados model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ComercioProductosRelacionados();
     if ($model->load(Yii::$app->request->post())) {
         $productos = $model->id_producto;
         foreach ($productos as $value) {
             $newModel = new ComercioProductosRelacionados();
             $newModel->id_producto = $value;
             $newModel->id_comercio = $model->id_comercio;
             if ($newModel->validate()) {
                 $newModel->save();
             } else {
                 foreach ($newModel->errors as $error) {
                     Yii::$app->getSession()->setFlash('danger', $error);
                     return $this->redirect(['view', 'id_comercio' => $model->id_comercio, 'id_producto' => $model->id_producto[0]]);
                 }
             }
         }
         return $this->redirect(['view', 'id_comercio' => $model->id_comercio, 'id_producto' => $model->id_producto[0]]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 public function cargarArrayVentas($id)
 {
     $productosDelComercio = ComercioProductosRelacionados::find()->where(['id_comercio' => $id])->all();
     $ventasGenerales = [];
     $respuesta = [];
     $i = 0;
     foreach ($productosDelComercio as $prod) {
         $ventas = ComercioProducto::find()->where(['id_producto' => $prod->id_producto])->all();
         $cantidad = 0;
         foreach ($ventas as $venta) {
             $cantidad = $cantidad + $venta->vendidos;
         }
         $row = ['id_producto' => $prod->id_producto, 'cantidad' => $cantidad];
         $ventasGenerales[$prod->id_producto] = $row;
         $i++;
     }
     foreach ($ventasGenerales as $venta) {
         $respuesta = $this->actualizarMasVendidos($respuesta, $venta);
     }
     return $respuesta;
 }
 public function getComercioProductoRelacionados()
 {
     return $this->hasMany(ComercioProductosRelacionados::className(), ['id_producto' => 'id']);
 }
 /**
  * Deletes an existing Comercio model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $model = $this->findModel($id);
     $mensaje = Yii::t('core', 'Store has been deleted!');
     $validar = $model->esValidoBorrar();
     if ($validar == "OK") {
         Yii::$app->getSession()->setFlash('success', $mensaje);
         $relacionesProducto = ComercioProductosRelacionados::find()->where(['id_comercio' => $model->id])->all();
         foreach ($relacionesProducto as $relacionProducto) {
             $relacionProducto->delete();
         }
         $model->delete();
     } else {
         Yii::$app->getSession()->setFlash('danger', $validar);
     }
     return $this->redirect(['index']);
 }