commit 723a11583380d2cef883c12f654d6a35bffee83b
Author: Geoffrey Richardson <information@kivitendo-premium.de>
Date:   Tue Aug 9 15:44:22 2016 +0200

    inventory_accno_id aufräumen

diff --git a/SL/IC.pm b/SL/IC.pm
index 84e913eb3..2cf7996fb 100644
--- a/SL/IC.pm
+++ b/SL/IC.pm
@@ -44,6 +44,7 @@ use SL::HTML::Restrict;
 use SL::TransNumber;
 use SL::Util qw(trim);
 use SL::DB;
+use Carp;
 
 use strict;
 
@@ -1562,7 +1563,7 @@ sub retrieve_accounts {
   my %accno_by_part = map { $_->{id} => $_ }
     selectall_hashref_query($form, $dbh, <<SQL, @part_ids);
     SELECT
-      p.id, p.inventory_accno_id AS is_part,
+      p.id, p.part_type,
       bg.inventory_accno_id,
       tc.income_accno_id AS income_accno_id,
       tc.expense_accno_id AS expense_accno_id,
@@ -1595,7 +1596,7 @@ SQL
   while (my ($index => $part_id) = each %args) {
     my $ref = $accno_by_part{$part_id} or next;
 
-    $ref->{"inventory_accno_id"} = undef unless $ref->{"is_part"};
+    $ref->{"inventory_accno_id"} = undef unless $ref->{"part_type"} eq 'part';
 
     my %accounts;
     for my $type (qw(inventory income expense)) {
diff --git a/SL/IR.pm b/SL/IR.pm
index 25820bd12..a0bf6cdf6 100644
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -837,7 +837,7 @@ sub reverse_invoice {
 
   # reverse inventory items
   my $query =
-    qq|SELECT i.parts_id, p.inventory_accno_id, p.expense_accno_id, i.qty, i.allocated, i.sellprice
+    qq|SELECT i.parts_id, p.part_type, i.qty, i.allocated, i.sellprice
        FROM invoice i, parts p
        WHERE (i.parts_id = p.id)
          AND (i.trans_id = ?)|;
@@ -848,7 +848,7 @@ sub reverse_invoice {
   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
     $netamount += $form->round_amount($ref->{sellprice} * $ref->{qty} * -1, 2);
 
-    next unless $ref->{inventory_accno_id};
+    next unless $ref->{part_type} eq 'part';
 
     # if $ref->{allocated} > 0 than we sold that many items
     next if ($ref->{allocated} <= 0);
@@ -1019,7 +1019,7 @@ sub retrieve_invoice {
         i.id AS invoice_id,
         i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.parts_id AS id, i.unit, i.deliverydate, i.project_id, i.serialnumber,
         i.price_factor_id, i.price_factor, i.marge_price_factor, i.discount, i.active_price_source, i.active_discount_source,
-        p.partnumber, p.inventory_accno_id AS part_inventory_accno_id,  pr.projectnumber, pg.partsgroup
+        p.partnumber, p.part_type, pr.projectnumber, pg.partsgroup
 
         FROM invoice i
         JOIN parts p ON (i.parts_id = p.id)
@@ -1043,8 +1043,7 @@ sub retrieve_invoice {
                                           );
     map { $ref->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
 
-    map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_inventory_accno_id"};
-    delete($ref->{"part_inventory_accno_id"});
+    map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid)) if !$ref->{"part_type"} eq 'part';
 
     foreach my $type (qw(inventory income expense)) {
       while ($ref->{"${type}_new_chart"} && ($ref->{"${type}_valid"} >=0)) {
diff --git a/SL/IS.pm b/SL/IS.pm
index f09b17871..b9e9010fb 100644
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -1656,8 +1656,7 @@ sub process_assembly {
   my ($dbh, $myconfig, $form, $position, $id, $totalqty) = @_;
 
   my $query =
-    qq|SELECT a.parts_id, a.qty, p.part_type, p.partnumber, p.description, p.unit,
-         p.inventory_accno_id, p.income_accno_id, p.expense_accno_id
+    qq|SELECT a.parts_id, a.qty, p.part_type, p.partnumber, p.description, p.unit
        FROM assembly a
        JOIN parts p ON (a.parts_id = p.id)
        WHERE (a.id = ?)|;
@@ -1797,7 +1796,7 @@ sub reverse_invoice {
 
   # reverse inventory items
   my $query =
-    qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.part_type, p.inventory_accno_id
+    qq|SELECT i.id, i.parts_id, i.qty, i.assemblyitem, p.part_type
        FROM invoice i
        JOIN parts p ON (i.parts_id = p.id)
        WHERE i.trans_id = ?|;
@@ -2003,7 +2002,7 @@ sub _retrieve_invoice {
            i.description, i.longdescription, i.qty, i.fxsellprice AS sellprice, i.discount, i.parts_id AS id, i.unit, i.deliverydate AS reqdate,
            i.project_id, i.serialnumber, i.pricegroup_id, i.ordnumber, i.donumber, i.transdate, i.cusordnumber, i.subtotal, i.lastcost,
            i.price_factor_id, i.price_factor, i.marge_price_factor, i.active_price_source, i.active_discount_source,
-           p.partnumber, p.part_type, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id, p.formel, p.listprice,
+           p.partnumber, p.part_type, p.notes AS partnotes, p.formel, p.listprice,
            pr.projectnumber, pg.partsgroup, prg.pricegroup
 
          FROM invoice i
diff --git a/SL/OE.pm b/SL/OE.pm
index d3863497b..d347d5b2f 100644
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -1094,7 +1094,7 @@ sub _retrieve {
            c3.accno AS expense_accno,   c3.new_chart_id AS expense_new_chart,   date($transdate) - c3.valid_from as expense_valid,
            oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
            p.partnumber, p.part_type, p.listprice, o.description, o.qty,
-           o.sellprice, o.parts_id AS id, o.unit, o.discount, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id,
+           o.sellprice, o.parts_id AS id, o.unit, o.discount, p.notes AS partnotes, p.part_type,
            o.reqdate, o.project_id, o.serialnumber, o.ship, o.lastcost,
            o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
            o.price_factor_id, o.price_factor, o.marge_price_factor, o.active_price_source, o.active_discount_source,
@@ -1126,10 +1126,10 @@ sub _retrieve {
       map { $ref->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
 
       # Handle accounts.
-      if (!$ref->{"part_inventory_accno_id"}) {
+      if (!$ref->{"part_type"} eq 'part') {
         map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
       }
-      delete($ref->{"part_inventory_accno_id"});
+      # delete($ref->{"part_inventory_accno_id"});
 
       # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
       #   unless already present there
