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 #======================================================================
 
  42   $main::lxdebug->enter_sub();
 
  44   my ($self, $myconfig, $form) = @_;
 
  47   my $dbh = $form->dbconnect($myconfig);
 
  51   if ($form->{partsgroup}) {
 
  52     $where .= qq| AND partsgroup ILIKE ?|;
 
  53     push(@values, '%' . $form->{partsgroup} . '%');
 
  56   if ($form->{status} eq 'orphaned') {
 
  59       qq|  (SELECT DISTINCT partsgroup_id FROM parts | .
 
  60       qq|   WHERE NOT partsgroup_id ISNULL) |;
 
  63   substr($where, 0, 4) = "WHERE " if ($where);
 
  65   my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
 
  66   $sortorder =~ s/[^a-z_]//g;
 
  69     qq|SELECT id, partsgroup FROM partsgroup | .
 
  71     qq|ORDER BY $sortorder|;
 
  73   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
  77   $main::lxdebug->leave_sub();
 
  79   return scalar(@{ $form->{item_list} });
 
  83   $main::lxdebug->enter_sub();
 
  85   my ($self, $myconfig, $form) = @_;
 
  88   my $dbh = $form->dbconnect($myconfig);
 
  90   $form->{discount} /= 100;
 
  92   my @values = ($form->{partsgroup});
 
  95     $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
 
  96                 push(@values, $form->{id});
 
  98     $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
 
 100   do_query($form, $dbh, $query, @values);
 
 104   $main::lxdebug->leave_sub();
 
 108   $main::lxdebug->enter_sub();
 
 110   my ($self, $myconfig, $form) = @_;
 
 112   # connect to database
 
 113   my $dbh = $form->dbconnect($myconfig);
 
 117     qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
 
 118     qq|FROM partsgroup pg | .
 
 120   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
 
 122   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 124   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 129   $main::lxdebug->leave_sub();
 
 133   $main::lxdebug->enter_sub();
 
 135   my ($self, $myconfig, $form) = @_;
 
 137   # connect to database
 
 138   my $dbh = $form->dbconnect($myconfig);
 
 140   my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup";
 
 142   $query = qq|DELETE FROM $table WHERE id = ?|;
 
 143   do_query($form, $dbh, $query, $form->{id});
 
 147   $main::lxdebug->leave_sub();
 
 150 ##########################
 
 151 # get pricegroups from database
 
 154   $main::lxdebug->enter_sub();
 
 156   my ($self, $myconfig, $form) = @_;
 
 158   # connect to database
 
 159   my $dbh = $form->dbconnect($myconfig);
 
 161   my ($where, @values);
 
 163   if ($form->{pricegroup}) {
 
 164     $where .= qq| AND pricegroup ILIKE ?|;
 
 165     push(@values, '%' . $form->{pricegroup} . '%');
 
 168   if ($form->{status} eq 'orphaned') {
 
 171     $where .= qq| AND id NOT IN (|;
 
 172     foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 173       $where .= "UNION " unless ($first);
 
 176         qq|SELECT DISTINCT pricegroup_id FROM $table | .
 
 177         qq|WHERE NOT pricegroup_id ISNULL |;
 
 182   substr($where, 0, 4) = "WHERE " if ($where);
 
 184   my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
 
 185   $sortorder =~ s/[^a-z_]//g;
 
 188     qq|SELECT id, pricegroup FROM pricegroup | .
 
 190     qq|ORDER BY $sortorder|;
 
 192   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 196   $main::lxdebug->leave_sub();
 
 198   return scalar(@{ $form->{item_list} });
 
 201 ########################
 
 202 # save pricegruop to database
 
 204 sub save_pricegroup {
 
 205   $main::lxdebug->enter_sub();
 
 207   my ($self, $myconfig, $form) = @_;
 
 209   # connect to database
 
 210   my $dbh = $form->dbconnect($myconfig);
 
 213   $form->{discount} /= 100;
 
 215   my @values = ($form->{pricegroup});
 
 218     $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
 
 219                 push(@values, $form->{id});
 
 221     $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
 
 223   do_query($form, $dbh, $query, @values);
 
 227   $main::lxdebug->leave_sub();
 
 230 ############################
 
 231 # get one pricegroup from database
 
 234   $main::lxdebug->enter_sub();
 
 236   my ($self, $myconfig, $form) = @_;
 
 238   # connect to database
 
 239   my $dbh = $form->dbconnect($myconfig);
 
 241   my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
 
 242   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
 243   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 245   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 252   $query = qq|SELECT |;
 
 253   foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 254     $query .= " + " unless ($first);
 
 256     $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
 
 257     push(@values, $form->{id});
 
 260   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 261   $form->{orphaned} = !$form->{orphaned};
 
 265   $main::lxdebug->leave_sub();