Projekt

Allgemein

Profil

Herunterladen (7,19 KB) Statistiken
| Zweig: | Markierung: | Revision:
d319704a Moritz Bunkus
#=====================================================================
# LX-Office ERP
# Copyright (C) 2004
# Based on SQL-Ledger Version 2.1.9
# Web http://www.lx-office.org
#
#=====================================================================
# SQL-Ledger Accounting
# Copyright (C) 2003
#
# Author: Dieter Simader
# Email: dsimader@sql-ledger.org
# Web: http://www.sql-ledger.org
#
# Contributors:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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
f7b15d43 Christian Wittmer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1335, USA.
d319704a Moritz Bunkus
#======================================================================
#
# Batch printing module backend routines
#
#======================================================================

package BP;

bb374138 Moritz Bunkus
use SL::DBUtils;
aad72b4a Sven Schöling
use SL::DB;
bb374138 Moritz Bunkus
76c486e3 Sven Schöling
use strict;

d319704a Moritz Bunkus
sub payment_accounts {
$main::lxdebug->enter_sub();

my ($self, $myconfig, $form) = @_;

# connect to database
aad72b4a Sven Schöling
my $dbh = SL::DB->client->dbh;
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
my $query =
qq|SELECT DISTINCT ON (s.chart_id) c.accno, c.description | .
qq|FROM status s, chart c | .
qq|WHERE s.chart_id = c.id AND s.formname = ?|;
d319704a Moritz Bunkus
my $sth = $dbh->prepare($query);
bb374138 Moritz Bunkus
$sth->execute($form->{type}) || $form->dberror($query . " ($form->{type})");
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
$form->{accounts} = [];
76c486e3 Sven Schöling
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
d319704a Moritz Bunkus
push @{ $form->{accounts} }, $ref;
}

$sth->finish;

$main::lxdebug->leave_sub();
}

sub get_spoolfiles {
$main::lxdebug->enter_sub();

my ($self, $myconfig, $form) = @_;

aad72b4a Sven Schöling
my $dbh = SL::DB->client->dbh;
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
my ($query, $arap, @values);
d319704a Moritz Bunkus
my $invnumber = "invnumber";

bb374138 Moritz Bunkus
my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";

d319704a Moritz Bunkus
if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {

$arap = ($form->{type} eq 'check') ? "ap" : "ar";
my ($accno) = split /--/, $form->{account};

bb374138 Moritz Bunkus
$query =
qq|SELECT a.id, s.spoolfile, vc.name, ac.transdate, a.invnumber, | .
qq| a.invoice, '$arap' AS module | .
qq|FROM status s, chart c, $vc vc, $arap a, acc_trans ac | .
qq|WHERE s.formname = ? | .
qq| AND s.chart_id = c.id | .
qq| AND c.accno = ? | .
qq| AND s.trans_id = a.id | .
qq| AND a.${vc}_id = vc.id | .
qq| AND ac.trans_id = s.trans_id | .
qq| AND ac.chart_id = c.id | .
qq| AND NOT ac.fx_transaction|;
@values = ($form->{type}, $accno);
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
} else {
d319704a Moritz Bunkus
$arap = "ar";
my $invoice = "a.invoice";
9ae7f1f9 Sven Schöling
my $quonumber = "a.quonumber";
d319704a Moritz Bunkus
if ($form->{type} =~ /_(order|quotation)$/) {
$invnumber = "ordnumber";
$arap = "oe";
$invoice = '0';
}

9ae7f1f9 Sven Schöling
if ($form->{type} eq 'packing_list') {
$invnumber = "donumber";
$arap = "delivery_orders";
$invoice = '0';
$quonumber = '0';
}

bb374138 Moritz Bunkus
$query =
9ae7f1f9 Sven Schöling
qq|SELECT a.id, a.$invnumber AS invnumber, a.ordnumber, $quonumber, | .
bb374138 Moritz Bunkus
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 | .
9ae7f1f9 Sven Schöling
($form->{type} eq 'packing_list'
? qq| AND s.formname IN (?, ?) |
: qq| AND s.formname = ? |) .
bb374138 Moritz Bunkus
qq| AND a.${vc}_id = vc.id|;
@values = ($form->{type});
9ae7f1f9 Sven Schöling
if ($form->{type} eq 'packing_list') {
@values = qw(sales_delivery_order purchase_delivery_order);
}
d319704a Moritz Bunkus
}

bb374138 Moritz Bunkus
if ($form->{"${vc}_id"}) {
$query .= qq| AND a.${vc}_id = ?|;
push(@values, conv_i($form->{"${vc}_id"}));
} elsif ($form->{ $vc }) {
$query .= " AND vc.name ILIKE ?";
bed19453 Moritz Bunkus
push(@values, like($form->{ $vc }));
d319704a Moritz Bunkus
}
9ae7f1f9 Sven Schöling
foreach my $column (qw(invnumber ordnumber quonumber donumber)) {
bb374138 Moritz Bunkus
if ($form->{$column}) {
$query .= " AND a.$column ILIKE ?";
bed19453 Moritz Bunkus
push(@values, like($form->{$column}));
bb374138 Moritz Bunkus
}
d319704a Moritz Bunkus
}

71180454 Sven Schöling
if ($form->{type} =~ /(invoice|sales_order|sales_quotation|purchase_order|request_quotation|packing_list)$/) {
bb374138 Moritz Bunkus
if ($form->{transdatefrom}) {
$query .= " AND a.transdate >= ?";
push(@values, $form->{transdatefrom});
}
if ($form->{transdateto}) {
$query .= " AND a.transdate <= ?";
push(@values, $form->{transdateto});
}
1ff9da9f Moritz Bunkus
}
d319704a Moritz Bunkus
76c486e3 Sven Schöling
my @a = ("transdate", $invnumber, "name");
d319704a Moritz Bunkus
my $sortorder = join ', ', $form->sort_columns(@a);

bb374138 Moritz Bunkus
if (grep({ $_ eq $form->{sort} }
9ae7f1f9 Sven Schöling
qw(transdate invnumber ordnumber quonumber donumber name))) {
bb374138 Moritz Bunkus
$sortorder = $form->{sort};
}

$query .= " ORDER BY $sortorder";
d319704a Moritz Bunkus
my $sth = $dbh->prepare($query);
bb374138 Moritz Bunkus
$sth->execute(@values) ||
$form->dberror($query . " (" . join(", ", @values) . ")");
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
$form->{SPOOL} = [];
76c486e3 Sven Schöling
while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
d319704a Moritz Bunkus
push @{ $form->{SPOOL} }, $ref;
}

