krgm.so-manager-dev.com/app/Http/Controllers/Admin/InvSettingController.php
kin.rinzen 70bfd18204
All checks were successful
Deploy preview (main_kin) / deploy (push) Successful in 13s
画面追加(mail_templates/inv_settings)
2025-08-27 17:16:05 +09:00

152 lines
6.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\InvSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class InvSettingController extends Controller
{
/**
* 登録フォーム表示
*/
public function form(Request $request)
{
$row = InvSetting::first();
$zip1 = $zip2 = $tel1 = $tel2 = $tel3 = $fax1 = $fax2 = $fax3 = '';
if ($row) {
// 郵便番号(そのままハイフン分割)
if (!empty($row->zipcode) && str_contains($row->zipcode, '-')) {
[$zip1, $zip2] = explode('-', $row->zipcode);
}
// 電話番号:数字以外を除去 → 2桁+4桁+4桁 に分割
if (!empty($row->tel_num)) {
$tel = preg_replace('/\D/', '', $row->tel_num); // 数字以外を除去
$tel1 = substr($tel, 0, 2);
$tel2 = substr($tel, 2, 4);
$tel3 = substr($tel, 6, 4);
}
// FAX番号同じく 2桁+4桁+4桁
if (!empty($row->fax_num)) {
$fax = preg_replace('/\D/', '', $row->fax_num);
$fax1 = substr($fax, 0, 2);
$fax2 = substr($fax, 2, 4);
$fax3 = substr($fax, 6, 4);
}
}
return view('admin.invsettings._form', compact(
'row', 'zip1', 'zip2', 'tel1', 'tel2', 'tel3', 'fax1', 'fax2', 'fax3'
));
}
/**
* 登録・更新処理
*/
public function save(Request $request)
{
// バリデーションルール
$rules = [
't_number' => 'required|string|max:20',
't_name' => 'required|string|max:50',
'zip1' => 'required|digits:3',
'zip2' => 'required|digits:4',
'adrs' => 'required|string|max:100',
'bldg' => 'nullable|string|max:80',
'tel1' => 'nullable|digits_between:2,4',
'tel2' => 'nullable|digits_between:2,4',
'tel3' => 'nullable|digits_between:3,4',
'fax1' => 'nullable|digits_between:2,4',
'fax2' => 'nullable|digits_between:2,4',
'fax3' => 'nullable|digits_between:3,4',
'company_image' => 'nullable|image|mimes:png,jpg,jpeg|max:2048',
];
// カスタム日本語メッセージ
$messages = [
't_number.required' => '適格請求書発行事業者番号を入力してください。',
't_number.max' => '適格請求書発行事業者番号は20文字以内で入力してください。',
't_name.required' => '適格事業者名を入力してください。',
't_name.max' => '適格事業者名は50文字以内で入力してください。',
'zip1.required' => '郵便番号(前半)を入力してください。',
'zip1.digits' => '郵便番号前半は3桁で入力してください。',
'zip2.required' => '郵便番号(後半)を入力してください。',
'zip2.digits' => '郵便番号後半は4桁で入力してください。',
'adrs.required' => '表示住所を入力してください。',
'adrs.max' => '表示住所は100文字以内で入力してください。',
'bldg.max' => '建物名は80文字以内で入力してください。',
'tel1.digits_between' => '電話番号1は2桁から4桁で入力してください。',
'tel2.digits_between' => '電話番号2は2桁から4桁で入力してください。',
'tel3.digits_between' => '電話番号3は3桁から4桁で入力してください。',
'fax1.digits_between' => 'FAX番号1は2桁から4桁で入力してください。',
'fax2.digits_between' => 'FAX番号2は2桁から4桁で入力してください。',
'fax3.digits_between' => 'FAX番号3は3桁から4桁で入力してください。',
'company_image.image' => '社判画像は画像ファイルを選択してください。',
'company_image.mimes' => '社判画像はpng, jpg, jpeg形式でアップロードしてください。',
'company_image.max' => '社判画像は2MB以下にしてください。',
];
// バリデーション実行(カスタムメッセージ適用)
$request->validate($rules, $messages);
// データ整形
$zipcode = $request->zip1 . '-' . $request->zip2;
$tel = implode('-', array_filter([$request->tel1, $request->tel2, $request->tel3]));
$fax = implode('-', array_filter([$request->fax1, $request->fax2, $request->fax3]));
// 既存レコードを取得1レコード運用
$row = InvSetting::first();
// 画像処理
$imagePath = $row?->company_image_path;
if ($request->hasFile('company_image')) {
if ($imagePath && Storage::disk('public')->exists($imagePath)) {
Storage::disk('public')->delete($imagePath);
}
$imagePath = $request->file('company_image')->store('inv', 'public');
}
// レコードを新規作成 or 更新
if ($row) {
$row->update([
't_number' => $request->t_number,
't_name' => $request->t_name,
'zipcode' => $zipcode,
'adrs' => $request->adrs,
'bldg' => $request->bldg,
'tel_num' => $tel,
'fax_num' => $fax,
'company_image_path' => $imagePath,
]);
} else {
InvSetting::create([
't_number' => $request->t_number,
't_name' => $request->t_name,
'zipcode' => $zipcode,
'adrs' => $request->adrs,
'bldg' => $request->bldg,
'tel_num' => $tel,
'fax_num' => $fax,
'company_image_path' => $imagePath,
]);
}
return back()->with('success', 'インボイス設定を登録しました。');
}
}