/** * Finds the Journal model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Journal the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Journal::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Updates an existing Transaction model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model->tags = !empty($model->tags) ? explode(",", $model->tags) : []; if (Yii::$app->request->post()) { $post = Yii::$app->request->post(); $debet = isset($post['Transaction']['debet']) ? $post['Transaction']['debet'] : []; $credit = isset($post['Transaction']['credit']) ? $post['Transaction']['credit'] : []; $p = $post['Transaction']; $istemplate = false; if (isset($post['Transaction']['template'])) { $istemplate = true; } if ($model->saveTemplate($p, $istemplate)) { if ($istemplate) { return $this->redirect(['index']); } if (is_array($post['Transaction']['tags'])) { $post['Transaction']['tags'] = implode(",", $post['Transaction']['tags']); } $model->load($post); $model->total = $model->total == null ? 0 : $model->total; $transaction = Yii::$app->db->beginTransaction(); try { if ($model->save()) { $all = array_merge($debet, $credit); $js = Journal::find()->where("transaction_id = :id", ["id" => $model->id])->all(); foreach ($js as $j) { //$j->delete(); $ada = false; foreach ($all as $key => $val) { if ($val['account_id'] == $j->account_id && $val['type'] == $j->type && $val['amount'] > 0) { $ada = true; } } if (!$ada && $j->isdel == 0) { $j->isdel = 1; $j->save(); } } foreach ($all as $d) { $j = Journal::find()->where("transaction_id = :id AND account_id = :aid AND type = :t", ["id" => $model->id, "t" => intval($d["type"]), "aid" => intval($d["account_id"])])->one(); if (!$j) { $j = new Journal(); } $j->load(["Journal" => $d]); $j->remarks = empty($j->remarks) ? $model->remarks . " (" . $model->tags . ")" : $j->remarks; $j->transaction_id = $model->id; if ($j->amount > 0) { $j->isdel = 0; $j->save(); } } $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } else { $transaction->rollBack(); } } catch (Exception $e) { $transaction->rollBack(); } } } else { return $this->render('update', ['model' => $model]); } }
/** * @return \yii\db\ActiveQuery */ public function getJournals() { return $this->hasMany(Journal::className(), ['transaction_id' => 'id'])->where("isdel=0"); }
/** * @return \yii\db\ActiveQuery */ public function getJournals() { return $this->hasMany(Journal::className(), ['account_id' => 'id']); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = $this->find(); $query->joinWith(['transaction', 'account']); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['time'] = ['asc' => [Transaction::tableName() . '.time' => SORT_ASC], 'desc' => [Transaction::tableName() . '.time' => SORT_DESC]]; $dataProvider->sort->attributes['title'] = ['asc' => [Transaction::tableName() . '.title' => SORT_ASC], 'desc' => [Transaction::tableName() . '.title' => SORT_DESC]]; $dataProvider->sort->attributes['subject'] = ['asc' => [Transaction::tableName() . '.subject' => SORT_ASC], 'desc' => [Transaction::tableName() . '.subject' => SORT_DESC]]; $dataProvider->sort->attributes['tags'] = ['asc' => [Transaction::tableName() . '.tags' => SORT_ASC], 'desc' => [Transaction::tableName() . '.tags' => SORT_DESC]]; $dataProvider->sort->attributes['transactionRemarks'] = ['asc' => [Transaction::tableName() . '.remarks' => SORT_ASC], 'desc' => [Transaction::tableName() . '.remarks' => SORT_DESC]]; $dataProvider->sort->attributes['accountName'] = ['asc' => [AccountCode::tableName() . '.code' => SORT_ASC], 'desc' => [AccountCode::tableName() . '.code' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'account_id' => $this->account_id, 'transaction_id' => $this->transaction_id, 'type' => $this->type, 'isdel' => $this->isdel]); $params = self::queryNumber([['quantity'], ['amount']]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryTime([['time', Transaction::tableName()]]); foreach ($params as $p) { $query->andFilterWhere($p); } $params = self::queryString([['remarks', Journal::tableName()], ['title', Transaction::tableName()], ['subject', Transaction::tableName()], ['tags', Transaction::tableName()]]); foreach ($params as $p) { $query->andFilterWhere($p); } $query->andFilterWhere(['like', 'lower(' . Transaction::tableName() . '.remarks)', strtolower($this->transactionRemarks)])->andFilterWhere(['like', "lower(concat(" . AccountCode::tableName() . ".code,' - '," . AccountCode::tableName() . ".name))", strtolower($this->accountName)]); return $dataProvider; }