1 #=====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #=====================================================================
 
   8 # SQL-Ledger Accounting
 
   9 # Copyright (C) 1998-2002
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # Partsgroups and pricegroups
 
  33 #======================================================================
 
  44   $main::lxdebug->enter_sub();
 
  46   my ($self, $myconfig, $form) = @_;
 
  49   my $dbh = $form->dbconnect($myconfig);
 
  53   if ($form->{partsgroup}) {
 
  54     $where .= qq| AND partsgroup ILIKE ?|;
 
  55     push(@values, '%' . $form->{partsgroup} . '%');
 
  58   if ($form->{status} eq 'orphaned') {
 
  61       qq|  (SELECT DISTINCT partsgroup_id FROM parts | .
 
  62       qq|   WHERE NOT partsgroup_id ISNULL) |;
 
  65   substr($where, 0, 4) = "WHERE " if ($where);
 
  67   my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
 
  68   $sortorder =~ s/[^a-z_]//g;
 
  71     qq|SELECT id, partsgroup FROM partsgroup | .
 
  73     qq|ORDER BY $sortorder|;
 
  75   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
  79   $main::lxdebug->leave_sub();
 
  81   return scalar(@{ $form->{item_list} });
 
  85   $main::lxdebug->enter_sub();
 
  87   my ($self, $myconfig, $form) = @_;
 
  90   my $dbh = $form->dbconnect($myconfig);
 
  92   $form->{discount} /= 100;
 
  94   my @values = ($form->{partsgroup});
 
  98     $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
 
  99     push(@values, $form->{id});
 
 101     $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
 
 103   do_query($form, $dbh, $query, @values);
 
 107   $main::lxdebug->leave_sub();
 
 111   $main::lxdebug->enter_sub();
 
 113   my ($self, $myconfig, $form) = @_;
 
 115   # connect to database
 
 116   my $dbh = $form->dbconnect($myconfig);
 
 120     qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
 
 121     qq|FROM partsgroup pg | .
 
 123   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
 
 125   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 127   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 132   $main::lxdebug->leave_sub();
 
 136   $main::lxdebug->enter_sub();
 
 138   my ($self, $myconfig, $form) = @_;
 
 140   # connect to database
 
 141   my $dbh = $form->dbconnect($myconfig);
 
 143   my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup";
 
 145   my $query = qq|DELETE FROM $table WHERE id = ?|;
 
 146   do_query($form, $dbh, $query, $form->{id});
 
 150   $main::lxdebug->leave_sub();
 
 153 ##########################
 
 154 # get pricegroups from database
 
 157   $main::lxdebug->enter_sub();
 
 159   my ($self, $myconfig, $form) = @_;
 
 161   # connect to database
 
 162   my $dbh = $form->dbconnect($myconfig);
 
 164   my ($where, @values);
 
 166   if ($form->{pricegroup}) {
 
 167     $where .= qq| AND pricegroup ILIKE ?|;
 
 168     push(@values, '%' . $form->{pricegroup} . '%');
 
 171   if ($form->{status} eq 'orphaned') {
 
 174     $where .= qq| AND id NOT IN (|;
 
 175     foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 176       $where .= "UNION " unless ($first);
 
 179         qq|SELECT DISTINCT pricegroup_id FROM $table | .
 
 180         qq|WHERE NOT pricegroup_id ISNULL |;
 
 185   substr($where, 0, 4) = "WHERE " if ($where);
 
 187   my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
 
 188   $sortorder =~ s/[^a-z_]//g;
 
 191     qq|SELECT id, pricegroup FROM pricegroup | .
 
 193     qq|ORDER BY $sortorder|;
 
 195   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 199   $main::lxdebug->leave_sub();
 
 201   return scalar(@{ $form->{item_list} });
 
 204 ########################
 
 205 # save pricegruop to database
 
 207 sub save_pricegroup {
 
 208   $main::lxdebug->enter_sub();
 
 210   my ($self, $myconfig, $form) = @_;
 
 212   # connect to database
 
 213   my $dbh = $form->dbconnect($myconfig);
 
 216   $form->{discount} /= 100;
 
 218   my @values = ($form->{pricegroup});
 
 221     $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
 
 222     push(@values, $form->{id});
 
 224     $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
 
 226   do_query($form, $dbh, $query, @values);
 
 230   $main::lxdebug->leave_sub();
 
 233 ############################
 
 234 # get one pricegroup from database
 
 237   $main::lxdebug->enter_sub();
 
 239   my ($self, $myconfig, $form) = @_;
 
 241   # connect to database
 
 242   my $dbh = $form->dbconnect($myconfig);
 
 244   my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
 
 245   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
 246   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 248   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 255   $query = qq|SELECT |;
 
 256   foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 257     $query .= " + " unless ($first);
 
 259     $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
 
 260     push(@values, $form->{id});
 
 263   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 264   $form->{orphaned} = !$form->{orphaned};
 
 268   $main::lxdebug->leave_sub();