so-manager-dev.com/app/Http/Controllers/ReceiptController.php
Yuka Higashide 84b13a70da
All checks were successful
Deploy preview (main_higashide) / deploy (push) Successful in 11s
契約確認、領収書発行関連修正
2025-08-29 22:25:20 +09:00

118 lines
4.0 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;
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();
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' => 'この契約の領収書は既に発行されています。契約履歴から再発行を行ってください。']);
}
// 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"');
}
}