JSlip  1.0
_UserMenuController.php
Go to the documentation of this file.
1 <?php
8 require_once(dirname(__FILE__) . '/../../lib/View.php');
9 require_once(dirname(__FILE__) . '/../../lib/Controller.php');
10 require_once(dirname(__FILE__) . '/UserMenuModel.php');
11 
13 {
14  public $param;
15  public $rest;
16  public $viewName;
17  public $model;
18  public $view;
19  public $dat;
20  public $csv;
21 
22  public function main($param) {
23 
24  $this->rest = '';
25  $this->csv = '';
26  $this->param = $param;
27  $this->model = new UserMenuModel();
28  $this->view = new View();
29 
30  $this->param['base'] = dirname(__FILE__);
31 
32  if (empty($this->param['act'])) {
33  $this->_menu();
34  } else {
35  switch ($this->param['act']) {
36  case 'useLast': $this->_useLast(); break;
37  case 'cntJournal': $this->_cntJournal(); break;
38  case 'setBid': $this->_setBid(); break;
39  case 'exportCsv': $this->_exportCsv(); break;
40  case 'importCsv': $this->_importCsv(); break;
41  default: $this->_menu(); break;
42  }
43  }
44  }
45 
46  public function dispCsv() {
47  $this->_dispCsv();
48  $this->_clearFiles();
49  }
50 
51  private function _menu() {
52 
53  $this->viewName = 'user_menu';
54 
55  if (empty($this->param['bid'])) {
56  if (empty($_SESSION['minfo']['mid'])) {
57  $this->dat = ['err' => 'Out of service.'];
58  $this->viewName = 'user_menu_error';
59  return;
60  } else {
61  if ($_SESSION['minfo']['role'] == 'root') {
62  $basic = $this->model->getBasicByBid($_SESSION['minfo']['bid']);
63  } else {
64  if (empty($_SESSION['minfo']['bid'])) {
65  $basic = $this->model->getBasicByMid($_SESSION['minfo']['mid']);
66  } else {
67  $basic = $this->model->getBasicByBid($_SESSION['minfo']['bid']);
68  }
69  }
70  }
71  } else {
72  $basic = $this->model->getBasicByBid($this->param['bid']);
73  }
74 
75  $this->dat = ['basic' => $basic];
76 
77  if (empty($this->dat['basic'])) {
78  $this->dat = ['err' => 'Out of service.'];
79  $this->viewName = 'user_menu_err';
80  return;
81  }
82 
83  if (empty($this->param['bid'])) {
84  $_SESSION['minfo']['bid'] = $this->dat['basic'][0]['id'];
85  } else {
86  $_SESSION['minfo']['bid'] = $this->param['bid'];
87  }
88  }
89 
90  private function _useLast() {
91 
92  $bid = $this->param['bid'];
93  $last = $this->model->getLast($bid);
94 
95  if ($last < 0) {
96  $ans = ['sts' => 'NG', 'err' => '前年度のデータが見つかりません。'];
97  } else {
98  $err = $this->model->useLast($_SESSION['minfo']['mid'], $last, $bid);
99  if (empty($err)) {
100  $ans = ['sts' => 'OK', 'err' => ''];
101  } else {
102  $ans = ['sts' => 'NG', 'err' => $err['msg']];
103  }
104  }
105 
106  $this->rest = json_encode($ans);
107  }
108 
109  private function _cntJournal() {
110  $cnt = $this->model->cntJournal($this->param['bid']);
111  $this->rest = json_encode(['sts' => 'OK', 'cnt' => $cnt]);
112  }
113 
114  private function _setBid() {
115  $_SESSION['minfo']['bid'] = $this->param['bid'];
116  $this->rest = json_encode(['sts' => 'OK']);
117  }
118 
119  private function _exportCsv() {
120  $this->csv = $this->param['tmp_dir'] . '/slip_' . $_SESSION['minfo']['bid'] . '_' . date('YmdHis') . '.csv';
121  $this->model->makeCsv($_SESSION['minfo']['bid'], $this->csv);
122  }
123 
124  private function _importCsv() {
125 
126  $err = [];
127  $records = [];
128 
129  $this->viewName = 'user_menu';
130 
131  $upfile = $this->param['tmp_dir'] . '/' . basename($_FILES['upfile']['name']);
132 
133  if (empty($_FILES['upfile']['name'])) {
134  $err['File Name'] = 'ファイル名が指定されていません。';
135  } elseif (!move_uploaded_file($_FILES['upfile']['tmp_name'], $upfile)) {
136  $err['Upload'] = 'ファイルのアップロードに失敗しました。';
137  } else {
138  $file = new SplFileObject($upfile);
139  $file->setFlags(SplFileObject::READ_CSV);
140  foreach ($file as $line) {
141  if (!is_null($line[0])) {
142  $records[] = $line;
143  }
144  }
145  }
146 
147  if (empty($err)) {
148  $validErr = $this->_ValidImportCsv($_SESSION['minfo']['bid'], $records);
149  if (empty($validErr)) {
150  $err = $this->model->setImportedCsvData($_SESSION['minfo']['mid'], $_SESSION['minfo']['bid'], $records);
151  } else {
152  $err['Valid'] = $validErr;
153  }
154  }
155 
156  $basic = $this->model->getBasicByBid($this->param['bid']);
157  $this->dat = ['err' => $err, 'basic' => $basic];
158  }
159 
160  private function _ValidImportCsv($bid, $rec) {
161 
162  $r0 = (empty($rec[0][0])) ? '' : $rec[0][0];
163  $r1 = (empty($rec[1][0])) ? '' : $rec[1][0];
164  $r2 = (empty($rec[2][0])) ? '' : $rec[2][0];
165  $r3 = (empty($rec[3][0])) ? '' : $rec[3][0];
166  $r = $r0 . $r1 . $r2 . $r3;
167 
168  if ($r != 'HEADERBASICHEADERSECTION') {
169  return 'データ・フォーマットが違います。';
170  }
171 
172  $cnt = $this->model->cntJournal($bid);
173 
174  if ($cnt > 0) {
175  return '伝票が入力されています。';
176  }
177 
178  return '';
179  }
180 
181  private function _dispCsv() {
182  header('Content-Type: application/octet-stream');
183  header('Content-Length: ' . filesize($this->csv));
184  header('Content-Disposition: inline; filename=' . basename($this->csv));
185  readfile($this->csv);
186  }
187 
188  private function _clearFiles() {
189  exec(UTL_DELETE . ' -f ' . $this->csv);
190  }
191 }
UserMenuController\$viewName
$viewName
Definition: _UserMenuController.php:16
UserMenuController\_useLast
_useLast()
Definition: _UserMenuController.php:90
UserMenuController\dispCsv
dispCsv()
Definition: _UserMenuController.php:46
UserMenuController\_exportCsv
_exportCsv()
Definition: _UserMenuController.php:119
UserMenuController\_setBid
_setBid()
Definition: _UserMenuController.php:114
UserMenuController\main
main($param)
Definition: _UserMenuController.php:22
View
Definition: View.php:8
UTL_DELETE
const UTL_DELETE
Definition: local.php:52
UserMenuController\_menu
_menu()
Definition: _UserMenuController.php:51
UserMenuController\$param
$param
Definition: _UserMenuController.php:14
UserMenuController\_ValidImportCsv
_ValidImportCsv($bid, $rec)
Definition: _UserMenuController.php:160
Controller
Definition: Controller.php:15
UserMenuController\_clearFiles
_clearFiles()
Definition: _UserMenuController.php:188
UserMenuController\_dispCsv
_dispCsv()
Definition: _UserMenuController.php:181
UserMenuController
Definition: _UserMenuController.php:12
UserMenuController\$rest
$rest
Definition: _UserMenuController.php:15
UserMenuController\_importCsv
_importCsv()
Definition: _UserMenuController.php:124
UserMenuController\$csv
$csv
Definition: _UserMenuController.php:20
UserMenuController\$model
$model
Definition: _UserMenuController.php:17
UserMenuController\$dat
$dat
Definition: _UserMenuController.php:19
UserMenuController\$view
$view
Definition: _UserMenuController.php:18
UserMenuModel
Definition: UserMenuModel.php:10
$cnt
$cnt
Definition: tex_tmplt_bs.php:319
UserMenuController\_cntJournal
_cntJournal()
Definition: _UserMenuController.php:109