Projekt

Allgemein

Profil

Herunterladen (82,5 KB) Statistiken
| Zweig: | Markierung: | Revision:
package SL::Controller::Reclamation;

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

use SL::Helper::Flash qw(flash_later);
use SL::HTML::Util;
use SL::Presenter::Tag qw(select_tag hidden_tag div_tag);
use SL::Presenter::Filter::Reclamation;
use SL::Locale::String qw(t8);
use SL::SessionFile::Random;
use SL::PriceSource;
use SL::ReportGenerator;
use SL::Controller::Helper::ReportGenerator;
use SL::Webdav;
use SL::File;
use SL::MIME;
use SL::Util qw(trim);
use SL::YAML;
use SL::DB::History;
use SL::DB::Reclamation;
use SL::DB::ReclamationItem;
use SL::DB::Default;
use SL::DB::Printer;
use SL::DB::Language;
use SL::DB::RecordLink;
use SL::DB::Shipto;
use SL::DB::Translation;
use SL::DB::ValidityToken;
use SL::DB::EmailJournal;
use SL::DB::Helper::RecordLink qw(set_record_link_conversions RECORD_ID RECORD_TYPE_REF RECORD_ITEM_ID RECORD_ITEM_TYPE_REF);
use SL::DB::Helper::TypeDataProxy;
use SL::DB::Helper::Record qw(get_object_name_from_type get_class_from_type);

use SL::Helper::CreatePDF qw(:all);
use SL::Helper::PrintOptions;
use SL::Helper::UserPreferences::PositionsScrollbar;
use SL::Helper::UserPreferences::UpdatePositions;

use SL::Controller::Helper::GetModels;

use SL::DB::Order;
use SL::DB::DeliveryOrder;
use SL::DB::Invoice;
use SL::Model::Record;
use SL::DB::Order::TypeData qw(:types);
use SL::DB::DeliveryOrder::TypeData qw(:types);
use SL::DB::Reclamation::TypeData qw(:types);

use List::Util qw(first sum0);
use List::UtilsBy qw(sort_by uniq_by);
use List::MoreUtils qw(any none pairwise first_index);
use English qw(-no_match_vars);
use File::Spec;
use Cwd;
use Sort::Naturally;

use Rose::Object::MakeMethods::Generic
(
scalar => [ qw(item_ids_to_delete is_custom_shipto_to_delete) ],
'scalar --get_set_init' => [qw(
all_price_factors cv models p part_picker_classification_ids reclamation
search_cvpartnumber show_update_button type valid_types type_data
)],
);


# safety
__PACKAGE__->run_before('check_auth');

__PACKAGE__->run_before('recalc',
only => [qw(
save save_as_new print preview_pdf send_email
save_and_show_email_dialog
save_and_new_record
save_and_credit_note
)]);

__PACKAGE__->run_before('get_unalterable_data',
only => [qw(
save save_as_new print preview_pdf send_email
save_and_show_email_dialog
save_and_new_record
save_and_credit_note
)]);

#
# actions
#

# add a new reclamation
sub action_add {
my ($self) = @_;

$self->pre_render();

if (!$::form->{form_validity_token}) {
$::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
}

$self->render(
'reclamation/form',
title => $self->type_data->text('add'),
%{$self->{template_args}},
);
}

sub action_add_from_record {
my ($self) = @_;
my $from_type = $::form->{from_type};
my $from_id = $::form->{from_id};

die "No 'from_type' was given." unless ($from_type);
die "No 'from_id' was given." unless ($from_id);

my %flags = ();
if (defined($::form->{from_item_ids})) {
my %use_item = map { $_ => 1 } @{$::form->{from_item_ids}};
$flags{item_filter} = sub {
my ($item) = @_;
return %use_item{$item->{RECORD_ITEM_ID()}};
}
}

my $record = SL::Model::Record->get_record($from_type, $from_id);
my $reclamation = SL::Model::Record->new_from_workflow($record, $self->type, %flags);
$self->reclamation($reclamation);
$self->reinit_after_new_reclamation();

if ($record->type eq SALES_RECLAMATION_TYPE()) { # check for direct delivery
# copy shipto in custom shipto (custom shipto will be copied by new_from() in case)
if ($::form->{use_shipto}) {
my $custom_shipto = $record->shipto->clone('SL::DB::Reclamation');
$self->reclamation->custom_shipto($custom_shipto) if $custom_shipto;
} else {
# remove any custom shipto if not wanted
$self->reclamation->custom_shipto(SL::DB::Shipto->new(module => 'RC', custom_variables => []));
}
}

$self->action_add;
}

sub action_add_from_email_journal {
my ($self) = @_;
die "No 'email_journal_id' was given." unless ($::form->{email_journal_id});

$self->action_add();
}

