JSlip  1.0
UserJournalModel.php
Go to the documentation of this file.
1 <?php
8 require_once(dirname(__FILE__) . '/../../lib/Model.php');
9 
10 class UserJournalModel extends Model
11 {
12  public $bid;
13 
14  function __construct($bid) {
15  $this->bid = $bid;
16  }
17 
18  public function getLimit() {
19 
20  $limit = [];
21  $limit['max_slip'] = MAX_SLIP;
22 
23  $this->connect();
24 
25  $fmt = "SELECT COUNT(`s`.`id`) AS `cnt`"
26  . " FROM `t_journal` `j`"
27  . " INNER JOIN `t_jslip` `s` ON `j`.`id` = `s`.`jid`"
28  . " WHERE `j`.`bid` = '%s'"
29  ;
30  $sql = sprintf($fmt, $this->esc($this->bid));
31  $rec = $this->getRecord($sql);
32 
33  $this->close();
34 
35  $limit['cnt_slip'] = $rec[0]['cnt'];
36  $limit['chk_slip'] = ($limit['cnt_slip'] > $limit['max_slip']) ? 'NG' : 'OK';
37 
38  return $limit;
39  }
40 
41  public function getList($cnd) {
42 
43  $this->connect();
44 
45  $where = $this->_getListWhere($cnd);
46  $cnt = $this->_getListCnt($where);
47  $list = $this->_getListDat($where, $cnt, $cnd['pager']);
48 
49  $this->close();
50 
51  return $list;
52  }
53 
54  private function _getListWhere($cnd) {
55 
56  $where = " WHERE `j`.`bid` = '" . $this->esc($this->bid) . "'"
57  . " AND `j`.`ymd` >= '" . $this->esc($cnd['cnd_begin']) . "'"
58  . " AND `j`.`ymd` <= '" . $this->esc($cnd['cnd_end']) . "'"
59  ;
60 
61  if ($cnd['cnd_scd'] != '-1') {
62  $where .= " AND `j`.`scd` = '" . $this->esc($cnd['cnd_scd']) . "'";
63  }
64 
65  if ($cnd['cnd_denpyo'] != '') {
66  $where .= " AND `j`.`id` = '" . $this->esc($cnd['cnd_denpyo']) . "'";
67  }
68 
69  if ($cnd['cnd_kcd'] != '-1') {
70  $where .= " AND (`s`.`debit` = '" . $this->esc($cnd['cnd_kcd']) . "'"
71  . " OR"
72  . " `s`.`credit` = '" . $this->esc($cnd['cnd_kcd']) . "'"
73  . ")"
74  ;
75  }
76 
77  if ($cnd['cnd_remark'] != '') {
78  $where .= " AND `s`.`remark` LIKE '%" . $this->esc($cnd['cnd_remark']) . "%'";
79  }
80 
81  if ($cnd['cnd_stflg'] != '-2') {
82  $where .= " AND `j`.`settled_flg` = '" . $this->esc($cnd['cnd_stflg']) . "'";
83  }
84 
85  if ($cnd['cnd_nuflg'] != '-1') {
86  $where .= " AND `j`.`not_use_flg` = '" . $this->esc($cnd['cnd_nuflg']) . "'";
87  }
88 
89  return $where;
90  }
91 
92  private function _getListCnt($where) {
93 
94  $sql = "SELECT DISTINCT `j`.`id`"
95  . " FROM `t_journal` `j`"
96  . " INNER JOIN `t_jslip` `s` ON `j`.`id` = `s`.`jid`"
97  . $where
98  ;
99  $rec = $this->getRecord($sql);
100  $cnt = (empty($rec)) ? 0 : count($rec);
101 
102  return $cnt;
103  }
104 
105  private function _getListDat($where, $cnt, $pager) {
106 
107  $pg = $this->getPaging($cnt, $pager['page'], $pager['rpp']);
108 
109  if ($cnt < 0) {
110  $rec = [];
111  } else {
112  $sql = "SELECT DISTINCT"
113  . " `j`.`id`" . "AS id"
114  . ", `j`.`scd`" . "AS scd"
115  . ", `j`.`ymd`" . "AS ymd"
116  . ", `j`.`settled_flg`" . "AS settled_flg"
117  . ", `j`.`not_use_flg`" . "AS not_use_flg"
118  . " FROM `t_journal` `j`"
119  . " INNER JOIN `t_jslip` `s` ON `j`.`id` = `s`.`jid`"
120  . $where
121  . " ORDER BY `ymd`, `id`"
122  . " LIMIT " . $pg['ofst'] . ", " . $pg['rpp']
123  ;
124  $jod = $this->getRecord($sql);
125  $jsd = [];
126  foreach ($jod as $j) {
127  $sql = "SELECT"
128  . " `id`"
129  . ", `line`"
130  . ", `debit`"
131  . ", `credit`"
132  . ", `amount`"
133  . ", `remark`"
134  . " FROM `t_jslip`"
135  . " WHERE `jid` = '" . $j['id'] . "'"
136  . " ORDER BY `line`"
137  ;
138  $rec = $this->getRecord($sql);
139 
140  foreach ($rec as $d) {
141  $jsd[$j['id']][$d['id']] = [
142  'line' => $d['line'],
143  'debit' => $d['debit'],
144  'credit' => $d['credit'],
145  'amount' => $d['amount'],
146  'remark' => $d['remark'],
147  ];
148  }
149  }
150  }
151 
152  return [
153  'cnt' => $pg['cnt'],
154  'rpp' => $pg['rpp'],
155  'last' => $pg['last'],
156  'page' => $pg['page'],
157  'rec' => ['journal' => $jod, 'slip' => $jsd],
158  ];
159  }
160 
161  public function getData($id) {
162 
163  $this->connect();
164 
165  $sql = "SELECT * FROM `t_journal` WHERE `id` = '" . $this->esc($id) . "'";
166  $jod = $this->getRecord($sql);
167 
168  if (empty($jod[0])) {
169  $this->close();
170  return [];
171  }
172 
173  $sql = "SELECT * FROM `t_jslip`"
174  . " WHERE `jid` = '" . $this->esc($id) . "'"
175  . " ORDER BY `line`"
176  ;
177  $rec = $this->getRecord($sql);
178 
179  $this->close();
180 
181  return (empty($rec[0])) ? [] : ['journal' => $jod[0], 'slip' => $rec];
182  }
183 
184  public function regist($param) {
185 
186  $err = '';
187 
188  $bid = $param['bid'];
189  $arg = $param['arg'];
190  $jid = $arg['jid'];
191 
192  $this->connect();
193  $this->begin();
194 
195  try {
196 
197  $sql = "DELETE FROM `t_jslip` WHERE `jid` = '" . $this->esc($jid) . "'";
198  $ans = $this->query($sql);
199 
200  $sql = "UPDATE `t_journal` SET"
201  . " `bid` = '" . $this->esc($bid) . "'"
202  . ", `scd` = '" . $this->esc($arg['scd']) . "'"
203  . ", `ymd` = '" . $this->esc($arg['ymd']) . "'"
204  . ", `settled_flg` = '" . $this->esc($arg['settled_flg']) . "'"
205  . ", `not_use_flg` = '" . $this->esc($arg['not_use_flg']) . "'"
206  . ", `update_person` = '" . $_SESSION['minfo']['mid'] . "'"
207  . " WHERE `id` = '" . $this->esc($arg['jid']) . "'"
208  ;
209  $ans = $this->query($sql);
210  $n = 0;
211  $kcd = [];
212  foreach ($arg['dat'] as $k => $d) {
213 
214  if ($d['deb_name'] == -1 && $d['cre_name'] == -1) {
215  continue;
216  }
217 
218  $kcd[] = $d['deb_name'];
219  $kcd[] = $d['cre_name'];
220 
221  $amt = ($d['deb_amount'] > 0) ? $d['deb_amount'] : $d['cre_amount'];
222  $amt = str_replace(',', '', $amt);
223  $n++;
224  $sql = "INSERT INTO `t_jslip`"
225  . " (`jid`, `line`, `debit`, `credit`, `amount`, `remark`, `update_person`)"
226  . " VALUES"
227  . " ('" . $this->esc($jid) . "'"
228  . ", '" . $this->esc($n) . "'"
229  . ", '" . $this->esc($d['deb_name']) . "'"
230  . ", '" . $this->esc($d['cre_name']) . "'"
231  . ", '" . $this->esc($amt) . "'"
232  . ", '" . $this->esc($d['remark']) . "'"
233  . ", '" . $_SESSION['minfo']['mid']. "'"
234  . ")"
235  ;
236  $ans = $this->query($sql);
237  }
238 
239  foreach ($kcd as $d) {
240  $sql = "UPDATE `t_item` SET"
241  . " `dummy` = '" . date('YmdJis') . "'"
242  . " WHERE `bid` = '" . $this->esc($bid) . "'"
243  . " AND `kcd` = '" . $this->esc($d) . "'"
244  ;
245  $ans = $this->query($sql);
246  }
247 
248  } catch(Exception $e) {
249  $err = $e->getMessage();
250  }
251 
252  if (empty($err)) {
253  $this->commit();
254  } else {
255  $this->rollback();
256  }
257 
258  $this->close();
259 
260  return $err;
261  }
262 
263  public function insert($param) {
264 
265  $err = '';
266 
267  $bid = $param['bid'];
268  $arg = $param['arg'];
269 
270  $this->connect();
271  $this->begin();
272 
273  try {
274 
275  $sql = "INSERT INTO `t_journal`"
276  . " (`bid`, `scd`, `ymd`, `settled_flg`, `not_use_flg`, `update_person`)"
277  . " VALUES"
278  . " ('" . $this->esc($bid) . "'"
279  . ", '" . $this->esc($arg['scd']) . "'"
280  . ", '" . $this->esc($arg['ymd']) . "'"
281  . ", " . $this->esc($arg['settled_flg'])
282  . ", " . $this->esc($arg['not_use_flg'])
283  . ", '" . $_SESSION['minfo']['mid']. "'"
284  . ")"
285  ;
286  $ans = $this->query($sql);
287  $jid = $this->insert_id();
288  $n = 0;
289  $kcd = [];
290  foreach ($arg['dat'] as $k => $d) {
291 
292  if ($d['deb_name'] == -1 && $d['cre_name'] == -1) {
293  continue;
294  }
295 
296  $kcd[] = $d['deb_name'];
297  $kcd[] = $d['cre_name'];
298 
299  $amt = ($d['deb_amount'] > 0) ? $d['deb_amount'] : $d['cre_amount'];
300  $amt = str_replace(',', '', $amt);
301  $n++;
302  $sql = "INSERT INTO `t_jslip`"
303  . " (`jid`, `line`, `debit`, `credit`, `amount`, `remark`, `update_person`)"
304  . " VALUES"
305  . " ('" . $this->esc($jid) . "'"
306  . ", '" . $this->esc($n) . "'"
307  . ", '" . $this->esc($d['deb_name']) . "'"
308  . ", '" . $this->esc($d['cre_name']) . "'"
309  . ", '" . $this->esc($amt) . "'"
310  . ", '" . $this->esc($d['remark']) . "'"
311  . ", '" . $_SESSION['minfo']['mid']. "'"
312  . ")"
313  ;
314  $ans = $this->query($sql);
315  }
316 
317  foreach ($kcd as $d) {
318  $sql = "UPDATE `t_item` SET"
319  . " `dummy` = '" . date('YmdJis') . "'"
320  . " WHERE `bid` = '" . $this->esc($bid) . "'"
321  . " AND `kcd` = '" . $this->esc($d) . "'"
322  ;
323  $ans = $this->query($sql);
324  }
325 
326  } catch(Exception $e) {
327  $err = $e->getMessage();
328  }
329 
330  if (empty($err)) {
331  $this->commit();
332  } else {
333  $this->rollback();
334  }
335 
336  $this->close();
337 
338  return $err;
339  }
340 
341  public function delete($param) {
342 
343  $err = '';
344  $dno = $param['dno'];
345 
346  $this->connect();
347  $this->begin();
348 
349  try {
350 
351  $sql = "DELETE FROM `t_jslip` WHERE `jid` = '" . $this->esc($dno) . "'";
352  $ans = $this->query($sql);
353 
354  $sql = "DELETE FROM `t_journal` WHERE `id` = '" . $this->esc($dno) . "'";
355  $ans = $this->query($sql);
356 
357  } catch(Exception $e) {
358  $err = $e->getMessage();
359  }
360 
361  if (empty($err)) {
362  $this->commit();
363  } else {
364  $this->rollback();
365  }
366 
367  $this->close();
368 
369  return $err;
370  }
371 
372  public function getRound($id) {
373 
374  $this->connect();
375 
376  $sql = "SELECT `r`.*"
377  . " FROM `t_basic` `b`"
378  . " INNER JOIN `c_round` `r` ON `b`.`round` = `r`.`c0`"
379  . " WHERE `b`.`id` = '" . $this->esc($id) . "'"
380  ;
381  $rec = $this->getRecord($sql);
382 
383  $this->close();
384 
385  return (empty($rec[0])) ? [] : $rec[0];
386  }
387 
388  public function getTax() {
389 
390  $this->connect();
391 
392  $sql = "SELECT `rate`, `name` FROM `t_tax` WHERE `bid` = '1' AND `valid_flg` IS TRUE ORDER BY `rate`";
393  $rec = $this->getRecord($sql);
394 
395  $this->close();
396 
397  return $rec;
398  }
399 }
UserJournalModel\insert
insert($param)
Definition: UserJournalModel.php:263
Model\connect
connect()
Definition: Model.php:12
Model\begin
begin()
Definition: Model.php:31
UserJournalModel\__construct
__construct($bid)
Definition: UserJournalModel.php:14
Model\query
query($sql)
Definition: Model.php:47
Model\getRecord
getRecord($sql)
Definition: Model.php:55
UserJournalModel\getList
getList($cnd)
Definition: UserJournalModel.php:41
UserJournalModel\getRound
getRound($id)
Definition: UserJournalModel.php:372
UserJournalModel
Definition: UserJournalModel.php:10
UserJournalModel\$bid
$bid
Definition: UserJournalModel.php:12
UserJournalModel\getLimit
getLimit()
Definition: UserJournalModel.php:18
Model\insert_id
insert_id()
Definition: Model.php:51
UserJournalModel\_getListDat
_getListDat($where, $cnt, $pager)
Definition: UserJournalModel.php:105
Model
Definition: Model.php:8
Model\commit
commit()
Definition: Model.php:35
Model\close
close()
Definition: Model.php:27
Model\esc
esc($str)
Definition: Model.php:43
Model\getPaging
getPaging($cnt, $page, $rpp)
Definition: Model.php:69
UserJournalModel\getData
getData($id)
Definition: UserJournalModel.php:161
UserJournalModel\regist
regist($param)
Definition: UserJournalModel.php:184
MAX_SLIP
const MAX_SLIP
Definition: local.php:12
UserJournalModel\getTax
getTax()
Definition: UserJournalModel.php:388
$cnt
$cnt
Definition: tex_tmplt_bs.php:319
Model\rollback
rollback()
Definition: Model.php:39
UserJournalModel\_getListWhere
_getListWhere($cnd)
Definition: UserJournalModel.php:54
UserJournalModel\_getListCnt
_getListCnt($where)
Definition: UserJournalModel.php:92