1 package SL::Controller::Order;
4 use parent qw(SL::Controller::Base);
6 use SL::Helper::Flash qw(flash_later);
7 use SL::Presenter::Tag qw(select_tag hidden_tag);
8 use SL::Locale::String qw(t8);
9 use SL::SessionFile::Random;
13 use SL::Util qw(trim);
20 use SL::DB::RecordLink;
22 use SL::Helper::CreatePDF qw(:all);
23 use SL::Helper::PrintOptions;
25 use SL::Controller::Helper::GetModels;
27 use List::Util qw(first);
28 use List::UtilsBy qw(sort_by uniq_by);
29 use List::MoreUtils qw(any none pairwise first_index);
30 use English qw(-no_match_vars);
34 use Rose::Object::MakeMethods::Generic
36 scalar => [ qw(item_ids_to_delete) ],
37 'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models all_price_factors) ],
42 __PACKAGE__->run_before('_check_auth');
44 __PACKAGE__->run_before('_recalc',
45 only => [ qw(save save_as_new save_and_delivery_order save_and_invoice print create_pdf send_email) ]);
47 __PACKAGE__->run_before('_get_unalterable_data',
48 only => [ qw(save save_as_new save_and_delivery_order save_and_invoice print create_pdf send_email) ]);
58 $self->order->transdate(DateTime->now_local());
59 $self->order->reqdate(DateTime->today_local->next_workday) if !$self->order->reqdate;
64 title => $self->_get_title_for('add'),
65 %{$self->{template_args}}
69 # edit an existing order
78 title => $self->_get_title_for('edit'),
79 %{$self->{template_args}}
87 my $errors = $self->_delete();
89 if (scalar @{ $errors }) {
90 $self->js->flash('error', $_) foreach @{ $errors };
91 return $self->js->render();
94 my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been deleted')
95 : $self->type eq _purchase_order_type() ? $::locale->text('The order has been deleted')
96 : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been deleted')
97 : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been deleted')
99 flash_later('info', $text);
101 my @redirect_params = (
106 $self->redirect_to(@redirect_params);
113 my $errors = $self->_save();
115 if (scalar @{ $errors }) {
116 $self->js->flash('error', $_) foreach @{ $errors };
117 return $self->js->render();
120 my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been saved')
121 : $self->type eq _purchase_order_type() ? $::locale->text('The order has been saved')
122 : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been saved')
123 : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been saved')
125 flash_later('info', $text);
127 my @redirect_params = (
130 id => $self->order->id,
133 $self->redirect_to(@redirect_params);
136 # save the order as new document an open it for edit
137 sub action_save_as_new {
140 if (!$self->order->id) {
141 $self->js->flash('error', t8('This object has not been saved yet.'));
142 return $self->js->render();
145 delete $::form->{$_} for qw(closed delivered converted_from_oe_id converted_from_orderitems_ids);
147 my $src_order = SL::DB::Order->new(id => $self->order->id)->load;
149 # Lets assign a new number if the user hasn't changed the previous one.
150 # If it has been changed manually then use it as-is.
151 if (trim($self->order->number) eq $src_order->number) {
152 $self->order->number('');
155 # Clear reqdate and transdate unless changed
156 if ($self->order->transdate == $src_order->transdate) {
157 $self->order->transdate(DateTime->today_local)
159 if ($self->order->reqdate == $src_order->reqdate) {
160 my $extra_days = $self->type eq _sales_quotation_type() ? $::instance_conf->get_reqdate_interval : 1;
161 $self->order->reqdate(DateTime->today_local->next_workday(extra_days => $extra_days));
165 $self->order->employee(SL::DB::Manager::Employee->current);
168 $self->action_save();
173 # This is called if "print" is pressed in the print dialog.
174 # If PDF creation was requested and succeeded, the pdf is stored in a session
175 # file and the filename is stored as session value with an unique key. A
176 # javascript function with this key is then called. This function calls the
177 # download action below (action_download_pdf), which offers the file for
182 my $format = $::form->{print_options}->{format};
183 my $media = $::form->{print_options}->{media};
184 my $formname = $::form->{print_options}->{formname};
185 my $copies = $::form->{print_options}->{copies};
186 my $groupitems = $::form->{print_options}->{groupitems};
189 if (none { $format eq $_ } qw(pdf)) {
190 return $self->js->flash('error', t8('Format \'#1\' is not supported yet/anymore.', $format))->render;
193 # only screen or printer by now
194 if (none { $media eq $_ } qw(screen printer)) {
195 return $self->js->flash('error', t8('Media \'#1\' is not supported yet/anymore.', $media))->render;
199 $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
201 # create a form for generate_attachment_filename
202 my $form = Form->new;
203 $form->{ordnumber} = $self->order->ordnumber;
204 $form->{type} = $self->type;
205 $form->{format} = $format;
206 $form->{formname} = $formname;
207 $form->{language} = '_' . $language->template_code if $language;
208 my $pdf_filename = $form->generate_attachment_filename();
211 my @errors = _create_pdf($self->order, \$pdf, { format => $format,
212 formname => $formname,
213 language => $language,
214 groupitems => $groupitems });
215 if (scalar @errors) {
216 return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render;
219 if ($media eq 'screen') {
221 my $sfile = SL::SessionFile::Random->new(mode => "w");
222 $sfile->fh->print($pdf);
225 my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
226 $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
229 ->run('kivi.Order.download_pdf', $pdf_filename, $key)
230 ->flash('info', t8('The PDF has been created'));
232 } elsif ($media eq 'printer') {
234 my $printer_id = $::form->{print_options}->{printer_id};
235 SL::DB::Printer->new(id => $printer_id)->load->print_document(
240 $self->js->flash('info', t8('The PDF has been printed'));
243 # copy file to webdav folder
244 if ($self->order->ordnumber && $::instance_conf->get_webdav_documents) {
245 my $webdav = SL::Webdav->new(
247 number => $self->order->ordnumber,
249 my $webdav_file = SL::Webdav::File->new(
251 filename => $pdf_filename,
254 $webdav_file->store(data => \$pdf);
257 $self->js->flash('error', t8('Storing PDF to webdav folder failed: #1', $@));
260 if ($self->order->ordnumber && $::instance_conf->get_doc_storage) {
262 SL::File->save(object_id => $self->order->id,
263 object_type => $self->type,
264 mime_type => 'application/pdf',
266 file_type => 'document',
267 file_name => $pdf_filename,
268 file_contents => $pdf);
271 $self->js->flash('error', t8('Storing PDF in storage backend failed: #1', $@));
277 # offer pdf for download
279 # It needs to get the key for the session value to get the pdf file.
280 sub action_download_pdf {
283 my $key = $::form->{key};
284 my $tmp_filename = $::auth->get_session_value("Order::create_pdf-${key}");
285 return $self->send_file(
287 type => 'application/pdf',
288 name => $::form->{pdf_filename},
292 # open the email dialog
293 sub action_show_email_dialog {
296 my $cv_method = $self->cv;
298 if (!$self->order->$cv_method) {
299 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'))
304 $email_form->{to} = $self->order->contact->cp_email if $self->order->contact;
305 $email_form->{to} ||= $self->order->$cv_method->email;
306 $email_form->{cc} = $self->order->$cv_method->cc;
307 $email_form->{bcc} = join ', ', grep $_, $self->order->$cv_method->bcc, SL::DB::Default->get->global_bcc;
308 # Todo: get addresses from shipto, if any
310 my $form = Form->new;
311 $form->{ordnumber} = $self->order->ordnumber;
312 $form->{formname} = $self->type;
313 $form->{type} = $self->type;
314 $form->{language} = 'de';
315 $form->{format} = 'pdf';
317 $email_form->{subject} = $form->generate_email_subject();
318 $email_form->{attachment_filename} = $form->generate_attachment_filename();
319 $email_form->{message} = $form->generate_email_body();
320 $email_form->{js_send_function} = 'kivi.Order.send_email()';
322 my %files = $self->_get_files_for_email_dialog();
323 my $dialog_html = $self->render('common/_send_email_dialog', { output => 0 },
324 email_form => $email_form,
325 show_bcc => $::auth->assert('email_bcc', 'may fail'),
327 is_customer => $self->cv eq 'customer',
331 ->run('kivi.Order.show_email_dialog', $dialog_html)
338 # Todo: handling error messages: flash is not displayed in dialog, but in the main form
339 sub action_send_email {
342 my $email_form = delete $::form->{email_form};
343 my %field_names = (to => 'email');
345 $::form->{ $field_names{$_} // $_ } = $email_form->{$_} for keys %{ $email_form };
347 # for Form::cleanup which may be called in Form::send_email
348 $::form->{cwd} = getcwd();
349 $::form->{tmpdir} = $::lx_office_conf{paths}->{userspath};
351 $::form->{media} = 'email';
353 if (($::form->{attachment_policy} // '') eq 'normal') {
355 $language = SL::DB::Language->new(id => $::form->{print_options}->{language_id})->load if $::form->{print_options}->{language_id};
358 my @errors = _create_pdf($self->order, \$pdf, {media => $::form->{media},
359 format => $::form->{print_options}->{format},
360 formname => $::form->{print_options}->{formname},
361 language => $language,
362 groupitems => $::form->{print_options}->{groupitems}});
363 if (scalar @errors) {
364 return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self);
367 my $sfile = SL::SessionFile::Random->new(mode => "w");
368 $sfile->fh->print($pdf);
371 $::form->{tmpfile} = $sfile->file_name;
372 $::form->{tmpdir} = $sfile->get_path; # for Form::cleanup which may be called in Form::send_email
375 $::form->send_email(\%::myconfig, 'pdf');
378 my $intnotes = $self->order->intnotes;
379 $intnotes .= "\n\n" if $self->order->intnotes;
380 $intnotes .= t8('[email]') . "\n";
381 $intnotes .= t8('Date') . ": " . $::locale->format_date_object(DateTime->now_local, precision => 'seconds') . "\n";
382 $intnotes .= t8('To (email)') . ": " . $::form->{email} . "\n";
383 $intnotes .= t8('Cc') . ": " . $::form->{cc} . "\n" if $::form->{cc};
384 $intnotes .= t8('Bcc') . ": " . $::form->{bcc} . "\n" if $::form->{bcc};
385 $intnotes .= t8('Subject') . ": " . $::form->{subject} . "\n\n";
386 $intnotes .= t8('Message') . ": " . $::form->{message};
389 ->val('#order_intnotes', $intnotes)
390 ->run('kivi.Order.close_email_dialog')
391 ->flash('info', t8('The email has been sent.'))
395 # open the periodic invoices config dialog
397 # If there are values in the form (i.e. dialog was opened before),
398 # then use this values. Create new ones, else.
399 sub action_show_periodic_invoices_config_dialog {
402 my $config = _make_periodic_invoices_config_from_yaml(delete $::form->{config});
403 $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id};
404 $config ||= SL::DB::PeriodicInvoicesConfig->new(periodicity => 'm',
405 order_value_periodicity => 'p', # = same as periodicity
406 start_date_as_date => $::form->{transdate} || $::form->current_date,
407 extend_automatically_by => 12,
409 email_subject => GenericTranslations->get(
410 language_id => $::form->{language_id},
411 translation_type =>"preset_text_periodic_invoices_email_subject"),
412 email_body => GenericTranslations->get(
413 language_id => $::form->{language_id},
414 translation_type =>"preset_text_periodic_invoices_email_body"),
416 $config->periodicity('m') if none { $_ eq $config->periodicity } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES;
417 $config->order_value_periodicity('p') if none { $_ eq $config->order_value_periodicity } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES);
419 $::form->get_lists(printers => "ALL_PRINTERS",
420 charts => { key => 'ALL_CHARTS',
421 transdate => 'current_date' });
423 $::form->{AR} = [ grep { $_->{link} =~ m/(?:^|:)AR(?::|$)/ } @{ $::form->{ALL_CHARTS} } ];
425 if ($::form->{customer_id}) {
426 $::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(where => [ cp_cv_id => $::form->{customer_id} ]);
429 $self->render('oe/edit_periodic_invoices_config', { layout => 0 },
431 popup_js_close_function => 'kivi.Order.close_periodic_invoices_config_dialog()',
432 popup_js_assign_function => 'kivi.Order.assign_periodic_invoices_config()',
437 # assign the values of the periodic invoices config dialog
438 # as yaml in the hidden tag and set the status.
439 sub action_assign_periodic_invoices_config {
442 $::form->isblank('start_date_as_date', $::locale->text('The start date is missing.'));
444 my $config = { active => $::form->{active} ? 1 : 0,
445 terminated => $::form->{terminated} ? 1 : 0,
446 direct_debit => $::form->{direct_debit} ? 1 : 0,
447 periodicity => (any { $_ eq $::form->{periodicity} } @SL::DB::PeriodicInvoicesConfig::PERIODICITIES) ? $::form->{periodicity} : 'm',
448 order_value_periodicity => (any { $_ eq $::form->{order_value_periodicity} } ('p', @SL::DB::PeriodicInvoicesConfig::ORDER_VALUE_PERIODICITIES)) ? $::form->{order_value_periodicity} : 'p',
449 start_date_as_date => $::form->{start_date_as_date},
450 end_date_as_date => $::form->{end_date_as_date},
451 first_billing_date_as_date => $::form->{first_billing_date_as_date},
452 print => $::form->{print} ? 1 : 0,
453 printer_id => $::form->{print} ? $::form->{printer_id} * 1 : undef,
454 copies => $::form->{copies} * 1 ? $::form->{copies} : 1,
455 extend_automatically_by => $::form->{extend_automatically_by} * 1 || undef,
456 ar_chart_id => $::form->{ar_chart_id} * 1,
457 send_email => $::form->{send_email} ? 1 : 0,
458 email_recipient_contact_id => $::form->{email_recipient_contact_id} * 1 || undef,
459 email_recipient_address => $::form->{email_recipient_address},
460 email_sender => $::form->{email_sender},
461 email_subject => $::form->{email_subject},
462 email_body => $::form->{email_body},
465 my $periodic_invoices_config = YAML::Dump($config);
467 my $status = $self->_get_periodic_invoices_status($config);
470 ->remove('#order_periodic_invoices_config')
471 ->insertAfter(hidden_tag('order.periodic_invoices_config', $periodic_invoices_config), '#periodic_invoices_status')
472 ->run('kivi.Order.close_periodic_invoices_config_dialog')
473 ->html('#periodic_invoices_status', $status)
474 ->flash('info', t8('The periodic invoices config has been assigned.'))
478 sub action_get_has_active_periodic_invoices {
481 my $config = _make_periodic_invoices_config_from_yaml(delete $::form->{config});
482 $config ||= SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $::form->{id}) if $::form->{id};
484 my $has_active_periodic_invoices =
485 $self->type eq _sales_order_type()
488 && (!$config->end_date || ($config->end_date > DateTime->today_local))
489 && $config->get_previous_billed_period_start_date;
491 $_[0]->render(\ !!$has_active_periodic_invoices, { type => 'text' });
494 # save the order and redirect to the frontend subroutine for a new
496 sub action_save_and_delivery_order {
499 my $errors = $self->_save();
501 if (scalar @{ $errors }) {
502 $self->js->flash('error', $_) foreach @{ $errors };
503 return $self->js->render();
506 my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been saved')
507 : $self->type eq _purchase_order_type() ? $::locale->text('The order has been saved')
508 : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been saved')
509 : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been saved')
511 flash_later('info', $text);
513 my @redirect_params = (
514 controller => 'oe.pl',
515 action => 'oe_delivery_order_from_order',
516 id => $self->order->id,
519 $self->redirect_to(@redirect_params);
522 # save the order and redirect to the frontend subroutine for a new
524 sub action_save_and_invoice {
527 my $errors = $self->_save();
529 if (scalar @{ $errors }) {
530 $self->js->flash('error', $_) foreach @{ $errors };
531 return $self->js->render();
534 my $text = $self->type eq _sales_order_type() ? $::locale->text('The order has been saved')
535 : $self->type eq _purchase_order_type() ? $::locale->text('The order has been saved')
536 : $self->type eq _sales_quotation_type() ? $::locale->text('The quotation has been saved')
537 : $self->type eq _request_quotation_type() ? $::locale->text('The rfq has been saved')
539 flash_later('info', $text);
541 my @redirect_params = (
542 controller => 'oe.pl',
543 action => 'oe_invoice_from_order',
544 id => $self->order->id,
547 $self->redirect_to(@redirect_params);
550 # workflow from sales quotation to sales order
551 sub action_sales_order {
552 $_[0]->_workflow_sales_or_purchase_order();
555 # workflow from rfq to purchase order
556 sub action_purchase_order {
557 $_[0]->_workflow_sales_or_purchase_order();
560 # set form elements in respect to a changed customer or vendor
562 # This action is called on an change of the customer/vendor picker.
563 sub action_customer_vendor_changed {
566 my $cv_method = $self->cv;
568 if ($self->order->$cv_method->contacts && scalar @{ $self->order->$cv_method->contacts } > 0) {
569 $self->js->show('#cp_row');
571 $self->js->hide('#cp_row');
574 if ($self->order->$cv_method->shipto && scalar @{ $self->order->$cv_method->shipto } > 0) {
575 $self->js->show('#shipto_row');
577 $self->js->hide('#shipto_row');
580 $self->order->taxzone_id($self->order->$cv_method->taxzone_id);
582 if ($self->order->is_sales) {
583 $self->order->taxincluded(defined($self->order->$cv_method->taxincluded_checked)
584 ? $self->order->$cv_method->taxincluded_checked
585 : $::myconfig{taxincluded_checked});
586 $self->js->val('#order_salesman_id', $self->order->$cv_method->salesman_id);
589 $self->order->payment_id($self->order->$cv_method->payment_id);
590 $self->order->delivery_term_id($self->order->$cv_method->delivery_term_id);
595 ->replaceWith('#order_cp_id', $self->build_contact_select)
596 ->replaceWith('#order_shipto_id', $self->build_shipto_select)
597 ->replaceWith('#business_info_row', $self->build_business_info_row)
598 ->val( '#order_taxzone_id', $self->order->taxzone_id)
599 ->val( '#order_taxincluded', $self->order->taxincluded)
600 ->val( '#order_payment_id', $self->order->payment_id)
601 ->val( '#order_delivery_term_id', $self->order->delivery_term_id)
602 ->val( '#order_intnotes', $self->order->$cv_method->notes)
603 ->focus( '#order_' . $self->cv . '_id');
605 $self->_js_redisplay_amounts_and_taxes;
609 # called if a unit in an existing item row is changed
610 sub action_unit_changed {
613 my $idx = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} };
614 my $item = $self->order->items_sorted->[$idx];
616 my $old_unit_obj = SL::DB::Unit->new(name => $::form->{old_unit})->load;
617 $item->sellprice($item->unit_obj->convert_to($item->sellprice, $old_unit_obj));
622 ->run('kivi.Order.update_sellprice', $::form->{item_id}, $item->sellprice_as_number);
623 $self->_js_redisplay_line_values;
624 $self->_js_redisplay_amounts_and_taxes;
628 # add an item row for a new item entered in the input row
629 sub action_add_item {
632 my $form_attr = $::form->{add_item};
634 return unless $form_attr->{parts_id};
636 my $item = _new_item($self->order, $form_attr);
638 $self->order->add_items($item);
642 my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
643 my $row_as_html = $self->p->render('order/tabs/_row',
647 ALL_PRICE_FACTORS => $self->all_price_factors
651 ->append('#row_table_id', $row_as_html);
653 if ( $item->part->is_assortment ) {
654 $form_attr->{qty_as_number} = 1 unless $form_attr->{qty_as_number};
655 foreach my $assortment_item ( @{$item->part->assortment_items} ) {
656 my $attr = { parts_id => $assortment_item->parts_id,
657 qty => $assortment_item->qty * $::form->parse_amount(\%::myconfig, $form_attr->{qty_as_number}), # TODO $form_attr->{unit}
658 unit => $assortment_item->unit,
659 description => $assortment_item->part->description,
661 my $item = _new_item($self->order, $attr);
663 # set discount to 100% if item isn't supposed to be charged, overwriting any customer discount
664 $item->discount(1) unless $assortment_item->charge;
666 $self->order->add_items( $item );
668 my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
669 my $row_as_html = $self->p->render('order/tabs/_row',
673 ALL_PRICE_FACTORS => $self->all_price_factors
676 ->append('#row_table_id', $row_as_html);
681 ->val('.add_item_input', '')
682 ->run('kivi.Order.init_row_handlers')
683 ->run('kivi.Order.row_table_scroll_down')
684 ->run('kivi.Order.renumber_positions')
685 ->focus('#add_item_parts_id_name');
687 $self->_js_redisplay_amounts_and_taxes;
691 # open the dialog for entering multiple items at once
692 sub action_show_multi_items_dialog {
693 require SL::DB::PartsGroup;
694 $_[0]->render('order/tabs/_multi_items_dialog', { layout => 0 },
695 all_partsgroups => SL::DB::Manager::PartsGroup->get_all);
698 # update the filter results in the multi item dialog
699 sub action_multi_items_update_result {
702 $::form->{multi_items}->{filter}->{obsolete} = 0;
704 my $count = $_[0]->multi_items_models->count;
707 my $text = SL::Presenter::EscapedText->new(text => $::locale->text('No results.'));
708 $_[0]->render($text, { layout => 0 });
709 } elsif ($count > $max_count) {
710 my $text = SL::Presenter::EscapedText->new(text => $::locale->text('Too many results (#1 from #2).', $count, $max_count));
711 $_[0]->render($text, { layout => 0 });
713 my $multi_items = $_[0]->multi_items_models->get;
714 $_[0]->render('order/tabs/_multi_items_result', { layout => 0 },
715 multi_items => $multi_items);
719 # add item rows for multiple items at once
720 sub action_add_multi_items {
723 my @form_attr = grep { $_->{qty_as_number} } @{ $::form->{add_multi_items} };
724 return $self->js->render() unless scalar @form_attr;
727 foreach my $attr (@form_attr) {
728 my $item = _new_item($self->order, $attr);
730 if ( $item->part->is_assortment ) {
731 foreach my $assortment_item ( @{$item->part->assortment_items} ) {
732 my $attr = { parts_id => $assortment_item->parts_id,
733 qty => $assortment_item->qty * $item->qty, # TODO $form_attr->{unit}
734 unit => $assortment_item->unit,
735 description => $assortment_item->part->description,
737 my $item = _new_item($self->order, $attr);
739 # set discount to 100% if item isn't supposed to be charged, overwriting any customer discount
740 $item->discount(1) unless $assortment_item->charge;
745 $self->order->add_items(@items);
749 foreach my $item (@items) {
750 my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
751 my $row_as_html = $self->p->render('order/tabs/_row',
755 ALL_PRICE_FACTORS => $self->all_price_factors
758 $self->js->append('#row_table_id', $row_as_html);
762 ->run('kivi.Order.close_multi_items_dialog')
763 ->run('kivi.Order.init_row_handlers')
764 ->run('kivi.Order.row_table_scroll_down')
765 ->run('kivi.Order.renumber_positions')
766 ->focus('#add_item_parts_id_name');
768 $self->_js_redisplay_amounts_and_taxes;
772 # recalculate all linetotals, amounts and taxes and redisplay them
773 sub action_recalc_amounts_and_taxes {
778 $self->_js_redisplay_line_values;
779 $self->_js_redisplay_amounts_and_taxes;
783 # redisplay item rows if they are sorted by an attribute
784 sub action_reorder_items {
788 partnumber => sub { $_[0]->part->partnumber },
789 description => sub { $_[0]->description },
790 qty => sub { $_[0]->qty },
791 sellprice => sub { $_[0]->sellprice },
792 discount => sub { $_[0]->discount },
795 my $method = $sort_keys{$::form->{order_by}};
796 my @to_sort = map { { old_pos => $_->position, order_by => $method->($_) } } @{ $self->order->items_sorted };
797 if ($::form->{sort_dir}) {
798 @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort;
800 @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort;
803 ->run('kivi.Order.redisplay_items', \@to_sort)
807 # show the popup to choose a price/discount source
808 sub action_price_popup {
811 my $idx = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} };
812 my $item = $self->order->items_sorted->[$idx];
814 $self->render_price_dialog($item);
817 # get the longdescription for an item if the dialog to enter/change the
818 # longdescription was opened and the longdescription is empty
820 # If this item is new, get the longdescription from Part.
821 # Otherwise get it from OrderItem.
822 sub action_get_item_longdescription {
825 if ($::form->{item_id}) {
826 $longdescription = SL::DB::OrderItem->new(id => $::form->{item_id})->load->longdescription;
827 } elsif ($::form->{parts_id}) {
828 $longdescription = SL::DB::Part->new(id => $::form->{parts_id})->load->notes;
830 $_[0]->render(\ $longdescription, { type => 'text' });
833 # load the second row for one or more items
835 # This action gets the html code for all items second rows by rendering a template for
836 # the second row and sets the html code via client js.
837 sub action_load_second_rows {
840 $self->_recalc() if $self->order->is_sales; # for margin calculation
842 foreach my $item_id (@{ $::form->{item_ids} }) {
843 my $idx = first_index { $_ eq $item_id } @{ $::form->{orderitem_ids} };
844 my $item = $self->order->items_sorted->[$idx];
846 $self->_js_load_second_row($item, $item_id, 0);
849 $self->js->run('kivi.Order.init_row_handlers') if $self->order->is_sales; # for lastcosts change-callback
854 sub _js_load_second_row {
855 my ($self, $item, $item_id, $do_parse) = @_;
858 # Parse values from form (they are formated while rendering (template)).
859 # Workaround to pre-parse number-cvars (parse_custom_variable_values does not parse number values).
860 # This parsing is not necessary at all, if we assure that the second row/cvars are only loaded once.
861 foreach my $var (@{ $item->cvars_by_config }) {
862 $var->unparsed_value($::form->parse_amount(\%::myconfig, $var->{__unparsed_value})) if ($var->config->type eq 'number' && exists($var->{__unparsed_value}));
864 $item->parse_custom_variable_values;
867 my $row_as_html = $self->p->render('order/tabs/_second_row', ITEM => $item, TYPE => $self->type);
870 ->html('.row_entry:has(#item_' . $item_id . ') [name = "second_row"]', $row_as_html)
871 ->data('.row_entry:has(#item_' . $item_id . ') [name = "second_row"]', 'loaded', 1);
874 sub _js_redisplay_line_values {
877 my $is_sales = $self->order->is_sales;
879 # sales orders with margins
884 $::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0),
885 $::form->format_amount(\%::myconfig, $_->{marge_total}, 2, 0),
886 $::form->format_amount(\%::myconfig, $_->{marge_percent}, 2, 0),
887 ]} @{ $self->order->items_sorted };
891 $::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0),
892 ]} @{ $self->order->items_sorted };
896 ->run('kivi.Order.redisplay_line_values', $is_sales, \@data);
899 sub _js_redisplay_amounts_and_taxes {
902 if (scalar @{ $self->{taxes} }) {
903 $self->js->show('#taxincluded_row_id');
905 $self->js->hide('#taxincluded_row_id');
908 if ($self->order->taxincluded) {
909 $self->js->hide('#subtotal_row_id');
911 $self->js->show('#subtotal_row_id');
915 ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2))
916 ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2))
918 ->insertBefore($self->build_tax_rows, '#amount_row_id');
925 sub init_valid_types {
926 [ _sales_order_type(), _purchase_order_type(), _sales_quotation_type(), _request_quotation_type() ];
932 if (none { $::form->{type} eq $_ } @{$self->valid_types}) {
933 die "Not a valid type for order";
936 $self->type($::form->{type});
942 my $cv = (any { $self->type eq $_ } (_sales_order_type(), _sales_quotation_type())) ? 'customer'
943 : (any { $self->type eq $_ } (_purchase_order_type(), _request_quotation_type())) ? 'vendor'
944 : die "Not a valid type for order";
957 # model used to filter/display the parts in the multi-items dialog
958 sub init_multi_items_models {
959 SL::Controller::Helper::GetModels->new(
962 with_objects => [ qw(unit_obj) ],
963 disable_plugin => 'paginated',
964 source => $::form->{multi_items},
970 partnumber => t8('Partnumber'),
971 description => t8('Description')}
975 sub init_all_price_factors {
976 SL::DB::Manager::PriceFactor->get_all;
982 my $right_for = { map { $_ => $_.'_edit' } @{$self->valid_types} };
984 my $right = $right_for->{ $self->type };
985 $right ||= 'DOES_NOT_EXIST';
987 $::auth->assert($right);
990 # build the selection box for contacts
992 # Needed, if customer/vendor changed.
993 sub build_contact_select {
996 select_tag('order.cp_id', [ $self->order->{$self->cv}->contacts ],
997 value_key => 'cp_id',
998 title_key => 'full_name_dep',
999 default => $self->order->cp_id,
1001 style => 'width: 300px',
1005 # build the selection box for shiptos
1007 # Needed, if customer/vendor changed.
1008 sub build_shipto_select {
1011 select_tag('order.shipto_id', [ $self->order->{$self->cv}->shipto ],
1012 value_key => 'shipto_id',
1013 title_key => 'displayable_id',
1014 default => $self->order->shipto_id,
1016 style => 'width: 300px',
1020 # render the info line for business
1022 # Needed, if customer/vendor changed.
1023 sub build_business_info_row
1025 $_[0]->p->render('order/tabs/_business_info_row', SELF => $_[0]);
1028 # build the rows for displaying taxes
1030 # Called if amounts where recalculated and redisplayed.
1031 sub build_tax_rows {
1035 foreach my $tax (sort { $a->{tax}->rate cmp $b->{tax}->rate } @{ $self->{taxes} }) {
1036 $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax, TAXINCLUDED => $self->order->taxincluded);
1038 return $rows_as_html;
1042 sub render_price_dialog {
1043 my ($self, $record_item) = @_;
1045 my $price_source = SL::PriceSource->new(record_item => $record_item, record => $self->order);
1049 'kivi.io.price_chooser_dialog',
1050 t8('Available Prices'),
1051 $self->render('order/tabs/_price_sources_dialog', { output => 0 }, price_source => $price_source)
1056 # $self->js->text('#dialog_flash_error_content', join ' ', @errors);
1057 # $self->js->show('#dialog_flash_error');
1066 return if !$::form->{id};
1068 $self->order(SL::DB::Manager::Order->find_by(id => $::form->{id}));
1071 # load or create a new order object
1073 # And assign changes from the form to this object.
1074 # If the order is loaded from db, check if items are deleted in the form,
1075 # remove them form the object and collect them for removing from db on saving.
1076 # Then create/update items from form (via _make_item) and add them.
1080 # add_items adds items to an order with no items for saving, but they cannot
1081 # be retrieved via items until the order is saved. Adding empty items to new
1082 # order here solves this problem.
1084 $order = SL::DB::Manager::Order->find_by(id => $::form->{id}) if $::form->{id};
1085 $order ||= SL::DB::Order->new(orderitems => [],
1086 quotation => (any { $self->type eq $_ } (_sales_quotation_type(), _request_quotation_type())));
1088 my $form_orderitems = delete $::form->{order}->{orderitems};
1089 my $form_periodic_invoices_config = delete $::form->{order}->{periodic_invoices_config};
1091 $order->assign_attributes(%{$::form->{order}});
1093 my $periodic_invoices_config = _make_periodic_invoices_config_from_yaml($form_periodic_invoices_config);
1094 $order->periodic_invoices_config($periodic_invoices_config) if $periodic_invoices_config;
1096 # remove deleted items
1097 $self->item_ids_to_delete([]);
1098 foreach my $idx (reverse 0..$#{$order->orderitems}) {
1099 my $item = $order->orderitems->[$idx];
1100 if (none { $item->id == $_->{id} } @{$form_orderitems}) {
1101 splice @{$order->orderitems}, $idx, 1;
1102 push @{$self->item_ids_to_delete}, $item->id;
1108 foreach my $form_attr (@{$form_orderitems}) {
1109 my $item = _make_item($order, $form_attr);
1110 $item->position($pos);
1114 $order->add_items(grep {!$_->id} @items);
1119 # create or update items from form
1121 # Make item objects from form values. For items already existing read from db.
1122 # Create a new item else. And assign attributes.
1124 my ($record, $attr) = @_;
1127 $item = first { $_->id == $attr->{id} } @{$record->items} if $attr->{id};
1129 my $is_new = !$item;
1131 # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
1132 # they cannot be retrieved via custom_variables until the order/orderitem is
1133 # saved. Adding empty custom_variables to new orderitem here solves this problem.
1134 $item ||= SL::DB::OrderItem->new(custom_variables => []);
1136 $item->assign_attributes(%$attr);
1137 $item->longdescription($item->part->notes) if $is_new && !defined $attr->{longdescription};
1138 $item->project_id($record->globalproject_id) if $is_new && !defined $attr->{project_id};
1139 $item->lastcost($record->is_sales ? $item->part->lastcost : 0) if $is_new && !defined $attr->{lastcost_as_number};
1146 # This is used to add one item
1148 my ($record, $attr) = @_;
1150 my $item = SL::DB::OrderItem->new;
1151 $item->assign_attributes(%$attr);
1153 my $part = SL::DB::Part->new(id => $attr->{parts_id})->load;
1154 my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
1156 $item->unit($part->unit) if !$item->unit;
1159 if ( $part->is_assortment ) {
1160 # add assortment items with price 0, as the components carry the price
1161 $price_src = $price_source->price_from_source("");
1162 $price_src->price(0);
1163 } elsif ($item->sellprice) {
1164 $price_src = $price_source->price_from_source("");
1165 $price_src->price($item->sellprice);
1167 $price_src = $price_source->best_price
1168 ? $price_source->best_price
1169 : $price_source->price_from_source("");
1170 $price_src->price(0) if !$price_source->best_price;
1174 if ($item->discount) {
1175 $discount_src = $price_source->discount_from_source("");
1176 $discount_src->discount($item->discount);
1178 $discount_src = $price_source->best_discount
1179 ? $price_source->best_discount
1180 : $price_source->discount_from_source("");
1181 $discount_src->discount(0) if !$price_source->best_discount;
1185 $new_attr{part} = $part;
1186 $new_attr{description} = $part->description if ! $item->description;
1187 $new_attr{qty} = 1.0 if ! $item->qty;
1188 $new_attr{price_factor_id} = $part->price_factor_id if ! $item->price_factor_id;
1189 $new_attr{sellprice} = $price_src->price;
1190 $new_attr{discount} = $discount_src->discount;
1191 $new_attr{active_price_source} = $price_src;
1192 $new_attr{active_discount_source} = $discount_src;
1193 $new_attr{longdescription} = $part->notes if ! defined $attr->{longdescription};
1194 $new_attr{project_id} = $record->globalproject_id;
1195 $new_attr{lastcost} = $record->is_sales ? $part->lastcost : 0;
1197 # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
1198 # they cannot be retrieved via custom_variables until the order/orderitem is
1199 # saved. Adding empty custom_variables to new orderitem here solves this problem.
1200 $new_attr{custom_variables} = [];
1202 $item->assign_attributes(%new_attr);
1207 # recalculate prices and taxes
1209 # Using the PriceTaxCalculator. Store linetotals in the item objects.
1213 # bb: todo: currency later
1214 $self->order->currency_id($::instance_conf->get_currency_id());
1216 my %pat = $self->order->calculate_prices_and_taxes();
1217 $self->{taxes} = [];
1218 foreach my $tax_chart_id (keys %{ $pat{taxes} }) {
1219 my $tax = SL::DB::Manager::Tax->find_by(chart_id => $tax_chart_id);
1221 my @amount_keys = grep { $pat{amounts}->{$_}->{tax_id} == $tax->id } keys %{ $pat{amounts} };
1222 push(@{ $self->{taxes} }, { amount => $pat{taxes}->{$tax_chart_id},
1223 netamount => $pat{amounts}->{$amount_keys[0]}->{amount},
1227 pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items}, @{$pat{items}};
1230 # get data for saving, printing, ..., that is not changed in the form
1232 # Only cvars for now.
1233 sub _get_unalterable_data {
1236 foreach my $item (@{ $self->order->items }) {
1237 # autovivify all cvars that are not in the form (cvars_by_config can do it).
1238 # workaround to pre-parse number-cvars (parse_custom_variable_values does not parse number values).
1239 foreach my $var (@{ $item->cvars_by_config }) {
1240 $var->unparsed_value($::form->parse_amount(\%::myconfig, $var->{__unparsed_value})) if ($var->config->type eq 'number' && exists($var->{__unparsed_value}));
1242 $item->parse_custom_variable_values;
1248 # And remove related files in the spool directory
1253 my $db = $self->order->db;
1255 $db->with_transaction(
1257 my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $self->order->id ]) };
1258 $self->order->delete;
1259 my $spool = $::lx_office_conf{paths}->{spool};
1260 unlink map { "$spool/$_" } @spoolfiles if $spool;
1263 }) || push(@{$errors}, $db->error);
1270 # And delete items that are deleted in the form.
1275 my $db = $self->order->db;
1277 $db->with_transaction(sub {
1278 SL::DB::OrderItem->new(id => $_)->delete for @{$self->item_ids_to_delete};
1279 $self->order->save(cascade => 1);
1282 if ($::form->{converted_from_oe_id}) {
1283 SL::DB::Order->new(id => $::form->{converted_from_oe_id})->load->link_to_record($self->order);
1285 if (scalar @{ $::form->{converted_from_orderitems_ids} || [] }) {
1287 foreach (@{ $self->order->items_sorted }) {
1288 my $from_id = $::form->{converted_from_orderitems_ids}->[$idx];
1290 SL::DB::RecordLink->new(from_table => 'orderitems',
1291 from_id => $from_id,
1292 to_table => 'orderitems',
1300 }) || push(@{$errors}, $db->error);
1305 sub _workflow_sales_or_purchase_order {
1308 my $destination_type = $::form->{type} eq _sales_quotation_type() ? _sales_order_type()
1309 : $::form->{type} eq _request_quotation_type() ? _purchase_order_type()
1312 $self->order(SL::DB::Order->new_from($self->order, destination_type => $destination_type));
1313 $self->{converted_from_oe_id} = delete $::form->{id};
1316 $::form->{type} = $destination_type;
1321 $self->_get_unalterable_data();
1322 $self->_pre_render();
1324 # trigger rendering values for second row/longdescription as hidden,
1325 # because they are loaded only on demand. So we need to keep the values
1327 $_->{render_second_row} = 1 for @{ $self->order->items_sorted };
1328 $_->{render_longdescription} = 1 for @{ $self->order->items_sorted };
1332 title => $self->_get_title_for('edit'),
1333 %{$self->{template_args}}
1341 $self->{all_taxzones} = SL::DB::Manager::TaxZone->get_all_sorted();
1342 $self->{all_departments} = SL::DB::Manager::Department->get_all_sorted();
1343 $self->{all_employees} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id,
1346 $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->salesman_id,
1349 $self->{all_projects} = SL::DB::Manager::Project->get_all(where => [ or => [ id => $self->order->globalproject_id,
1351 sort_by => 'projectnumber');
1352 $self->{all_payment_terms} = SL::DB::Manager::PaymentTerm->get_all_sorted(where => [ or => [ id => $self->order->payment_id,
1354 $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
1355 $self->{current_employee_id} = SL::DB::Manager::Employee->current->id;
1356 $self->{periodic_invoices_status} = $self->_get_periodic_invoices_status($self->order->periodic_invoices_config);
1357 $self->{order_probabilities} = [ map { { title => ($_ * 10) . '%', id => $_ * 10 } } (0..10) ];
1359 my $print_form = Form->new('');
1360 $print_form->{type} = $self->type;
1361 $print_form->{printers} = SL::DB::Manager::Printer->get_all_sorted;
1362 $print_form->{languages} = SL::DB::Manager::Language->get_all_sorted;
1363 $self->{print_options} = SL::Helper::PrintOptions->get_print_options(
1364 form => $print_form,
1365 options => {dialog_name_prefix => 'print_options.',
1369 no_opendocument => 1,
1373 foreach my $item (@{$self->order->orderitems}) {
1374 my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
1375 $item->active_price_source( $price_source->price_from_source( $item->active_price_source ));
1376 $item->active_discount_source($price_source->discount_from_source($item->active_discount_source));
1379 if ($self->order->ordnumber && $::instance_conf->get_webdav) {
1380 my $webdav = SL::Webdav->new(
1381 type => $self->type,
1382 number => $self->order->ordnumber,
1384 my @all_objects = $webdav->get_all_objects;
1385 @{ $self->{template_args}->{WEBDAV} } = map { { name => $_->filename,
1387 link => File::Spec->catfile($_->full_filedescriptor),
1391 $::request->{layout}->use_javascript("${_}.js") for qw(kivi.SalesPurchase kivi.Order kivi.File ckeditor/ckeditor ckeditor/adapters/jquery edit_periodic_invoices_config);
1392 $self->_setup_edit_action_bar;
1395 sub _setup_edit_action_bar {
1396 my ($self, %params) = @_;
1398 my $deletion_allowed = (any { $self->type eq $_ } (_sales_quotation_type(), _request_quotation_type()))
1399 || (($self->type eq _sales_order_type()) && $::instance_conf->get_sales_order_show_delete)
1400 || (($self->type eq _purchase_order_type()) && $::instance_conf->get_purchase_order_show_delete);
1402 for my $bar ($::request->layout->get('actionbar')) {
1407 call => [ 'kivi.Order.save', 'save', $::instance_conf->get_order_warn_duplicate_parts ],
1408 checks => [ 'kivi.Order.check_save_active_periodic_invoices' ],
1412 call => [ 'kivi.Order.save', 'save_as_new', $::instance_conf->get_order_warn_duplicate_parts ],
1413 checks => [ 'kivi.Order.check_save_active_periodic_invoices' ],
1414 disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef,
1417 t8('Save and Delivery Order'),
1418 call => [ 'kivi.Order.save', 'save_and_delivery_order', $::instance_conf->get_order_warn_duplicate_parts ],
1419 checks => [ 'kivi.Order.check_save_active_periodic_invoices' ],
1420 only_if => (any { $self->type eq $_ } (_sales_order_type(), _purchase_order_type()))
1423 t8('Save and Invoice'),
1424 call => [ 'kivi.Order.save', 'save_and_invoice', $::instance_conf->get_order_warn_duplicate_parts ],
1425 checks => [ 'kivi.Order.check_save_active_periodic_invoices' ],
1427 ], # end of combobox "Save"
1435 submit => [ '#order_form', { action => "Order/sales_order" } ],
1436 only_if => (any { $self->type eq $_ } (_sales_quotation_type())),
1437 disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef,
1440 t8('Purchase Order'),
1441 submit => [ '#order_form', { action => "Order/purchase_order" } ],
1442 only_if => (any { $self->type eq $_ } (_request_quotation_type())),
1443 disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef,
1445 ], # end of combobox "Workflow"
1453 call => [ 'kivi.Order.show_print_options' ],
1457 call => [ 'kivi.Order.email' ],
1460 t8('Download attachments of all parts'),
1461 call => [ 'kivi.File.downloadOrderitemsFiles', $::form->{type}, $::form->{id} ],
1462 disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef,
1463 only_if => $::instance_conf->get_doc_storage,
1465 ], # end of combobox "Export"
1469 call => [ 'kivi.Order.delete_order' ],
1470 confirm => $::locale->text('Do you really want to delete this object?'),
1471 disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef,
1472 only_if => $deletion_allowed,
1479 my ($order, $pdf_ref, $params) = @_;
1483 my $print_form = Form->new('');
1484 $print_form->{type} = $order->type;
1485 $print_form->{formname} = $params->{formname} || $order->type;
1486 $print_form->{format} = $params->{format} || 'pdf';
1487 $print_form->{media} = $params->{media} || 'file';
1488 $print_form->{groupitems} = $params->{groupitems};
1489 $print_form->{media} = 'file' if $print_form->{media} eq 'screen';
1491 $order->language($params->{language});
1492 $order->flatten_to_form($print_form, format_amounts => 1);
1494 # search for the template
1495 my ($template_file, @template_files) = SL::Helper::CreatePDF->find_template(
1496 name => $print_form->{formname},
1497 email => $print_form->{media} eq 'email',
1498 language => $params->{language},
1499 printer_id => $print_form->{printer_id}, # todo
1502 if (!defined $template_file) {
1503 push @errors, $::locale->text('Cannot find matching template for this print request. Please contact your template maintainer. I tried these: #1.', join ', ', map { "'$_'"} @template_files);
1506 return @errors if scalar @errors;
1508 $print_form->throw_on_error(sub {
1510 $print_form->prepare_for_printing;
1512 $$pdf_ref = SL::Helper::CreatePDF->create_pdf(
1513 template => $template_file,
1514 variables => $print_form,
1515 variable_content_types => {
1516 longdescription => 'html',
1517 partnotes => 'html',
1522 } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR;
1528 sub _get_files_for_email_dialog {
1531 my %files = map { ($_ => []) } qw(versions files vc_files part_files);
1533 return %files if !$::instance_conf->get_doc_storage;
1535 if ($self->order->id) {
1536 $files{versions} = [ SL::File->get_all_versions(object_id => $self->order->id, object_type => $self->order->type, file_type => 'document') ];
1537 $files{files} = [ SL::File->get_all( object_id => $self->order->id, object_type => $self->order->type, file_type => 'attachment') ];
1538 $files{vc_files} = [ SL::File->get_all( object_id => $self->order->{$self->cv}->id, object_type => $self->cv, file_type => 'attachment') ];
1542 uniq_by { $_->{id} }
1544 +{ id => $_->part->id,
1545 partnumber => $_->part->partnumber }
1546 } @{$self->order->items_sorted};
1548 foreach my $part (@parts) {
1549 my @pfiles = SL::File->get_all(object_id => $part->{id}, object_type => 'part');
1550 push @{ $files{part_files} }, map { +{ %{ $_ }, partnumber => $part->{partnumber} } } @pfiles;
1553 foreach my $key (keys %files) {
1554 $files{$key} = [ sort_by { lc $_->{db_file}->{file_name} } @{ $files{$key} } ];
1560 sub _make_periodic_invoices_config_from_yaml {
1561 my ($yaml_config) = @_;
1563 return if !$yaml_config;
1564 my $attr = YAML::Load($yaml_config);
1565 return if 'HASH' ne ref $attr;
1566 return SL::DB::PeriodicInvoicesConfig->new(%$attr);
1570 sub _get_periodic_invoices_status {
1571 my ($self, $config) = @_;
1573 return if $self->type ne _sales_order_type();
1574 return t8('not configured') if !$config;
1576 my $active = ('HASH' eq ref $config) ? $config->{active}
1577 : ('SL::DB::PeriodicInvoicesConfig' eq ref $config) ? $config->active
1578 : die "Cannot get status of periodic invoices config";
1580 return $active ? t8('active') : t8('inactive');
1583 sub _get_title_for {
1584 my ($self, $action) = @_;
1586 return '' if none { lc($action)} qw(add edit);
1589 # $::locale->text("Add Sales Order");
1590 # $::locale->text("Add Purchase Order");
1591 # $::locale->text("Add Quotation");
1592 # $::locale->text("Add Request for Quotation");
1593 # $::locale->text("Edit Sales Order");
1594 # $::locale->text("Edit Purchase Order");
1595 # $::locale->text("Edit Quotation");
1596 # $::locale->text("Edit Request for Quotation");
1598 $action = ucfirst(lc($action));
1599 return $self->type eq _sales_order_type() ? $::locale->text("$action Sales Order")
1600 : $self->type eq _purchase_order_type() ? $::locale->text("$action Purchase Order")
1601 : $self->type eq _sales_quotation_type() ? $::locale->text("$action Quotation")
1602 : $self->type eq _request_quotation_type() ? $::locale->text("$action Request for Quotation")
1606 sub _sales_order_type {
1610 sub _purchase_order_type {
1614 sub _sales_quotation_type {
1618 sub _request_quotation_type {
1619 'request_quotation';
1630 SL::Controller::Order - controller for orders
1634 This is a new form to enter orders, completely rewritten with the use
1635 of controller and java script techniques.
1637 The aim is to provide the user a better expirience and a faster flow
1638 of work. Also the code should be more readable, more reliable and
1647 One input row, so that input happens every time at the same place.
1651 Use of pickers where possible.
1655 Possibility to enter more than one item at once.
1659 Save order only on "save" (and "save and delivery order"-workflow). No
1660 hidden save on "print" or "email".
1664 Item list in a scrollable area, so that the workflow buttons stay at
1669 Reordering item rows with drag and drop is possible. Sorting item rows is
1670 possible (by partnumber, description, qty, sellprice and discount for now).
1674 No C<update> is necessary. All entries and calculations are managed
1675 with ajax-calls and the page does only reload on C<save>.
1679 User can see changes immediately, because of the use of java script
1690 =item * C<SL/Controller/Order.pm>
1694 =item * C<template/webpages/order/form.html>
1698 =item * C<template/webpages/order/tabs/basic_data.html>
1700 Main tab for basic_data.
1702 This is the only tab here for now. "linked records" and "webdav" tabs are
1703 reused from generic code.
1707 =item * C<template/webpages/order/tabs/_business_info_row.html>
1709 For displaying information on business type
1711 =item * C<template/webpages/order/tabs/_item_input.html>
1713 The input line for items
1715 =item * C<template/webpages/order/tabs/_row.html>
1717 One row for already entered items
1719 =item * C<template/webpages/order/tabs/_tax_row.html>
1721 Displaying tax information
1723 =item * C<template/webpages/order/tabs/_multi_items_dialog.html>
1725 Dialog for entering more than one item at once
1727 =item * C<template/webpages/order/tabs/_multi_items_result.html>
1729 Results for the filter in the multi items dialog
1731 =item * C<template/webpages/order/tabs/_price_sources_dialog.html>
1733 Dialog for selecting price and discount sources
1737 =item * C<js/kivi.Order.js>
1739 java script functions
1751 =item * customer/vendor details ('D'-button)
1753 =item * credit limit
1755 =item * more workflows (save as new, quotation, purchase order)
1757 =item * price sources: little symbols showing better price / better discount
1759 =item * select units in input row?
1761 =item * custom shipto address
1763 =item * language / part translations
1765 =item * access rights
1767 =item * display weights
1773 =item * optional client/user behaviour
1775 (transactions has to be set - department has to be set -
1776 force project if enabled in client config - transport cost reminder)
1780 =head1 KNOWN BUGS AND CAVEATS
1786 Customer discount is not displayed as a valid discount in price source popup
1787 (this might be a bug in price sources)
1789 (I cannot reproduce this (Bernd))
1793 No indication that <shift>-up/down expands/collapses second row.
1797 Inline creation of parts is not currently supported
1801 Table header is not sticky in the scrolling area.
1805 Sorting does not include C<position>, neither does reordering.
1807 This behavior was implemented intentionally. But we can discuss, which behavior
1808 should be implemented.
1812 C<show_multi_items_dialog> does not use the currently inserted string for
1817 The language selected in print or email dialog is not saved when the order is saved.
1821 =head1 To discuss / Nice to have
1827 How to expand/collapse second row. Now it can be done clicking the icon or
1832 Possibility to change longdescription in input row?
1836 Possibility to select PriceSources in input row?
1840 This controller uses a (changed) copy of the template for the PriceSource
1841 dialog. Maybe there could be used one code source.
1845 Rounding-differences between this controller (PriceTaxCalculator) and the old
1846 form. This is not only a problem here, but also in all parts using the PTC.
1847 There exists a ticket and a patch. This patch should be testet.
1851 An indicator, if the actual inputs are saved (like in an
1852 editor or on text processing application).
1856 A warning when leaving the page without saveing unchanged inputs.
1860 Workflows for delivery order and invoice are in the menu "Save", because the
1861 order is saved before opening the new document form. Nevertheless perhaps these
1862 workflow buttons should be put under "Workflows".
1869 Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt>