/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Pagamento::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['idConta' => SORT_DESC]]]);
     $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(['formapagamento_idTipoPagamento' => $this->formapagamento_idTipoPagamento, 'idConta' => $this->idConta, 'idPedido' => $this->idPedido]);
     return $dataProvider;
 }
Beispiel #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPagamento()
 {
     return $this->hasOne(Pagamento::className(), ['idPedido' => 'idPedido']);
 }
 public function actionFinalizarPedido($formaPagamento, $idPedido)
 {
     if ($formaPagamento != null && $idPedido != null) {
         $pagamento = Pagamento::find()->where(['idPedido' => $idPedido])->one();
         if ($pagamento != null) {
             $pedido = $this->findModel($idPedido);
             $pagamento->formapagamento_idTipoPagamento = $formaPagamento;
             //Inicia a transação:
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 $itensInseridos = true;
                 if ($pagamento->save()) {
                     if ($pedido != null) {
                         $situacaoPedido = Pedido::CONCLUIDO;
                         $pedido->idSituacaoAtual = $situacaoPedido;
                         if ($pedido->save() && $pedido->cadastrarNovaHistoricoSituacaoPedido(intval($pedido->idPedido), intval($pedido->idSituacaoAtual), Yii::$app->getUser()->id)) {
                             $caixa = new Caixa();
                             $caixa = $caixa->getCaixaAberto();
                             if ($caixa != null) {
                                 $caixa = Caixa::findOne($caixa->idcaixa);
                                 $caixa->valoremcaixa += $pedido->totalPedido;
                                 $caixa->valorapurado += $pedido->totalPedido;
                                 $caixa->valorlucro += number_format($caixa->calculaValorLucroPedido($pedido->idPedido), 2);
                                 if (!$caixa->save()) {
                                     $mensagem = "Não foi possível salvar os dados de algum item do Pedido";
                                     $transaction->rollBack();
                                     //desfaz alterações no BD
                                     $itensInseridos = false;
                                     echo Json::encode(false);
                                 }
                             } else {
                                 $mensagem = "Não foi possível salvar os dados de algum item do Pedido";
                                 $transaction->rollBack();
                                 //desfaz alterações no BD
                                 $itensInseridos = false;
                                 echo Json::encode('caixanull');
                             }
                         } else {
                             $mensagem = "Não foi possível salvar os dados de algum item do Pedido";
                             $transaction->rollBack();
                             //desfaz alterações no BD
                             $itensInseridos = false;
                             echo Json::encode(false);
                         }
                     } else {
                         $mensagem = "Não foi possível salvar os dados de algum item do Pedido";
                         $transaction->rollBack();
                         //desfaz alterações no BD
                         $itensInseridos = false;
                         echo Json::encode(false);
                     }
                 } else {
                     $mensagem = "Não foi possível salvar os dados de algum item do Pedido";
                     $transaction->rollBack();
                     //desfaz alterações no BD
                     $itensInseridos = false;
                     echo Json::encode(false);
                 }
                 //Testa se todos os itens foram inseridos (ou tudo ou nada):
                 if ($itensInseridos) {
                     $transaction->commit();
                     echo Json::encode($pedido->idSituacaoAtual);
                 }
             } catch (\Exception $exception) {
                 $transaction->rollBack();
                 $mensagem = "Ocorreu uma falha inesperada ao tentar salvar o Pedido";
             }
         } else {
             echo Json::encode(false);
         }
     } else {
         echo Json::encode(false);
     }
 }
 /**
  * Deletes an existing Conta model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     //Guarda a mensagem
     $mensagem = "";
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         $idpedido = Pagamento::find()->where(['idconta' => $id])->one();
         if (isset($idpedido)) {
             $itenspedido = Itempedido::find()->where(['idPedido' => $idpedido->idPedido])->all();
             foreach ($itenspedido as $p) {
                 Insumo::atualizaQtdNoEstoqueDelete($p->idProduto, $p->quantidade);
             }
         }
         if ($this->findModel($id)->delete()) {
             $transaction->commit();
         }
     } catch (\Exception $exception) {
         $transaction->rollBack();
         $mensagem = "Ocorreu uma falha inesperada ao tentar salvar ";
     }
     $searchModel = new ContaSearch();
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'mensagem' => $mensagem]);
 }
 /**
  * Finds the Pagamento model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $idConta
  * @param integer $idPedido
  * @return Pagamento the loaded modelPagamento
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($idConta, $idPedido)
 {
     if (($modelPagamento = Pagamento::findOne(['idConta' => $idConta, 'idPedido' => $idPedido])) !== null) {
         return $modelPagamento;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }