JSlip  1.0
tex_tmplt_slip.php
Go to the documentation of this file.
1 <?php
8 define('MAX_LINES_PAR_PAGE', 40);
9 
10 class tex_tmplt
11 {
12  private $csvfile;
13  private $dat;
14 
15  // for amount
16  private function amount($x)
17  {
18  return ($x == 0) ? "" : "{\\tt{" . str_replace("-", "▲", number_format($x)) . "}}";
19  }
20 
21  // yyyy/mm/dd
22  private function yyyymmdd($x)
23  {
24  $y = explode(" ", $x);
25  $z = explode("-", $y[0]);
26 
27  return $z[0]."/" . $z[1] . "/" . $z[2];
28  }
29 
30  // calculation
31  private function sum($id, $n)
32  {
33  $s[0] = 0;
34  $s[1] = "合計";
35  $s[2] = 0;
36 
37  $d = $this->dat["data"];
38 
39  $i = $n;
40  while (true) {
41  if (!isset($d[$i]["id"])) {
42  break;
43  }
44 
45  if ($id != $d[$i]["id"]) {
46  break;
47  }
48 
49  $s[0] += $d[$i]["debit_amount"];
50  $s[2] += $d[$i]["credit_amount"];
51  $i--;
52  }
53 
54  return $s;
55  }
56 
57  // TABセパレータCSVファイルからデータを取得し$datにデータを設定します。
58  private function set_dat()
59  {
60  $csv = file($this->csvfile); // TABセパレータCSVファイル・データ読み込み
61  $data_n = 0;
62  $cnt = count($csv);
63  for ($i = 0; $i < $cnt; $i++) {
64  $rec = explode("\t", $csv[$i]);
65  switch ($rec[0]) {
66  case "title":
67  case "name":
68  case "era":
69  case "rows":
70  $this->dat[$rec[0]] = trim($rec[1]);
71  break;
72  case "field":
73  $this->dat[$rec[0]]["n"] = trim($rec[1]);
74  $this->dat[$rec[0]]["id"] = trim($rec[2]);
75  $this->dat[$rec[0]]["scd"] = trim($rec[3]);
76  $this->dat[$rec[0]]["name"] = trim($rec[4]);
77  $this->dat[$rec[0]]["ymd"] = trim($rec[5]);
78  $this->dat[$rec[0]]["line"] = trim($rec[6]);
79  $this->dat[$rec[0]]["debit"] = trim($rec[7]);
80  $this->dat[$rec[0]]["credit"] = trim($rec[8]);
81  $this->dat[$rec[0]]["debit_name"] = trim($rec[9]);
82  $this->dat[$rec[0]]["credit_name"] = trim($rec[10]);
83  $this->dat[$rec[0]]["debit_account"] = trim($rec[11]);
84  $this->dat[$rec[0]]["credit_account"] = trim($rec[12]);
85  $this->dat[$rec[0]]["debit_amount"] = trim($rec[13]);
86  $this->dat[$rec[0]]["credit_amount"] = trim($rec[14]);
87  $this->dat[$rec[0]]["amount"] = trim($rec[15]);
88  $this->dat[$rec[0]]["remarks"] = trim($rec[16]);
89  $this->dat[$rec[0]]["settled_flg"] = trim($rec[17]);
90  break;
91  case "data":
92  $this->dat[$rec[0]][$data_n]["n"] = trim($rec[1]);
93  $this->dat[$rec[0]][$data_n]["id"] = trim($rec[2]);
94  $this->dat[$rec[0]][$data_n]["scd"] = trim($rec[3]);
95  $this->dat[$rec[0]][$data_n]["name"] = trim($rec[4]);
96  $this->dat[$rec[0]][$data_n]["ymd"] = trim($rec[5]);
97  $this->dat[$rec[0]][$data_n]["line"] = trim($rec[6]);
98  $this->dat[$rec[0]][$data_n]["debit"] = trim($rec[7]);
99  $this->dat[$rec[0]][$data_n]["credit"] = trim($rec[8]);
100  $this->dat[$rec[0]][$data_n]["debit_name"] = trim($rec[9]);
101  $this->dat[$rec[0]][$data_n]["credit_name"] = trim($rec[10]);
102  $this->dat[$rec[0]][$data_n]["debit_account"] = trim($rec[11]);
103  $this->dat[$rec[0]][$data_n]["credit_account"] = trim($rec[12]);
104  $this->dat[$rec[0]][$data_n]["debit_amount"] = trim($rec[13]);
105  $this->dat[$rec[0]][$data_n]["credit_amount"] = trim($rec[14]);
106  $this->dat[$rec[0]][$data_n]["amount"] = trim($rec[15]);
107  $this->dat[$rec[0]][$data_n]["remark"] = trim($rec[16]);
108  $this->dat[$rec[0]][$data_n]["settled_flg"] = trim($rec[17]);
109  $data_n++;
110  break;
111  }
112  }
113  }
114 
115  // ヘッダ
116  private function header($x, $n)
117  {
118  $this->dat["seq"]++;
119  echo "\\multicolumn{5}{c}{\\makebox[7.2cm][c]{}} \\\\\n";
120  $x++;
121 
122  echo "\\makebox[2.2cm][l]{\\tt{" . sprintf("%06d", $this->dat["seq"]) . "}} & ";
123  echo "\\makebox[2.2cm][l]{" . $this->yyyymmdd($this->dat["data"][$n]["ymd"]) . "} & ";
124  echo "\\multicolumn{2}{l}{\\makebox[7.2cm][l]{}} & ";
125  echo "\\makebox[2.2cm][r]{} \\\\\n";
126  echo "\\Hline\n";
127  $x++;
128  echo "\\makebox[2.2cm][c]{\\bf{金額}} & ";
129  echo "\\makebox[2.2cm][c]{\\bf{借方科目}} & ";
130  echo "\\makebox[5.0cm][c]{\\bf{摘  要}} & ";
131  echo "\\makebox[2.2cm][c]{\\bf{貸方科目}} & ";
132  echo "\\makebox[2.2cm][c]{\\bf{金額}} \\\\\n";
133  echo "\\Hline\n";
134  $x++;
135 
136  return $x;
137  }
138 
139  // フッタ
140  private function footer($x, $id, $n)
141  {
142  $s = $this->sum($id, $n);
143  $s[0] = ($s[0] == 0) ? "" : $this->amount($s[0]);
144  $s[2] = ($s[2] == 0) ? "" : $this->amount($s[2]);
145 
146  echo "\\makebox[2.2cm][r]{" . $s[0] . "} & ";
147  echo "\\multicolumn{3}{c}{\\makebox[9.4cm][c]{\\bf{" . $s[1] . "}}} & ";
148  echo "\\makebox[2.2cm][r]{" . $s[2] . "} \\\\\n";
149  echo "\\Hline\n";
150  $x++;
151 
152  return $x;
153  }
154 
155  // 主処理
156  public function main()
157  {
158  $this->set_dat();
159  $this->dat["seq"] = 0;
160  $this->dat["pre_id"] = -1;
161  }
162 
163  // make a page
164  public function make_a_page($n, $cnt)
165  {
166  $max = MAX_LINES_PAR_PAGE;
167  $x = 0;
168  $d = $this->dat["data"];
169  $title = $this->dat["title"];
170  $name = $this->dat["name"];
171  $era = $this->dat["era"];
172 
173  echo "\\begin{center}\n";
174 
175  echo "\\begin{tabular}{ccccc}\n";
176  echo "\\multicolumn{4}{l}{\\makebox[12.5cm][l]{" . $name . "}} & ";
177  echo "\\makebox[2.5cm][r]{\\tt{" . $era . "年度}} \\\\\n";
178  $x++;
179  echo "\\multicolumn{5}{c}{\\makebox[15.0cm][c]{\\Large\\bf{" . $title . "}}} \\\\\n";
180  $x++;
181  echo "\\multicolumn{5}{l}{\\makebox[15.0cm][c]{}} \\\\\n";
182  $x++;
183 
184  $start = $x;
185  $id = $this->dat["pre_id"];
186  while ($n < $cnt && $x < $max) {
187  if ($id != $d[$n]["id"]) {
188  if ($x != $start) {
189  $x = $this->footer($x, $id, $n - 1);
190  }
191  $x = $this->header($x, $n);
192  }
193 
194  $id = $d[$n]["id"];
195 
196  $dn = $d[$n]["debit_name"];
197  $cn = $d[$n]["credit_name"];
198  $da = $d[$n]["debit_amount"];
199  $ca = $d[$n]["credit_amount"];
200  $itm = $d[$n]["remark"];
201 
202  $da = ($da == 0) ? "" : $this->amount($da);
203  $ca = ($ca == 0) ? "" : $this->amount($ca);
204 
205  echo "\\makebox[2.2cm][r]{" . $da . "} & ";
206  echo "\\makebox[2.2cm][c]{" . $dn . "} & ";
207  echo "\\makebox[5.0cm][c]{" . $itm . "} & ";
208  echo "\\makebox[2.2cm][c]{" . $cn . "} & ";
209  echo "\\makebox[2.2cm][r]{" . $ca . "} \\\\\n";
210  echo "\\hline\n";
211 
212  $x++;
213  $n++;
214  }
215 
216  if ($n == $cnt) {
217  $x = $this->footer($x, $id, $n - 1);
218  echo "\\end{tabular}\n";
219  echo "\\end{center}\n";
220  } else {
221  if ($id != $d[$n]["id"]) { // 先読み
222  $x = $this->footer($x, $id, $n - 1);
223  }
224  echo "\\end{tabular}\n";
225  echo "\\end{center}\n";
226  echo "\\newpage\n";
227  }
228 
229  $this->dat["pre_id"] = $id;
230 
231  return $n;
232  }
233 
234  // check data for debug
235  public function chk_dat()
236  {
237  echo "title = " . $this->dat["title"] . "\n\n";
238  echo "name = " . $this->dat["name"] . "\n\n";
239  echo "era = " . $this->dat["era"] . "\n\n";
240  echo "rows = " . $this->dat["rows"] . "\n\n";
241 
242  echo $this->dat["field"]["n"] . ", ".
243  $this->dat["field"]["id"] . ", ".
244  $this->dat["field"]["scd"] . ", ".
245  $this->dat["field"]["name"] . ", ".
246  $this->dat["field"]["ymd"] . ", ".
247  $this->dat["field"]["line"] . ", ".
248  $this->dat["field"]["debit"] . ", ".
249  $this->dat["field"]["credit"] . ", ".
250  $this->dat["field"]["debit_name"] . ", ".
251  $this->dat["field"]["credit_name"] . ", ".
252  $this->dat["field"]["debit_account"] . ", ".
253  $this->dat["field"]["credit_account"] . ", ".
254  $this->dat["field"]["debit_amount"] . ", ".
255  $this->dat["field"]["credit_amount"] . ", ".
256  $this->dat["field"]["amount"] . ", ".
257  $this->dat["field"]["remark"] . ", ".
258  $this->dat["field"]["settled_flg"] . "\n\n";
259 
260  $cnt = $this->dat["rows"];
261  for ($i = 0; $i < $cnt; $i++) {
262  echo $this->dat["data"][$i]["n"] . ", ".
263  $this->dat["data"][$i]["id"] . ", ".
264  $this->dat["data"][$i]["scd"] . ", ".
265  $this->dat["data"][$i]["name"] . ", ".
266  $this->dat["data"][$i]["ymd"] . ", ".
267  $this->dat["data"][$i]["line"] . ", ".
268  $this->dat["data"][$i]["debit"] . ", ".
269  $this->dat["data"][$i]["credit"] . ", ".
270  $this->dat["data"][$i]["debit_name"] . ", ".
271  $this->dat["data"][$i]["credit_name"] . ", ".
272  $this->dat["data"][$i]["debit_account"] . ", ".
273  $this->dat["data"][$i]["credit_account"] . ", ".
274  $this->dat["data"][$i]["debit_amount"] . ", ".
275  $this->dat["data"][$i]["credit_amount"] . ", ".
276  $this->dat["data"][$i]["amount"] . ", ".
277  $this->dat["data"][$i]["remark"] . ", ".
278  $this->dat["data"][$i]["settled_flg"] . "\n\n";
279  }
280  }
281 
282  // 表示データ取得
283  public function get_dat()
284  {
285  return $this->dat;
286  }
287 
288  // 明示的コンストラクタ
289  public function __construct($filename)
290  {
291  $this->csvfile = $filename;
292  $this->dat = array();
293  }
294 }
295 
296 $my = new tex_tmplt($argv[1]);
297 $my->main();
298 $dat = $my->get_dat();
299 ?>
300 \documentclass[a4j]{jarticle}
301 
302 \usepackage{supertabular}
303 \usepackage{multirow}
304 
305 \pagestyle{plain}
306 
307 \topmargin -25mm
308 \oddsidemargin 0mm
309 \evensidemargin 0mm
310 \textheight 260mm
311 \textwidth 160mm
312 \parindent 1zw
313 \parskip 0.5zw
314 
315 % 太い罫線のために
316 \def\Hline{\noalign{\hrule height .5mm}}
317 \def\Vline{\vrule width .5mm}
318 
319 \begin{document}
320 
321 <?php
322 if (0) {
323  $my->chk_dat();
324 } else {
325  $n = 0;
326  $cnt = $dat["rows"];
327  while ($n < $cnt)
328  $n = $my->make_a_page($n, $cnt);
329 }
330 ?>
331 
332 \end{document}
tex_tmplt\header
header($x, $n)
Definition: tex_tmplt_slip.php:116
$cnt
$cnt
Definition: tex_tmplt_slip.php:326
tex_tmplt\make_a_page
make_a_page($n, $cnt)
Definition: tex_tmplt_slip.php:164
tex_tmplt\footer
footer($x, $id, $n)
Definition: tex_tmplt_slip.php:140
tex_tmplt\get_dat
get_dat()
Definition: tex_tmplt_slip.php:283
tex_tmplt\chk_dat
chk_dat()
Definition: tex_tmplt_slip.php:235
tex_tmplt\$dat
$dat
Definition: tex_tmplt_bs.php:13
tex_tmplt\sum
sum($mm)
Definition: tex_tmplt_bs.php:60
tex_tmplt\$csvfile
$csvfile
Definition: tex_tmplt_bs.php:12
$dat
$dat
Definition: tex_tmplt_slip.php:298
tex_tmplt\yyyymmdd
yyyymmdd($x)
Definition: tex_tmplt_slip.php:22
tex_tmplt\amount
amount($x)
Definition: tex_tmplt_slip.php:16
tex_tmplt\main
main()
Definition: tex_tmplt_slip.php:156
tex_tmplt\sum
sum($id, $n)
Definition: tex_tmplt_slip.php:31
tex_tmplt\__construct
__construct($filename)
Definition: tex_tmplt_slip.php:289
MAX_LINES_PAR_PAGE
const MAX_LINES_PAR_PAGE
Definition: tex_tmplt_slip.php:8
tex_tmplt\set_dat
set_dat()
Definition: tex_tmplt_slip.php:58
tex_tmplt
Definition: tex_tmplt_bs.php:10
$my
$my
Definition: tex_tmplt_slip.php:296