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