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