Auftrags-Controller: Webdav
[kivitendo-erp.git] / SL / Controller / Order.pm
index 8a4f245..9310f91 100644 (file)
@@ -9,6 +9,7 @@ use SL::Locale::String;
 use SL::SessionFile::Random;
 use SL::PriceSource;
 use SL::Form;
+use SL::Webdav;
 
 use SL::DB::Order;
 use SL::DB::Customer;
@@ -51,6 +52,7 @@ sub action_add {
   my ($self) = @_;
 
   $self->order->transdate(DateTime->now_local());
+  $self->order->reqdate(DateTime->today_local->next_workday) if !$self->order->reqdate;
 
   $self->_pre_render();
   $self->render(
@@ -124,9 +126,32 @@ sub action_create_pdf {
   my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
   $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
 
-  $::form->{formname}  = $self->type;
-  $::form->{language} = 'de';
-  my $pdf_filename =  $::form->get_formname_translation . '_' . $self->order->ordnumber . '.pdf';
+  my $form = Form->new;
+  $form->{ordnumber} = $self->order->ordnumber;
+  $form->{formname}  = $self->type;
+  $form->{type}      = $self->type;
+  $form->{language}  = 'de';
+  $form->{format}    = 'pdf';
+
+  my $pdf_filename = $form->generate_attachment_filename();
+
+  # 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', $@));
+    }
+  }
 
   $self->js
     ->run('download_pdf', $pdf_filename, $key)
@@ -287,53 +312,7 @@ sub action_add_item {
 
   return unless $form_attr->{parts_id};
 
-  my $item = SL::DB::OrderItem->new;
-  $item->assign_attributes(%$form_attr);
-
-  my $part         = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
-
-  my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
-
-  my $price_src;
-  if ($item->sellprice) {
-    $price_src = $price_source->price_from_source("");
-    $price_src->price($item->sellprice);
-  } else {
-    $price_src = $price_source->best_price
-           ? $price_source->best_price
-           : $price_source->price_from_source("");
-    $price_src->price(0) if !$price_source->best_price;
-  }
-
-  # bb: not sure but: maybe there should be a $price_source->discount_from_source
-  # which can alse return an empty_discout if source is "".
-  my $discount;
-  my $discount_src;
-  if ($item->discount) {
-    $discount = $item->discount;
-  } else {
-    $discount = $price_source->best_discount
-              ? $price_source->best_discount->discount
-              : 0;
-    $discount_src = $price_source->best_discount->source if $price_source->best_discount;
-  }
-
-  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{sellprice}              = $price_src->price;
-  $new_attr{discount}               = $discount;
-  $new_attr{active_price_source}    = $price_src;
-  $new_attr{active_discount_source} = $discount_src;
-
-  # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
-  # they cannot be retrieved via custom_variables until the order/orderitem is
-  # saved. Adding empty custom_variables to new orderitem here solves this problem.
-  $new_attr{custom_variables} = [];
-
-  $item->assign_attributes(%new_attr);
-
+  my $item = _make_item($self->order, $form_attr);
   $self->order->add_items($item);
 
   $self->_recalc();
@@ -352,6 +331,53 @@ sub action_add_item {
     ->run('row_table_scroll_down')
     ->run('row_set_keyboard_events_by_id', $item_id)
     ->on('.recalc', 'change', 'recalc_amounts_and_taxes')
+    ->on('.reformat_number', 'change', 'reformat_number')
+    ->focus('#add_item_parts_id_name');
+
+  $self->_js_redisplay_amounts_and_taxes;
+  $self->js->render();
+}
+
+sub action_show_multi_items_dialog {
+  my ($self) = @_;
+
+  $self->{multi_items}->{parts} = SL::DB::Manager::Part->get_all_sorted(where => [ obsolete => 0 ]);
+  my $dialog_html = $self->render('order/tabs/_multi_items_dialog', { output => 0 });
+
+  $self->js
+    ->run('show_multi_items_dialog', $dialog_html, t8('Add multiple parts'))
+    ->reinit_widgets
+    ->render();
+}
+
+sub action_add_multi_items {
+  my ($self) = @_;
+
+  my @form_attr = grep { $_->{qty} } @{ $::form->{add_multi_items} };
+  return unless scalar @form_attr;
+
+  my @items;
+  foreach my $attr (@form_attr) {
+    push @items, _make_item($self->order, $attr);
+  }
+  $self->order->add_items(@items);
+
+  $self->_recalc();
+
+  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);
+
+    $self->js
+        ->append('#row_table_id', $row_as_html)
+        ->run('row_set_keyboard_events_by_id', $item_id);
+  }
+
+  $self->js
+    ->run('close_multi_items_dialog')
+    ->run('row_table_scroll_down')
+    ->on('.recalc', 'change', 'recalc_amounts_and_taxes')
+    ->on('.reformat_number', 'change', 'reformat_number')
     ->focus('#add_item_parts_id_name');
 
   $self->_js_redisplay_amounts_and_taxes;
@@ -525,6 +551,58 @@ sub _make_order {
   return $order;
 }
 
+sub _make_item {
+  my ($record, $attr) = @_;
+
+  my $item = SL::DB::OrderItem->new;
+  $item->assign_attributes(%$attr);
+
+  my $part         = SL::DB::Part->new(id => $attr->{parts_id})->load;
+  my $price_source = SL::PriceSource->new(record_item => $item, record => $record);
+
+  $item->unit($part->unit) if !$item->unit;
+
+  my $price_src;
+  if ($item->sellprice) {
+    $price_src = $price_source->price_from_source("");
+    $price_src->price($item->sellprice);
+  } else {
+    $price_src = $price_source->best_price
+           ? $price_source->best_price
+           : $price_source->price_from_source("");
+    $price_src->price(0) if !$price_source->best_price;
+  }
+
+  my $discount_src;
+  if ($item->discount) {
+    $discount_src = $price_source->discount_from_source("");
+    $discount_src->discount($item->discount);
+  } else {
+    $discount_src = $price_source->best_discount
+                  ? $price_source->best_discount
+                  : $price_source->discount_from_source("");
+    $discount_src->discount(0) if !$price_source->best_discount;
+  }
+
+  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{sellprice}              = $price_src->price;
+  $new_attr{discount}               = $discount_src->discount;
+  $new_attr{active_price_source}    = $price_src;
+  $new_attr{active_discount_source} = $discount_src;
+
+  # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
+  # they cannot be retrieved via custom_variables until the order/orderitem is
+  # saved. Adding empty custom_variables to new orderitem here solves this problem.
+  $new_attr{custom_variables} = [];
+
+  $item->assign_attributes(%new_attr);
+
+  return $item;
+}
+
 sub _recalc {
   my ($self) = @_;
 
@@ -588,6 +666,7 @@ sub _pre_render {
   my ($self) = @_;
 
   $self->{all_taxzones}        = SL::DB::Manager::TaxZone->get_all_sorted();
+  $self->{all_departments}     = SL::DB::Manager::Department->get_all_sorted();
   $self->{all_employees}       = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id,
                                                                                        deleted => 0 ] ],
                                                                     sort_by => 'name');
