JSlip  1.0
tex_tmplt_pl.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  // 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 
58  $a[0] = $z;
59  $a[1] = $p;
60 
61  return $a;
62  }
63 
64  // calculation
65  private function sum($mm)
66  {
67  $s[0] = 0;
68  $s[1] = 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  $r = $d[$i]["remain"];
76  if ($d[$i]["division"]) {
77  $s[1] += $r;
78  } else {
79  $s[0] += $r;
80  }
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]]["account_cd"] = trim($rec[4]);
108  $this->dat[$rec[0]]["name"] = trim($rec[5]);
109  $this->dat[$rec[0]]["remain"] = trim($rec[6]);
110  $this->dat[$rec[0]]["division"] = trim($rec[7]);
111  break;
112  case "data":
113  $this->dat[$rec[0]][$data_n]["n"] = trim($rec[1]);
114  $this->dat[$rec[0]][$data_n]["m"] = trim($rec[2]);
115  $this->dat[$rec[0]][$data_n]["mm"] = trim($rec[3]);
116  $this->dat[$rec[0]][$data_n]["account_cd"] = trim($rec[4]);
117  $this->dat[$rec[0]][$data_n]["name"] = trim($rec[5]);
118  $this->dat[$rec[0]][$data_n]["remain"] = trim($rec[6]);
119  $this->dat[$rec[0]][$data_n]["division"] = trim($rec[7]);
120  $data_n++;
121  break;
122  }
123  }
124  }
125 
126  // 主処理
127  public function main()
128  {
129  $this->set_dat();
130  }
131 
132  // make a page
133  public function make_a_page($n)
134  {
135  $d = $this->dat["data"];
136  $title = $this->dat["title"];
137  $name = $this->dat["name"];
138  $era = $this->dat["era"];
139  $bymd = $this->dat["bymd"];
140  $m = $d[$n]["m"];
141  $mm = $d[$n]["mm"];
142  $inam = $d[$n]["name"];
143 
144  $yyyy = intval($m / 100);
145  $term = $this->term($bymd, $yyyy, $mm);
146  $term0 = $term[0];
147  $term1 = $term[1];
148 
149  $p = $this->pages($n, $mm);
150  echo "\\begin{center}\n";
151  echo "\\begin{tabular}{ccc}\n";
152  echo "\\multicolumn{2}{l}{\\makebox[12.5cm][l]{" . $name . "}} & ";
153  echo "\\makebox[2.5cm][r]{\\tt{" . $era . "年度}} \\\\\n";
154  echo "\\makebox[2.5cm][l]{} & ";
155  echo "\\makebox[10cm][c]{\\bf\\LARGE{" . $title . "}} & ";
156  echo "\\makebox[2.5cm][r]{" . $mm . "月度} \\\\\n";
157  echo "\\makebox[2.5cm][l]{} & ";
158  echo "\\makebox[10cm][c]{{\\tt{" . $term0 . " 〜 " . $term1 . "}}} & ";
159  echo "\\makebox[2.5cm][r]{\\tt{" . $p[0] . "/" . $p[1] . "}} \\\\\n";
160  echo "\\end{tabular}\n";
161 
162  echo "\\begin{center}\n";
163  echo "\\begin{tabular}{@{\\Vline\\ }c|c|c|c@{\\ \\Vline}}\n";
164  echo "\\Hline\n";
165  echo "\\multicolumn{2}{@{\\Vline\\ }c|}{\\makebox[7.4cm][c]{\\bf{費 用}}} & ";
166  echo "\\multicolumn{2}{c@{\\ \\Vline}}{\\makebox[7.4cm][c]{\\bf{収 益}}} \\\\\n";
167  echo "\\Hline\n";
168 
169  $max = $n + MAX_LINES_PAR_PAGE;
170  $month = $mm;
171  $drec = array();
172  $n0 = 0;
173  $n1 = 0;
174  while (true) {
175  if ($n >= $max) {
176  $flg = 0;
177  break;
178  }
179 
180  if (!isset($d[$n]["mm"])) {
181  $flg = 1;
182  break;
183  } else if ($month != $d[$n]["mm"]) {
184  $flg = 1;
185  break;
186  }
187 
188  if ($d[$n]["division"]) {
189  $drec[$n1][1]["name"] = $d[$n]["name"];
190  $drec[$n1][1]["remain"] = $d[$n]["remain"];
191  $n1++;
192  } else {
193  $drec[$n0][0]["name"] = $d[$n]["name"];
194  $drec[$n0][0]["remain"] = $d[$n]["remain"];
195  $n0++;
196  }
197 
198  $n++;
199  }
200 
201  $nmax = max($n0, $n1);
202  for ($i = 0; $i < $nmax; $i++) {
203  if ($i < $n0) {
204  $dn = $drec[$i][0]["name"];
205  $dr = $this->amount($drec[$i][0]["remain"]);
206  } else {
207  $dn = "";
208  $dr = "";
209  }
210 
211  if ($i < $n1) {
212  $cn = $drec[$i][1]["name"];
213  $cr = $this->amount($drec[$i][1]["remain"]);
214  } else {
215  $cn = "";
216  $cr = "";
217  }
218 
219  echo "\\makebox[3.7cm][l]{" . $dn . "} & ";
220  echo "\\makebox[3.7cm][r]{" . $dr . "} & ";
221  echo "\\makebox[3.7cm][l]{" . $cn . "} & ";
222  echo "\\makebox[3.7cm][r]{" . $cr . "} \\\\\n";
223  }
224 
225  if ($flg) {
226  $s = $this->sum($mm);
227 
228  $dr = $s[0];
229  $cr = $s[1];
230 
231  $dr = ($dr == 0) ? "" : $this->amount($dr);
232  $cr = ($cr == 0) ? "" : $this->amount($cr);
233 
234  echo "\\Hline\n";
235  echo "\\makebox[3.7cm][c]{\bf{合 計}} & ";
236  echo "\\makebox[3.7cm][r]{" . $dr . "} & ";
237  echo "\\makebox[3.7cm][c]{\bf{合 計}} & ";
238  echo "\\makebox[3.7cm][r]{" . $cr . "} \\\\\n";
239  }
240 
241  echo "\\Hline\n";
242  echo "\\end{tabular}\n";
243  echo "\\end{center}\n";
244  echo "\\newpage\n";
245 
246  return $n;
247  }
248 
249  // check data for debug
250  public function chk_dat()
251  {
252  echo "title = " . $this->dat["title"] ."\n\n";
253  echo "name = " . $this->dat["name"] ."\n\n";
254  echo "era = " . $this->dat["era"] ."\n\n";
255  echo "bymd = " . $this->dat["bymd"] ."\n\n";
256  echo "rows = " . $this->dat["rows"] ."\n\n";
257 
258  echo $this->dat["field"]["n"] . ", " .
259  $this->dat["field"]["m"] . ", " .
260  $this->dat["field"]["mm"] . ", " .
261  $this->dat["field"]["account_cd"] . ", " .
262  $this->dat["field"]["name"] . ", " .
263  $this->dat["field"]["remain"] . ", " .
264  $this->dat["field"]["division"] . "\n\n";
265 
266  $cnt = $this->dat["rows"];
267  for ($i = 0; $i < $cnt; $i++) {
268  echo $this->dat["data"][$i]["n"] . ", " .
269  $this->dat["data"][$i]["m"] . ", " .
270  $this->dat["data"][$i]["mm"] . ", " .
271  $this->dat["data"][$i]["account_cd"] . ", " .
272  $this->dat["data"][$i]["name"] . ", " .
273  $this->dat["data"][$i]["remain"] . ", " .
274  $this->dat["data"][$i]["division"] . "\n\n";
275  }
276  }
277 
278  // 表示データ取得
279  public function get_dat()
280  {
281  return $this->dat;
282  }
283 
284  // 明示的コンストラクタ
285  public function __construct($filename)
286  {
287  $this->csvfile = $filename;
288  $this->dat = array();
289  }
290 }
291 
292 $my = new tex_tmplt($argv[1]);
293 $my->main();
294 $dat = $my->get_dat();
295 ?>
296 \documentclass[a4j]{jarticle}
297 
298 \usepackage{supertabular}
299 \usepackage{multirow}
300 
301 \pagestyle{plain}
302 
303 \topmargin -25mm
304 \oddsidemargin 0mm
305 \evensidemargin 0mm
306 \textheight 260mm
307 \textwidth 160mm
308 \parindent 1zw
309 \parskip 0.5zw
310 
311 % 太い罫線のために
312 \def\Hline{\noalign{\hrule height .5mm}}
313 \def\Vline{\vrule width .5mm}
314 
315 \begin{document}
316 
317 <?php
318 if (0) {
319  $my->chk_dat();
320 } else {
321  $n = 0;
322  $cnt = $dat["rows"];
323  while ($n < $cnt)
324  $n = $my->make_a_page($n);
325 }
326 ?>
327 
328 \end{document}
$cnt
$cnt
Definition: tex_tmplt_pl.php:322
MAX_LINES_PAR_PAGE
const MAX_LINES_PAR_PAGE
Definition: tex_tmplt_pl.php:8
$my
$my
Definition: tex_tmplt_pl.php:292
tex_tmplt\get_dat
get_dat()
Definition: tex_tmplt_pl.php:279
tex_tmplt\chk_dat
chk_dat()
Definition: tex_tmplt_pl.php:250
tex_tmplt\$dat
$dat
Definition: tex_tmplt_bs.php:13
$dat
$dat
Definition: tex_tmplt_pl.php:294
tex_tmplt\sum
sum($mm)
Definition: tex_tmplt_pl.php:65
tex_tmplt\term
term($bymd, $y, $m)
Definition: tex_tmplt_pl.php:22
tex_tmplt\$csvfile
$csvfile
Definition: tex_tmplt_bs.php:12
tex_tmplt\amount
amount($x)
Definition: tex_tmplt_pl.php:16
tex_tmplt\main
main()
Definition: tex_tmplt_pl.php:127
tex_tmplt\make_a_page
make_a_page($n)
Definition: tex_tmplt_pl.php:133
tex_tmplt\__construct
__construct($filename)
Definition: tex_tmplt_pl.php:285
tex_tmplt\set_dat
set_dat()
Definition: tex_tmplt_pl.php:88
tex_tmplt\pages
pages($n, $mm)
Definition: tex_tmplt_pl.php:36
tex_tmplt
Definition: tex_tmplt_bs.php:10