1 package SL::Controller::Letter;
4 use parent qw(SL::Controller::Base);
8 use POSIX qw(strftime);
9 use SL::Controller::Helper::GetModels;
10 use SL::Controller::Helper::ReportGenerator;
15 use SL::DB::LetterDraft;
17 use SL::Helper::Flash qw(flash flash_later);
18 use SL::Helper::CreatePDF;
19 use SL::Helper::PrintOptions;
20 use SL::Locale::String qw(t8);
23 use SL::ReportGenerator;
27 use Rose::Object::MakeMethods::Generic (
28 'scalar --get_set_init' => [ qw(letter all_employees models webdav_objects is_sales) ],
31 __PACKAGE__->run_before('check_auth_edit');
32 __PACKAGE__->run_before('check_auth_report', only => [ qw(list) ]);
34 use constant TEXT_CREATED_FOR_VALUES => (qw(presskit fax letter));
35 use constant PAGE_CREATED_FOR_VALUES => (qw(sketch 1 2));
39 subject => t8('Subject'),
40 letternumber => t8('Letternumber'),
41 customer_id => t8('Customer'),
42 vendor_id => t8('Vendor'),
43 contact => t8('Contact'),
49 my ($self, %params) = @_;
51 return if $self->load_letter_draft(%params);
53 $self->letter->employee_id(SL::DB::Manager::Employee->current->id);
54 $self->letter->salesman_id(SL::DB::Manager::Employee->current->id);
57 title => t8('Add Letter'),
58 language_id => $params{language_id},
63 my ($self, %params) = @_;
65 return $self->action_add
66 unless $::form->{letter} || $::form->{draft};
68 if ($::form->{draft}) {
69 $self->letter(SL::DB::Letter->new_from_draft($::form->{draft}{id}));
70 $self->is_sales($self->letter->is_sales);
74 title => t8('Edit Letter'),
79 my ($self, %params) = @_;
81 my $letter = $self->_update;
83 if (!$self->check_letter($letter)) {
84 return $self->_display;
90 flash('error', t8('There was an error saving the letter'));
91 return $self->_display;
94 flash('info', t8('Letter saved!'));
99 sub action_update_contacts {
102 my $letter = $self->letter;
104 if (!$self->letter->has_customer_vendor) {
108 SL::Presenter->get->select_tag('letter.cp_id', [], value_key => 'cp_id', title_key => 'full_name')
113 my $contacts = $letter->customer_vendor->contacts;
116 if ( $letter->contact
117 && $letter->contact->cp_cv_id
118 && $letter->contact->cp_cv_id == $letter->customer_vendor_id) {
119 $default = $letter->contact->cp_id;
127 SL::Presenter->get->select_tag('letter.cp_id', $contacts, default => $default, value_key => 'cp_id', title_key => 'full_name')
132 sub action_save_letter_draft {
133 my ($self, %params) = @_;
137 my $letter_draft = SL::DB::LetterDraft->new_from_letter($self->_update);
139 if (!$letter_draft->save) {
140 flash('error', t8('There was an error saving the letter draft'));
141 return $self->_display;
144 flash('info', t8('Draft for this Letter saved!'));
150 my ($self, %params) = @_;
152 if (!$self->letter->delete) {
153 flash('error', t8('An error occured. Letter could not be deleted.'));
154 return $self->action_update;
157 flash_later('info', t8('Letter deleted'));
158 $self->redirect_to(action => 'list');
161 sub action_delete_letter_drafts {
162 my ($self, %params) = @_;
164 my @ids = grep { /^checked_(.*)/ && $::form->{$_} } keys %$::form;
166 SL::DB::Manager::LetterDraft->delete_all(query => [ ids => \@ids ]) if @ids;
168 $self->redirect_to(action => 'add');
172 my ($self, %params) = @_;
174 $self->make_filter_summary;
175 $self->prepare_report;
177 my $letters = $self->models->get;
178 $self->report_generator_list_objects(report => $self->{report}, objects => $letters);
182 sub action_print_letter {
183 my ($self, %params) = @_;
185 my $display_form = $::form->{display_form} || "display_form";
186 my $letter = $self->_update;
188 my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
190 printer_id => $::form->{printer_id},
191 language_id => $::form->{language_id},
192 formname => 'letter',
196 if (!defined $template_file) {
197 $::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));
202 %result = SL::Template::LaTeX->parse_and_create_pdf(
208 formname => 'letter',
209 language => SL::DB::Language->new,
211 format => $::form->{format},
212 media => $::form->{media},
213 printer => SL::DB::Manager::Printer->find_by_or_create(id => $::form->{printer_id} || undef),
214 today => DateTime->today,
218 die $result{error} if $result{error};
220 $::form->{type} = 'letter';
221 $::form->{formname} = 'letter';
222 $::form->{letternumber} = $letter->letternumber;
223 my $attachment_name = $::form->generate_attachment_filename;
225 if ($::instance_conf->get_webdav_documents) {
226 my $webdav_file = SL::Webdav::File->new(
227 filename => $attachment_name,
228 webdav => SL::Webdav->new(
230 number => $letter->letternumber,
234 $webdav_file->store(file => $result{file_name});
237 # set some form defaults for printing webdav copy variables
238 if ( $::form->{media} eq 'email') {
239 my $mail = Mailer->new;
240 my $signature = $::myconfig{signature};
241 $mail->{$_} = $params{email}->{$_} for qw(to cc subject message bcc);
242 $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
243 $mail->{attachments} = [{ filename => $result{file_name},
244 name => $params{email}->{attachment_filename} }];
245 $mail->{message} .= "\n-- \n$signature";
246 $mail->{message} =~ s/\r//g;
249 unlink $result{file_name};
251 flash_later('info', t8('The email has been sent.'));
252 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
257 if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
258 $self->send_file($result{file_name}, name => $attachment_name);
259 unlink $result{file_name};
264 my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
265 $printer->print_document(
266 copies => $::form->{copies},
267 file_name => $result{file_name},
270 unlink $result{file_name};
272 flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
273 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
276 unlink $result{file_name} if $result{file_name};
277 $::form->error(t8("Creating the PDF failed:") . " " . $@);
282 my ($self, $name_selected) = @_;
285 letter => $self->_update,
289 sub action_skip_draft {
291 $self->action_add(skip_drafts => 1);
294 sub action_delete_drafts {
297 my @ids = @{ $::form->{ids} || [] };
298 SL::DB::Manager::LetterDraft->delete_all(where => [ id => \@ids ]) if @ids;
300 $self->action_add(skip_drafts => 1);
303 sub action_edit_email {
306 my $letter = $self->_update;
307 $self->export_letter_to_form($letter);
309 $::form->{formname} = "letter";
310 $::form->{type} = "letter";
311 $::form->{letternumber} = $self->letter->letternumber;
314 my $value = $letter->$_;
315 $value = $value->to_kivitendo if ref($_) =~ m{Date};
317 { name => "letter.$_", value => $value }
318 } ($letter->meta->columns);
321 script => 'controller.pl',
322 title => t8('Send letter via e-mail'),
323 email => $letter->contact ? $letter->contact->cp_email : '',
324 subject => $::form->generate_email_subject,
325 a_filename => $::form->generate_attachment_filename,
326 action => 'Letter/send_email',
328 SHOW_BCC => $::auth->assert('email_bcc', 'may fail'),
331 $self->render('generic/edit_email', %vars);
334 sub action_send_email {
337 $::form->{media} = 'email';
338 $self->action_print_letter(
340 to => $::form->{email},
341 map { ($_ => $::form->{$_}) } qw(cc bcc subject attachment_filename message)
349 my ($self, %params) = @_;
351 $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
353 my $letter = $self->letter;
355 $params{title} ||= t8('Edit Letter');
357 $::form->{type} = 'letter'; # needed for print_options
358 $::form->{vc} = $letter->is_sales ? 'customer' : 'vendor'; # needs to be for _get_contacts...
360 $::request->layout->add_javascripts('customer_or_vendor_selection.js');
361 $::request->layout->add_javascripts('edit_part_window.js');
363 $::form->{language_id} ||= $params{language_id};
364 $::form->{languages} ||= SL::DB::Manager::Language->get_all_sorted;
365 $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
367 $self->render('letter/edit',
369 TCF => [ map { key => $_, value => t8(ucfirst $_) }, TEXT_CREATED_FOR_VALUES() ],
370 PCF => [ map { key => $_, value => t8(ucfirst $_) }, PAGE_CREATED_FOR_VALUES() ],
372 employees => $self->all_employees,
373 print_options => SL::Helper::PrintOptions->get_print_options (
374 options => { no_postscript => 1,
375 no_opendocument => 1,
383 my ($self, %params) = @_;
385 my $letter = $self->letter;
388 $self->set_greetings;
396 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
397 $self->{report} = $report;
399 my @columns = qw(date subject letternumber customer_id vendor_id contact date);
400 my @sortable = qw(date subject letternumber customer_id vendor_id contact date);
403 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
404 subject => { text => t8('Subject'), sub => sub { $_[0]->subject },
405 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
406 letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
407 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
408 customer_id => { text => t8('Customer'), sub => sub { SL::DB::Manager::Customer->find_by_or_create(id => $_[0]->customer_id)->displayable_name }, visible => $self->is_sales },
409 vendor_id => { text => t8('Vendor'), sub => sub { SL::DB::Manager::Vendor->find_by_or_create(id => $_[0]->vendor_id)->displayable_name }, visible => !$self->is_sales},
410 contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
413 $column_defs{$_}{text} = $sort_columns{$_} for keys %column_defs;
415 $report->set_options(
416 std_column_visibility => 1,
417 controller_class => 'Letter',
418 output_format => 'HTML',
419 top_info_text => t8('Letters'),
420 title => t8('Letters'),
421 allow_pdf_export => 1,
422 allow_csv_export => 1,
425 $report->set_columns(%column_defs);
426 $report->set_column_order(@columns);
427 $report->set_export_options(qw(list filter is_sales));
428 $report->set_options_from_form;
430 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
431 $self->models->add_additional_url_params(is_sales => $self->is_sales);
432 $self->models->finalize;
433 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
435 $report->set_options(
436 raw_top_info_text => $self->render('letter/report_top', { output => 0 }),
437 raw_bottom_info_text => $self->render('letter/report_bottom', { output => 0 }, models => $self->models),
438 attachment_basename => t8('letters_list') . strftime('_%Y%m%d', localtime time),
442 sub make_filter_summary {
445 my $filter = $::form->{filter} || {};
448 my $employee = $filter->{employee_id} ? SL::DB::Employee->new(id => $filter->{employee_id})->load->name : '';
449 my $salesman = $filter->{salesman_id} ? SL::DB::Employee->new(id => $filter->{salesman_id})->load->name : '';
452 [ $filter->{"letternumber:substr::ilike"}, t8('Number') ],
453 [ $filter->{"subject:substr::ilike"}, t8('Subject') ],
454 [ $filter->{"body:substr::ilike"}, t8('Body') ],
455 [ $filter->{"date:date::ge"}, t8('From Date') ],
456 [ $filter->{"date:date::le"}, t8('To Date') ],
457 [ $employee, t8('Employee') ],
458 [ $salesman, t8('Salesman') ],
463 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
466 push @filter_strings, $_ if $_;
469 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
472 $self->{filter_summary} = join ', ', @filter_strings;
475 sub load_letter_draft {
476 my ($self, %params) = @_;
478 return 0 if $params{skip_drafts};
480 my $letter_drafts = SL::DB::Manager::LetterDraft->get_all(
482 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
486 return unless @$letter_drafts;
488 $self->render('letter/load_drafts',
489 title => t8('Letter Draft'),
490 LETTER_DRAFTS => $letter_drafts,
498 my $letter = $self->letter;
500 return unless $letter;
501 return if $letter->date;
503 $letter->date(DateTime->today)
507 my ($self, $letter) = @_;
509 $letter ||= $self->letter;
513 if (!$letter->subject) {
514 flash('error', t8('The subject is missing.'));
517 if (!$letter->body) {
518 flash('error', t8('The body is missing.'));
521 if (!$letter->employee_id) {
522 flash('error', t8('The employee is missing.'));
530 my ($self, $letter) = @_;
532 $letter ||= $self->letter;
534 return if $letter->letternumber;
536 $letter->letternumber(SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique);
541 my $letter = $self->letter;
543 return unless $letter;
544 return if $letter->greeting;
546 $letter->greeting(t8('Dear Sir or Madam,'));
549 sub export_letter_to_form {
550 my ($self, $letter) = @_;
553 $letter ||= $self->letter;
555 for ($letter->meta->columns) {
556 if ((ref $_) =~ /Date/i) {
557 $::form->{$_->name} = $letter->$_->to_kivitendo;
559 $::form->{$_->name} = $letter->$_;
567 my $letter = SL::DB::Manager::Letter->find_by_or_create(id => $::form->{letter}{id} || 0)
568 ->assign_attributes(%{ $::form->{letter} });
570 if ($letter->cp_id) {
571 # $letter->customer_vendor_id($letter->contact->cp_cv_id);
572 # contacts don't have language_id yet
573 # $letter->greeting(GenericTranslations->get(
574 # translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
575 # language_id => $letter->contact->language_id,
576 # allow_fallback => 1
580 $self->is_sales($letter->is_sales) if $letter->id;
588 SL::Controller::Helper::GetModels->new(
592 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
594 sorted => \%sort_columns,
595 with_objects => [ 'contact', 'salesman', 'employee' ],
599 sub init_all_employees {
600 SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
603 sub init_webdav_objects {
606 return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
608 my $webdav = SL::Webdav->new(
610 number => $self->letter->letternumber,
613 my $webdav_path = $webdav->webdav_path;
614 my @all_objects = $webdav->get_all_objects;
617 +{ name => $_->filename,
619 link => File::Spec->catdir($webdav_path, $_->filename),
625 die 'is_sales must be set' unless defined $::form->{is_sales};
629 sub check_auth_edit {
630 $::auth->assert('sales_letter_edit');
633 sub check_auth_report {
634 $::auth->assert('sales_letter_report');
645 SL::Controller::Letter - Letters CRUD and printing
649 Simple letter CRUD controller with drafting capabilities.
651 copy to webdav is crap
655 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>