121 lines
3.4 KiB
PHP
121 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Web;
|
|
|
|
use Inertia\Inertia;
|
|
use App\Models\Group;
|
|
use App\Http\Controllers\Controller;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Http\Resources\SubgroupResource;
|
|
use App\Imports\TradingsImport;
|
|
use App\Models\Subgroup;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TradingController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
if (blank(request('group'))) {
|
|
request()->merge([
|
|
'group' => optional(Group::whereType('trading')->where('is_default', true)->first())->id
|
|
]);
|
|
}
|
|
$subgroupsWithTradings = Subgroup::with(['tradings' => function ($query) {
|
|
$query
|
|
->where('group_id', request('group'))
|
|
->where('locale', app()->getLocale());
|
|
}])
|
|
->where('group_id', request('group'))
|
|
->where('locale', app()->getLocale())
|
|
->simplePaginate(50);
|
|
|
|
$groups = Group::whereType('trading')->get();
|
|
|
|
$filters = array_filter(request()->all([
|
|
'group',
|
|
]));
|
|
|
|
if (array_key_exists('category', $filters)) {
|
|
$filters['category'] = intval($filters['category']);
|
|
}
|
|
|
|
if (array_key_exists('group', $filters)) {
|
|
$filters['group'] = intval($filters['group']);
|
|
}
|
|
|
|
return Inertia::render('Tradings', [
|
|
'text' => settings('text')[app()->getLocale()],
|
|
'filters' => $filters,
|
|
'subgroupsWithTradings' => SubgroupResource::collection($subgroupsWithTradings),
|
|
'groups' => fn () => $groups,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function import()
|
|
{
|
|
request()->validate([
|
|
'group' => ['exists:groups,id'],
|
|
'file' => ['required', 'mimes:xlsx, csv, xls'],
|
|
]);
|
|
|
|
if (!$group = Group::find(request('group'))) {
|
|
$group = Group::create([
|
|
'title' => 'New group',
|
|
'type' => 'export',
|
|
'is_default' => true
|
|
]);
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$group->tradings()->whereLocale(app()->getLocale())->delete();
|
|
$group->subgroups()->whereLocale(app()->getLocale())->delete();
|
|
} catch (\Throwable $th) {
|
|
Log::info($th);
|
|
DB::rollback();
|
|
return redirect()->route('tradings');
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
try {
|
|
$id = now()->unix();
|
|
session(['import' => $id]);
|
|
|
|
$file = request()->file('file')->storeAs('uploads', $filename = $group->filename);
|
|
$group->update(['file' => $filename]);
|
|
|
|
Excel::queueImport(new TradingsImport($id, $group, app()->getLocale()), $file);
|
|
} catch (\Throwable $th) {
|
|
info('error here');
|
|
info($th->getMessage());
|
|
}
|
|
|
|
return redirect()->route('tradings');
|
|
}
|
|
|
|
public function status()
|
|
{
|
|
$id = session('import');
|
|
|
|
return response([
|
|
'started' => filled(cache("start_date_$id")),
|
|
'finished' => filled(cache("end_date_$id")),
|
|
'current_row' => (int) cache("current_row_$id"),
|
|
'total_rows' => (int) cache("total_rows_$id"),
|
|
]);
|
|
}
|
|
}
|