/** * create a Purchase * @param $purchase OefPurchase * @param $fmShortDatePhp * @throws Exception * @return string|true */ private function createPurchase($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 { // FinAccountEntry $finPayment = new FinAccountEntry(); $finPayment->entry_date = $purchase->purchase_date; $finPayment->entry_value = $purchase->investment; $finPayment->account_source = MasterValueUtils::MV_FIN_ACCOUNT_OTHER_FOUND; $finPayment->account_target = MasterValueUtils::MV_FIN_ACCOUNT_VCBF_TBF; $finPayment->entry_status = MasterValueUtils::MV_FIN_ENTRY_TYPE_INTEREST_VCBF_TBF; $finPayment->description = serialize([MasterValueUtils::MV_FIN_ENTRY_LOG_TRANSFER]); $save = $finPayment->save(); // save FinAccount (Debit) if ($save !== false) { $debitFinAccount = FinAccount::findOne($finPayment->account_source); $debitFinAccount->opening_balance = $debitFinAccount->opening_balance - $purchase->investment; $save = $debitFinAccount->save(); } // save FinAccount (Credit) if ($save !== false) { $creditFinAccount = FinAccount::findOne($finPayment->account_target); $creditFinAccount->opening_balance = $creditFinAccount->opening_balance + $purchase->investment; $creditFinAccount->capital = $creditFinAccount->capital + $purchase->investment; $save = $creditFinAccount->save(); } // save JarPayment $jarPayment = new JarPayment(); if ($save !== false) { $jarPayment->entry_date = $purchase->purchase_date; $jarPayment->entry_value = $purchase->investment; $jarPayment->account_source = MasterValueUtils::MV_JAR_ACCOUNT_LTSS; $jarPayment->account_target = MasterValueUtils::MV_JAR_ACCOUNT_TEMP; $jarPayment->share_id = MasterValueUtils::MV_JAR_ACCOUNT_NONE; $jarPayment->description = 'VCBF-TBF'; $jarPayment->entry_status = MasterValueUtils::MV_JAR_ENTRY_TYPE_TEMP; $save = $jarPayment->save(); } // save JarAccount (Debit) if ($save !== false) { $debitJarAccount = JarAccount::findOne($jarPayment->account_source); $debitJarAccount->useable_balance = $debitJarAccount->useable_balance - $jarPayment->entry_value; $save = $debitJarAccount->save(); } // save JarAccount (Credit) if ($save !== false) { $creditJarAccount = JarAccount::findOne($jarPayment->account_target); $creditJarAccount->useable_balance = $creditJarAccount->useable_balance + $jarPayment->entry_value; $save = $creditJarAccount->save(); } // save OefPurchase if ($save !== false) { $purchase->fin_entry_id = $finPayment->entry_id; $purchase->jar_payment_id = $jarPayment->id; $save = $purchase->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; }
/** * create Distribute * @param $share JarShare * @param $arrShareDetail JarShareDetail * @param $fmShortDatePhp * @throws Exception * @return string|true */ private function createDistribute($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 += $shareDetail->share_value; $account->useable_balance += $shareDetail->share_value; $save = $account->save(); if ($save === false) { break; } // save JarShareDetail $shareDetail->share_id = $share->share_id; $save = $shareDetail->save(); if ($save === false) { break; } // save JarPayment $payment = new JarPayment(); $payment->entry_date = $share->share_date; $payment->entry_value = $shareDetail->share_value; $payment->account_source = MasterValueUtils::MV_JAR_ACCOUNT_NONE; $payment->account_target = $shareDetail->account_id; $payment->share_id = $shareDetail->share_id; $payment->entry_status = MasterValueUtils::MV_JAR_ENTRY_TYPE_SIMPLE; $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; }