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 #======================================================================
 
  32 # also used for partsgroups
 
  34 #======================================================================
 
  43   $main::lxdebug->enter_sub();
 
  45   my ($self, $myconfig, $form) = @_;
 
  48   my $dbh = $form->dbconnect($myconfig);
 
  52   foreach my $column (qw(projectnumber description)) {
 
  53     if ($form->{$column}) {
 
  54       $where .= qq|AND $column ILIKE ? |;
 
  55       push(@values, '%' . $form->{$column} . '%');
 
  59   if ($form->{status} eq 'orphaned') {
 
  60     my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
 
  63     $where .= qq|AND id NOT IN (|;
 
  64     foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
 
  65       $where .= "UNION " unless ($first);
 
  68         qq|SELECT DISTINCT $col_prefix{$table}project_id FROM $table | .
 
  69         qq|WHERE NOT $col_prefix{$table}project_id ISNULL |;
 
  74   if ($form->{active} eq "active") {
 
  75     $where .= qq|AND active |;
 
  76   } elsif ($form->{active} eq "inactive") {
 
  77     $where .= qq|AND NOT active |;
 
  80   substr($where, 0, 4) = "WHERE " if ($where);
 
  82   my $sortorder = $form->{sort} ? $form->{sort} : "projectnumber";
 
  83   $sortorder =~ s/[^a-z_]//g;
 
  85     qq|SELECT id, projectnumber, description, active | .
 
  88     qq|ORDER BY $sortorder|;
 
  90   $form->{project_list} =
 
  91     selectall_hashref_query($form, $dbh, $query, @values);
 
  94   $main::lxdebug->leave_sub();
 
  96   return scalar(@{ $form->{project_list} });
 
 100   $main::lxdebug->enter_sub();
 
 102   my ($self, $myconfig, $form) = @_;
 
 104   # connect to database
 
 105   my $dbh = $form->dbconnect($myconfig);
 
 108     qq|SELECT * FROM project | .
 
 110         my @values = ($form->{id});
 
 111   my $sth = $dbh->prepare($query);
 
 112   $sth->execute(@values) || $form->dberror($query);
 
 114   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 116   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 120   # check if it is orphaned
 
 121   my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
 
 123   $query = qq|SELECT |;
 
 125   foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
 
 126     $query .= " + " unless ($first);
 
 129       qq|(SELECT COUNT(*) FROM $table | .
 
 130       qq| WHERE $col_prefix{$table}project_id = ?) |;
 
 131     push(@values, $form->{id});
 
 134   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 135   $form->{orphaned} = !$form->{orphaned};
 
 139   $main::lxdebug->leave_sub();
 
 143   $main::lxdebug->enter_sub();
 
 145   my ($self, $myconfig, $form) = @_;
 
 147   # connect to database
 
 148   my $dbh = $form->dbconnect($myconfig);
 
 150   my @values = ($form->{projectnumber}, $form->{description});
 
 154       qq|UPDATE project SET projectnumber = ?, description = ?, active = ? | .
 
 156     push(@values, ($form->{active} ? 't' : 'f'), $form->{id});
 
 159       qq|INSERT INTO project (projectnumber, description, active) | .
 
 160       qq|VALUES (?, ?, 't')|;
 
 162   do_query($form, $dbh, $query, @values);
 
 166   $main::lxdebug->leave_sub();
 
 170   $main::lxdebug->enter_sub();
 
 172   my ($self, $myconfig, $form) = @_;
 
 174   # connect to database
 
 175   my $dbh = $form->dbconnect($myconfig);
 
 177   my ($where, @values);
 
 179   if ($form->{partsgroup}) {
 
 180     $where .= qq| AND partsgroup ILIKE ?|;
 
 181     push(@values, '%' . $form->{partsgroup} . '%');
 
 184   if ($form->{status} eq 'orphaned') {
 
 186       qq| AND id NOT IN | .
 
 187       qq|  (SELECT DISTINCT partsgroup_id FROM parts | .
 
 188       qq|   WHERE NOT partsgroup_id ISNULL) |;
 
 191   substr($where, 0, 4) = "WHERE " if ($where);
 
 193   my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
 
 194   $sortorder =~ s/[^a-z_]//g;
 
 197     qq|SELECT id, partsgroup FROM partsgroup | .
 
 199     qq|ORDER BY $sortorder|;
 
 201   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 205   $main::lxdebug->leave_sub();
 
 207   return scalar(@{ $form->{item_list} });
 
 210 sub save_partsgroup {
 
 211   $main::lxdebug->enter_sub();
 
 213   my ($self, $myconfig, $form) = @_;
 
 215   # connect to database
 
 216   my $dbh = $form->dbconnect($myconfig);
 
 218   $form->{discount} /= 100;
 
 220   my @values = ($form->{partsgroup});
 
 223     $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
 
 224                 push(@values, $form->{id});
 
 226     $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
 
 228   do_query($form, $dbh, $query, @values);
 
 232   $main::lxdebug->leave_sub();
 
 236   $main::lxdebug->enter_sub();
 
 238   my ($self, $myconfig, $form) = @_;
 
 240   # connect to database
 
 241   my $dbh = $form->dbconnect($myconfig);
 
 245     qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
 
 246     qq|FROM partsgroup pg | .
 
 248   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
 
 250   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 252   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 257   $main::lxdebug->leave_sub();
 
 261   $main::lxdebug->enter_sub();
 
 263   my ($self, $myconfig, $form) = @_;
 
 265   # connect to database
 
 266   my $dbh = $form->dbconnect($myconfig);
 
 269     $form->{type} eq "project" ? "project" :
 
 270     $form->{type} eq "pricegroup" ? "pricegroup" :
 
 273   $query = qq|DELETE FROM $table WHERE id = ?|;
 
 274   do_query($form, $dbh, $query, $form->{id});
 
 278   $main::lxdebug->leave_sub();
 
 281 ##########################
 
 282 # get pricegroups from database
 
 285   $main::lxdebug->enter_sub();
 
 287   my ($self, $myconfig, $form) = @_;
 
 289   # connect to database
 
 290   my $dbh = $form->dbconnect($myconfig);
 
 292   my ($where, @values);
 
 294   if ($form->{pricegroup}) {
 
 295     $where .= qq| AND pricegroup ILIKE ?|;
 
 296     push(@values, '%' . $form->{pricegroup} . '%');
 
 299   if ($form->{status} eq 'orphaned') {
 
 302     $where .= qq| AND id NOT IN (|;
 
 303     foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 304       $where .= "UNION " unless ($first);
 
 307         qq|SELECT DISTINCT pricegroup_id FROM $table | .
 
 308         qq|WHERE NOT pricegroup_id ISNULL |;
 
 313   substr($where, 0, 4) = "WHERE " if ($where);
 
 315   my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
 
 316   $sortorder =~ s/[^a-z_]//g;
 
 319     qq|SELECT id, pricegroup FROM pricegroup | .
 
 321     qq|ORDER BY $sortorder|;
 
 323   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
 
 327   $main::lxdebug->leave_sub();
 
 329   return scalar(@{ $form->{item_list} });
 
 332 ########################
 
 333 # save pricegruop to database
 
 335 sub save_pricegroup {
 
 336   $main::lxdebug->enter_sub();
 
 338   my ($self, $myconfig, $form) = @_;
 
 340   # connect to database
 
 341   my $dbh = $form->dbconnect($myconfig);
 
 344   $form->{discount} /= 100;
 
 346   my @values = ($form->{pricegroup});
 
 349     $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
 
 350                 push(@values, $form->{id});
 
 352     $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
 
 354   do_query($form, $dbh, $query, @values);
 
 358   $main::lxdebug->leave_sub();
 
 361 ############################
 
 362 # get one pricegroup from database
 
 365   $main::lxdebug->enter_sub();
 
 367   my ($self, $myconfig, $form) = @_;
 
 369   # connect to database
 
 370   my $dbh = $form->dbconnect($myconfig);
 
 372   my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
 
 373   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
 374   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 376   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
 
 383   $query = qq|SELECT |;
 
 384   foreach my $table (qw(invoice orderitems prices rmaitems)) {
 
 385     $query .= " + " unless ($first);
 
 387     $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
 
 388     push(@values, $form->{id});
 
 391   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
 
 392   $form->{orphaned} = !$form->{orphaned};
 
 396   $main::lxdebug->leave_sub();