Add final step to process of importing CSV
MODIFY
app/Http/Controllers/ImportController.php
app/Http/Controllers/ImportController.php
namespace App\Http\Controllers;
use App\Import;
+use App\Spending;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
@@ -67,4 +68,51 @@ class ImportController extends Controller {
return redirect()->route('imports.index');
}
+
+ public function getComplete(Import $import) {
+ // TODO AUTHORIZE
+
+ $file = fopen(storage_path('app/imports/' . $import->file), 'r');
+
+ $heading = true;
+ $rows = [];
+
+ while ($row = fgetcsv($file, 999, ',')) {
+ if (!$heading) {
+ $rows[] = [
+ 'happened_on' => $row[$import->column_happened_on],
+ 'description' => $row[$import->column_description],
+ 'amount' => $row[$import->column_amount]
+ ];
+ }
+
+ $heading = false;
+ }
+
+ return view('imports.complete', compact('rows'));
+ }
+
+ public function postComplete(Request $request, Import $import) {
+ // TODO AUTHORIZE
+
+ // TODO VALIDATE
+
+ foreach ($request->input('rows') as $row) {
+ if (isset($row['import'])) {
+ Spending::create([
+ 'space_id' => session('space')->id,
+ 'import_id' => $import->id,
+ 'happened_on' => $row['happened_on'],
+ 'description' => $row['description'],
+ 'amount' => (int) ($row['amount'] * 100)
+ ]);
+ }
+ }
+
+ $import->fill([
+ 'status' => 2
+ ])->save();
+
+ return redirect()->route('imports.index');
+ }
}

MODIFY
app/Spending.php
app/Spending.php
@@ -10,6 +10,14 @@ class Spending extends Model {
protected $dates = ['deleted_at'];
+ protected $fillable = [
+ 'space_id',
+ 'import_id',
+ 'happened_on',
+ 'description',
+ 'amount'
+ ];
+
// Accessors
public function getFormattedAmountAttribute() {
return number_format($this->amount / 100, 2);

ADD
resources/views/imports/complete.blade.php
+<form method="POST">
+ {{ csrf_field() }}
+ <div style="display: flex;">
+ <div>Import</div>
+ <div>{{ __('fields.date') }}</div>
+ <div>{{ __('fields.description') }}</div>
+ <div>{{ __('fields.amount') }}</div>
+ </div>
+ @foreach ($rows as $index => $row)
+ <div style="display: flex;">
+ <div>
+ <input type="checkbox" name="rows[{{ $index }}][import]" />
+ </div>
+ <div>
+ <input type="text" name="rows[{{ $index }}][happened_on]" value="{{ $row['happened_on'] }}" />
+ </div>
+ <div>
+ <input type="text" name="rows[{{ $index }}][description]" value="{{ $row['description'] }}" />
+ </div>
+ <div>
+ <input type="text" name="rows[{{ $index }}][amount]" value="{{ $row['amount'] }}" />
+ </div>
+ </div>
+ @endforeach
+ <button>Submit</button>
+</form>
\ No newline at end of file

MODIFY
resources/views/imports/index.blade.php
resources/views/imports/index.blade.php
@foreach ($imports as $import)
<div class="box__section row">
<div class="row__column">{{ $import->name }}</div>
- <div class="row__column">{{ $import->status }} {!! $import->status == 0 ? '<a href="/imports/' . $import->id . '/prepare">Prepare</a>' : '' !!}</div>
+ <div class="row__column">{{ $import->status }} {!! $import->status == 0 ? '<a href="/imports/' . $import->id . '/prepare">Prepare</a>' : '' !!} {!! $import->status == 1 ? '<a href="/imports/' . $import->id . '/complete">Complete</a>' : '' !!}</div>
</div>
@endforeach
@else

MODIFY
routes/web.php
routes/web.php
@@ -51,6 +51,8 @@ Route::group(['middleware' => ['auth']], function () {
Route::post('/imports', 'ImportController@store')->name('store');
Route::get('/imports/{import}/prepare', 'ImportController@getPrepare')->name('prepare');
Route::post('/imports/{import}/prepare', 'ImportController@postPrepare');
+ Route::get('/imports/{import}/complete', 'ImportController@getComplete')->name('complete');
+ Route::post('/imports/{import}/complete', 'ImportController@postComplete');
});
Route::get('/settings', 'SettingsController@index')->name('settings');

Add final step to process of importing CSV