/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Compraproduto::find();
     // add conditions that should always apply here
     $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;
     }
     // grid filtering conditions
     $query->andFilterWhere(['idCompra' => $this->idCompra, 'idProduto' => $this->idProduto, 'quantidade' => $this->quantidade, 'valorCompra' => $this->valorCompra]);
     return $dataProvider;
 }
 /**
  * Finds the Compraproduto model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $idCompra
  * @param integer $idProduto
  * @return Compraproduto the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($idCompra, $idProduto)
 {
     if (($model = Compraproduto::findOne(['idCompra' => $idCompra, 'idProduto' => $idProduto])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Deletes an existing Compra model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     $mensagem = "";
     //Inicia a transação:
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         $itensDeletados = true;
         $produtosCompra = Compraproduto::find()->where(['idCompra' => $id])->all();
         foreach ($produtosCompra as $pc) {
             $produto = Produto::findOne($pc->idProduto);
             $produto->quantidadeEstoque -= $pc->quantidade;
             if (!$produto->save()) {
                 $transaction->rollBack();
                 //desfaz alterações no BD
                 $itensDeletados = false;
             }
         }
         if ($itensDeletados) {
             $this->findModel($id)->delete();
             $transaction->commit();
             return $this->redirect(['index']);
         }
     } catch (\Exception $exception) {
         $transaction->rollBack();
         $mensagem = "Ocorreu uma falha inesperada ao tentar salvar ";
     }
     $searchModel = new CompraSearch();
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'mensagem' => $mensagem]);
 }
 /**
  * Busca e verifica se um produto está em alguma compra registrada
  * @param $idProduto
  */
 public function actionGetCompraProduto($idProduto)
 {
     $compraProduto = Compraproduto::find()->where(['idProduto' => $idProduto])->one();
     if ($compraProduto != null) {
         echo Json::encode(true);
     } else {
         echo Json::encode(false);
     }
 }
 /**
  * Retorna um Compraproduto de acordo com o id do produto passado,
  * pegando a ultima compra feita que contenha esse produto
  * @param $idProduto
  * @return array|null|\yii\db\ActiveRecord
  */
 public function searchProdutosCompra($idProduto)
 {
     $query = Compraproduto::find()->join('INNER JOIN', 'compra', 'idCompra = idconta')->where(['idProduto' => $idProduto])->orderBy('dataCompra, idconta DESC')->one();
     return $query;
 }