Bankerweiterung - Zwischenstand, erster Entwurf
[kivitendo-erp.git] / SL / OE.pm
index df43e2a..8f7c050 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -176,7 +176,7 @@ SQL
 
   if (!$main::auth->assert('sales_all_edit', 1)) {
     $query .= " AND o.employee_id = (select id from employee where login= ?)";
-    push @values, $form->{login};
+    push @values, $::myconfig{login};
   }
   if ($form->{employee_id}) {
     $query .= " AND o.employee_id = ?";
@@ -325,7 +325,7 @@ sub transactions_for_todo_list {
   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
 
   my $query    = qq|SELECT id FROM employee WHERE login = ?|;
-  my ($e_id)   = selectrow_query($form, $dbh, $query, $form->{login});
+  my ($e_id)   = selectrow_query($form, $dbh, $query, $::myconfig{login});
 
   $query       =
     qq|SELECT oe.id, oe.transdate, oe.reqdate, oe.quonumber, oe.transaction_description, oe.amount,
@@ -511,7 +511,7 @@ sub save {
 
       CVar->get_non_editable_ic_cvars(form               => $form,
                                       dbh                => $dbh,
-                                      row                => $i, 
+                                      row                => $i,
                                       sub_module         => 'orderitems',
                                       may_converted_from => ['orderitems', 'invoice']);
 
@@ -568,9 +568,10 @@ SQL
                                   name_prefix  => 'ic_',
                                   name_postfix => "_$i",
                                   dbh          => $dbh);
+
       # link previous items with orderitems
       foreach (qw(orderitems invoice)) {
-        if ($form->{"converted_from_${_}_id_$i"}) {
+        if (!$form->{saveasnew} && !$form->{useasnew} && $form->{"converted_from_${_}_id_$i"}) {
           RecordLinks->create_links('dbh'        => $dbh,
                                     'mode'       => 'ids',
                                     'from_table' => $_,
@@ -578,11 +579,12 @@ SQL
                                     'to_table'   => 'orderitems',
                                     'to_id'      => $orderitems_id,
           );
-          delete $form->{"converted_from_${_}_id_$i"};
         }
+        delete $form->{"converted_from_${_}_id_$i"};
       }
     }
   }
+
   # search for orphaned ids
   $query  = sprintf 'SELECT id FROM orderitems WHERE trans_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_orderitems;
   @values = (conv_i($form->{id}), map { conv_i($_) } @processed_orderitems);
@@ -632,7 +634,7 @@ SQL
              conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
              $amount, $netamount, conv_date($reqdate),
              $form->{taxincluded} ? 't' : 'f', $form->{shippingpoint},
-             $form->{shipvia}, $form->{notes}, $form->{intnotes},
+             $form->{shipvia}, $restricter->process($form->{notes}), $form->{intnotes},
              $form->{currency}, $form->{closed} ? 't' : 'f',
              $form->{delivered} ? "t" : "f", $form->{proforma} ? 't' : 'f',
              $quotation, conv_i($form->{department_id}),
@@ -672,7 +674,7 @@ SQL
   delete $form->{convert_from_oe_ids};
   @{ $form->{convert_from_oe_ids} }      =  @convert_from_oe_ids;
   foreach (qw(ar oe)) {
-    if ($form->{"convert_from_${_}_ids"}) {
+    if (!$form->{useasnew} && $form->{"convert_from_${_}_ids"}) {
       RecordLinks->create_links('dbh'        => $dbh,
                                 'mode'       => 'ids',
                                 'from_table' => $_,
@@ -736,7 +738,7 @@ sub load_periodic_invoice_config {
     my $config_obj = SL::DB::Manager::PeriodicInvoicesConfig->find_by(oe_id => $form->{id});
 
     if ($config_obj) {
-      my $config = { map { $_ => $config_obj->$_ } qw(active terminated periodicity start_date_as_date end_date_as_date first_billing_date_as_date extend_automatically_by ar_chart_id
+      my $config = { map { $_ => $config_obj->$_ } qw(active terminated periodicity order_value_periodicity start_date_as_date end_date_as_date first_billing_date_as_date extend_automatically_by ar_chart_id
                                                       print printer_id copies) };
       $form->{periodic_invoices_config} = YAML::Dump($config);
     }
@@ -834,10 +836,12 @@ sub retrieve {
   if ($form->{"rowcount"} and $#ids == 0) {
     $form->{"id"} = $ids[0];
     undef @ids;
+    delete $form->{convert_from_oe_ids};
   }
 
   # and remember for the rest of the function
   my $is_collective_order = scalar @ids;
+  $form->{useasnew} = !!$is_collective_order;
 
   if (!$form->{id}) {
     my $extra_days   = $form->{type} eq 'sales_quotation' ? $::instance_conf->get_reqdate_interval : 1;
@@ -1042,7 +1046,7 @@ sub retrieve {
       }
 
       # delete orderitems_id in collective orders, so that they get cloned no matter what
-      delete $ref->{orderitems_id} if $is_collective_order;
+      $ref->{converted_from_orderitems_id} = delete $ref->{orderitems_id} if $is_collective_order;
 
       # get tax rates and description
       my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
@@ -1404,7 +1408,7 @@ sub order_details {
 
       CVar->get_non_editable_ic_cvars(form               => $form,
                                       dbh                => $dbh,
-                                      row                => $i, 
+                                      row                => $i,
                                       sub_module         => 'orderitems',
                                       may_converted_from => ['orderitems', 'invoice']);