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