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);
 
  50   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
 
  52   my $query = qq|SELECT p.id, p.projectnumber, p.description, p.active
 
  56   if ($form->{projectnumber}) {
 
  57     my $projectnumber = $form->like(lc $form->{projectnumber});
 
  58     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
 
  60   if ($form->{projectdescription}) {
 
  61     my $description = $form->like(lc $form->{projectdescription});
 
  62     $query .= " AND lower(description) LIKE '$description'";
 
  64   if ($form->{status} eq 'orphaned') {
 
  65     $query .= " AND id NOT IN (SELECT p.id
 
  66                                FROM project p, acc_trans a
 
  67                                WHERE p.id = a.project_id)
 
  68                 AND id NOT IN (SELECT p.id
 
  69                                FROM project p, invoice i
 
  70                                WHERE p.id = i.project_id)
 
  71                 AND id NOT IN (SELECT p.id
 
  72                                FROM project p, orderitems o
 
  73                                WHERE p.id = o.project_id)";
 
  75   if ($form->{active} eq "active") {
 
  76     $query .= " AND p.active";
 
  77   } elsif ($form->{active} eq "inactive") {
 
  78     $query .= " AND NOT p.active";
 
  84   $sth = $dbh->prepare($query);
 
  85   $sth->execute || $form->dberror($query);
 
  88   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  89     push @{ $form->{project_list} }, $ref;
 
  96   $main::lxdebug->leave_sub();
 
 102   $main::lxdebug->enter_sub();
 
 104   my ($self, $myconfig, $form) = @_;
 
 106   # connect to database
 
 107   my $dbh = $form->dbconnect($myconfig);
 
 109   my $query = qq|SELECT p.*
 
 111                  WHERE p.id = $form->{id}|;
 
 112   my $sth = $dbh->prepare($query);
 
 113   $sth->execute || $form->dberror($query);
 
 115   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 117   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 121   # check if it is orphaned
 
 122   $query = qq|SELECT count(*)
 
 124               WHERE a.project_id = $form->{id}|;
 
 125   $sth = $dbh->prepare($query);
 
 126   $sth->execute || $form->dberror($query);
 
 128   ($form->{orphaned}) = $sth->fetchrow_array;
 
 129   $form->{orphaned} = !$form->{orphaned};
 
 135   $main::lxdebug->leave_sub();
 
 139   $main::lxdebug->enter_sub();
 
 141   my ($self, $myconfig, $form) = @_;
 
 143   # connect to database
 
 144   my $dbh = $form->dbconnect($myconfig);
 
 146   my @values = ($form->{projectnumber}, $form->{description});
 
 150       qq|UPDATE project SET projectnumber = ?, description = ?, active = ? | .
 
 152     push(@values, $form->{active} ? 't' : 'f', $form->{id});
 
 155       qq|INSERT INTO project (projectnumber, description, active) | .
 
 156       qq|VALUES (?, ?, 't')|;
 
 158   do_query($form, $dbh, $query, @values);
 
 162   $main::lxdebug->leave_sub();
 
 166   $main::lxdebug->enter_sub();
 
 168   my ($self, $myconfig, $form) = @_;
 
 172   # connect to database
 
 173   my $dbh = $form->dbconnect($myconfig);
 
 175   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
 
 177   my $query = qq|SELECT g.*
 
 182   if ($form->{partsgroup}) {
 
 183     $var = $form->like(lc $form->{partsgroup});
 
 184     $where .= " AND lower(g.partsgroup) LIKE '$var'";
 
 188                ORDER BY $sortorder|;
 
 190   if ($form->{status} eq 'orphaned') {
 
 191     $query = qq|SELECT g.*
 
 193                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
 
 198                 JOIN parts p ON (p.partsgroup_id = g.id)
 
 200                 ORDER BY $sortorder|;
 
 203   $sth = $dbh->prepare($query);
 
 204   $sth->execute || $form->dberror($query);
 
 207   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 208     push @{ $form->{item_list} }, $ref;
 
 215   $main::lxdebug->leave_sub();
 
 220 sub save_partsgroup {
 
 221   $main::lxdebug->enter_sub();
 
 223   my ($self, $myconfig, $form) = @_;
 
 225   # connect to database
 
 226   my $dbh = $form->dbconnect($myconfig);
 
 228   map { $form->{$_} =~ s/\'/\'\'/g } qw(partsgroup);
 
 229   $form->{discount} /= 100;
 
 232     $query = qq|UPDATE partsgroup SET
 
 233                 partsgroup = '$form->{partsgroup}'
 
 234                 WHERE id = $form->{id}|;
 
 236     $query = qq|INSERT INTO partsgroup
 
 238                 VALUES ('$form->{partsgroup}')|;
 
 240   $dbh->do($query) || $form->dberror($query);
 
 244   $main::lxdebug->leave_sub();
 
 248   $main::lxdebug->enter_sub();
 
 250   my ($self, $myconfig, $form) = @_;
 
 252   # connect to database
 
 253   my $dbh = $form->dbconnect($myconfig);
 
 255   my $query = qq|SELECT p.*
 
 257                  WHERE p.id = $form->{id}|;
 
 258   my $sth = $dbh->prepare($query);
 
 259   $sth->execute || $form->dberror($query);
 
 261   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 263   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 267   # check if it is orphaned
 
 268   $query = qq|SELECT count(*)
 
 270               WHERE p.partsgroup_id = $form->{id}|;
 
 271   $sth = $dbh->prepare($query);
 
 272   $sth->execute || $form->dberror($query);
 
 274   ($form->{orphaned}) = $sth->fetchrow_array;
 
 275   $form->{orphaned} = !$form->{orphaned};
 
 281   $main::lxdebug->leave_sub();
 
 285   $main::lxdebug->enter_sub();
 
 287   my ($self, $myconfig, $form) = @_;
 
 289   # connect to database
 
 290   my $dbh = $form->dbconnect($myconfig);
 
 292   $query = qq|DELETE FROM $form->{type}
 
 293               WHERE id = $form->{id}|;
 
 294   $dbh->do($query) || $form->dberror($query);
 
 298   $main::lxdebug->leave_sub();
 
 301 ##########################
 
 302 # get pricegroups from database
 
 305   $main::lxdebug->enter_sub();
 
 307   my ($self, $myconfig, $form) = @_;
 
 311   # connect to database
 
 312   my $dbh = $form->dbconnect($myconfig);
 
 314   my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
 
 316   my $query = qq|SELECT g.id, g.pricegroup
 
 321   if ($form->{pricegroup}) {
 
 322     $var = $form->like(lc $form->{pricegroup});
 
 323     $where .= " AND lower(g.pricegroup) LIKE '$var'";
 
 327                ORDER BY $sortorder|;
 
 329   if ($form->{status} eq 'orphaned') {
 
 330     $query = qq|SELECT pg.*
 
 332                 LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
 
 337                 JOIN prices p ON (p.pricegroup_id = pg.id)
 
 339                 ORDER BY $sortorder|;
 
 342   $sth = $dbh->prepare($query);
 
 343   $sth->execute || $form->dberror($query);
 
 346   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 347     push @{ $form->{item_list} }, $ref;
 
 354   $main::lxdebug->leave_sub();
 
 358 ########################
 
 359 # save pricegruop to database
 
 361 sub save_pricegroup {
 
 362   $main::lxdebug->enter_sub();
 
 364   my ($self, $myconfig, $form) = @_;
 
 366   # connect to database
 
 367   my $dbh = $form->dbconnect($myconfig);
 
 369   map { $form->{$_} =~ s/\'/\'\'/g } qw(pricegroup);
 
 371   $form->{discount} /= 100;
 
 374     $query = qq|UPDATE pricegroup SET
 
 375                 pricegroup = '$form->{pricegroup}'
 
 376                 WHERE id = $form->{id}|;
 
 378     $query = qq|INSERT INTO pricegroup
 
 380                 VALUES ('$form->{pricegroup}')|;
 
 382   $dbh->do($query) || $form->dberror($query);
 
 386   $main::lxdebug->leave_sub();
 
 388 ############################
 
 389 # get one pricegroup from database
 
 392   $main::lxdebug->enter_sub();
 
 394   my ($self, $myconfig, $form) = @_;
 
 396   # connect to database
 
 397   my $dbh = $form->dbconnect($myconfig);
 
 399   my $query = qq|SELECT p.id, p.pricegroup
 
 401                  WHERE p.id = $form->{id}|;
 
 402   my $sth = $dbh->prepare($query);
 
 403   $sth->execute || $form->dberror($query);
 
 405   my $ref = $sth->fetchrow_hashref(NAME_lc);
 
 407   map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 411   # check if it is orphaned
 
 412   $query = qq|SELECT count(*)
 
 414               WHERE p.pricegroup_id = $form->{id}|;
 
 415   $sth = $dbh->prepare($query);
 
 416   $sth->execute || $form->dberror($query);
 
 418   ($form->{orphaned}) = $sth->fetchrow_array;
 
 419   $form->{orphaned} = !$form->{orphaned};
 
 425   $main::lxdebug->leave_sub();