JSlip  1.0
UserJournalController.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__) . '/UserJournalModel.php');
11 
12 define('PAGER_RPP', 3);
13 
15 {
16  public $rest;
17  public $param;
18  public $model;
19  public $view;
20  public $viewName;
21  public $simpleName;
22  public $dat;
23  public $pager;
24  public $err;
25  public $bid;
26  public $basic;
27  public $mode;
28  public $item;
29  public $sect;
30  public $stld;
31  public $kcd;
32  public $section;
33  public $settled;
34  public $begin;
35  public $end;
36 
37  public function main($param) {
38 
39  $this->rest = '';
40  $this->param = $param;
41  $this->bid = $_SESSION['minfo']['bid'];
42  $this->model = new UserJournalModel($this->bid);
43  $this->view = new View();
44  $this->dat = [];
45 
46  $this->param['base'] = dirname(__FILE__);
47 
48  $basic = $this->model->getBasicByBid($this->bid);
49 
50  if (empty($basic[0])) {
51  $this->_error('基本情報が見つかりません。');
52  return;
53  } else {
54  $this->basic = $basic[0];
55  }
56 
57  $this->begin = str_replace('-', '/', $this->basic['term_begin']);
58  $this->end = str_replace('-', '/', $this->basic['term_end']);
59 
60  $this->dat['limit'] = $this->model->getLimit();
61 
62  if (empty($this->param['act'])) {
63  $this->_list();
64  } else {
65  switch ($this->param['act']) {
66  case 'create': $this->_create(); break;
67  case 'drop': $this->_drop(); break;
68  case 'edit': $this->_edit(); break;
69  case 'duplicate': $this->_duplicate(); break;
70  case 'tax': $this->_tax(); break;
71  case 'remember': $this->_remember(); break;
72  case 'search': $this->_search(); break;
73  case 'check': $this->_check(); break;
74  case 'regist': $this->_regist(); break;
75  default: $this->_list(); break;
76  }
77  }
78  }
79 
80  private function _error($err) {
81 
82  $this->viewName = 'user_journal_err';
83  $this->err = $err;
84  }
85 
86  private function _list() {
87 
88  $this->viewName = 'user_journal_list';
89 
90  $this->dat['cnd'] = [
91  'cnd_scd' => '-1',
92  'cnd_begin' => $this->begin,
93  'cnd_end' => $this->end,
94  'cnd_denpyo' => '',
95  'cnd_kcd' => '-1',
96  'cnd_remark' => '',
97  'cnd_stflg' => '-2', // setteled flag
98  'cnd_nuflg' => '-1', // not use flag
99  'pager' => ['page' => 1, 'rpp' => PAGER_RPP],
100  ];
101 
102  $this->dat['list'] = $this->model->getList($this->dat['cnd']);
103  $this->kcd = $this->model->getKcd($this->bid);
104  $this->section = $this->model->getSection($this->bid);
105  $this->settled = $this->model->getConst('c_settled');
106 
107  $_SESSION['user_journal_list_cnd'] = $this->dat['cnd'];
108  }
109 
110  private function _search() {
111 
112  $this->viewName = 'user_journal_list';
113 
114  $this->dat['cnd'] = [
115  'cnd_scd' => $this->param['cnd_scd'],
116  'cnd_begin' => $this->param['cnd_begin'],
117  'cnd_end' => $this->param['cnd_end'],
118  'cnd_denpyo' => $this->param['cnd_denpyo'],
119  'cnd_kcd' => $this->param['cnd_kcd'],
120  'cnd_remark' => $this->param['cnd_remark'],
121  'cnd_stflg' => $this->param['cnd_stflg'],
122  'cnd_nuflg' => $this->param['cnd_nuflg'],
123  'pager' => ['page' => $this->param['page_curr'], 'rpp' => PAGER_RPP],
124  ];
125 
126  $this->dat['list'] = $this->model->getList($this->dat['cnd']);
127  $this->kcd = $this->model->getKcd($this->bid);
128  $this->section = $this->model->getSection($this->bid);
129  $this->settled = $this->model->getConst('c_settled');
130 
131  $_SESSION['user_journal_list_cnd'] = $this->dat['cnd'];
132  }
133 
134  private function _remember() {
135 
136  $this->viewName = 'user_journal_list';
137  $this->dat['cnd'] = $_SESSION['user_journal_list_cnd'];
138  $this->dat['list'] = $this->model->getList($this->dat['cnd']);
139  $this->kcd = $this->model->getKcd($this->bid);
140  $this->section = $this->model->getSection($this->bid);
141  $this->settled = $this->model->getConst('c_settled');
142  }
143 
144  private function _edit() {
145 
146  $this->viewName = 'user_journal_edit';
147 
148  $this->mode = 'edit';
149  $this->dat = $this->model->getData($this->param['id']);
150  $this->item['name'] = $this->model->getItemByName($this->bid);
151  $this->item['time'] = $this->model->getItemByTime($this->bid);
152  $this->sect = $this->model->getSection($this->bid);
153  $this->stld = $this->model->getConst('c_settled');
154  }
155 
156  private function _duplicate() {
157 
158  $this->viewName = 'user_journal_edit';
159 
160  $this->mode = 'duplicate';
161  $this->dat = $this->model->getData($this->param['id']);
162  $this->item['name'] = $this->model->getItemByName($this->bid);
163  $this->item['time'] = $this->model->getItemByTime($this->bid);
164  $this->sect = $this->model->getSection($this->bid);
165  $this->stld = $this->model->getConst('c_settled');
166  }
167 
168  private function _tax() {
169 
170  $this->singleViewName = 'user_journal_tax';
171 
172  $round = $this->model->getRound($this->bid);
173  $tax = $this->model->getTax($this->bid);
174  $val = $this->param['val'] * 1.0;
175 
176  $rid = $round['c0'];
177  $rname = $round['name'];
178 
179  $ans = [];
180  $ans['val'] = $val;
181  $ans['rname'] = $rname;
182  $ans['otax'] = [];
183  $ans['itax'] = [];
184 
185  // 外税
186  foreach ($tax as $k => $d) {
187  $n = $d['name'];
188  $r = $d['rate'] * 1.0;
189  $t = $val * $r;
190  switch ($rid) {
191  case -1: $t = floor($t); break; // 切捨て
192  case 0: $t = round($t); break; // 四捨五入
193  case 1: $t = ceil($t); break; // 切上げ
194  }
195  $ans['otax'][$k] = ['name' => $n, 'tax' => $t];
196  }
197 
198  // 内税
199  foreach ($tax as $k => $d) {
200  $n = $d['name'];
201  $r = $d['rate'] * 1.0;
202  $t = ($val * $r) / (1.0 + $r);
203  switch ($rid) {
204  case -1: $t = floor($t); break; // 切捨て
205  case 0: $t = round($t); break; // 四捨五入
206  case 1: $t = ceil($t); break; // 切上げ
207  }
208  $ans['itax'][$k] = ['name' => $n, 'tax' => $t];
209  }
210 
211  $this->ans = $ans;
212  }
213 
214  private function _check() {
215 
216  $err = [];
217 
218  $insert = (empty($this->param['insert'])) ? false : true;
219  $ymd = $this->param['arg']['ymd'];
220  $dat = $this->param['arg']['dat'];
221 
222  if (empty($ymd)) {
223  $err[] = '伝票日付は必須です。';
224  } else {
225  if (!$this->chkYmd($ymd)) {
226  $err[] = '不正な伝票日付です。';
227  } else {
228  if ($ymd < $this->begin || $this->end < $ymd) {
229  $err[] = '伝票日付が範囲外です。';
230  }
231  }
232  }
233 
234  $deb_sum = 0;
235  $cre_sum = 0;
236  $n = 0;
237 
238  foreach ($dat as $k => $d) {
239 
240  if ($d['deb_name'] == -1 && $d['cre_name'] == -1) {
241  continue;
242  }
243 
244  $n++;
245 
246  if ($d['deb_name'] == -1 || $d['cre_name'] == -1) {
247  $err[] = $k . '行目:借方科目または貸方科目が指定されていません。';
248  }
249 
250  if ($d['deb_name'] == $d['cre_name']) {
251  $err[] = $k . '行目:借方科目と貸方科目が同じです。';
252  }
253 
254  if ($d['deb_amount'] == 0 && $d['cre_amount'] == 0) {
255  $err[] = $k . '行目:金額が入力されていません。';
256  }
257 
258  if ($d['remark'] == '') {
259  $err[] = $k . '行目:摘要が入力されていません。';
260  }
261 
262  $deb_sum += $d['deb_amount'];
263  $cre_sum += $d['cre_amount'];
264  }
265 
266  if ($deb_sum != $cre_sum) {
267  $err[] = '合計が等しくありません。';
268  }
269 
270  if ($n < 1) {
271  $err[] = '不正な伝票です。';
272  }
273 
274  if (empty($err)) {
275  $this->rest = json_encode(['sts' => 'OK']);
276  } else {
277  $this->rest = json_encode(['sts' => 'NG', 'err' => $err]);
278  }
279  }
280 
281  private function _regist() {
282 
283  if ($this->param['arg']['settled_flg'] == -1) {
284  $this->param['arg']['ymd'] = $this->begin;
285  }
286 
287  $insert = (empty($this->param['insert'])) ? false : true;
288 
289  if ($insert) {
290  $err = $this->model->insert($this->param);
291  } else {
292  $err = $this->model->regist($this->param);
293  }
294 
295  if (!empty($err)) {
296  $this->rest = json_encode(['sts' => 'NG', 'err' => $err]);
297  return;
298  }
299 
300  $this->rest = json_encode(['sts' => 'OK']);
301  }
302 
303  private function _create() {
304 
305  $this->viewName = 'user_journal_create';
306  $this->item['name'] = $this->model->getItemByName($this->bid);
307  $this->item['time'] = $this->model->getItemByTime($this->bid);
308  $this->sect = $this->model->getSection($this->bid);
309  $this->stld = $this->model->getConst('c_settled');
310  }
311 
312  private function _drop() {
313 
314  $err = $this->model->delete($this->param);
315 
316  if (!empty($err)) {
317  $this->rest = json_encode(['sts' => 'NG', 'err' => $err]);
318  return;
319  }
320 
321  $this->rest = json_encode(['sts' => 'OK']);
322  }
323 }
UserJournalController\$stld
$stld
Definition: UserJournalController.php:30
UserJournalController\_remember
_remember()
Definition: UserJournalController.php:134
UserJournalController\$end
$end
Definition: UserJournalController.php:35
UserJournalController\_tax
_tax()
Definition: UserJournalController.php:168
UserJournalController\$viewName
$viewName
Definition: UserJournalController.php:20
UserJournalController\$sect
$sect
Definition: UserJournalController.php:29
View
Definition: View.php:8
UserJournalController\$mode
$mode
Definition: UserJournalController.php:27
UserJournalController\$item
$item
Definition: UserJournalController.php:28
Controller\chkYmd
chkYmd($dt)
Definition: Controller.php:32
UserJournalController\$view
$view
Definition: UserJournalController.php:19
UserJournalController\$settled
$settled
Definition: UserJournalController.php:33
UserJournalController\$bid
$bid
Definition: UserJournalController.php:25
UserJournalController\_list
_list()
Definition: UserJournalController.php:86
UserJournalController\$pager
$pager
Definition: UserJournalController.php:23
UserJournalController\_regist
_regist()
Definition: UserJournalController.php:281
UserJournalModel
Definition: UserJournalModel.php:10
Controller
Definition: Controller.php:15
UserJournalController\$rest
$rest
Definition: UserJournalController.php:16
UserJournalController\_create
_create()
Definition: UserJournalController.php:303
UserJournalController
Definition: UserJournalController.php:14
UserJournalController\_error
_error($err)
Definition: UserJournalController.php:80
UserJournalController\$begin
$begin
Definition: UserJournalController.php:34
UserJournalController\_duplicate
_duplicate()
Definition: UserJournalController.php:156
UserJournalController\$kcd
$kcd
Definition: UserJournalController.php:31
UserJournalController\_search
_search()
Definition: UserJournalController.php:110
UserJournalController\$err
$err
Definition: UserJournalController.php:24
UserJournalController\$param
$param
Definition: UserJournalController.php:17
UserJournalController\$section
$section
Definition: UserJournalController.php:32
UserJournalController\_check
_check()
Definition: UserJournalController.php:214
UserJournalController\_drop
_drop()
Definition: UserJournalController.php:312
UserJournalController\$basic
$basic
Definition: UserJournalController.php:26
PAGER_RPP
const PAGER_RPP
Definition: UserJournalController.php:12
UserJournalController\$simpleName
$simpleName
Definition: UserJournalController.php:21
UserJournalController\$model
$model
Definition: UserJournalController.php:18
UserJournalController\main
main($param)
Definition: UserJournalController.php:37
UserJournalController\$dat
$dat
Definition: UserJournalController.php:22
UserJournalController\_edit
_edit()
Definition: UserJournalController.php:144