public function generateProgress($progress = 0)
 {
     $user = new User();
     $user->username = "******";
     $user->email = "*****@*****.**";
     $user->password = "******";
     $user->save();
     $plan = new Plan();
     $plan->user_id = $user->id;
     $plan->name = 'UnitTestProject';
     $plan->description = 'UnitTestProject';
     $plan->budget = '0';
     $plan->target = '10000';
     $plan->expected = '1000';
     $plan->period = '10';
     $plan->created_at = date('Y-m-d H:i:s', strtotime('-1 month', strtotime(date('Y-m-d'))));
     $plan->updated_at = date('Y-m-d H:i:s', strtotime('-1 month', strtotime(date('Y-m-d'))));
     $plan->save();
     $month = new Monthly();
     $month->plan_id = $plan->id;
     $month->status = 0;
     $month->month = 1;
     $month->limit = 1000;
     $month->progress = $progress;
     $month->save();
     return ['plan' => $plan, 'month' => $month, 'user' => $user];
 }
Esempio n. 2
0
 public function postFinance(Request $request)
 {
     $successes = array();
     $messages = ["plan_id.required" => "Plan is not selected.", "fcategory.required" => "Please select category.", "fname.required" => "Please specify the name of this transaction.", "famount.required" => "Please insert the amount.", "ftype.required" => "Is it expense or income?", "plan_id.numeric" => "Plan is select illegally.", "fcategory.numeric" => "Category is select illegally.", "famount.numeric" => "Amount can be only numeric.", "ftype.boolean" => "Type is illegally injected.", "plan_id.exists" => "Plan is not exist within system.", "fcategory.exists" => "Category is not exist within system."];
     $validator = Validator::make($request->all(), ['plan_id' => 'required|numeric|exists:plan,id', 'fcategory' => 'required|numeric|exists:category,id', 'fname' => 'required', 'famount' => 'required|numeric', 'ftype' => 'required|boolean'], $messages);
     if ($validator->fails()) {
         return redirect()->back()->withErrors($validator)->withInput();
     }
     if (!Session::has('Plan')) {
         $validator->errors()->add('User', 'Please select plan!');
         return redirect()->back()->withInput()->withErrors($validator);
     }
     if ($this->user == null) {
         $validator->errors()->add('User', 'You are not login!');
         return redirect('login')->withErrors($validator);
     } else {
         $plan = Plan::find(Session::get('Plan'));
         $now = new DateTime(date('Y-m-d'));
         $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 = Session::get('Plan');
             $month->status = 0;
             $month->month = sizeof($plan->months) + 1;
             $month->limit = $plan->expected;
             $month->progress = 0;
             $newExpected = ceil(($plan->target - $plan->budget) / ($plan->period - $diff));
             $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();
         } else {
             $month = $plan->months()->orderBy('id', 'desc')->first();
         }
         $date = str_replace('/', '-', $request->fdate);
         $daily = $month->days()->where('date', '>=', date('Y-m-d', strtotime($date)))->where('date', '<=', date('Y-m-d', strtotime('+1 day', strtotime($date))))->first();
         if ($daily == null) {
             $daily = new Daily();
             $daily->monthly_id = $month->id;
             $daily->date = date('Y-m-d', strtotime($date));
             $daily->expense = 0;
             $daily->income = 0;
         }
         $sumExpenseDaily = 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', '=', $request->fcategory)->where('type', '=', 0)->where('daily.id', '=', $daily->id)->sum('amount');
         $sumExpenseMonthly = 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', '=', $request->fcategory)->where('type', '=', 0)->sum('amount');
         $restrictDaily = Restrict::where('plan_id', '=', $plan->id)->where('category_id', '=', $request->fcategory)->where('for', '=', 0)->first();
         $restrictMonthly = Restrict::where('plan_id', '=', $plan->id)->where('category_id', '=', $request->fcategory)->where('for', '=', 1)->first();
         if ($restrictDaily) {
             $sumExpenseDaily += $request->famount;
             if ($restrictDaily->exceed < $sumExpenseDaily) {
                 $validator->errors()->add('User', 'Exceed per day!');
                 //return redirect()->back()->withInput()->withErrors($validator);
             }
         }
         if ($restrictMonthly) {
             $sumExpenseMonthly += $request->famount;
             if ($restrictMonthly->exceed < $sumExpenseMonthly) {
                 $validator->errors()->add('User', 'Exceed per month!');
                 //return redirect()->back()->withInput()->withErrors($validator);
             }
         }
         if ($request->ftype == 0) {
             $daily->expense += $request->famount;
             $month->progress = $month->progress - $request->famount;
             $plan->budget -= $request->famount;
         } else {
             $daily->income += $request->famount;
             $month->progress = $month->progress + $request->famount;
             $plan->budget += $request->famount;
         }
         $daily->save();
         $finance = new Finance();
         $finance->daily_id = $daily->id;
         $finance->category_id = $request->fcategory;
         $finance->description = $request->fdescription;
         $finance->name = $request->fname;
         $finance->amount = $request->famount;
         $finance->type = $request->ftype;
         $plan->save();
         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);
         $finance->save();
         $month->save();
     }
     return redirect()->back()->withInput()->withErrors($validator);
 }
Esempio n. 3
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) + 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');
     }
 }
Esempio n. 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');
     }
 }