Skip to content

Commit dfdfb88

Browse files
committed
EmailJournal: Komplexer Bericht mit komplexen Filter
1 parent 6aa91d8 commit dfdfb88

File tree

13 files changed

+472
-340
lines changed

13 files changed

+472
-340
lines changed

SL/Controller/EmailJournal.pm

Lines changed: 183 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ use strict;
44

55
use parent qw(SL::Controller::Base);
66

7+
use SL::ReportGenerator;
8+
use SL::Controller::Helper::ReportGenerator;
79
use SL::ZUGFeRD;
810
use SL::Controller::ZUGFeRD;
911
use SL::Controller::Helper::GetModels;
1012
use SL::DB::Employee;
1113
use SL::DB::EmailJournal;
1214
use SL::DB::EmailJournalAttachment;
1315
use SL::Presenter::EmailJournal;
16+
use SL::Presenter::Filter::EmailJournal;
1417
use SL::Presenter::Record qw(grouped_record_list);
1518
use SL::Presenter::Tag qw(html_tag div_tag button_tag);
1619
use SL::Helper::Flash;
@@ -222,22 +225,21 @@ sub action_list {
222225
my ($self) = @_;
223226

224227
$::auth->assert('email_journal');
225-
# default filter
226-
$::form->{filter} ||= {"obsolete:eq_ignore_empty" => 0};
227228

228229
if ( $::instance_conf->get_email_journal == 0 ) {
229-
flash('info', $::locale->text('Storing the emails in the journal is currently disabled in the client configuration.'));
230+
flash('info', t8('Storing the emails in the journal is currently disabled in the client configuration.'));
230231
}
232+
233+
$::form->{filter} ||= {
234+
'obsolete:eq_ignore_empty' => 0,
235+
};
236+
231237
$self->setup_list_action_bar;
232-
my @record_types_with_info = $self->get_record_types_with_info();
233-
my %record_types_to_text = $self->get_record_types_to_text();
234-
$self->render('email_journal/list',
235-
title => $::locale->text('Email journal'),
236-
ENTRIES => $self->models->get,
237-
MODELS => $self->models,
238-
RECORD_TYPES_WITH_INFO => \@record_types_with_info,
239-
RECORD_TYPES_TO_TEXT => \%record_types_to_text,
240-
);
238+
my $report = $self->prepare_report;
239+
$self->report_generator_list_objects(
240+
report => $report,
241+
objects => $self->models->get,
242+
);
241243
}
242244

