Issues (166)

app/Http/Controllers/ExpensesController.php (7 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Auth;
7
use Carbon\Carbon;
8
use App\Expense;
9
use App\ExpenseCategory;
10
use App\Http\Controllers\Controller;
11
use App\Http\Requests;
12
13
14
class ExpensesController extends Controller
15
{
16
    public function __construct()
17
    {
18
        $this->middleware('auth');
19
    }
20
     /* Display a listing of the resource.
21
     *
22
     * @return Response
0 ignored issues
show
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
23
     */
24
    public function index(Request $request)
25
    {
26
      $expenses = Expense::indexQuery($request->category_id,$request->sort_field,$request->sort_direction,$request->drp_start,$request->drp_end)->search('"'.$request->input('search').'"')->paginate(10);
27
      $expenseTotal = Expense::indexQuery($request->category_id,$request->sort_field,$request->sort_direction,$request->drp_start,$request->drp_end)->search('"'.$request->input('search').'"')->get();
28
      $count = $expenseTotal->sum('amount');
29
     
30
 
31
      if (!$request->has('drp_start') or !$request->has('drp_end')) 
32
      {
33
        $drp_placeholder = "Select daterange filter";
34
      }
35
      else
36
      {
37
        $drp_placeholder = $request->drp_start. ' - ' .$request->drp_end;
38
      }
39
40
      $request->flash();
41
      
42
      return view('expenses.index', compact('expenses','count','drp_placeholder'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('expenses.in...t', 'drp_placeholder')) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
43
    }
44
45
    /**
46
     * Display the specified resource.
47
     *
48
     * @param  int  $id
49
     * @return Response
50
     */
51
    public function show()
52
    {
53
    	$expense = Expense::findOrFail($id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
54
55
    	return view('expenses.show', compact('expense'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('expenses.show', compact('expense')) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
56
    }
57
58
     /**
59
     * Show the form for creating a new resource.
60
     *
61
     * @return Response
62
     */
63
    public function create()
64
    {
65
    	return view('expenses.create');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('expenses.create') returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
66
    }
67
    
68
     /**
69
     * Store a newly created resource in storage.
70
     *
71
     * @return Response
72
     */
73
    public function store(Request $request)
74
    {
75
        $expenseData = array('name' => $request->name,
76
                             'category_id' => $request->category_id,
77
                             'due_date' => $request->due_date,
78
                             'repeat' => $request->repeat,
79
                             'note' => $request->note,
80
                             'amount' => $request->amount);
81
82
        $expense  = new Expense($expenseData);
83
        $expense->createdBy()->associate(Auth::user());
84
        $expense->updatedBy()->associate(Auth::user());
85
        $expense->save();
86
87
        if ($request->due_date <= Carbon::today()->format('Y-m-d')) 
88
        {
89
            $expense->paid = \constPaymentStatus::Paid;
90
        }
91
        else
92
        {
93
            $expense->paid = \constPaymentStatus::Unpaid;
94
        }
95
96
        $expense->createdBy()->associate(Auth::user());
97
98
        $expense->save();
99
        flash()->success('Expense was successfully added');
100
    	return redirect('expenses/all'); 
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect('expenses/all') returns the type Illuminate\Http\Redirect...nate\Routing\Redirector which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
101
    }
102
103
    public function edit($id)
104
    {
105
        $expense=Expense::findOrFail($id);
106
107
        return view('expenses.edit', compact('expense'));
108
    }
109
110
    public function update($id, Request $request)
111
    {
112
        $expense=Expense::findOrFail($id);
113
114
        $expense->update($request->all());
115
116
        if ($request->due_date == Carbon::today()) 
117
        {
118
            $expense->paid = \constPaymentStatus::Paid;
119
        }
120
        elseif($request->due_date != Carbon::today() && $expense->paid == \constPaymentStatus::Paid)
121
        {
122
            $expense->paid = \constPaymentStatus::Paid;
123
        }
124
        else
125
        {
126
            $expense->paid = \constPaymentStatus::Unpaid;
127
        }
128
129
        $expense->updatedBy()->associate(Auth::user());
130
131
        $expense->save();
132
        flash()->success('Expense was successfully updated');
133
        return redirect('expenses/all');
134
    }
135
136
    public function paid($id, Request $request)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

136
    public function paid($id, /** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        Expense::where('id','=',$id)->update(['paid' => \constPaymentStatus::Paid]);
139
        
140
        flash()->success('Expense was successfully paid');
141
        return redirect('expenses/all');
142
    }
143
144
    public function delete($id)
145
    {
146
        Expense::destroy($id);
147
        
148
        return redirect('expenses/all');
149
    }
150
151
}
152