use strict;
use parent qw(SL::Controller::Base);
+use Carp;
+use File::Basename;
use POSIX qw(strftime);
use SL::Controller::Helper::GetModels;
use SL::Controller::Helper::ReportGenerator;
+use SL::CT;
use SL::DB::Letter;
use SL::DB::LetterDraft;
use SL::DB::Employee;
-use SL::Helper::Flash qw(flash);
+use SL::Helper::Flash qw(flash flash_later);
use SL::Helper::CreatePDF;
use SL::Helper::PrintOptions;
use SL::Locale::String qw(t8);
+use SL::Mailer;
use SL::IS;
use SL::ReportGenerator;
+use SL::Webdav;
+use SL::Webdav::File;
use Rose::Object::MakeMethods::Generic (
- 'scalar --get_set_init' => [ qw(letter all_employees models) ],
+ 'scalar --get_set_init' => [ qw(letter all_employees models webdav_objects) ],
);
__PACKAGE__->run_before('check_auth_edit');
date => t8('Date'),
subject => t8('Subject'),
letternumber => t8('Letternumber'),
- vc_id => t8('Customer'),
+ customer_id => t8('Customer'),
contact => t8('Contact'),
);
my $letter = $self->letter;
- if (!$self->letter->vc_id || !$self->letter->customer) {
+ if (!$self->letter->customer_id || !$self->letter->customer) {
return $self->js
->replaceWith(
'#letter_cp_id',
my $default;
if ( $letter->contact
&& $letter->contact->cp_cv_id
- && $letter->contact->cp_cv_id == $letter->vc_id) {
+ && $letter->contact->cp_cv_id == $letter->customer_id) {
$default = $letter->contact->cp_id;
} else {
$default = '';
}
sub action_print_letter {
- my ($self, $old_form) = @_;
+ my ($self, %params) = @_;
my $display_form = $::form->{display_form} || "display_form";
my $letter = $self->_update;
- $self->export_letter_to_form($letter);
- $::form->{formname} = "letter";
- $::form->{format} = "pdf";
-
- my $language_saved = $::form->{language_id};
- my $greeting_saved = $::form->{greeting};
- my $cp_id_saved = $::form->{cp_id};
-
- IS->customer_details(\%::myconfig, $::form);
-
- if (!$cp_id_saved) {
- # No contact was selected. Delete all contact variables because
- # IS->customer_details() and IR->vendor_details() get the default
- # contact anyway.
- map({ delete($::form->{$_}); } grep(/^cp_/, keys(%{ $::form })));
- }
-
- $::form->{greeting} = $greeting_saved;
- $::form->{language_id} = $language_saved;
-
- if ($::form->{cp_id}) {
- CT->get_contact(\%::myconfig, $::form);
- }
-
- $::form->{cp_contact_formal} = ($::form->{cp_greeting} ? "$::form->{cp_greeting} " : '') . ($::form->{cp_givenname} ? "$::form->{cp_givenname} " : '') . $::form->{cp_name};
-
- $::form->get_employee_data('prefix' => 'employee', 'id' => $letter->{employee_id});
- $::form->get_employee_data('prefix' => 'salesman', 'id' => $letter->{salesman_id});
-
my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
name => 'letter',
printer_id => $::form->{printer_id},
$::form->error($::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files));
}
- my %create_params = (
- template => $template_file,
- variables => $::form,
- return => 'file_name',
- );
-
- my $pdf_file_name;
+ my %result;
eval {
- $pdf_file_name = SL::Helper::CreatePDF->create_pdf(%create_params);
+ %result = SL::Template::LaTeX->parse_and_create_pdf(
+ $template_file,
+ SELF => $self,
+ FORM => $::form,
+ letter => $letter,
+ template_meta => {
+ formname => 'letter',
+ language => SL::DB::Language->new,
+ extension => 'pdf',
+ format => $::form->{format},
+ media => $::form->{media},
+ printer => SL::DB::Manager::Printer->find_by_or_create(id => $::form->{printer_id} || undef),
+ today => DateTime->today,
+ },
+ );
+
+ die $result{error} if $result{error};
+
+ $::form->{type} = 'letter';
+ $::form->{formname} = 'letter';
+ $::form->{letternumber} = $letter->letternumber;
+ my $attachment_name = $::form->generate_attachment_filename;
+
+ if ($::instance_conf->get_webdav_documents) {
+ my $webdav_file = SL::Webdav::File->new(
+ filename => $attachment_name,
+ webdav => SL::Webdav->new(
+ type => 'letter',
+ number => $letter->letternumber,
+ ),
+ );
+
+ $webdav_file->store(file => $result{file_name});
+ }
# set some form defaults for printing webdav copy variables
if ( $::form->{media} eq 'email') {
my $mail = Mailer->new;
my $signature = $::myconfig{signature};
- $mail->{$_} = $::form->{$_} for qw(cc subject message bcc to);
+ $mail->{$_} = $params{email}->{$_} for qw(to cc subject message bcc);
$mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
- $mail->{fileid} = time() . '.' . $$ . '.';
- $mail->{attachments} = [{ "filename" => $pdf_file_name,
- "name" => $::form->{attachment_name} }];
+ $mail->{attachments} = [{ filename => $result{file_name},
+ name => $params{email}->{attachment_filename} }];
$mail->{message} .= "\n-- \n$signature";
$mail->{message} =~ s/\r//g;
- # copy_file_to_webdav was already done via io.pl -> edit_e_mail
- my $err = $mail->send;
- return !$err;
- }
-
- if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
+ $mail->send;
+ unlink $result{file_name};
- my $file = IO::File->new($pdf_file_name, 'r') || croak("Cannot open file '$pdf_file_name'");
- my $size = -s $pdf_file_name;
- my $content_type = 'application/pdf';
- my $attachment_name = $::form->generate_attachment_filename;
- $attachment_name =~ s:.*//::g;
+ flash_later('info', t8('The email has been sent.'));
+ $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
- print $::form->create_http_response(content_type => $content_type,
- content_disposition => 'attachment; filename="' . $attachment_name . '"',
- content_length => $size);
+ return 1;
+ }
- $::locale->with_raw_io(\*STDOUT, sub { print while <$file> });
- $file->close;
+ if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
+ $self->send_file($result{file_name}, name => $attachment_name);
+ unlink $result{file_name};
- Common::copy_file_to_webdav_folder($::form) if $::instance_conf->get_webdav_documents;
- unlink $pdf_file_name;
return 1;
}
my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
- my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command);
+ $printer->print_document(
+ copies => $::form->{copies},
+ file_name => $result{file_name},
+ );
- open my $out, '|-', $command or die $!;
- binmode $out;
- print $out scalar(read_file($pdf_file_name));
- close $out;
- Common::copy_file_to_webdav_folder($::form) if $::instance_conf->get_webdav_documents;
+ unlink $result{file_name};
flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
- $self->redirect_to(action => 'edit', id => $letter->{id}, 'printer_id' => $::form->{printer_id});
+ $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
1;
} or do {
- unlink $pdf_file_name;
+ unlink $result{file_name} if $result{file_name};
$::form->error(t8("Creating the PDF failed:") . " " . $@);
};
-
}
sub action_update {
sub action_delete_drafts {
my ($self) = @_;
- delete_letter_drafts();
+
+ my @ids = @{ $::form->{ids} || [] };
+ SL::DB::Manager::LetterDraft->delete_all(where => [ id => \@ids ]) if @ids;
+
$self->action_add(skip_drafts => 1);
}
+sub action_edit_email {
+ my ($self) = @_;
+
+ my $letter = $self->_update;
+ $self->export_letter_to_form($letter);
+
+ $::form->{formname} = "letter";
+ $::form->{type} = "letter";
+ $::form->{letternumber} = $self->letter->letternumber;
+
+ my @hiddens = map {
+ my $value = $letter->$_;
+ $value = $value->to_kivitendo if ref($_) =~ m{Date};
+
+ { name => "letter.$_", value => $value }
+ } ($letter->meta->columns);
+
+ my %vars = (
+ script => 'controller.pl',
+ title => t8('Send letter via e-mail'),
+ email => $letter->contact ? $letter->contact->cp_email : '',
+ subject => $::form->generate_email_subject,
+ a_filename => $::form->generate_attachment_filename,
+ action => 'Letter/send_email',
+ HIDDEN => \@hiddens,
+ SHOW_BCC => $::auth->assert('email_bcc', 'may fail'),
+ );
+
+ $self->render('generic/edit_email', %vars);
+}
+
+sub action_send_email {
+ my ($self) = @_;
+
+ $::form->{media} = 'email';
+ $self->action_print_letter(
+ email => {
+ to => $::form->{email},
+ map { ($_ => $::form->{$_}) } qw(cc bcc subject attachment_filename message)
+ }
+ );
+}
+
sub _display {
my ($self, %params) = @_;
+ $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
+
my $letter = $self->letter;
$params{title} ||= t8('Edit Letter');
$::request->layout->add_javascripts('edit_part_window.js');
$::form->{language_id} ||= $params{language_id};
+ $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
$self->render('letter/edit',
%params,
my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
$self->{report} = $report;
- my @columns = qw(date subject letternumber vc_id contact date);
- my @sortable = qw(date subject letternumber vc_id contact date);
+ my @columns = qw(date subject letternumber customer_id contact date);
+ my @sortable = qw(date subject letternumber customer_id contact date);
my %column_defs = (
date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
- vc_id => { text => t8('Customer'), sub => sub { $_[0]->customer->displayable_name } },
+ customer_id => { text => t8('Customer'), sub => sub { SL::DB::Manager::Customer->find_by_or_create(id => $_[0]->customer_id)->displayable_name } },
contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
);
$self->{filter_summary} = join ', ', @filter_strings;
}
-sub e_mail {
- my $letter = _update();
-
- $letter->check_number;
- $letter->save;
-
- $::form->{formname} = "letter";
- $letter->export_to($::form);
-
- $::form->{id} = $letter->{id};
- edit_e_mail();
-}
-
sub load_letter_draft {
my ($self, %params) = @_;
->assign_attributes(%{ $::form->{letter} });
if ($letter->cp_id) {
-# $letter->vc_id($letter->contact->cp_cv_id);
+# $letter->customer_id($letter->contact->cp_cv_id);
# contacts don't have language_id yet
# $letter->greeting(GenericTranslations->get(
# translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
SL::Controller::Helper::GetModels->new(
controller => $self,
model => 'Letter',
- sorted => {
- _default => {
- by => 'letternumber',
- dir => 1,
- },
- %sort_columns,
- },
+ sorted => \%sort_columns,
with_objects => [ 'contact', 'salesman', 'employee' ],
);
}
SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
}
+sub init_webdav_objects {
+ my ($self) = @_;
+
+ return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
+
+ my $webdav = SL::Webdav->new(
+ type => 'letter',
+ number => $self->letter->letternumber,
+ );
+
+ my $webdav_path = $webdav->webdav_path;
+ my @all_objects = $webdav->get_all_objects;
+
+ return [ map {
+ +{ name => $_->filename,
+ type => t8('File'),
+ link => File::Spec->catdir($webdav_path, $_->filename),
+ }
+ } @all_objects ];
+}
+
sub check_auth_edit {
$::auth->assert('sales_letter_edit');
}