4c145bb57dd5fe138c25ec4882d016bf680aad34
[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   if ($params{valid} eq "valid") {
98     push @filters, 'p.valid';
99
100   } elsif ($params{valid} eq "invalid") {
101     push @filters, 'NOT COALESCE(p.valid, FALSE)';
102   }
103
104   if ($params{customer}) {
105     push @filters, 'c.name ILIKE ?';
106     push @values,  '%' . $params{customer} . '%';
107   }
108
109   if ($params{type}) {
110     push @filters, 'p.type ILIKE ?';
111     push @values,  '%' . $params{type} . '%';
112   }
113
114   my ($cvar_where, @cvar_values) = CVar->build_filter_query('module'         => 'Projects',
115                                                             'trans_id_field' => 'p.id',
116                                                             'filter'         => $form);
117
118   if ($cvar_where) {
119     push @filters, $cvar_where;
120     push @values,  @cvar_values;
121   }
122
123
124   my $where = @filters ? 'WHERE ' . join(' AND ', map { "($_)" } @filters) : '';
125
126   my $sortorder =  $params{sort} ? $params{sort} : "projectnumber";
127   $sortorder    =~ s/[^a-z_]//g;
128   my $query     = qq|SELECT p.id, p.projectnumber, p.description, p.active, p.valid, p.type,
129                        c.name AS customer
130                      FROM project p
131                      LEFT JOIN customer c ON (p.customer_id = c.id)
132                      $where
133                      ORDER BY $sortorder|;
134
135   $form->{project_list} = selectall_hashref_query($form, $dbh, $query, @values);
136
137   $main::lxdebug->leave_sub();
138
139   return scalar(@{ $form->{project_list} });
140 }
141
142 sub get_project {
143   $main::lxdebug->enter_sub();
144
145   my $self     = shift;
146   my %params   = @_;
147
148   if (!$params{id}) {
149     $main::lxdebug->leave_sub();
150     return { };
151   }
152
153   my $myconfig = \%main::myconfig;
154   my $form     = $main::form;
155
156   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
157
158   my $project  = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM project WHERE id = ?|, conv_i($params{id})) || { };
159
160   if ($params{orphaned}) {
161     # check if it is orphaned
162     my (@values, $query);
163
164     foreach my $table (@tables_with_project_id_cols) {
165       $query .= " + " if ($query);
166       $query .= qq|(SELECT COUNT(*) FROM $table
167                     WHERE $project_id_column_prefixes{$table}project_id = ?) |;
168       push @values, conv_i($params{id});
169     }
170
171     $query = 'SELECT ' . $query;
172
173     ($project->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
174     $project->{orphaned}   = !$project->{orphaned};
175   }
176
177   $main::lxdebug->leave_sub();
178
179   return $project;
180 }
181
182 sub save_project {
183   $main::lxdebug->enter_sub();
184
185   my $self     = shift;
186   my %params   = @_;
187
188   my $myconfig = \%main::myconfig;
189   my $form     = $main::form;
190
191   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
192
193   my @values;
194
195   if (!$params{id}) {
196     ($params{id}) = selectfirst_array_query($form, $dbh, qq|SELECT nextval('id')|);
197     do_query($form, $dbh, qq|INSERT INTO project (id) VALUES (?)|, conv_i($params{id}));
198
199     $params{active} = 1;
200   }
201
202   my $query  = <<SQL;
203     UPDATE project
204     SET projectnumber = ?, description = ?, active = ?, customer_id = ?, type = ?, valid = ?
205     WHERE id = ?
206 SQL
207
208   @values = ($params{projectnumber}, $params{description}, $params{active} ? 't' : 'f', conv_i($params{customer_id}), $params{type}, $params{valid} ? 't' : 'f', conv_i($params{id}));
209   do_query($form, $dbh, $query, @values);
210
211   CVar->save_custom_variables('dbh'       => $dbh,
212                               'module'    => 'Projects',
213                               'trans_id'  => $params{id},
214                               'variables' => $form,
215                               'always_valid' => 1);
216
217   $dbh->commit();
218
219   $main::lxdebug->leave_sub();
220
221   return $params{id};
222 }
223
224 sub delete_project {
225   $main::lxdebug->enter_sub();
226
227   my $self     = shift;
228   my %params   = @_;
229
230   Common::check_params(\%params, qw(id));
231
232   my $myconfig = \%main::myconfig;
233   my $form     = $main::form;
234
235   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
236
237   do_query($form, $dbh, qq|DELETE FROM project WHERE id = ?|, conv_i($params{id}));
238
239   $dbh->commit();
240
241   $main::lxdebug->leave_sub();
242 }
243
244 1;