$sth->finish;

$main::lxdebug->leave_sub();
}

sub delete_spool {
$main::lxdebug->enter_sub();

8cd05ad6 Moritz Bunkus
my ($self, $myconfig, $form) = @_;

my $spool = $::lx_office_conf{paths}->{spool};
d319704a Moritz Bunkus
aad72b4a Sven Schöling
SL::DB->client->with_transaction(sub {
my $dbh = SL::DB->client->dbh;
d319704a Moritz Bunkus
aad72b4a Sven Schöling
my $query;
d319704a Moritz Bunkus
aad72b4a Sven Schöling
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 = ?|;
d319704a Moritz Bunkus
}
aad72b4a Sven Schöling
my $sth = $dbh->prepare($query) || $form->dberror($query);
d319704a Moritz Bunkus
aad72b4a Sven Schöling
foreach my $i (1 .. $form->{rowcount}) {
if ($form->{"checked_$i"}) {
$sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
$sth->finish;
}
}
d319704a Moritz Bunkus
foreach my $i (1 .. $form->{rowcount}) {
if ($form->{"checked_$i"}) {
bb374138 Moritz Bunkus
unlink(qq|$spool/$form->{"spoolfile_$i"}|);
d319704a Moritz Bunkus
}
}
6b23fb21 Sven Schöling
1;
}) or do { die SL::DB->client->error };
d319704a Moritz Bunkus
$main::lxdebug->leave_sub();
aad72b4a Sven Schöling
return 1;
d319704a Moritz Bunkus
}

sub print_spool {
$main::lxdebug->enter_sub();

8cd05ad6 Moritz Bunkus
my ($self, $myconfig, $form, $output) = @_;

my $spool = $::lx_office_conf{paths}->{spool};
d319704a Moritz Bunkus
# connect to database
aad72b4a Sven Schöling
my $dbh = SL::DB->client->dbh;
d319704a Moritz Bunkus
bb374138 Moritz Bunkus
my $query =
qq|UPDATE status SET printed = '1' | .
qq|WHERE formname = ? AND spoolfile = ?|;
d319704a Moritz Bunkus
my $sth = $dbh->prepare($query) || $form->dberror($query);

foreach my $i (1 .. $form->{rowcount}) {
if ($form->{"checked_$i"}) {
b5157f97 Moritz Bunkus
# $output is safe ( = does not come directly from the browser).
open(OUT, $output) or $form->error("$output : $!");
d319704a Moritz Bunkus
b5157f97 Moritz Bunkus
$form->{"spoolfile_$i"} =~ s|.*/||;
76c486e3 Sven Schöling
my $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
d319704a Moritz Bunkus
# send file to printer
open(IN, $spoolfile) or $form->error("$spoolfile : $!");

while (<IN>) {
print OUT $_;
}
close(IN);
close(OUT);

bb374138 Moritz Bunkus
$sth->execute($form->{type}, $form->{"spoolfile_$i"}) ||
$form->dberror($query . " ($form->{type}, " . $form->{"spoolfile_$i"} . ")");
d319704a Moritz Bunkus
$sth->finish;

}
}

$main::lxdebug->leave_sub();
}

1;