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