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., 51 Franklin Street, Fifth Floor, Boston,
30 #======================================================================
32 # Partsgroups and pricegroups
34 #======================================================================
46 $main::lxdebug->enter_sub();
48 my ($self, $myconfig, $form) = @_;
51 my $dbh = SL::DB->client->dbh;
55 if ($form->{partsgroup}) {
56 $where .= qq| AND partsgroup ILIKE ?|;
57 push(@values, like($form->{partsgroup}));
60 if ($form->{status} eq 'orphaned') {
63 qq| (SELECT DISTINCT partsgroup_id FROM parts | .
64 qq| WHERE NOT partsgroup_id ISNULL | .
66 qq| SELECT DISTINCT partsgroup_id FROM custom_variable_config_partsgroups | .
67 qq| WHERE NOT partsgroup_id ISNULL) |;
70 substr($where, 0, 4) = "WHERE " if ($where);
72 my $sortorder = $form->{sort} ? $form->{sort} : "partsgroup";
73 $sortorder =~ s/[^a-z_]//g;
76 qq|SELECT id, partsgroup FROM partsgroup | .
78 qq|ORDER BY $sortorder|;
80 $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
82 $main::lxdebug->leave_sub();
84 return scalar(@{ $form->{item_list} });
88 $main::lxdebug->enter_sub();
90 my ($self, $myconfig, $form) = @_;
93 my $dbh = SL::DB->client->dbh;
95 $form->{discount} /= 100;
97 my @values = ($form->{partsgroup});
101 $query = qq|UPDATE partsgroup SET partsgroup = ? WHERE id = ?|;
102 push(@values, $form->{id});
104 $query = qq|INSERT INTO partsgroup (partsgroup) VALUES (?)|;
106 do_query($form, $dbh, $query, @values);
108 $main::lxdebug->leave_sub();
112 $main::lxdebug->enter_sub();
114 my ($self, $myconfig, $form) = @_;
116 # connect to database
117 my $dbh = SL::DB->client->dbh;
121 qq|(SELECT COUNT(*) FROM parts WHERE partsgroup_id = ?) = 0 AS orphaned | .
122 qq|FROM partsgroup pg | .
124 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id},
126 my $ref = $sth->fetchrow_hashref("NAME_lc");
128 map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
131 # also not orphaned if partsgroup is selected for a cvar filter
132 if ($form->{orphaned}) {
133 my $cvar_count = scalar( @{ SL::DB::PartsGroup->new(id => $form->{id})->custom_variable_configs } );
134 $form->{orphaned} = !$cvar_count;
137 $main::lxdebug->leave_sub();
141 $main::lxdebug->enter_sub();
143 my ($self, $myconfig, $form) = @_;
145 # connect to database
146 SL::DB->client->with_transaction(sub {
147 my $dbh = SL::DB->client->dbh;
149 my $table = $form->{type} eq "pricegroup" ? "pricegroup" : "partsgroup";
151 my $query = qq|DELETE FROM $table WHERE id = ?|;
152 do_query($form, $dbh, $query, $form->{id});
154 }) or do { die SL::DB->client->error };
156 $main::lxdebug->leave_sub();
159 ##########################
160 # get pricegroups from database
163 $main::lxdebug->enter_sub();
165 my ($self, $myconfig, $form) = @_;
167 # connect to database
168 my $dbh = SL::DB->client->dbh;
170 my ($where, @values);
172 if ($form->{pricegroup}) {
173 $where .= qq| AND pricegroup ILIKE ?|;
174 push(@values, like($form->{pricegroup}));
177 if ($form->{status} eq 'orphaned') {
180 $where .= qq| AND id NOT IN (|;
181 foreach my $table (qw(invoice orderitems prices)) {
182 $where .= "UNION " unless ($first);
185 qq|SELECT DISTINCT pricegroup_id FROM $table | .
186 qq|WHERE NOT pricegroup_id ISNULL |;
191 substr($where, 0, 4) = "WHERE " if ($where);
193 my $sortorder = $form->{sort} ? $form->{sort} : "pricegroup";
194 $sortorder =~ s/[^a-z_]//g;
197 qq|SELECT id, pricegroup FROM pricegroup | .
199 qq|ORDER BY $sortorder|;
201 $form->{item_list} = selectall_hashref_query($form, $dbh, $query, @values);
203 $main::lxdebug->leave_sub();
205 return scalar(@{ $form->{item_list} });
208 ########################
209 # save pricegruop to database
211 sub save_pricegroup {
212 $main::lxdebug->enter_sub();
214 my ($self, $myconfig, $form) = @_;
216 SL::DB->client->with_transaction(sub {
217 my $dbh = SL::DB->client->dbh;
220 $form->{discount} /= 100;
222 my @values = ($form->{pricegroup});
225 $query = qq|UPDATE pricegroup SET pricegroup = ? WHERE id = ? |;
226 push(@values, $form->{id});
228 $query = qq|INSERT INTO pricegroup (pricegroup) VALUES (?)|;
230 do_query($form, $dbh, $query, @values);
232 }) or do { die SL::DB->client->error };
234 $main::lxdebug->leave_sub();
237 ############################
238 # get one pricegroup from database
241 $main::lxdebug->enter_sub();
243 my ($self, $myconfig, $form) = @_;
245 # connect to database
246 my $dbh = SL::DB->client->dbh;
248 my $query = qq|SELECT id, pricegroup FROM pricegroup WHERE id = ?|;
249 my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
250 my $ref = $sth->fetchrow_hashref("NAME_lc");
252 map({ $form->{$_} = $ref->{$_} } keys(%{$ref}));
259 $query = qq|SELECT |;
260 foreach my $table (qw(invoice orderitems prices)) {
261 $query .= " + " unless ($first);
263 $query .= qq|(SELECT COUNT(*) FROM $table WHERE pricegroup_id = ?) |;
264 push(@values, $form->{id});
267 ($form->{orphaned}) = selectrow_query($form, $dbh, $query, @values);
268 $form->{orphaned} = !$form->{orphaned};
270 $main::lxdebug->leave_sub();