<%= title %>
<%= stylesheet '/css/printa4.css' %>
<%= content %>
![templates/rwt/print_main.html.ep]
% layout 'defrwt';
% title $title ;
<%
$self->sth($self->rwt_prepare($self->sql));
$self->sth->execute();
my $init_sw = 0;
while(my $ref = $self->rwt_dataget){
$self->rskip(0);
$self->endsw(0);
$self->rwt_tisel($ref);
if($self->endsw != 0 or $self->page_count > $self->end_page){
last;
}
next if($self->rskip == 1);
$self->rwt_data_save($ref) if($init_sw == 0);
$init_sw = 1;
$self->break_check($ref);
$self->rwt_dtail($ref);
$self->rwt_dtail_opt($ref);
$self->rwt_print_dt($ref);
$self->rwt_sumry($ref);
$self->rwt_sumcomp($ref);
$self->head_print(0);
%>
<%== $self->c_get() %>
<%
$self->rwt_data_save($ref);
}
$self->break_level(0);
$self->break_routine(0);
$self->footer(99);
%>
<%== $self->c_get() %>
![controller/Rwt.pm]
package Tool::mmt::Controller::Rwt;
use Mojo::Base 'Tool::mmt::Controller::Mmt';
use Mojo::Util qw(slurp);
use Encode qw/ decode decode_utf8 encode encode_utf8/;
has contents => "";
has line_count => 0;
has page_count => 0;
has page => 0;
has max_line => 37;
has restart_page => 1;
has end_page => 10000;
has rskip => 0;
has endsw => 0;
has break_level => -1;
has head_print => 0;
has head_line => sub {[]};
has line => sub {[]};
has sth => "";
has sql =>"";
has ref => sub {{}};
has oref => sub {{}};
has bottom_const => "
powered by perl-mojolicious
";
has column => 1;
has title => "";
has print_form => "rwt/print_main";
has item_list => sub {[]};
has lf_spec => sub {{}};
has break_ctl => sub {[
{
level => 99,
key => '項目名',
_bef => 0, # 前改行
_aft => 0, # 後改行
'項目1' => 0, # 0: sumry
'項目2' => '小計' # コンスタント
},
]};
my $line;
my $dbh;
#my $oref = "";
sub print_main {
my $s = shift;
$dbh = $s->app->model->webdb->dbh;
$s->rwt_init;
$s->_rwt_init;
#$s->stash->{contents} = $s->rwt_start();
$s->render($s->print_form);
}
sub rwt_init{
my $s = shift;
$s->item_list([]);
}
sub _rwt_init{
my $s = shift;
$s->stash->{title} = "rwt sample";
if($s->sql eq ""){
if($s->param('table') ne ''){
$s->sql(qq{select * from @{[$s->param('table')]}});
}else{
$s->sql("select * from M_KBN");
}
}
$s->sth($s->rwt_prepare($s->sql));
$s->title($s->title||$s->param('title')||$s->param('table')||'title');
$s->line_count(0);
$s->page_count(0);
# @{$s->item_list} = map { encode_utf8($_) } @{$s->item_list};
if(@{$s->item_list} == 0){
$s->item_list([@{$s->sth->{NAME}}]);
}
}
sub rwt_start{
my $s = shift;
$s->sth($s->rwt_prepare($s->sql));
$s->sth->execute();
while(my $ref = $s->rwt_dataget){
$s->rskip(0);
$s->endsw(0);
$s->rwt_tisel($ref);
if($s->endsw != 0 or $s->page_count > $s->end_page){
last;
}
next if($s->rskip == 1);
$s->rwt_data_save($ref) if($s->oref == "");
$s->break_check($ref);
$s->rwt_dtail($ref);
$s->rwt_dtail_opt($ref);
$s->rwt_print_dt($ref);
$s->rwt_sumry($ref);
$s->rwt_sumcomp($ref);
$s->head_print(0);
}
$s->break_level(99);
$s->break_routine(99);
$s->footer(99);
return $s->c_get();
}
sub _bottom_print{
my $s = shift;
$s->printout("");
$s->bottom_print();
$s->printout("");
}
sub bottom_print{
my $s = shift;
$s->printout($s->bottom_const) if($s->bottom_const);
}
sub break_check{
my $s = shift;
my $ref = shift;
my $level = 99;
if($s->line_count > $s->max_line or $s->line_count == 0){
$s->rwt_head_print();
}
my $oref = $s->oref;
my $i = -1;
for my $break_control (@{$s->break_ctl}){
$i++;
$level = $break_control->{level};
my $log = Mojo::Log->new();
$log->debug($break_control->{key}
. ":" . $ref->{$break_control->{key}}
. " old:" . $s->dumper ($oref->{$break_control->{key}})
);
if ($ref->{$break_control->{key}} ne
$oref->{$break_control->{key}}){
$s->break_routine($i);
last;
}
}
}
sub break_routine{
my $s = shift;
my $i = shift;
for (my $j = @{$s->break_ctl} - 1;$j>=$i;$j--){
$s->rwt_print_levelset($j);
$s->rwt_print_level($j);
}
}
sub rwt_print_levelset{
my $s = shift;
my $i = shift;
my $ctl = $s->break_ctl->[$i];
$line = "
";
for (@{$s->item_list}) {
if (exists($ctl->{$_})){
$line .= $s->set_lf_spec($ctl->{$_},$_);
if ($ctl->{$_} =~ m/^[0-9\-\+\.]+$/){
$ctl->{$_} = 0;
}
}else{
$line .= "
";
}
}
$line .= "
";
}
sub rwt_print_level{
my $s = shift;
my $i = shift;
$s->printout($line);
$s->line_count($s->line_count+1)
}
sub rwt_dtail_opt{
my $s = shift;
}
sub rwt_dtail_bef{
my $s = shift;
}
sub rwt_dtail{
my $s = shift;
my $ref = shift;
$s->rwt_dtail_bef($ref);
$s->line->[0] = "
};
return $item;
}
sub edit{
my $s = shift;
my $data = shift;
my $edit = shift;
$data = sprintf($edit->[1],$data) if $edit->[1];
if ($edit->[0] & hex("04")){
1 while $data =~ s/(.*\d)(\d\d\d)/$1,$2/;
}
if ($edit->[0] & hex("02")){
$data = "" if($data == 0)
}
return $data;
}
sub group_inji{
}
sub rwt_sumry{
}
sub rwt_sumcomp{
my $s = shift;
my $ref = shift;
for my $ctl (@{$s->break_ctl}){
for (@{$s->sth->{NAME}}){
if (exists($ctl->{$_}) and $ctl->{$_} =~ m/^[0-9\-\+\.]+$/){
$ctl->{$_} += $ref->{$_};
}
}
}
}
sub rwt_print_dt{
my $s = shift;
my $ref = shift;
for(@{$s->line}){
if($s->line_count > $s->max_line){
$s->line_count(0);
$s->rwt_head_print();
}
$s->printout($_);
$s->line_count($s->line_count + 1);
}
}
sub rwt_feed{
my $s = shift;
$s->line_count($s->max_line + 1);
}
sub rwt_head_print{
my $s = shift;
$s->head();
if ($s->column > 1){
my $colwidth = sprintf("width=%d%%",int(100/$s->column));
my $mod = $s->page_count % $s->column;
if ($mod == 1){
if ($s->page_count != 1){
$s->_bottom_print();
}
$s->printout($s->head_line->[0]);
$s->printout(qq{
});
}
my $i = 0;
for(@{$s->head_line}){
if ($i == 0 and $s->column > 1){
}else{
$s->printout($_);
}
$i++;
}
}
sub printout{
my $s = shift;
if($s->page < $s->restart_page){
return;
}
if($s->page > $s->end_page){
return;
}
$s->c_set(shift);
}
sub rwt_prepare{
my $s = shift;
return $dbh->prepare($s->sql);
}
sub rwt_dataget{
my $s = shift;
return $s->sth->fetchrow_hashref();
}
sub rwt_tisel{
my $s = shift;
}
sub rwt_data_save{
my $s = shift;
my $ref = shift;
$s->oref({});
my $oref = {};
for (@{$s->sth->{NAME}}){
$oref->{$_} = $ref->{$_};
}
$s->oref($oref);
}
sub head{
my $s = shift;
$s->footer();
$s->page_count($s->page_count + 1);
if ($s->column > 1) {
$s->page(int($s->page_count / $s->column) + 1);
}else{
$s->page($s->page_count);
}
$s->head_line->[0] = qq{};
$s->head_line->[0] .= qq{
@{[$s->title]}
page:@{[$s->page]}
};
$s->head_line->[1] = "
";
for (@{$s->item_list}) {
#$s->head_line->[1] .= qq{
@{[decode_utf8($_)]}
};
$s->head_line->[1] .= qq{
$_
};
}
$s->head_line->[1] .= "
";
$s->line_count(1);
}
sub footer{
my $s = shift;
my $level = shift||0;
return if ($s->page_count == 0);
$s->feed();
if ($s->column > 1){
$s->printout("
");
if ($s->page_count % $s->column == 0){
$s->printout("