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