コード例 #1
0
 /**
  * Creates a new Transaction model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Transaction();
     $model->time = date("Y/m/d H:i:s");
     $model->isdel = 0;
     $model->type = 0;
     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()) {
                     foreach (array_merge($debet, $credit) as $d) {
                         $j = new Journal();
                         $j->load(["Journal" => $d]);
                         $j->remarks = empty($j->remarks) ? $model->remarks . " (" . $model->tags . ")" : $j->remarks;
                         $j->transaction_id = $model->id;
                         $j->isdel = 0;
                         if ($j->amount > 0) {
                             $j->save();
                         }
                     }
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 } else {
                     $transaction->rollBack();
                     $model->id = array_merge($debet, $credit);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         } else {
             $model->id = array_merge($debet, $credit);
         }
     }
     return $this->render('create', ['model' => $model]);
 }