]> wagnertech.de Git - mfinanz.git/blobdiff - SL/IR.pm
Artikel-Klassifizierung
[mfinanz.git] / SL / IR.pm
index f1a1b7c171c02e32f2ef36a09d2f4964ff4dcd89..f25aa921e38c721b0cd1646e00b1cb660ea0e223 100644 (file)
--- a/SL/IR.pm
+++ b/SL/IR.pm
@@ -25,7 +25,8 @@
 # GNU General Public License for more details.
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1335, USA.
 #======================================================================
 #
 # Inventory received module
@@ -51,6 +52,9 @@ use SL::DB;
 use List::Util qw(min);
 
 use strict;
+use constant PCLASS_OK             =>   0;
+use constant PCLASS_NOTFORSALE     =>   1;
+use constant PCLASS_NOTFORPURCHASE =>   2;
 
 sub post_invoice {
   my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
@@ -837,7 +841,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 +852,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);
@@ -933,7 +937,8 @@ sub delete_invoice {
     );
 
     map { do_query($form, $dbh, $_, @values) } @queries;
-  });
+    1;
+  }) or do { die SL::DB->client->error };
 
   return 1;
 }
@@ -944,7 +949,7 @@ sub retrieve_invoice {
   my ($self, $myconfig, $form) = @_;
 
   # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my ($query, $sth, $ref, $q_invdate);
 
@@ -976,7 +981,6 @@ sub retrieve_invoice {
   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
   if (!$form->{id}) {
-    $dbh->disconnect();
     $main::lxdebug->leave_sub();
 
     return;
@@ -1019,7 +1023,8 @@ 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
+        ,p.classification_id
 
         FROM invoice i
         JOIN parts p ON (i.parts_id = p.id)
@@ -1043,8 +1048,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)) {
@@ -1094,8 +1098,6 @@ sub retrieve_invoice {
 
   Common::webdav_folder($form);
 
-  $dbh->disconnect();
-
   $main::lxdebug->leave_sub();
 }
 
@@ -1107,7 +1109,7 @@ sub get_vendor {
   $params = $form unless defined $params && ref $params eq "HASH";
 
   # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $dateformat = $myconfig->{dateformat};
   $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
@@ -1207,8 +1209,6 @@ sub get_vendor {
     $params->{rowcount} = $i if ($i && !$params->{type});
   }
 
-  $dbh->disconnect();
-
   $main::lxdebug->leave_sub();
 }
 
@@ -1217,13 +1217,12 @@ sub retrieve_item {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $i = $form->{rowcount};
 
   # don't include assemblies or obsolete parts
-  my $where = "NOT p.assembly = '1' AND NOT p.obsolete = '1'";
+  my $where = "NOT p.part_type = 'assembly' AND NOT p.obsolete = '1'";
   my @values;
 
   foreach my $table_column (qw(p.partnumber p.description pg.partsgroup)) {
@@ -1281,10 +1280,11 @@ sub retrieve_item {
   my $query =
     qq|SELECT
          p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice,
-         p.unit, p.assembly, p.onhand, p.formel,
+         p.unit, p.part_type, p.onhand, p.formel,
          p.notes AS partnotes, p.notes AS longdescription, p.not_discountable,
-         p.inventory_accno_id, p.price_factor_id,
+         p.price_factor_id,
          p.ean,
+         p.classification_id,
 
          pfac.factor AS price_factor,
 
@@ -1300,6 +1300,7 @@ sub retrieve_item {
          c3.new_chart_id                  AS expense_new_chart,
          date($transdate) - c3.valid_from AS expense_valid,
 
+         pt.used_for_purchase AS used_for_purchase,
          pg.partsgroup
 
        FROM parts p
@@ -1316,6 +1317,7 @@ sub retrieve_item {
            FROM taxzone_charts tc
            WHERE tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c3.id)
        LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
+       LEFT JOIN part_classifications pt ON (pt.id = p.classification_id)
        LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id)
        WHERE $where|;
   my $sth = prepare_execute_query($form, $dbh, $query, @values);
@@ -1334,6 +1336,7 @@ sub retrieve_item {
   map { push @{ $_ }, prepare_query($form, $dbh, $_->[0]) } @translation_queries;
 
   $form->{item_list} = [];
+  my $has_wrong_pclass = PCLASS_OK;
   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
     if ($mm_by_id{$ref->{id}}) {
@@ -1344,7 +1347,13 @@ sub retrieve_item {
     if (($::form->{"partnumber_$i"} ne '') && ($ref->{ean} eq $::form->{"partnumber_$i"})) {
       push @{ $ref->{matches} ||= [] }, $::locale->text('EAN') . ': ' . $ref->{ean};
     }
+    $ref->{type_and_classific} = $::request->presenter->type_abbreviation($ref->{part_type}).
+                                 $::request->presenter->classification_abbreviation($ref->{classification_id});
 
+    if (! $ref->{used_for_purchase} ) {
+       $has_wrong_pclass = PCLASS_NOTFORPURCHASE;
+       next;
+    }
     # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn
     # es sich um eine Dienstleistung handelt. Bei Dienstleistungen muss das
     # Buchungskonto also aus dem Ergebnis rausgenommen werden.
@@ -1414,17 +1423,18 @@ sub retrieve_item {
   $sth->finish();
   $_->[1]->finish for @translation_queries;
 
+  $form->{is_wrong_pclass} = $has_wrong_pclass;
+  $form->{NOTFORSALE}      = PCLASS_NOTFORSALE;
+  $form->{NOTFORPURCHASE}  = PCLASS_NOTFORPURCHASE;
   foreach my $item (@{ $form->{item_list} }) {
     my $custom_variables = CVar->get_custom_variables(module   => 'IC',
                                                       trans_id => $item->{id},
                                                       dbh      => $dbh,
                                                      );
-
+    $form->{is_wrong_pclass} = PCLASS_OK; # one correct type
     map { $item->{"ic_cvar_" . $_->{name} } = $_->{value} } @{ $custom_variables };
   }
 
-  $dbh->disconnect();
-
   $main::lxdebug->leave_sub();
 }
 
@@ -1433,8 +1443,7 @@ sub vendor_details {
 
   my ($self, $myconfig, $form, @wanted_vars) = @_;
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my @values;
 
@@ -1480,8 +1489,6 @@ sub vendor_details {
                                                   'translation_type' => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'),
                                                   'allow_fallback'   => 1);
 
-  $dbh->disconnect();
-
   $main::lxdebug->leave_sub();
 }
 
@@ -1490,8 +1497,7 @@ sub item_links {
 
   my ($self, $myconfig, $form) = @_;
 
-  # connect to database
-  my $dbh = $form->dbconnect($myconfig);
+  my $dbh = SL::DB->client->dbh;
 
   my $query =
     qq|SELECT accno, description, link
@@ -1511,8 +1517,6 @@ sub item_links {
   }
 
   $sth->finish();
-  $dbh->disconnect();
-
   $main::lxdebug->leave_sub();
 }