package Ernad::Indat::Report; use strict; use warnings; use base 'Ernad::Indat'; use Carp qw(confess); use Clone 'clone'; use Data::Dumper; use File::Basename; use Ernad::Indat; use Ernad::FileInfo; use Ernad::Store; use Krichel::Shoti; use Ernad::Indat::Add; use Ernad::Indat::Vypot; use Ernad::Indat::Otria; our $e=$main::e; ## This does not remove the files from the vypot directory. We ## use a manual delete for that. sub remove_from_issues { my $i=shift; my $repcode=shift // confess "I need a repcode as argument here."; my $e=$i->{'e'}; my $given_issuedate=shift // ''; my $issuedates; if($given_issuedate) { $issuedates=[$given_issuedate]; } else { $issuedates=$i->get_issues(); } $e->echo(__LINE__,"I look data for $repcode to remove."); my $reports_dir=$i->{'dir'}->{'reports'}; $e->echo(__LINE__,"I don't remove the json files in $reports_dir."); foreach my $issuedate (@$issuedates) { my $json_file=$i->{'dir'}->{'issues'}."/$issuedate.json"; if(not -f $json_file) { $e->echo(__LINE__,"I don't see the file $json_file, skip"); next; } $i->{'i'}=&Ernad::Store::load($json_file); if(not $i->{'i'}->{'d'}->{$repcode}) { $e->echo(__LINE__,"I have no data for $repcode in $json_file, skip"); next; } $e->echo(__LINE__,"I delete $repcode in $json_file."); delete $i->{'i'}->{'d'}->{$repcode}; my $shoti=&Krichel::Shoti::now(); $e->echo(__LINE__,"I set the shoti on the data to $shoti."); $i->{'v'}=$shoti; $e->echo(__LINE__,"I save $json_file."); $i->{'i'}=&Ernad::Store::save($i->{'i'},$json_file); delete $i->{'i'}; } } ## moved to Ernad::Indat::Vypot sub add { confess "I have moved to Ernad::Indat::Vypot."; } ## for the reports blatt sub gather_all_reports { my $i=shift; ## new param to sort by my $sort_by=shift // ''; my $e=$i->{'e'}; $i->{'do_save'}=0; my $json=$i->{'file'}->{'json'}->{'reports'}; if(-f $json) { $i->{'i'}=&Ernad::Store::load($json); } else { $i->{'i'}={}; } my $otria=Ernad::Indat::Otria->new(); ## FixMe: this should really be triggered for maiden reports only $otria->update(); my $report_list=$otria->load()->{'list'}; ## by default, sort by lafise #if($sort_by eq 'by_lafise') { my @repcodes=@{$e->{'o'}->by_lafise()}; $report_list=\@repcodes; #} foreach my $repcode (@{$report_list}) { $i->gather_for_reports($repcode); } if($i->{'do_save'}) { $i->{'i'}->{'v'}=Krichel::Shoti::now; &Ernad::Store::save($i->{'i'},$json); } delete $i->{'do_save'}; delete $i->{'i'}; return $report_list; } sub gather_for_reports { my $i=shift; my $repcode=shift // confess "I need this here."; my $v=Ernad::Indat::Vypot->new(); my $jsons=$v->get($repcode); if(not scalar keys %$jsons) { return 0; } foreach my $json (keys %{$jsons}) { my $issuedate=substr(basename($json),0,10); ## ouch my $shoti_on_json=$jsons->{$json}; my $version=$i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{'v'} // ''; if($version and Krichel::Shoti::compare($shoti_on_json,$version)) { #print "I skip $json\n"; next; } $i->{'do_save'}=1; $i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{'v'}=&Krichel::Shoti::now(); my $vypot=&Ernad::Store::load($json); my $in=$vypot->{'d'}->{$repcode}->{$issuedate}; #delete $in->{'class'}; #delete $in->{'created'}; #delete $in->{'filtered'}; #delete $in->{'selected'}; #print Dumper $in->{'sent'}; my $vypot_data=$v->first_shoti_d($in->{'sent'}); delete $vypot_data->{'papid'}; $i->{'i'}->{'d'}->{$repcode}->{$issuedate}->{'d'}=clone($vypot_data); } } sub json { my $i=shift; my $json=$i->{'file'}->{'json'}->{'reports'} // confess "I need this set here."; if(not -f $json) { return {}; } my $out=&Ernad::Store::load($json); return $out; } sub compile_old { my $i=shift; my $repcode=shift // confess "I need an issuedate here."; my $given_repcode=shift; my $e=$i->{'e'}; ## if there is no namf ->set_file returns zero, we leave as well #$i->set_file() or return 0; my $v=Ernad::Indat::Vypot->new(); my $jsons=$v->get($repcode); my $json=$i->{'dir'}->{'reports'}."/$repcode.json"; if(-f $json) { $i->{'i'}=&Ernad::Store::load($json); $i->{'i'}->{'v'}=&Ernad::Dates::mshoti($json); } else { $i->{'i'}={}; } foreach my $json (sort keys %$jsons) { my $issuedate=substr(basename($json),0,10); my $in=&Ernad::Store::load($json); my $data=$in->{'d'}->{$repcode}->{$issuedate}; delete $data->{'class'}; delete $data->{'created'}; delete $data->{'filtered'}; delete $data->{'selected'}; $i->{'i'}->{$issuedate}->{'d'}=$data; $i->{'i'}->{$issuedate}->{'v'}=$jsons->{$json}; } # print Dumper $i->{'i'}; if(not scalar keys %{$i->{'i'}}) { return 0; } &Ernad::Store::save($i->{'i'},$json); } 1;