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->setup_list_action_bar;
175 $self->make_filter_summary;
176 $self->prepare_report;
178 my $letters = $self->models->get;
179 $self->report_generator_list_objects(report => $self->{report}, objects => $letters);
183 sub action_print_letter {
184 my ($self, %params) = @_;
186 my $display_form = $::form->{display_form} || "display_form";
187 my $letter = $self->_update;
189 my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
191 printer_id => $::form->{printer_id},
192 language_id => $::form->{language_id},
193 formname => 'letter',
197 if (!defined $template_file) {
198 $::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));
203 %result = SL::Template::LaTeX->parse_and_create_pdf(
209 formname => 'letter',
210 language => SL::DB::Language->new,
212 format => $::form->{format},
213 media => $::form->{media},
214 printer => SL::DB::Manager::Printer->find_by_or_create(id => $::form->{printer_id} || undef),
215 today => DateTime->today,
219 die $result{error} if $result{error};
221 $::form->{type} = 'letter';
222 $::form->{formname} = 'letter';
223 $::form->{letternumber} = $letter->letternumber;
224 my $attachment_name = $::form->generate_attachment_filename;
226 if ($::instance_conf->get_webdav_documents) {
227 my $webdav_file = SL::Webdav::File->new(
228 filename => $attachment_name,
229 webdav => SL::Webdav->new(
231 number => $letter->letternumber,
235 $webdav_file->store(file => $result{file_name});
238 # set some form defaults for printing webdav copy variables
239 if ( $::form->{media} eq 'email') {
240 my $mail = Mailer->new;
241 my $signature = $::myconfig{signature};
242 $mail->{$_} = $params{email}->{$_} for qw(to cc subject message bcc);
243 $mail->{from} = qq|"$::myconfig{name}" <$::myconfig{email}>|;
244 $mail->{attachments} = [{ filename => $result{file_name},
245 name => $params{email}->{attachment_filename} }];
246 $mail->{message} .= "\n-- \n$signature";
247 $mail->{message} =~ s/\r//g;
250 unlink $result{file_name};
252 flash_later('info', t8('The email has been sent.'));
253 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id);
258 if (!$::form->{printer_id} || $::form->{media} eq 'screen') {
259 $self->send_file($result{file_name}, name => $attachment_name);
260 unlink $result{file_name};
265 my $printer = SL::DB::Printer->new(id => $::form->{printer_id})->load;
266 $printer->print_document(
267 copies => $::form->{copies},
268 file_name => $result{file_name},
271 unlink $result{file_name};
273 flash_later('info', t8('The documents have been sent to the printer \'#1\'.', $printer->printer_description));
274 $self->redirect_to(action => 'edit', 'letter.id' => $letter->id, media => 'printer', printer_id => $::form->{printer_id});
277 unlink $result{file_name} if $result{file_name};
278 $::form->error(t8("Creating the PDF failed:") . " " . $@);
283 my ($self, $name_selected) = @_;
286 letter => $self->_update,
290 sub action_skip_draft {
292 $self->action_add(skip_drafts => 1);
295 sub action_delete_drafts {
298 my @ids = @{ $::form->{ids} || [] };
299 SL::DB::Manager::LetterDraft->delete_all(where => [ id => \@ids ]) if @ids;
301 $self->action_add(skip_drafts => 1);
304 sub action_send_email {
307 $::form->{media} = 'email';
308 $self->action_print_letter(email => $::form->{email_form});
314 my ($self, %params) = @_;
316 $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery kivi.Letter);
318 my $letter = $self->letter;
320 $params{title} ||= t8('Edit Letter');
322 $::form->{type} = 'letter'; # needed for print_options
323 $::form->{vc} = $letter->is_sales ? 'customer' : 'vendor'; # needs to be for _get_contacts...
325 $::request->layout->add_javascripts('customer_or_vendor_selection.js');
326 $::request->layout->add_javascripts('edit_part_window.js');
328 $::form->{language_id} ||= $params{language_id};
329 $::form->{languages} ||= SL::DB::Manager::Language->get_all_sorted;
330 $::form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
332 $self->setup_display_action_bar;
333 $self->render('letter/edit',
335 TCF => [ map { key => $_, value => t8(ucfirst $_) }, TEXT_CREATED_FOR_VALUES() ],
336 PCF => [ map { key => $_, value => t8(ucfirst $_) }, PAGE_CREATED_FOR_VALUES() ],
338 employees => $self->all_employees,
339 print_options => SL::Helper::PrintOptions->get_print_options (
340 options => { no_postscript => 1,
341 no_opendocument => 1,
351 my ($self, %params) = @_;
353 my $letter = $self->letter;
356 $self->set_greetings;
364 my $report = SL::ReportGenerator->new(\%::myconfig, $::form);
365 $self->{report} = $report;
367 my @columns = qw(date subject letternumber customer_id vendor_id contact date);
368 my @sortable = qw(date subject letternumber customer_id vendor_id contact date);
371 date => { text => t8('Date'), sub => sub { $_[0]->date_as_date } },
372 subject => { text => t8('Subject'), sub => sub { $_[0]->subject },
373 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
374 letternumber => { text => t8('Letternumber'), sub => sub { $_[0]->letternumber },
375 obj_link => sub { $self->url_for(action => 'edit', 'letter.id' => $_[0]->id, callback => $self->models->get_callback) } },
376 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 },
377 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},
378 contact => { text => t8('Contact'), sub => sub { $_[0]->contact ? $_[0]->contact->full_name : '' } },
381 $column_defs{$_}{text} = $sort_columns{$_} for keys %column_defs;
383 $report->set_options(
384 std_column_visibility => 1,
385 controller_class => 'Letter',
386 output_format => 'HTML',
387 title => t8('Letters'),
388 allow_pdf_export => 1,
389 allow_csv_export => 1,
392 $report->set_columns(%column_defs);
393 $report->set_column_order(@columns);
394 $report->set_export_options(qw(list filter is_sales));
395 $report->set_options_from_form;
397 $self->models->disable_plugin('paginated') if $report->{options}{output_format} =~ /^(pdf|csv)$/i;
398 $self->models->add_additional_url_params(is_sales => $self->is_sales);
399 $self->models->finalize;
400 $self->models->set_report_generator_sort_options(report => $report, sortable_columns => \@sortable);
402 $report->set_options(
403 raw_top_info_text => $self->render('letter/report_top', { output => 0 }),
404 raw_bottom_info_text => $self->render('letter/report_bottom', { output => 0 }, models => $self->models),
405 attachment_basename => t8('letters_list') . strftime('_%Y%m%d', localtime time),
409 sub make_filter_summary {
412 my $filter = $::form->{filter} || {};
415 my $employee = $filter->{employee_id} ? SL::DB::Employee->new(id => $filter->{employee_id})->load->name : '';
416 my $salesman = $filter->{salesman_id} ? SL::DB::Employee->new(id => $filter->{salesman_id})->load->name : '';
419 [ $filter->{"letternumber:substr::ilike"}, t8('Number') ],
420 [ $filter->{"subject:substr::ilike"}, t8('Subject') ],
421 [ $filter->{"body:substr::ilike"}, t8('Body') ],
422 [ $filter->{"date:date::ge"}, t8('From Date') ],
423 [ $filter->{"date:date::le"}, t8('To Date') ],
424 [ $employee, t8('Employee') ],
425 [ $salesman, t8('Salesman') ],
430 my @flags = map { $flags{$_} } @{ $filter->{part}{type} || [] };
433 push @filter_strings, $_ if $_;
436 push @filter_strings, "$_->[1]: $_->[0]" if $_->[0];
439 $self->{filter_summary} = join ', ', @filter_strings;
442 sub load_letter_draft {
443 my ($self, %params) = @_;
445 return 0 if $params{skip_drafts};
447 my $letter_drafts = SL::DB::Manager::LetterDraft->get_all(
449 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
453 return unless @$letter_drafts;
455 $self->setup_load_letter_draft_action_bar;
456 $self->render('letter/load_drafts',
457 title => t8('Letter Draft'),
458 LETTER_DRAFTS => $letter_drafts,
466 my $letter = $self->letter;
468 return unless $letter;
469 return if $letter->date;
471 $letter->date(DateTime->today)
475 my ($self, $letter) = @_;
477 $letter ||= $self->letter;
481 if (!$letter->subject) {
482 flash('error', t8('The subject is missing.'));
485 if (!$letter->body) {
486 flash('error', t8('The body is missing.'));
489 if (!$letter->employee_id) {
490 flash('error', t8('The employee is missing.'));
498 my ($self, $letter) = @_;
500 $letter ||= $self->letter;
502 return if $letter->letternumber;
504 $letter->letternumber(SL::TransNumber->new(type => 'letter', id => $self->{id}, number => $self->{letternumber})->create_unique);
509 my $letter = $self->letter;
511 return unless $letter;
512 return if $letter->greeting;
514 $letter->greeting(t8('Dear Sir or Madam,'));
517 sub export_letter_to_form {
518 my ($self, $letter) = @_;
521 $letter ||= $self->letter;
523 for ($letter->meta->columns) {
524 if ((ref $_) =~ /Date/i) {
525 $::form->{$_->name} = $letter->$_->to_kivitendo;
527 $::form->{$_->name} = $letter->$_;
535 my $letter = SL::DB::Manager::Letter->find_by_or_create(id => $::form->{letter}{id} || 0)
536 ->assign_attributes(%{ $::form->{letter} });
538 if ($letter->cp_id) {
539 # $letter->customer_vendor_id($letter->contact->cp_cv_id);
540 # contacts don't have language_id yet
541 # $letter->greeting(GenericTranslations->get(
542 # translation_type => 'greetings::' . ($letter->contact->cp_gender eq 'f' ? 'female' : 'male'),
543 # language_id => $letter->contact->language_id,
544 # allow_fallback => 1
548 $self->is_sales($letter->is_sales) if $letter->id;
556 SL::Controller::Helper::GetModels->new(
560 SL::DB::Manager::Letter->is_sales_filter($self->is_sales),
562 sorted => \%sort_columns,
563 with_objects => [ 'contact', 'salesman', 'employee' ],
567 sub init_all_employees {
568 SL::DB::Manager::Employee->get_all(query => [ deleted => 0 ]);
571 sub init_webdav_objects {
574 return [] if !$self->letter || !$self->letter->letternumber || !$::instance_conf->get_webdav;
576 my $webdav = SL::Webdav->new(
578 number => $self->letter->letternumber,
581 my $webdav_path = $webdav->webdav_path;
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>