public function testProgressLessThanZero() { $obj = $this->generateProgress(-500); $this->withSession(['Auth' => $obj['user']])->visit('/progress/?id=' . $obj['plan']->id)->see('Too bad you use'); $obj['user']->delete(); $obj['plan']->delete(); Monthly::where('plan_id', '=', $obj['plan']->id)->delete(); }
public function getCharts(Request $request) { if ($this->user != null && Session::has('Plan') && Session::get('Plan') != "") { $plan = Plan::find(Session::get('Plan')); if ($plan) { $months = $plan->months()->get(); $attach["Plan"] = $plan; $attach["MonthList"] = $months; if ($plan) { $attach['Month'] = array(); for ($i = 0; $i < $plan->period; $i++) { $attach['Month'][] = date('F', strtotime('+' . $i . ' month', strtotime($plan->created_at))); if (isset($months[$i])) { $attach['Limit'][] = $months[$i]->limit; $attach['Progress'][] = $months[$i]->progress; } else { $attach['Limit'][] = ""; $attach['Progress'][] = ""; } } $attach["Daily"] = array(); if (isset($months)) { if (isset($months[count($months) - 1])) { $month = $months[count($months) - 1]; $days = $month->days()->get(); $attach["Daily"]['Day'] = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y')); for ($i = 1; $i <= $attach["Daily"]['Day']; $i++) { $attach["Daily"]["Expense"][$i - 1] = 0; $attach["Daily"]["Income"][$i - 1] = 0; foreach ($days as $day) { if ($i == date('d', strtotime($day->date))) { $attach["Daily"]["Expense"][$i - 1] = $day->expense; $attach["Daily"]["Income"][$i - 1] = $day->income; } } } } } $attach["Category"] = array(); if ($request->month != null) { $month = Monthly::find($request->month); $categories = Category::where('user_id', '=', $this->user->id)->orWhere('user_id', '=', 0)->get(); $start = date('Y-m-d', strtotime('+' . $month->month - 1 . ' month', strtotime($plan->created_at))); $end = date('Y-m-d', strtotime('+' . $month->month . ' month', strtotime($plan->created_at))); $attach["SumIncome"] = 0; $attach["SumExpense"] = 0; foreach ($categories as $key => $value) { $expense = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $value->id)->where('type', '=', 0)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); $attach["SumExpense"] += $expense; $income = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $value->id)->where('type', '=', 1)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); $attach["SumIncome"] += $income; $rand = dechex(rand(0x0, 0xffffff)); if (intval($expense) > 0 || intval($income) > 0) { $attach["Category"][$value->name]["expense"] = $expense; $attach["Category"][$value->name]["income"] = $income; $attach["Category"][$value->name]["color"] = $rand; } } } } return view('home.chart')->with($attach); } } return redirect('/progress'); }
public function postDelete(Request $request) { if ($request->expense != null) { foreach ($request->expense as $key => $value) { $finance = Finance::find($value); if ($finance->type == 0) { $day = Daily::find($finance->daily_id); $day->expense -= $finance->amount; $day->save(); $month = Monthly::find($day->monthly_id); $month->progress += $finance->amount; $month->save(); $plan = Plan::find($month->plan_id); $plan->budget += $finance->amount; $plan->save(); } else { $day = Daily::find($finance->daily_id); $day->income -= $finance->amount; $day->save(); $month = Monthly::find($day->monthly_id); $month->progress -= $finance->amount; $month->save(); $plan = Plan::find($month->plan_id); $plan->budget -= $finance->amount; $plan->save(); } $finance->delete(); Session::forget("successes"); Session::put("successes", ["Delete expense successes fully."]); } } else { Session::forget("successes"); Session::put("successes", ["No item selected!"]); } return redirect()->back()->withInput(); }
public function getIndex(Request $request) { $successes = array(); $validator = Validator::make($request->all(), []); if (isset($request->id)) { $plan = $this->user->plans()->where('id', '=', $request->id)->first(); } else { if (Session::has('Plan')) { $plan = $this->user->plans()->where('id', '=', Session::get('Plan'))->first(); } } if (isset($plan)) { $now = new DateTime(date('m/d/Y')); $create = new DateTime($plan->created_at); // $create = new DateTime('2016-11-14'); $interval = $now->diff($create); $diff = $interval->format('%m'); if (sizeof($plan->months()->get()) <= $diff && $diff <= $plan->period || $diff == 0 && sizeof($plan->months()) <= $diff) { $month = new Monthly(); $month->plan_id = $plan->id; $month->status = 0; $month->month = sizeof($plan->months) + 1; $month->limit = $plan->expected; $month->progress = 0; $monthLeft = $plan->period - $diff; if ($monthLeft > 0) { $newExpected = ceil(($plan->target - $plan->budget) / $monthLeft); } else { $newExpected = 1; } $lastMonth = $plan->months()->orderBy('id', 'desc')->first(); if ($lastMonth) { if ($lastMonth->progress != $lastMonth->limit) { $month->limit = $newExpected; if ($lastMonth->progress > $lastMonth->limit) { $successes[] = "Very good! you have succeed " . ($lastMonth->progress - $lastMonth->limit) . " more than limit!"; } else { if ($lastMonth->progress < $lastMonth->limit && $lastMonth->progress >= 0) { $validator->errors()->add('User', 'Too bad cannot reach you goal last month! anyway, keep doing!'); } else { if ($lastMonth->progress < $lastMonth->limit && $lastMonth->progress < 0) { $validator->errors()->add('User', 'Too bad you use ' . $lastMonth->progress . ' more than limit last month!!'); } } } } } $month->save(); $attach['progress'] = $month->progress / $month->limit * 100; } else { $month = $plan->months()->orderBy('id', 'desc')->first(); if ($month) { if ($month->limit == 0) { $month->limit = 1; } $attach['progress'] = $month->progress / $month->limit * 100; } } $attach['plan'] = $plan; $attach['month'] = $month; $attach['category'] = []; $categories = Category::where('user_id', '=', $this->user->id)->orWhere('user_id', '=', 0)->get(); $start = date('Y-m-d', strtotime('+' . $month->month - 1 . ' month', strtotime($plan->created_at))); $end = date('Y-m-d', strtotime('+' . $month->month . ' month', strtotime($plan->created_at))); foreach ($categories as $category) { $sumIncome = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $category->id)->where('type', '=', 1)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); $sumExpense = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $category->id)->where('type', '=', 0)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); if ($sumIncome > 0 || $sumExpense > 0) { $attach['category'][] = ["name" => $category->name, "income" => $sumIncome, "expense" => $sumExpense]; } } if ($plan->budget >= $plan->target) { $successes[] = "Congratuation! You have reach your goal!!!"; } else { if ($diff >= $plan->period && $plan->budget < $plan->target) { $validator->errors()->add('User', 'This plan has been fail!! Please try again with more discipline!'); } } Session::forget('successes'); Session::put('successes', $successes); return view('progress.index')->with($attach)->withErrors($validator); } else { return view('progress.index'); } }
public function getIndex(Request $request) { $successes = array(); $validator = Validator::make($request->all(), []); if (isset($request->id)) { $plan = $this->user->plans()->where('id', '=', $request->id)->first(); } else { if (Session::has('Plan')) { $plan = $this->user->plans()->where('id', '=', Session::get('Plan'))->first(); } } if (isset($plan)) { $now = new DateTime(date('m/d/Y')); $create = new DateTime($plan->created_at); // $create = new DateTime('2016-11-14'); $interval = $now->diff($create); $diff = $interval->format('%m'); if (sizeof($plan->months()->get()) <= $diff && $diff <= $plan->period || $diff == 0 && sizeof($plan->months()) <= $diff) { $month = new Monthly(); $month->plan_id = $plan->id; $month->status = 0; $month->month = sizeof($plan->months()); $month->limit = $plan->expected; $month->progress = 0; $lastMonth = $plan->months()->orderBy('id', 'desc')->first(); if ($lastMonth) { if ($lastMonth->progress != $lastMonth->limit) { if ($lastMonth->progress > $lastMonth->limit) { $month->progress = $month->progress + ($lastMonth->progress - $lastMonth->limit); $successes[] = '<img src="https://pbs.twimg.com/media/CcBq-RpUkAEdOPm.jpg:large" height="50px" width="50px"/>' . "Very good! you have succeed " . ($lastMonth->progress - $lastMonth->limit) . " more than limit!"; //$successes[] = ; } else { if ($lastMonth->progress < $lastMonth->limit) { $month->limit += $lastMonth->progress; $validator->errors()->add('User', '<img src="https://pbs.twimg.com/media/CcBq-RpUkAEdOPm.jpg:large" height="50px" width="50px"/>' . 'Too bad you use ' . $lastMonth->progress . ' more than limit last month!!'); } } } } $month->save(); $attach['progress'] = $month->progress / $month->limit * 100; } else { $month = $plan->months()->orderBy('id', 'desc')->first(); if ($month) { if ($month->limit == 0) { $month->limit = 1; } $attach['progress'] = $month->progress / $month->limit * 100; } } $attach['plan'] = $plan; $attach['month'] = $month; $attach['category'] = []; $categories = Category::where('user_id', '=', $this->user->id)->orWhere('user_id', '=', 0)->get(); $start = date('Y-m-d', strtotime('+' . $month->month - 1 . ' month', strtotime($plan->created_at))); $end = date('Y-m-d', strtotime('+' . $month->month . ' month', strtotime($plan->created_at))); foreach ($categories as $category) { $sumIncome = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $category->id)->where('type', '=', 1)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); $sumExpense = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->where('monthly.id', '=', $month->id)->where('finance.category_id', '=', $category->id)->where('type', '=', 0)->where('daily.date', '>=', $start)->where('daily.date', '<=', $end)->sum('amount'); if ($sumIncome > 0 || $sumExpense > 0) { $attach['category'][] = ["name" => $category->name, "income" => $sumIncome, "expense" => $sumExpense]; } } Session::forget('successes'); Session::put('successes', $successes); return view('report.index')->with($attach)->withErrors($validator); } else { return view('report.index'); } }