243245
sub action_show {
@@ -778,6 +780,168 @@ sub find_customer_vendor_from_email {
778780
return $customer_vendor;
779781
}
780782

783+
sub prepare_report {
784+
my ($self) = @_;
785+
786+
my %record_types_to_text = $self->get_record_types_to_text();
787+
my @record_types_with_info = $self->get_record_types_with_info();
788+
789+
my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
790+
791+
my $callback = $self->models->get_callback;
792+
793+
my @columns_order = qw(
794+
id
795+
sender
796+
from
797+
recipients
798+
subject
799+
sent_on
800+
attachment_names
801+
has_unprocessed_attachments
802+
unprocessed_attachment_names
803+
status
804+
extended_status
805+
record_type
806+
linked_to
807+
obsolete
808+
);
809+
810+
my @default_columns = qw(
811+
from
812+
recipients
813+
subject
814+
sent_on
815+
);
816+
817+
my %column_defs = (
818+
id => {
819+
obj_link => sub {$self->url_for(
820+
action => 'show', id => $_[0]->id, callback => $callback
821+
)},
822+
sub => sub { $_[0]->id },
823+
},
824+
sender => {
825+
sub => sub { $_[0]->sender ? $_[0]->sender->name : '' },
826+
},
827+
from => {
828+
obj_link => sub {$self->url_for(
829+
action => 'show', id => $_[0]->id, callback => $callback
830+
)},
831+
sub => sub { $_[0]->from },
832+
},
833+
recipients => {
834+
obj_link => sub {$self->url_for(
835+
action => 'show', id => $_[0]->id, callback => $callback
836+
)},
837+
sub => sub { $_[0]->recipients },
838+
},
839+
subject => {
840+
obj_link => sub {$self->url_for(
841+
action => 'show', id => $_[0]->id, callback => $callback
842+
)},
843+
sub => sub { $_[0]->subject },
844+
},
845+
sent_on => {
846+
obj_link => sub {$self->url_for(
847+
action => 'show', id => $_[0]->id, callback => $callback
848+
)},
849+
sub => sub { $_[0]->sent_on->to_kivitendo(precision => 'minute') },
850+
},
851+
attachment_names => {
852+
sub => sub {join(', ',
853+
map {$_->name}
854+
sort {$a->position <=> $b->position}
855+
@{$_[0]->attachments}
856+
)},
857+
},
858+
has_unprocessed_attachments => {
859+
sub => sub { $_[0]->has_unprocessed_attachments }
860+
},
861+
unprocessed_attachment_names => {
862+
sub => sub {join(', ',
863+
map {$_->name}
864+
sort {$a->position <=> $b->position}
865+
grep {$_->processed == 0}
866+
@{$_[0]->attachments}
867+
)},
868+
},
869+
status => {
870+
sub => sub { SL::Presenter::EmailJournal::entry_status($_[0]) },
871+
},
872+
extended_status => {
873+
sub => sub { $_[0]->extended_status },
874+
},
875+
record_type => {
876+
sub => sub { $record_types_to_text{$_[0]->record_type} },
877+
},
878+
linked_to => {
879+
raw_data => sub {
880+
SL::Presenter::Record::simple_grouped_record_list($_[0]->linked_records)
881+
}
882+
},
883+
obsolete => {
884+
sub => sub { $_[0]->obsolete_as_bool_yn }
885+
},
886+
);
887+
$column_defs{$_}->{text} ||=
888+
t8( $self->models->get_sort_spec->{$_}->{title} || $_ )
889+
for keys %column_defs;
890+
891+
# make all sortable
892+
my @sortable = keys %column_defs;
893+
894+
unless ($::form->{active_in_report}) {
895+
$::form->{active_in_report}->{$_} = 1 foreach @default_columns;
896+
}
897+
898+
$column_defs{$_}->{visible} = $::form->{active_in_report}->{"$_"} || 0
899+
foreach keys %column_defs;
900+
901+
my $filter_html = SL::Presenter::Filter::EmailJournal::filter(
902+
$::form->{filter},
903+
active_in_report => $::form->{active_in_report},
904+
record_types_with_info => \@record_types_with_info,
905+
);
906+
907+
$self->models->disable_plugin('paginated')
908+
if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
909+
$self->models->add_additional_url_params(
910+
active_in_report => $::form->{active_in_report}
911+
);
912+
$self->models->finalize; # for filter laundering
913+
914+
$report->set_options(
915+
std_column_visibility => 1,
916+
controller_class => 'EmailJournal',
917+
output_format => 'HTML',
918+
raw_top_info_text => $self->render(
919+
'email_journal/_report_top',
920+
{ output => 0 },
921+
FILTER_HTML => $filter_html,
922+
),
923+
raw_bottom_info_text => $self->render(
924+
'email_journal/_report_bottom',
925+
{ output => 0 },
926+
models => $self->models
927+
),
928+
title => t8('Email journal'),
929+
allow_pdf_export => 1,
930+
allow_csv_export => 1,
931+
);
932+
$report->set_columns(%column_defs);
933+
$report->set_column_order(@columns_order);
934+
$report->set_export_options('list', qw(filter active_in_report));
935+
$report->set_options_from_form;
936+
937+
$self->models->set_report_generator_sort_options(
938+
report => $report,
939+
sortable_columns => \@sortable
940+
);
941+
942+
return $report;
943+
}
944+
781945
sub add_js {
782946
$::request->{layout}->use_javascript("${_}.js") for qw(
783947
kivi.EmailJournal
@@ -797,18 +961,23 @@ sub init_models {
797961
query => \@where,
798962
with_objects => [ 'sender' ],
799963
sorted => {
964+
_default => {
965+
by => 'sent_on',
966+
dir => 0,
967+
},
800968
sender => t8('Sender'),
801969
from => t8('From'),
802970
recipients => t8('Recipients'),
803971
subject => t8('Subject'),
804-
attachments => t8('Attachments'),
972+
attachment_names => t8('Attachments'),
973+
has_unprocessed_attachments => t8('Has unprocessed attachments'),
974+
unprocessed_attachment_names => t8('Unprocessed Attachments'),
805975
sent_on => t8('Sent on'),
806976
status => t8('Status'),
807977
extended_status => t8('Extended status'),
808978
record_type => t8('Record Type'),
809979
obsolete => t8('Obsolete'),
810980
linked_to => t8('Linked to'),
811-
has_unprocessed_attachments => t8('Has unprocessed attachments'),
812981
},
813982
);
814983
}

SL/DB/Manager/EmailJournal.pm

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ __PACKAGE__->add_filter_specs(
3333
)
3434
)} => \'TRUE';
3535
},
36+
unprocessed_attachment_names => sub {
37+
my ($key, $value, $prefix) = @_;
38+
return (
39+
and => [
40+
'attachments.name' => $value,
41+
'attachments.processed' => 0,
42+
],
43+
'attachments'
44+
)
45+
},
3646
has_unprocessed_attachments => sub {
3747
my ($key, $value, $prefix) = @_;
3848

@@ -68,7 +78,17 @@ sub _sort_spec {
6878
AND record_links.to_id = email_journal.id
6979
)
7080
)},
71-
attachment => qq{(
81+
attachment_names => qq{(
82+
SELECT STRING_AGG(
83+
email_journal_attachments.name,
84+
', '
85+
ORDER BY email_journal_attachments.position ASC
86+
)
87+
FROM email_journal_attachments
88+
WHERE
89+
email_journal_attachments.email_journal_id = email_journal.id
90+
)},
91+
unprocessed_attachment_names => qq{(
7292
SELECT STRING_AGG(
7393
email_journal_attachments.name,
7494
', '
@@ -77,6 +97,7 @@ sub _sort_spec {
7797
FROM email_journal_attachments
7898
WHERE
7999
email_journal_attachments.email_journal_id = email_journal.id
100+
AND email_journal_attachments.processed = FALSE
80101
)},
81102
has_unprocessed_attachments => qq{(
82103
SELECT count(*)

0 commit comments

Comments
 (0)