/**
  * @param OefPurchase $purchase
  * @param OefFundCertificate $condition
  */
 public function initialize($purchase, $condition)
 {
     $this->purchase_id = $purchase->id;
     $this->purchase_date = DateTimeUtils::getDateFromDB($purchase->purchase_date);
     $this->purchase_type = $purchase->purchase_type;
     $this->sellable_certificate = $purchase->found_stock - $purchase->found_stock_sold;
     $this->sell_date = $condition->sell_date_obj;
     $sellCertificate = $condition->sell_certificate - $condition->sum_sell_certificate;
     $this->sell_certificate = $sellCertificate < $this->sellable_certificate ? $sellCertificate : $this->sellable_certificate;
     $this->investment = $this->sell_certificate * ($purchase->purchase + $purchase->transfer_fee + $purchase->other_fee) / $purchase->found_stock;
     $this->revenue = $this->sell_certificate * $condition->nav;
     $this->kept_months = DateTimeUtils::diffMonths($this->purchase_date, $this->sell_date);
     $this->income_tax_rate = $condition->income_tax_rate;
     $this->calculate();
 }
Exemple #2
0
<?php

use yii\bootstrap\ActiveForm;
use yii\helpers\Html;
use app\components\DateTimeUtils;
use app\components\MasterValueUtils;
use app\components\NumberUtils;
$formModeValue = $formMode[MasterValueUtils::PG_MODE_NAME];
$this->title = Yii::t('fin.deposit', 'Create Fixed Deposit');
if ($formModeValue === MasterValueUtils::PG_MODE_EDIT) {
    $this->title = Yii::t('fin.deposit', 'Edit Fixed Deposit');
} elseif ($formModeValue === MasterValueUtils::PG_MODE_COPY) {
    $this->title = Yii::t('fin.deposit', 'Copy Fixed Deposit');
}
$openingDate = DateTimeUtils::getDateFromDB($model->opening_date);
$closingDate = DateTimeUtils::getDateFromDB($model->closing_date);
$dateDiff = $closingDate->diff($openingDate);
?>

<div class="box box-default">
    <div class="box-header with-border"><h3 class="box-title"><?php 
echo Yii::t('fin.form', 'Confirm Values');
?>
</h3></div>
    <div id="finDepositConfirmForm" class="box-body"><?php 
