/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Parcela::find(); //$query->joinWith(['vendaIdvenda']); $query->joinWith(['vendaIdvenda.clienteIdcliente', 'vendaIdvenda.lojaIdloja']); $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; } if ($this->dataPagamentoPeriodo) { //2015-07-15 - 2015-07-22 $dataInicial = substr($this->dataPagamentoPeriodo, 0, 10); $dataFinal = substr($this->dataPagamentoPeriodo, -10); $query->andFilterWhere(['between', 'data_pagamento', $dataInicial . ' 00:00:00', $dataFinal . ' 23:59:59']); } if ($this->lojaId) { $query->andFilterWhere(['like', 'loja.idloja', $this->lojaId]); } /*if($this->clienteNome){ $query->joinWith(['vendaIdvenda.clienteIdcliente']); $query->andFilterWhere(['like', 'cliente.nome',$this->clienteNome]); }*/ if ($this->clienteNome) { $query->andFilterWhere(['like', 'cliente.nome', $this->clienteNome]); } /*if($this->lojaId){ $query->joinWith(['vendaIdvenda']); $query->andFilterWhere([ 'venda.loja_idloja' => $this->loja ]); $query->andFilterWhere(['like','loja.idloja', $this->lojaId]); }*/ $query->andFilterWhere(['idparcela' => $this->idparcela, 'valor_a_pagar' => $this->valor_a_pagar, 'parcela' => $this->parcela, 'valor_pago' => $this->valor_pago, 'data_vencimento' => $this->data_vencimento, 'venda_idvenda' => $this->venda_idvenda, 'forma_pagamento_id' => $this->forma_pagamento_id, 'data_pagamento' => $this->data_pagamento]); $query->andFilterWhere(['like', 'obs', $this->obs]); //->andFilterWhere(['like', 'vendaIdvenda.clienteIdcliente.nome', $this->vendaIdvenda->clienteIdcliente->nome]) return $dataProvider; }
public function actionSinteticoCaixa() { $searchModel = new ParcelaSearch(); if (Yii::$app->user->identity->perfil != 'Administrador') { $searchModel->lojaId = Yii::$app->user->identity->loja_idloja; } //$searchModel->dataPagamentoPeriodo = date('Y-m-d') .' - '.date('Y-m-d') ; //'2015-07-15 - 2015-07-22' $query = Parcela::find()->select(['fp.forma_pagamento as nomeFormaPagamento,SUM(valor_pago) as valor_pago'])->where(['status' => 'paga'])->innerJoin('forma_pagamento fp', 'parcela.forma_pagamento_id=fp.idforma_pagamento')->groupBy('forma_pagamento_id'); //$query = "SELECT fp.forma_pagamento, SUM(p.valor_pago) FROM parcela p, forma_pagamento fp where fp.idforma_pagamento = p.forma_pagamento_id GROUP BY p.forma_pagamento_id"; $dataProvider = new ActiveDataProvider(['query' => $query]); $searchModel->load(Yii::$app->request->queryParams); if ($searchModel->dataPagamentoPeriodo) { //2015-07-15 - 2015-07-22 $dataInicial = substr($searchModel->dataPagamentoPeriodo, 0, 10); $dataFinal = substr($searchModel->dataPagamentoPeriodo, -10); $query->andFilterWhere(['between', 'data_pagamento', $dataInicial . ' 00:00:00', $dataFinal . ' 23:59:59']); } if ($searchModel->lojaId) { $query->joinWith(['vendaIdvenda']); $query->andFilterWhere(['venda.loja_idloja' => $searchModel->lojaId]); } //VarDumper::dump($dataProvider->getModels(),10,true);die; return $this->render('sinteticoCaixa', ['dataProvider' => $dataProvider, 'searchModel' => $searchModel]); }
/** * Finds the Parcela model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Parcela the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Parcela::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getParcelas() { return $this->hasMany(Parcela::className(), ['venda_idvenda' => 'idvenda']); }
/** * Creates a new Venda model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { /*$data = '02/08/2015'; echo $dataConvertida = Yii::$app->utilcomponent->ConvDateToMySQL($data); echo '<br>'; echo date('Y-m-d'); if($dataConvertida == date('Y-m-d')){ echo 'igual'; } die;*/ $user = User::find()->where(['iduser' => Yii::$app->user->id])->one(); //Verifica se caixa aberto pelo usuário $caixa = Caixa::find()->where(['loja_idloja' => $user->loja_idloja])->one(); $caixaAberturaFechamentoVerify = CaixaAberturaFechamento::find()->where(['caixa_id' => $caixa->idcaixa])->orderBy('data DESC')->one(); if ($caixaAberturaFechamentoVerify != null and ($caixaAberturaFechamentoVerify->user_id != $user->id and $caixaAberturaFechamentoVerify->tipo == 'abertura' or $caixaAberturaFechamentoVerify->tipo == 'fechamento')) { $this->redirect(['caixa/abrir']); } $model = new Venda(); $model->data = date('Y-m-d H:i:m'); $model->loja_idloja = $user->loja_idloja; $vendaHasProduto = new VendaHasProduto(); $parcela = new Parcela(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { //VarDumper::dump($_POST,10,true);die; //Alisson, sempre que quiser DEBUGAR o código, faça como eu fiz na linha abaixo. Primeiro, imprime os valores $_POST que chegaram aqui e depois com DIE mata o processo, não deixando a aplicação continuar e salvar a venda. Serve para apenas vc conferir como os valores estão chegando. $connection = \Yii::$app->db; //throw Exception('Unable to save record.'); $transaction = $connection->beginTransaction(); try { $model->desconto = $model->desconto == NULL ? 0 : $model->desconto; if (!$model->save()) { throw new Exception('Não foi possível salvar - #1.'); } foreach ($_POST['VendaHasProduto'] as $item) { //ESTOQUE $estoque = Estoque::find()->where(['produto_idproduto' => $item['produto_idproduto'], 'loja_idloja' => $model->loja_idloja])->one(); if ($estoque->quantidade < $item['quantidade']) { //erro de validaçao aqui e chamar um exception } $estoque->quantidade -= $item['quantidade']; if (!$estoque->save()) { throw new Exception('Não foi possível salvar - #2.'); } //VENDA HAS PRODUTO $vendaHasProduto = new VendaHasProduto(); $vendaHasProduto->venda_idvenda = $model->idvenda; $vendaHasProduto->produto_idproduto = $item['produto_idproduto']; $vendaHasProduto->quantidade = $item['quantidade']; $vendaHasProduto->valor_unitario = Yii::$app->utilcomponent->formatNumberToMysql($item['valor_unitario']); if (!$vendaHasProduto->save()) { throw new Exception('Não foi possível salvar - #3.'); } } foreach ($_POST['Parcela'] as $key => $p) { //VENDA TEM PARCELAS $parcela = new Parcela(); $parcela->data_vencimento = Yii::$app->utilcomponent->ConvDateToMySQL($p['data_vencimento']); $parcela->valor_a_pagar = Yii::$app->utilcomponent->formatNumberToMysql($p['valor_a_pagar']); $parcela->parcela = $key; $parcela->venda_idvenda = $model->idvenda; $parcela->forma_pagamento_id = $p['forma_pagamento_id']; if ($parcela->data_vencimento == date('Y-m-d') and $p['forma_pagamento_id'] == FormaPagamento::DINHEIRO) { $parcela->data_pagamento = date('Y-m-d H:i:s'); $parcela->valor_pago = $parcela->valor_a_pagar; $parcela->status = 'paga'; $caixa->valor = $caixa->valor + $parcela->valor_pago; if (!$caixa->save()) { throw new Exception('Não foi possível salvar - #4.'); } $caixaMovimentacao = new CaixaMovimentacao(); $caixaMovimentacao->data = date('Y-m-d H:i:s'); $caixaMovimentacao->valor = $parcela->valor_pago; $caixaMovimentacao->caixa_movimentacao_tipo_id = 5; $caixaMovimentacao->caixa_idcaixa = $caixa->idcaixa; if (!$caixaMovimentacao->save()) { throw new Exception('Não foi possível salvar - #5.'); } } else { if ($p['forma_pagamento_id'] != FormaPagamento::DINHEIRO or $p['forma_pagamento_id'] == FormaPagamento::DINHEIRO & $parcela->data_vencimento != date('Y-m-d')) { //$parcela->data_pagamento = date('Y-m-d H:i:s'); //$parcela->valor_pago = $parcela->valor_a_pagar; $parcela->status = 'aberta'; } } if (!$parcela->save()) { foreach ($parcela->firstErrors as $erro) { $erro = $erro; break; } throw new Exception('Não foi possível salvar - #6.' . $erro); } } $transaction->commit(); return $this->redirect(['view', 'id' => $model->idvenda]); Yii::$app->session->setFlash('success', \Yii::t('app', "Sucesso. ")); return $this->refresh(); } catch (Exception $e) { Yii::$app->session->setFlash('danger', $e->getMessage()); $transaction->rollback(); return $this->refresh(); } } //die; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->idvenda]); } else { return $this->render('create', ['model' => $model, 'vendaHasProduto' => $vendaHasProduto, 'parcela' => $parcela]); } }
/** * @return \yii\db\ActiveQuery */ public function getParcelaIdparcela() { return $this->hasOne(Parcela::className(), ['idparcela' => 'parcela_idparcela']); }
/** * @return \yii\db\ActiveQuery */ public function getParcelas() { return $this->hasMany(Parcela::className(), ['forma_pagamento_id' => 'idforma_pagamento']); }