X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/b39653f82441d0db075db54950752f6efa267e1c..9db40ab09e6cd8266db1817ccd16887480b757f6:/SL/Controller/Order.pm diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 55f3f747f..86ccb7297 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -11,6 +11,7 @@ use SL::PriceSource; use SL::Webdav; use SL::File; use SL::Util qw(trim); +use SL::YAML; use SL::DB::Order; use SL::DB::Default; use SL::DB::Unit; @@ -32,6 +33,7 @@ use List::MoreUtils qw(any none pairwise first_index); use English qw(-no_match_vars); use File::Spec; use Cwd; +use Sort::Naturally; use Rose::Object::MakeMethods::Generic ( @@ -58,9 +60,11 @@ sub action_add { my ($self) = @_; $self->order->transdate(DateTime->now_local()); - my $extra_days = $self->type eq sales_quotation_type() ? $::instance_conf->get_reqdate_interval : 1; + my $extra_days = $self->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval : + $self->{type} eq 'sales_order' ? $::instance_conf->get_delivery_date_interval : 1; $self->order->reqdate(DateTime->today_local->next_workday(extra_days => $extra_days)) if !$self->order->reqdate; + $self->pre_render(); $self->render( 'order/form', @@ -210,7 +214,8 @@ sub action_save_as_new { # 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; + my $extra_days = $self->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval : + $self->{type} eq 'sales_order' ? $::instance_conf->get_delivery_date_interval : 1; $new_attrs{reqdate} = DateTime->today_local->next_workday(extra_days => $extra_days); } else { $new_attrs{reqdate} = $order->reqdate; @@ -488,7 +493,7 @@ sub action_show_periodic_invoices_config_dialog { $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, + start_date_as_date => $::form->{transdate_as_date} || $::form->current_date, extend_automatically_by => 12, active => 1, email_subject => GenericTranslations->get( @@ -509,6 +514,7 @@ sub action_show_periodic_invoices_config_dialog { if ($::form->{customer_id}) { $::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(where => [ cp_cv_id => $::form->{customer_id} ]); + $::form->{email_recipient_invoice_address} = SL::DB::Manager::Customer->find_by(id => $::form->{customer_id})->invoice_mail; } $self->render('oe/edit_periodic_invoices_config', { layout => 0 }, @@ -547,7 +553,7 @@ sub action_assign_periodic_invoices_config { email_body => $::form->{email_body}, }; - my $periodic_invoices_config = YAML::Dump($config); + my $periodic_invoices_config = SL::YAML::Dump($config); my $status = $self->get_periodic_invoices_status($config); @@ -905,9 +911,17 @@ sub action_reorder_items { my $method = $sort_keys{$::form->{order_by}}; my @to_sort = map { { old_pos => $_->position, order_by => $method->($_) } } @{ $self->order->items_sorted }; if ($::form->{sort_dir}) { - @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort; + if ( $::form->{order_by} =~ m/qty|sellprice|discount/ ){ + @to_sort = sort { $a->{order_by} <=> $b->{order_by} } @to_sort; + } else { + @to_sort = sort { $a->{order_by} cmp $b->{order_by} } @to_sort; + } } else { - @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort; + if ( $::form->{order_by} =~ m/qty|sellprice|discount/ ){ + @to_sort = sort { $b->{order_by} <=> $a->{order_by} } @to_sort; + } else { + @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort; + } } $self->js ->run('kivi.Order.redisplay_items', \@to_sort) @@ -977,8 +991,8 @@ sub js_load_second_row { my $row_as_html = $self->p->render('order/tabs/_second_row', ITEM => $item, TYPE => $self->type); $self->js - ->html('.row_entry:has(#item_' . $item_id . ') [name = "second_row"]', $row_as_html) - ->data('.row_entry:has(#item_' . $item_id . ') [name = "second_row"]', 'loaded', 1); + ->html('#second_row_' . $item_id, $row_as_html) + ->data('#second_row_' . $item_id, 'loaded', 1); } sub js_redisplay_line_values { @@ -1204,7 +1218,7 @@ sub make_order { # be retrieved via items until the order is saved. Adding empty items to new # order here solves this problem. my $order; - $order = SL::DB::Manager::Order->find_by(id => $::form->{id}) if $::form->{id}; + $order = SL::DB::Order->new(id => $::form->{id})->load(with => [ 'orderitems', 'orderitems.part' ]) if $::form->{id}; $order ||= SL::DB::Order->new(orderitems => [], quotation => (any { $self->type eq $_ } (sales_quotation_type(), request_quotation_type()))); @@ -1219,7 +1233,7 @@ sub make_order { $order->assign_attributes(%{$::form->{order}}); - if (my $periodic_invoices_config_attrs = $form_periodic_invoices_config ? YAML::Load($form_periodic_invoices_config) : undef) { + if (my $periodic_invoices_config_attrs = $form_periodic_invoices_config ? SL::YAML::Load($form_periodic_invoices_config) : undef) { my $periodic_invoices_config = $order->periodic_invoices_config || $order->periodic_invoices_config(SL::DB::PeriodicInvoicesConfig->new); $periodic_invoices_config->assign_attributes(%$periodic_invoices_config_attrs); } @@ -1379,7 +1393,7 @@ sub recalc { tax => $tax }); } - pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items}, @{$pat{items}}; + pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items_sorted}, @{$pat{items}}; } # get data for saving, printing, ..., that is not changed in the form @@ -1464,6 +1478,14 @@ sub save { sub workflow_sales_or_purchase_order { my ($self) = @_; + # always save + my $errors = $self->save(); + + if (scalar @{ $errors }) { + $self->js->flash('error', $_) foreach @{ $errors }; + return $self->js->render(); + } + my $destination_type = $::form->{type} eq sales_quotation_type() ? sales_order_type() : $::form->{type} eq request_quotation_type() ? purchase_order_type() : $::form->{type} eq purchase_order_type() ? sales_order_type() @@ -1591,13 +1613,13 @@ sub setup_edit_action_bar { t8('Workflow'), ], action => [ - t8('Sales Order'), + t8('Save and Sales Order'), submit => [ '#order_form', { action => "Order/sales_order" } ], only_if => (any { $self->type eq $_ } (sales_quotation_type(), purchase_order_type())), disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, ], action => [ - t8('Purchase Order'), + t8('Save and Purchase Order'), submit => [ '#order_form', { action => "Order/purchase_order" } ], only_if => (any { $self->type eq $_ } (sales_order_type(), request_quotation_type())), disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, @@ -1702,7 +1724,7 @@ sub generate_pdf { }, ); 1; - } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR; + } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->error : $EVAL_ERROR; }); return @errors; @@ -1744,7 +1766,7 @@ sub make_periodic_invoices_config_from_yaml { my ($yaml_config) = @_; return if !$yaml_config; - my $attr = YAML::Load($yaml_config); + my $attr = SL::YAML::Load($yaml_config); return if 'HASH' ne ref $attr; return SL::DB::PeriodicInvoicesConfig->new(%$attr); }