sub action_edit_with_email_journal_workflow {
my ($self) = @_;
die "No 'email_journal_id' was given." unless ($::form->{email_journal_id});
$::form->{workflow_email_journal_id} = delete $::form->{email_journal_id};
$::form->{workflow_email_attachment_id} = delete $::form->{email_attachment_id};
$::form->{workflow_email_callback} = delete $::form->{callback};

$self->action_edit();
}

# edit an existing reclamation
sub action_edit {
my ($self) = @_;
die "No 'id' was given." unless $::form->{id};

$self->load_reclamation();

$self->pre_render();
$self->render(
'reclamation/form',
title => $self->type_data->text('edit'),
%{$self->{template_args}},
);
}

# delete the reclamation
sub action_delete {
my ($self) = @_;


SL::Model::Record->delete($self->reclamation);
flash_later('info', $self->type_data->text('delete'));

my @redirect_params = (
action => 'add',
type => $self->type,
);

$self->redirect_to(@redirect_params);
}

# save the reclamation
sub action_save {
my ($self) = @_;

$self->save();

flash_later('info', t8('The reclamation has been saved'));

my @redirect_params;
if ($::form->{back_to_caller}) {
@redirect_params = $::form->{callback} ? ($::form->{callback})
: (controller => 'LoginScreen', action => 'user_login');
} else {
@redirect_params = (
action => 'edit',
type => $self->type,
id => $self->reclamation->id,
callback => $::form->{callback},
);
}

$self->redirect_to(@redirect_params);
}

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

$::form->{filter} ||= {};

$self->_setup_search_action_bar;
my $report = $self->prepare_report;
$self->report_generator_list_objects(
report => $report,
objects => $self->models->get,
options => {
action_bar_additional_submit_values => {
type => $self->type,
},
},
);
}

# save the reclamation as new document an open it for edit
sub action_save_as_new {
my ($self) = @_;

my $reclamation = $self->reclamation;

if (!$reclamation->id) {
$self->js->flash('error', t8('This object has not been saved yet.'));
return $self->js->render();
}

my $saved_reclamation = SL::DB::Reclamation->new(id => $reclamation->id)->load;

# Create new record from current one
my $new_reclamation = SL::Model::Record->clone_for_save_as_new($saved_reclamation, $reclamation);
$self->reclamation($new_reclamation);

if (!$::form->{form_validity_token}) {
$::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_RECLAMATION_SAVE())->token;
}

# save
$self->action_save();
}

# print the reclamation
#
# This is called if "print" is pressed in the print dialog.
# If PDF creation was requested and succeeded, the pdf is offered for download
# via send_file (which uses ajax in this case).
sub action_print {
my ($self) = @_;

$self->save();

$self->js_reset_reclamation_and_item_ids_after_save;

my $format = $::form->{print_options}->{format};
my $media = $::form->{print_options}->{media};
my $formname = $::form->{print_options}->{formname};
my $copies = $::form->{print_options}->{copies};
my $groupitems = $::form->{print_options}->{groupitems};
my $printer_id = $::form->{print_options}->{printer_id};

# only pdf and opendocument by now
if (none { $format eq $_ } qw(pdf opendocument opendocument_pdf)) {
return $self->js->flash('error', t8('Format \'#1\' is not supported yet/anymore.', $format))->render;
}

# only screen or printer by now
if (none { $media eq $_ } qw(screen printer)) {
return $self->js->flash('error', t8('Media \'#1\' is not supported yet/anymore.', $media))->render;
}

# create a form for generate_attachment_filename
my $form = Form->new;
$form->{record_number} = $self->reclamation->record_number;
$form->{type} = $self->type;
$form->{format} = $format;
$form->{formname} = $formname;
$form->{language} = '_' . $self->reclamation->language->template_code if $self->reclamation->language;
my $pdf_filename = $form->generate_attachment_filename();

my $pdf;
my @errors = generate_pdf($self->reclamation, \$pdf, {
format => $format,
formname => $formname,
language => $self->reclamation->language,
printer_id => $printer_id,
groupitems => $groupitems,
});
if (scalar @errors) {
return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render;
}

if ($media eq 'screen') { # screen/download
$self->js->flash('info', t8('The PDF has been created'));
$self->send_file(
\$pdf,
type => SL::MIME->mime_type_from_ext($pdf_filename),
name => $pdf_filename,
js_no_render => 1,
);
} elsif ($media eq 'printer') { # printer
my $printer_id = $::form->{print_options}->{printer_id};
SL::DB::Printer->new(id => $printer_id)->load->print_document(
copies => $copies,
content => $pdf,
);
$self->js->flash('info', t8('The PDF has been printed'));
}

my @warnings = store_pdf_to_webdav_and_filemanagement($self->reclamation, $pdf, $pdf_filename);
if (scalar @warnings) {
$self->js->flash('warning', $_) for @warnings;
}

$self->save_history('PRINTED');

$self->js
->run('kivi.ActionBar.setEnabled', '#save_and_email_action')
->render;
}

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

