public function actionAjaxchart() { // master value $fmShortDatePhp = DateTimeUtils::getDateFormat(DateTimeUtils::FM_KEY_PHP, null); $searchTradeDateFrom = isset($_POST['trade_date_from']) ? DateTimeUtils::parse($_POST['trade_date_from'], $fmShortDatePhp, DateTimeUtils::FM_DB_DATE) : false; $searchTradeDateTo = isset($_POST['trade_date_to']) ? DateTimeUtils::parse($_POST['trade_date_to'], $fmShortDatePhp, DateTimeUtils::FM_DB_DATE) : false; $searchMaxItemsChart = isset($_POST['max_items_chart']) ? $_POST['max_items_chart'] : 10; $dataQuery = OefNav::find()->where(['delete_flag' => MasterValueUtils::MV_FIN_FLG_DELETE_FALSE]); if (!empty($searchTradeDateFrom)) { $dataQuery->andWhere(['>=', 'trade_date', $searchTradeDateFrom]); } if (!empty($searchTradeDateTo)) { $dataQuery->andWhere(['<=', 'trade_date', $searchTradeDateTo]); } $dataQuery->orderBy('trade_date DESC')->limit($searchMaxItemsChart); $arrData = $dataQuery->all(); $count = count($arrData); $arrDataChart = []; $arrDataChartAlias = []; $arrLabelChart = []; while ($count > 0) { $count--; $data = $arrData[$count]; $arrDataChart[] = $data->nav_value; $arrDataChartAlias[] = NumberUtils::format($data->nav_value, 2); $arrLabelChart[] = DateTimeUtils::formatDateFromDB($data->trade_date, $fmShortDatePhp); } $renderData['chartData'] = count($arrDataChart) > 0 ? json_encode(['nav' => $arrDataChart, 'label' => $arrLabelChart, 'alias' => $arrDataChartAlias], JSON_NUMERIC_CHECK) : false; return $this->renderPartial('ajaxchart', $renderData); }
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); }