Bei Projekten ein Flag "aktiv" hinzugefĆ¼gt.
[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 use Data::Dumper;
39
40 use SL::DBUtils;
41
42 sub projects {
43   $main::lxdebug->enter_sub();
44
45   my ($self, $myconfig, $form) = @_;
46
47   # connect to database
48   my $dbh = $form->dbconnect($myconfig);
49
50   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
51
52   my $query = qq|SELECT p.id, p.projectnumber, p.description, p.active
53                  FROM project p
54                  WHERE 1 = 1|;
55
56   if ($form->{projectnumber}) {
57     my $projectnumber = $form->like(lc $form->{projectnumber});
58     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
59   }
60   if ($form->{projectdescription}) {
61     my $description = $form->like(lc $form->{projectdescription});
62     $query .= " AND lower(description) LIKE '$description'";
63   }
64   if ($form->{status} eq 'orphaned') {
65     $query .= " AND id NOT IN (SELECT p.id
66                                FROM project p, acc_trans a
67                                WHERE p.id = a.project_id)
68                 AND id NOT IN (SELECT p.id
69                                FROM project p, invoice i
70                                WHERE p.id = i.project_id)
71                 AND id NOT IN (SELECT p.id
72                                FROM project p, orderitems o
73                                WHERE p.id = o.project_id)";
74   }
75   if ($form->{active} eq "active") {
76     $query .= " AND p.active";
77   } elsif ($form->{active} eq "inactive") {
78     $query .= " AND NOT p.active";
79   }
80
81   $query .= qq|
82                  ORDER BY $sortorder|;
83
84   $sth = $dbh->prepare($query);
85   $sth->execute || $form->dberror($query);
86
87   my $i = 0;
88   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
89     push @{ $form->{project_list} }, $ref;
90     $i++;
91   }
92
93   $sth->finish;
94   $dbh->disconnect;
95
96   $main::lxdebug->leave_sub();
97
98   return $i;
99 }
100
101 sub get_project {
102   $main::lxdebug->enter_sub();
103
104   my ($self, $myconfig, $form) = @_;
105
106   # connect to database
107   my $dbh = $form->dbconnect($myconfig);
108
109   my $query = qq|SELECT p.*
110                  FROM project p
111                  WHERE p.id = $form->{id}|;
112   my $sth = $dbh->prepare($query);
113   $sth->execute || $form->dberror($query);
114
115   my $ref = $sth->fetchrow_hashref(NAME_lc);
116
117   map { $form->{$_} = $ref->{$_} } keys %$ref;
118
119   $sth->finish;
120
121   # check if it is orphaned
122   $query = qq|SELECT count(*)
123               FROM acc_trans a
124               WHERE a.project_id = $form->{id}|;
125   $sth = $dbh->prepare($query);
126   $sth->execute || $form->dberror($query);
127
128   ($form->{orphaned}) = $sth->fetchrow_array;
129   $form->{orphaned} = !$form->{orphaned};
130
131   $sth->finish;
132
133   $dbh->disconnect;
134
135   $main::lxdebug->leave_sub();
136 }
137
138 sub save_project {
139   $main::lxdebug->enter_sub();
140
141   my ($self, $myconfig, $form) = @_;
142
143   # connect to database
144   my $dbh = $form->dbconnect($myconfig);
145
146   my @values = ($form->{projectnumber}, $form->{description});
147
148   if ($form->{id}) {
149     $query =
150       qq|UPDATE project SET projectnumber = ?, description = ?, active = ? | .
151       qq|WHERE id = ?|;
152     push(@values, $form->{active} ? 't' : 'f', $form->{id});
153   } else {
154     $query =
155       qq|INSERT INTO project (projectnumber, description, active) | .
156       qq|VALUES (?, ?, 't')|;
157   }
158   do_query($form, $dbh, $query, @values);
159
160   $dbh->disconnect;
161
162   $main::lxdebug->leave_sub();
163 }
164
165 sub partsgroups {
166   $main::lxdebug->enter_sub();
167
168   my ($self, $myconfig, $form) = @_;
169
170   my $var;
171
172   # connect to database
173   my $dbh = $form->dbconnect($myconfig);
174
175   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
176
177   my $query = qq|SELECT g.*
178                  FROM partsgroup g|;
179
180   my $where = "1 = 1";
181
182   if ($form->{partsgroup}) {
183     $var = $form->like(lc $form->{partsgroup});
184     $where .= " AND lower(g.partsgroup) LIKE '$var'";
185   }
186   $query .= qq|
187                WHERE $where
188                ORDER BY $sortorder|;
189
190   if ($form->{status} eq 'orphaned') {
191     $query = qq|SELECT g.*
192                 FROM partsgroup g
193                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
194                 WHERE $where
195                 EXCEPT
196                 SELECT g.*
197                 FROM partsgroup g
198                 JOIN parts p ON (p.partsgroup_id = g.id)
199                 WHERE $where
200                 ORDER BY $sortorder|;
201   }
202
203   $sth = $dbh->prepare($query);
204   $sth->execute || $form->dberror($query);
205
206   my $i = 0;
207   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
208     push @{ $form->{item_list} }, $ref;
209     $i++;
210   }
211
212   $sth->finish;
213   $dbh->disconnect;
214
215   $main::lxdebug->leave_sub();
216
217   return $i;
218 }
219
220 sub save_partsgroup {
221   $main::lxdebug->enter_sub();
222
223   my ($self, $myconfig, $form) = @_;
224
225   # connect to database
226   my $dbh = $form->dbconnect($myconfig);
227
228   map { $form->{$_} =~ s/\'/\'\'/g } qw(partsgroup);
229   $form->{discount} /= 100;
230
231   if ($form->{id}) {
232     $query = qq|UPDATE partsgroup SET
233                 partsgroup = '$form->{partsgroup}'
234                 WHERE id = $form->{id}|;
235   } else {
236     $query = qq|INSERT INTO partsgroup
237                 (partsgroup)
238                 VALUES ('$form->{partsgroup}')|;
239   }
240   $dbh->do($query) || $form->dberror($query);
241
242   $dbh->disconnect;
243
244   $main::lxdebug->leave_sub();
245 }
246
247 sub get_partsgroup {
248   $main::lxdebug->enter_sub();
249
250   my ($self, $myconfig, $form) = @_;
251
252   # connect to database
253   my $dbh = $form->dbconnect($myconfig);
254
255   my $query = qq|SELECT p.*
256                  FROM partsgroup p
257                  WHERE p.id = $form->{id}|;
258   my $sth = $dbh->prepare($query);
259   $sth->execute || $form->dberror($query);
260
261   my $ref = $sth->fetchrow_hashref(NAME_lc);
262
263   map { $form->{$_} = $ref->{$_} } keys %$ref;
264
265   $sth->finish;
266
267   # check if it is orphaned
268   $query = qq|SELECT count(*)
269               FROM parts p
270               WHERE p.partsgroup_id = $form->{id}|;
271   $sth = $dbh->prepare($query);
272   $sth->execute || $form->dberror($query);
273
274   ($form->{orphaned}) = $sth->fetchrow_array;
275   $form->{orphaned} = !$form->{orphaned};
276
277   $sth->finish;
278
279   $dbh->disconnect;
280
281   $main::lxdebug->leave_sub();
282 }
283
284 sub delete_tuple {
285   $main::lxdebug->enter_sub();
286
287   my ($self, $myconfig, $form) = @_;
288
289   # connect to database
290   my $dbh = $form->dbconnect($myconfig);
291
292   $query = qq|DELETE FROM $form->{type}
293               WHERE id = $form->{id}|;
294   $dbh->do($query) || $form->dberror($query);
295
296   $dbh->disconnect;
297
298   $main::lxdebug->leave_sub();
299 }
300
301 ##########################
302 # get pricegroups from database
303 #
304 sub pricegroups {
305   $main::lxdebug->enter_sub();
306
307   my ($self, $myconfig, $form) = @_;
308
309   my $var;
310
311   # connect to database
312   my $dbh = $form->dbconnect($myconfig);
313
314   my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
315
316   my $query = qq|SELECT g.id, g.pricegroup
317                  FROM pricegroup g|;
318
319   my $where = "1 = 1";
320
321   if ($form->{pricegroup}) {
322     $var = $form->like(lc $form->{pricegroup});
323     $where .= " AND lower(g.pricegroup) LIKE '$var'";
324   }
325   $query .= qq|
326                WHERE $where
327                ORDER BY $sortorder|;
328
329   if ($form->{status} eq 'orphaned') {
330     $query = qq|SELECT pg.*
331                 FROM pricegroup pg
332                 LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
333                 WHERE $where
334                 EXCEPT
335                 SELECT pg.*
336                 FROM pricegroup pg
337                 JOIN prices p ON (p.pricegroup_id = pg.id)
338                 WHERE $where
339                 ORDER BY $sortorder|;
340   }
341
342   $sth = $dbh->prepare($query);
343   $sth->execute || $form->dberror($query);
344
345   my $i = 0;
346   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
347     push @{ $form->{item_list} }, $ref;
348     $i++;
349   }
350
351   $sth->finish;
352   $dbh->disconnect;
353
354   $main::lxdebug->leave_sub();
355
356   return $i;
357 }
358 ########################
359 # save pricegruop to database
360 #
361 sub save_pricegroup {
362   $main::lxdebug->enter_sub();
363
364   my ($self, $myconfig, $form) = @_;
365
366   # connect to database
367   my $dbh = $form->dbconnect($myconfig);
368
369   map { $form->{$_} =~ s/\'/\'\'/g } qw(pricegroup);
370
371   $form->{discount} /= 100;
372
373   if ($form->{id}) {
374     $query = qq|UPDATE pricegroup SET
375                 pricegroup = '$form->{pricegroup}'
376                 WHERE id = $form->{id}|;
377   } else {
378     $query = qq|INSERT INTO pricegroup
379                 (pricegroup)
380                 VALUES ('$form->{pricegroup}')|;
381   }
382   $dbh->do($query) || $form->dberror($query);
383
384   $dbh->disconnect;
385
386   $main::lxdebug->leave_sub();
387 }
388 ############################
389 # get one pricegroup from database
390 #
391 sub get_pricegroup {
392   $main::lxdebug->enter_sub();
393
394   my ($self, $myconfig, $form) = @_;
395
396   # connect to database
397   my $dbh = $form->dbconnect($myconfig);
398
399   my $query = qq|SELECT p.id, p.pricegroup
400                  FROM pricegroup p
401                  WHERE p.id = $form->{id}|;
402   my $sth = $dbh->prepare($query);
403   $sth->execute || $form->dberror($query);
404
405   my $ref = $sth->fetchrow_hashref(NAME_lc);
406
407   map { $form->{$_} = $ref->{$_} } keys %$ref;
408
409   $sth->finish;
410
411   # check if it is orphaned
412   $query = qq|SELECT count(*)
413               FROM prices p
414               WHERE p.pricegroup_id = $form->{id}|;
415   $sth = $dbh->prepare($query);
416   $sth->execute || $form->dberror($query);
417
418   ($form->{orphaned}) = $sth->fetchrow_array;
419   $form->{orphaned} = !$form->{orphaned};
420
421   $sth->finish;
422
423   $dbh->disconnect;
424
425   $main::lxdebug->leave_sub();
426 }
427
428 1;
429