All checks were successful
Deploy preview (main_higashide) / deploy (push) Successful in 11s
134 lines
4.8 KiB
PHP
134 lines
4.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Http\Controllers\Controller;
|
||
use Mpdf\Mpdf;
|
||
use function base_path;
|
||
|
||
class ReceiptController extends Controller
|
||
{
|
||
// 宛名入力画面表示
|
||
public function input($contract_id)
|
||
{
|
||
$user_id = session('user_id');
|
||
if (!$user_id) {
|
||
return redirect('/login');
|
||
}
|
||
$user = DB::table('user')->where('user_id', $user_id)->first();
|
||
|
||
\Log::info('領収書宛名入力画面にアクセス', [
|
||
'user_id' => $user_id,
|
||
]);
|
||
|
||
return view('receipt.input', [
|
||
'user_name' => $user ? $user->user_name : '', // ユーザー名(ヘッダー用)
|
||
'contract_id' => $contract_id
|
||
]);
|
||
}
|
||
|
||
// 領収書発行(入力内容の保存)
|
||
public function issue(Request $request, $contract_id)
|
||
{
|
||
$user_id = session('user_id');
|
||
if (!$user_id) {
|
||
return redirect('/login');
|
||
}
|
||
|
||
$receipt_name = $request->input('receipt_name');
|
||
$keisho = $request->input('keisho');
|
||
|
||
// 既存レコードチェック
|
||
$exists = DB::table('inv_publish')->where('contract_id', $contract_id)->exists();
|
||
if ($exists) {
|
||
// エラー時はinput画面に戻し、メッセージ表示
|
||
return redirect()->back()->withInput()->withErrors(['contract_id' => 'この契約の領収書は既に発行されています。契約履歴から再発行を行ってください。']);
|
||
}
|
||
|
||
// 4バイト文字(絵文字等)チェック
|
||
if (preg_match('/[\xF0-\xF7][\x80-\xBF]{3}/', $receipt_name)) {
|
||
return redirect()->back()->withInput()->withErrors(['contract_id' => '宛名に絵文字などの特殊文字は使用できません。']);
|
||
}
|
||
|
||
// 文字数チェック
|
||
if (mb_strlen($receipt_name) > 30) {
|
||
return redirect()->back()->withInput()->withErrors(['contract_id' => '宛名は30文字以内で入力してください。']);
|
||
}
|
||
|
||
// 敬称選択チェック
|
||
if (empty($keisho)) {
|
||
return redirect()->back()->withInput()->withErrors(['contract_id' => '敬称を選択してください。']);
|
||
}
|
||
|
||
// inv_publishテーブルに新規登録(insert)
|
||
$inv_name = $receipt_name . $keisho;
|
||
$now = date('Y-m-d H:i:s');
|
||
$seq = DB::table('inv_publish')->max('seq') ?? 0;
|
||
$seq = $seq + 1;
|
||
DB::table('inv_publish')->insert([
|
||
'seq' => $seq,
|
||
'user_id' => $user_id,
|
||
'contract_id' => $contract_id,
|
||
'inv_name' => $inv_name,
|
||
'published_at' => date('Y-m-d'),
|
||
'type' => 0,
|
||
'count' => 1,
|
||
'created_at' => $now,
|
||
'updated_at' => null,
|
||
]);
|
||
|
||
// 完了後はdownloadメソッドを直接呼び出し(再発行フラグfalseで渡す)
|
||
$is_reissue = false;
|
||
return $this->download($contract_id, $is_reissue);
|
||
}
|
||
|
||
public function download($contract_id, $is_reissue = true)
|
||
{
|
||
// 必要なデータを取得
|
||
$contract = DB::table('regular_contract')->where('contract_id', $contract_id)->first();
|
||
$inv = DB::table('inv_publish')->where('contract_id', $contract_id)->first();
|
||
$t_number = DB::table('inv_setting')->value('t_number');
|
||
|
||
// park_name取得(regular_contract.park_id=park.park_id)
|
||
$park_name = '';
|
||
if ($contract && $contract->park_id) {
|
||
$park = DB::table('park')->where('park_id', $contract->park_id)->first();
|
||
if ($park && $park->park_name) {
|
||
$park_name = $park->park_name;
|
||
}
|
||
}
|
||
|
||
// BladeテンプレートをHTMLにレンダリング
|
||
$html = view('receipt.pdf', [
|
||
'contract' => $contract,
|
||
'inv' => $inv,
|
||
't_number' => $t_number,
|
||
'park_name' => $park_name,
|
||
'is_reissue' => $is_reissue,
|
||
])->render();
|
||
|
||
// mPDF最新版(autoload対応)
|
||
$mpdf = new \Mpdf\Mpdf([
|
||
'mode' => 'ja',
|
||
'format' => 'A4',
|
||
'custom_font_dir' => resource_path('fonts'),
|
||
'custom_font_data' => [
|
||
'noto_sans_jp' => [
|
||
'R' => 'NotoSansJP-Regular.ttf', // 通常フォント
|
||
'B' => 'NotoSansJP-Bold.ttf', // 太字フォント
|
||
]
|
||
],
|
||
'default_font' => 'noto_sans_jp',
|
||
]);
|
||
|
||
$mpdf->WriteHTML($html);
|
||
|
||
// PDFダウンロード
|
||
return response($mpdf->Output('receipt_' . $contract_id . '.pdf', 'S'))
|
||
->header('Content-Type', 'application/pdf')
|
||
->header('Content-Disposition', 'attachment; filename="receipt_' . $contract_id . '.pdf"');
|
||
}
|
||
}
|