$form = ActiveForm::begin();
?>
        <div class="row"><div class="col-md-12">
            <table class="table table-bordered">
                <tr>
                    <th class="warning" style="width: 200px;"><?php 
 public function actionCopy($id)
 {
     $this->objectId = $id;
     $model = FinTimeDepositTran::findOne(['transactions_id' => $id, 'delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]);
     $renderView = 'copy';
     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('fin.models', 'Fixed Deposit')]));
     } else {
         // master value
         $phpFmShortDate = DateTimeUtils::getPhpDateFormat();
         $arrTimedepositTrantype = MasterValueUtils::getArrData('fin_timedeposit_trantype');
         $arrSavingAccount = ModelUtils::getArrData(FinAccount::find()->select(['account_id', 'account_name'])->where(['delete_flag' => 0, 'account_type' => 4])->orderBy('account_type, order_num'), 'account_id', 'account_name');
         $arrCurrentAssets = ModelUtils::getArrData(FinAccount::find()->select(['account_id', 'account_name'])->where(['delete_flag' => 0, 'account_type' => [1, 2]])->orderBy('account_type, order_num'), 'account_id', 'account_name');
         // 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);
         // reset value
         if (Yii::$app->request->getIsGet()) {
             $openingDate = DateTimeUtils::getDateFromDB($model->opening_date);
             $closingDate = DateTimeUtils::getDateFromDB($model->closing_date);
             $dateDiff = $closingDate->diff($openingDate);
             $interval = 'P' . $dateDiff->m . 'M';
             DateTimeUtils::addDateTime($closingDate, $interval, null, false);
             $model->opening_date = $model->closing_date;
             $model->closing_date = $closingDate->format(DateTimeUtils::FM_DB_DATE);
             $model->interest_add = 0;
             $model->add_flag = MasterValueUtils::MV_FIN_TIMEDP_TRANTYPE_ADDING;
         }
         // init value
         FinTimeDepositTran::$_PHP_FM_SHORTDATE = $phpFmShortDate;
         FinTimeDepositTran::$_ARR_SAVING_ACOUNT = $arrSavingAccount;
         $model->scenario = MasterValueUtils::SCENARIO_COPY;
         $renderData = ['model' => $model, 'phpFmShortDate' => $phpFmShortDate, 'arrTimedepositTrantype' => $arrTimedepositTrantype, 'arrSavingAccount' => $arrSavingAccount, 'arrCurrentAssets' => $arrCurrentAssets];
         switch ($submitMode) {
             case MasterValueUtils::SM_MODE_INPUT:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $renderView = 'confirm';
                     $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_COPY];
                 }
                 break;
             case MasterValueUtils::SM_MODE_CONFIRM:
                 $isValid = $model->validate();
                 if ($isValid) {
                     $result = $this->copyFixedDeposit($model);
                     if ($result === true) {
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_SUCCESS, Yii::t('common', '{record} has been saved successfully.', ['record' => Yii::t('fin.models', 'Fixed Deposit')]));
                         return Yii::$app->getResponse()->redirect(Url::to(['index']));
                     } else {
                         Yii::$app->session->setFlash(MasterValueUtils::FLASH_ERROR, $result);
                         $renderView = 'confirm';
                         $renderData['formMode'] = [MasterValueUtils::PG_MODE_NAME => MasterValueUtils::PG_MODE_COPY];
                     }
                 }
                 break;
             case MasterValueUtils::SM_MODE_BACK:
                 break;
             default:
                 break;
         }
     }
     // render GUI
     return $this->render($renderView, $renderData);
 }
 /**
  * @param OefPurchase $purchase
  * @param OefFundCertificate $condition
  */
 public function initialize($purchase, $condition)
 {
     $this->sip_date = DateTimeUtils::getDateFromDB($purchase->sip_date);
     $this->sip_months = DateTimeUtils::diffMonths($this->sip_date, $condition->sell_date_obj);
     parent::initialize($purchase, $condition);
 }
 public function actionDeposit()
 {
     $model = new FinTotalInterestMonth();
     $fmShortDatePhp = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_PHP, null);
     $startDateJui = DateTimeUtils::parse('20151001', DateTimeUtils::FM_DEV_DATE, $fmShortDatePhp);
     $fmKeyPhp = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_PHP, null, DateTimeUtils::FM_KEY_FMONTH);
     $fmKeyJui = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_JUI, null, DateTimeUtils::FM_KEY_FMONTH);
     $td = DateTimeUtils::getNow();
     // is get page than reset value
     if (Yii::$app->request->getIsGet()) {
         $tdTimestamp = $td->getTimestamp();
         $tdInfo = getdate($tdTimestamp);
         $thismonth = $td->format($fmKeyPhp);
         // for report Model
         $model->fmonth = $thismonth;
         // for search Model
         $model->fmonth_to = $thismonth;
         $model->fmonth_from = DateTimeUtils::parse($tdInfo[DateTimeUtils::FN_KEY_GETDATE_YEAR] - 1 . '0101', DateTimeUtils::FM_DEV_DATE, $fmKeyPhp);
     } else {
         // submit data
         $postData = Yii::$app->request->post();
         $model->load($postData);
         $submitMode = isset($postData[MasterValueUtils::SM_MODE_NAME]) ? $postData[MasterValueUtils::SM_MODE_NAME] : false;
         switch ($submitMode) {
             case MasterValueUtils::SM_MODE_INPUT:
                 $reportMonthStr = DateTimeUtils::parse($model->fmonth, $fmKeyPhp, DateTimeUtils::FM_DEV_YM) . '01';
                 $reportMonthObj = DateTimeUtils::parse($reportMonthStr, DateTimeUtils::FM_DEV_DATE);
                 $reportMonthInfo = getdate($reportMonthObj->getTimestamp());
                 $year = $reportMonthInfo[DateTimeUtils::FN_KEY_GETDATE_YEAR];
                 $month = $reportMonthInfo[DateTimeUtils::FN_KEY_GETDATE_MONTH_INT];
                 $reportModel = FinTotalInterestMonth::findOne(['year' => $year, 'month' => $month, 'delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]);
                 if (is_null($reportModel)) {
                     $reportModel = new FinTotalInterestMonth();
                     $reportModel->year = $year;
                     $reportModel->month = $month;
                 }
                 $reportMonthStr = $year . str_pad($month, 2, '0', STR_PAD_LEFT);
                 $reportMonth = DateTimeUtils::parse($reportMonthStr . '01', DateTimeUtils::FM_DEV_DATE);
                 $fromDate = $reportMonth->format(DateTimeUtils::FM_DB_DATE);
                 DateTimeUtils::addDateTime($reportMonth, 'P1M', null, false);
                 $toDate = DateTimeUtils::subDateTime($reportMonth, 'P1D', DateTimeUtils::FM_DB_DATE);
                 $interestUnitQuery = (new Query())->select(['start_date', 'end_date', 'interest_unit']);
                 $interestUnitQuery->from('fin_total_interest_unit')->where(['<=', 'start_date', $toDate]);
                 $interestUnitQuery->andWhere(['OR', ['>=', 'end_date', $fromDate], ['is', 'end_date', null]]);
                 $totalInterestMonth = 0;
                 $arrInterestUnit = $interestUnitQuery->createCommand()->queryAll();
                 foreach ($arrInterestUnit as $interestUnit) {
                     $unit = $interestUnit['interest_unit'];
                     $currentDateObj = DateTimeUtils::getDateFromDB($interestUnit['start_date']);
                     $startDate = $currentDateObj->format(DateTimeUtils::FM_DEV_DATE);
                     $endDate = empty($interestUnit['end_date']) ? DateTimeUtils::formatNow(DateTimeUtils::FM_DEV_DATE) : DateTimeUtils::formatDateFromDB($interestUnit['end_date'], DateTimeUtils::FM_DEV_DATE);
                     while ($startDate <= $endDate) {
                         $currentMonthStr = $currentDateObj->format(DateTimeUtils::FM_DEV_YM);
                         if ($currentMonthStr == $reportMonthStr) {
                             $totalInterestMonth += $unit;
                         }
                         DateTimeUtils::addDateTime($currentDateObj, 'P1D', null, false);
                         $startDate = $currentDateObj->format(DateTimeUtils::FM_DEV_DATE);
                     }
                 }
                 $reportModel->term_interest = intval($totalInterestMonth);
                 $reportModel->save();
                 Yii::$app->session->setFlash(MasterValueUtils::FLASH_SUCCESS, Yii::t('common', 'Monthly Interest Report of {month} has been saved successfully.', ['month' => $model->fmonth]));
                 return Yii::$app->getResponse()->redirect(Url::to(['deposit']));
                 break;
             default:
                 break;
         }
     }
     $renderData = ['fmKeyJui' => $fmKeyJui, 'fmKeyPhp' => $fmKeyPhp, 'model' => $model, 'startDateJui' => $startDateJui];
     $fMonthInfo = getdate(DateTimeUtils::parse($model->fmonth_from, $fmKeyPhp)->getTimestamp());
     $tMonthInfo = getdate(DateTimeUtils::parse($model->fmonth_to, $fmKeyPhp)->getTimestamp());
     $fYear = $fMonthInfo[DateTimeUtils::FN_KEY_GETDATE_YEAR];
     $fMonth = $fMonthInfo[DateTimeUtils::FN_KEY_GETDATE_MONTH_INT];
     $fMonthMM = str_pad($fMonth, 2, '0', STR_PAD_LEFT);
     $tYear = $tMonthInfo[DateTimeUtils::FN_KEY_GETDATE_YEAR];
     $tMonth = $tMonthInfo[DateTimeUtils::FN_KEY_GETDATE_MONTH_INT];
     $tMonthMM = str_pad($tMonth, 2, '0', STR_PAD_LEFT);
     $gridData = null;
     $resultModel = FinTotalInterestMonth::find()->where(['delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE])->andWhere(['>=', 'year', $fYear])->andWhere(['OR', ['>', 'year', $fYear], ['>=', 'month', $fMonth]])->andWhere(['<=', 'year', $tYear])->andWhere(['OR', ['<', 'year', $tYear], ['<=', 'month', $tMonth]])->orderBy('year, month')->all();
     if (count($resultModel) > 0) {
         // Init data for chart
         $sMonth = $fYear . $fMonthMM . '01';
         $eMonth = $tYear . $tMonthMM . '01';
         $currentMonthObj = DateTimeUtils::parse($sMonth, DateTimeUtils::FM_DEV_DATE);
         $arrDataChartTemp = [];
         while ($sMonth <= $eMonth) {
             $arrDataChartTemp[$sMonth] = ['noterm' => 0, 'term' => 0, 'total' => 0];
             DateTimeUtils::addDateTime($currentMonthObj, 'P1M', null, false);
             $sMonth = $currentMonthObj->format(DateTimeUtils::FM_DEV_DATE);
         }
         $firstResult = $resultModel[0];
         $prevNoterm = $firstResult->noterm_interest;
         $prevTerm = $firstResult->term_interest;
         $prevTotal = $prevNoterm + $prevTerm;
         $gridData = [];
         foreach ($resultModel as $rm) {
             $key = $rm->year . str_pad($rm->month, 2, '0', STR_PAD_LEFT) . '01';
             $total = $rm->noterm_interest + $rm->term_interest;
             $compareNoterm = $rm->noterm_interest - $prevNoterm;
             $compareTerm = $rm->term_interest - $prevTerm;
             $compareTotal = $total - $prevTotal;
             $prevNoterm = $rm->noterm_interest;
             $prevTerm = $rm->term_interest;
             $prevTotal = $prevNoterm + $prevTerm;
             $girdRow = ['month' => DateTimeUtils::parse($key, DateTimeUtils::FM_DEV_DATE), 'noterm' => $prevNoterm, 'term' => $prevTerm, 'total' => $prevTotal, 'compareNoterm' => $compareNoterm, 'compareTerm' => $compareTerm, 'compareTotal' => $compareTotal];
             $gridData[$key] = $girdRow;
             // data for chart
             if (isset($arrDataChartTemp[$key])) {
                 $arrDataChartTemp[$key]['noterm'] = $prevNoterm;
                 $arrDataChartTemp[$key]['term'] = $prevTerm;
                 $arrDataChartTemp[$key]['total'] = $prevTotal;
             }
         }
         // Total & Average
         $sumGridData = (new Query())->select(['SUM(noterm_interest) AS sum_noterm', 'AVG(noterm_interest) AS avg_noterm', 'SUM(term_interest) AS sum_term', 'AVG(term_interest) AS avg_term'])->from('fin_total_interest_month')->where(['=', 'delete_flag', MasterValueUtils::MV_FIN_FLG_DELETE_FALSE])->andWhere(['>=', 'year', $fYear])->andWhere(['OR', ['>', 'year', $fYear], ['>=', 'month', $fMonth]])->andWhere(['<=', 'year', $tYear])->andWhere(['OR', ['<', 'year', $tYear], ['<=', 'month', $tMonth]])->createCommand()->queryOne();
         $renderData['sumGridData'] = $sumGridData;
         // data for chart
         $arrLabelChart = [];
         $arrNotermDataChart = [];
         $arrTermDataChart = [];
         $arrTotalDataChart = [];
         $arrNotermAliasDataChart = [];
         $arrTermAliasDataChart = [];
         $arrTotalAliasDataChart = [];
         foreach ($arrDataChartTemp as $labelChart => $dataChartTemp) {
             $arrLabelChart[] = DateTimeUtils::parse($labelChart, DateTimeUtils::FM_DEV_DATE, $fmKeyPhp);
             $arrNotermDataChart[] = $dataChartTemp['noterm'];
             $arrTermDataChart[] = $dataChartTemp['term'];
             $arrTotalDataChart[] = $dataChartTemp['total'];
             $arrNotermAliasDataChart[] = NumberUtils::format($dataChartTemp['noterm']);
             $arrTermAliasDataChart[] = NumberUtils::format($dataChartTemp['term']);
             $arrTotalAliasDataChart[] = NumberUtils::format($dataChartTemp['total']);
         }
         $renderData['chartData'] = json_encode(['label' => $arrLabelChart, 'noterm' => $arrNotermDataChart, 'notermAlias' => $arrNotermAliasDataChart, 'term' => $arrTermDataChart, 'termAlias' => $arrTermAliasDataChart, 'total' => $arrTotalDataChart, 'totalAlias' => $arrTotalAliasDataChart], JSON_NUMERIC_CHECK);
     }
     $renderData['gridData'] = $gridData;
     // sum Term Interest current month
     $beginCurrentMonth = DateTimeUtils::parse($td->format(DateTimeUtils::FM_DEV_YM) . '01', DateTimeUtils::FM_DEV_DATE);
     $endCurrentMonth = DateTimeUtils::addDateTime($beginCurrentMonth, 'P1M');
     DateTimeUtils::subDateTime($endCurrentMonth, 'P1D', null, false);
     $currentInterestUnitQuery = (new Query())->select(['start_date', 'end_date', 'interest_unit']);
     $currentInterestUnitQuery->from('fin_total_interest_unit')->where(['<=', 'start_date', $endCurrentMonth->format(DateTimeUtils::FM_DB_DATE)]);
     $currentInterestUnitQuery->andWhere(['OR', ['>=', 'end_date', $beginCurrentMonth->format(DateTimeUtils::FM_DB_DATE)], ['is', 'end_date', null]]);
     $currentTermInterestMonth = 0;
     $arrCurrentInterestUnit = $currentInterestUnitQuery->createCommand()->queryAll();
     $requireCurrentMonthStr = $beginCurrentMonth->format(DateTimeUtils::FM_DEV_YM);
     foreach ($arrCurrentInterestUnit as $currentInterestUnit) {
         $currentUnit = $currentInterestUnit['interest_unit'];
         $currentDateObj = DateTimeUtils::getDateFromDB($currentInterestUnit['start_date']);
         $currentStartDate = $currentDateObj->format(DateTimeUtils::FM_DEV_DATE);
         $currentEndDate = empty($currentInterestUnit['end_date']) ? DateTimeUtils::formatNow(DateTimeUtils::FM_DEV_DATE) : DateTimeUtils::formatDateFromDB($currentInterestUnit['end_date'], DateTimeUtils::FM_DEV_DATE);
         while ($currentStartDate <= $currentEndDate) {
             $currentMonthStr = $currentDateObj->format(DateTimeUtils::FM_DEV_YM);
             if ($currentMonthStr == $requireCurrentMonthStr) {
                 $currentTermInterestMonth += $currentUnit;
             }
             DateTimeUtils::addDateTime($currentDateObj, 'P1D', null, false);
             $currentStartDate = $currentDateObj->format(DateTimeUtils::FM_DEV_DATE);
         }
     }
     // sum No Term Interest current month
     $currentNotermInterestMonth = 0;
     $arrFinAccount = FinAccount::find()->where(['delete_flag' => 0, 'account_type' => MasterValueUtils::MV_FIN_ACCOUNT_TYPE_CURRENT])->all();
     foreach ($arrFinAccount as $finAccount) {
         $instance = $finAccount->instance();
         $instance->initialize();
         $currentNotermInterestMonth += $instance->capital + $instance->now_interest;
     }
     $renderData['sumCurrentInterestData'] = ['term' => $currentTermInterestMonth, 'noterm' => $currentNotermInterestMonth];
     return $this->render('deposit', $renderData);
 }