$self->save();

$self->js_reset_reclamation_and_item_ids_after_save;

my $format = 'pdf';
my $media = 'screen';
my $formname = $self->type;

# only pdf
# create a form for generate_attachment_filename
my $form = Form->new;
$form->{record_number} = $self->reclamation->record_number;
$form->{type} = $self->type;
$form->{format} = $format;
$form->{formname} = $formname;
$form->{language} = '_' . $self->reclamation->language->template_code if $self->reclamation->language;
my $pdf_filename = $form->generate_attachment_filename();

my $pdf;
my @errors = generate_pdf($self->reclamation, \$pdf, {
format => $format,
formname => $formname,
language => $self->reclamation->language,
});
if (scalar @errors) {
return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render;
}
$self->save_history('PREVIEWED');
$self->js->flash('info', t8('The PDF has been previewed'));
# screen/download
$self->send_file(
\$pdf,
if ($print_form->{format} =~ /(opendocument|oasis)/i) {
$template_ext = 'odt';
$template_type = 'OpenDocument';

# add variables for printing with the built-in parser
$reclamation->flatten_to_form($print_form, format_amounts => 1);
$reclamation->add_legacy_template_arrays($print_form);

$variable_content_types = {
longdescription => 'html',
notes => 'html',
$::form->get_variable_content_types_for_cvars,
}
}

# search for the template
my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
name => $print_form->{formname},
extension => $template_ext,
email => $print_form->{media} eq 'email',
language => $params->{language},
printer_id => $print_form->{printer_id},
);

if (!defined $template_file) {
push @errors, t8(
'Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.',
join ', ',
map { "'$_'"} @template_files
);
}

return @errors if scalar @errors;

$print_form->throw_on_error(sub {
eval {
$print_form->prepare_for_printing;

$$pdf_ref = SL::Helper::CreatePDF->create_pdf(
format => $print_form->{format},
template_type => $template_type,
template => $template_file,
variables => $print_form,
variable_content_types => $variable_content_types,
);
1;
} || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->error : $EVAL_ERROR;
});

return @errors;
}

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

my %files = map { ($_ => []) } qw(versions files cv_files project_files part_files);

return %files if !$::instance_conf->get_doc_storage;

if ($self->reclamation->id) {
$files{versions} = [
SL::File->get_all_versions(
object_id => $self->reclamation->id,
object_type => $self->reclamation->type,
file_type => 'document')
];
$files{files} = [
SL::File->get_all(
object_id => $self->reclamation->id,
object_type => $self->reclamation->type,
file_type => 'attachment')
];
$files{cv_files} = [
SL::File->get_all(
object_id => $self->reclamation->customervendor->id,
object_type => $self->cv,
file_type => 'attachment')
];
$files{project_files} = [
SL::File->get_all(
object_id => $self->reclamation->globalproject_id,
object_type => 'project',
file_type => 'attachment')
];
}

my @parts =
uniq_by { $_->{id} }
map {
+{ id => $_->part->id,
partnumber => $_->part->partnumber }
} @{$self->reclamation->items_sorted};

foreach my $part (@parts) {
my @pfiles = SL::File->get_all(object_id => $part->{id}, object_type => 'part');
push @{ $files{part_files} }, map { +{ %{ $_ }, partnumber => $part->{partnumber} } } @pfiles;
}

foreach my $key (keys %files) {
$files{$key} = [ sort_by { lc $_->{db_file}->{file_name} } @{ $files{$key} } ];
}

return %files;
}

sub get_item_cvpartnumber {
my ($self, $item) = @_;

return if !$self->search_cvpartnumber;
return if !$self->reclamation->customervendor;

if (!$self->reclamation->is_sales) {
my @mms = grep { $_->make eq $self->reclamation->customervendor->id } @{$item->part->makemodels};
$item->{cvpartnumber} = $mms[0]->model if scalar @mms;
} elsif ($self->reclamation->is_sales) {
my @cps = grep { $_->customer_id eq $self->reclamation->customervendor->id } @{$item->part->customerprices};
$item->{cvpartnumber} = $cps[0]->customer_partnumber if scalar @cps;
}
}

