示例#1
0
 /**
  * Creates a new Journal model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     return $this->redirect(['index']);
     $model = new Journal();
     $model->isdel = 0;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
示例#2
0
 /**
  * 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]);
     }
 }