JSlip  1.0
tex_tmplt_tb_detail.php
Go to the documentation of this file.
1 <?php
8 define('MAX_LINES_PAR_PAGE', 40);
9 
10 class ledger_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  // 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 
42  for ($i = 0; $i < $cnt; $i++) {
43  if ($mm == $d[$i]["mm"]) {
44  $x++;
45  }
46  }
47 
48  $p = intval($x / MAX_LINES_PAR_PAGE);
49  $y = $x % MAX_LINES_PAR_PAGE;
50 
51  if ($y) {
52  $p++;
53  }
54 
55  $z = intval(($n % $x) / MAX_LINES_PAR_PAGE) + 1;
56  $a = array();
57  $a[0] = $z;
58  $a[1] = $p;
59 
60  return $a;
61  }
62 
63  // calculation
64  private function sum($mm)
65  {
66  $s[0] = 0;
67  $s[1] = 0;
68  $s[2] = "合計";
69  $s[3] = 0;
70  $s[4] = 0;
71 
72  $cnt = $this->dat["rows"];
73  $d = $this->dat["data"];
74 
75  for ($i = 0; $i < $cnt; $i++) {
76  if ($mm == $d[$i]["mm"]) {
77  $s[0] += $d[$i]["debit_remain"];
78  $s[1] += $d[$i]["debit_sum"];
79  $s[3] += $d[$i]["credit_sum"];
80  $s[4] += $d[$i]["credit_remain"];
81  }
82  }
83 
84  return $s;
85  }
86 
87  // TABセパレータCSVファイルからデータを取得し$datにデータを設定します。
88  private function set_dat()
89  {
90  $csv = file($this->csvfile); // TABセパレータCSVファイル・データ読み込み
91  $data_n = 0;
92  $cnt = count($csv);
93  for ($i = 0; $i < $cnt; $i++) {
94  $rec = explode("\t", $csv[$i]);
95  switch ($rec[0]) {
96  case "title":
97  case "name":
98  case "era":
99  case "bymd":
100  case "rows":
101  $this->dat[$rec[0]] = trim($rec[1]);
102  break;
103  case "field":
104  $this->dat[$rec[0]]["n"] = trim($rec[1]);
105  $this->dat[$rec[0]]["m"] = trim($rec[2]);
106  $this->dat[$rec[0]]["mm"] = trim($rec[3]);
107  $this->dat[$rec[0]]["ctg_div"] = trim($rec[4]);
108  $this->dat[$rec[0]]["division"] = trim($rec[5]);
109  $this->dat[$rec[0]]["item"] = trim($rec[6]);
110  $this->dat[$rec[0]]["debit_remain"] = trim($rec[7]);
111  $this->dat[$rec[0]]["debit_sum"] = trim($rec[8]);
112  $this->dat[$rec[0]]["name"] = trim($rec[9]);
113  $this->dat[$rec[0]]["credit_sum"] = trim($rec[10]);
114  $this->dat[$rec[0]]["credit_remain"] = trim($rec[11]);
115  break;
116  case "data":
117  $this->dat[$rec[0]][$data_n]["n"] = trim($rec[1]);
118  $this->dat[$rec[0]][$data_n]["m"] = trim($rec[2]);
119  $this->dat[$rec[0]][$data_n]["mm"] = trim($rec[3]);
120  $this->dat[$rec[0]][$data_n]["ctg_div"] = trim($rec[4]);
121  $this->dat[$rec[0]][$data_n]["division"] = trim($rec[5]);
122  $this->dat[$rec[0]][$data_n]["item"] = trim($rec[6]);
123  $this->dat[$rec[0]][$data_n]["debit_remain"] = trim($rec[7]);
124  $this->dat[$rec[0]][$data_n]["debit_sum"] = trim($rec[8]);
125  $this->dat[$rec[0]][$data_n]["name"] = trim($rec[9]);
126  $this->dat[$rec[0]][$data_n]["credit_sum"] = trim($rec[10]);
127  $this->dat[$rec[0]][$data_n]["credit_remain"] = trim($rec[11]);
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  if ($n >= $max) {
190  $flg = 0;
191  break;
192  }
193 
194  if (!isset($d[$n]["mm"])) {
195  $flg = 1;
196  break;
197  } else if ($month != $d[$n]["mm"]) {
198  $flg = 1;
199  break;
200  }
201 
202  $dr = $d[$n]["debit_remain"];
203  $ds = $d[$n]["debit_sum"];
204  $nam = $d[$n]["name"];
205  $cs = $d[$n]["credit_sum"];
206  $cr = $d[$n]["credit_remain"];
207 
208  $dr = ($dr == 0) ? "" : $this->amount($dr);
209  $ds = ($ds == 0) ? "" : $this->amount($ds);
210  $cs = ($cs == 0) ? "" : $this->amount($cs);
211  $cr = ($cr == 0) ? "" : $this->amount($cr);
212 
213  echo "\\makebox[2.2cm][r]{" . $dr . "} & ";
214  echo "\\makebox[2.2cm][r]{" . $ds . "} & ";
215  echo "\\makebox[5.0cm][c]{" . $nam . "} & ";
216  echo "\\makebox[2.2cm][r]{" . $cs . "} & ";
217  echo "\\makebox[2.2cm][r]{" . $cr . "} \\\\\n";
218 
219  $n++;
220  }
221 
222  if ($flg) {
223  $s = $this->sum($mm);
224 
225  $dr = $s[0];
226  $ds = $s[1];
227  $nam = $s[2];
228  $cs = $s[3];
229  $cr = $s[4];
230 
231  $dr = ($dr == 0) ? "" : $this->amount($dr);
232  $ds = ($ds == 0) ? "" : $this->amount($ds);
233  $nam = "{\\bf{" . $nam . "}}";
234  $cs = ($cs == 0) ? "" : $this->amount($cs);
235  $cr = ($cr == 0) ? "" : $this->amount($cr);
236 
237  echo "\\Hline\n";
238  echo "\\makebox[2.2cm][r]{" . $dr . "} & ";
239  echo "\\makebox[2.2cm][r]{" . $ds . "} & ";
240  echo "\\makebox[5.0cm][c]{" . $nam . "} & ";
241  echo "\\makebox[2.2cm][r]{" . $cs . "} & ";
242  echo "\\makebox[2.2cm][r]{" . $cr . "} \\\\\n";
243  }
244 
245  echo "\\Hline\n";
246  echo "\\end{tabular}\n";
247  echo "\\end{center}\n";
248  echo "\\newpage\n";
249 
250  return $n;
251  }
252 
253  // check data for debug
254  public function chk_dat()
255  {
256  echo "title = " .$this->dat["title"] . "\n\n";
257  echo "name = " .$this->dat["name"] . "\n\n";
258  echo "era = " .$this->dat["era"] . "\n\n";
259  echo "bymd = " .$this->dat["bymd"] . "\n\n";
260  echo "rows = " .$this->dat["rows"] . "\n\n";
261 
262  echo $this->dat["field"]["n"] . ", " .
263  $this->dat["field"]["m"] . ", " .
264  $this->dat["field"]["mm"] . ", " .
265  $this->dat["field"]["ctg_div"] . ", " .
266  $this->dat["field"]["division"] . ", " .
267  $this->dat["field"]["item"] . ", " .
268  $this->dat["field"]["debit_remain"] . ", " .
269  $this->dat["field"]["debit_sum"] . ", " .
270  $this->dat["field"]["name"] . ", " .
271  $this->dat["field"]["credit_sum"] . ", " .
272  $this->dat["field"]["credit_remain"] . "\n\n";
273 
274  $cnt = $this->dat["rows"];
275  for ($i = 0; $i < $cnt; $i++) {
276  echo $this->dat["data"][$i]["n"] . ", " .
277  $this->dat["data"][$i]["m"] . ", " .
278  $this->dat["data"][$i]["mm"] . ", " .
279  $this->dat["data"][$i]["ctg_div"] . ", " .
280  $this->dat["data"][$i]["division"] . ", " .
281  $this->dat["data"][$i]["item"] . ", " .
282  $this->dat["data"][$i]["debit_remain"] . ", " .
283  $this->dat["data"][$i]["debit_sum"] . ", " .
284  $this->dat["data"][$i]["name"] . ", " .
285  $this->dat["data"][$i]["credit_sum"] . ", " .
286  $this->dat["data"][$i]["credit_remain"] . "\n\n";
287  }
288  }
289 
290  // 表示データ取得
291  public function get_dat()
292  {
293  return $this->dat;
294  }
295 
296  // 明示的コンストラクタ
297  public function __construct($filename)
298  {
299  $this->csvfile = $filename;
300  $this->dat = array();
301  }
302 }
303 
304 $my = new ledger_tmplt($argv[1]);
305 $my->main();
306 $dat = $my->get_dat();
307 ?>
308 \documentclass[a4j]{jarticle}
309 
310 \usepackage{supertabular}
311 \usepackage{multirow}
312 
313 \pagestyle{plain}
314 
315 \topmargin -25mm
316 \oddsidemargin 0mm
317 \evensidemargin 0mm
318 \textheight 260mm
319 \textwidth 160mm
320 \parindent 1zw
321 \parskip 0.5zw
322 
323 % 太い罫線のために
324 \def\Hline{\noalign{\hrule height .5mm}}
325 \def\Vline{\vrule width .5mm}
326 
327 \begin{document}
328 
329 <?php
330 if (0) {
331  $my->chk_dat();
332 } else {
333  $n = 0;
334  $cnt = $dat["rows"];
335  while ($n < $cnt)
336  $n = $my->make_a_page($n);
337 }
338 ?>
339 
340 \end{document}
ledger_tmplt\term
term($bymd, $y, $m)
Definition: tex_tmplt_tb_detail.php:22
ledger_tmplt\sum
sum($mm)
Definition: tex_tmplt_tb_detail.php:64
ledger_tmplt\main
main()
Definition: tex_tmplt_tb_detail.php:135
ledger_tmplt\chk_dat
chk_dat()
Definition: tex_tmplt_tb_detail.php:254
ledger_tmplt\$csvfile
$csvfile
Definition: tex_tmplt_tb.php:12
ledger_tmplt\make_a_page
make_a_page($n)
Definition: tex_tmplt_tb_detail.php:141
ledger_tmplt\pages
pages($n, $mm)
Definition: tex_tmplt_tb_detail.php:36
ledger_tmplt\set_dat
set_dat()
Definition: tex_tmplt_tb_detail.php:88
ledger_tmplt\$dat
$dat
Definition: tex_tmplt_tb.php:13
$my
$my
Definition: tex_tmplt_tb_detail.php:304
$cnt
$cnt
Definition: tex_tmplt_tb_detail.php:334
MAX_LINES_PAR_PAGE
const MAX_LINES_PAR_PAGE
Definition: tex_tmplt_tb_detail.php:8
$dat
$dat
Definition: tex_tmplt_tb_detail.php:306
ledger_tmplt\__construct
__construct($filename)
Definition: tex_tmplt_tb_detail.php:297
ledger_tmplt\get_dat
get_dat()
Definition: tex_tmplt_tb_detail.php:291
ledger_tmplt
Definition: tex_tmplt_tb.php:10
ledger_tmplt\amount
amount($x)
Definition: tex_tmplt_tb_detail.php:16