sub get_part_texts {
my ($part_or_id, $language_or_id, %defaults) = @_;

my $part = ref($part_or_id) ? $part_or_id : SL::DB::Part->load_cached($part_or_id);
my $language_id = ref($language_or_id) ? $language_or_id->id : $language_or_id;
my $texts = {
description => $defaults{description} // $part->description,
longdescription => $defaults{longdescription} // $part->notes,
};

return $texts unless $language_id;

my $translation = SL::DB::Manager::Translation->get_first(
where => [
parts_id => $part->id,
language_id => $language_id,
]);

$texts->{description} = $translation->translation if $translation && $translation->translation;
$texts->{longdescription} = $translation->longdescription if $translation && $translation->longdescription;

return $texts;
}

sub save_history {
my ($self, $addition) = @_;

SL::DB::History->new(
trans_id => $self->reclamation->id,
employee_id => SL::DB::Manager::Employee->current->id,
what_done => $self->reclamation->type,
snumbers => 'record_number_' . $self->reclamation->record_number,
addition => $addition,
)->save;
}

sub store_pdf_to_webdav_and_filemanagement {
my($reclamation, $content, $filename) = @_;

my @errors;

# copy file to webdav folder
if ($reclamation->record_number && $::instance_conf->get_webdav_documents) {
my $webdav = SL::Webdav->new(
type => $reclamation->type,
number => $reclamation->record_number,
);
my $webdav_file = SL::Webdav::File->new(
webdav => $webdav,
filename => $filename,
);
eval {
$webdav_file->store(data => \$content);
1;
} or do {
push @errors, t8('Storing PDF to webdav folder failed: #1', $@);
};
}
if ($reclamation->id && $::instance_conf->get_doc_storage) {
eval {
SL::File->save(object_id => $reclamation->id,
object_type => $reclamation->type,
mime_type => 'application/pdf',
source => 'created',
file_type => 'document',
file_name => $filename,
file_contents => $content);
1;
} or do {
push @errors, t8('Storing PDF in storage backend failed: #1', $@);
};
}

return @errors;
}

sub init_type_data {
my ($self) = @_;
SL::DB::Helper::TypeDataProxy->new('SL::DB::Reclamation', $self->reclamation->record_type);
}

1;

__END__

=encoding utf-8

=head1 NAME

SL::Controller::Reclamation - controller for reclamations

=head1 SYNOPSIS

This is a new form to enter reclamations, written with the use
of controller and java script techniques.

The aim is to provide the user a good experience and a fast workflow.

=head2 Key Features

=over 4

=item *

One input row, so that input happens every time at the same place.

=item *

Use of pickers where possible.

=item *

Possibility to enter more than one item at once.

=item *

Item list in a scrollable area, so that the workflow buttons stay at
the bottom.

=item *

Ordering item rows with drag and drop is possible. Sorting item rows is
possible (by partnumber, description, reason, qty, sellprice
and discount for now).

=item *

No C<update> is necessary. All entries and calculations are managed
with ajax-calls and the page only reloads on C<save>.

=item *

User can see changes immediately, because of the use of java script
and ajax.

=item *

Parts that are linked though RecordLinks are protected against price editing.

=back

=head1 CODE

=head2 Layout

=over 4

=item * C<SL/Controller/Reclamation.pm>

the controller

=item * C<template/webpages/reclamation/form.html>

main form

=item * C<template/webpages/reclamation/tabs/basic_data.html>

Main tab for basic_data.

This is the only tab here for now. "webdav", "documents", "attachements" and
"linked records" tabs are reused from generic code.

=over 4

=item * C<template/webpages/reclamation/tabs/basic_data/_business_info_row.html>

For displaying information on business type

=item * C<template/webpages/reclamation/tabs/basic_data/_item_input.html>

The input line for items

=item * C<template/webpages/reclamation/tabs/basic_data/_row.html>

One row for already entered items

=item * C<template/webpages/reclamation/tabs/basic_data/_second_row.html>

Foldable second row for already entered items with more fields

=item * C<template/webpages/reclamation/tabs/basic_data/_tax_row.html>

Displaying tax information

=item * C<template/webpages/reclamation/tabs/basic_data/_price_sources_dialog.html>

Dialog for selecting price and discount sources

=back

=item * C<js/kivi.Reclamation.js>

java script functions

=back

=head1 KNOWN BUGS AND CAVEATS

=over 4

=item *

Table header is not sticky in the scrolling area.

=item *

Sorting does not include C<position>, neither does reordering.

This behavior was implemented intentionally. But we can discuss, which behavior
should be implemented.

=back

=head1 To discuss / Nice to have

=over 4

=item *

Possibility to select PriceSources in input row?

=item *

This controller uses a (changed) copy of the template for the PriceSource
dialog. Maybe there could be used one code source.

=item *

A warning when leaving the page without saving unchanged inputs.

=back

=head1 AUTHOR

Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>

=cut
(59-59/85)