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 } qw(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 } qw(partsgroup);
222 $form->{discount} /= 100;
225 $query = qq|UPDATE partsgroup SET
226 partsgroup = '$form->{partsgroup}'
227 WHERE id = $form->{id}|;
229 $query = qq|INSERT INTO partsgroup
231 VALUES ('$form->{partsgroup}')|;
233 $dbh->do($query) || $form->dberror($query);
237 $main::lxdebug->leave_sub();
241 $main::lxdebug->enter_sub();
243 my ($self, $myconfig, $form) = @_;
245 # connect to database
246 my $dbh = $form->dbconnect($myconfig);
248 my $query = qq|SELECT p.*
250 WHERE p.id = $form->{id}|;
251 my $sth = $dbh->prepare($query);
252 $sth->execute || $form->dberror($query);
254 my $ref = $sth->fetchrow_hashref(NAME_lc);
256 map { $form->{$_} = $ref->{$_} } keys %$ref;
260 # check if it is orphaned
261 $query = qq|SELECT count(*)
263 WHERE p.partsgroup_id = $form->{id}|;
264 $sth = $dbh->prepare($query);
265 $sth->execute || $form->dberror($query);
267 ($form->{orphaned}) = $sth->fetchrow_array;
268 $form->{orphaned} = !$form->{orphaned};
274 $main::lxdebug->leave_sub();
278 $main::lxdebug->enter_sub();
280 my ($self, $myconfig, $form) = @_;
282 # connect to database
283 my $dbh = $form->dbconnect($myconfig);
285 $query = qq|DELETE FROM $form->{type}
286 WHERE id = $form->{id}|;
287 $dbh->do($query) || $form->dberror($query);
291 $main::lxdebug->leave_sub();
294 ##########################
295 # get pricegroups from database
298 $main::lxdebug->enter_sub();
300 my ($self, $myconfig, $form) = @_;
304 # connect to database
305 my $dbh = $form->dbconnect($myconfig);
307 my $sortorder = ($form->{sort}) ? $form->{sort} : "pricegroup";
309 my $query = qq|SELECT g.id, g.pricegroup
314 if ($form->{pricegroup}) {
315 $var = $form->like(lc $form->{pricegroup});
316 $where .= " AND lower(g.pricegroup) LIKE '$var'";
320 ORDER BY $sortorder|;
322 if ($form->{status} eq 'orphaned') {
323 $query = qq|SELECT pg.*
325 LEFT JOIN prices p ON (p.pricegroup_id = pg.id)
330 JOIN prices p ON (p.pricegroup_id = pg.id)
332 ORDER BY $sortorder|;
335 $sth = $dbh->prepare($query);
336 $sth->execute || $form->dberror($query);
339 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
340 push @{ $form->{item_list} }, $ref;
347 $main::lxdebug->leave_sub();
351 ########################
352 # save pricegruop to database
354 sub save_pricegroup {
355 $main::lxdebug->enter_sub();
357 my ($self, $myconfig, $form) = @_;
359 # connect to database
360 my $dbh = $form->dbconnect($myconfig);
362 map { $form->{$_} =~ s/\'/\'\'/g } qw(pricegroup);
364 $form->{discount} /= 100;
367 $query = qq|UPDATE pricegroup SET
368 pricegroup = '$form->{pricegroup}'
369 WHERE id = $form->{id}|;
371 $query = qq|INSERT INTO pricegroup
373 VALUES ('$form->{pricegroup}')|;
375 $dbh->do($query) || $form->dberror($query);
379 $main::lxdebug->leave_sub();
381 ############################
382 # get one pricegroup from database
385 $main::lxdebug->enter_sub();
387 my ($self, $myconfig, $form) = @_;
389 # connect to database
390 my $dbh = $form->dbconnect($myconfig);
392 my $query = qq|SELECT p.id, p.pricegroup
394 WHERE p.id = $form->{id}|;
395 my $sth = $dbh->prepare($query);
396 $sth->execute || $form->dberror($query);
398 my $ref = $sth->fetchrow_hashref(NAME_lc);
400 map { $form->{$_} = $ref->{$_} } keys %$ref;
404 # check if it is orphaned
405 $query = qq|SELECT count(*)
407 WHERE p.pricegroup_id = $form->{id}|;
408 $sth = $dbh->prepare($query);
409 $sth->execute || $form->dberror($query);
411 ($form->{orphaned}) = $sth->fetchrow_array;
412 $form->{orphaned} = !$form->{orphaned};
418 $main::lxdebug->leave_sub();