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 | .
 
  64       qq|   SELECT DISTINCT partsgroup_id FROM custom_variable_config_partsgroups | .
 
  65       qq|   WHERE NOT partsgroup_id ISNULL) |;
 
  68   substr($where, 0, 4) = "WHERE " if ($where);
 
  70   my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
 
  71   $sortorder =~ s/[^a-z_]//g;
 
  74     qq|SELECT id, partsgroup FROM partsgroup | .
 
  76     qq|ORDER BY $sortorder|;
 
  78   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
  82   $main::lxdebug->leave_sub();
 
  84   return scalar(@{ $form->{item_list} });
 
  88   $main::lxdebug->enter_sub();
 
  90   my ($self, $myconfig, $form) = @_;
 
  93   my $dbh = $form->dbconnect($myconfig);
 
  95   $form->{discount} /= 100;
 
  97   my @values = ($form->{partsgroup});
 
 101     $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
 
 102     push(@values, $form->{id});
 
 104     $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
 
 106   do_query($form, $dbh, $query, @values);
 
 110   $main::lxdebug->leave_sub();
 
 114   $main::lxdebug->enter_sub();
 
 116   my ($self, $myconfig, $form) = @_;
 
 118   # connect to database
 
 119   my $dbh = $form->dbconnect($myconfig);
 
 123     qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
 
 124     qq|FROM partsgroup pg | .
 
 126   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
 
 128   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 130   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 135   # also not orphaned if partsgroup is selected for a cvar filter
 
 136   if ($form->{orphaned}) {
 
 137     my $cvar_count = scalar( @{ SL::DB::PartsGroup->new(id => $form->{id})->custom_variable_configs } );
 
 138     $form->{orphaned} = !$cvar_count;
 
 141   $main::lxdebug->leave_sub();
 
 145   $main::lxdebug->enter_sub();
 
 147   my ($self, $myconfig, $form) = @_;
 
 149   # connect to database
 
 150   my $dbh = $form->dbconnect($myconfig);
 
 152   my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup";
 
 154   my $query = qq|DELETE FROM $table WHERE id = ?|;
 
 155   do_query($form, $dbh, $query, $form->{id});
 
 159   $main::lxdebug->leave_sub();
 
 162 ##########################
 
 163 # get pricegroups from database
 
 166   $main::lxdebug->enter_sub();
 
 168   my ($self, $myconfig, $form) = @_;
 
 170   # connect to database
 
 171   my $dbh = $form->dbconnect($myconfig);
 
 173   my ($where, @values);
 
 175   if ($form->{pricegroup}) {
 
 176     $where .= qq| AND pricegroup ILIKE ?|;
 
 177     push(@values, '%' . $form->{pricegroup} . '%');
 
 180   if ($form->{status} eq 'orphaned') {
 
 183     $where .= qq| AND id NOT IN (|;
 
 184     foreach my $table (qw(invoice orderitems prices)) {
 
 185       $where .= "UNION " unless ($first);
 
 188         qq|SELECT DISTINCT pricegroup_id FROM $table | .
 
 189         qq|WHERE NOT pricegroup_id ISNULL |;
 
 194   substr($where, 0, 4) = "WHERE " if ($where);
 
 196   my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
 
 197   $sortorder =~ s/[^a-z_]//g;
 
 200     qq|SELECT id, pricegroup FROM pricegroup | .
 
 202     qq|ORDER BY $sortorder|;
 
 204   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 208   $main::lxdebug->leave_sub();
 
 210   return scalar(@{ $form->{item_list} });
 
 213 ########################
 
 214 # save pricegruop to database
 
 216 sub save_pricegroup {
 
 217   $main::lxdebug->enter_sub();
 
 219   my ($self, $myconfig, $form) = @_;
 
 221   # connect to database
 
 222   my $dbh = $form->dbconnect($myconfig);
 
 225   $form->{discount} /= 100;
 
 227   my @values = ($form->{pricegroup});
 
 230     $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
 
 231     push(@values, $form->{id});
 
 233     $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
 
 235   do_query($form, $dbh, $query, @values);
 
 239   $main::lxdebug->leave_sub();
 
 242 ############################
 
 243 # get one pricegroup from database
 
 246   $main::lxdebug->enter_sub();
 
 248   my ($self, $myconfig, $form) = @_;
 
 250   # connect to database
 
 251   my $dbh = $form->dbconnect($myconfig);
 
 253   my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
 
 254   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
 255   my $ref = $sth->fetchrow_hashref("NAME_lc");
 
 257   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 264   $query = qq|SELECT |;
 
 265   foreach my $table (qw(invoice orderitems prices)) {
 
 266     $query .= " + " unless ($first);
 
 268     $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
 
 269     push(@values, $form->{id});
 
 272   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 273   $form->{orphaned} = !$form->{orphaned};
 
 277   $main::lxdebug->leave_sub();