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 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 occurred. 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;
249 $mail->{record_id} = $letter->id;
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 $::form->{language_id} ||= $params{language_id};
327 $::form->{languages} ||= SL::DB::Manager::Language->get_all_sorted;
328 $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
330 $self->setup_display_action_bar;
331 $self->render('letter/edit',
333 TCF => [ map { key => $_, value => t8(ucfirst $_) }, TEXT_CREATED_FOR_VALUES() ],
334 PCF => [ map { key => $_, value => t8(ucfirst $_) }, PAGE_CREATED_FOR_VALUES() ],
336 employees => $self->all_employees,
337 print_options => SL::Helper::PrintOptions->get_print_options (
338 options => { no_postscript => 1,
339 no_opendocument => 1,
349 my ($self, %params) = @_;
351 my $letter = $self->letter;
354 $self->set_greetings;
362 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
363 $self->{report} = $report;
365 my @columns = qw(date subject letternumber customer_id vendor_id contact date);
366 my @sortable = qw(date subject letternumber customer_id vendor_id contact date);
369 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
370 subject => { text => t8('Subject'), sub => sub { $_[0]->subject },
371 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
372 letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
373 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
374 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 },
375 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},
376 contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
379 $column_defs{$_}{text} = $sort_columns{$_} for keys %column_defs;
381 $report->set_options(
382 std_column_visibility => 1,
383 controller_class => 'Letter',
384 output_format => 'HTML',
385 title => t8('Letters'),
386 allow_pdf_export => 1,
387 allow_csv_export => 1,
390 $report->set_columns(%column_defs);
391 $report->set_column_order(@columns);
392 $report->set_export_options(qw(list filter is_sales));
393 $report->set_options_from_form;
395 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
396 $self->models->add_additional_url_params(is_sales => $self->is_sales);
397 $self->models->finalize;
398 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
400 $report->set_options(
401 raw_top_info_text => $self->render('letter/report_top', { output => 0 }),
402 raw_bottom_info_text => $self->render('letter/report_bottom', { output => 0 }, models => $self->models),
403 attachment_basename => t8('letters_list') . strftime('_%Y%m%d', localtime time),
407 sub make_filter_summary {
410 my $filter = $::form->{filter} || {};
413 my $employee = $filter->{employee_id} ? SL::DB::Employee->new(id => $filter->{employee_id})->load->name : '';
414 my $salesman = $filter->{salesman_id} ? SL::DB::Employee->new(id => $filter->{salesman_id})->load->name : '';
417 [ $filter->{"letternumber:substr::ilike"}, t8('Number') ],
418 [ $filter->{"subject:substr::ilike"}, t8('Subject') ],
419 [ $filter->{"body:substr::ilike"}, t8('Body') ],
420 [ $filter->{"date:date::ge"}, t8('From Date') ],
421 [ $filter->{"date:date::le"}, t8('To Date') ],
422 [ $employee, t8('Employee') ],
423 [ $salesman, t8('Salesman') ],
428 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
431 push @filter_strings, $_ if $_;
434 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
437 $self->{filter_summary} = join ', ', @filter_strings;
440 sub load_letter_draft {
441 my ($self, %params) = @_;
443 return 0 if $params{skip_drafts};
445 my $letter_drafts = SL::DB::Manager::LetterDraft->get_all(
447 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
451 return unless @$letter_drafts;
453 $self->setup_load_letter_draft_action_bar;
454 $self->render('letter/load_drafts',
455 title => t8('Letter Draft'),
456 LETTER_DRAFTS => $letter_drafts,
464 my $letter = $self->letter;
466 return unless $letter;
467 return if $letter->date;
469 $letter->date(DateTime->today)
473 my ($self, $letter) = @_;
475 $letter ||= $self->letter;
479 if (!$letter->subject) {
480 flash('error', t8('The subject is missing.'));
483 if (!$letter->body) {
484 flash('error', t8('The body is missing.'));
487 if (!$letter->employee_id) {
488 flash('error', t8('The employee is missing.'));
496 my ($self, $letter) = @_;
498 $letter ||= $self->letter;
500 return if $letter->letternumber;
502 $letter->letternumber(SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique);
507 my $letter = $self->letter;
509 return unless $letter;
510 return if $letter->greeting;
512 $letter->greeting(t8('Dear Sir or Madam,'));
515 sub export_letter_to_form {
516 my ($self, $letter) = @_;
519 $letter ||= $self->letter;
521 for ($letter->meta->columns) {
522 if ((ref $_) =~ /Date/i) {
523 $::form->{$_->name} = $letter->$_->to_kivitendo;
525 $::form->{$_->name} = $letter->$_;
533 my $letter = SL::DB::Manager::Letter->find_by_or_create(id => $::form->{letter}{id} || 0)
534 ->assign_attributes(%{ $::form->{letter} });
536 if ($letter->cp_id) {
537 # $letter->customer_vendor_id($letter->contact->cp_cv_id);
538 # contacts don't have language_id yet
539 # $letter->greeting(GenericTranslations->get(
540 # translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
541 # language_id => $letter->contact->language_id,
542 # allow_fallback => 1
546 $self->is_sales($letter->is_sales) if $letter->id;
554 SL::Controller::Helper::GetModels->new(
558 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
560 sorted => \%sort_columns,
561 with_objects => [ 'contact', 'salesman', 'employee' ],
565 sub init_all_employees {
566 SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
569 sub init_webdav_objects {
572 return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
574 my $webdav = SL::Webdav->new(
576 number => $self->letter->letternumber,
579 my @all_objects = $webdav->get_all_objects;
582 +{ name => $_->filename,
584 link => File::Spec->catfile($_->full_filedescriptor),
590 die 'is_sales must be set' unless defined $::form->{is_sales};
594 sub check_auth_edit {
595 $::form->{is_sales} ? $::auth->assert('sales_letter_edit')
596 : $::auth->assert('purchase_letter_edit');
599 sub check_auth_report {
600 $::form->{is_sales} ? $::auth->assert('sales_letter_report')
601 : $::auth->assert('purchase_letter_report');
604 sub setup_load_letter_draft_action_bar {
605 my ($self, %params) = @_;
607 for my $bar ($::request->layout->get('actionbar')) {
611 link => $self->url_for(action => 'skip_draft', is_sales => $self->is_sales),
612 accesskey => 'enter',
616 submit => [ '#form', { action => 'delete_drafts' } ],
617 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[+]"]' ] ],
618 confirm => t8('Do you really want to delete this draft?'),
624 sub setup_display_action_bar {
625 my ($self, %params) = @_;
627 for my $bar ($::request->layout->get('actionbar')) {
631 submit => [ '#form', { action => 'Letter/update' } ],
632 accesskey => 'enter',
638 submit => [ '#form', { action => 'Letter/save' } ],
642 submit => [ '#form', { action => 'Letter/save_letter_draft' } ],
644 ], # end of combobox "Save"
648 submit => [ '#form', { action => 'Letter/delete' } ],
649 confirm => t8('Are you sure you want to delete this letter?'),
650 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
654 action => [ t8('Export') ],
657 call => [ 'kivi.SalesPurchase.show_print_dialog', 'Letter/print_letter' ],
658 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
662 call => [ 'kivi.SalesPurchase.show_email_dialog', 'Letter/send_email' ],
663 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
670 sub setup_list_action_bar {
671 my ($self, %params) = @_;
673 for my $bar ($::request->layout->get('actionbar')) {
677 submit => [ '#search_form', { action => 'Letter/list' } ],
678 accesskey => 'enter',
692 SL::Controller::Letter - Letters CRUD and printing
696 Simple letter CRUD controller with drafting capabilities.
698 copy to webdav is crap
702 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>