Projektverwaltung in eine eigene Datei ausgelagert und auf die Verwendung von Templat...
[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
41 my %project_id_column_prefixes  = ("ar"              => "global",
42                                    "ap"              => "global",
43                                    "oe"              => "global",
44                                    "delivery_orders" => "global");
45
46 my @tables_with_project_id_cols = qw(acc_trans
47                                      invoice
48                                      orderitems
49                                      rmaitems
50                                      ar
51                                      ap
52                                      oe
53                                      delivery_orders
54                                      delivery_order_items);
55
56 sub search_projects {
57   $main::lxdebug->enter_sub();
58
59   my $self     = shift;
60   my %params   = @_;
61
62   my $myconfig = \%main::myconfig;
63   my $form     = $main::form;
64
65   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
66
67   my (@filters, @values);
68
69   foreach my $column (qw(projectnumber description)) {
70     if ($params{$column}) {
71       push @filters, "$column ILIKE ?";
72       push @values, '%' . $params{$column} . '%';
73     }
74   }
75
76   if ($params{status} eq 'orphaned') {
77     my @sub_filters;
78
79     foreach my $table (@tables_with_project_id_cols) {
80       push @sub_filters, qq|SELECT DISTINCT $project_id_column_prefixes{$table}project_id FROM $table
81                             WHERE NOT $project_id_column_prefixes{$table}project_id ISNULL|;
82     }
83
84     push @filters, "id NOT IN (" . join(" UNION ", @sub_filters) . ")";
85   }
86
87   if ($params{active} eq "active") {
88     push @filters, 'active';
89
90   } elsif ($params{active} eq "inactive") {
91     push @filters, 'NOT COALESCE(active, FALSE)';
92   }
93
94   my $where = 'WHERE ' . join(' AND ', map { "($_)" } @filters) if (scalar @filters);
95
96   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
97   $sortorder    =~ s/[^a-z_]//g;
98   my $query     = qq|SELECT id, projectnumber, description, active
99                      FROM project
100                      $where
101                      ORDER BY $sortorder|;
102
103   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
104
105   $main::lxdebug->leave_sub();
106
107   return scalar(@{ $form->{project_list} });
108 }
109
110 sub get_project {
111   $main::lxdebug->enter_sub();
112
113   my $self     = shift;
114   my %params   = @_;
115
116   if (!$params{id}) {
117     $main::lxdebug->leave_sub();
118     return { };
119   }
120
121   my $myconfig = \%main::myconfig;
122   my $form     = $main::form;
123
124   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
125
126   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
127
128   if ($params{orphaned}) {
129     # check if it is orphaned
130     my (@values, $query);
131
132     foreach my $table (@tables_with_project_id_cols) {
133       $query .= " + " if ($query);
134       $query .= qq|(SELECT COUNT(*) FROM $table
135                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
136       push @values, conv_i($params{id});
137     }
138
139     $query = 'SELECT ' . $query;
140
141     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
142     $project->{orphaned}   = !$project->{orphaned};
143   }
144
145   $main::lxdebug->leave_sub();
146
147   return $project;
148 }
149
150 sub save_project {
151   $main::lxdebug->enter_sub();
152
153   my $self     = shift;
154   my %params   = @_;
155
156   my $myconfig = \%main::myconfig;
157   my $form     = $main::form;
158
159   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
160
161   my @values;
162
163   if (!$params{id}) {
164     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
165     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
166
167     $params{active} = 1;
168   }
169
170   $query  = qq|UPDATE project SET projectnumber = ?, description = ?, active = ?
171                WHERE id = ?|;
172
173   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{id}));
174   do_query($form, $dbh, $query, @values);
175
176   $dbh->commit();
177
178   $main::lxdebug->leave_sub();
179
180   return $params{id};
181 }
182
183 sub delete_project {
184   $main::lxdebug->enter_sub();
185
186   my $self     = shift;
187   my %params   = @_;
188
189   Common::check_params(\%params, qw(id));
190
191   my $myconfig = \%main::myconfig;
192   my $form     = $main::form;
193
194   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
195
196   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
197
198   $dbh->commit();
199
200   $main::lxdebug->leave_sub();
201 }
202
203 1;
204