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::Presenter::Tag qw(select_tag);
24 use SL::ReportGenerator;
28 use Rose::Object::MakeMethods::Generic (
29 'scalar --get_set_init' => [ qw(letter all_employees models webdav_objects is_sales) ],
32 __PACKAGE__->run_before('check_auth_edit');
33 __PACKAGE__->run_before('check_auth_report', only => [ qw(list) ]);
35 use constant TEXT_CREATED_FOR_VALUES => (qw(presskit fax letter));
36 use constant PAGE_CREATED_FOR_VALUES => (qw(sketch 1 2));
40 subject => t8('Subject'),
41 letternumber => t8('Letternumber'),
42 customer_id => t8('Customer'),
43 vendor_id => t8('Vendor'),
44 contact => t8('Contact'),
50 my ($self, %params) = @_;
52 return if $self->load_letter_draft(%params);
54 $self->letter->employee_id(SL::DB::Manager::Employee->current->id);
55 $self->letter->salesman_id(SL::DB::Manager::Employee->current->id);
58 title => t8('Add Letter'),
59 language_id => $params{language_id},
64 my ($self, %params) = @_;
66 return $self->action_add
67 unless $::form->{letter} || $::form->{draft};
69 if ($::form->{draft}) {
70 $self->letter(SL::DB::Letter->new_from_draft($::form->{draft}{id}));
71 $self->is_sales($self->letter->is_sales);
75 title => t8('Edit Letter'),
80 my ($self, %params) = @_;
82 my $letter = $self->_update;
84 if (!$self->check_letter($letter)) {
85 return $self->_display;
91 flash('error', t8('There was an error saving the letter'));
92 return $self->_display;
95 flash('info', t8('Letter saved!'));
100 sub action_update_contacts {
103 my $letter = $self->letter;
105 if (!$self->letter->has_customer_vendor) {
109 SL::Presenter->get->select_tag('letter.cp_id', [], value_key => 'cp_id', title_key => 'full_name')
114 my $contacts = $letter->customer_vendor->contacts;
117 if ( $letter->contact
118 && $letter->contact->cp_cv_id
119 && $letter->contact->cp_cv_id == $letter->customer_vendor_id) {
120 $default = $letter->contact->cp_id;
128 select_tag('letter.cp_id', $contacts, default => $default, value_key => 'cp_id', title_key => 'full_name')
133 sub action_save_letter_draft {
134 my ($self, %params) = @_;
138 my $letter_draft = SL::DB::LetterDraft->new_from_letter($self->_update);
140 if (!$letter_draft->save) {
141 flash('error', t8('There was an error saving the letter draft'));
142 return $self->_display;
145 flash('info', t8('Draft for this Letter saved!'));
151 my ($self, %params) = @_;
153 if (!$self->letter->delete) {
154 flash('error', t8('An error occured. Letter could not be deleted.'));
155 return $self->action_update;
158 flash_later('info', t8('Letter deleted'));
159 $self->redirect_to(action => 'list');
162 sub action_delete_letter_drafts {
163 my ($self, %params) = @_;
165 my @ids = grep { /^checked_(.*)/ && $::form->{$_} } keys %$::form;
167 SL::DB::Manager::LetterDraft->delete_all(query => [ ids => \@ids ]) if @ids;
169 $self->redirect_to(action => 'add');
173 my ($self, %params) = @_;
175 $self->setup_list_action_bar;
176 $self->make_filter_summary;
177 $self->prepare_report;
179 my $letters = $self->models->get;
180 $self->report_generator_list_objects(report => $self->{report}, objects => $letters);
184 sub action_print_letter {
185 my ($self, %params) = @_;
187 my $display_form = $::form->{display_form} || "display_form";
188 my $letter = $self->_update;
190 my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
192 printer_id => $::form->{printer_id},
193 language_id => $::form->{language_id},
194 formname => 'letter',
198 if (!defined $template_file) {
199 $::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));
204 %result = SL::Template::LaTeX->parse_and_create_pdf(
210 formname => 'letter',
211 language => SL::DB::Language->new,
213 format => $::form->{format},
214 media => $::form->{media},
215 printer => SL::DB::Manager::Printer->find_by_or_create(id => $::form->{printer_id} || undef),
216 today => DateTime->today,
220 die $result{error} if $result{error};
222 $::form->{type} = 'letter';
223 $::form->{formname} = 'letter';
224 $::form->{letternumber} = $letter->letternumber;
225 my $attachment_name = $::form->generate_attachment_filename;
227 if ($::instance_conf->get_webdav_documents) {
228 my $webdav_file = SL::Webdav::File->new(
229 filename => $attachment_name,
230 webdav => SL::Webdav->new(
232 number => $letter->letternumber,
236 $webdav_file->store(file => $result{file_name});
239 # set some form defaults for printing webdav copy variables
240 if ( $::form->{media} eq 'email') {
241 my $mail = Mailer->new;
242 my $signature = $::myconfig{signature};
243 $mail->{$_} = $params{email}->{$_} for qw(to cc subject message bcc);
244 $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
245 $mail->{attachments} = [{ path => $result{file_name},
246 name => $params{email}->{attachment_filename} }];
247 $mail->{message} .= "\n-- \n$signature";
248 $mail->{message} =~ s/\r//g;
251 unlink $result{file_name};
253 flash_later('info', t8('The email has been sent.'));
254 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
259 if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
260 $self->send_file($result{file_name}, name => $attachment_name);
261 unlink $result{file_name};
266 my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
267 $printer->print_document(
268 copies => $::form->{copies},
269 file_name => $result{file_name},
272 unlink $result{file_name};
274 flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
275 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
278 unlink $result{file_name} if $result{file_name};
279 $::form->error(t8("Creating the PDF failed:") . " " . $@);
284 my ($self, $name_selected) = @_;
287 letter => $self->_update,
291 sub action_skip_draft {
293 $self->action_add(skip_drafts => 1);
296 sub action_delete_drafts {
299 my @ids = @{ $::form->{ids} || [] };
300 SL::DB::Manager::LetterDraft->delete_all(where => [ id => \@ids ]) if @ids;
302 $self->action_add(skip_drafts => 1);
305 sub action_send_email {
308 $::form->{media} = 'email';
309 $self->action_print_letter(email => $::form->{email_form});
315 my ($self, %params) = @_;
317 $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery kivi.Letter);
319 my $letter = $self->letter;
321 $params{title} ||= t8('Edit Letter');
323 $::form->{type} = 'letter'; # needed for print_options
324 $::form->{vc} = $letter->is_sales ? 'customer' : 'vendor'; # needs to be for _get_contacts...
326 $::request->layout->add_javascripts('customer_or_vendor_selection.js');
327 $::request->layout->add_javascripts('edit_part_window.js');
329 $::form->{language_id} ||= $params{language_id};
330 $::form->{languages} ||= SL::DB::Manager::Language->get_all_sorted;
331 $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
333 $self->setup_display_action_bar;
334 $self->render('letter/edit',
336 TCF => [ map { key => $_, value => t8(ucfirst $_) }, TEXT_CREATED_FOR_VALUES() ],
337 PCF => [ map { key => $_, value => t8(ucfirst $_) }, PAGE_CREATED_FOR_VALUES() ],
339 employees => $self->all_employees,
340 print_options => SL::Helper::PrintOptions->get_print_options (
341 options => { no_postscript => 1,
342 no_opendocument => 1,
352 my ($self, %params) = @_;
354 my $letter = $self->letter;
357 $self->set_greetings;
365 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
366 $self->{report} = $report;
368 my @columns = qw(date subject letternumber customer_id vendor_id contact date);
369 my @sortable = qw(date subject letternumber customer_id vendor_id contact date);
372 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
373 subject => { text => t8('Subject'), sub => sub { $_[0]->subject },
374 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
375 letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
376 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
377 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 },
378 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},
379 contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
382 $column_defs{$_}{text} = $sort_columns{$_} for keys %column_defs;
384 $report->set_options(
385 std_column_visibility => 1,
386 controller_class => 'Letter',
387 output_format => 'HTML',
388 title => t8('Letters'),
389 allow_pdf_export => 1,
390 allow_csv_export => 1,
393 $report->set_columns(%column_defs);
394 $report->set_column_order(@columns);
395 $report->set_export_options(qw(list filter is_sales));
396 $report->set_options_from_form;
398 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
399 $self->models->add_additional_url_params(is_sales => $self->is_sales);
400 $self->models->finalize;
401 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
403 $report->set_options(
404 raw_top_info_text => $self->render('letter/report_top', { output => 0 }),
405 raw_bottom_info_text => $self->render('letter/report_bottom', { output => 0 }, models => $self->models),
406 attachment_basename => t8('letters_list') . strftime('_%Y%m%d', localtime time),
410 sub make_filter_summary {
413 my $filter = $::form->{filter} || {};
416 my $employee = $filter->{employee_id} ? SL::DB::Employee->new(id => $filter->{employee_id})->load->name : '';
417 my $salesman = $filter->{salesman_id} ? SL::DB::Employee->new(id => $filter->{salesman_id})->load->name : '';
420 [ $filter->{"letternumber:substr::ilike"}, t8('Number') ],
421 [ $filter->{"subject:substr::ilike"}, t8('Subject') ],
422 [ $filter->{"body:substr::ilike"}, t8('Body') ],
423 [ $filter->{"date:date::ge"}, t8('From Date') ],
424 [ $filter->{"date:date::le"}, t8('To Date') ],
425 [ $employee, t8('Employee') ],
426 [ $salesman, t8('Salesman') ],
431 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
434 push @filter_strings, $_ if $_;
437 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
440 $self->{filter_summary} = join ', ', @filter_strings;
443 sub load_letter_draft {
444 my ($self, %params) = @_;
446 return 0 if $params{skip_drafts};
448 my $letter_drafts = SL::DB::Manager::LetterDraft->get_all(
450 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
454 return unless @$letter_drafts;
456 $self->setup_load_letter_draft_action_bar;
457 $self->render('letter/load_drafts',
458 title => t8('Letter Draft'),
459 LETTER_DRAFTS => $letter_drafts,
467 my $letter = $self->letter;
469 return unless $letter;
470 return if $letter->date;
472 $letter->date(DateTime->today)
476 my ($self, $letter) = @_;
478 $letter ||= $self->letter;
482 if (!$letter->subject) {
483 flash('error', t8('The subject is missing.'));
486 if (!$letter->body) {
487 flash('error', t8('The body is missing.'));
490 if (!$letter->employee_id) {
491 flash('error', t8('The employee is missing.'));
499 my ($self, $letter) = @_;
501 $letter ||= $self->letter;
503 return if $letter->letternumber;
505 $letter->letternumber(SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique);
510 my $letter = $self->letter;
512 return unless $letter;
513 return if $letter->greeting;
515 $letter->greeting(t8('Dear Sir or Madam,'));
518 sub export_letter_to_form {
519 my ($self, $letter) = @_;
522 $letter ||= $self->letter;
524 for ($letter->meta->columns) {
525 if ((ref $_) =~ /Date/i) {
526 $::form->{$_->name} = $letter->$_->to_kivitendo;
528 $::form->{$_->name} = $letter->$_;
536 my $letter = SL::DB::Manager::Letter->find_by_or_create(id => $::form->{letter}{id} || 0)
537 ->assign_attributes(%{ $::form->{letter} });
539 if ($letter->cp_id) {
540 # $letter->customer_vendor_id($letter->contact->cp_cv_id);
541 # contacts don't have language_id yet
542 # $letter->greeting(GenericTranslations->get(
543 # translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
544 # language_id => $letter->contact->language_id,
545 # allow_fallback => 1
549 $self->is_sales($letter->is_sales) if $letter->id;
557 SL::Controller::Helper::GetModels->new(
561 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
563 sorted => \%sort_columns,
564 with_objects => [ 'contact', 'salesman', 'employee' ],
568 sub init_all_employees {
569 SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
572 sub init_webdav_objects {
575 return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
577 my $webdav = SL::Webdav->new(
579 number => $self->letter->letternumber,
582 my @all_objects = $webdav->get_all_objects;
585 +{ name => $_->filename,
587 link => File::Spec->catfile($_->full_filedescriptor),
593 die 'is_sales must be set' unless defined $::form->{is_sales};
597 sub check_auth_edit {
598 $::auth->assert('sales_letter_edit');
601 sub check_auth_report {
602 $::auth->assert('sales_letter_report');
605 sub setup_load_letter_draft_action_bar {
606 my ($self, %params) = @_;
608 for my $bar ($::request->layout->get('actionbar')) {
612 link => $self->url_for(action => 'skip_draft', is_sales => $self->is_sales),
613 accesskey => 'enter',
617 submit => [ '#form', { action => 'delete_drafts' } ],
618 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[+]"]' ] ],
619 confirm => t8('Do you really want to delete this draft?'),
625 sub setup_display_action_bar {
626 my ($self, %params) = @_;
628 for my $bar ($::request->layout->get('actionbar')) {
632 submit => [ '#form', { action => 'Letter/update' } ],
633 accesskey => 'enter',
639 submit => [ '#form', { action => 'Letter/save' } ],
643 submit => [ '#form', { action => 'Letter/save_letter_draft' } ],
645 ], # end of combobox "Save"
649 submit => [ '#form', { action => 'Letter/delete' } ],
650 confirm => t8('Are you sure you want to delete this letter?'),
651 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
655 action => [ t8('Export') ],
658 call => [ 'kivi.SalesPurchase.show_print_dialog', 'Letter/print_letter' ],
659 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
663 call => [ 'kivi.SalesPurchase.show_email_dialog', 'Letter/send_email' ],
664 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
671 sub setup_list_action_bar {
672 my ($self, %params) = @_;
674 for my $bar ($::request->layout->get('actionbar')) {
678 submit => [ '#search_form', { action => 'Letter/list' } ],
679 accesskey => 'enter',
693 SL::Controller::Letter - Letters CRUD and printing
697 Simple letter CRUD controller with drafting capabilities.
699 copy to webdav is crap
703 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>