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