Fixes für das "my $var if $cond;" pattern.
[kivitendo-erp.git] / SL / Projects.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Project module
32 #
33 #======================================================================
34
35 package Projects;
36
37 use Data::Dumper;
38
39 use SL::DBUtils;
40 use SL::CVar;
41
42 use strict;
43
44 my %project_id_column_prefixes  = ("ar"              => "global",
45                                    "ap"              => "global",
46                                    "oe"              => "global",
47                                    "delivery_orders" => "global");
48
49 my @tables_with_project_id_cols = qw(acc_trans
50                                      invoice
51                                      orderitems
52                                      rmaitems
53                                      ar
54                                      ap
55                                      oe
56                                      delivery_orders
57                                      delivery_order_items);
58
59 sub search_projects {
60   $main::lxdebug->enter_sub();
61
62   my $self     = shift;
63   my %params   = @_;
64
65   my $myconfig = \%main::myconfig;
66   my $form     = $main::form;
67
68   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
69
70   my (@filters, @values);
71
72   foreach my $column (qw(projectnumber description)) {
73     if ($params{$column}) {
74       push @filters, "p.$column ILIKE ?";
75       push @values, '%' . $params{$column} . '%';
76     }
77   }
78
79   if ($params{status} eq 'orphaned') {
80     my @sub_filters;
81
82     foreach my $table (@tables_with_project_id_cols) {
83       push @sub_filters, qq|SELECT DISTINCT $project_id_column_prefixes{$table}project_id FROM $table
84                             WHERE NOT $project_id_column_prefixes{$table}project_id ISNULL|;
85     }
86
87     push @filters, "p.id NOT IN (" . join(" UNION ", @sub_filters) . ")";
88   }
89
90   if ($params{active} eq "active") {
91     push @filters, 'p.active';
92
93   } elsif ($params{active} eq "inactive") {
94     push @filters, 'NOT COALESCE(p.active, FALSE)';
95   }
96
97   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'Projects',
98                                                             'trans_id_field' => 'p.id',
99                                                             'filter'         => $form);
100
101   if ($cvar_where) {
102     push @filters, $cvar_where;
103     push @values,  @cvar_values;
104   }
105
106
107   my $where = @filters ? 'WHERE ' . join(' AND ', map { "($_)" } @filters) : '';
108
109   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
110   $sortorder    =~ s/[^a-z_]//g;
111   my $query     = qq|SELECT p.id, p.projectnumber, p.description, p.active
112                      FROM project p
113                      $where
114                      ORDER BY $sortorder|;
115
116   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
117
118   $main::lxdebug->leave_sub();
119
120   return scalar(@{ $form->{project_list} });
121 }
122
123 sub get_project {
124   $main::lxdebug->enter_sub();
125
126   my $self     = shift;
127   my %params   = @_;
128
129   if (!$params{id}) {
130     $main::lxdebug->leave_sub();
131     return { };
132   }
133
134   my $myconfig = \%main::myconfig;
135   my $form     = $main::form;
136
137   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
138
139   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
140
141   if ($params{orphaned}) {
142     # check if it is orphaned
143     my (@values, $query);
144
145     foreach my $table (@tables_with_project_id_cols) {
146       $query .= " + " if ($query);
147       $query .= qq|(SELECT COUNT(*) FROM $table
148                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
149       push @values, conv_i($params{id});
150     }
151
152     $query = 'SELECT ' . $query;
153
154     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
155     $project->{orphaned}   = !$project->{orphaned};
156   }
157
158   $main::lxdebug->leave_sub();
159
160   return $project;
161 }
162
163 sub save_project {
164   $main::lxdebug->enter_sub();
165
166   my $self     = shift;
167   my %params   = @_;
168
169   my $myconfig = \%main::myconfig;
170   my $form     = $main::form;
171
172   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
173
174   my @values;
175
176   if (!$params{id}) {
177     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
178     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
179
180     $params{active} = 1;
181   }
182
183   my $query  = qq|UPDATE project SET projectnumber = ?, description = ?, active = ?
184                WHERE id = ?|;
185
186   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{id}));
187   do_query($form, $dbh, $query, @values);
188
189   CVar->save_custom_variables('dbh'       => $dbh,
190                               'module'    => 'Projects',
191                               'trans_id'  => $params{id},
192                               'variables' => $form,
193                               'always_valid' => 1);
194
195   $dbh->commit();
196
197   $main::lxdebug->leave_sub();
198
199   return $params{id};
200 }
201
202 sub delete_project {
203   $main::lxdebug->enter_sub();
204
205   my $self     = shift;
206   my %params   = @_;
207
208   Common::check_params(\%params, qw(id));
209
210   my $myconfig = \%main::myconfig;
211   my $form     = $main::form;
212
213   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
214
215   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
216
217   $dbh->commit();
218
219   $main::lxdebug->leave_sub();
220 }
221
222 1;
223