ProjectPicker: SL::DB::Manager::Project hat (noch) keinen type_filter.
[kivitendo-erp.git] / SL / Controller / Order.pm
index c980db7..8ed5b33 100644 (file)
@@ -21,6 +21,7 @@ use SL::DB::Project;
 use SL::DB::Default;
 use SL::DB::Unit;
 use SL::DB::Price;
+use SL::DB::PriceFactor;
 use SL::DB::Part;
 use SL::DB::Printer;
 use SL::DB::Language;
@@ -39,7 +40,7 @@ use File::Spec;
 use Rose::Object::MakeMethods::Generic
 (
  scalar => [ qw(item_ids_to_delete) ],
- 'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models) ],
+ 'scalar --get_set_init' => [ qw(order valid_types type cv p multi_items_models all_price_factors) ],
 );
 
 
@@ -47,7 +48,7 @@ use Rose::Object::MakeMethods::Generic
 __PACKAGE__->run_before('_check_auth');
 
 __PACKAGE__->run_before('_recalc',
-                        only => [ qw(update save save_and_delivery_order print create_pdf send_email) ]);
+                        only => [ qw(save save_and_delivery_order print create_pdf send_email) ]);
 
 __PACKAGE__->run_before('_get_unalterable_data',
                         only => [ qw(save save_and_delivery_order print create_pdf send_email) ]);
@@ -87,19 +88,6 @@ sub action_edit {
   );
 }
 
-sub action_update {
-  my ($self) = @_;
-
-  $self->_pre_render();
-  $self->render(
-    'order/form',
-    title => $self->type eq _sales_order_type()    ? $::locale->text('Edit Sales Order')
-           : $self->type eq _purchase_order_type() ? $::locale->text('Edit Purchase Order')
-           : '',
-    %{$self->{template_args}}
-  );
-}
-
 sub action_delete {
   my ($self) = @_;
 
@@ -188,26 +176,16 @@ sub action_print {
     $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
 
     $self->js
-    ->run('download_pdf', $pdf_filename, $key)
+    ->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};
-    my $printer;
-    $printer = SL::DB::Printer->new(id => $printer_id)->load if $printer_id;
-    if (!$printer) {
-      return $self->js->flash('error', t8('Printer not found.'))->render;
-    }
-
-    my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command);
-
-    for my $i (1 .. $copies) {
-      open my $out, '|-', $command or die $!;
-      binmode $out;
-      print $out $pdf;
-      close $out;
-    }
+    SL::DB::Printer->new(id => $printer_id)->load->print_document(
+      copies  => $copies,
+      content => $pdf,
+    );
 
     $self->js->flash('info', t8('The PDF has been printed'));
   }
@@ -274,7 +252,7 @@ sub action_show_email_dialog {
 
   my $dialog_html = $self->render('order/tabs/_email_dialog', { output => 0 });
   $self->js
-      ->run('show_email_dialog', $dialog_html)
+      ->run('kivi.Order.show_email_dialog', $dialog_html)
       ->reinit_widgets
       ->render($self);
 }
@@ -314,7 +292,7 @@ sub action_send_email {
 
   $self->js
       ->val('#order_intnotes', $intnotes)
-      ->run('close_email_dialog')
+      ->run('kivi.Order.close_email_dialog')
       ->render($self);
 }
 
