/** * Updates the list from database, in the right format for rendering the partial * @param options An array of options */ private function updateAssetList($options = array()) { // Defaulting options $options = array_merge($this->listOptionsDefault, $options); $this->assetList = AssetModel::where('title', '<>', ''); // Applying options if ($options['include_old'] == false) { $this->assetList = $this->assetList->where(function ($query) { $query->where('display_to', '>=', date('Y-m-d'))->orWherenull('display_to'); }); } // Getting filtered list $this->assetList = $this->assetList->orderBy('title', 'ASC'); $this->assetList = $this->assetList->paginate(15); if ($this->assetList && $this->assetList->count()) { $this->assetList->each(function ($asset) { $asset->setUrl($this->controller, $this->assetDetails); }); } }
public function onCreateAsset() { $this->asset = AssetModel::create(post('Asset')); $url = $this->pageUrl($this->page->baseFileName, ['asset_id' => $this->asset->id, 'action' => 'view']); Flash::success(trans('piratmac.smmm::lang.messages.success_creation')); return Redirect::to($url); }
public function onGraphRender() { if (strtotime(post('basis_date')) < strtotime(post('start_date')) || strtotime(post('basis_date')) > strtotime(post('end_date'))) { throw new ApplicationException(trans('piratmac.smmm::lang.messages.basis_date_outside_scope')); } if (!is_array(post('asset')) && !is_array(post('portfolio'))) { throw new ApplicationException(trans('piratmac.smmm::lang.messages.no_element_selected')); } /**************** Determining dates and X-scale *************/ $dates['start'] = post('start_date'); $dates['start_timestamp'] = strtotime($dates['start']); $dates['end'] = post('end_date'); $dates['end_timestamp'] = strtotime($dates['end']); /* Determining the scale */ $nb_days = ($dates['end_timestamp'] - $dates['start_timestamp']) / 60 / 60 / 24; switch (true) { case $nb_days <= 210: $date_scale = 60 * 60 * 24; break; case $nb_days <= 701: $date_scale = 60 * 60 * 24 * 7; break; case $nb_days <= 1401: $date_scale = 60 * 60 * 24 * 7 * 2; break; default: $date_scale = 60 * 60 * 24 * 30; break; } $timeDots = range($dates['start_timestamp'], $dates['end_timestamp'], $date_scale); // Rounding basis date to match one of the points on x-scale $dates['basis'] = strtotime(post('basis_date')); $dates['basis'] = date('Y-m-d', $dates['start_timestamp'] + $date_scale * round(($dates['basis'] - $dates['start_timestamp']) / $date_scale)); $dates['basis_timestamp'] = strtotime($dates['basis']); /**************** Get the data *************/ // Get selected portfolios $portfolios = PortfolioModel::whereIn('id', post('portfolio'))->with(['heldAssets' => function ($query) use($dates) { $query->where('date_from', '<=', $dates['end'])->where('date_to', '>=', $dates['start']); }])->get(); // Get all assets held (directly or not) $allAssets = []; $portfolios->each(function ($portfolio, $key = '') use(&$allAssets) { $allAssets = array_merge($allAssets, explode('#', $portfolio->heldAssets->implode('id', '#'))); }); $allAssets = array_merge($allAssets, is_array(post('asset')) ? post('asset') : []); $assets = AssetModel::whereIn('id', $allAssets)->with(['value' => function ($query) { $query->orderBy('date'); }])->get(); unset($allAssets); // Assets to display $assetsToDisplay = array_merge(is_array(post('asset')) ? post('asset') : [], is_array(post('portfolio')) ? post('portfolio') : []); /**************** Transform data to match target format + use basis values *************/ $seriesData = []; $assets->each(function ($asset, $key = '') use($timeDots, &$seriesData, $dates) { $a_ID = $asset->id; if ($asset->type == 'cash') { return; } $seriesData[$a_ID] = []; $seriesData[$a_ID]['label'] = $asset->title; foreach ($timeDots as $day_time) { $value = $asset->value->where('date', date('Y-m-d', $day_time))->last(); if (is_null($value)) { $value = $asset->value->filter(function ($assetValue) use($day_time) { return strtotime($assetValue->date) <= $day_time; })->last(); } if (is_null($value)) { throw new ApplicationException(sprintf(trans('piratmac.smmm::lang.messages.no_value_found_for_on'), $asset->title, date('Y-m-d', $day_time))); } $seriesData[$a_ID]['value'][$day_time] = $value->value; } $seriesData[$a_ID]['basis_value'] = $seriesData[$a_ID]['value'][$dates['basis_timestamp']]; foreach ($timeDots as $day_time) { $seriesData[$a_ID]['value_basis'][$day_time] = $seriesData[$a_ID]['value'][$day_time] / $seriesData[$a_ID]['basis_value']; } }); $portfolios->each(function ($portfolio, $key = '') use(&$timeDots, $dates, &$seriesData) { $p_ID = $portfolio->id; $seriesData[$p_ID] = []; $seriesData[$p_ID]['label'] = $portfolio->description; $seriesData[$p_ID]['value'] = []; $seriesData[$p_ID]['value_basis'] = []; foreach ($timeDots as $day_time) { $seriesData[$p_ID]['value'][$day_time] = 0; $heldAssets = $portfolio->heldAssets->filter(function ($heldAsset) use($day_time) { return strtotime($heldAsset->pivot->date_from) <= $day_time && strtotime($heldAsset->pivot->date_to) > $day_time; }); $heldAssets->each(function ($heldAsset) use($day_time, &$seriesData, $p_ID) { $hA_ID = $heldAsset->id; if ($heldAsset->type == 'cash') { $seriesData[$p_ID]['value'][$day_time] += $heldAsset->pivot->asset_count; } else { $seriesData[$hA_ID]['held'][$day_time] = true; $seriesData[$p_ID]['value'][$day_time] += $heldAsset->pivot->asset_count * $seriesData[$hA_ID]['value'][$day_time]; } }); } $seriesData[$p_ID]['basis_value'] = $seriesData[$p_ID]['value'][$dates['basis_timestamp']]; foreach ($timeDots as $day_time) { $seriesData[$p_ID]['value_basis'][$day_time] = round($seriesData[$p_ID]['value'][$day_time] / $seriesData[$p_ID]['basis_value'], 3); } }); /**************** Prepare rendering *************/ $timeDots_rendering = []; $timeDots_rendering['labels'] = '"' . implode(array_map(function ($dot_dates) { return date('d-m-Y', $dot_dates); }, $timeDots), '", "') . '"'; $timeDots_rendering['datasets'] = []; foreach ($seriesData as $assetID => $assetValue) { if (!in_array($assetID, $assetsToDisplay)) { continue; } $timeDots_rendering['datasets'][$assetID]['label'] = $seriesData[$assetID]['label']; $timeDots_rendering['datasets'][$assetID]['data'] = implode(', ', array_map(function ($value) { return sprintf("%+.3f", $value); }, $seriesData[$assetID]['value_basis'])); $timeDots_rendering['datasets'][$assetID]['data_label'] = []; foreach ($seriesData[$assetID]['value_basis'] as $day_time => $value_basis) { $timeDots_rendering['datasets'][$assetID]['data_label'][] = '"' . sprintf("%.2f", $seriesData[$assetID]['value'][$day_time]) . ' (' . sprintf("%+.1f %%", ($value_basis - 1) * 100) . ')"'; } $timeDots_rendering['datasets'][$assetID]['data_label'] = implode(', ', $timeDots_rendering['datasets'][$assetID]['data_label']); } $this->timeDots_rendering = $timeDots_rendering; \Debugbar::info($this->timeDots_rendering); }