public function actionUpdate($id)
 {
     $this->objectId = $id;
     // master value
     $fmShortDatePhp = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_PHP, null);
     $fmShortDateJui = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_JUI, null);
     JarPayment::$_PHP_FM_SHORTDATE = $fmShortDatePhp;
     $model = JarPayment::findOne(['id' => $id, 'delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]);
     $renderView = 'update';
     if (is_null($model)) {
         $model = false;
         $renderData = ['model' => $model];
         Yii::$app->session->setFlash(MasterValueUtils::FLASH_ERROR, Yii::t('common', 'The requested {record} does not exist.', ['record' => Yii::t('jar.models', 'Payment')]));
     } else {
         if ($model->share_id > 0) {
             return Yii::$app->getResponse()->redirect(Url::to(['/jar/distribute/update', 'id' => $model->share_id]));
         }
         // master value
         $arrAccount = ModelUtils::getArrData(JarAccount::find()->select(['account_id', 'account_name'])->where(['delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE])->orderBy('account_type, order_num'), 'account_id', 'account_name');
         $arrEntryLog = MasterValueUtils::getArrData('jar_payment_status');
         // submit data
         $postData = Yii::$app->request->post();
         $submitMode = isset($postData[MasterValueUtils::SM_MODE_NAME]) ? $postData[MasterValueUtils::SM_MODE_NAME] : false;
         // populate model attributes with user inputs
         $model->load($postData);
         // init value
         $model->scenario = MasterValueUtils::SCENARIO_UPDATE;
         // render GUI
         $renderData = ['model' => $model, 'fmShortDatePhp' => $fmShortDatePhp, 'fmShortDateJui' => $fmShortDateJui, 'arrAccount' => $arrAccount, 'arrEntryLog' => $arrEntryLog];
         switch ($submitMode) {
             case MasterValueUtils::SM_MODE_INPUT:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $renderView = 'confirm';
                     $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_EDIT];
                 }
                 break;
             case MasterValueUtils::SM_MODE_CONFIRM:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $result = $this->updatePayment($model, $fmShortDatePhp);
                     if ($result === true) {
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_SUCCESS, Yii::t('common', '{record} has been saved successfully.', ['record' => Yii::t('jar.models', 'Payment')]));
                         return Yii::$app->getResponse()->redirect(Url::to(['update', 'id' => $id]));
                     } else {
                         // restore Data for View
                         $model->entry_date = DateTimeUtils::parse($model->entry_date, DateTimeUtils::FM_DB_DATE, $fmShortDatePhp);
                         // render View
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_ERROR, $result);
                         $renderView = 'confirm';
                         $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_EDIT];
                     }
                 }
                 break;
             case MasterValueUtils::SM_MODE_BACK:
                 break;
             default:
                 break;
         }
     }
     // render GUI
     return $this->render($renderView, $renderData);
 }
 /**
  * update a Purchase
  * @param $purchase OefPurchase
  * @param $fmShortDatePhp
  * @throws Exception
  * @return string|true
  */
 private function updatePurchase($purchase, $fmShortDatePhp)
 {
     // modify data for DB
     $purchase->purchase_date = DateTimeUtils::parse($purchase->purchase_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE);
     if (!empty($purchase->sip_date)) {
         $purchase->sip_date = DateTimeUtils::parse($purchase->sip_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE);
     }
     $transaction = Yii::$app->db->beginTransaction();
     $save = true;
     $message = null;
     // begin transaction
     try {
         // save OefPurchase
         $save = $purchase->save();
         // FinAccountEntry
         $finPayment = FinAccountEntry::findOne($purchase->fin_entry_id);
         if ($save !== false && !is_null($finPayment)) {
             $finPayment->entry_date = $purchase->purchase_date;
             $finPayment->entry_value = $purchase->investment;
             $save = $finPayment->save();
             // save FinAccount (Debit)
             if ($save !== false) {
                 $debitFinAccount = FinAccount::findOne($finPayment->account_source);
                 $debitFinAccount->opening_balance = $debitFinAccount->opening_balance - $purchase->investment + $purchase->investment_old;
                 $save = $debitFinAccount->save();
             }
             // save FinAccount (Credit)
             if ($save !== false) {
                 $creditFinAccount = FinAccount::findOne($finPayment->account_target);
                 $creditFinAccount->opening_balance = $creditFinAccount->opening_balance + $purchase->investment - $purchase->investment_old;
                 $creditFinAccount->capital = $creditFinAccount->capital + $purchase->investment - $purchase->investment_old;
                 $save = $creditFinAccount->save();
             }
         }
         // save JarPayment
         $jarPayment = JarPayment::findOne($purchase->jar_payment_id);
         if ($save !== false && !is_null($jarPayment)) {
             $jarPayment->entry_date = $purchase->purchase_date;
             $jarPayment->entry_value = $purchase->investment;
             $save = $jarPayment->save();
             // save JarAccount (Debit)
             if ($save !== false) {
                 $debitJarAccount = JarAccount::findOne($jarPayment->account_source);
                 $debitJarAccount->useable_balance = $debitJarAccount->useable_balance - $purchase->investment + $purchase->investment_old;
                 $save = $debitJarAccount->save();
             }
             // save JarAccount (Credit)
             if ($save !== false) {
                 $creditJarAccount = JarAccount::findOne($jarPayment->account_target);
                 $creditJarAccount->useable_balance = $creditJarAccount->useable_balance + $purchase->investment - $purchase->investment_old;
                 $save = $creditJarAccount->save();
             }
         }
     } catch (Exception $e) {
         $save = false;
         $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('oef.models', 'Purchase')]);
     }
     // end transaction
     try {
         if ($save === false) {
             $transaction->rollback();
             return $message;
         } else {
             $transaction->commit();
         }
     } catch (Exception $e) {
         throw Exception(Yii::t('common', 'Unable to excute Transaction.'));
     }
     return true;
 }
 /**
  * update Distribute
  * @param $share JarShare
  * @param $arrShareDetail JarShareDetail
  * @param $fmShortDatePhp
  * @throws Exception
  * @return string|true
  */
 private function updateDistribute($share, $arrShareDetail, $fmShortDatePhp)
 {
     // modify data for DB
     $share->share_date = DateTimeUtils::parse($share->share_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE);
     $transaction = Yii::$app->db->beginTransaction();
     $save = true;
     $message = null;
     // begin transaction
     try {
         // save JarShare
         $save = $share->save();
         if ($save !== false) {
             foreach ($arrShareDetail as $shareDetail) {
                 // save JarAccount
                 $account = JarAccount::findOne($shareDetail->account_id);
                 $account->real_balance = $account->real_balance - $shareDetail->share_value_old + $shareDetail->share_value;
                 $account->useable_balance = $account->useable_balance - $shareDetail->share_value_old + $shareDetail->share_value;
                 $save = $account->save();
                 if ($save === false) {
                     break;
                 }
                 // save JarPayment
                 $payment = JarPayment::findOne(['account_target' => $shareDetail->account_id, 'share_id' => $share->share_id]);
                 $payment->entry_date = $share->share_date;
                 $payment->entry_value = $shareDetail->share_value;
                 $payment->description = $share->description;
                 $save = $payment->save();
                 if ($save === false) {
                     break;
                 }
             }
         }
     } catch (Exception $e) {
         $save = false;
         $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('jar.models', 'Distribute')]);
     }
     // end transaction
     try {
         if ($save === false) {
             $transaction->rollback();
             return $message;
         } else {
             $transaction->commit();
         }
     } catch (Exception $e) {
         throw Exception(Yii::t('common', 'Unable to excute Transaction.'));
     }
     return true;
 }