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;
18 use SL::Helper::Flash qw(flash flash_later);
19 use SL::Helper::CreatePDF;
20 use SL::Helper::PrintOptions;
21 use SL::Locale::String qw(t8);
24 use SL::Presenter::Tag qw(select_tag);
25 use SL::ReportGenerator;
29 use Rose::Object::MakeMethods::Generic (
30 'scalar --get_set_init' => [ qw(letter all_employees models webdav_objects is_sales) ],
33 __PACKAGE__->run_before('check_auth_edit');
34 __PACKAGE__->run_before('check_auth_report', only => [ qw(list) ]);
36 use constant TEXT_CREATED_FOR_VALUES => (qw(presskit fax letter));
37 use constant PAGE_CREATED_FOR_VALUES => (qw(sketch 1 2));
41 subject => t8('Subject'),
42 letternumber => t8('Letternumber'),
43 customer_id => t8('Customer'),
44 vendor_id => t8('Vendor'),
45 contact => t8('Contact'),
51 my ($self, %params) = @_;
53 return if $self->load_letter_draft(%params);
55 $self->letter->employee_id(SL::DB::Manager::Employee->current->id);
56 $self->letter->salesman_id(SL::DB::Manager::Employee->current->id);
59 title => t8('Add Letter'),
60 language_id => $params{language_id},
65 my ($self, %params) = @_;
67 return $self->action_add
68 unless $::form->{letter} || $::form->{draft};
70 if ($::form->{draft}) {
71 $self->letter(SL::DB::Letter->new_from_draft($::form->{draft}{id}));
72 $self->is_sales($self->letter->is_sales);
76 title => t8('Edit Letter'),
81 my ($self, %params) = @_;
83 my $letter = $self->_update;
85 if (!$self->check_letter($letter)) {
86 return $self->_display;
92 flash('error', t8('There was an error saving the letter'));
93 return $self->_display;
96 flash('info', t8('Letter saved!'));
101 sub action_update_contacts {
104 my $letter = $self->letter;
106 if (!$self->letter->has_customer_vendor) {
110 select_tag('letter.cp_id', [], value_key => 'cp_id', title_key => 'full_name')
115 my $contacts = $letter->customer_vendor->contacts;
118 if ( $letter->contact
119 && $letter->contact->cp_cv_id
120 && $letter->contact->cp_cv_id == $letter->customer_vendor_id) {
121 $default = $letter->contact->cp_id;
129 select_tag('letter.cp_id', $contacts, default => $default, value_key => 'cp_id', title_key => 'full_name')
134 sub action_save_letter_draft {
135 my ($self, %params) = @_;
139 my $letter_draft = SL::DB::LetterDraft->new_from_letter($self->_update);
141 if (!$letter_draft->save) {
142 flash('error', t8('There was an error saving the letter draft'));
143 return $self->_display;
146 flash('info', t8('Draft for this Letter saved!'));
152 my ($self, %params) = @_;
154 if (!$self->letter->delete) {
155 flash('error', t8('An error occurred. Letter could not be deleted.'));
156 return $self->action_update;
159 flash_later('info', t8('Letter deleted'));
160 $self->redirect_to(action => 'list');
163 sub action_delete_letter_drafts {
164 my ($self, %params) = @_;
166 my @ids = grep { /^checked_(.*)/ && $::form->{$_} } keys %$::form;
168 SL::DB::Manager::LetterDraft->delete_all(query => [ ids => \@ids ]) if @ids;
170 $self->redirect_to(action => 'add');
174 my ($self, %params) = @_;
176 $self->setup_list_action_bar;
177 $self->make_filter_summary;
178 $self->prepare_report;
180 my $letters = $self->models->get;
181 $self->report_generator_list_objects(report => $self->{report}, objects => $letters);
185 sub action_print_letter {
186 my ($self, %params) = @_;
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::Manager::Language->find_by_or_create(id => $::form->{language_id}*1),
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 if ($::instance_conf->get_doc_storage) {
240 my %save_params = (object_id => $letter->id,
241 object_type => 'letter',
242 mime_type => 'application/pdf',
244 file_type => 'document',
245 file_name => $attachment_name,
246 file_path => $result{file_name});
247 SL::File->save(%save_params);
250 # set some form defaults for printing webdav copy variables
251 if ( $::form->{media} eq 'email') {
252 my $mail = Mailer->new;
253 my $signature = $::myconfig{signature};
254 $mail->{$_} = $params{email}->{$_} for qw(to cc subject message bcc);
255 $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
256 $mail->{attachments} = [{ path => $result{file_name},
257 name => $params{email}->{attachment_filename} }];
258 $mail->{message} .= "\n-- \n$signature";
259 $mail->{message} =~ s/\r//g;
260 $mail->{record_id} = $letter->id;
262 unlink $result{file_name};
264 flash_later('info', t8('The email has been sent.'));
265 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
270 if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
271 $self->send_file($result{file_name}, name => $attachment_name);
272 unlink $result{file_name};
277 my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
278 $printer->print_document(
279 copies => $::form->{copies},
280 file_name => $result{file_name},
283 unlink $result{file_name};
285 flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
286 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
289 unlink $result{file_name} if $result{file_name};
290 $::form->error(t8("Creating the PDF failed:") . " " . $@);
295 my ($self, $name_selected) = @_;
298 letter => $self->_update,
302 sub action_skip_draft {
304 $self->action_add(skip_drafts => 1);
307 sub action_delete_drafts {
310 my @ids = @{ $::form->{ids} || [] };
311 SL::DB::Manager::LetterDraft->delete_all(where => [ id => \@ids ]) if @ids;
313 $self->action_add(skip_drafts => 1);
316 sub action_send_email {
319 $::form->{media} = 'email';
320 $self->action_print_letter(email => $::form->{email_form});
326 my ($self, %params) = @_;
328 $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery kivi.Letter kivi.SalesPurchase kivi.File);
330 my $letter = $self->letter;
332 $params{title} ||= t8('Edit Letter');
334 $::form->{type} = 'letter'; # needed for print_options
335 $::form->{vc} = $letter->is_sales ? 'customer' : 'vendor'; # needs to be for _get_contacts...
337 $::form->{language_id} ||= $params{language_id};
338 $::form->{languages} ||= SL::DB::Manager::Language->get_all_sorted;
339 $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
341 $self->setup_display_action_bar;
342 $self->render('letter/edit',
344 TCF => [ map { key => $_, value => t8(ucfirst $_) }, TEXT_CREATED_FOR_VALUES() ],
345 PCF => [ map { key => $_, value => t8(ucfirst $_) }, PAGE_CREATED_FOR_VALUES() ],
347 employees => $self->all_employees,
348 print_options => SL::Helper::PrintOptions->get_print_options (
349 options => { no_postscript => 1,
350 no_opendocument => 1,
360 my ($self, %params) = @_;
362 my $letter = $self->letter;
365 $self->set_greetings;
373 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
374 $self->{report} = $report;
376 my @columns = qw(date subject letternumber customer_id vendor_id contact date);
377 my @sortable = qw(date subject letternumber customer_id vendor_id contact date);
380 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
381 subject => { text => t8('Subject'), sub => sub { $_[0]->subject },
382 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
383 letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
384 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
385 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 },
386 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},
387 contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
390 $column_defs{$_}{text} = $sort_columns{$_} for keys %column_defs;
392 $report->set_options(
393 std_column_visibility => 1,
394 controller_class => 'Letter',
395 output_format => 'HTML',
396 title => t8('Letters'),
397 allow_pdf_export => 1,
398 allow_csv_export => 1,
401 $report->set_columns(%column_defs);
402 $report->set_column_order(@columns);
403 $report->set_export_options(qw(list filter is_sales));
404 $report->set_options_from_form;
406 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
407 $self->models->add_additional_url_params(is_sales => $self->is_sales);
408 $self->models->finalize;
409 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
411 $report->set_options(
412 raw_top_info_text => $self->render('letter/report_top', { output => 0 }),
413 raw_bottom_info_text => $self->render('letter/report_bottom', { output => 0 }, models => $self->models),
414 attachment_basename => t8('letters_list') . strftime('_%Y%m%d', localtime time),
418 sub make_filter_summary {
421 my $filter = $::form->{filter} || {};
424 my $employee = $filter->{employee_id} ? SL::DB::Employee->new(id => $filter->{employee_id})->load->name : '';
425 my $salesman = $filter->{salesman_id} ? SL::DB::Employee->new(id => $filter->{salesman_id})->load->name : '';
428 [ $filter->{"letternumber:substr::ilike"}, t8('Number') ],
429 [ $filter->{"subject:substr::ilike"}, t8('Subject') ],
430 [ $filter->{"body:substr::ilike"}, t8('Body') ],
431 [ $filter->{"date:date::ge"}, t8('From Date') ],
432 [ $filter->{"date:date::le"}, t8('To Date') ],
433 [ $employee, t8('Employee') ],
434 [ $salesman, t8('Salesman') ],
439 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
442 push @filter_strings, $_ if $_;
445 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
448 $self->{filter_summary} = join ', ', @filter_strings;
451 sub load_letter_draft {
452 my ($self, %params) = @_;
454 return 0 if $params{skip_drafts};
456 my $letter_drafts = SL::DB::Manager::LetterDraft->get_all(
458 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
462 return unless @$letter_drafts;
464 $self->setup_load_letter_draft_action_bar;
465 $self->render('letter/load_drafts',
466 title => t8('Letter Draft'),
467 LETTER_DRAFTS => $letter_drafts,
475 my $letter = $self->letter;
477 return unless $letter;
478 return if $letter->date;
480 $letter->date(DateTime->today)
484 my ($self, $letter) = @_;
486 $letter ||= $self->letter;
490 if (!$letter->subject) {
491 flash('error', t8('The subject is missing.'));
494 if (!$letter->body) {
495 flash('error', t8('The body is missing.'));
498 if (!$letter->employee_id) {
499 flash('error', t8('The employee is missing.'));
507 my ($self, $letter) = @_;
509 $letter ||= $self->letter;
511 return if $letter->letternumber;
513 $letter->letternumber(SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique);
518 my $letter = $self->letter;
520 return unless $letter;
521 return if $letter->greeting;
523 $letter->greeting(t8('Dear Sir or Madam,'));
529 my $letter = SL::DB::Manager::Letter->find_by_or_create(id => $::form->{letter}{id} || 0)
530 ->assign_attributes(%{ $::form->{letter} });
532 if ($letter->cp_id) {
533 # $letter->customer_vendor_id($letter->contact->cp_cv_id);
534 # contacts don't have language_id yet
535 # $letter->greeting(GenericTranslations->get(
536 # translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
537 # language_id => $letter->contact->language_id,
538 # allow_fallback => 1
542 $self->is_sales($letter->is_sales) if $letter->id;
550 SL::Controller::Helper::GetModels->new(
554 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
556 sorted => \%sort_columns,
557 with_objects => [ 'contact', 'salesman', 'employee' ],
561 sub init_all_employees {
562 SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
565 sub init_webdav_objects {
568 return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
570 my $webdav = SL::Webdav->new(
572 number => $self->letter->letternumber,
575 my @all_objects = $webdav->get_all_objects;
578 +{ name => $_->filename,
580 link => File::Spec->catfile($_->full_filedescriptor),
586 die 'is_sales must be set' unless defined $::form->{is_sales};
590 sub check_auth_edit {
591 $::form->{is_sales} ? $::auth->assert('sales_letter_edit')
592 : $::auth->assert('purchase_letter_edit');
595 sub check_auth_report {
596 $::form->{is_sales} ? $::auth->assert('sales_letter_report')
597 : $::auth->assert('purchase_letter_report');
600 sub setup_load_letter_draft_action_bar {
601 my ($self, %params) = @_;
603 for my $bar ($::request->layout->get('actionbar')) {
607 link => $self->url_for(action => 'skip_draft', is_sales => $self->is_sales),
608 accesskey => 'enter',
612 submit => [ '#form', { action => 'delete_drafts' } ],
613 checks => [ [ 'kivi.check_if_entries_selected', '[name="ids[+]"]' ] ],
614 confirm => t8('Do you really want to delete this draft?'),
620 sub setup_display_action_bar {
621 my ($self, %params) = @_;
623 my $vc = $self->is_sales ? 'customer' : 'vendor'; # needed for show_email_dialog
625 for my $bar ($::request->layout->get('actionbar')) {
629 submit => [ '#form', { action => 'Letter/update' } ],
630 accesskey => 'enter',
636 submit => [ '#form', { action => 'Letter/save' } ],
640 submit => [ '#form', { action => 'Letter/save_letter_draft' } ],
642 ], # end of combobox "Save"
646 submit => [ '#form', { action => 'Letter/delete' } ],
647 confirm => t8('Are you sure you want to delete this letter?'),
648 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
652 action => [ t8('Export') ],
655 call => [ 'kivi.SalesPurchase.show_print_dialog', 'Letter/print_letter' ],
656 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
660 call => [ 'kivi.SalesPurchase.show_email_dialog', 'Letter/send_email', $vc, '#letter_' . $vc . '_id' ],
661 disabled => !$self->letter->id ? t8('The object has not been saved yet.') : undef,
668 sub setup_list_action_bar {
669 my ($self, %params) = @_;
671 for my $bar ($::request->layout->get('actionbar')) {
675 submit => [ '#search_form', { action => 'Letter/list' } ],
676 accesskey => 'enter',
690 SL::Controller::Letter - Letters CRUD and printing
694 Simple letter CRUD controller with drafting capabilities.
696 copy to webdav is crap
700 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>