teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

6

コントローラーの追記

2020/05/14 09:32

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -105,11 +105,30 @@
105
105
  ]
106
106
  ];
107
107
  ```
108
- ヘッダーの定義部分はこのようになっています。
108
+ ヘッダーの定義部分(コントローラー)はこのようになっています。
109
109
  ```PHP
110
+ class HourListController extends BaseController
111
+ {
112
+ const CSV_DEFAULT_DATE_FORMAT = 'Y-m-d';
113
+ const PROJECT_SESSION_NAME = 'userDepartData';
114
+
115
+ public function download()
116
+ {
110
- $stream = fopen('php://temp', 'r+b');
117
+ $stream = fopen('php://temp', 'r+b');
111
- $header = config('hourListCSV.csv_header');
118
+ $header = config('hourListCSV.csv_header');
112
- fputcsv($stream, $header);
119
+ fputcsv($stream, $header);
120
+
121
+ rewind($stream);
122
+ $csv = str_replace(PHP_EOL, "\r\n", stream_get_contents($stream));
123
+ $csv = mb_convert_encoding($csv, 'SJIS-win', 'UTF-8');
124
+ $file_name = date('Y_m_d').'_工数一覧'.'.csv';
125
+ $headers = [
126
+ 'Content-Type' => 'text/csv',
127
+ 'Content-Disposition' => 'attachment; filename="'. $file_name . '"',
128
+ ];
129
+
130
+ return response()->make($csv, 200, $headers);
131
+ }
113
132
  ```
114
133
 
115
134
  現状

5

追記

2020/05/14 09:32

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -51,6 +51,17 @@
51
51
  日曜日の回数+最後の週が日曜日で終わらない時は+1となっています。
52
52
  Carbonを使って以下のように取得しています。
53
53
  ```PHP
54
+ public function periodSeparator($date)
55
+ {
56
+ if ($date->quarter < 3) {
57
+ $periodSeparator["startMonth"] = 1;
58
+ $periodSeparator["endMonth"] = 6;
59
+ } else {
60
+ $periodSeparator["startMonth"] = 7;
61
+ $periodSeparator["endMonth"]= 12;
62
+ }
63
+ return $periodSeparator;
64
+ }
54
65
  $todayDate = Carbon::now();
55
66
  $periodSeparator = self::periodSeparator($todayDate);
56
67
  for ($month = $periodSeparator["startMonth"]; $month <= $periodSeparator["endMonth"]; $month++) {
@@ -96,6 +107,7 @@
96
107
  ```
97
108
  ヘッダーの定義部分はこのようになっています。
98
109
  ```PHP
110
+ $stream = fopen('php://temp', 'r+b');
99
111
  $header = config('hourListCSV.csv_header');
100
112
  fputcsv($stream, $header);
101
113
  ```

4

weekNumByMonthの説明追加

2020/05/14 09:24

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -47,10 +47,30 @@
47
47
  $periodSeparator["endMonth"] = 12
48
48
  ```ここに言語を入力
49
49
  $weekNumByMonth[$month]
50
+ ```には月ごとの週数が入ってます。
51
+ 日曜日の回数+最後の週が日曜日で終わらない時は+1となっています。
52
+ Carbonを使って以下のように取得しています。
53
+ ```PHP
54
+ $todayDate = Carbon::now();
55
+ $periodSeparator = self::periodSeparator($todayDate);
56
+ for ($month = $periodSeparator["startMonth"]; $month <= $periodSeparator["endMonth"]; $month++) {
57
+     $date = Carbon::create($todayDate->year, $month);
58
+     $weekNumByMonth[$month] = $date->endOfMonth()->weekOfYear - $date->startOfMonth()->weekOfYear + 1;
59
+ }
60
+ ```
50
- ```には月ごとの週数が入ってます。例)$weekNumByMonth[1] = 5
61
+ 例)$weekNumByMonth[1] = 5
62
+ 例)$weekNumByMonth[2] = 5
63
+ 例)$weekNumByMonth[3] = 6
64
+ 例)$weekNumByMonth[4] = 5
65
+ 例)$weekNumByMonth[5] = 5
66
+ 例)$weekNumByMonth[6] = 5
67
+ 例)$weekNumByMonth[7] = 5
68
+ 例)$weekNumByMonth[8] = 6
69
+ 例)$weekNumByMonth[9] = 5
70
+ 例)$weekNumByMonth[10] = 5
71
+ 例)$weekNumByMonth[11] = 6
72
+ 例)$weekNumByMonth[12] = 5
51
73
 
