X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;ds=sidebyside;f=SL%2FBP.pm;h=724ab2767e9e5db7366fde0d1cd4c7f212172367;hb=6ba38ffedd99c9d3ade9134b01ac510c50d92d2e;hp=94a1e6a2f0edb49b308a8f84f9660622497a7123;hpb=b079ee7c4f1c7f9e8252dbab91b068036aa7df23;p=kivitendo-erp.git diff --git a/SL/BP.pm b/SL/BP.pm index 94a1e6a2f..724ab2767 100644 --- a/SL/BP.pm +++ b/SL/BP.pm @@ -25,7 +25,8 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1335, USA. #====================================================================== # # Batch printing module backend routines @@ -35,6 +36,9 @@ package BP; use SL::DBUtils; +use SL::DB; + +use strict; sub get_vc { $main::lxdebug->enter_sub(); @@ -45,7 +49,6 @@ sub get_vc { my $dbh = $form->dbconnect($myconfig); my %arap = (invoice => 'ar', - packing_list => 'ar', sales_order => 'oe', purchase_order => 'oe', sales_quotation => 'oe', @@ -56,7 +59,7 @@ sub get_vc { my $vc = $form->{vc} eq "customer" ? "customer" : "vendor"; my $arap_type = defined($arap{$form->{type}}) ? $arap{$form->{type}} : 'ar'; - $query = + my $query = qq|SELECT count(*) | . qq|FROM (SELECT DISTINCT ON (vc.id) vc.id FROM $vc vc, $arap_type a, status s | . qq| WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | . @@ -72,18 +75,16 @@ sub get_vc { qq|WHERE a.${vc}_id = vc.id AND s.trans_id = a.id AND s.formname = ? | . qq| AND s.spoolfile IS NOT NULL|; - $sth = $dbh->prepare($query); + my $sth = $dbh->prepare($query); $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})"); $form->{"all_${vc}"} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{"all_${vc}"} }, $ref; } $sth->finish; } - $dbh->disconnect; - $main::lxdebug->leave_sub(); } @@ -93,7 +94,7 @@ sub payment_accounts { my ($self, $myconfig, $form) = @_; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | . @@ -103,12 +104,11 @@ sub payment_accounts { $sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})"); $form->{accounts} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{accounts} }, $ref; } $sth->finish; - $dbh->disconnect; $main::lxdebug->leave_sub(); } @@ -118,8 +118,7 @@ sub get_spoolfiles { my ($self, $myconfig, $form) = @_; - # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my ($query, $arap, @values); my $invnumber = "invnumber"; @@ -148,6 +147,7 @@ sub get_spoolfiles { } else { $arap = "ar"; my $invoice = "a.invoice"; + my $quonumber = "a.quonumber"; if ($form->{type} =~ /_(order|quotation)$/) { $invnumber = "ordnumber"; @@ -155,16 +155,29 @@ sub get_spoolfiles { $invoice = '0'; } + if ($form->{type} eq 'packing_list') { + $invnumber = "donumber"; + $arap = "delivery_orders"; + $invoice = '0'; + $quonumber = '0'; + } + $query = - qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, a.quonumber, | . + qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | . qq| a.transdate, $invoice AS invoice, '$arap' AS module, vc.name, | . qq| s.spoolfile | . qq|FROM $arap a, ${vc} vc, status s | . qq|WHERE s.trans_id = a.id | . qq| AND s.spoolfile IS NOT NULL | . - qq| AND s.formname = ? | . + ($form->{type} eq 'packing_list' + ? qq| AND s.formname IN (?, ?) | + : qq| AND s.formname = ? |) . qq| AND a.${vc}_id = vc.id|; @values = ($form->{type}); + + if ($form->{type} eq 'packing_list') { + @values = qw(sales_delivery_order purchase_delivery_order); + } } if ($form->{"${vc}_id"}) { @@ -172,16 +185,16 @@ sub get_spoolfiles { push(@values, conv_i($form->{"${vc}_id"})); } elsif ($form->{ $vc }) { $query .= " AND vc.name ILIKE ?"; - push(@values, $form->like($form->{ $vc })); + push(@values, like($form->{ $vc })); } - foreach my $column (qw(invnumber ordnumber quonumber)) { + foreach my $column (qw(invnumber ordnumber quonumber donumber)) { if ($form->{$column}) { $query .= " AND a.$column ILIKE ?"; - push(@values, $form->like($form->{$column})); + push(@values, like($form->{$column})); } } - if ($form->{type} =~ /(invoice|sales_order|sales_quotation|packing_list|puchase_order|request_quotation)$/) { + if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) { if ($form->{transdatefrom}) { $query .= " AND a.transdate >= ?"; push(@values, $form->{transdatefrom}); @@ -192,11 +205,11 @@ sub get_spoolfiles { } } - my @a = (transdate, $invnumber, name); + my @a = ("transdate", $invnumber, "name"); my $sortorder = join ', ', $form->sort_columns(@a); if (grep({ $_ eq $form->{sort} } - qw(transdate invnumber ordnumber quonumber name))) { + qw(transdate invnumber ordnumber quonumber donumber name))) { $sortorder = $form->{sort}; } @@ -207,12 +220,11 @@ sub get_spoolfiles { $form->dberror($query . " (" . join(", ", @values) . ")"); $form->{SPOOL} = []; - while (my $ref = $sth->fetchrow_hashref(NAME_lc)) { + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { push @{ $form->{SPOOL} }, $ref; } $sth->finish; - $dbh->disconnect; $main::lxdebug->leave_sub(); } @@ -220,53 +232,52 @@ sub get_spoolfiles { sub delete_spool { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form, $spool) = @_; + my ($self, $myconfig, $form) = @_; - # connect to database, turn AutoCommit off - my $dbh = $form->dbconnect_noauto($myconfig); + my $spool = $::lx_office_conf{paths}->{spool}; - my $query; + SL::DB->client->with_transaction(sub { + my $dbh = SL::DB->client->dbh; - if ($form->{type} =~ /(check|receipt)/) { - $query = qq|DELETE FROM status WHERE spoolfile = ?|; - } else { - $query = - qq|UPDATE status SET spoolfile = NULL, printed = '1' | . - qq|WHERE spoolfile = ?|; - } - my $sth = $dbh->prepare($query) || $form->dberror($query); + my $query; - foreach my $i (1 .. $form->{rowcount}) { - if ($form->{"checked_$i"}) { - $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query); - $sth->finish; + if ($form->{type} =~ /(check|receipt)/) { + $query = qq|DELETE FROM status WHERE spoolfile = ?|; + } else { + $query = + qq|UPDATE status SET spoolfile = NULL, printed = '1' | . + qq|WHERE spoolfile = ?|; } - } + my $sth = $dbh->prepare($query) || $form->dberror($query); - # commit - my $rc = $dbh->commit; - $dbh->disconnect; + foreach my $i (1 .. $form->{rowcount}) { + if ($form->{"checked_$i"}) { + $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query); + $sth->finish; + } + } - if ($rc) { foreach my $i (1 .. $form->{rowcount}) { if ($form->{"checked_$i"}) { unlink(qq|$spool/$form->{"spoolfile_$i"}|); } } - } + 1; + }) or do { die SL::DB->client->error }; $main::lxdebug->leave_sub(); - - return $rc; + return 1; } sub print_spool { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form, $spool) = @_; + my ($self, $myconfig, $form, $output) = @_; + + my $spool = $::lx_office_conf{paths}->{spool}; # connect to database - my $dbh = $form->dbconnect($myconfig); + my $dbh = SL::DB->client->dbh; my $query = qq|UPDATE status SET printed = '1' | . @@ -275,9 +286,11 @@ sub print_spool { foreach my $i (1 .. $form->{rowcount}) { if ($form->{"checked_$i"}) { - open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!"); + # $output is safe ( = does not come directly from the browser). + open(OUT, $output) or $form->error("$output : $!"); - $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|; + $form->{"spoolfile_$i"} =~ s|.*/||; + my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|; # send file to printer open(IN, $spoolfile) or $form->error("$spoolfile : $!"); @@ -295,8 +308,6 @@ sub print_spool { } } - $dbh->disconnect; - $main::lxdebug->leave_sub(); }