+
+sub sort {
+ $main::lxdebug->enter_sub();
+
+ check_do_access();
+
+ my $form = $main::form;
+ my %temp_hash;
+
+ save(no_redirect => 1); # has to be done, at least for newly added positions
+
+ # hashify partnumbers, positions. key is delivery_order_items_id
+ for my $i (1 .. ($form->{rowcount}) ) {
+ $temp_hash{$form->{"delivery_order_items_id_$i"}} = { runningnumber => $form->{"runningnumber_$i"}, partnumber => $form->{"partnumber_$i"} };
+ if ($form->{id} && $form->{"discount_$i"}) {
+ # prepare_order assumes a db value if there is a form->id and multiplies *100
+ # We hope for new controller code (no more format_amount/parse_amount distinction)
+ $form->{"discount_$i"} /=100;
+ }
+ }
+ # naturally sort partnumbers and get a sorted array of doi_ids
+ my @sorted_doi_ids = sort { Sort::Naturally::ncmp($temp_hash{$a}->{"partnumber"}, $temp_hash{$b}->{"partnumber"}) } keys %temp_hash;
+
+
+ my $new_number = 1;
+
+ for (@sorted_doi_ids) {
+ $form->{"runningnumber_$temp_hash{$_}->{runningnumber}"} = $new_number;
+ $new_number++;
+ }
+ # all parse_amounts changes are in form (i.e. , to .) therefore we need
+ # another format_amount to change it back, for the next save ;-(
+ # works great except for row discounts (see above comment)
+ prepare_order();
+
+
+ $main::lxdebug->leave_sub();
+ save();
+}
+
+__END__
+
+=pod
+
+=encoding utf8
+
+=head1 NAME
+
+do.pl - Script for all calls to delivery order
+
+
+=head1 FUNCTIONS
+
+=over 2
+
+=item C<sort>
+
+Sorts all position with Natural Sort. Can be activated in form_footer.html like this
+C<E<lt>input class="submit" type="submit" name="action_sort" id="sort_button" value="[% 'Sort and Save' | $T8 %]"E<gt>>
+
+=back
+
+=head1 TODO
+
+Sort and Save can be implemented as an optional button if configuration ca be set by client config.
+Example coding for database scripts and templates in (git show af2f24b8), check also
+autogeneration for rose (scripts/rose_auto_create_model.pl --h)