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