151 lines
4.4 KiB
PHP
151 lines
4.4 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Requests\InvSettingRequest;
|
||
use App\Models\InvSetting;
|
||
use App\Models\Management;
|
||
use App\Services\InvSettingService;
|
||
use Illuminate\Http\Request;
|
||
|
||
class InvSettingController extends Controller
|
||
{
|
||
private InvSettingService $service;
|
||
|
||
public function __construct(InvSettingService $service)
|
||
{
|
||
$this->service = $service;
|
||
}
|
||
|
||
/**
|
||
* 一覧画面
|
||
*/
|
||
public function list(Request $request)
|
||
{
|
||
$allowed = [
|
||
'management_id', 't_number', 't_name',
|
||
'zipcode', 'adrs', 'bldg', 'tel_num', 'fax_num'
|
||
];
|
||
|
||
$sort = in_array($request->sort, $allowed, true)
|
||
? $request->sort
|
||
: 'management_id';
|
||
|
||
$sortType = in_array($request->sort_type, ['asc', 'desc'], true)
|
||
? $request->sort_type
|
||
: 'asc';
|
||
|
||
$list = InvSetting::orderBy($sort, $sortType)
|
||
->paginate(20)
|
||
->appends($request->query());
|
||
|
||
//return view('admin.invsettings.list', compact('list', 'sort', 'sortType'));
|
||
return view('admin.invsettings.list', [
|
||
'list' => $list,
|
||
'sort' => $sort,
|
||
'sort_type' => $sortType,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 新規登録
|
||
*/
|
||
public function add(Request $request)
|
||
{
|
||
if ($request->isMethod('post')) {
|
||
$validated = app(InvSettingRequest::class)->validated();
|
||
$this->service->create($request);
|
||
|
||
return redirect()
|
||
->route('inv_settings')
|
||
->with('success', 'インボイス設定を登録しました。');
|
||
}
|
||
|
||
$managementList = Management::pluck('management_name', 'management_id');
|
||
|
||
return view('admin.invsettings.add', compact('managementList'));
|
||
}
|
||
|
||
/**
|
||
* 編集
|
||
*/
|
||
public function edit(Request $request, $id)
|
||
{
|
||
$record = InvSetting::findOrFail($id);
|
||
|
||
if ($request->isMethod('post')) {
|
||
$validated = app(InvSettingRequest::class)->validated();
|
||
$this->service->update($request, $record);
|
||
|
||
return redirect()
|
||
->route('inv_settings')
|
||
->with('success', 'インボイス設定を更新しました。');
|
||
}
|
||
|
||
[$zip1, $zip2] = explode('-', $record->zipcode ?? '-');
|
||
[$tel1, $tel2, $tel3] = array_pad(explode('-', $record->tel_num ?? ''), 3, '');
|
||
[$fax1, $fax2, $fax3] = array_pad(explode('-', $record->fax_num ?? ''), 3, '');
|
||
|
||
$managementList = Management::pluck('management_name', 'management_id');
|
||
|
||
return view('admin.invsettings.edit', compact(
|
||
'record',
|
||
'zip1', 'zip2',
|
||
'tel1', 'tel2', 'tel3',
|
||
'fax1', 'fax2', 'fax3',
|
||
'managementList'
|
||
));
|
||
}
|
||
|
||
/**
|
||
* 削除
|
||
*/
|
||
public function delete(Request $request)
|
||
{
|
||
$this->service->delete((array) $request->id);
|
||
|
||
return redirect()
|
||
->route('inv_settings')
|
||
->with('success', '削除しました。');
|
||
}
|
||
|
||
/**
|
||
* 社判画像アップロード(AJAX)
|
||
*
|
||
* ・ファイル選択ダイアログで選択された画像をアップロード
|
||
* ・物理ファイルを storage に保存
|
||
* ・DB には保存せず、画像パスのみを返却する
|
||
*/
|
||
public function upload(Request $request)
|
||
{
|
||
// バリデーション
|
||
$request->validate([
|
||
'company_image_file' => [
|
||
'required',
|
||
'file',
|
||
'mimes:jpg,jpeg,png,gif',
|
||
'max:2048', // 2MB
|
||
],
|
||
], [
|
||
'company_image_file.required' => '画像ファイルを選択してください。',
|
||
'company_image_file.mimes' => 'jpg / png / gif 形式の画像を選択してください。',
|
||
'company_image_file.max' => '画像サイズは2MB以内にしてください。',
|
||
]);
|
||
|
||
// ファイル取得
|
||
$file = $request->file('company_image_file');
|
||
|
||
// storage/app/public/inv に保存
|
||
$path = $file->store('inv', 'public');
|
||
|
||
// AJAX 用レスポンス
|
||
return response()->json([
|
||
'path' => $path, // DB保存用パス
|
||
'file_name' => $file->getClientOriginalName(), // 表示用ファイル名
|
||
]);
|
||
}
|
||
|
||
|
||
}
|