Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[kivitendo-erp.git] / SL / PE.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 # also used for partsgroups
33 #
34 #======================================================================
35
36 package PE;
37
38 sub projects {
39   $main::lxdebug->enter_sub();
40
41   my ($self, $myconfig, $form) = @_;
42
43   # connect to database
44   my $dbh = $form->dbconnect($myconfig);
45
46   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
47
48   my $query = qq|SELECT p.id, p.projectnumber, p.description
49                  FROM project p
50                  WHERE 1 = 1|;
51
52   if ($form->{projectnumber}) {
53     my $projectnumber = $form->like(lc $form->{projectnumber});
54     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
55   }
56   if ($form->{projectdescription}) {
57     my $description = $form->like(lc $form->{projectdescription});
58     $query .= " AND lower(description) LIKE '$description'";
59   }
60   if ($form->{status} eq 'orphaned') {
61     $query .= " AND id NOT IN (SELECT p.id
62                                FROM project p, acc_trans a
63                                WHERE p.id = a.project_id)
64                 AND id NOT IN (SELECT p.id
65                                FROM project p, invoice i
66                                WHERE p.id = i.project_id)
67                 AND id NOT IN (SELECT p.id
68                                FROM project p, orderitems o
69                                WHERE p.id = o.project_id)";
70   }
71
72   $query .= qq|
73                  ORDER BY $sortorder|;
74
75   $sth = $dbh->prepare($query);
76   $sth->execute || $form->dberror($query);
77
78   my $i = 0;
79   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
80     push @{ $form->{project_list} }, $ref;
81     $i++;
82   }
83
84   $sth->finish;
85   $dbh->disconnect;
86
87   $main::lxdebug->leave_sub();
88
89   return $i;
90 }
91
92 sub get_project {
93   $main::lxdebug->enter_sub();
94
95   my ($self, $myconfig, $form) = @_;
96
97   # connect to database
98   my $dbh = $form->dbconnect($myconfig);
99
100   my $query = qq|SELECT p.*
101                  FROM project p
102                  WHERE p.id = $form->{id}|;
103   my $sth = $dbh->prepare($query);
104   $sth->execute || $form->dberror($query);
105
106   my $ref = $sth->fetchrow_hashref(NAME_lc);
107
108   map { $form->{$_} = $ref->{$_} } keys %$ref;
109
110   $sth->finish;
111
112   # check if it is orphaned
113   $query = qq|SELECT count(*)
114               FROM acc_trans a
115               WHERE a.project_id = $form->{id}|;
116   $sth = $dbh->prepare($query);
117   $sth->execute || $form->dberror($query);
118
119   ($form->{orphaned}) = $sth->fetchrow_array;
120   $form->{orphaned} = !$form->{orphaned};
121
122   $sth->finish;
123
124   $dbh->disconnect;
125
126   $main::lxdebug->leave_sub();
127 }
128
129 sub save_project {
130   $main::lxdebug->enter_sub();
131
132   my ($self, $myconfig, $form) = @_;
133
134   # connect to database
135   my $dbh = $form->dbconnect($myconfig);
136
137   map { $form->{$_} =~ s/\'/\'\'/g } (projectnumber, description);
138
139   if ($form->{id}) {
140     $query = qq|UPDATE project SET
141                 projectnumber = '$form->{projectnumber}',
142                 description = '$form->{description}'
143                 WHERE id = $form->{id}|;
144   } else {
145     $query = qq|INSERT INTO project
146                 (projectnumber, description)
147                 VALUES ('$form->{projectnumber}', '$form->{description}')|;
148   }
149   $dbh->do($query) || $form->dberror($query);
150
151   $dbh->disconnect;
152
153   $main::lxdebug->leave_sub();
154 }
155
156 sub partsgroups {
157   $main::lxdebug->enter_sub();
158
159   my ($self, $myconfig, $form) = @_;
160
161   my $var;
162
163   # connect to database
164   my $dbh = $form->dbconnect($myconfig);
165
166   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
167
168   my $query = qq|SELECT g.*
169                  FROM partsgroup g|;
170
171   my $where = "1 = 1";
172
173   if ($form->{partsgroup}) {
174     $var = $form->like(lc $form->{partsgroup});
175     $where .= " AND lower(g.partsgroup) LIKE '$var'";
176   }
177   $query .= qq|
178                WHERE $where
179                ORDER BY $sortorder|;
180
181   if ($form->{status} eq 'orphaned') {
182     $query = qq|SELECT g.*
183                 FROM partsgroup g
184                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
185                 WHERE $where
186                 EXCEPT
187                 SELECT g.*
188                 FROM partsgroup g
189                 JOIN parts p ON (p.partsgroup_id = g.id)
190                 WHERE $where
191                 ORDER BY $sortorder|;
192   }
193
194   $sth = $dbh->prepare($query);
195   $sth->execute || $form->dberror($query);
196
197   my $i = 0;
198   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
199     push @{ $form->{item_list} }, $ref;
200     $i++;
201   }
202
203   $sth->finish;
204   $dbh->disconnect;
205
206   $main::lxdebug->leave_sub();
207
208   return $i;
209 }
210
211 sub save_partsgroup {
212   $main::lxdebug->enter_sub();
213
214   my ($self, $myconfig, $form) = @_;
215
216   # connect to database
217   my $dbh = $form->dbconnect($myconfig);
218
219   map { $form->{$_} =~ s/\'/\'\'/g } (partsgroup);
220
221   $form->{discount} /= 100;
222
223   if ($form->{id}) {
224     $query = qq|UPDATE partsgroup SET
225                 partsgroup = '$form->{partsgroup}'
226                 WHERE id = $form->{id}|;
227   } else {
228     $query = qq|INSERT INTO partsgroup
229                 (partsgroup)
230                 VALUES ('$form->{partsgroup}')|;
231   }
232   $dbh->do($query) || $form->dberror($query);
233
234   $dbh->disconnect;
235
236   $main::lxdebug->leave_sub();
237 }
238
239 sub get_partsgroup {
240   $main::lxdebug->enter_sub();
241
242   my ($self, $myconfig, $form) = @_;
243
244   # connect to database
245   my $dbh = $form->dbconnect($myconfig);
246
247   my $query = qq|SELECT p.*
248                  FROM partsgroup p
249                  WHERE p.id = $form->{id}|;
250   my $sth = $dbh->prepare($query);
251   $sth->execute || $form->dberror($query);
252
253   my $ref = $sth->fetchrow_hashref(NAME_lc);
254
255   map { $form->{$_} = $ref->{$_} } keys %$ref;
256
257   $sth->finish;
258
259   # check if it is orphaned
260   $query = qq|SELECT count(*)
261               FROM parts p
262               WHERE p.partsgroup_id = $form->{id}|;
263   $sth = $dbh->prepare($query);
264   $sth->execute || $form->dberror($query);
265
266   ($form->{orphaned}) = $sth->fetchrow_array;
267   $form->{orphaned} = !$form->{orphaned};
268
269   $sth->finish;
270
271   $dbh->disconnect;
272
273   $main::lxdebug->leave_sub();
274 }
275
276 sub delete_tuple {
277   $main::lxdebug->enter_sub();
278
279   my ($self, $myconfig, $form) = @_;
280
281   # connect to database
282   my $dbh = $form->dbconnect($myconfig);
283
284   $query = qq|DELETE FROM $form->{type}
285               WHERE id = $form->{id}|;
286   $dbh->do($query) || $form->dberror($query);
287
288   $dbh->disconnect;
289
290   $main::lxdebug->leave_sub();
291 }
292
293 1;
294