public function compare() { // get a list of all months: $months = array(); $first = BaseController::getFirst(); $first->modify('first day of this month midnight'); $today = new Carbon('now'); $today->modify('first day of this month midnight'); $prev = clone $today; $prev->sub(new DateInterval('P2D')); $prev->modify('first day of this month midnight'); while ($first <= $today) { $index = $first->format('Y-m-d'); $months[$index] = $first->format('F Y'); $first->add(new DateInterval('P1M')); } // account list: $accs = Auth::user()->accounts()->get(); $accounts = array(0 => '(all accounts)'); foreach ($accs as $acc) { $accounts[intval($acc->id)] = Crypt::decrypt($acc->name); } $account = Setting::getSetting('defaultCheckingAccount'); return View::make('pages.compare')->with('months', $months)->with('thisMonth', $today)->with('prevMonth', $prev)->with('account', $account)->with('accounts', $accounts); }
protected function injectCarbon() { $dt = new Carbon(Input::get('date', Carbon::now()->subDay()->format('Y-m-d H:i:s'))); if (NULL !== Input::get('date')) { $dt->modify('last day of this month'); } $dt->hour = 23; $dt->minute = 59; $dt->second = 59; return $dt; }
public static function _determinePeriod() { // get the period from the session: $sessionPeriod = Session::get('period'); $today = new Carbon('now'); $today->modify('midnight'); if (is_null($sessionPeriod)) { // new period: today at midnight: $sessionPeriod = new Carbon('now'); $sessionPeriod->modify('midnight'); Session::put('period', $sessionPeriod); } // if there is something in the URL: if (!is_null(Request::segment(2)) && !is_null(Request::segment(3))) { if (intval(Request::segment(2)) > 1000) { // crude check for year. $string = '1 ' . Request::segment(3) . ' ' . Request::segment(2); $date = new Carbon('now'); try { $date = new Carbon($string); } catch (Exception $e) { } // check if matches today: if ($date > $today) { // in the future: $date->modify('first day of this month'); } else { if ($date < $today) { // in the past: $date->modify('last day of this month'); } else { if ($date == $today) { $date = clone $today; } else { Log::error('No catch for date'); } } } Session::put('period', $date); } } }
public function homeOverviewChart($id = 0, $date = null) { $date = is_null($date) ? Session::get('period') : $date; $key = cacheKey('Budget', 'homeOverviewChart', $id, $date); if (Cache::has($key)) { return Response::json(Cache::get($key)); } // 30 days into the past. $end = clone Session::get('period'); $end->modify('last day of this month '); $today = new Carbon('now'); $today->modify('midnight'); $past = clone $end; $past->modify('first day of this month midnight'); $budget = Auth::user()->budgets()->find($id); $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date', 'p' => array('role' => 'domain')), array('id' => 'balance', 'label' => 'Left', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'boolean', 'p' => array('role' => 'certainty'))), 'rows' => array()); $index = 0; $balance = $budget->amount; // get the prediction points (if any): $points = $budget->budgetpredictionpoints()->get(); $prediction = array(); foreach ($points as $p) { $prediction[intval($p->day)] = floatval($p->amount); } while ($past <= $end) { $month = intval($past->format('n')) - 1; $year = intval($past->format('Y')); $day = intval($past->format('j')); $data['rows'][$index]['c'][0]['v'] = 'Date(' . $year . ', ' . $month . ', ' . $day . ')'; if ($past <= $today) { $balance = $budget->left($past); $data['rows'][$index]['c'][1]['v'] = $balance; $data['rows'][$index]['c'][2]['v'] = true; } else { $balance = $balance - (isset($prediction[$day]) ? $prediction[$day] : 0); $data['rows'][$index]['c'][1]['v'] = $balance; $data['rows'][$index]['c'][2]['v'] = false; } $past->add(new DateInterval('P1D')); $index++; } Cache::put($key, $data, 1440); return Response::json($data); }
public function homeOverviewChart($id = 0, $date = null) { $date = is_null($date) ? Session::get('period') : $date; $key = cacheKey('Account', 'homeOverviewChart', $id, $date); if (Cache::has($key)) { return Response::json(Cache::get($key)); } // 30 days into the past. $today = new Carbon(Session::get('period')->format('Y-m-d')); // we do some fixing in case we're in the future: $actuallyToday = new Carbon('now'); if ($today > $actuallyToday) { $today->modify('last day of this month'); } $past = clone $today; $past->subDays(30); $account = Auth::user()->accounts()->find($id); $data = array('cols' => array(array('id' => 'date', 'label' => 'Date', 'type' => 'date', 'p' => array('role' => 'domain')), array('id' => 'balance', 'label' => 'Balance', 'type' => 'number', 'p' => array('role' => 'data'))), 'rows' => array()); $bdp_q = $account->balancedatapoints()->where('date', '>=', $past->format('Y-m-d'))->where('date', '<=', $today->format('Y-m-d'))->get(); $bdp = array(); foreach ($bdp_q as $b) { $bdp[$b->date] = floatval($b->balance); } $index = 0; while ($past <= $today) { $month = intval($past->format('n')) - 1; $year = intval($past->format('Y')); $day = intval($past->format('j')); $data['rows'][$index]['c'][0]['v'] = 'Date(' . $year . ', ' . $month . ', ' . $day . ')'; $balance = isset($bdp[$past->format('Y-m-d')]) ? $bdp[$past->format('Y-m-d')] : null; $data['rows'][$index]['c'][1]['v'] = $balance; $past->add(new DateInterval('P1D')); $index++; } Cache::put($key, $data, 1440); return Response::json($data); }
public function predictionChart() { // in order to predict the future, we look at the past. //$baseAccount = ?; //$startBalance = ?; $setting = Auth::user()->settings()->where('name', '=', 'defaultAmount')->first(); $balance = intval(Crypt::decrypt($setting->value)); $account = Auth::user()->accounts()->orderBy('id', 'ASC')->first(); $debug = Input::get('debug') == 'true' ? true : false; $this->_debug = $debug; $key = $debug ? cacheKey('prediction', Session::get('period'), rand(1, 10000)) : cacheKey('prediction', Session::get('period')); // a setting related to corrections: $doCorrect = Setting::getSetting('correctPredictionChart') == 'true' ? true : false; if (Cache::has($key)) { $data = Cache::get($key); } else { $data = array('cols' => array(array('id' => 'day', 'label' => 'Day of the month', 'type' => 'string', 'p' => array('role' => 'domain')), array('id' => 'actualbalance', 'label' => 'Current balance', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'boolean', 'p' => array('role' => 'certainty')), array('id' => 'predictedbalance', 'label' => 'Predicted balance', 'type' => 'number', 'p' => array('role' => 'data')), array('type' => 'number', 'p' => array('role' => 'interval')), array('type' => 'number', 'p' => array('role' => 'interval'))), 'rows' => array()); // set the data array: // some working vars: $first = BaseController::getFirst(); $this->_e('FIRST is ' . $first->format('d M Y')); $today = new Carbon('now'); $this->_e('Today is ' . $today->format('d M Y')); $chartdate = new Carbon('now'); $chartdate->modify('first day of this month'); $index = 0; $this->_e(''); $diff = $first->diff($today); $months = $diff->y * 12 + $diff->m; unset($diff); $specificAmount = Auth::user()->settings()->where('name', '=', 'monthlyAmount')->where('date', '=', $today->format('Y-m-d'))->first(); if ($specificAmount) { $balance = floatval(Crypt::decrypt($specificAmount->value)); } $this->_e('Opening balance: ' . $balance); // loop over each day of the month: $this->_e('start month loop'); for ($i = 1; $i <= intval($today->format('t')); $i++) { $this->_e('Now at day #' . $i); // this array will be used to collect average amounts: $this->_e('Chartdate is: ' . $chartdate->format('Y-m-d')); $this->_e('Today is: ' . $today->format('Y-m-d')); if ($doCorrect && $chartdate > $today || !$doCorrect) { $average = array(); // loop over each month: // get all transaction results for this day of the month: $transactions = Auth::user()->transactions()->where('amount', '<', 0)->where('onetime', '=', 0)->where(DB::Raw('DATE_FORMAT(`date`,"%e")'), '=', $i)->orderBy('amount', 'ASC')->get(); // lets see what we have if (count($transactions) > 0) { $min = floatval($transactions[count($transactions) - 1]->amount) * -1; $max = floatval($transactions[0]->amount) * -1; // fill the array for the averages later on: foreach ($transactions as $t) { //$this->_e('Add to avg['.count($average).'] for transactions: ' . (floatval($t->amount) * -1)); $average[] = floatval($t->amount) * -1; } } else { $min = 0; $max = 0; } // calc avg: $avg = $months > 0 ? array_sum($average) / $months : array_sum($average); //$this->_e('New avg: ' . $avg); $this->_e('Max: ' . $max . ', min: ' . $min . ', avg: ' . $avg); $data['rows'][$index]['c'][0]['v'] = $chartdate->format('j F'); $data['rows'][$index]['c'][1]['v'] = $account->balance($chartdate); // actual balance if ($chartdate > $today) { $data['rows'][$index]['c'][2]['v'] = false; } else { $data['rows'][$index]['c'][2]['v'] = true; } $data['rows'][$index]['c'][3]['v'] = $balance - $avg; // predicted balance $data['rows'][$index]['c'][4]['v'] = $balance - $max; // predicted max expenses. $data['rows'][$index]['c'][5]['v'] = $balance - $min; // predicted max expenses. $balance = $balance - $avg; } else { // don't predict. //$balance = $account->balance($chartdate); $this->_e('No prediction today!'); $balance = $account->balance($chartdate); $data['rows'][$index]['c'][0]['v'] = $chartdate->format('j F'); $data['rows'][$index]['c'][1]['v'] = $balance; // actual balance $data['rows'][$index]['c'][2]['v'] = true; $data['rows'][$index]['c'][3]['v'] = null; $data['rows'][$index]['c'][4]['v'] = null; $data['rows'][$index]['c'][5]['v'] = null; } $index++; $chartdate->addDay(); $this->_e(' '); } Cache::put($key, $data, 1440); } if ($debug) { return '<pre>' . print_r($data, true) . '</pre>'; } return Response::json($data); }
/** * return all days of specified month * * @param Carbon $month * @return mixed */ public static function daysInMonth(Carbon $month) { return self::where(function ($query) use($month) { $query->where('date', '>=', clone $month); $query->where('date', '<=', $month->modify('last day of this month')); //TODO: change it for inner join })->where('user_id', '=', Auth::user()->id)->get(); }
/** * Internally used by modify method to calculate calendar-aware modifications * * @param array $matches * @return string An empty string */ protected function modifyCallback($matches) { if (!empty($matches[1])) { parent::modify($matches[1]); } list($y, $m, $d) = explode('-', $this->format('Y-n-j')); $change = strtolower($matches[2]); $unit = strtolower($matches[3]); switch ($change) { case "next": $change = 1; break; case "last": case "previous": $change = -1; break; } switch ($unit) { case "month": $m += $change; if ($m > 12) { $y += floor($m / 12); $m = $m % 12; } elseif ($m < 1) { $y += ceil($m / 12) - 1; $m = $m % 12 + 12; } break; case "year": $y += $change; break; } $this->setDate($y, $m, $d); return ''; }
protected function getQuery(Carbon $date) { // Prevent reference link error $cloneDate = new Carbon($date->format('Y-m-d H:i:s')); $pszCurrentYear = $cloneDate->format('Y'); $pszCurrentMonth = $cloneDate->format('m'); $pszCurrentDay = $cloneDate->format('d'); $pszPastYear = $cloneDate->subYear()->format('Y'); $pszTailDate = $pszCurrentMonth . $pszCurrentDay; $pszPastYearLastDayThisMonth = $cloneDate->modify('last day of this month')->format('d'); return str_replace(['$pszCurrentYear', '$pszCurrentMonth', '$pszCurrentDay', '$pszPastYearLastDayThisMonth', '$pszPastYear', '$pszTailDate'], [$pszCurrentYear, $pszCurrentMonth, $pszCurrentDay, $pszPastYearLastDayThisMonth, $pszPastYear, $pszTailDate], Processor::getStorageSql('RetailSales.sql')); }