+ my ($where, @values);
+
+ if ($form->{pricegroup}) {
+ $where .= qq| AND pricegroup ILIKE ?|;
+ push(@values, '%' . $form->{pricegroup} . '%');
+ }
+
+ if ($form->{status} eq 'orphaned') {
+ my $first = 1;
+
+ $where .= qq| AND id NOT IN (|;
+ foreach my $table (qw(invoice orderitems prices rmaitems)) {
+ $where .= "UNION " unless ($first);
+ $first = 0;
+ $where .=
+ qq|SELECT DISTINCT pricegroup_id FROM $table | .
+ qq|WHERE NOT pricegroup_id ISNULL |;
+ }
+ $where .= qq|) |;
+ }
+
+ substr($where, 0, 4) = "WHERE " if ($where);
+
+ my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
+ $sortorder =~ s/[^a-z_]//g;
+
+ my $query =
+ qq|SELECT id, pricegroup FROM pricegroup | .
+ $where .
+ qq|ORDER BY $sortorder|;
+
+ $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+
+ return scalar(@{ $form->{item_list} });
+}
+
+########################
+# save pricegruop to database
+#
+sub save_pricegroup {