kivitendo/SL/PE.pm @ 596c6378
d319704a | Moritz Bunkus | #=====================================================================
|
||
# LX-Office ERP
|
||||
# Copyright (C) 2004
|
||||
# Based on SQL-Ledger Version 2.1.9
|
||||
# Web http://www.lx-office.org
|
||||
#
|
||||
#=====================================================================
|
||||
# SQL-Ledger Accounting
|
||||
# Copyright (C) 1998-2002
|
||||
#
|
||||
# Author: Dieter Simader
|
||||
# Email: dsimader@sql-ledger.org
|
||||
# Web: http://www.sql-ledger.org
|
||||
#
|
||||
# Contributors:
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
#======================================================================
|
||||
#
|
||||
# Project module
|
||||
# also used for partsgroups
|
||||
#
|
||||
#======================================================================
|
||||
package PE;
|
||||
195883fd | Stephan Köhler | use Data::Dumper;
|
||
59f8f1fa | Moritz Bunkus | use SL::DBUtils;
|
||
d319704a | Moritz Bunkus | sub projects {
|
||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my ($where, @values);
|
||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | foreach my $column (qw(projectnumber description)) {
|
||
if ($form->{$column}) {
|
||||
$where .= qq|AND $column ILIKE ? |;
|
||||
push(@values, '%' . $form->{$column} . '%');
|
||||
}
|
||||
d319704a | Moritz Bunkus | }
|
||
527617f2 | Moritz Bunkus | |||
d319704a | Moritz Bunkus | if ($form->{status} eq 'orphaned') {
|
||
527617f2 | Moritz Bunkus | my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
|
||
my $first = 1;
|
||||
$where .= qq|AND id NOT IN (|;
|
||||
foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
|
||||
$where .= "UNION " unless ($first);
|
||||
$first = 0;
|
||||
$where .=
|
||||
qq|SELECT DISTINCT $col_prefix{$table}project_id FROM $table | .
|
||||
qq|WHERE NOT $col_prefix{$table}project_id ISNULL |;
|
||||
}
|
||||
$where .= qq|) |;
|
||||
d319704a | Moritz Bunkus | }
|
||
527617f2 | Moritz Bunkus | |||
59f8f1fa | Moritz Bunkus | if ($form->{active} eq "active") {
|
||
527617f2 | Moritz Bunkus | $where .= qq|AND active |;
|
||
59f8f1fa | Moritz Bunkus | } elsif ($form->{active} eq "inactive") {
|
||
527617f2 | Moritz Bunkus | $where .= qq|AND NOT active |;
|
||
59f8f1fa | Moritz Bunkus | }
|
||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | substr($where, 0, 4) = "WHERE " if ($where);
|
||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | my $sortorder = $form->{sort} ? $form->{sort} : "projectnumber";
|
||
$sortorder =~ s/[^a-z_]//g;
|
||||
my $query =
|
||||
qq|SELECT id, projectnumber, description, active | .
|
||||
qq|FROM project | .
|
||||
$where .
|
||||
qq|ORDER BY $sortorder|;
|
||||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | $form->{project_list} =
|
||
selectall_hashref_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | $dbh->disconnect;
|
||
$main::lxdebug->leave_sub();
|
||||
527617f2 | Moritz Bunkus | return scalar(@{ $form->{project_list} });
|
||
d319704a | Moritz Bunkus | }
|
||
sub get_project {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my $query =
|
||
qq|SELECT * FROM project | .
|
||||
qq|WHERE id = ?|;
|
||||
my @values = ($form->{id});
|
||||
d319704a | Moritz Bunkus | my $sth = $dbh->prepare($query);
|
||
527617f2 | Moritz Bunkus | $sth->execute(@values) || $form->dberror($query);
|
||
d319704a | Moritz Bunkus | |||
my $ref = $sth->fetchrow_hashref(NAME_lc);
|
||||
map { $form->{$_} = $ref->{$_} } keys %$ref;
|
||||
$sth->finish;
|
||||
# check if it is orphaned
|
||||
527617f2 | Moritz Bunkus | my %col_prefix = ("ar" => "global", "ap" => "global", "oe" => "global");
|
||
@values = ();
|
||||
$query = qq|SELECT |;
|
||||
my $first = 1;
|
||||
foreach my $table (qw(acc_trans invoice orderitems rmaitems ar ap oe)) {
|
||||
$query .= " + " unless ($first);
|
||||
$first = 0;
|
||||
$query .=
|
||||
qq|(SELECT COUNT(*) FROM $table | .
|
||||
qq| WHERE $col_prefix{$table}project_id = ?) |;
|
||||
push(@values, $form->{id});
|
||||
}
|
||||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
|
||
d319704a | Moritz Bunkus | $form->{orphaned} = !$form->{orphaned};
|
||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub save_project {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
59f8f1fa | Moritz Bunkus | my @values = ($form->{projectnumber}, $form->{description});
|
||
d319704a | Moritz Bunkus | |||
if ($form->{id}) {
|
||||
59f8f1fa | Moritz Bunkus | $query =
|
||
qq|UPDATE project SET projectnumber = ?, description = ?, active = ? | .
|
||||
qq|WHERE id = ?|;
|
||||
527617f2 | Moritz Bunkus | push(@values, ($form->{active} ? 't' : 'f'), $form->{id});
|
||
d319704a | Moritz Bunkus | } else {
|
||
59f8f1fa | Moritz Bunkus | $query =
|
||
qq|INSERT INTO project (projectnumber, description, active) | .
|
||||
qq|VALUES (?, ?, 't')|;
|
||||
d319704a | Moritz Bunkus | }
|
||
59f8f1fa | Moritz Bunkus | do_query($form, $dbh, $query, @values);
|
||
d319704a | Moritz Bunkus | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub partsgroups {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my ($where, @values);
|
||
d319704a | Moritz Bunkus | |||
if ($form->{partsgroup}) {
|
||||
527617f2 | Moritz Bunkus | $where .= qq| AND partsgroup ILIKE ?|;
|
||
push(@values, '%' . $form->{partsgroup} . '%');
|
||||
d319704a | Moritz Bunkus | }
|
||
if ($form->{status} eq 'orphaned') {
|
||||
527617f2 | Moritz Bunkus | $where .=
|
||
qq| AND id NOT IN | .
|
||||
qq| (SELECT DISTINCT partsgroup_id FROM parts | .
|
||||
qq| WHERE NOT partsgroup_id ISNULL) |;
|
||||
d319704a | Moritz Bunkus | }
|
||
527617f2 | Moritz Bunkus | substr($where, 0, 4) = "WHERE " if ($where);
|
||
d319704a | Moritz Bunkus | |||
527617f2 | Moritz Bunkus | my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
|
||
$sortorder =~ s/[^a-z_]//g;
|
||||
my $query =
|
||||
qq|SELECT id, partsgroup FROM partsgroup | .
|
||||
$where .
|
||||
qq|ORDER BY $sortorder|;
|
||||
$form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
|
||||
d319704a | Moritz Bunkus | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
527617f2 | Moritz Bunkus | return scalar(@{ $form->{item_list} });
|
||
d319704a | Moritz Bunkus | }
|
||
sub save_partsgroup {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
$form->{discount} /= 100;
|
||||
527617f2 | Moritz Bunkus | my @values = ($form->{partsgroup});
|
||
d319704a | Moritz Bunkus | if ($form->{id}) {
|
||
527617f2 | Moritz Bunkus | $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
|
||
push(@values, $form->{id});
|
||||
d319704a | Moritz Bunkus | } else {
|
||
527617f2 | Moritz Bunkus | $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
|
||
d319704a | Moritz Bunkus | }
|
||
527617f2 | Moritz Bunkus | do_query($form, $dbh, $query, @values);
|
||
d319704a | Moritz Bunkus | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub get_partsgroup {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my $query =
|
||
qq|SELECT pg.*, | .
|
||||
qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
|
||||
qq|FROM partsgroup pg | .
|
||||
qq|WHERE pg.id = ?|;
|
||||
my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
|
||||
$form->{id});
|
||||
d319704a | Moritz Bunkus | my $ref = $sth->fetchrow_hashref(NAME_lc);
|
||
527617f2 | Moritz Bunkus | map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
|
||
d319704a | Moritz Bunkus | $sth->finish;
|
||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
sub delete_tuple {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my $table =
|
||
$form->{type} eq "project" ? "project" :
|
||||
$form->{type} eq "pricegroup" ? "pricegroup" :
|
||||
"partsgroup";
|
||||
$query = qq|DELETE FROM $table WHERE id = ?|;
|
||||
do_query($form, $dbh, $query, $form->{id});
|
||||
d319704a | Moritz Bunkus | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
195883fd | Stephan Köhler | ##########################
|
||
# get pricegroups from database
|
||||
#
|
||||
sub pricegroups {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my ($where, @values);
|
||
195883fd | Stephan Köhler | |||
if ($form->{pricegroup}) {
|
||||
527617f2 | Moritz Bunkus | $where .= qq| AND pricegroup ILIKE ?|;
|
||
push(@values, '%' . $form->{pricegroup} . '%');
|
||||
195883fd | Stephan Köhler | }
|
||
if ($form->{status} eq 'orphaned') {
|
||||
527617f2 | Moritz Bunkus | my $first = 1;
|
||
$where .= qq| AND id NOT IN (|;
|
||||
foreach my $table (qw(invoice orderitems prices rmaitems)) {
|
||||
$where .= "UNION " unless ($first);
|
||||
$first = 0;
|
||||
$where .=
|
||||
qq|SELECT DISTINCT pricegroup_id FROM $table | .
|
||||
qq|WHERE NOT pricegroup_id ISNULL |;
|
||||
}
|
||||
$where .= qq|) |;
|
||||
195883fd | Stephan Köhler | }
|
||
527617f2 | Moritz Bunkus | substr($where, 0, 4) = "WHERE " if ($where);
|
||
195883fd | Stephan Köhler | |||
527617f2 | Moritz Bunkus | my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
|
||
$sortorder =~ s/[^a-z_]//g;
|
||||
my $query =
|
||||
qq|SELECT id, pricegroup FROM pricegroup | .
|
||||
$where .
|
||||
qq|ORDER BY $sortorder|;
|
||||
$form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
|
||||
195883fd | Stephan Köhler | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
527617f2 | Moritz Bunkus | return scalar(@{ $form->{item_list} });
|
||
195883fd | Stephan Köhler | }
|
||
527617f2 | Moritz Bunkus | |||
195883fd | Stephan Köhler | ########################
|
||
# save pricegruop to database
|
||||
#
|
||||
sub save_pricegroup {
|
||||
$main::lxdebug->enter_sub();
|
||||
my ($self, $myconfig, $form) = @_;
|
||||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my $query;
|
||
195883fd | Stephan Köhler | |||
$form->{discount} /= 100;
|
||||
527617f2 | Moritz Bunkus | my @values = ($form->{pricegroup});
|
||
195883fd | Stephan Köhler | if ($form->{id}) {
|
||
527617f2 | Moritz Bunkus | $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
|
||
push(@values, $form->{id});
|
||||
195883fd | Stephan Köhler | } else {
|
||
527617f2 | Moritz Bunkus | $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
|
||
195883fd | Stephan Köhler | }
|
||
527617f2 | Moritz Bunkus | do_query($form, $dbh, $query, @values);
|
||
195883fd | Stephan Köhler | |||
$dbh->disconnect;
|
||||
$main::lxdebug->leave_sub();
|
||||
}
|
||||
527617f2 | Moritz Bunkus | |||
195883fd | Stephan Köhler | ############################
|
||
# get one pricegroup from database
|
||||
#
|
||||
sub get_pricegroup {
|
||||
$main::lxdebug->enter_sub();
|
||||
58b14f96 | Stephan Köhler | |||
195883fd | Stephan Köhler | my ($self, $myconfig, $form) = @_;
|
||
# connect to database
|
||||
my $dbh = $form->dbconnect($myconfig);
|
||||
527617f2 | Moritz Bunkus | my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
|
||
my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
|
||||
195883fd | Stephan Köhler | my $ref = $sth->fetchrow_hashref(NAME_lc);
|
||
527617f2 | Moritz Bunkus | map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
|
||
195883fd | Stephan Köhler | |||
$sth->finish;
|
||||
527617f2 | Moritz Bunkus | my $first = 1;
|
||
195883fd | Stephan Köhler | |||
527617f2 | Moritz Bunkus | my @values = ();
|
||
$query = qq|SELECT |;
|
||||
foreach my $table (qw(invoice orderitems prices rmaitems)) {
|
||||
$query .= " + " unless ($first);
|
||||
$first = 0;
|
||||
$query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
|
||||
push(@values, $form->{id});
|
||||
}
|
||||
195883fd | Stephan Köhler | |||
527617f2 | Moritz Bunkus | ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
|
||
$form->{orphaned} = !$form->{orphaned};
|
||||
195883fd | Stephan Köhler | |||
$dbh->disconnect;
|
||||
58b14f96 | Stephan Köhler | |||
195883fd | Stephan Köhler | $main::lxdebug->leave_sub();
|
||
}
|
||||
d319704a | Moritz Bunkus | 1;
|