/**
  * Creates a new CaixaAberturaFechamento model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new CaixaAberturaFechamento();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id, 'tipo' => $model->tipo, 'data' => $model->data]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemple #2
0
 public function actionFechar()
 {
     $user = User::find()->where(['iduser' => Yii::$app->user->id])->one();
     $caixa = Caixa::find()->where(['loja_idloja' => $user->loja_idloja])->one();
     $caixaAberturaFechamentoVerify = CaixaAberturaFechamento::find()->where(['caixa_id' => $caixa->idcaixa])->orderBy('data DESC')->one();
     //Se caxa aberto por outro usuário, não deixar o usuário logado fechar
     if ($caixaAberturaFechamentoVerify != null and $caixaAberturaFechamentoVerify->tipo == 'abertura' and $caixaAberturaFechamentoVerify->user_id != $user->id) {
         Yii::$app->session->setFlash('warning', \Yii::t('app', 'Este caixa já está aberto por outro usuário'));
         $this->redirect('/site/index');
         Yii::$app->end();
     }
     if ($caixaAberturaFechamentoVerify != null and $caixaAberturaFechamentoVerify->tipo == 'fechamento') {
         $this->redirect(['abrir']);
     } else {
         $connection = \Yii::$app->db;
         //throw Exception('Unable to save record.');
         $transaction = $connection->beginTransaction();
         $caixaAberturaFechamento = new CaixaAberturaFechamento();
         $caixaAberturaFechamento->valor = $caixa->valor;
         $caixaAberturaFechamento->tipo = 'fechamento';
         $caixaAberturaFechamento->data = date('Y-m-d H:i:s');
         $caixaAberturaFechamento->caixa_id = $caixa->idcaixa;
         $caixaAberturaFechamento->user_id = Yii::$app->user->id;
         if ($caixaAberturaFechamento->load(Yii::$app->request->post()) && $caixaAberturaFechamento->validate()) {
             try {
                 $caixaAberturaFechamento->save();
                 $caixa->valor = $caixaAberturaFechamento->valor;
                 $caixa->status = 'fechado';
                 $caixa->save();
                 $transaction->commit();
                 Yii::$app->session->setFlash('success', \Yii::t('app', 'Caixa fechado com sucesso'));
                 return $this->redirect(['site/index']);
             } catch (Exception $e) {
                 Yii::$app->session->setFlash('danger', \Yii::t('app', 'Ops! Tivemos um erro. Tente novamente ou fale com a equipe técnica'));
                 $transaction->rollback();
                 return $this->refresh();
             }
         } else {
             return $this->render('fechar', ['caixaAberturaFechamento' => $caixaAberturaFechamento, 'caixaAberturaFechamentoVerify' => $caixaAberturaFechamentoVerify]);
         }
     }
 }