@@ -604,13 +683,22 @@ sub _pre_render {
 
   foreach  my $item (@{$self->order->items}) {
     my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
+    $item->active_price_source(   $price_source->price_from_source(   $item->active_price_source   ));
+    $item->active_discount_source($price_source->discount_from_source($item->active_discount_source));
 
-    my $price_src = $price_source->price_from_source($item->active_price_source);
-    $item->active_price_source($price_src);
+  }
 
-    my $discount_src;
-    $discount_src = $price_source->price_from_source($item->active_discount_source)->source if $item->active_discount_source;
-    $item->active_discount_source($discount_src);
+  if ($self->order->ordnumber && $::instance_conf->get_webdav) {
+    my $webdav = SL::Webdav->new(
+      type     => $self->type,
+      number   => $self->order->ordnumber,
+    );
+    my $webdav_path = $webdav->webdav_path;
+    my @all_objects = $webdav->get_all_objects;
+    @{ $self->{template_args}->{WEBDAV} } = map { { name => $_->filename,
+                                                    type => t8('File'),
+                                                    link => File::Spec->catdir($webdav_path, $_->filename),
+                                                } } @all_objects;
   }
 
   $::request->{layout}->use_javascript("${_}.js")  for qw(ckeditor/ckeditor ckeditor/adapters/jquery);