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