1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
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.
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 #======================================================================
32 # also used for partsgroups
34 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
46 my $dbh = $form->dbconnect($myconfig);
48 my $sortorder = ($form->{sort}) ? $form->{sort} : "projectnumber";
50 my $query = qq|SELECT p.id, p.projectnumber, p.description
54 if ($form->{projectnumber}) {
55 my $projectnumber = $form->like(lc $form->{projectnumber});
56 $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
58 if ($form->{projectdescription}) {
59 my $description = $form->like(lc $form->{projectdescription});
60 $query .= " AND lower(description) LIKE '$description'";
62 if ($form->{status} eq 'orphaned') {
63 $query .= " AND id NOT IN (SELECT p.id
64 FROM project p, acc_trans a
65 WHERE p.id = a.project_id)
66 AND id NOT IN (SELECT p.id
67 FROM project p, invoice i
68 WHERE p.id = i.project_id)
69 AND id NOT IN (SELECT p.id
70 FROM project p, orderitems o
71 WHERE p.id = o.project_id)";
77 $sth = $dbh->prepare($query);
78 $sth->execute || $form->dberror($query);
81 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
82 push @{ $form->{project_list} }, $ref;
89 $main::lxdebug->leave_sub();
95 $main::lxdebug->enter_sub();
97 my ($self, $myconfig, $form) = @_;
100 my $dbh = $form->dbconnect($myconfig);
102 my $query = qq|SELECT p.*
104 WHERE p.id = $form->{id}|;
105 my $sth = $dbh->prepare($query);
106 $sth->execute || $form->dberror($query);
108 my $ref = $sth->fetchrow_hashref(NAME_lc);
110 map { $form->{$_} = $ref->{$_} } keys %$ref;
114 # check if it is orphaned
115 $query = qq|SELECT count(*)
117 WHERE a.project_id = $form->{id}|;
118 $sth = $dbh->prepare($query);
119 $sth->execute || $form->dberror($query);
121 ($form->{orphaned}) = $sth->fetchrow_array;
122 $form->{orphaned} = !$form->{orphaned};
128 $main::lxdebug->leave_sub();
132 $main::lxdebug->enter_sub();
134 my ($self, $myconfig, $form) = @_;
136 # connect to database
137 my $dbh = $form->dbconnect($myconfig);
139 map { $form->{$_} =~ s/\'/\'\'/g } (projectnumber, description);
142 $query = qq|UPDATE project SET
143 projectnumber = '$form->{projectnumber}',
144 description = '$form->{description}'
145 WHERE id = $form->{id}|;
147 $query = qq|INSERT INTO project
148 (projectnumber, description)
149 VALUES ('$form->{projectnumber}', '$form->{description}')|;
151 $dbh->do($query) || $form->dberror($query);
155 $main::lxdebug->leave_sub();
159 $main::lxdebug->enter_sub();
161 my ($self, $myconfig, $form) = @_;
165 # connect to database
166 my $dbh = $form->dbconnect($myconfig);
168 my $sortorder = ($form->{sort}) ? $form->{sort} : "partsgroup";
170 my $query = qq|SELECT g.*
175 if ($form->{partsgroup}) {
176 $var = $form->like(lc $form->{partsgroup});
177 $where .= " AND lower(g.partsgroup) LIKE '$var'";
181 ORDER BY $sortorder|;
183 if ($form->{status} eq 'orphaned') {
184 $query = qq|SELECT g.*
186 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
191 JOIN parts p ON (p.partsgroup_id = g.id)
193 ORDER BY $sortorder|;
196 $sth = $dbh->prepare($query);
197 $sth->execute || $form->dberror($query);
200 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
201 push @{ $form->{item_list} }, $ref;
208 $main::lxdebug->leave_sub();
213 sub save_partsgroup {
214 $main::lxdebug->enter_sub();
216 my ($self, $myconfig, $form) = @_;
218 # connect to database
219 my $dbh = $form->dbconnect($myconfig);
221 map { $form->{$_} =~ s/\'/\'\'/g } (partsgroup);
223 $form->{discount} /= 100;
226 $query = qq|UPDATE partsgroup SET
227 partsgroup = '$form->{partsgroup}'
228 WHERE id = $form->{id}|;
230 $query = qq|INSERT INTO partsgroup
232 VALUES ('$form->{partsgroup}')|;
234 $dbh->do($query) || $form->dberror($query);
238 $main::lxdebug->leave_sub();
242 $main::lxdebug->enter_sub();
244 my ($self, $myconfig, $form) = @_;
246 # connect to database
247 my $dbh = $form->dbconnect($myconfig);
249 my $query = qq|SELECT p.*
251 WHERE p.id = $form->{id}|;
252 my $sth = $dbh->prepare($query);
253 $sth->execute || $form->dberror($query);
255 my $ref = $sth->fetchrow_hashref(NAME_lc);
257 map { $form->{$_} = $ref->{$_} } keys %$ref;
261 # check if it is orphaned
262 $query = qq|SELECT count(*)
264 WHERE p.partsgroup_id = $form->{id}|;
265 $sth = $dbh->prepare($query);
266 $sth->execute || $form->dberror($query);
268 ($form->{orphaned}) = $sth->fetchrow_array;
269 $form->{orphaned} = !$form->{orphaned};
275 $main::lxdebug->leave_sub();
279 $main::lxdebug->enter_sub();
281 my ($self, $myconfig, $form) = @_;
283 # connect to database
284 my $dbh = $form->dbconnect($myconfig);
286 $query = qq|DELETE FROM $form->{type}
287 WHERE id = $form->{id}|;
288 $dbh->do($query) || $form->dberror($query);
292 $main::lxdebug->leave_sub();
295 ##########################
296 # get pricegroups from database
299 $main::lxdebug->enter_sub();
301 my ($self, $myconfig, $form) = @_;
305 # connect to database
306 my $dbh = $form->dbconnect($myconfig);
308 my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
310 my $query = qq|SELECT g.id, g.pricegroup
315 if ($form->{pricegroup}) {
316 $var = $form->like(lc $form->{pricegroup});
317 $where .= " AND lower(g.pricegroup) LIKE '$var'";
321 ORDER BY $sortorder|;
323 if ($form->{status} eq 'orphaned') {
324 $query = qq|SELECT pg.*
326 LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
331 JOIN prices p ON (p.pricegroup_id = pg.id)
333 ORDER BY $sortorder|;
336 $sth = $dbh->prepare($query);
337 $sth->execute || $form->dberror($query);
340 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
341 push @{ $form->{item_list} }, $ref;
348 $main::lxdebug->leave_sub();
352 ########################
353 # save pricegruop to database
355 sub save_pricegroup {
356 $main::lxdebug->enter_sub();
358 my ($self, $myconfig, $form) = @_;
360 # connect to database
361 my $dbh = $form->dbconnect($myconfig);
363 map { $form->{$_} =~ s/\'/\'\'/g } (pricegroup);
365 $form->{discount} /= 100;
368 $query = qq|UPDATE pricegroup SET
369 pricegroup = '$form->{pricegroup}'
370 WHERE id = $form->{id}|;
372 $query = qq|INSERT INTO pricegroup
374 VALUES ('$form->{pricegroup}')|;
376 $dbh->do($query) || $form->dberror($query);
380 $main::lxdebug->leave_sub();
382 ############################
383 # get one pricegroup from database
386 $main::lxdebug->enter_sub();
388 my ($self, $myconfig, $form) = @_;
390 # connect to database
391 my $dbh = $form->dbconnect($myconfig);
393 my $query = qq|SELECT p.id, p.pricegroup
395 WHERE p.id = $form->{id}|;
396 my $sth = $dbh->prepare($query);
397 $sth->execute || $form->dberror($query);
399 my $ref = $sth->fetchrow_hashref(NAME_lc);
401 map { $form->{$_} = $ref->{$_} } keys %$ref;
405 # check if it is orphaned
406 $query = qq|SELECT count(*)
408 WHERE p.pricegroup_id = $form->{id}|;
409 $sth = $dbh->prepare($query);
410 $sth->execute || $form->dberror($query);
412 ($form->{orphaned}) = $sth->fetchrow_array;
413 $form->{orphaned} = !$form->{orphaned};
419 $main::lxdebug->leave_sub();