Merge pull request #11 from freiphone/patch-3
[kivitendo-erp.git] / bin / mozilla / io.pl
index bbbf5f3..778c718 100644 (file)
@@ -63,6 +63,7 @@ use SL::DB::Vendor;
 use SL::Helper::CreatePDF;
 use SL::Helper::Flash;
 use SL::Helper::PrintOptions;
+use SL::Helper::ShippedQty;
 
 require "bin/mozilla/common.pl";
 
@@ -560,13 +561,15 @@ sub item_selected {
   my $row = $curr_row;
 
   if ($myconfig{item_multiselect}) {
-    foreach (grep(/^select_qty_/, keys(%{ $form }))) {
+    my %multi_items;
+    for (keys %$form) {
       next unless $form->{$_};
-      $_ =~ /^select_qty_(\d+)/;
-      $form->{"id_${row}"}  = $1;
-      $form->{"qty_${row}"} = $form->{$_};
+      next unless /^select_qty_(\d+)/;
+      $multi_items{"id_${row}"}  = $1;
+      $multi_items{"qty_${row}"} = $form->{$_};
       $row++;
     }
+    $form->{$_} = $multi_items{$_} for keys %multi_items;
   } else {
     $form->{"id_${row}"} = delete($form->{select_item_id}) || croak 'Missing item selection ID';
     $row++;
@@ -695,34 +698,39 @@ sub item_selected {
 }
 
 sub new_item {
-  $main::lxdebug->enter_sub();
+  _check_io_auth();
 
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
+  my $price = $::form->{vc} eq 'customer' ? 'sellprice_as_number' : 'lastcost_as_number';
+  my $previousform = $::auth->save_form_in_session;
+  my $callback     = build_std_url("action=return_from_new_item", "previousform=$previousform");
+  my $i            = $::form->{rowcount};
 
-  _check_io_auth();
+  my $parts_classification_type = $::form->{vc} eq 'customer' ? 'sales' : 'purchases';
 
-  my $price_key = ($form->{type} =~ m/request_quotation|purchase_order/) || ($form->{script} eq 'ir.pl') ? 'lastcost' : 'sellprice';
+  my @HIDDENS;
+  push @HIDDENS,      { 'name' => 'callback',     'value' => $callback };
+  push @HIDDENS, map +{ 'name' => $_,             'value' => $::form->{$_} },        qw(rowcount vc);
+  push @HIDDENS, map +{ 'name' => "part.$_",      'value' => $::form->{"${_}_$i"} }, qw(partnumber description unit price_factor_id);
+  push @HIDDENS,      { 'name' => "part.$price",  'value' => $::form->{"sellprice_$i"} };
+  push @HIDDENS,      { 'name' => "part.notes",   'value' => $::form->{"longdescription_$i"} };
 
-  # change callback
-  $form->{old_callback} = $form->escape($form->{callback}, 1);
-  $form->{callback}     = $form->escape("$form->{script}?action=display_form", 1);
+  push @HIDDENS,      { 'name' => "parts_classification_type", 'value' => $parts_classification_type };
 
-  # save all form variables except action in the session and keep the key in the previousform variable
-  my $previousform = $::auth->save_form_in_session(skip_keys => [ qw(action) ]);
+  $::form->header;
+  print $::form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
+}
 
-  my @HIDDENS;
-  push @HIDDENS,      { 'name' => 'previousform', 'value' => $previousform };
-  push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{$_} },                       qw(rowcount vc);
-  push @HIDDENS, map +{ 'name' => $_,             'value' => $form->{"${_}_$form->{rowcount}"} }, qw(partnumber description unit);
-  push @HIDDENS,      { 'name' => 'taxaccount2',  'value' => $form->{taxaccounts} };
-  push @HIDDENS,      { 'name' => $price_key,     'value' => $form->parse_amount(\%myconfig, $form->{"sellprice_$form->{rowcount}"}) };
-  push @HIDDENS,      { 'name' => 'notes',        'value' => $form->{"longdescription_$form->{rowcount}"} };
+sub return_from_new_item {
+  _check_io_auth();
 
-  $form->header();
-  print $form->parse_html_template("generic/new_item", { HIDDENS => [ sort { $a->{name} cmp $b->{name} } @HIDDENS ] } );
+  my $part = SL::DB::Manager::Part->find_by(id => delete $::form->{new_parts_id}) or die 'can not find part that was just saved!';
 
-  $main::lxdebug->leave_sub();
+  $::auth->restore_form_from_session(delete $::form->{previousform}, form => $::form);
+
+  $::form->{"id_$::form->{rowcount}"} = $part->id;
+
+  my $url = build_std_url("script=$::form->{script}", "RESTORE_FORM_FROM_SESSION_ID=" . $::auth->save_form_in_session);
+  print $::request->{cgi}->redirect($url);
 }
 
 sub check_form {
@@ -1662,37 +1670,14 @@ sub _update_part_information {
 }
 
 sub _update_ship {
-  $main::lxdebug->enter_sub();
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-
-  if (!$form->{ordnumber} || !$form->{id}) {
-    map { $form->{"ship_$_"} = 0 } (1..$form->{rowcount});
-    $main::lxdebug->leave_sub();
-    return;
-  }
-
-  my $all_units = AM->retrieve_all_units();
-
-  my %ship = DO->get_shipped_qty('oe_id' => $form->{id});
-
-  foreach my $i (1..$form->{rowcount}) {
-    next unless ($form->{"id_${i}"});
-
-    $form->{"ship_$i"} = 0;
-
-    my $ship_entry = $ship{$i};
-
-    next if (!$ship_entry || ($ship_entry->{qty_ordered} <= 0));
+  return unless $::form->{id};
+  my $helper = SL::Helper::ShippedQty->new->calculate($::form->{id});
 
-    my $rowqty = $ship_entry->{qty_ordered} - $ship_entry->{qty_notdelivered};
-    $rowqty   *= $all_units->{$form->{"unit_$i"}}->{factor} /
-                 $all_units->{$form->{"partunit_$i"}}->{factor} if !$form->{simple_save};
-    $form->{"ship_$i"}  = $rowqty;
+  for my $i (1..$::form->{rowcount}) {
+    if (my $oid = $::form->{"orderitems_id_$i"}) {
+      $::form->{"ship_$i"} = $helper->shipped_qty->{$oid};
+    }
   }
-
-  $main::lxdebug->leave_sub();
 }
 
 sub _update_custom_variables {
@@ -1952,10 +1937,12 @@ sub _get_files_for_email_dialog {
 
   return %files if !$::instance_conf->get_doc_storage;
 
-  $files{versions} = [ SL::File->get_all_versions(object_id => $::form->{id},    object_type => $::form->{type}, file_type => 'document') ];
-  $files{files}    = [ SL::File->get_all(         object_id => $::form->{id},    object_type => $::form->{type}, file_type => 'attachment') ];
-  $files{vc_files} = [ SL::File->get_all(         object_id => $::form->{vc_id}, object_type => $::form->{vc},   file_type => 'attachment') ]
-    if $::form->{vc} && $::form->{"vc_id"};
+  if ($::form->{id}) {
+    $files{versions} = [ SL::File->get_all_versions(object_id => $::form->{id},    object_type => $::form->{type}, file_type => 'document') ];
+    $files{files}    = [ SL::File->get_all(         object_id => $::form->{id},    object_type => $::form->{type}, file_type => 'attachment') ];
+    $files{vc_files} = [ SL::File->get_all(         object_id => $::form->{vc_id}, object_type => $::form->{vc},   file_type => 'attachment') ]
+      if $::form->{vc} && $::form->{"vc_id"};
+  }
 
   my @parts =
     uniq_by { $_->{id} }
@@ -1979,10 +1966,22 @@ sub _get_files_for_email_dialog {
 }
 
 sub show_sales_purchase_email_dialog {
-  my $contact    = $::form->{cp_id} ? SL::DB::Contact->load_cached($::form->{cp_id}) : undef;
+  my $email = '';
+  if ($::form->{cp_id}) {
+    $email = SL::DB::Contact->load_cached($::form->{cp_id})->cp_email;
+  }
+
+  if (!$email && $::form->{vc} && $::form->{vc_id}) {
+    $email = SL::DB::Customer->load_cached($::form->{vc_id})->email if 'customer' eq $::form->{vc};
+    $email = SL::DB::Vendor  ->load_cached($::form->{vc_id})->email if 'vendor'   eq $::form->{vc};
+  }
+
+  $email = '' if $::form->{type} eq 'purchase_delivery_order';
+
   my $email_form = {
-    to                  => $contact ? $contact->cp_email : '',
+    to                  => $email,
     subject             => $::form->generate_email_subject,
+    message             => $::form->generate_email_body,
     attachment_filename => $::form->generate_attachment_filename,
   };