+# save the order as new document an open it for edit
+sub action_save_as_new {
+ my ($self) = @_;
+
+ my $order = $self->order;
+
+ if (!$order->id) {
+ $self->js->flash('error', t8('This object has not been saved yet.'));
+ return $self->js->render();
+ }
+
+ # load order from db to check if values changed
+ my $saved_order = SL::DB::Order->new(id => $order->id)->load;
+
+ my %new_attrs;
+ # Lets assign a new number if the user hasn't changed the previous one.
+ # If it has been changed manually then use it as-is.
+ $new_attrs{number} = (trim($order->number) eq $saved_order->number)
+ ? ''
+ : trim($order->number);
+
+ # Clear transdate unless changed
+ $new_attrs{transdate} = ($order->transdate == $saved_order->transdate)
+ ? DateTime->today_local
+ : $order->transdate;
+
+ # Set new reqdate unless changed
+ if ($order->reqdate == $saved_order->reqdate) {
+ my $extra_days = $self->type eq _sales_quotation_type() ? $::instance_conf->get_reqdate_interval : 1;
+ $new_attrs{reqdate} = DateTime->today_local->next_workday(extra_days => $extra_days);
+ } else {
+ $new_attrs{reqdate} = $order->reqdate;
+ }
+
+ # Update employee
+ $new_attrs{employee} = SL::DB::Manager::Employee->current;
+
+ # Create new record from current one
+ $self->order(SL::DB::Order->new_from($order, destination_type => $order->type, attributes => \%new_attrs));
+
+ # no linked records on save as new
+ delete $::form->{$_} for qw(converted_from_oe_id converted_from_orderitems_ids);
+
+ # save
+ $self->action_save();
+}
+
+# print the order
+#
+# This is called if "print" is pressed in the print dialog.
+# If PDF creation was requested and succeeded, the pdf is stored in a session
+# file and the filename is stored as session value with an unique key. A
+# javascript function with this key is then called. This function calls the
+# download action below (action_download_pdf), which offers the file for
+# download.
+sub action_print {
+ my ($self) = @_;
+
+ my $format = $::form->{print_options}->{format};
+ my $media = $::form->{print_options}->{media};
+ my $formname = $::form->{print_options}->{formname};
+ my $copies = $::form->{print_options}->{copies};
+ my $groupitems = $::form->{print_options}->{groupitems};
+
+ # only pdf by now
+ if (none { $format eq $_ } qw(pdf)) {
+ return $self->js->flash('error', t8('Format \'#1\' is not supported yet/anymore.', $format))->render;
+ }
+
+ # only screen or printer by now
+ if (none { $media eq $_ } qw(screen printer)) {
+ return $self->js->flash('error', t8('Media \'#1\' is not supported yet/anymore.', $media))->render;
+ }
+
+ my $language;
+ $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
+
+ # create a form for generate_attachment_filename
+ my $form = Form->new;
+ $form->{ordnumber} = $self->order->ordnumber;
+ $form->{type} = $self->type;
+ $form->{format} = $format;
+ $form->{formname} = $formname;
+ $form->{language} = '_' . $language->template_code if $language;
+ my $pdf_filename = $form->generate_attachment_filename();
+
+ my $pdf;
+ my @errors = _create_pdf($self->order, \$pdf, { format => $format,
+ formname => $formname,
+ language => $language,
+ groupitems => $groupitems });
+ if (scalar @errors) {
+ return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render;
+ }
+
+ if ($media eq 'screen') {
+ # screen/download
+ my $sfile = SL::SessionFile::Random->new(mode => "w");
+ $sfile->fh->print($pdf);
+ $sfile->fh->close;
+
+ my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
+ $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
+
+ $self->js
+ ->run('kivi.Order.download_pdf', $pdf_filename, $key)
+ ->flash('info', t8('The PDF has been created'));
+
+ } elsif ($media eq 'printer') {
+ # printer
+ my $printer_id = $::form->{print_options}->{printer_id};
+ SL::DB::Printer->new(id => $printer_id)->load->print_document(
+ copies => $copies,
+ content => $pdf,
+ );
+
+ $self->js->flash('info', t8('The PDF has been printed'));
+ }
+
+ # copy file to webdav folder
+ if ($self->order->ordnumber && $::instance_conf->get_webdav_documents) {
+ my $webdav = SL::Webdav->new(
+ type => $self->type,
+ number => $self->order->ordnumber,
+ );
+ my $webdav_file = SL::Webdav::File->new(
+ webdav => $webdav,
+ filename => $pdf_filename,
+ );
+ eval {
+ $webdav_file->store(data => \$pdf);
+ 1;
+ } or do {
+ $self->js->flash('error', t8('Storing PDF to webdav folder failed: #1', $@));
+ }
+ }
+ if ($self->order->ordnumber && $::instance_conf->get_doc_storage) {
+ eval {
+ SL::File->save(object_id => $self->order->id,
+ object_type => $self->type,
+ mime_type => 'application/pdf',
+ source => 'created',
+ file_type => 'document',
+ file_name => $pdf_filename,
+ file_contents => $pdf);
+ 1;
+ } or do {
+ $self->js->flash('error', t8('Storing PDF in storage backend failed: #1', $@));
+ }
+ }
+ $self->js->render;
+}
+
+# offer pdf for download
+#
+# It needs to get the key for the session value to get the pdf file.
+sub action_download_pdf {
+ my ($self) = @_;
+
+ my $key = $::form->{key};
+ my $tmp_filename = $::auth->get_session_value("Order::create_pdf-${key}");
+ return $self->send_file(
+ $tmp_filename,
+ type => 'application/pdf',
+ name => $::form->{pdf_filename},
+ );
+}
+
+# open the email dialog
+sub action_show_email_dialog {
+ my ($self) = @_;
+
+ my $cv_method = $self->cv;
+
+ if (!$self->order->$cv_method) {
+ return $self->js->flash('error', $self->cv eq 'customer' ? t8('Cannot send E-mail without customer given') : t8('Cannot send E-mail without vendor given'))
+ ->render($self);
+ }
+
+ my $email_form;
+ $email_form->{to} = $self->order->contact->cp_email if $self->order->contact;
+ $email_form->{to} ||= $self->order->$cv_method->email;
+ $email_form->{cc} = $self->order->$cv_method->cc;
+ $email_form->{bcc} = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
+ # Todo: get addresses from shipto, if any
+
+ my $form = Form->new;
+ $form->{ordnumber} = $self->order->ordnumber;
+ $form->{formname} = $self->type;
+ $form->{type} = $self->type;
+ $form->{language} = 'de';
+ $form->{format} = 'pdf';
+
+ $email_form->{subject} = $form->generate_email_subject();
+ $email_form->{attachment_filename} = $form->generate_attachment_filename();
+ $email_form->{message} = $form->generate_email_body();
+ $email_form->{js_send_function} = 'kivi.Order.send_email()';
+
+ my %files = $self->_get_files_for_email_dialog();
+ my $dialog_html = $self->render('common/_send_email_dialog', { output => 0 },
+ email_form => $email_form,
+ show_bcc => $::auth->assert('email_bcc', 'may fail'),
+ FILES => \%files,
+ is_customer => $self->cv eq 'customer',
+ );
+
+ $self->js
+ ->run('kivi.Order.show_email_dialog', $dialog_html)
+ ->reinit_widgets
+ ->render($self);
+}
+
+# send email
+#
+# Todo: handling error messages: flash is not displayed in dialog, but in the main form
+sub action_send_email {
+ my ($self) = @_;
+
+ my $email_form = delete $::form->{email_form};
+ my %field_names = (to => 'email');
+
+ $::form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
+
+ # for Form::cleanup which may be called in Form::send_email
+ $::form->{cwd} = getcwd();
+ $::form->{tmpdir} = $::lx_office_conf{paths}->{userspath};
+
+ $::form->{media} = 'email';
+
+ if (($::form->{attachment_policy} // '') eq 'normal') {
+ my $language;
+ $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
+
+ my $pdf;
+ my @errors = _create_pdf($self->order, \$pdf, {media => $::form->{media},
+ format => $::form->{print_options}->{format},
+ formname => $::form->{print_options}->{formname},
+ language => $language,
+ groupitems => $::form->{print_options}->{groupitems}});
+ if (scalar @errors) {
+ return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
+ }
+
+ my $sfile = SL::SessionFile::Random->new(mode => "w");
+ $sfile->fh->print($pdf);
+ $sfile->fh->close;
+
+ $::form->{tmpfile} = $sfile->file_name;
+ $::form->{tmpdir} = $sfile->get_path; # for Form::cleanup which may be called in Form::send_email
+ }
+
+ $::form->send_email(\%::myconfig, 'pdf');
+
+ # internal notes
+ my $intnotes = $self->order->intnotes;
+ $intnotes .= "\n\n" if $self->order->intnotes;
+ $intnotes .= t8('[email]') . "\n";
+ $intnotes .= t8('Date') . ": " . $::locale->format_date_object(DateTime->now_local, precision => 'seconds') . "\n";
+ $intnotes .= t8('To (email)') . ": " . $::form->{email} . "\n";
+ $intnotes .= t8('Cc') . ": " . $::form->{cc} . "\n" if $::form->{cc};
+ $intnotes .= t8('Bcc') . ": " . $::form->{bcc} . "\n" if $::form->{bcc};
+ $intnotes .= t8('Subject') . ": " . $::form->{subject} . "\n\n";
+ $intnotes .= t8('Message') . ": " . $::form->{message};
+
+ $self->js
+ ->val('#order_intnotes', $intnotes)
+ ->run('kivi.Order.close_email_dialog')
+ ->flash('info', t8('The email has been sent.'))
+ ->render($self);
+}
+
+# open the periodic invoices config dialog
+#
+# If there are values in the form (i.e. dialog was opened before),
+# then use this values. Create new ones, else.
+sub action_show_periodic_invoices_config_dialog {
+ my ($self) = @_;
+
+ my $config = _make_periodic_invoices_config_from_yaml(delete $::form->{config});
+ $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id};
+ $config ||= SL::DB::PeriodicInvoicesConfig->new(periodicity => 'm',
+ order_value_periodicity => 'p', # = same as periodicity
+ start_date_as_date => $::form->{transdate} || $::form->current_date,
+ extend_automatically_by => 12,
+ active => 1,
+ email_subject => GenericTranslations->get(
+ language_id => $::form->{language_id},
+ translation_type =>"preset_text_periodic_invoices_email_subject"),
+ email_body => GenericTranslations->get(
+ language_id => $::form->{language_id},
+ translation_type =>"preset_text_periodic_invoices_email_body"),
+ );
+ $config->periodicity('m') if none { $_ eq $config->periodicity } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES;
+ $config->order_value_periodicity('p') if none { $_ eq $config->order_value_periodicity } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES);
+
+ $::form->get_lists(printers => "ALL_PRINTERS",
+ charts => { key => 'ALL_CHARTS',
+ transdate => 'current_date' });
+
+ $::form->{AR} = [ grep { $_->{link} =~ m/(?:^|:)AR(?::|$)/ } @{ $::form->{ALL_CHARTS} } ];
+
+ if ($::form->{customer_id}) {
+ $::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(where => [ cp_cv_id => $::form->{customer_id} ]);
+ }
+
+ $self->render('oe/edit_periodic_invoices_config', { layout => 0 },
+ popup_dialog => 1,
+ popup_js_close_function => 'kivi.Order.close_periodic_invoices_config_dialog()',
+ popup_js_assign_function => 'kivi.Order.assign_periodic_invoices_config()',
+ config => $config,
+ %$::form);
+}
+
+# assign the values of the periodic invoices config dialog
+# as yaml in the hidden tag and set the status.
+sub action_assign_periodic_invoices_config {
+ my ($self) = @_;
+
+ $::form->isblank('start_date_as_date', $::locale->text('The start date is missing.'));
+
+ my $config = { active => $::form->{active} ? 1 : 0,
+ terminated => $::form->{terminated} ? 1 : 0,
+ direct_debit => $::form->{direct_debit} ? 1 : 0,
+ periodicity => (any { $_ eq $::form->{periodicity} } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES) ? $::form->{periodicity} : 'm',
+ order_value_periodicity => (any { $_ eq $::form->{order_value_periodicity} } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES)) ? $::form->{order_value_periodicity} : 'p',
+ start_date_as_date => $::form->{start_date_as_date},
+ end_date_as_date => $::form->{end_date_as_date},
+ first_billing_date_as_date => $::form->{first_billing_date_as_date},
+ print => $::form->{print} ? 1 : 0,
+ printer_id => $::form->{print} ? $::form->{printer_id} * 1 : undef,
+ copies => $::form->{copies} * 1 ? $::form->{copies} : 1,
+ extend_automatically_by => $::form->{extend_automatically_by} * 1 || undef,
+ ar_chart_id => $::form->{ar_chart_id} * 1,
+ send_email => $::form->{send_email} ? 1 : 0,
+ email_recipient_contact_id => $::form->{email_recipient_contact_id} * 1 || undef,
+ email_recipient_address => $::form->{email_recipient_address},
+ email_sender => $::form->{email_sender},
+ email_subject => $::form->{email_subject},
+ email_body => $::form->{email_body},
+ };
+
+ my $periodic_invoices_config = YAML::Dump($config);
+
+ my $status = $self->_get_periodic_invoices_status($config);
+
+ $self->js
+ ->remove('#order_periodic_invoices_config')
+ ->insertAfter(hidden_tag('order.periodic_invoices_config', $periodic_invoices_config), '#periodic_invoices_status')
+ ->run('kivi.Order.close_periodic_invoices_config_dialog')
+ ->html('#periodic_invoices_status', $status)
+ ->flash('info', t8('The periodic invoices config has been assigned.'))
+ ->render($self);
+}
+
+sub action_get_has_active_periodic_invoices {
+ my ($self) = @_;
+
+ my $config = _make_periodic_invoices_config_from_yaml(delete $::form->{config});
+ $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id};
+
+ my $has_active_periodic_invoices =
+ $self->type eq _sales_order_type()
+ && $config
+ && $config->active
+ && (!$config->end_date || ($config->end_date > DateTime->today_local))
+ && $config->get_previous_billed_period_start_date;
+
+ $_[0]->render(\ !!$has_active_periodic_invoices, { type => 'text' });
+}
+
+# save the order and redirect to the frontend subroutine for a new
+# delivery order
+sub action_save_and_delivery_order {
+ my ($self) = @_;
+
+ my $errors = $self->_save();
+
+ if (scalar @{ $errors }) {
+ $self->js->flash('error', $_) foreach @{ $errors };
+ return $self->js->render();
+ }
+
+ my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been saved')
+ : $self->type eq _purchase_order_type() ? $::locale->text('The order has been saved')
+ : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been saved')
+ : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been saved')
+ : '';
+ flash_later('info', $text);
+
+ my @redirect_params = (
+ controller => 'oe.pl',
+ action => 'oe_delivery_order_from_order',
+ id => $self->order->id,
+ );
+
+ $self->redirect_to(@redirect_params);
+}
+
+# save the order and redirect to the frontend subroutine for a new
+# invoice
+sub action_save_and_invoice {
+ my ($self) = @_;
+
+ my $errors = $self->_save();
+
+ if (scalar @{ $errors }) {
+ $self->js->flash('error', $_) foreach @{ $errors };
+ return $self->js->render();
+ }
+
+ my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been saved')
+ : $self->type eq _purchase_order_type() ? $::locale->text('The order has been saved')
+ : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been saved')
+ : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been saved')
+ : '';
+ flash_later('info', $text);
+
+ my @redirect_params = (
+ controller => 'oe.pl',
+ action => 'oe_invoice_from_order',
+ id => $self->order->id,
+ );
+
+ $self->redirect_to(@redirect_params);
+}
+
+# workflow from sales quotation to sales order
+sub action_sales_order {
+ $_[0]->_workflow_sales_or_purchase_order();
+}
+
+# workflow from rfq to purchase order
+sub action_purchase_order {
+ $_[0]->_workflow_sales_or_purchase_order();
+}
+
+# set form elements in respect to a changed customer or vendor
+#
+# This action is called on an change of the customer/vendor picker.