generic translations strict
[kivitendo-erp.git] / bin / mozilla / do.pl
index ade606b..7f83bf3 100644 (file)
@@ -121,7 +121,8 @@ sub edit {
   }
 
   if ($form->{print_and_save}) {
-    $form->{action}   = "print";
+    $form->{action}   = "dispatcher";
+    $form->{action_print} = "1";
     $form->{resubmit} = 1;
     $language_id      = $form->{language_id};
     $printer_id       = $form->{printer_id};
@@ -156,16 +157,22 @@ sub order_links {
 
   my $editing = $form->{id};
 
-  DO->retrieve();
+  DO->retrieve('vc'  => $form->{vc},
+               'ids' => $form->{id});
 
-  $payment_id  = $form->{payment_id}  if ($form->{payment_id});
-  $language_id = $form->{language_id} if ($form->{language_id});
-  $taxzone_id  = $form->{taxzone_id}  if ($form->{taxzone_id});
-  $salesman_id = $form->{salesman_id} if ($editing);
+  $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
+  $form->{shipto} = 1 if $form->{id};
 
+  # get customer / vendor
+  if ($form->{vc} eq 'vendor') {
+    IR->get_vendor(\%myconfig, \%$form);
+  } else {
+    IS->get_customer(\%myconfig, \%$form);
+  }
 
-  $taxincluded    = $form->{taxincluded};
-  $form->{shipto} = 1 if $form->{id};
+  $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
+  $form->restore_vars(qw(taxincluded)) if $form->{id};
+  $form->restore_vars(qw(salesman_id)) if $editing;
 
   if ($form->{"all_$form->{vc}"}) {
     unless ($form->{"$form->{vc}_id"}) {
@@ -173,25 +180,11 @@ sub order_links {
     }
   }
 
-  $cp_id    = $form->{cp_id};
-  $intnotes = $form->{intnotes};
-
-  $form->{cp_id} = $cp_id;
-
-  $form->{payment_id}  = $payment_id  if ($payment_id);
-  $form->{language_id} = $language_id if ($language_id);
-  $form->{taxzone_id}  = $taxzone_id  if ($taxzone_id);
-  $form->{intnotes}    = $intnotes    if ($intnotes);
-
   ($form->{ $form->{vc} })  = split /--/, $form->{ $form->{vc} };
   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
 
-  $form->{taxincluded} = $taxincluded if ($form->{id});
-
   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
-  $form->{salesman_id} = $salesman_id if ($editing);
-
   $lxdebug->leave_sub();
 }
 
@@ -226,8 +219,6 @@ sub prepare_order {
     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
     $dec_qty = length $dec_qty;
     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
-
-    map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
   }
 
   $lxdebug->leave_sub();
@@ -280,10 +271,15 @@ sub form_header {
     if ($form->{format} eq "html") {
       $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
     }
+    # emulate click for resubmitting actions
+    $form->{onload} .= "document.do.${_}.click(); " for grep { /^action_/ } keys %$form;
     $form->{onload} .= "document.do.submit();"
   }
 
   $form->header();
+  # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
+  $form->{department} .= '--' . $form->{department_id};
+
   print $form->parse_html_template('do/form_header');
 
   $lxdebug->leave_sub();
@@ -345,7 +341,6 @@ sub update_delivery_order {
 
       } else {
 
-        map { $form->{item_list}[$i]{$_} =~ s/\"/"/g }    qw(partnumber description unit);
         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
@@ -382,7 +377,7 @@ sub search {
 
   check_do_access();
 
-  $form->{vc} = $form->{type} eq 'purchase_order' ? 'vendor' : 'customer';
+  $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
 
   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS",
                                        "all" => 1 },
@@ -408,14 +403,14 @@ sub orders {
 
   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
 
-  $form->{sort} ||= 'transdate';
+  report_generator_set_default_sort('transdate', 1);
 
   DO->transactions();
 
   $form->{rowcount} = scalar @{ $form->{DO} };
 
   my @columns = qw(
-    transdate
+    ids                     transdate
     id                      donumber
     ordnumber
     name                    employee
@@ -454,20 +449,22 @@ sub orders {
     'delivered'               => { 'text' => $locale->text('Delivered'), },
   );
 
-  foreach my $name (qw(id transdate donumber ordnumber name employee shipvia)) {
-    $column_defs{$name}->{link} = $href . "&sort=$name";
+  foreach my $name (qw(id transdate donumber ordnumber name employee shipvia transaction_description)) {
+    my $sortdir                 = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
+    $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
   }
 
   $form->{"l_type"} = "Y";
   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
-  $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
+
+  $column_defs{ids}->{visible} = 'HTML';
 
   $report->set_columns(%column_defs);
   $report->set_column_order(@columns);
 
-  $report->set_export_options('orders', @hidden_variables);
+  $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
 
-  $report->set_sort_indicator($form->{sort}, 1);
+  $report->set_sort_indicator($form->{sort}, $form->{sortdir});
 
   my @options;
   if ($form->{customer}) {
@@ -490,10 +487,10 @@ sub orders {
     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
   }
   if ($form->{transdatefrom}) {
-    push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
+    push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
   }
   if ($form->{transdateto}) {
-    push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
+    push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
   }
   if ($form->{open}) {
     push @options, $locale->text('Open');
@@ -508,10 +505,12 @@ sub orders {
     push @options, $locale->text('Not delivered');
   }
 
-  $report->set_options('top_info_text'       => join("\n", @options),
-                       'output_format'       => 'HTML',
-                       'title'               => $form->{title},
-                       'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
+  $report->set_options('top_info_text'        => join("\n", @options),
+                       'raw_top_info_text'    => $form->parse_html_template('do/orders_top'),
+                       'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom'),
+                       'output_format'        => 'HTML',
+                       'title'                => $form->{title},
+                       'attachment_basename'  => $attachment_basename . strftime('_%Y%m%d', localtime time),
     );
   $report->set_options_from_form();
 
@@ -524,16 +523,27 @@ sub orders {
   my $edit_url       = build_std_url('action=edit', 'type', 'vc');
   my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
 
+  my $idx            = 1;
+
   foreach $dord (@{ $form->{DO} }) {
     $dord->{open}      = $dord->{closed}    ? $locale->text('No')  : $locale->text('Yes');
     $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
 
     my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
 
+    $row->{ids}  = {
+      'raw_data' =>   $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $dord->{id})
+                    . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
+      'valign'   => 'center',
+      'align'    => 'center',
+    };
+
     $row->{donumber}->{link}  = $edit_url       . "&id=" . E($dord->{id})      . "&callback=${callback}";
     $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id})   . "&callback=${callback}";
 
     $report->add_data($row);
+
+    $idx++;
   }
 
   $report->generate_with_headers();
@@ -631,22 +641,23 @@ sub invoice {
   $lxdebug->enter_sub();
 
   check_do_access();
-  $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
+  $auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
 
-  $form->{deliverydate} = $form->{transdate};
-  $form->{transdate}    = $form->{invdate} = $form->current_date(\%myconfig);
-  $form->{duedate}      = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
+  $form->{convert_from_do_ids} = $form->{id};
+  $form->{deliverydate}        = $form->{transdate};
+  $form->{transdate}           = $form->{invdate} = $form->current_date(\%myconfig);
+  $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
+  $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
 
-  $form->{id}     = '';
-  $form->{closed} = 0;
   $form->{rowcount}--;
 
-  $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
+  delete @{$form}{qw(id closed delivered)};
 
   if ($form->{type} eq 'purchase_delivery_order') {
     $form->{title}  = $locale->text('Add Vendor Invoice');
     $form->{script} = 'ir.pl';
     $script         = "ir";
+    $buysell        = 'sell';
 
   } else {
     $form->{title}  = $locale->text('Add Sales Invoice');
@@ -671,8 +682,8 @@ sub invoice {
 
   $form->{currency}     = $currency;
   $form->{exchangerate} = "";
-  $form->{forex}        = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell));
-  $form->{exchangerate} = $exchangerate if ($form->{forex});
+  $form->{forex}        = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
+  $form->{exchangerate} = $form->{forex} if ($form->{forex});
 
   prepare_invoice();
 
@@ -704,6 +715,80 @@ sub invoice {
   $lxdebug->leave_sub();
 }
 
+sub invoice_multi {
+  $lxdebug->enter_sub();
+
+  check_do_access();
+  $auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
+
+  my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
+
+  if (!scalar @do_ids) {
+    $form->show_generic_error($locale->text('You have not selected any delivery order.'), 'back_button' => 1);
+  }
+
+  map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
+
+  if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
+    $form->show_generic_error($form->{vc} eq 'customer' ?
+                              $locale->text('You cannot create an invoice for delivery orders for different customers.') :
+                              $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
+                              'back_button' => 1);
+  }
+
+  my $source_type              = $form->{type};
+  $form->{convert_from_do_ids} = join ' ', @do_ids;
+  # bei der auswahl von mehreren Lieferscheinen fuer eine Rechnung, die einfach in donumber_array
+  # zwischenspeichern (DO.pm) und als ' '-separierte Liste wieder zurueckschreiben
+  # Hinweis: delete gibt den wert zurueck und loescht danach das element (nett und einfach)
+  # $shell: perldoc perlunc; /delete EXPR
+  $form->{donumber}           = delete $form->{donumber_array};
+  $form->{deliverydate}        = $form->{transdate};
+  $form->{transdate}           = $form->current_date(\%myconfig);
+  $form->{duedate}             = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
+  $form->{type}                = "invoice";
+  $form->{closed}              = 0;
+  $form->{defaultcurrency}     = $form->get_default_currency(\%myconfig);
+
+  my $buysell;
+  if ($source_type eq 'purchase_delivery_order') {
+    $form->{title}  = $locale->text('Add Vendor Invoice');
+    $form->{script} = 'ir.pl';
+    $script         = "ir";
+    $buysell        = 'sell';
+
+  } else {
+    $form->{title}  = $locale->text('Add Sales Invoice');
+    $form->{script} = 'is.pl';
+    $script         = "is";
+    $buysell        = 'buy';
+  }
+
+  map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
+
+  $form->{rowcount} = 0;
+  foreach my $ref (@{ $form->{form_details} }) {
+    $form->{rowcount}++;
+    $ref->{reqdate} ||= $ref->{dord_transdate}; # copy transdates into each invoice row
+    map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
+    map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice discount lastcost);
+               $form->{"discount_$form->{rowcount}"}   = $form->{"discount_$form->{rowcount}"}  * 100; #s.a. Bug 1151
+               # Anm.: Eine Änderung des discounts in der SL/DO.pm->retrieve (select (doi.discount * 100) as discount) ergibt in psql einen
+               # Wert von 10.0000001490116. Ferner ist der Rabatt in der Rechnung dann bei 1.0 (?). Deswegen lasse ich das hier. jb 10.10.09
+  }
+  delete $form->{form_details};
+
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+
+  invoice_links();
+  prepare_invoice();
+  display_form();
+
+  $lxdebug->leave_sub();
+}
+
 sub save_as_new {
   $lxdebug->enter_sub();
 
@@ -711,6 +796,7 @@ sub save_as_new {
 
   $form->{saveasnew} = 1;
   $form->{closed}    = 0;
+  $form->{delivered} = 0;
   map { delete $form->{$_} } qw(printed emailed queued);
 
   # Let Lx-Office assign a new order number if the user hasn't changed the
@@ -756,6 +842,8 @@ sub calculate_stock_in_out {
     return '';
   }
 
+  AM->retrieve_all_units();
+
   my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
   my $sinfo    = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
 
@@ -763,7 +851,7 @@ sub calculate_stock_in_out {
 
   my $content  = $form->format_amount_units('amount'      => $sum * 1,
                                             'part_unit'   => $form->{"partunit_$i"},
-                                            'amount_unit' => $units->{$form->{"partunit_$i"}}->{base_unit},
+                                            'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
                                             'conv_units'  => 'convertible_not_smaller',
                                             'max_places'  => 2);
   $content    .= qq| <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
@@ -825,9 +913,9 @@ sub update_stock_in {
   my $stock_info = [];
 
   foreach my $i (1..$form->{rowcount}) {
+    $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
     push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
   }
-  $main::lxdebug->dump(0, "si", $stock_info);
 
   display_stock_in_form($stock_info);
 
@@ -853,16 +941,13 @@ sub display_stock_in_form {
 
   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
 
-  my $units      = AM->retrieve_units(\%myconfig, $form, "dimension");
+  my $units      = AM->retrieve_units(\%myconfig, $form);
   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
 
-  my $access     = $auth->check_right($form->{login}, 'all_warehouses') ? undef : $form->{login};
-
   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
-                                     'bins'   => 'BINS',
-                                     'access' => $access, });
+                                     'bins'   => 'BINS' });
 
-  redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{closed});
+  redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
 
   get_basic_bin_wh_info($stock_info);
 
@@ -902,14 +987,14 @@ sub stock_out_form {
 
   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
 
-  my $units      = AM->retrieve_units(\%myconfig, $form, "dimension");
+  my $units      = AM->retrieve_units(\%myconfig, $form);
   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
 
   my @contents   = DO->get_item_availability('parts_id' => $form->{parts_id});
 
   my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
 
-  if (!$form->{closed}) {
+  if (!$form->{delivered}) {
     foreach my $row (@contents) {
       $row->{available_qty} = $form->format_amount_units('amount'      => $row->{qty} * 1,
                                                          'part_unit'   => $part_info->{unit},
@@ -935,7 +1020,7 @@ sub stock_out_form {
 
   $form->header();
   print $form->parse_html_template('do/stock_out_form', { 'UNITS'      => $units_data,
-                                                          'WHCONTENTS' => $form->{closed} ? $stock_info : \@contents,
+                                                          'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
                                                           'PART_INFO'  => $part_info, });
 
   $lxdebug->leave_sub();
@@ -979,14 +1064,18 @@ sub set_stock_out {
   $lxdebug->leave_sub();
 }
 
-sub transfer_in_and_close {
+sub transfer_in {
   $lxdebug->enter_sub();
 
+  if (DO->is_marked_as_delivered('id' => $form->{id})) {
+    $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'), 'back_button' => 1);
+  }
+
   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
   my @all_requests;
 
   if (@part_ids) {
-    my $units         = AM->retrieve_units(\%myconfig, $form, "dimension");
+    my $units         = AM->retrieve_units(\%myconfig, $form);
     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
     my %request_map;
 
@@ -1016,7 +1105,7 @@ sub transfer_in_and_close {
     }
 
     if (@{ $form->{ERRORS} }) {
-      push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been closed. The warehouse contents have not changed.');
+      push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
 
       update();
       $lxdebug->leave_sub();
@@ -1028,7 +1117,6 @@ sub transfer_in_and_close {
   DO->transfer_in_out('direction' => 'in',
                       'requests'  => \@all_requests);
 
-  $form->{closed}    = 1;
   $form->{delivered} = 1;
 
   save();
@@ -1036,14 +1124,18 @@ sub transfer_in_and_close {
   $lxdebug->leave_sub();
 }
 
-sub transfer_out_and_close {
+sub transfer_out {
   $lxdebug->enter_sub();
 
+  if (DO->is_marked_as_delivered('id' => $form->{id})) {
+    $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'), 'back_button' => 1);
+  }
+
   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
   my @all_requests;
 
   if (@part_ids) {
-    my $units         = AM->retrieve_units(\%myconfig, $form, "dimension");
+    my $units         = AM->retrieve_units(\%myconfig, $form);
     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
     my %request_map;
 
@@ -1109,7 +1201,7 @@ sub transfer_out_and_close {
     }
 
     if (@{ $form->{ERRORS} }) {
-      push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been closed. The warehouse contents have not changed.');
+      push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
 
       update();
       $lxdebug->leave_sub();
@@ -1121,7 +1213,6 @@ sub transfer_out_and_close {
   DO->transfer_in_out('direction' => 'out',
                       'requests'  => \@all_requests);
 
-  $form->{closed}    = 1;
   $form->{delivered} = 1;
 
   save();
@@ -1129,6 +1220,19 @@ sub transfer_out_and_close {
   $lxdebug->leave_sub();
 }
 
+sub mark_closed {
+  $lxdebug->enter_sub();
+
+  DO->close_orders('ids' => [ $form->{id} ]);
+
+  $form->{closed} = 1;
+
+  update();
+
+  $lxdebug->leave_sub();
+}
+
+
 sub yes {
   call_sub($form->{yes_nextsub});
 }
@@ -1140,3 +1244,14 @@ sub no {
 sub update {
   call_sub($form->{update_nextsub} || $form->{nextsub} || 'update_delivery_order');
 }
+
+sub dispatcher {
+  foreach my $action (qw(update ship_to print e_mail save transfer_out transfer_in mark_closed save_as_new invoice delete)) {
+    if ($form->{"action_${action}"}) {
+      call_sub($action);
+      return;
+    }
+  }
+
+  $form->error($locale->text('No action defined.'));
+}