better error message for missing tables
[freeside.git] / sql-ledger / SL / PE.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 1998-2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # Project module
26 # also used for partsgroups
27 #
28 #======================================================================
29
30 package PE;
31
32
33 sub projects {
34   my ($self, $myconfig, $form) = @_;
35   
36   # connect to database
37   my $dbh = $form->dbconnect($myconfig);
38
39   my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
40
41   my $query = qq|SELECT id, projectnumber, description
42                  FROM project
43                  WHERE 1 = 1|;
44
45   if ($form->{projectnumber}) {
46     my $projectnumber = $form->like(lc $form->{projectnumber});
47     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
48   }
49   if ($form->{projectdescription}) {
50     my $description = $form->like(lc $form->{projectdescription});
51     $query .= " AND lower(description) LIKE '$description'";
52   }
53   if ($form->{status} eq 'orphaned') {
54     $query .= " AND id NOT IN (SELECT p.id
55                                FROM project p, acc_trans a
56                                WHERE p.id = a.project_id)
57                 AND id NOT IN (SELECT p.id
58                                FROM project p, invoice i
59                                WHERE p.id = i.project_id)
60                 AND id NOT IN (SELECT p.id
61                                FROM project p, orderitems o
62                                WHERE p.id = o.project_id)";
63   }
64
65   $query .= qq|
66                  ORDER BY $sortorder|;
67
68   $sth = $dbh->prepare($query);
69   $sth->execute || $form->dberror($query);
70
71   my $i = 0;
72   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
73     push @{ $form->{project_list} }, $ref;
74     $i++;
75   }
76
77   $sth->finish;
78   $dbh->disconnect;
79   
80   $i;
81
82 }
83
84
85 sub get_project {
86   my ($self, $myconfig, $form) = @_;
87
88   # connect to database
89   my $dbh = $form->dbconnect($myconfig);
90   
91   my $query = qq|SELECT *
92                  FROM project
93                  WHERE id = $form->{id}|;
94   my $sth = $dbh->prepare($query);
95   $sth->execute || $form->dberror($query);
96
97   my $ref = $sth->fetchrow_hashref(NAME_lc);
98   
99   map { $form->{$_} = $ref->{$_} } keys %$ref;
100
101   $sth->finish;
102
103   # check if it is orphaned
104   $query = qq|SELECT count(*)
105               FROM acc_trans
106               WHERE project_id = $form->{id}|;
107   $sth = $dbh->prepare($query);
108   $sth->execute || $form->dberror($query);
109
110   ($form->{orphaned}) = $sth->fetchrow_array;
111   $form->{orphaned} = !$form->{orphaned};
112        
113   $sth->finish;
114   
115   $dbh->disconnect;
116
117 }
118
119
120 sub save_project {
121   my ($self, $myconfig, $form) = @_;
122   
123   # connect to database
124   my $dbh = $form->dbconnect($myconfig);
125   
126   map { $form->{$_} =~ s/'/''/g } (projectnumber, description);
127
128   if ($form->{id}) {
129     $query = qq|UPDATE project SET
130                 projectnumber = '$form->{projectnumber}',
131                 description = '$form->{description}'
132                 WHERE id = $form->{id}|;
133   } else {
134     $query = qq|INSERT INTO project
135                 (projectnumber, description)
136                 VALUES ('$form->{projectnumber}', '$form->{description}')|;
137   }
138   $dbh->do($query) || $form->dberror($query);
139   
140   $dbh->disconnect;
141
142 }
143
144
145 sub partsgroups {
146   my ($self, $myconfig, $form) = @_;
147   
148   my $var;
149   
150   # connect to database
151   my $dbh = $form->dbconnect($myconfig);
152
153   my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
154
155   my $query = qq|SELECT g.*
156                  FROM partsgroup g|;
157
158   my $where = "1 = 1";
159   
160   if ($form->{partsgroup}) {
161     $var = $form->like(lc $form->{partsgroup});
162     $where .= " AND lower(partsgroup) LIKE '$var'";
163   }
164   $query .= qq|
165                WHERE $where
166                ORDER BY $sortorder|;
167   
168   if ($form->{status} eq 'orphaned') {
169     $query = qq|SELECT g.*
170                 FROM partsgroup g
171                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
172                 WHERE $where
173                 EXCEPT
174                 SELECT g.*
175                 FROM partsgroup g
176                 JOIN parts p ON (p.partsgroup_id = g.id)
177                 WHERE $where
178                 ORDER BY $sortorder|;
179   }
180
181   $sth = $dbh->prepare($query);
182   $sth->execute || $form->dberror($query);
183
184   my $i = 0;
185   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
186     push @{ $form->{item_list} }, $ref;
187     $i++;
188   }
189
190   $sth->finish;
191   $dbh->disconnect;
192   
193   $i;
194
195 }
196
197
198 sub save_partsgroup {
199   my ($self, $myconfig, $form) = @_;
200   
201   # connect to database
202   my $dbh = $form->dbconnect($myconfig);
203   
204   map { $form->{$_} =~ s/'/''/g } (partsgroup);
205
206
207   if ($form->{id}) {
208     $query = qq|UPDATE partsgroup SET
209                 partsgroup = '$form->{partsgroup}'
210                 WHERE id = $form->{id}|;
211   } else {
212     $query = qq|INSERT INTO partsgroup
213                 (partsgroup)
214                 VALUES ('$form->{partsgroup}')|;
215   }
216   $dbh->do($query) || $form->dberror($query);
217   
218   $dbh->disconnect;
219
220 }
221
222
223 sub get_partsgroup {
224   my ($self, $myconfig, $form) = @_;
225
226   # connect to database
227   my $dbh = $form->dbconnect($myconfig);
228   
229   my $query = qq|SELECT *
230                  FROM partsgroup
231                  WHERE id = $form->{id}|;
232   my $sth = $dbh->prepare($query);
233   $sth->execute || $form->dberror($query);
234
235   my $ref = $sth->fetchrow_hashref(NAME_lc);
236  
237   map { $form->{$_} = $ref->{$_} } keys %$ref;
238
239   $sth->finish;
240
241   # check if it is orphaned
242   $query = qq|SELECT count(*)
243               FROM parts
244               WHERE partsgroup_id = $form->{id}|;
245   $sth = $dbh->prepare($query);
246   $sth->execute || $form->dberror($query);
247
248   ($form->{orphaned}) = $sth->fetchrow_array;
249   $form->{orphaned} = !$form->{orphaned};
250        
251   $sth->finish;
252   
253   $dbh->disconnect;
254
255 }
256
257
258
259 sub delete_tuple {
260   my ($self, $myconfig, $form) = @_;
261   
262   # connect to database
263   my $dbh = $form->dbconnect($myconfig);
264   
265   $query = qq|DELETE FROM $form->{type}
266               WHERE id = $form->{id}|;
267   $dbh->do($query) || $form->dberror($query);
268   
269   $dbh->disconnect;
270
271 }
272
273
274
275 1;
276