Kosmetik: Alignment, Einrückung
[kivitendo-erp.git] / SL / OE.pm
index f18cb06..694732e 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -40,7 +40,9 @@ use YAML;
 use SL::AM;
 use SL::Common;
 use SL::CVar;
+use SL::DB::Order;
 use SL::DB::PeriodicInvoicesConfig;
+use SL::DB::Status;
 use SL::DBUtils;
 use SL::IC;
 
@@ -660,59 +662,16 @@ sub delete {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database
-  my $dbh = $form->get_standard_dbh;
-  $dbh->begin_work;
-
-  # delete spool files
-  my $query = qq|SELECT s.spoolfile FROM status s | .
-              qq|WHERE s.trans_id = ?|;
-  my @values = (conv_i($form->{id}));
-  my $sth = $dbh->prepare($query);
-  $sth->execute(@values) || $self->dberror($query);
-
-  my $spoolfile;
-  my @spoolfiles = ();
-
-  while (($spoolfile) = $sth->fetchrow_array) {
-    push @spoolfiles, $spoolfile;
-  }
-  $sth->finish;
-
-  # delete-values
-  @values = (conv_i($form->{id}));
+  my $rc = SL::DB::Order->new->db->with_transaction(sub {
+    my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $form->{id} ]) };
 
-  # periodic invoices and their configuration
-  do_query($form, $dbh, qq|DELETE FROM periodic_invoices         WHERE config_id IN (SELECT id FROM periodic_invoices_configs WHERE oe_id = ?)|, @values);
-  do_query($form, $dbh, qq|DELETE FROM periodic_invoices_configs WHERE oe_id = ?|, @values);
-
-  # delete status entries
-  $query = qq|DELETE FROM status | .
-           qq|WHERE trans_id = ?|;
-  do_query($form, $dbh, $query, @values);
+    SL::DB::Order->new(id => $form->{id})->delete;
 
-  # delete individual entries
-  $query = qq|DELETE FROM orderitems | .
-           qq|WHERE trans_id = ?|;
-  do_query($form, $dbh, $query, @values);
-
-  $query = qq|DELETE FROM shipto | .
-           qq|WHERE trans_id = ? AND module = 'OE'|;
-  do_query($form, $dbh, $query, @values);
-
-  # delete OE record
-  $query = qq|DELETE FROM oe | .
-           qq|WHERE id = ?|;
-  do_query($form, $dbh, $query, @values);
-
-  my $rc = $dbh->commit;
-
-  if ($rc) {
     my $spool = $::lx_office_conf{paths}->{spool};
-    foreach $spoolfile (@spoolfiles) {
-      unlink "$spool/$spoolfile" if $spoolfile;
-    }
-  }
+    unlink map { "$spool/$_" } @spoolfiles if $spool;
+
+    1;
+  });
 
   $main::lxdebug->leave_sub();
 
@@ -853,9 +812,6 @@ sub retrieve {
       map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
     }    # if !@ids
 
-    my %oid = ('Pg'     => 'oid',
-               'Oracle' => 'rowid');
-
     my $transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
 
     $form->{taxzone_id} = 0 unless ($form->{taxzone_id});
@@ -887,7 +843,7 @@ sub retrieve {
       ($form->{id}
        ? qq|WHERE o.trans_id = ?|
        : qq|WHERE o.trans_id IN (| . join(", ", map("?", @ids)) . qq|)|) .
-      qq|ORDER BY o.$oid{$myconfig->{dbdriver}}|;
+      qq|ORDER BY o.oid|;
 
     @ids = $form->{id} ? ($form->{id}) : @ids;
     $sth = prepare_execute_query($form, $dbh, $query, @values);
@@ -1055,10 +1011,6 @@ sub order_details {
   my $tax_rate;
   my $taxamount;
 
-
-  my %oid = ('Pg'     => 'oid',
-             'Oracle' => 'rowid');
-
   my (@project_ids, %projectnumbers, %projectdescriptions);
 
   push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
@@ -1116,7 +1068,7 @@ sub order_details {
        partnotes serialnumber reqdate sellprice listprice netprice
        discount p_discount discount_sub nodiscount_sub
        linetotal  nodiscount_linetotal tax_rate projectnumber projectdescription
-       price_factor price_factor_name partsgroup);
+       price_factor price_factor_name partsgroup weight lineweight);
 
   push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
 
@@ -1124,6 +1076,7 @@ sub order_details {
 
   map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays, @tax_arrays);
 
+  my $totalweight = 0;
   my $sameitem = "";
   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
     $i = $item->[0];
@@ -1238,6 +1191,13 @@ sub order_details {
       push(@{ $form->{TEMPLATE_ARRAYS}->{projectnumber} },              $projectnumbers{$form->{"project_id_$i"}});
       push(@{ $form->{TEMPLATE_ARRAYS}->{projectdescription} },         $projectdescriptions{$form->{"project_id_$i"}});
 
+      my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
+      $totalweight += $lineweight;
+      push @{ $form->{TEMPLATE_ARRAYS}->{weight} },            $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
+      push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} },      $form->{"weight_$i"};
+      push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} },        $form->format_amount($myconfig, $lineweight, 3);
+      push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} },  $lineweight;
+
       my ($taxamount, $taxbase);
       my $taxrate = 0;
 
@@ -1269,9 +1229,9 @@ sub order_details {
         # get parts and push them onto the stack
         my $sortorder = "";
         if ($form->{groupitems}) {
-          $sortorder = qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
+          $sortorder = qq|ORDER BY pg.partsgroup, a.oid|;
         } else {
-          $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
+          $sortorder = qq|ORDER BY a.oid|;
         }
 
         $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty, | .
@@ -1304,6 +1264,11 @@ sub order_details {
     }
   }
 
+  $form->{totalweight}       = $form->format_amount($myconfig, $totalweight, 3);
+  $form->{totalweight_nofmt} = $totalweight;
+  my $defaults = AM->get_defaults();
+  $form->{weightunit}        = $defaults->{weightunit};
+
   my $tax = 0;
   foreach $item (sort keys %taxaccounts) {
     $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);