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 #======================================================================
 
  39   $main::lxdebug->enter_sub();
 
  41   my ($self, $myconfig, $form) = @_;
 
  44   my $dbh = $form->dbconnect($myconfig);
 
  46   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
 
  48   my $query = qq|SELECT p.id, p.projectnumber, p.description
 
  52   if ($form->{projectnumber}) {
 
  53     my $projectnumber = $form->like(lc $form->{projectnumber});
 
  54     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
 
  56   if ($form->{projectdescription}) {
 
  57     my $description = $form->like(lc $form->{projectdescription});
 
  58     $query .= " AND lower(description) LIKE '$description'";
 
  60   if ($form->{status} eq 'orphaned') {
 
  61     $query .= " AND id NOT IN (SELECT p.id
 
  62                                FROM project p, acc_trans a
 
  63                                WHERE p.id = a.project_id)
 
  64                 AND id NOT IN (SELECT p.id
 
  65                                FROM project p, invoice i
 
  66                                WHERE p.id = i.project_id)
 
  67                 AND id NOT IN (SELECT p.id
 
  68                                FROM project p, orderitems o
 
  69                                WHERE p.id = o.project_id)";
 
  75   $sth = $dbh->prepare($query);
 
  76   $sth->execute || $form->dberror($query);
 
  79   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  80     push @{ $form->{project_list} }, $ref;
 
  87   $main::lxdebug->leave_sub();
 
  93   $main::lxdebug->enter_sub();
 
  95   my ($self, $myconfig, $form) = @_;
 
  98   my $dbh = $form->dbconnect($myconfig);
 
 100   my $query = qq|SELECT p.*
 
 102                  WHERE p.id = $form->{id}|;
 
 103   my $sth = $dbh->prepare($query);
 
 104   $sth->execute || $form->dberror($query);
 
 106   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 108   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 112   # check if it is orphaned
 
 113   $query = qq|SELECT count(*)
 
 115               WHERE a.project_id = $form->{id}|;
 
 116   $sth = $dbh->prepare($query);
 
 117   $sth->execute || $form->dberror($query);
 
 119   ($form->{orphaned}) = $sth->fetchrow_array;
 
 120   $form->{orphaned} = !$form->{orphaned};
 
 126   $main::lxdebug->leave_sub();
 
 130   $main::lxdebug->enter_sub();
 
 132   my ($self, $myconfig, $form) = @_;
 
 134   # connect to database
 
 135   my $dbh = $form->dbconnect($myconfig);
 
 137   map { $form->{$_} =~ s/\'/\'\'/g } (projectnumber, description);
 
 140     $query = qq|UPDATE project SET
 
 141                 projectnumber = '$form->{projectnumber}',
 
 142                 description = '$form->{description}'
 
 143                 WHERE id = $form->{id}|;
 
 145     $query = qq|INSERT INTO project
 
 146                 (projectnumber, description)
 
 147                 VALUES ('$form->{projectnumber}', '$form->{description}')|;
 
 149   $dbh->do($query) || $form->dberror($query);
 
 153   $main::lxdebug->leave_sub();
 
 157   $main::lxdebug->enter_sub();
 
 159   my ($self, $myconfig, $form) = @_;
 
 163   # connect to database
 
 164   my $dbh = $form->dbconnect($myconfig);
 
 166   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
 
 168   my $query = qq|SELECT g.*
 
 173   if ($form->{partsgroup}) {
 
 174     $var = $form->like(lc $form->{partsgroup});
 
 175     $where .= " AND lower(g.partsgroup) LIKE '$var'";
 
 179                ORDER BY $sortorder|;
 
 181   if ($form->{status} eq 'orphaned') {
 
 182     $query = qq|SELECT g.*
 
 184                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
 
 189                 JOIN parts p ON (p.partsgroup_id = g.id)
 
 191                 ORDER BY $sortorder|;
 
 194   $sth = $dbh->prepare($query);
 
 195   $sth->execute || $form->dberror($query);
 
 198   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 199     push @{ $form->{item_list} }, $ref;
 
 206   $main::lxdebug->leave_sub();
 
 211 sub save_partsgroup {
 
 212   $main::lxdebug->enter_sub();
 
 214   my ($self, $myconfig, $form) = @_;
 
 216   # connect to database
 
 217   my $dbh = $form->dbconnect($myconfig);
 
 219   map { $form->{$_} =~ s/\'/\'\'/g } (partsgroup);
 
 221   $form->{discount} /= 100;
 
 224     $query = qq|UPDATE partsgroup SET
 
 225                 partsgroup = '$form->{partsgroup}'
 
 226                 WHERE id = $form->{id}|;
 
 228     $query = qq|INSERT INTO partsgroup
 
 230                 VALUES ('$form->{partsgroup}')|;
 
 232   $dbh->do($query) || $form->dberror($query);
 
 236   $main::lxdebug->leave_sub();
 
 240   $main::lxdebug->enter_sub();
 
 242   my ($self, $myconfig, $form) = @_;
 
 244   # connect to database
 
 245   my $dbh = $form->dbconnect($myconfig);
 
 247   my $query = qq|SELECT p.*
 
 249                  WHERE p.id = $form->{id}|;
 
 250   my $sth = $dbh->prepare($query);
 
 251   $sth->execute || $form->dberror($query);
 
 253   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 255   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 259   # check if it is orphaned
 
 260   $query = qq|SELECT count(*)
 
 262               WHERE p.partsgroup_id = $form->{id}|;
 
 263   $sth = $dbh->prepare($query);
 
 264   $sth->execute || $form->dberror($query);
 
 266   ($form->{orphaned}) = $sth->fetchrow_array;
 
 267   $form->{orphaned} = !$form->{orphaned};
 
 273   $main::lxdebug->leave_sub();
 
 277   $main::lxdebug->enter_sub();
 
 279   my ($self, $myconfig, $form) = @_;
 
 281   # connect to database
 
 282   my $dbh = $form->dbconnect($myconfig);
 
 284   $query = qq|DELETE FROM $form->{type}
 
 285               WHERE id = $form->{id}|;
 
 286   $dbh->do($query) || $form->dberror($query);
 
 290   $main::lxdebug->leave_sub();