public function messages()
 {
     $reportId = $this->route('reportId');
     if ($reportId == null) {
         $expenseId = $this->route('expenseId');
         $reportId = Expense::find($expenseId)->report_id;
     }
     $report = ExpenseReport::find($reportId);
     $messages = ['title.required' => "Title is required", 'date.after' => "Expense date should be on or after report start date (" . date('d M Y', strtotime($report->startDate)) . ")", $report->owner_id . '.numeric' => 'Contributions should be numeric', $report->owner_id . '.min' => 'Contributions cannot be negative', $report->owner_id . 'p.numeric' => 'Participation Ratios should be numeric', $report->owner_id . 'p.min' => 'Participation Ratios cannot be negative'];
     $amount = 0;
     $participationRatios = 0;
     $input = $this->all();
     foreach ($report->users as $user) {
         $amount += $input[$user->id];
         $participationRatios += $input[$user->id . 'p'];
         $messages[$user->id . '.numeric'] = 'Contributions should be numeric';
         $messages[$user->id . '.min'] = 'Contributions cannot be negative';
         $messages[$user->id . 'p.numeric'] = 'Participation Ratios should be numeric';
         $messages[$user->id . 'p.min'] = 'Participation Ratios cannot be negative';
     }
     if ($amount == 0) {
         $messages[$report->owner_id . '.min'] = 'Sum of Contributions cannot be 0';
         $messages[$report->owner_id . '.required'] = 'Sum of Contributions cannot be 0';
     }
     if ($participationRatios == 0) {
         $messages[$report->owner_id . 'p.min'] = 'Sum of Participation Ratios cannot be 0';
         $messages[$report->owner_id . 'p.required'] = 'Sum of Participation Ratios cannot be 0';
     }
     return $messages;
 }
 /**
  * Determine if the user is authorized to make this request.
  *
  * @return bool
  */
 public function authorize()
 {
     $reportId = $this->route('id');
     $report = ExpenseReport::findOrFail($reportId);
     if ($report->users()->get(['id'])->contains(\Auth::user()->id) || $report->owner_id == \Auth::user()->id) {
         return true;
     }
     return false;
 }
 /**
  * Determine if the user is authorized to make this request.
  *
  * @return bool
  */
 public function authorize()
 {
     $reportId = $this->route('id');
     $report = ExpenseReport::find($reportId);
     if ($report->owner_id == \Auth::user()->id) {
         return true;
     }
     return false;
 }
 /**
  * Determine if the user is authorized to make this request.
  *
  * @return bool
  */
 public function authorize()
 {
     $expenses = $this->input('expense_ids');
     if ($expenses != null && count($expenses) > 0) {
         $reportId = Expense::find($expenses[0])->report_id;
         $report = ExpenseReport::find($reportId);
         if ($report->status) {
             return false;
         }
     }
     return true;
 }
 public function storeExpense($reportId, AddExpenseRequest $request)
 {
     $report = ExpenseReport::find($reportId);
     $input = $request->all();
     $input['report_id'] = $reportId;
     $input['owner_id'] = \Auth::user()->id;
     $input['amount'] = 0;
     $input['usage_total'] = 0;
     $expense = Expense::create($input);
     $this->modifyAndSaveExpense($expense, $report, $input);
     session()->flash('message', 'Expense (' . $expense->title . ') added!');
     return redirect('expenseReports/' . $reportId);
 }
 public function show($reportId)
 {
     $authenticatedUser = \Auth::user();
     $selectStmtAuthUser = "******";
     $selectStmtOtherUsers = "select s.amount, s.owee_id, s.owed_id, owee.name as oweeName, owed.name as owedName from settlements s, users owee, users owed where s.owee_id=owee.id and s.owed_id=owed.id and s.report_id = ? and (s.owee_id != ? and s.owed_id != ?) order by owee.name, owed.name";
     $authUserSettlements = DB::select($selectStmtAuthUser, [$reportId, $authenticatedUser->id, $authenticatedUser->id]);
     $otherUserSettlements = DB::select($selectStmtOtherUsers, [$reportId, $authenticatedUser->id, $authenticatedUser->id]);
     //If report has only been closed and the settlements have not been determined
     $report = ExpenseReport::find($reportId);
     if ($report->status == 1) {
         if (\Auth::user()->id != $report->owner_id) {
             $messageHeader = 'Settlements for: ' . $report->title;
             $messageBody = 'Settlements for this report have not been determined yet.';
             return view('utilities.displayMessage', compact(['messageHeader', 'messageBody', 'report']));
         }
         return redirect('expenseReports/' . $reportId . '/close');
     }
     $report = ExpenseReport::find($reportId);
     $isArchived = $report->isArchivedForUser(\Auth::user()->id);
     return view('settlements.reportSettlements', compact('authUserSettlements', 'otherUserSettlements', 'report', 'authenticatedUser', 'isArchived'));
 }
