Skip to content

Commit

Permalink
EmailJournal: Komplexer Bericht mit komplexen Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
z4m1n0 committed Oct 25, 2024
1 parent efc0757 commit e6af833
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 340 deletions.
197 changes: 183 additions & 14 deletions SL/Controller/EmailJournal.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use strict;

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

use SL::ReportGenerator;
use SL::Controller::Helper::ReportGenerator;
use SL::ZUGFeRD;
use SL::Controller::ZUGFeRD;
use SL::Controller::Helper::GetModels;
use SL::DB::Employee;
use SL::DB::EmailJournal;
use SL::DB::EmailJournalAttachment;
use SL::Presenter::EmailJournal;
use SL::Presenter::Filter::EmailJournal;
use SL::Presenter::Record qw(grouped_record_list);
use SL::Presenter::Tag qw(html_tag div_tag button_tag);
use SL::Helper::Flash;
Expand Down Expand Up @@ -222,22 +225,21 @@ sub action_list {
my ($self) = @_;

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

if ( $::instance_conf->get_email_journal == 0 ) {
flash('info', $::locale->text('Storing the emails in the journal is currently disabled in the client configuration.'));
flash('info', t8('Storing the emails in the journal is currently disabled in the client configuration.'));
}

$::form->{filter} ||= {
'obsolete:eq_ignore_empty' => 0,
};

$self->setup_list_action_bar;
my @record_types_with_info = $self->get_record_types_with_info();
my %record_types_to_text = $self->get_record_types_to_text();
$self->render('email_journal/list',
title => $::locale->text('Email journal'),
ENTRIES => $self->models->get,
MODELS => $self->models,
RECORD_TYPES_WITH_INFO => \@record_types_with_info,
RECORD_TYPES_TO_TEXT => \%record_types_to_text,
);
my $report = $self->prepare_report;
$self->report_generator_list_objects(
report => $report,
objects => $self->models->get,
);
}