52
-
53
-
54
74
  ### 該当のソースコード
55
75
  現在のConfigはこちら
56
76
  **hourListCSV**

3

$periodSeparatorについての説明追加

2020/05/14 07:00

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -37,6 +37,14 @@
37
37
  $periodSeparator["endMonth"]
38
38
 
39
39
  ```には半期の始めと終わりの月が入ってます
40
+ 例)
41
+ (1〜6月の場合)
42
+ $periodSeparator["startMonth"] = 1
43
+ $periodSeparator["endMonth"] = 6
44
+
45
+ (7〜12月の場合)
46
+ $periodSeparator["startMonth"] = 7
47
+ $periodSeparator["endMonth"] = 12
40
48
  ```ここに言語を入力
41
49
  $weekNumByMonth[$month]
42
50
  ```には月ごとの週数が入ってます。例)$weekNumByMonth[1] = 5

2

HTMLの追加

2020/05/14 06:45

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  ここに質問の内容を詳しく書いてください。
4
- PHP(CakePHP)で労務管理システムを作っています。
4
+ PHP(Laravel)で労務管理システムを作っています。
5
5
  CSV出力機能実装中につまづいてしまいました。
6
6
 
7
7
  ####最終的な実装イメージ
@@ -9,7 +9,40 @@
9
9
 
10
10
  このように上段に月、下段に週となるように出力するCSVのヘッダーを作りたいです。
11
11
 
12
+ ###HTML
13
+ ```HTML
14
+ <thead>
15
+ <tr>
16
+ <th rowspan="2">名前</th>
17
+ <th rowspan="2">部署</th>
18
+ <th rowspan="2">役職</th>
19
+ <th rowspan="2">予想</th>
20
+ <th rowspan="2">残業</th>
21
+ <th rowspan="2">合計</th>
22
+ @for ($month = $periodSeparator["startMonth"]; $month <= $periodSeparator["endMonth"]; $month++)
23
+ <th colspan={{ $weekNumByMonth[$month] }}>{{ $month }}月</th>
24
+ @endfor
25
+ </tr>
26
+ <tr>
27
+ @for ($month = $periodSeparator["startMonth"]; $month <= $periodSeparator["endMonth"]; $month++)
28
+ @for ($weeks = 1; $weeks <= $weekNumByMonth[$month]; $weeks++)
29
+ <th>{{ $weeks }}週</th>
30
+ @endfor
31
+ @endfor
32
+ </tr>
33
+ </thead>
34
+ ```
35
+ ```ここに言語を入力
36
+ $periodSeparator["startMonth"]
37
+ $periodSeparator["endMonth"]
12
38
 
39
+ ```には半期の始めと終わりの月が入ってます
40
+ ```ここに言語を入力
41
+ $weekNumByMonth[$month]
42
+ ```には月ごとの週数が入ってます。例)$weekNumByMonth[1] = 5
43
+
44
+
45
+
13
46
  ### 該当のソースコード
14
47
  現在のConfigはこちら
15
48
  **hourListCSV**

1

最終的なCSVのヘッダーのイメージ追加

2020/05/14 06:33

投稿

rikutennis
rikutennis

スコア55

title CHANGED
File without changes
body CHANGED
@@ -4,13 +4,15 @@
4
4
  PHP(CakePHP)で労務管理システムを作っています。
5
5
  CSV出力機能実装中につまづいてしまいました。
6
6
 
7
- ####実装イメージ
7
+ ####最終的な実装イメージ
8
- ![実装イメージ](a6a1c4442f6c995eb1265b3258b73bff.png)
8
+ ![イメージ説明](786b8155a5909383a8b254b74141adf3.png)
9
9
 
10
- このように上段に月、下段に週となるようにCSVのヘッダーを作りたいです。
10
+ このように上段に月、下段に週となるように出力するCSVのヘッダーを作りたいです。
11
11
 
12
+
12
13
  ### 該当のソースコード
13
14
  現在のConfigはこちら
15
+ **hourListCSV**
14
16
  ```PHP
15
17
  <?php
16
18