Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 72 |
OperationsController | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
552 | |
0.00% |
0 / 72 |
__construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
index | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
indexWithCompanyAndDates | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 9 |
|||
indexWithCompanyAndJournalAndDates | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 10 |
|||
store | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 24 |
|||
show | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
update | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 13 |
|||
destroy | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 8 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Compta\Controllers; |
4 | |
5 | use App\Http\Controllers\Controller; |
6 | use Illuminate\Http\Response; |
7 | use Illuminate\Http\Request; |
8 | use Qmp\Laravel\Compta\Models\AccountLine; |
9 | use Qmp\Laravel\Compta\Models\Operation; |
10 | use Qmp\Laravel\MicroService\Controllers\AbstractMicroServiceController; |
11 | |
12 | class OperationsController 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 | $operation = Operation::select()->with('accountLines')->simplePaginate(); |
33 | return response()->json($operation); |
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 | $operation = Operation::where('company_id', $companyId); |
45 | if ($sinceDate != null) { |
46 | $operation->where('date_at', '<=', $sinceDate); |
47 | } |
48 | |
49 | if ($lastDate != null) { |
50 | $operation->where('date_at', '>=', $lastDate); |
51 | } |
52 | |
53 | $ope = $operation->with('accountLines')->simplePaginate(); |
54 | return response()->json($ope); |
55 | } |
56 | |
57 | return response()->json(['error' => $this->USER_NOT_ATTRIBUTED_MESSAGE], Response::HTTP_UNPROCESSABLE_ENTITY); |
58 | } |
59 | |
60 | |
61 | /** |
62 | * Display a listing of the resource. |
63 | * |
64 | * @return \Illuminate\Http\Response |
65 | */ |
66 | public function indexWithCompanyAndJournalAndDates($companyId, $journalId, $sinceDate = null, $lastDate = null) |
67 | { |
68 | if ($this->companyIsAttributedToUser($companyId)) { |
69 | $operation = Operation::where('company_id', $companyId) |
70 | ->where('journal_id', $journalId); |
71 | if ($sinceDate != null) { |
72 | $operation->where('date_at', '<=', $sinceDate); |
73 | } |
74 | |
75 | if ($lastDate != null) { |
76 | $operation->where('date_at', '>=', $lastDate); |
77 | } |
78 | |
79 | $ope = $operation->with('accountLines')->simplePaginate(); |
80 | return response()->json($ope); |
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 | 'company_id' => 'required|max:255', |
96 | 'journal_id' => 'required|max:255', |
97 | 'date_at' => 'required|max:255', |
98 | 'voucher' => 'required|max:255', |
99 | 'account_lines' => 'nullable|array' |
100 | ]); |
101 | |
102 | try { |
103 | if (!$this->companyIsAttributedToUser($request->company_id)) { |
104 | throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE); |
105 | } |
106 | |
107 | $operation = new Operation(); |
108 | $operation->company_id = $request->company_id; |
109 | $operation->journal_id = $request->journal_id; |
110 | $operation->date_at = $request->date_at; |
111 | $operation->voucher = $request->voucher; |
112 | $operation->save(); |
113 | |
114 | if ($request->account_lines) { |
115 | foreach ($request->account_lines as $line) { |
116 | $lineAccount = new AccountLine(); |
117 | $lineAccount->chart_account_id = $line->chart_account_id; |
118 | $lineAccount->operation_id = $operation->id; |
119 | $lineAccount->date_at = $line->date_at; |
120 | $lineAccount->title = $line->title; |
121 | $lineAccount->debit = $line->debit; |
122 | $lineAccount->credit = $line->credit; |
123 | $lineAccount->pointed = $line->pointed; |
124 | $lineAccount->save(); |
125 | } |
126 | } |
127 | |
128 | } catch(\Exception $e) { |
129 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
130 | } |
131 | |
132 | return response(['status' => 'ok', 'operation_id' => $operation->id], Response::HTTP_CREATED); |
133 | } |
134 | |
135 | /** |
136 | * Display the specified resource. |
137 | * |
138 | * @return \Illuminate\Http\Response |
139 | */ |
140 | public function show($id) |
141 | { |
142 | $operation = Operation::where('id', $id)->with('accountLines')->first(); |
143 | if ($this->companyIsAttributedToUser($operation->company_id)) { |
144 | response($operation); |
145 | } |
146 | |
147 | return response()->json(['error' => $this->USER_NOT_ATTRIBUTED_MESSAGE], Response::HTTP_UNPROCESSABLE_ENTITY); |
148 | } |
149 | |
150 | /** |
151 | * Update the specified resource in storage. |
152 | * |
153 | * @param \Illuminate\Http\Request $request |
154 | * @return \Illuminate\Http\Response |
155 | */ |
156 | public function update(Request $request, $id) |
157 | { |
158 | $request->validate([ |
159 | 'company_id' => 'required|max:255', |
160 | 'journal_id' => 'required|max:255', |
161 | 'date_at' => 'required|max:255', |
162 | 'voucher' => 'required|max:255', |
163 | ]); |
164 | |
165 | try { |
166 | if (!$this->companyIsAttributedToUser($request->company_id)) { |
167 | throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE); |
168 | } |
169 | |
170 | $operation = Operation::findOrFail($id); |
171 | $operation->company_id = $request->company_id; |
172 | $operation->journal_id = $request->journal_id; |
173 | $operation->date_at = $request->date_at; |
174 | $operation->voucher = $request->voucher; |
175 | $operation->save(); |
176 | |
177 | } catch (\Exception $e) { |
178 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
179 | } |
180 | |
181 | return response(['status' => 'ok'], Response::HTTP_OK); |
182 | } |
183 | |
184 | /** |
185 | * Remove the specified resource from storage. |
186 | * |
187 | * @return \Illuminate\Http\Response |
188 | */ |
189 | public function destroy($id) |
190 | { |
191 | try { |
192 | $operation = Operation::find($id); |
193 | if (!$this->companyIsAttributedToUser($operation->company_id)) { |
194 | throw new \Exception($this->USER_NOT_ATTRIBUTED_MESSAGE); |
195 | } |
196 | |
197 | AccountLine::where('operation_id', $id)->delete(); |
198 | $operation->delete(); |
199 | } catch (\Exception $e) { |
200 | return response()->json(['error' => $e->getMessage()], Response::HTTP_UNPROCESSABLE_ENTITY); |
201 | } |
202 | |
203 | return response(['status' => 'ok'], Response::HTTP_OK); |
204 | } |
205 | } |