package Ernad::Presort::State; use strict; use warnings; use Carp qw(confess); use base ('Ernad::Presorter'); use Ernad::Dates; use Ernad::Erimp; use Ernad::Files; binmode(STDOUT,':utf8'); ##learning states of in issue in a report ## ## 0 prenatal ## 1 untouched ## 2 pending ## 3 selected ## 4 done ## ## futile is 2, 3, and 4 ## grey is 2 and 3 ## returns the repcode sub setup { my $t=shift; my $repcode=$t->get_repcode() // confess "I can't get the repcode."; my $e=$t->{'e'} // confess "I need an erimp here."; # $t->{'birthday'}=$e->get_report_birthday($repcode) // $t->{'birthday'}=$e->{'p'}->get_report_birthday($repcode) // confess "I don't see a birthday for $repcode."; return $repcode; } ## find the learn state sub find { my $t=shift; my $issuedate=shift // confess "I need an issuedate here."; my $e=$t->{'e'} // confess "I need an erimp here."; my $repcode=$t->get_repcode() // confess "I can't get the repcode."; my $rerc=$e->{'report'}->{$repcode} // confess "I'm not aware of report $repcode."; my $sent_dir=$rerc->{'dir'}->{'sent'}; my $created_dir=$rerc->{'dir'}->{'created'}; my $selected_dir=$rerc->{'dir'}->{'selected'}; ## fixme, this should be somewhere else if(not $t->{'birthday'}) { $t->setup($repcode); } if(&Ernad::Dates::compare_dates($issuedate, $t->{'birthday'}) > 0) { return "prenatal"; } if($t->any_file_in($sent_dir,$issuedate)) { return 'done'; } my $is_selected=$t->any_file_in($selected_dir,$issuedate); ## we check noting in selected just as an additional measure if(not $t->any_file_in($created_dir,$issuedate) and not $is_selected) { if($t->any_file_in($sent_dir,$issuedate)) { confess "inconstency"; } return 'untouched'; } if($is_selected) { #if($t->any_file_in($sent_dir,$issuedate)) { # confess "inconstency"; #} return 'selected'; } return 'pending'; } ## FixMe: should be moved to Ernad::Common sub any_file_in { my $t=shift; my $dir=shift // confess "I need a directory here."; my $date=shift // confess "I need a directory here."; if(not -d $dir) { confess "I don't see your directory '$dir'."; } my @files=glob("$dir/$date*"); if(not scalar(@files)) { return ''; } return $files[0]; } ## a legacy concept #sub is_futile { # my $t=shift; # my $issuedate=shift // confess "I need an issuedate here"; # if($t->state($issuedate) eq 'untouched') { # return 0; # } # return 1; #} ## it's unclear whether we need to take action when a report issue is grey sub is_grey { my $t=shift; my $issuedate=shift // confess "I need an issuedate here"; my $state=$t->state($issuedate) // confess "I can't get a state for issue $issuedate"; if($state eq 'pending' or $state eq 'selected') { return 1; } return 0; } 1;