@@ -394,7 +372,7 @@ sub action_unit_changed {
   $self->_recalc();
 
   $self->js
-    ->run('update_sellprice', $::form->{item_id}, $item->sellprice_as_number);
+    ->run('kivi.Order.update_sellprice', $::form->{item_id}, $item->sellprice_as_number);
   $self->_js_redisplay_linetotals;
   $self->_js_redisplay_amounts_and_taxes;
   $self->js->render();
@@ -413,17 +391,18 @@ sub action_add_item {
   $self->_recalc();
 
   my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
-  my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id);
+  my $row_as_html = $self->p->render('order/tabs/_row',
+                                     ITEM              => $item,
+                                     ID                => $item_id,
+                                     ALL_PRICE_FACTORS => $self->all_price_factors
+  );
 
   $self->js
     ->append('#row_table_id', $row_as_html)
     ->val('.add_item_input', '')
-    ->run('row_table_scroll_down')
-    ->run('row_set_keyboard_events_by_id', $item_id)
-    ->run('set_unit_change_with_oldval_by_id', $item_id)
-    ->run('renumber_positions')
-    ->on('.recalc', 'change', 'recalc_amounts_and_taxes')
-    ->on('.reformat_number', 'change', 'reformat_number')
+    ->run('kivi.Order.init_row_handlers')
+    ->run('kivi.Order.row_table_scroll_down')
+    ->run('kivi.Order.renumber_positions')
     ->focus('#add_item_parts_id_name');
 
   $self->_js_redisplay_amounts_and_taxes;
@@ -438,6 +417,9 @@ sub action_show_multi_items_dialog {
 
 sub action_multi_items_update_result {
   my $max_count = 100;
+
+  $::form->{multi_items}->{filter}->{obsolete} = 0;
+
   my $count = $_[0]->multi_items_models->count;
 
   if ($count == 0) {
@@ -469,20 +451,20 @@ sub action_add_multi_items {
 
   foreach my $item (@items) {
     my $item_id = join('_', 'new', Time::HiRes::gettimeofday(), int rand 1000000000000);
-    my $row_as_html = $self->p->render('order/tabs/_row', ITEM => $item, ID => $item_id);
+    my $row_as_html = $self->p->render('order/tabs/_row',
+                                       ITEM              => $item,
+                                       ID                => $item_id,
+                                       ALL_PRICE_FACTORS => $self->all_price_factors
+    );
 
-    $self->js
-        ->append('#row_table_id', $row_as_html)
-        ->run('row_set_keyboard_events_by_id', $item_id)
-        ->run('set_unit_change_with_oldval_by_id', $item_id);
+    $self->js->append('#row_table_id', $row_as_html);
   }
 
   $self->js
-    ->run('close_multi_items_dialog')
-    ->run('row_table_scroll_down')
-    ->run('renumber_positions')
-    ->on('.recalc', 'change', 'recalc_amounts_and_taxes')
-    ->on('.reformat_number', 'change', 'reformat_number')
+    ->run('kivi.Order.close_multi_items_dialog')
+    ->run('kivi.Order.init_row_handlers')
+    ->run('kivi.Order.row_table_scroll_down')
+    ->run('kivi.Order.renumber_positions')
     ->focus('#add_item_parts_id_name');
 
   $self->_js_redisplay_amounts_and_taxes;
@@ -518,7 +500,7 @@ sub action_reorder_items {
     @to_sort = sort { $b->{order_by} cmp $a->{order_by} } @to_sort;
   }
   $self->js
-    ->run('redisplay_items', \@to_sort)
+    ->run('kivi.Order.redisplay_items', \@to_sort)
     ->render;
 }
 
@@ -548,7 +530,7 @@ sub _js_redisplay_linetotals {
 
   my @data = map {$::form->format_amount(\%::myconfig, $_->{linetotal}, 2, 0)} @{ $self->order->items_sorted };
   $self->js
-    ->run('redisplay_linetotals', \@data);
+    ->run('kivi.Order.redisplay_linetotals', \@data);
 }
 
 sub _js_redisplay_amounts_and_taxes {
@@ -626,6 +608,10 @@ sub init_multi_items_models {
   );
 }
 
+sub init_all_price_factors {
+  SL::DB::Manager::PriceFactor->get_all;
+}
+
 sub _check_auth {
   my ($self) = @_;
 
@@ -794,8 +780,9 @@ sub _new_item {
 
   my %new_attr;
   $new_attr{part}                   = $part;
-  $new_attr{description}            = $part->description if ! $item->description;
-  $new_attr{qty}                    = 1.0                if ! $item->qty;
+  $new_attr{description}            = $part->description     if ! $item->description;
+  $new_attr{qty}                    = 1.0                    if ! $item->qty;
+  $new_attr{price_factor_id}        = $part->price_factor_id if ! $item->price_factor_id;
   $new_attr{sellprice}              = $price_src->price;
   $new_attr{discount}               = $discount_src->discount;
   $new_attr{active_price_source}    = $price_src;
@@ -936,7 +923,7 @@ sub _pre_render {
                                                 } } @all_objects;
   }
 
-  $::request->{layout}->use_javascript("${_}.js")  for qw(kivi.SalesPurchase ckeditor/ckeditor ckeditor/adapters/jquery);
+  $::request->{layout}->use_javascript("${_}.js")  for qw(kivi.SalesPurchase kivi.Order ckeditor/ckeditor ckeditor/adapters/jquery);
 }
 
 sub _create_pdf {
@@ -1001,3 +988,93 @@ sub _purchase_order_type {
 }
 
 1;
+
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+SL::Controller::Order - controller for orders
+
+=head1 TODO
+
+=over 2
+
+=item *
+
+testing
+
+
+=item *
+
+currency
+
+
+=item *
+
+customer/vendor details ('D'-button)
+
+
+=item *
+
+credit limit
+
+
+=item *
+
+more workflows (save as new / invoice)
+
+
+=item *
+
+price sources: little symbols showing better price / better discount
+
+
+=item *
+
+custom shipto address
+
+
+=item *
+
+periodic invoices
+
+
+=item *
+
+more details on second row (marge, ...)
+
+
+=item *
+
+language / part translations
+
+
+=item *
+
+access rights
+
+
+=item *
+
+preset salesman from customer
+
+
+=item *
+
+display weights
+
+
+=item *
+
+force project if enabled in client config
+
+
+=back
+
+=head1 AUTHOR
+
+Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt>
+
+=cut