示例#7
0
 public function settledReports()
 {
     //Get all reports for the user - owned + associated
     $expenseReports = ExpenseReport::where(function ($query) {
         $query->where('owner_id', '=', $this->id)->orWhereHas('users', function ($query) {
             $query->where('user_id', '=', $this->id);
         });
         //And check for settlement conditions
     })->where(function ($query) {
         //Report is completely settled
         $query->where('status', '>=', 3)->orWhere(function ($query) {
             $query->where('status', '=', 2)->whereDoesntHave('settlements', function ($query) {
                 $query->where('completed', '=', 0)->where(function ($query) {
                     $query->where('owee_id', '=', $this->id)->orWhere('owed_id', '=', $this->id);
                 });
             });
         });
     });
     return $expenseReports;
 }
 public function closeReport(CloseReportRequest $request, $id, AppMailer $mailer)
 {
     $report = ExpenseReport::findorFail($id)->load('owner', 'users');
     if ($report->expenses()->count() == 0) {
         return redirect()->back()->withErrors('Reports with no expenses cannot be closed. Consider deleting it.');
     }
     if ($report->status == 0) {
         $report->updateStatus(1);
         $mailer->sendReportClosedNotification($report);
     }
     $oweesAndOwed = $report->oweesAndOwed();
     if (!$report->areSettlementsNecessary()) {
         $messageHeader = 'Settlements for: ' . $report->title;
         $messageBody = 'No settlements are necessary! All users have paid the right amount towards expenses.';
         $report->updateStatus(4);
         return view('utilities.displayMessage', compact(['messageHeader', 'messageBody', 'report']));
     }
     $owees = $oweesAndOwed['owees'];
     $oweds = $oweesAndOwed['owed'];
     $settlements = [];
     //If there is only 1 owee or 1 owed, then the settlements are pre-determined
     if (count($owees) == 1 || count($oweds) == 1) {
         foreach ($owees as $owee) {
             foreach ($oweds as $owed) {
                 $settlements['settlementowee' . $owee[1]->id . 'owed' . $owed[1]->id] = count($oweds) == 1 ? $owee[0] * -1 : $owed[0];
             }
         }
         return redirect('/settlements/' . $id . '/add')->with('settlements', $settlements);
     }
     return view('expenseReports.close', compact(['report', 'oweesAndOwed']));
 }
