/** * copy a Fixed Deposit * @param type $fixedDepositModel * @throws Exception * @return string|true */ private function copyFixedDeposit($fixedDepositModel) { $fixedDepositModel->setIsNewRecord(true); $transaction = Yii::$app->db->beginTransaction(); $save = true; $message = null; // begin transaction try { // saving account $savingAccount = FinAccount::findOne($fixedDepositModel->saving_account); $savingAccount->opening_date = $fixedDepositModel->opening_date . ' 00:00:00'; $savingAccount->closing_date = $fixedDepositModel->closing_date . ' 00:00:00'; $savingAccount->term_interest_rate = $fixedDepositModel->interest_rate; // TM or ATM $currentAssets = FinAccount::findOne($fixedDepositModel->current_assets); // history entry (interest) $interestEntry = new FinAccountEntry(); $interestEntry->entry_date = $fixedDepositModel->opening_date; $interestEntry->entry_value = $fixedDepositModel->interest_add; $interestEntry->entry_status = MasterValueUtils::MV_FIN_ENTRY_TYPE_INTEREST_DEPOSIT; $interestEntry->description = serialize([MasterValueUtils::MV_FIN_ENTRY_LOG_INTEREST]); $interestEntry->account_source = 0; $interestEntry->account_target = $fixedDepositModel->saving_account; // history entry (capital) $capitalEntry = new FinAccountEntry(); $capitalEntry->entry_date = $fixedDepositModel->opening_date; $capitalEntry->entry_value = $fixedDepositModel->entry_value; $capitalEntry->entry_status = MasterValueUtils::MV_FIN_ENTRY_TYPE_DEPOSIT; $capitalEntry->description = serialize([MasterValueUtils::MV_FIN_ENTRY_LOG_SAVING]); if ($fixedDepositModel->add_flag == MasterValueUtils::MV_FIN_TIMEDP_TRANTYPE_ADDING) { $savingAccount->opening_balance += $fixedDepositModel->interest_add + $fixedDepositModel->entry_value; $savingAccount->capital += $fixedDepositModel->entry_value; $currentAssets->opening_balance -= $fixedDepositModel->entry_value; $capitalEntry->account_source = $fixedDepositModel->current_assets; $capitalEntry->account_target = $fixedDepositModel->saving_account; } else { $savingAccount->opening_balance += $fixedDepositModel->interest_add - $fixedDepositModel->entry_value; $savingAccount->capital -= $fixedDepositModel->entry_value; $currentAssets->opening_balance += $fixedDepositModel->entry_value; $capitalEntry->account_source = $fixedDepositModel->saving_account; $capitalEntry->account_target = $fixedDepositModel->current_assets; } // interest unit $instance = $savingAccount->instance(); $instance->initialize(); $fixedDepositModel->interest_unit = $instance->closing_interest_unit; $save = $savingAccount->save(); if ($save !== false) { $save = $currentAssets->save(); } if ($save !== false) { $save = $interestEntry->save(); } if ($save !== false) { $save = $capitalEntry->save(); } if ($save !== false) { $save = $fixedDepositModel->save(); } } catch (Exception $e) { $save = false; $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('fin.models', 'Fixed Deposit')]); } // 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 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; }
/** * copy a payment * @param type $paymentModel * @throws Exception * @return string|true */ private function copyPayment($paymentModel) { $paymentModel->setIsNewRecord(true); $transaction = Yii::$app->db->beginTransaction(); $save = true; $message = null; // begin transaction try { $paymentModel->description = serialize($paymentModel->arr_entry_log); $accountSource = FinAccount::findOne($paymentModel->account_source); $accountTarget = FinAccount::findOne($paymentModel->account_target); // save source if (!is_null($accountSource) && $save !== false) { $accountSource->opening_balance -= $paymentModel->entry_value; $save = $accountSource->save(); } // save Target if (!is_null($accountTarget) && $save !== false) { $accountTarget->opening_balance += $paymentModel->entry_value; $save = $accountTarget->save(); } // save payment if ($save !== false) { $save = $paymentModel->save(); } } catch (Exception $e) { $save = false; $message = Yii::t('common', 'Unable to save {record}.', ['record' => Yii::t('fin.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; }