Quoting Barewords.
[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 } qw(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 } qw(partsgroup);
222   $form->{discount} /= 100;
223
224   if ($form->{id}) {
225     $query = qq|UPDATE partsgroup SET
226                 partsgroup = '$form->{partsgroup}'
227                 WHERE id = $form->{id}|;
228   } else {
229     $query = qq|INSERT INTO partsgroup
230                 (partsgroup)
231                 VALUES ('$form->{partsgroup}')|;
232   }
233   $dbh->do($query) || $form->dberror($query);
234
235   $dbh->disconnect;
236
237   $main::lxdebug->leave_sub();
238 }
239
240 sub get_partsgroup {
241   $main::lxdebug->enter_sub();
242
243   my ($self, $myconfig, $form) = @_;
244
245   # connect to database
246   my $dbh = $form->dbconnect($myconfig);
247
248   my $query = qq|SELECT p.*
249                  FROM partsgroup p
250                  WHERE p.id = $form->{id}|;
251   my $sth = $dbh->prepare($query);
252   $sth->execute || $form->dberror($query);
253
254   my $ref = $sth->fetchrow_hashref(NAME_lc);
255
256   map { $form->{$_} = $ref->{$_} } keys %$ref;
257
258   $sth->finish;
259
260   # check if it is orphaned
261   $query = qq|SELECT count(*)
262               FROM parts p
263               WHERE p.partsgroup_id = $form->{id}|;
264   $sth = $dbh->prepare($query);
265   $sth->execute || $form->dberror($query);
266
267   ($form->{orphaned}) = $sth->fetchrow_array;
268   $form->{orphaned} = !$form->{orphaned};
269
270   $sth->finish;
271
272   $dbh->disconnect;
273
274   $main::lxdebug->leave_sub();
275 }
276
277 sub delete_tuple {
278   $main::lxdebug->enter_sub();
279
280   my ($self, $myconfig, $form) = @_;
281
282   # connect to database
283   my $dbh = $form->dbconnect($myconfig);
284
285   $query = qq|DELETE FROM $form->{type}
286               WHERE id = $form->{id}|;
287   $dbh->do($query) || $form->dberror($query);
288
289   $dbh->disconnect;
290
291   $main::lxdebug->leave_sub();
292 }
293
294 ##########################
295 # get pricegroups from database
296 #
297 sub pricegroups {
298   $main::lxdebug->enter_sub();
299
300   my ($self, $myconfig, $form) = @_;
301
302   my $var;
303
304   # connect to database
305   my $dbh = $form->dbconnect($myconfig);
306
307   my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
308
309   my $query = qq|SELECT g.id, g.pricegroup
310                  FROM pricegroup g|;
311
312   my $where = "1 = 1";
313
314   if ($form->{pricegroup}) {
315     $var = $form->like(lc $form->{pricegroup});
316     $where .= " AND lower(g.pricegroup) LIKE '$var'";
317   }
318   $query .= qq|
319                WHERE $where
320                ORDER BY $sortorder|;
321
322   if ($form->{status} eq 'orphaned') {
323     $query = qq|SELECT pg.*
324                 FROM pricegroup pg
325                 LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
326                 WHERE $where
327                 EXCEPT
328                 SELECT pg.*
329                 FROM pricegroup pg
330                 JOIN prices p ON (p.pricegroup_id = pg.id)
331                 WHERE $where
332                 ORDER BY $sortorder|;
333   }
334
335   $sth = $dbh->prepare($query);
336   $sth->execute || $form->dberror($query);
337
338   my $i = 0;
339   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
340     push @{ $form->{item_list} }, $ref;
341     $i++;
342   }
343
344   $sth->finish;
345   $dbh->disconnect;
346
347   $main::lxdebug->leave_sub();
348
349   return $i;
350 }
351 ########################
352 # save pricegruop to database
353 #
354 sub save_pricegroup {
355   $main::lxdebug->enter_sub();
356
357   my ($self, $myconfig, $form) = @_;
358
359   # connect to database
360   my $dbh = $form->dbconnect($myconfig);
361
362   map { $form->{$_} =~ s/\'/\'\'/g } qw(pricegroup);
363
364   $form->{discount} /= 100;
365
366   if ($form->{id}) {
367     $query = qq|UPDATE pricegroup SET
368                 pricegroup = '$form->{pricegroup}'
369                 WHERE id = $form->{id}|;
370   } else {
371     $query = qq|INSERT INTO pricegroup
372                 (pricegroup)
373                 VALUES ('$form->{pricegroup}')|;
374   }
375   $dbh->do($query) || $form->dberror($query);
376
377   $dbh->disconnect;
378
379   $main::lxdebug->leave_sub();
380 }
381 ############################
382 # get one pricegroup from database
383 #
384 sub get_pricegroup {
385   $main::lxdebug->enter_sub();
386
387   my ($self, $myconfig, $form) = @_;
388
389   # connect to database
390   my $dbh = $form->dbconnect($myconfig);
391
392   my $query = qq|SELECT p.id, p.pricegroup
393                  FROM pricegroup p
394                  WHERE p.id = $form->{id}|;
395   my $sth = $dbh->prepare($query);
396   $sth->execute || $form->dberror($query);
397
398   my $ref = $sth->fetchrow_hashref(NAME_lc);
399
400   map { $form->{$_} = $ref->{$_} } keys %$ref;
401
402   $sth->finish;
403
404   # check if it is orphaned
405   $query = qq|SELECT count(*)
406               FROM prices p
407               WHERE p.pricegroup_id = $form->{id}|;
408   $sth = $dbh->prepare($query);
409   $sth->execute || $form->dberror($query);
410
411   ($form->{orphaned}) = $sth->fetchrow_array;
412   $form->{orphaned} = !$form->{orphaned};
413
414   $sth->finish;
415
416   $dbh->disconnect;
417
418   $main::lxdebug->leave_sub();
419 }
420
421 1;
422