unstable-Zweig als Kopie des "alten" trunks erstellt.
[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
39 sub projects {
40   $main::lxdebug->enter_sub();
41
42   my ($self, $myconfig, $form) = @_;
43   
44   # connect to database
45   my $dbh = $form->dbconnect($myconfig);
46
47   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
48
49   my $query = qq|SELECT p.id, p.projectnumber, p.description
50                  FROM project p
51                  WHERE 1 = 1|;
52
53   if ($form->{projectnumber}) {
54     my $projectnumber = $form->like(lc $form->{projectnumber});
55     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
56   }
57   if ($form->{projectdescription}) {
58     my $description = $form->like(lc $form->{projectdescription});
59     $query .= " AND lower(description) LIKE '$description'";
60   }
61   if ($form->{status} eq 'orphaned') {
62     $query .= " AND id NOT IN (SELECT p.id
63                                FROM project p, acc_trans a
64                                WHERE p.id = a.project_id)
65                 AND id NOT IN (SELECT p.id
66                                FROM project p, invoice i
67                                WHERE p.id = i.project_id)
68                 AND id NOT IN (SELECT p.id
69                                FROM project p, orderitems o
70                                WHERE p.id = o.project_id)";
71   }
72
73   $query .= qq|
74                  ORDER BY $sortorder|;
75
76   $sth = $dbh->prepare($query);
77   $sth->execute || $form->dberror($query);
78
79   my $i = 0;
80   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
81     push @{ $form->{project_list} }, $ref;
82     $i++;
83   }
84
85   $sth->finish;
86   $dbh->disconnect;
87   
88   $main::lxdebug->leave_sub();
89
90   return $i;
91 }
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
132 sub save_project {
133   $main::lxdebug->enter_sub();
134
135   my ($self, $myconfig, $form) = @_;
136   
137   # connect to database
138   my $dbh = $form->dbconnect($myconfig);
139   
140   map { $form->{$_} =~ s/\'/\'\'/g } (projectnumber, description);
141
142   if ($form->{id}) {
143     $query = qq|UPDATE project SET
144                 projectnumber = '$form->{projectnumber}',
145                 description = '$form->{description}'
146                 WHERE id = $form->{id}|;
147   } else {
148     $query = qq|INSERT INTO project
149                 (projectnumber, description)
150                 VALUES ('$form->{projectnumber}', '$form->{description}')|;
151   }
152   $dbh->do($query) || $form->dberror($query);
153   
154   $dbh->disconnect;
155
156   $main::lxdebug->leave_sub();
157 }
158
159
160 sub partsgroups {
161   $main::lxdebug->enter_sub();
162
163   my ($self, $myconfig, $form) = @_;
164   
165   my $var;
166   
167   # connect to database
168   my $dbh = $form->dbconnect($myconfig);
169
170   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
171
172   my $query = qq|SELECT g.*
173                  FROM partsgroup g|;
174
175   my $where = "1 = 1";
176   
177   if ($form->{partsgroup}) {
178     $var = $form->like(lc $form->{partsgroup});
179     $where .= " AND lower(g.partsgroup) LIKE '$var'";
180   }
181   $query .= qq|
182                WHERE $where
183                ORDER BY $sortorder|;
184   
185   if ($form->{status} eq 'orphaned') {
186     $query = qq|SELECT g.*
187                 FROM partsgroup g
188                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
189                 WHERE $where
190                 EXCEPT
191                 SELECT g.*
192                 FROM partsgroup g
193                 JOIN parts p ON (p.partsgroup_id = g.id)
194                 WHERE $where
195                 ORDER BY $sortorder|;
196   }
197
198   $sth = $dbh->prepare($query);
199   $sth->execute || $form->dberror($query);
200
201   my $i = 0;
202   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
203     push @{ $form->{item_list} }, $ref;
204     $i++;
205   }
206
207   $sth->finish;
208   $dbh->disconnect;
209   
210   $main::lxdebug->leave_sub();
211
212   return $i;
213 }
214
215
216 sub save_partsgroup {
217   $main::lxdebug->enter_sub();
218
219   my ($self, $myconfig, $form) = @_;
220   
221   # connect to database
222   my $dbh = $form->dbconnect($myconfig);
223   
224   map { $form->{$_} =~ s/\'/\'\'/g } (partsgroup);
225
226   $form->{discount} /= 100;
227
228   if ($form->{id}) {
229     $query = qq|UPDATE partsgroup SET
230                 partsgroup = '$form->{partsgroup}'
231                 WHERE id = $form->{id}|;
232   } else {
233     $query = qq|INSERT INTO partsgroup
234                 (partsgroup)
235                 VALUES ('$form->{partsgroup}')|;
236   }
237   $dbh->do($query) || $form->dberror($query);
238   
239   $dbh->disconnect;
240
241   $main::lxdebug->leave_sub();
242 }
243
244
245 sub get_partsgroup {
246   $main::lxdebug->enter_sub();
247
248   my ($self, $myconfig, $form) = @_;
249
250   # connect to database
251   my $dbh = $form->dbconnect($myconfig);
252   
253   my $query = qq|SELECT p.*
254                  FROM partsgroup p
255                  WHERE p.id = $form->{id}|;
256   my $sth = $dbh->prepare($query);
257   $sth->execute || $form->dberror($query);
258
259   my $ref = $sth->fetchrow_hashref(NAME_lc);
260  
261   map { $form->{$_} = $ref->{$_} } keys %$ref;
262
263   $sth->finish;
264
265   # check if it is orphaned
266   $query = qq|SELECT count(*)
267               FROM parts p
268               WHERE p.partsgroup_id = $form->{id}|;
269   $sth = $dbh->prepare($query);
270   $sth->execute || $form->dberror($query);
271
272   ($form->{orphaned}) = $sth->fetchrow_array;
273   $form->{orphaned} = !$form->{orphaned};
274        
275   $sth->finish;
276   
277   $dbh->disconnect;
278
279   $main::lxdebug->leave_sub();
280 }
281
282
283
284 sub delete_tuple {
285   $main::lxdebug->enter_sub();
286
287   my ($self, $myconfig, $form) = @_;
288   
289   # connect to database
290   my $dbh = $form->dbconnect($myconfig);
291   
292   $query = qq|DELETE FROM $form->{type}
293               WHERE id = $form->{id}|;
294   $dbh->do($query) || $form->dberror($query);
295   
296   $dbh->disconnect;
297
298   $main::lxdebug->leave_sub();
299 }
300
301
302
303 1;
304