25
25
use App \Models \User \Opacs \OpacsConfigsSelect ;
26
26
27
27
use Carbon \Carbon ;
28
-
29
28
use App \Mail \ConnectMail ;
30
29
use App \Plugins \User \UserPluginBase ;
31
30
32
31
use App \Enums \DeliveryRequestFlag ;
32
+ use App \Enums \LentFlag ;
33
33
use App \Enums \OpacConfigSelectType ;
34
+ use App \Enums \CsvCharacterCode ;
35
+
36
+ use App \Utilities \Csv \CsvUtils ;
34
37
35
38
/**
36
39
* Opacプラグイン
@@ -61,8 +64,25 @@ public function getPublicFunctions()
61
64
{
62
65
// 標準関数以外で画面などから呼ばれる関数の定義
63
66
$ functions = [];
64
- $ functions ['get ' ] = ['settingOpacFrame ' , 'lentlist ' , 'searchClear ' , 'searchDetailClear ' , 'roleLent ' , 'editDeliveryRequest ' ];
65
- $ functions ['post ' ] = ['lent ' , 'requestLent ' , 'returnLent ' , 'search ' , 'saveOpacFrame ' , 'getBookInfo ' , 'destroyRequest ' , 'saveDeliveryRequest ' ];
67
+ $ functions ['get ' ] = [
68
+ 'settingOpacFrame ' ,
69
+ 'lentlist ' ,
70
+ 'searchClear ' ,
71
+ 'searchDetailClear ' ,
72
+ 'roleLent ' ,
73
+ 'editDeliveryRequest '
74
+ ];
75
+ $ functions ['post ' ] = [
76
+ 'lent ' ,
77
+ 'requestLent ' ,
78
+ 'returnLent ' ,
79
+ 'search ' ,
80
+ 'saveOpacFrame ' ,
81
+ 'getBookInfo ' ,
82
+ 'destroyRequest ' ,
83
+ 'saveDeliveryRequest ' ,
84
+ 'downloadCsvLent '
85
+ ];
66
86
return $ functions ;
67
87
}
68
88
@@ -87,6 +107,7 @@ public function declareRole()
87
107
88
108
$ role_check_table ["lentlist " ] = ['role_article ' ];
89
109
$ role_check_table ["roleLent " ] = ['role_article ' ];
110
+ $ role_check_table ["downloadCsvLent " ] = ['role_article ' ];
90
111
91
112
// change: getBookInfoの呼び出しは create,edit のみ. create,edit の権限を設定する
92
113
// $role_check_table["getBookInfo"] = ['role_article'];
@@ -1849,6 +1870,12 @@ public function lentlist($request, $page_id, $frame_id, $message = null, $errors
1849
1870
return $ this ->viewError (403 );
1850
1871
}
1851
1872
1873
+ // OPAC&フレームデータ
1874
+ $ opac_frame = $ this ->getOpacFrame ($ frame_id );
1875
+ if (empty ($ opac_frame ) || empty ($ opac_frame ->bucket_id )) {
1876
+ return ;
1877
+ }
1878
+
1852
1879
// 郵送リクエスト一覧取得
1853
1880
$ books_requests = OpacsBooksLents::select ('opacs_books_lents.* ' , 'users.name ' , 'opacs_books.title ' , 'opacs_books.barcode ' , 'opacs_books.isbn ' )
1854
1881
->leftJoin ('opacs_books ' , 'opacs_books.id ' , '= ' , 'opacs_books_lents.opacs_books_id ' )
@@ -1871,6 +1898,7 @@ public function lentlist($request, $page_id, $frame_id, $message = null, $errors
1871
1898
return $ this ->view ('opacs_lentlist ' , [
1872
1899
'books_lents ' => $ books_lents ,
1873
1900
'books_requests ' => $ books_requests ,
1901
+ 'opac_frame ' => $ opac_frame ,
1874
1902
'message ' => $ message ,
1875
1903
'errors ' => $ errors ,
1876
1904
]);
@@ -1984,6 +2012,107 @@ public function destroyRequest($request, $page_id, $frame_id, $opacs_books_id)
1984
2012
return $ this ->index ($ request , $ page_id , $ frame_id , null , ['貸し出しリクエストを削除しました。 ' ]);
1985
2013
}
1986
2014
2015
+ /**
2016
+ * 貸出数ダウンロード
2017
+ */
2018
+ public function downloadCsvLent ($ request , $ page_id , $ frame_id , $ id )
2019
+ {
2020
+ // opac&フレームデータ
2021
+ $ opac_frame = $ this ->getOpacFrame ($ frame_id );
2022
+ if (empty ($ opac_frame ) || empty ($ opac_frame ->bucket_id )) {
2023
+ return ;
2024
+ }
2025
+
2026
+ $ validator = Validator::make ($ request ->all (), [
2027
+ 'lent_term_from ' => ['nullable ' , 'date ' ],
2028
+ 'lent_term_to ' => ['nullable ' , 'date ' ],
2029
+ ]);
2030
+ $ validator ->setAttributeNames ([
2031
+ 'lent_term_from ' => '期間(From) ' ,
2032
+ 'lent_term_to ' => '期間(To) ' ,
2033
+ ]);
2034
+ if ($ validator ->fails ()) {
2035
+ return $ this ->lentList ($ request , $ page_id , $ frame_id , null , $ validator ->errors ());
2036
+ }
2037
+
2038
+ // カラム
2039
+ $ columns = [
2040
+ 'title ' => 'タイトル ' ,
2041
+ 'barcode ' => 'バーコード ' ,
2042
+ 'lent_count ' => '貸出数 ' ,
2043
+ ];
2044
+
2045
+ // 返却用配列
2046
+ $ csv_array = [];
2047
+
2048
+ // 見出し行
2049
+ foreach ($ columns as $ columnKey => $ column ) {
2050
+ $ csv_array [0 ][$ columnKey ] = $ column ;
2051
+ }
2052
+
2053
+ // 貸出中・貸出終了の累計
2054
+ $ lents_query = OpacsBooksLents::select ('opacs_books.title ' , 'opacs_books.barcode ' , DB ::raw ("count(opacs_books_lents.id) as lent_count " ))
2055
+ ->leftJoin ('opacs_books ' , function ($ join ) use ($ opac_frame ) {
2056
+ $ join ->on ('opacs_books.id ' , '= ' , 'opacs_books_lents.opacs_books_id ' )
2057
+ ->where ('opacs_books.opacs_id ' , '= ' , $ opac_frame ->opacs_id );
2058
+ })
2059
+ ->whereIn ('opacs_books_lents.lent_flag ' , [LentFlag::rented, LentFlag::finished])
2060
+ ->groupBy ('opacs_books.title ' )
2061
+ ->groupBy ('opacs_books.barcode ' );
2062
+
2063
+ if ($ request ->lent_term_from ) {
2064
+ $ lents_query = $ lents_query ->whereDate ('opacs_books_lents.created_at ' , '>= ' , $ request ->lent_term_from . ' 00:00:00 ' );
2065
+ }
2066
+ if ($ request ->lent_term_to ) {
2067
+ $ lents_query = $ lents_query ->whereDate ('opacs_books_lents.created_at ' , '<= ' , $ request ->lent_term_to . ' 23:59:59 ' );
2068
+ }
2069
+
2070
+ $ lents = $ lents_query ->get ();
2071
+
2072
+ // 行数
2073
+ $ csv_line_no = 1 ;
2074
+
2075
+ // データ
2076
+ foreach ($ lents as $ counter_count ) {
2077
+ $ csv_line = [];
2078
+ foreach ($ columns as $ columnKey => $ column ) {
2079
+ $ csv_line [$ columnKey ] = $ counter_count ->$ columnKey ;
2080
+ }
2081
+
2082
+ $ csv_array [$ csv_line_no ] = $ csv_line ;
2083
+ $ csv_line_no ++;
2084
+ }
2085
+
2086
+ // レスポンス版
2087
+ $ filename = 'lent_counts.csv ' ;
2088
+ $ headers = [
2089
+ 'Content-Type ' => 'text/csv ' ,
2090
+ 'content-Disposition ' => 'attachment; filename=" ' .$ filename .'" ' ,
2091
+ ];
2092
+
2093
+ // データ
2094
+ $ csv_data = '' ;
2095
+ foreach ($ csv_array as $ csv_line ) {
2096
+ foreach ($ csv_line as $ csv_col ) {
2097
+ $ csv_data .= '" ' . $ csv_col . '", ' ;
2098
+ }
2099
+ // 末尾カンマを削除
2100
+ $ csv_data = substr ($ csv_data , 0 , -1 );
2101
+ $ csv_data .= "\n" ;
2102
+ }
2103
+
2104
+ // 文字コード変換
2105
+ if ($ request ->character_code == CsvCharacterCode::utf_8) {
2106
+ $ csv_data = mb_convert_encoding ($ csv_data , CsvCharacterCode::utf_8);
2107
+ // UTF-8のBOMコードを追加する(UTF-8 BOM付きにするとExcelで文字化けしない)
2108
+ $ csv_data = CsvUtils::addUtf8Bom ($ csv_data );
2109
+ } else {
2110
+ $ csv_data = mb_convert_encoding ($ csv_data , CsvCharacterCode::sjis_win);
2111
+ }
2112
+
2113
+ return response ()->make ($ csv_data , 200 , $ headers );
2114
+ }
2115
+
1987
2116
/**
1988
2117
* 配送希望設定の表示
1989
2118
*
0 commit comments