Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 67
AccountLinesController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
462
0.00% covered (danger)
0.00%
0 / 67
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 index
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 indexWithCompanyAndDates
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 9
 indexWithCompanyAndChartAndDates
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 9
 store
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 17
 show
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 update
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 17
 destroy
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 7
1<?php
2
3namespace Qmp\Laravel\Compta\Controllers;
4
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Response;
7use Illuminate\Http\Request;
8use Qmp\Laravel\Compta\Models\AccountLine;
9use Qmp\Laravel\Compta\Models\Company;
10use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController;
11
12class AccountLinesController extends AbstractMicroServiceController
13{
14    use UserAttributionTrait;
15
16    /**
17     * AccountLinesController constructor.
18     * @param Request $request
19     */
20    public function __construct(Request $request)
21    {
22        parent::__construct($request);
23    }
24
25    /**
26     * Display a listing of the resource.
27     *
28     * @return \Illuminate\Http\Response
29     */
30    public function index()
31    {
32        $accountLine = AccountLine::select()->simplePaginate();
33        return response()->json($accountLine);
34    }
35
36    /**
37     * Display a listing of the resource.
38     *
39     * @return \Illuminate\Http\Response
40     */
41    public function indexWithCompanyAndDates($companyId, $sinceDate = null, $lastDate = null)
42    {
43        if ($this->companyIsAttributedToUser($companyId)) {
44            $accountLine = AccountLine::where('company_id', $companyId);
45            if ($sinceDate != null) {
46                $accountLine->where('date_at', '<=', $sinceDate);
47            }
48
49            if ($lastDate != null) {
50                $accountLine->where('date_at', '>=', $lastDate);
51            }
52
53            $line = $accountLine->simplePaginate();
54
55            return response()->json($line);
56        }
57
58        return response()->json(['error' => $this->USER_NOT_ATTRIBUTED_MESSAGE], Response::HTTP_UNPROCESSABLE_ENTITY);
59    }
60
61
62    /**
63     * Display a listing of the resource.
64     *
65     * @return \Illuminate\Http\Response
66     */
67    public function indexWithCompanyAndChartAndDates($companyId, $chartId, $sinceDate = null, $lastDate = null)
68    {
69        if ($this->companyIsAttributedToUser($companyId)) {
70            $accountLine = AccountLine::where('company_id', $companyId)->where('chart_account_id', $chartId);
71            if ($sinceDate != null) {
72                $accountLine->where('date_at', '<=', $sinceDate);
73            }
74
75            if ($lastDate != null) {
76                $accountLine->where('date_at', '>=', $lastDate);
77            }
78
79            $line = $accountLine->simplePaginate();
80            return response()->json($line);
81        }
82
83        return response()->json(['error' => $this->USER_NOT_ATTRIBUTED_MESSAGE], Response::HTTP_UNPROCESSABLE_ENTITY);
84    }
85
86    /**
87     * Store a newly created resource in storage.
88     *
89     * @param  \Illuminate\Http\Request  $request
90     * @return \Illuminate\Http\Response
91     */
92    public function store(Request $request)
93    {
94        $request->validate([
95            'chart_account_id' => 'required|max:255',
96            'company_id' => 'required|max:255',
97            'operation_id' => 'required|max:255',
98            'date_at' => 'required|max:255',
99            'title' => 'required|max:255',
100            'debit' => 'required|max:255',
101            'credit' => 'required|max:255',
102            'pointed' => 'required|max:255',
103        ]);
104
105        try {
106            if (!$this->companyIsAttributedToUser($request->company_id)) {
107                throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE);
108            }
109
110            $lineAccount = new AccountLine();
111            $lineAccount->chart_account_id = $request->chart_account_id;
112            $lineAccount->operation_id = $request->operation_id;
113            $lineAccount->company_id = $request->company_id;
114            $lineAccount->date_at = $request->date_at;
115            $lineAccount->title = $request->title;
116            $lineAccount->debit = $request->debit;
117            $lineAccount->credit = $request->credit;
118            $lineAccount->pointed = $request->pointed;
119            $lineAccount->save();
120
121        } catch(\Exception $e) {
122            return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
123        }
124
125        return response(['status' => 'ok', 'account_line_id' => $lineAccount->id], Response::HTTP_CREATED);
126    }
127
128    /**
129     * Display the specified resource.
130     *
131     * @return \Illuminate\Http\Response
132     */
133    public function show($id)
134    {
135        $line = AccountLine::where('id', $id)->first();
136
137        if ($this->companyIsAttributedToUser($line->company_id)) {
138            response($line);
139        }
140
141        return response()->json(['error' => $this->USER_NOT_ATTRIBUTED_MESSAGE], Response::HTTP_UNPROCESSABLE_ENTITY);
142    }
143
144    /**
145     * Update the specified resource in storage.
146     *
147     * @param  \Illuminate\Http\Request  $request
148     * @return \Illuminate\Http\Response
149     */
150    public function update(Request $request, $id)
151    {
152        $request->validate([
153            'chart_account_id' => 'required|max:255',
154            'operation_id' => 'required|max:255',
155            'company_id' => 'required|max:255',
156            'date_at' => 'required|max:255',
157            'title' => 'required|max:255',
158            'debit' => 'required|max:255',
159            'credit' => 'required|max:255',
160            'pointed' => 'required|max:255',
161        ]);
162
163        try {
164            if (!$this->companyIsAttributedToUser($request->company_id)) {
165                throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE);
166            }
167
168            $lineAccount = AccountLine::findOrFail($id);
169            $lineAccount->chart_account_id = $request->chart_account_id;
170            $lineAccount->operation_id = $request->operation_id;
171            $lineAccount->company_id = $request->company_id;
172            $lineAccount->date_at = $request->date_at;
173            $lineAccount->title = $request->title;
174            $lineAccount->debit = $request->debit;
175            $lineAccount->credit = $request->credit;
176            $lineAccount->pointed = $request->pointed;
177            $lineAccount->save();
178
179        } catch (\Exception $e) {
180            return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
181        }
182
183        return response(['status' => 'ok'], Response::HTTP_OK);
184    }
185
186    /**
187     * Remove the specified resource from storage.
188     *
189     * @return \Illuminate\Http\Response
190     */
191    public function destroy($id)
192    {
193        try {
194            $line = AccountLine::find($id);
195            if (!$this->companyIsAttributedToUser($line->company_id)) {
196                throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE);
197            }
198
199            $line->delete();
200        } catch (\Exception $e) {
201            return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY);
202        }
203
204        return response(['status' => 'ok'], Response::HTTP_OK);
205    }
206}