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