public function actionIndex() { $arrAccount = JarAccount::find()->where(['delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE, 'account_type' => MasterValueUtils::MV_JAR_ACCOUNT_TYPE_JAR])->orderBy('status, order_num')->all(); $tempAccount = JarAccount::findOne(9); $sumAccountQuery = (new Query())->select(['SUM(useable_balance) AS useable_balance, SUM(real_balance) AS real_balance, SUM(share_unit) AS share'])->from('jar_account')->where(['delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE, 'status' => MasterValueUtils::MV_JAR_ACCOUNT_STATUS_ON, 'account_type' => MasterValueUtils::MV_JAR_ACCOUNT_TYPE_JAR]); $sumAccountValue = $sumAccountQuery->createCommand()->queryOne(); // render GUI $renderData = ['arrAccount' => $arrAccount, 'tempAccount' => $tempAccount, 'sumAccountValue' => $sumAccountValue]; return $this->render('index', $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 Payment * @param $payment JarPayment * @param $fmShortDatePhp * @throws Exception * @return string|true */ private function updatePayment($payment, $fmShortDatePhp) { // modify data for DB $payment->entry_date = DateTimeUtils::parse($payment->entry_date, $fmShortDatePhp, DateTimeUtils::FM_DB_DATE); $transaction = Yii::$app->db->beginTransaction(); $save = true; $message = null; // begin transaction try { $accountSource = JarAccount::findOne($payment->account_source); $accountTarget = JarAccount::findOne($payment->account_target); if ($payment->account_source == 0 || $payment->account_target == 0) { // save source if (!is_null($accountSource) && $save !== false) { $accountSource->real_balance = $accountSource->real_balance - $payment->entry_adjust; $accountSource->useable_balance = $accountSource->useable_balance - $payment->entry_adjust; $save = $accountSource->save(); } // save target if (!is_null($accountTarget) && $save !== false) { $accountTarget->real_balance = $accountTarget->real_balance + $payment->entry_adjust; $accountTarget->useable_balance = $accountTarget->useable_balance + $payment->entry_adjust; $save = $accountTarget->save(); } } else { // save source if ($save !== false) { $accountSource->useable_balance = $accountSource->useable_balance - $payment->entry_adjust; $save = $accountSource->save(); } // save target if ($save !== false) { $accountTarget->useable_balance = $accountTarget->useable_balance + $payment->entry_adjust; $save = $accountTarget->save(); } } // save payment if ($save !== false) { $payment->entry_value = $payment->entry_value + $payment->entry_adjust; $save = $payment->save(); } } catch (Exception $e) { $save = false; $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('jar.models', 'Payment')]); } // 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; }