Example #1
0
 public function getReport(Request $request)
 {
     if ($this->user != null && Session::has('Plan') && Session::get('Plan') != "") {
         $plan = Plan::find(Session::get('Plan'));
         $attach["plan"] = $plan;
         $attach["Month"] = $plan->months()->get();
         $attach["Category"] = Category::where('user_id', '=', $this->user->id)->orWhere('user_id', '=', 0)->get();
         $attach['sumIncome'] = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->join('plan', 'plan.id', '=', 'monthly.plan_id')->where('plan.id', '=', $plan->id)->where('type', '=', 1)->sum('amount');
         $attach['sumExpense'] = Finance::join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->join('plan', 'plan.id', '=', 'monthly.plan_id')->where('plan.id', '=', $plan->id)->where('type', '=', 0)->sum('amount');
         if (isset($request->category) && isset($request->month)) {
             $cat = $request->category;
             $month = $request->month;
             $query = Finance::select('finance.name', 'category.name AS category', 'finance.created_at', 'plan.name AS plan', 'finance.type', 'finance.amount')->join('category', 'category.id', '=', 'finance.category_id')->join('daily', 'daily.id', '=', 'finance.daily_id')->join('monthly', 'monthly.id', '=', 'daily.monthly_id')->join('plan', 'plan.id', '=', 'monthly.plan_id');
             if ($cat != 0) {
                 $query->where('category.id', '=', $cat);
             }
             if ($month != 0) {
                 $query->where('monthly.id', '=', $month);
             }
             $query->where('plan.id', '=', $plan->id);
             $attach['finances'] = $query->paginate(20);
             // $attach['finances'] = Finance::select('finance.name', 'category.name AS category', 'finance.created_at', 'plan.name AS plan', 'finance.type', 'finance.amount')
             //     ->join('category', 'category.id', '=', 'finance.category_id')
             //     ->join('daily', 'daily.id', '=', 'finance.daily_id')
             //     ->join('monthly', 'monthly.id', '=', 'daily.monthly_id')
             //     ->join('plan', 'plan.id', '=', 'monthly.plan_id')
             //     ->where('category.id', '=', $cat)
             //     ->where('monthly.id', '=', $month)
             //     ->where('plan.i', '=', $plan->id)
             //     ->paginate(20);
         }
         return view('home.report', $attach);
     } else {
         return view('home.report');
     }
 }
 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 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();
 }
Example #4
0
 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');
     }
 }