示例#9
0
 public function doCreateExpenseReport(CreateExpenseReportRequest $request)
 {
     $totalCost = 0;
     $expenseReport = new ExpenseReport();
     $expenseReport->mr_id = \Auth::user()->id;
     $expenseReport->month = $request->month . '-' . $request->year;
     if ($expenseReport->save()) {
         $expenseReportId = $expenseReport->id;
         $expenseReportHotel = new ExpenseReportHotel();
         $expenseReportHotel->date = $request->hotel_date;
         $expenseReportHotel->expense_report_id = $expenseReportId;
         $expenseReportHotel->hotel = $request->hotel;
         $expenseReportHotel->meal = $request->hotel_meal;
         $expenseReportHotel->cost = $request->hotel_cost;
         $totalCost += $request->hotel_cost;
         $expenseReportHotel->save();
         if (is_array($request->extra_hotel_date)) {
             for ($i = 0; $i < count($request->extra_hotel_date); $i++) {
                 if (!empty($request->extra_hotel_date[$i]) && !empty($request->extra_hotel[$i]) && !empty($request->extra_hotel_meal[$i]) && !empty($request->extra_hotel_cost[$i])) {
                     $expenseReportHotel = new ExpenseReportHotel();
                     $expenseReportHotel->date = $request->extra_hotel_date[$i];
                     $expenseReportHotel->expense_report_id = $expenseReportId;
                     $expenseReportHotel->hotel = $request->extra_hotel[$i];
                     $expenseReportHotel->meal = $request->extra_hotel_meal[$i];
                     $expenseReportHotel->cost = $request->extra_hotel_cost[$i];
                     $totalCost += $request->extra_hotel_cost[$i];
                     $expenseReportHotel->save();
                 }
             }
         }
         $expenseReportTransportation = new ExpenseReportTransportation();
         $expenseReportTransportation->date = $request->transportation_date;
         $expenseReportTransportation->expense_report_id = $expenseReportId;
         $expenseReportTransportation->city = $request->transportation_city;
         $expenseReportTransportation->cost = $request->transportation_cost;
         $totalCost += $request->transportation_cost;
         $expenseReportTransportation->save();
         if (is_array($request->extra_transportation_date)) {
             for ($i = 0; $i < count($request->extra_transportation_date); $i++) {
                 if (!empty($request->extra_transportation_date[$i]) && !empty($request->extra_transportation_city[$i]) && !empty($request->extra_transportation_cost[$i])) {
                     $expenseReportTransportation = new ExpenseReportTransportation();
                     $expenseReportTransportation->date = $request->extra_transportation_date[$i];
                     $expenseReportTransportation->expense_report_id = $expenseReportId;
                     $expenseReportTransportation->city = $request->extra_transportation_city[$i];
                     $expenseReportTransportation->cost = $request->extra_transportation_cost[$i];
                     $totalCost += $request->extra_transportation_cost[$i];
                     $expenseReportTransportation->save();
                 }
             }
         }
         $expenseReportMeeting = new ExpenseReportMeeting();
         $expenseReportMeeting->date = $request->meeting_date;
         $expenseReportMeeting->expense_report_id = $expenseReportId;
         $expenseReportMeeting->meeting = $request->meeting;
         $expenseReportMeeting->cost = $request->meeting_cost;
         $totalCost += $request->meeting_cost;
         $expenseReportMeeting->save();
         if (is_array($request->extra_meeting_date)) {
             for ($i = 0; $i < count($request->extra_meeting_date); $i++) {
                 if (!empty($request->extra_meeting_date[$i]) && !empty($request->extra_meeting[$i]) && !empty($request->extra_meeting_cost[$i])) {
                     $expenseReportMeeting = new ExpenseReportMeeting();
                     $expenseReportMeeting->date = $request->extra_meeting_date[$i];
                     $expenseReportMeeting->expense_report_id = $expenseReportId;
                     $expenseReportMeeting->meeting = $request->extra_meeting[$i];
                     $expenseReportMeeting->cost = $request->extra_meeting_cost[$i];
                     $totalCost += $request->extra_meeting_cost[$i];
                     $expenseReportMeeting->save();
                 }
             }
         }
         $expenseReport = ExpenseReport::findOrFail($expenseReportId);
         $expenseReport->total = $totalCost;
         $expenseReport->save();
         if (!empty($request->file('invoices'))) {
             try {
                 $extension = $request->file('invoices')->getClientOriginalExtension();
                 $request->file('invoices')->move(public_path('uploads/expenses_reports/' . \Auth::user()->id . '/' . $expenseReport->month . '/'), $expenseReport->id . '.' . $extension);
                 return redirect()->back()->with('message', 'Expense Report has been sent to your managers successfully !');
                 print_r($expenseReport);
             } catch (ParseException $ex) {
                 echo 'Failed to create new expense report , with error message: ' . $ex->getMessage();
             }
         }
         return redirect()->back()->with('message', 'Expense Report has been sent to your managers successfully !');
     }
 }