Completed
Push — master ( 45f86d...419882 )
by Ajit
12s
created

ExpensesController::update()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

123
    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...
124
    {
125
        Expense::where('id', '=', $id)->update(['paid' => \constPaymentStatus::Paid]);
126
127
        flash()->success('Expense was successfully paid');
128
129
        return redirect('expenses/all');
130
    }
131
132
    public function delete($id)
133
    {
134
        Expense::destroy($id);
135
136
        return redirect('expenses/all');
137
    }
138
}
139