Projektverwaltung in eine eigene Datei ausgelagert und auf die Verwendung von Templat...
[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 # Partsgroups and pricegroups
32 #
33 #======================================================================
34
35 package PE;
36
37 use Data::Dumper;
38
39 use SL::DBUtils;
40
41 sub partsgroups {
42   $main::lxdebug->enter_sub();
43
44   my ($self, $myconfig, $form) = @_;
45
46   # connect to database
47   my $dbh = $form->dbconnect($myconfig);
48
49   my ($where, @values);
50
51   if ($form->{partsgroup}) {
52     $where .= qq| AND partsgroup ILIKE ?|;
53     push(@values, '%' . $form->{partsgroup} . '%');
54   }
55
56   if ($form->{status} eq 'orphaned') {
57     $where .=
58       qq| AND id NOT IN | .
59       qq|  (SELECT DISTINCT partsgroup_id FROM parts | .
60       qq|   WHERE NOT partsgroup_id ISNULL) |;
61   }
62
63   substr($where, 0, 4) = "WHERE " if ($where);
64
65   my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
66   $sortorder =~ s/[^a-z_]//g;
67
68   my $query =
69     qq|SELECT id, partsgroup FROM partsgroup | .
70     $where .
71     qq|ORDER BY $sortorder|;
72
73   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
74
75   $dbh->disconnect;
76
77   $main::lxdebug->leave_sub();
78
79   return scalar(@{ $form->{item_list} });
80 }
81
82 sub save_partsgroup {
83   $main::lxdebug->enter_sub();
84
85   my ($self, $myconfig, $form) = @_;
86
87   # connect to database
88   my $dbh = $form->dbconnect($myconfig);
89
90   $form->{discount} /= 100;
91
92   my @values = ($form->{partsgroup});
93
94   if ($form->{id}) {
95     $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
96                 push(@values, $form->{id});
97   } else {
98     $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
99   }
100   do_query($form, $dbh, $query, @values);
101
102   $dbh->disconnect;
103
104   $main::lxdebug->leave_sub();
105 }
106
107 sub get_partsgroup {
108   $main::lxdebug->enter_sub();
109
110   my ($self, $myconfig, $form) = @_;
111
112   # connect to database
113   my $dbh = $form->dbconnect($myconfig);
114
115   my $query =
116     qq|SELECT pg.*, | .
117     qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
118     qq|FROM partsgroup pg | .
119     qq|WHERE pg.id = ?|;
120   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
121                                   $form->{id});
122   my $ref = $sth->fetchrow_hashref(NAME_lc);
123
124   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
125   $sth->finish;
126
127   $dbh->disconnect;
128
129   $main::lxdebug->leave_sub();
130 }
131
132 sub delete_tuple {
133   $main::lxdebug->enter_sub();
134
135   my ($self, $myconfig, $form) = @_;
136
137   # connect to database
138   my $dbh = $form->dbconnect($myconfig);
139
140   my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup";
141
142   $query = qq|DELETE FROM $table WHERE id = ?|;
143   do_query($form, $dbh, $query, $form->{id});
144
145   $dbh->disconnect;
146
147   $main::lxdebug->leave_sub();
148 }
149
150 ##########################
151 # get pricegroups from database
152 #
153 sub pricegroups {
154   $main::lxdebug->enter_sub();
155
156   my ($self, $myconfig, $form) = @_;
157
158   # connect to database
159   my $dbh = $form->dbconnect($myconfig);
160
161   my ($where, @values);
162
163   if ($form->{pricegroup}) {
164     $where .= qq| AND pricegroup ILIKE ?|;
165     push(@values, '%' . $form->{pricegroup} . '%');
166   }
167
168   if ($form->{status} eq 'orphaned') {
169     my $first = 1;
170
171     $where .= qq| AND id NOT IN (|;
172     foreach my $table (qw(invoice orderitems prices rmaitems)) {
173       $where .= "UNION " unless ($first);
174       $first = 0;
175       $where .=
176         qq|SELECT DISTINCT pricegroup_id FROM $table | .
177         qq|WHERE NOT pricegroup_id ISNULL |;
178     }
179     $where .= qq|) |;
180   }
181
182   substr($where, 0, 4) = "WHERE " if ($where);
183
184   my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
185   $sortorder =~ s/[^a-z_]//g;
186
187   my $query =
188     qq|SELECT id, pricegroup FROM pricegroup | .
189     $where .
190     qq|ORDER BY $sortorder|;
191
192   $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
193
194   $dbh->disconnect;
195
196   $main::lxdebug->leave_sub();
197
198   return scalar(@{ $form->{item_list} });
199 }
200
201 ########################
202 # save pricegruop to database
203 #
204 sub save_pricegroup {
205   $main::lxdebug->enter_sub();
206
207   my ($self, $myconfig, $form) = @_;
208
209   # connect to database
210   my $dbh = $form->dbconnect($myconfig);
211   my $query;
212
213   $form->{discount} /= 100;
214
215   my @values = ($form->{pricegroup});
216
217   if ($form->{id}) {
218     $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
219                 push(@values, $form->{id});
220   } else {
221     $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
222   }
223   do_query($form, $dbh, $query, @values);
224
225   $dbh->disconnect;
226
227   $main::lxdebug->leave_sub();
228 }
229
230 ############################
231 # get one pricegroup from database
232 #
233 sub get_pricegroup {
234   $main::lxdebug->enter_sub();
235
236   my ($self, $myconfig, $form) = @_;
237
238   # connect to database
239   my $dbh = $form->dbconnect($myconfig);
240
241   my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
242   my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
243   my $ref = $sth->fetchrow_hashref(NAME_lc);
244
245   map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
246
247   $sth->finish;
248
249   my $first = 1;
250
251   my @values = ();
252   $query = qq|SELECT |;
253   foreach my $table (qw(invoice orderitems prices rmaitems)) {
254     $query .= " + " unless ($first);
255     $first = 0;
256     $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
257     push(@values, $form->{id});
258   }
259
260   ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
261   $form->{orphaned} = !$form->{orphaned};
262
263   $dbh->disconnect;
264
265   $main::lxdebug->leave_sub();
266 }
267
268 1;
269