Komfort- und Hilfsfunktionen fuer den Datenbankzugriff.
[kivitendo-erp.git] / SL / IC.pm
index 48c7996..8f2cfb3 100644 (file)
--- a/SL/IC.pm
+++ b/SL/IC.pm
@@ -33,6 +33,7 @@
 #======================================================================
 
 package IC;
 #======================================================================
 
 package IC;
+use Data::Dumper;
 
 sub get_part {
   $main::lxdebug->enter_sub();
 
 sub get_part {
   $main::lxdebug->enter_sub();
@@ -101,6 +102,76 @@ sub get_part {
   $form->{amount}{IC_expense} = $form->{expense_accno};
   $form->{amount}{IC_cogs}    = $form->{expense_accno};
 
   $form->{amount}{IC_expense} = $form->{expense_accno};
   $form->{amount}{IC_cogs}    = $form->{expense_accno};
 
+  # get prices
+  $query =
+    qq|SELECT p.parts_id, p.pricegroup_id, p.price, (SELECT pg.pricegroup FROM pricegroup pg WHERE pg.id=p.pricegroup_id) AS pricegroup FROM prices p
+              WHERE parts_id = $form->{id}
+              ORDER by pricegroup|;
+
+  $sth = $dbh->prepare($query);
+  $sth->execute || $form->dberror($query);
+
+  @pricegroups          = ();
+  @pricegroups_not_used = ();
+
+  #for pricegroups
+  my $i = 1;
+  while (
+         ($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
+          $form->{"price_$i"}, $form->{"pricegroup_$i"})
+         = $sth->fetchrow_array
+    ) {
+    $form->{"price_$i"} = $form->round_amount($form->{"price_$i"}, 5);
+    $form->{"price_$i"} =
+      $form->format_amount($myconfig, $form->{"price_$i"}, 5);
+    push @pricegroups, $form->{"pricegroup_id_$i"};
+    $i++;
+  }
+
+  $sth->finish;
+
+  # get pricegroups
+  $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
+
+  $pkq = $dbh->prepare($query);
+  $pkq->execute || $form->dberror($query);
+  while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
+    push @{ $form->{PRICEGROUPS} }, $pkr;
+  }
+  $pkq->finish;
+
+  #find not used pricegroups
+  while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
+    my $insert = 0;
+    foreach $item (@pricegroups) {
+      if ($item eq $tmp->{id}) {
+
+        #drop
+        $insert = 1;
+      }
+    }
+    if ($insert == 0) {
+      push @pricegroups_not_used, $tmp;
+    }
+  }
+
+  # if not used pricegroups are avaible
+  if (@pricegroups_not_used) {
+
+    foreach $name (@pricegroups_not_used) {
+      $form->{"klass_$i"} = "$name->{id}";
+      $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
+      $form->{"price_$i"} =
+        $form->format_amount($myconfig, $form->{"price_$i"}, 5);
+      $form->{"pricegroup_id_$i"} = "$name->{id}";
+      $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
+      $i++;
+    }
+  }
+
+  #correct rows
+  $form->{price_rows} = $i - 1;
+
   unless ($form->{item} eq 'service') {
 
     # get makes
   unless ($form->{item} eq 'service') {
 
     # get makes
@@ -161,6 +232,51 @@ sub get_part {
   $main::lxdebug->leave_sub();
 }
 
   $main::lxdebug->leave_sub();
 }
 
+sub get_pricegroups {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $myconfig, $form) = @_;
+  my $dbh                  = $form->dbconnect($myconfig);
+  my $i                    = 1;
+  my @pricegroups_not_used = ();
+
+  # get pricegroups
+  my $query = qq|SELECT p.id, p.pricegroup FROM pricegroup p|;
+
+  my $pkq = $dbh->prepare($query);
+  $pkq->execute || $form->dberror($query);
+  while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
+    push @{ $form->{PRICEGROUPS} }, $pkr;
+  }
+  $pkq->finish;
+
+  #find not used pricegroups
+  while ($tmp = pop @{ $form->{PRICEGROUPS} }) {
+    push @pricegroups_not_used, $tmp;
+  }
+
+  # if not used pricegroups are avaible
+  if (@pricegroups_not_used) {
+
+    foreach $name (@pricegroups_not_used) {
+      $form->{"klass_$i"} = "$name->{id}";
+      $form->{"price_$i"} = $form->round_amount($form->{sellprice}, 5);
+      $form->{"price_$i"} =
+        $form->format_amount($myconfig, $form->{"price_$i"}, 5);
+      $form->{"pricegroup_id_$i"} = "$name->{id}";
+      $form->{"pricegroup_$i"}    = "$name->{pricegroup}";
+      $i++;
+    }
+  }
+
+  #correct rows
+  $form->{price_rows} = $i - 1;
+
+  $dbh->disconnect;
+
+  $main::lxdebug->leave_sub();
+}
+
 sub save {
   $main::lxdebug->enter_sub();
 
 sub save {
   $main::lxdebug->enter_sub();
 
@@ -332,6 +448,32 @@ sub save {
              WHERE id = $form->{id}|;
   $dbh->do($query) || $form->dberror($query);
 
              WHERE id = $form->{id}|;
   $dbh->do($query) || $form->dberror($query);
 
+  # delete price records
+  $query = qq|DELETE FROM prices
+              WHERE parts_id = $form->{id}|;
+  $dbh->do($query) || $form->dberror($query);
+
+  # insert price records only if different to sellprice
+  for my $i (1 .. $form->{price_rows}) {
+    if ($form->{"price_$i"} eq "0") {
+      $form->{"price_$i"} = $form->{sellprice};
+    }
+    if (
+        (   $form->{"price_$i"}
+         || $form->{"klass_$i"}
+         || $form->{"pricegroup_id_$i"})
+        and $form->{"price_$i"} != $form->{sellprice}
+      ) {
+      $klass = $form->parse_amount($myconfig, $form->{"klass_$i"});
+      $price = $form->parse_amount($myconfig, $form->{"price_$i"});
+      $pricegroup_id =
+        $form->parse_amount($myconfig, $form->{"pricegroup_id_$i"});
+      $query = qq|INSERT INTO prices (parts_id, pricegroup_id, price)
+                  VALUES($form->{id},$pricegroup_id,$price)|;
+      $dbh->do($query) || $form->dberror($query);
+    }
+  }
+
   # insert makemodel records
   unless ($form->{item} eq 'service') {
     for my $i (1 .. $form->{makemodel_rows}) {
   # insert makemodel records
   unless ($form->{item} eq 'service') {
     for my $i (1 .. $form->{makemodel_rows}) {
@@ -576,6 +718,11 @@ sub delete {
   # connect to database, turn off AutoCommit
   my $dbh = $form->dbconnect_noauto($myconfig);
 
   # connect to database, turn off AutoCommit
   my $dbh = $form->dbconnect_noauto($myconfig);
 
+  # first delete prices of pricegroup 
+  my $query = qq|DELETE FROM prices
+           WHERE parts_id = $form->{id}|;
+  $dbh->do($query) || $form->dberror($query);
+
   my $query = qq|DELETE FROM parts
                 WHERE id = $form->{id}|;
   $dbh->do($query) || $form->dberror($query);
   my $query = qq|DELETE FROM parts
                 WHERE id = $form->{id}|;
   $dbh->do($query) || $form->dberror($query);
@@ -683,14 +830,10 @@ sub all_parts {
   my $group;
   my $limit;
 
   my $group;
   my $limit;
 
-  foreach my $item (qw(partnumber drawing microfiche make model)) {
+  foreach my $item (qw(partnumber drawing microfiche)) {
     if ($form->{$item}) {
       $var = $form->like(lc $form->{$item});
     if ($form->{$item}) {
       $var = $form->like(lc $form->{$item});
-
-      # make will build later Bugfix 145
-      if ($item ne 'make') {
-        $where .= " AND lower(p.$item) LIKE '$var'";
-      }
+      $where .= " AND lower(p.$item) LIKE '$var'";
     }
   }
 
     }
   }
 
@@ -795,7 +938,7 @@ sub all_parts {
 
   my $sortorder = $form->{sort};
   $sortorder .= $form->{desc};
 
   my $sortorder = $form->{sort};
   $sortorder .= $form->{desc};
-  $sortorder = $form->{sort} unless $sortorder;
+  $sortorder = $form->{sort} if $form->{sort};
 
   my $query = "";
 
 
   my $query = "";
 
@@ -858,7 +1001,7 @@ sub all_parts {
                    p.priceupdate, p.image, p.drawing, p.microfiche,
                    pg.partsgroup,
                    a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
                    p.priceupdate, p.image, p.drawing, p.microfiche,
                    pg.partsgroup,
                    a.invnumber, a.ordnumber, a.quonumber, i.trans_id,
-                   ct.name|;
+                   ct.name, i.deliverydate|;
 
       if ($form->{bought}) {
         $query = qq|
 
       if ($form->{bought}) {
         $query = qq|
@@ -902,7 +1045,8 @@ sub all_parts {
         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
       }
 
         $ordwhere .= " AND lower(oi.description) LIKE '$var'";
       }
 
-      $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
+      $flds =
+        qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
                 p.listprice, p.lastcost, p.rop, p.weight,
                 p.priceupdate, p.image, p.drawing, p.microfiche,
                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
                 p.listprice, p.lastcost, p.rop, p.weight,
                 p.priceupdate, p.image, p.drawing, p.microfiche,
@@ -928,7 +1072,8 @@ sub all_parts {
       }
 
       if ($form->{onorder}) {
       }
 
       if ($form->{onorder}) {
-        $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
+        $flds =
+          qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
                   p.listprice, p.lastcost, p.rop, p.weight,
                   p.priceupdate, p.image, p.drawing, p.microfiche,
                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
                   p.listprice, p.lastcost, p.rop, p.weight,
                   p.priceupdate, p.image, p.drawing, p.microfiche,
@@ -965,7 +1110,8 @@ sub all_parts {
         $quowhere .= " AND lower(oi.description) LIKE '$var'";
       }
 
         $quowhere .= " AND lower(oi.description) LIKE '$var'";
       }
 
-      $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
+      $flds =
+        qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
                 p.listprice, p.lastcost, p.rop, p.weight,
                 p.priceupdate, p.image, p.drawing, p.microfiche,
                  oi.qty AS onhand, oi.unit, p.bin, oi.sellprice,
                 p.listprice, p.lastcost, p.rop, p.weight,
                 p.priceupdate, p.image, p.drawing, p.microfiche,
@@ -991,7 +1137,8 @@ sub all_parts {
       }
 
       if ($form->{rfq}) {
       }
 
       if ($form->{rfq}) {
-        $flds = qq|p.id, p.partnumber, oi.description, '' AS serialnumber,
+        $flds =
+          qq|p.id, p.partnumber, oi.description, oi.serialnumber AS serialnumber,
                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
                   p.listprice, p.lastcost, p.rop, p.weight,
                   p.priceupdate, p.image, p.drawing, p.microfiche,
                    oi.qty * -1 AS onhand, oi.unit, p.bin, oi.sellprice,
                   p.listprice, p.lastcost, p.rop, p.weight,
                   p.priceupdate, p.image, p.drawing, p.microfiche,