sub action_show {
Expand Down Expand Up @@ -778,6 +780,168 @@ sub find_customer_vendor_from_email {
return $customer_vendor;
}

sub prepare_report {
my ($self) = @_;

my %record_types_to_text = $self->get_record_types_to_text();
my @record_types_with_info = $self->get_record_types_with_info();

my $report = SL::ReportGenerator->new(\%::myconfig, $::form);

my $callback = $self->models->get_callback;

my @columns_order = qw(
id
sender
from
recipients
subject
sent_on
attachment_names
has_unprocessed_attachments
unprocessed_attachment_names
status
extended_status
record_type
linked_to
obsolete
);

my @default_columns = qw(
from
recipients
subject
sent_on
);

my %column_defs = (
id => {
obj_link => sub {$self->url_for(
action => 'show', id => $_[0]->id, callback => $callback
)},
sub => sub { $_[0]->id },
},
sender => {
sub => sub { $_[0]->sender ? $_[0]->sender->name : '' },
},
from => {
obj_link => sub {$self->url_for(
action => 'show', id => $_[0]->id, callback => $callback
)},
sub => sub { $_[0]->from },
},
recipients => {
obj_link => sub {$self->url_for(
action => 'show', id => $_[0]->id, callback => $callback
)},
sub => sub { $_[0]->recipients },
},
subject => {
obj_link => sub {$self->url_for(
action => 'show', id => $_[0]->id, callback => $callback
)},
sub => sub { $_[0]->subject },
},
sent_on => {
obj_link => sub {$self->url_for(
action => 'show', id => $_[0]->id, callback => $callback
)},
sub => sub { $_[0]->sent_on->to_kivitendo(precision => 'minute') },
},
attachment_names => {
sub => sub {join(', ',
map {$_->name}
sort {$a->position <=> $b->position}
@{$_[0]->attachments}
)},
},
has_unprocessed_attachments => {
sub => sub { $_[0]->has_unprocessed_attachments }
},
unprocessed_attachment_names => {
sub => sub {join(', ',
map {$_->name}
sort {$a->position <=> $b->position}
grep {$_->processed == 0}
@{$_[0]->attachments}
)},
},
status => {
sub => sub { SL::Presenter::EmailJournal::entry_status($_[0]) },
},
extended_status => {
sub => sub { $_[0]->extended_status },
},
record_type => {
sub => sub { $record_types_to_text{$_[0]->record_type} },
},
linked_to => {
raw_data => sub {
SL::Presenter::Record::simple_grouped_record_list($_[0]->linked_records)
}
},
obsolete => {
sub => sub { $_[0]->obsolete_as_bool_yn }
},
);
$column_defs{$_}->{text} ||=
t8( $self->models->get_sort_spec->{$_}->{title} || $_ )
for keys %column_defs;

# make all sortable
my @sortable = keys %column_defs;

unless ($::form->{active_in_report}) {
$::form->{active_in_report}->{$_} = 1 foreach @default_columns;
}

$column_defs{$_}->{visible} = $::form->{active_in_report}->{"$_"} || 0
foreach keys %column_defs;

my $filter_html = SL::Presenter::Filter::EmailJournal::filter(
$::form->{filter},
active_in_report => $::form->{active_in_report},
record_types_with_info => \@record_types_with_info,
);

$self->models->disable_plugin('paginated')
if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
$self->models->add_additional_url_params(
active_in_report => $::form->{active_in_report}
);
$self->models->finalize; # for filter laundering

$report->set_options(
std_column_visibility => 1,
controller_class => 'EmailJournal',
output_format => 'HTML',
raw_top_info_text => $self->render(
'email_journal/_report_top',
{ output => 0 },
FILTER_HTML => $filter_html,
),
raw_bottom_info_text => $self->render(
'email_journal/_report_bottom',
{ output => 0 },
models => $self->models
),
title => t8('Email journal'),
allow_pdf_export => 1,
allow_csv_export => 1,
);
$report->set_columns(%column_defs);
$report->set_column_order(@columns_order);
$report->set_export_options('list', qw(filter active_in_report));
$report->set_options_from_form;

$self->models->set_report_generator_sort_options(
report => $report,
sortable_columns => \@sortable
);

return $report;
}

sub add_js {
$::request->{layout}->use_javascript("${_}.js") for qw(
kivi.EmailJournal
Expand All @@ -797,18 +961,23 @@ sub init_models {
query => \@where,
with_objects => [ 'sender' ],
sorted => {
_default => {
by => 'sent_on',
dir => 0,
},
sender => t8('Sender'),
from => t8('From'),
recipients => t8('Recipients'),
subject => t8('Subject'),
attachments => t8('Attachments'),
attachment_names => t8('Attachments'),
has_unprocessed_attachments => t8('Has unprocessed attachments'),
unprocessed_attachment_names => t8('Unprocessed Attachments'),
sent_on => t8('Sent on'),
status => t8('Status'),
extended_status => t8('Extended status'),
record_type => t8('Record Type'),
obsolete => t8('Obsolete'),
linked_to => t8('Linked to'),
has_unprocessed_attachments => t8('Has unprocessed attachments'),
},
);
}
Expand Down
23 changes: 22 additions & 1 deletion SL/DB/Manager/EmailJournal.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ __PACKAGE__->add_filter_specs(
)
)} => \'TRUE';
},
unprocessed_attachment_names => sub {
my ($key, $value, $prefix) = @_;
return (
and => [
'attachments.name' => $value,
'attachments.processed' => 0,
],
'attachments'
)
},
has_unprocessed_attachments => sub {
my ($key, $value, $prefix) = @_;

Expand Down Expand Up @@ -68,7 +78,17 @@ sub _sort_spec {
AND record_links.to_id = email_journal.id
)
)},
attachment => qq{(
attachment_names => qq{(
SELECT STRING_AGG(
email_journal_attachments.name,
', '
ORDER BY email_journal_attachments.position ASC
)
FROM email_journal_attachments
WHERE
email_journal_attachments.email_journal_id = email_journal.id
)},
unprocessed_attachment_names => qq{(
SELECT STRING_AGG(
email_journal_attachments.name,
', '
Expand All @@ -77,6 +97,7 @@ sub _sort_spec {
FROM email_journal_attachments
WHERE
email_journal_attachments.email_journal_id = email_journal.id
AND email_journal_attachments.processed = FALSE
)},
has_unprocessed_attachments => qq{(
SELECT count(*)
Expand Down
Loading

0 comments on commit e6af833

Please sign in to comment.