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
15 # Contributors: Thomas Bayen <bayen@gmx.de>
16 # Antti Kaihola <akaihola@siba.fi>
17 # Moritz Bunkus (tex code)
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 2 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 # You should have received a copy of the GNU General Public License
29 # along with this program; if not, write to the Free Software
30 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #======================================================================
32 # Utilities for parsing forms
33 # and supporting routines for linking account numbers
34 # used in AR, AP and IS, IR modules
36 #======================================================================
50 $main::lxdebug->enter_sub(2);
54 my @pairs = split(/&/, $input);
57 my ($name, $value) = split(/=/, $_, 2);
58 $in{$name} = unescape(undef, $value);
61 $main::lxdebug->leave_sub(2);
66 sub _request_to_hash {
67 $main::lxdebug->enter_sub(2);
70 my ($i, $loc, $key, $val);
71 my (%ATTACH, $f, $header, $header_body, $len, $buf);
72 my ($boundary, @list, $size, $body, $x, $blah, $name);
74 if ($ENV{'CONTENT_TYPE'}
75 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
76 $boundary = quotemeta('--' . $1);
77 @list = split(/$boundary/, $input);
79 # For some reason there are always 2 extra, that are empty
82 for ($x = 1; $x <= $size; $x++) {
83 $header_body = $list[$x];
84 $header_body =~ /\r\n\r\n|\n\n/;
86 # Here we split the header and body
91 # Now we try to get the file name
93 $name =~ /name=\"(.+)\"/;
94 ($name, $blah) = split(/\"/, $1);
96 # If the form name is not attach, then we need to parse this like
98 if ($name ne "attach") {
99 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
100 $ATTACH{$name} = $body;
102 # Otherwise it is an attachment and we need to finish it up
103 } elsif ($name eq "attach") {
104 $header =~ /filename=\"(.+)\"/;
105 $ATTACH{'FILE_NAME'} = $1;
106 $ATTACH{'FILE_NAME'} =~ s/\"//g;
107 $ATTACH{'FILE_NAME'} =~ s/\s//g;
108 $ATTACH{'FILE_CONTENT'} = $body;
110 for ($i = $x; $list[$i]; $i++) {
111 $list[$i] =~ s/^.+name=$//;
112 $list[$i] =~ /\"(\w+)\"/;
118 $main::lxdebug->leave_sub(2);
122 $main::lxdebug->leave_sub(2);
123 return _input_to_hash($input);
128 $main::lxdebug->enter_sub();
134 read(STDIN, $_, $ENV{CONTENT_LENGTH});
136 if ($ENV{QUERY_STRING}) {
137 $_ = $ENV{QUERY_STRING};
144 my %parameters = _request_to_hash($_);
145 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
147 $self->{action} = lc $self->{action};
148 $self->{action} =~ s/( |-|,|\#)/_/g;
150 $self->{version} = "2.4.1";
152 $main::lxdebug->leave_sub();
158 $main::lxdebug->enter_sub();
164 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
166 $main::lxdebug->leave_sub();
170 $main::lxdebug->enter_sub(2);
172 my ($self, $str, $beenthere) = @_;
174 # for Apache 2 we escape strings twice
175 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
176 # $str = $self->escape($str, 1);
179 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
181 $main::lxdebug->leave_sub(2);
187 $main::lxdebug->enter_sub(2);
189 my ($self, $str) = @_;
194 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
196 $main::lxdebug->leave_sub(2);
202 my ($self, $str) = @_;
204 if ($str && !ref($str)) {
205 $str =~ s/\"/"/g;
213 my ($self, $str) = @_;
215 if ($str && !ref($str)) {
216 $str =~ s/"/\"/g;
224 $main::lxdebug->enter_sub(2);
226 my ($self, $str) = @_;
229 ('order' => ['"', '<', '>'],
235 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
237 $main::lxdebug->leave_sub(2);
247 print qq|<input type=hidden name="$_" value="|
248 . $self->quote($self->{$_})
252 delete $self->{header};
253 for (sort keys %$self) {
254 print qq|<input type=hidden name="$_" value="|
255 . $self->quote($self->{$_})
263 $main::lxdebug->enter_sub();
265 my ($self, $msg) = @_;
266 if ($ENV{HTTP_USER_AGENT}) {
268 $self->show_generic_error($msg);
272 if ($self->{error_function}) {
273 &{ $self->{error_function} }($msg);
279 $main::lxdebug->leave_sub();
283 $main::lxdebug->enter_sub();
285 my ($self, $msg) = @_;
287 if ($ENV{HTTP_USER_AGENT}) {
290 if (!$self->{header}) {
303 if ($self->{info_function}) {
304 &{ $self->{info_function} }($msg);
310 $main::lxdebug->leave_sub();
314 $main::lxdebug->enter_sub();
316 my ($self, $str, $cols, $maxrows) = @_;
320 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
322 $maxrows = $rows unless defined $maxrows;
324 $main::lxdebug->leave_sub();
326 return ($rows > $maxrows) ? $maxrows : $rows;
330 $main::lxdebug->enter_sub();
332 my ($self, $msg) = @_;
334 $self->error("$msg\n" . $DBI::errstr);
336 $main::lxdebug->leave_sub();
340 $main::lxdebug->enter_sub();
342 my ($self, $name, $msg) = @_;
344 if ($self->{$name} =~ /^\s*$/) {
347 $main::lxdebug->leave_sub();
351 $main::lxdebug->enter_sub();
353 my ($self, $extra_code) = @_;
355 if ($self->{header}) {
356 $main::lxdebug->leave_sub();
360 my ($stylesheet, $favicon, $charset);
362 if ($ENV{HTTP_USER_AGENT}) {
364 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
366 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
370 $self->{favicon} = "favicon.ico" unless $self->{favicon};
372 if ($self->{favicon} && (-f "$self->{favicon}")) {
374 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
378 if ($self->{charset}) {
380 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
383 if ($self->{landscape}) {
384 $pagelayout = qq|<style type="text/css">
385 \@page { size:landscape; }
388 if ($self->{fokus}) {
389 $fokus = qq|<script type="text/javascript">
391 function fokus(){document.$self->{fokus}.focus();}
398 if ($self->{jsscript} == 1) {
401 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
402 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
403 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
404 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
411 ? "$self->{title} - $self->{titlebar}"
414 foreach $item (@ { $self->{AJAX} }) {
415 $ajax .= $item->show_javascript();
417 print qq|Content-Type: text/html
421 <title>$self->{titlebar}</title>
429 <meta name="robots" content="noindex,nofollow" />
430 <script type="text/javascript" src="js/highlight_input.js"></script>
431 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
433 <script type="text/javascript" src="js/tabcontent.js">
435 /***********************************************
436 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
437 * This notice MUST stay intact for legal use
438 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
439 ***********************************************/
450 $main::lxdebug->leave_sub();
453 sub parse_html_template {
454 $main::lxdebug->enter_sub();
456 my ($self, $file, $additional_params) = @_;
459 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
460 $language = $main::language;
462 $language = $main::myconfig{"countrycode"};
465 if (-f "templates/webpages/${file}_${language}.html") {
466 if ((-f ".developer") &&
467 (-f "templates/webpages/${file}_master.html") &&
468 ((stat("templates/webpages/${file}_master.html"))[9] >
469 (stat("templates/webpages/${file}_${language}.html"))[9])) {
470 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
471 "Please re-run 'locales.pl' in 'locale/${language}'.";
472 print(qq|<pre>$info</pre>|);
476 $file = "templates/webpages/${file}_${language}.html";
477 } elsif (-f "templates/webpages/${file}.html") {
478 $file = "templates/webpages/${file}.html";
480 my $info = "Web page template '${file}' not found.\n" .
481 "Please re-run 'locales.pl' in 'locale/${language}'.";
482 print(qq|<pre>$info</pre>|);
486 my $template = HTML::Template->new("filename" => $file,
487 "die_on_bad_params" => 0,
489 "case_sensitive" => 1,
490 "loop_context_vars" => 1,
493 $additional_params = {} unless ($additional_params);
494 if ($self->{"DEBUG"}) {
495 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
498 if ($additional_params->{"DEBUG"}) {
499 $additional_params->{"DEBUG"} =
500 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
503 if (%main::myconfig) {
504 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
505 my $jsc_dateformat = $main::myconfig{"dateformat"};
506 $jsc_dateformat =~ s/d+/\%d/gi;
507 $jsc_dateformat =~ s/m+/\%m/gi;
508 $jsc_dateformat =~ s/y+/\%Y/gi;
509 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
512 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
513 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
514 $additional_params->{"conf_latex_templates"} = $main::latex;
515 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
517 my @additional_param_names = keys(%{$additional_params});
518 foreach my $key ($template->param()) {
519 my $param = $self->{$key};
520 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
521 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
522 $template->param($key => $param);
525 my $output = $template->output();
527 $main::lxdebug->leave_sub();
532 sub show_generic_error {
533 my ($self, $error, $title, $action) = @_;
536 $add_params->{"title"} = $title if ($title);
537 $self->{"label_error"} = $error;
541 map({ delete($self->{$_}); } qw(action));
542 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
543 if (!ref($self->{$_})); }
545 $add_params->{"SHOW_BUTTON"} = 1;
546 $add_params->{"BUTTON_LABEL"} = $action;
548 $add_params->{"VARIABLES"} = \@vars;
551 print($self->parse_html_template("generic/error", $add_params));
553 die("Error: $error\n");
556 sub show_generic_information {
557 my ($self, $error, $title) = @_;
560 $add_params->{"title"} = $title if ($title);
561 $self->{"label_information"} = $error;
564 print($self->parse_html_template("generic/information", $add_params));
566 die("Information: $error\n");
569 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
570 # changed it to accept an arbitrary number of triggers - sschoeling
572 $main::lxdebug->enter_sub();
575 my $myconfig = shift;
578 # set dateform for jsscript
581 "dd.mm.yy" => "%d.%m.%Y",
582 "dd-mm-yy" => "%d-%m-%Y",
583 "dd/mm/yy" => "%d/%m/%Y",
584 "mm/dd/yy" => "%m/%d/%Y",
585 "mm-dd-yy" => "%m-%d-%Y",
586 "yyyy-mm-dd" => "%Y-%m-%d",
589 my $ifFormat = defined($dateformats{$myconfig{"dateformat"}}) ?
590 $dateformats{$myconfig{"dateformat"}} : "%d.%m.%Y";
597 inputField : "| . (shift) . qq|",
598 ifFormat :"$ifFormat",
599 align : "| . (shift) . qq|",
600 button : "| . (shift) . qq|"
606 <script type="text/javascript">
607 <!--| . join("", @triggers) . qq|//-->
611 $main::lxdebug->leave_sub();
614 } #end sub write_trigger
617 $main::lxdebug->enter_sub();
619 my ($self, $msg) = @_;
621 if ($self->{callback}) {
623 ($script, $argv) = split(/\?/, $self->{callback});
624 exec("perl", "$script", $argv);
632 $main::lxdebug->leave_sub();
635 # sort of columns removed - empty sub
637 $main::lxdebug->enter_sub();
639 my ($self, @columns) = @_;
641 $main::lxdebug->leave_sub();
647 $main::lxdebug->enter_sub(2);
649 my ($self, $myconfig, $amount, $places, $dash) = @_;
654 my $neg = ($amount =~ s/-//);
656 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
658 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
659 my @p = split(/\./, $amount); # split amount at decimal point
661 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
664 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
667 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
668 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
669 ($neg ? "-$amount" : "$amount" ) ;
673 $main::lxdebug->leave_sub(2);
678 $main::lxdebug->enter_sub(2);
680 my ($self, $myconfig, $amount) = @_;
682 if ($myconfig->{in_numberformat} == 1) {
683 # Extra input number format 1000.00 or 1000,00
685 $amount = scalar reverse $amount;
686 $amount =~ s/\./DOT/;
688 $amount =~ s/DOT/\./;
689 $amount = scalar reverse $amount;
690 $main::lxdebug->leave_sub(2);
691 return ($amount * 1);
694 if ( ($myconfig->{numberformat} eq '1.000,00')
695 || ($myconfig->{numberformat} eq '1000,00')) {
700 if ($myconfig->{numberformat} eq "1'000.00") {
706 $main::lxdebug->leave_sub(2);
708 return ($amount * 1);
712 $main::lxdebug->enter_sub(2);
714 my ($self, $amount, $places) = @_;
717 # Rounding like "Kaufmannsrunden"
718 # Descr. http://de.wikipedia.org/wiki/Rundung
720 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
723 $amount = $amount * (10**($places));
724 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
726 $main::lxdebug->leave_sub(2);
728 return $round_amount;
733 $main::lxdebug->enter_sub();
735 my ($self, $myconfig, $userspath) = @_;
738 $self->{"cwd"} = getcwd();
739 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
741 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
742 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
743 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
744 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
745 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
746 } elsif (($self->{"format"} =~ /html/i) ||
747 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
748 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
749 } elsif (($self->{"format"} =~ /xml/i) ||
750 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
751 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
752 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
753 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
755 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
756 } elsif ( defined $self->{'format'}) {
757 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
758 } elsif ( $self->{'format'} eq '' ) {
759 $self->error("No Outputformat given: $self->{'format'}");
760 } else { #Catch the rest
761 $self->error("Outputformat not defined: $self->{'format'}");
764 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
765 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
767 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
768 qw(email tel fax name signature company address businessnumber
769 co_ustid taxnumber duns));
770 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
771 qw(company address signature));
772 map({ $self->{$_} =~ s/\\n/\n/g; } qw(company address signature));
774 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
776 # OUT is used for the media, screen, printer, email
777 # for postscript we store a copy in a temporary file
779 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
780 if ($template->uses_temp_file() || $self->{media} eq 'email') {
782 $self->{OUT} = ">$self->{tmpfile}";
786 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
788 open(OUT, ">-") or $self->error("STDOUT : $!");
792 if (!$template->parse(*OUT)) {
794 $self->error("$self->{IN} : " . $template->get_error());
799 if ($template->uses_temp_file() || $self->{media} eq 'email') {
801 if ($self->{media} eq 'email') {
805 my $mail = new Mailer;
807 map { $mail->{$_} = $self->{$_} }
808 qw(cc bcc subject message version format charset);
809 $mail->{to} = qq|$self->{email}|;
810 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
811 $mail->{fileid} = "$fileid.";
812 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
814 # if we send html or plain text inline
815 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
816 $mail->{contenttype} = "text/html";
818 $mail->{message} =~ s/\r\n/<br>\n/g;
819 $myconfig->{signature} =~ s/\\n/<br>\n/g;
820 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
822 open(IN, $self->{tmpfile})
823 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
825 $mail->{message} .= $_;
832 if (!$self->{"do_not_attach"}) {
833 @{ $mail->{attachments} } =
834 ({ "filename" => $self->{"tmpfile"},
835 "name" => $self->{"attachment_filename"} ?
836 $self->{"attachment_filename"} : $self->{"tmpfile"} });
839 $mail->{message} =~ s/\r\n/\n/g;
840 $myconfig->{signature} =~ s/\\n/\n/g;
841 $mail->{message} .= "\n-- \n$myconfig->{signature}";
845 my $err = $mail->send($out);
846 $self->error($self->cleanup . "$err") if ($err);
852 my $numbytes = (-s $self->{tmpfile});
853 open(IN, $self->{tmpfile})
854 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
856 $self->{copies} = 1 unless $self->{media} eq 'printer';
858 chdir("$self->{cwd}");
859 #print(STDERR "Kopien $self->{copies}\n");
860 #print(STDERR "OUT $self->{OUT}\n");
861 for my $i (1 .. $self->{copies}) {
863 open(OUT, $self->{OUT})
864 or $self->error($self->cleanup . "$self->{OUT} : $!");
866 $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
868 print qq|Content-Type: | . $template->get_mime_type() . qq|
869 Content-Disposition: attachment; filename="$self->{attachment_filename}"
870 Content-Length: $numbytes
874 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
894 chdir("$self->{cwd}");
895 $main::lxdebug->leave_sub();
899 $main::lxdebug->enter_sub();
903 chdir("$self->{tmpdir}");
906 if (-f "$self->{tmpfile}.err") {
907 open(FH, "$self->{tmpfile}.err");
912 if ($self->{tmpfile}) {
913 $self->{tmpfile} =~ s|.*/||g;
915 $self->{tmpfile} =~ s/\.\w+$//g;
916 my $tmpfile = $self->{tmpfile};
917 unlink(<$tmpfile.*>);
920 chdir("$self->{cwd}");
922 $main::lxdebug->leave_sub();
928 $main::lxdebug->enter_sub();
930 my ($self, $date, $myconfig) = @_;
932 if ($date && $date =~ /\D/) {
934 if ($myconfig->{dateformat} =~ /^yy/) {
935 ($yy, $mm, $dd) = split /\D/, $date;
937 if ($myconfig->{dateformat} =~ /^mm/) {
938 ($mm, $dd, $yy) = split /\D/, $date;
940 if ($myconfig->{dateformat} =~ /^dd/) {
941 ($dd, $mm, $yy) = split /\D/, $date;
946 $yy = ($yy < 70) ? $yy + 2000 : $yy;
947 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
949 $dd = "0$dd" if ($dd < 10);
950 $mm = "0$mm" if ($mm < 10);
955 $main::lxdebug->leave_sub();
960 # Database routines used throughout
963 $main::lxdebug->enter_sub();
965 my ($self, $myconfig) = @_;
967 # connect to database
969 DBI->connect($myconfig->{dbconnect},
970 $myconfig->{dbuser}, $myconfig->{dbpasswd})
974 if ($myconfig->{dboptions}) {
975 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
978 $main::lxdebug->leave_sub();
983 sub dbconnect_noauto {
984 $main::lxdebug->enter_sub();
986 my ($self, $myconfig) = @_;
988 # connect to database
990 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
991 $myconfig->{dbpasswd}, { AutoCommit => 0 })
995 if ($myconfig->{dboptions}) {
996 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
999 $main::lxdebug->leave_sub();
1004 sub update_balance {
1005 $main::lxdebug->enter_sub();
1007 my ($self, $dbh, $table, $field, $where, $value) = @_;
1009 # if we have a value, go do it
1012 # retrieve balance from table
1013 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1014 my $sth = $dbh->prepare($query);
1016 $sth->execute || $self->dberror($query);
1017 my ($balance) = $sth->fetchrow_array;
1023 $query = "UPDATE $table SET $field = $balance WHERE $where";
1024 $dbh->do($query) || $self->dberror($query);
1026 $main::lxdebug->leave_sub();
1029 sub update_exchangerate {
1030 $main::lxdebug->enter_sub();
1032 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1034 # some sanity check for currency
1036 $main::lxdebug->leave_sub();
1040 my $query = qq|SELECT e.curr FROM exchangerate e
1041 WHERE e.curr = '$curr'
1042 AND e.transdate = '$transdate'
1044 my $sth = $dbh->prepare($query);
1045 $sth->execute || $self->dberror($query);
1048 if ($buy != 0 && $sell != 0) {
1049 $set = "buy = $buy, sell = $sell";
1050 } elsif ($buy != 0) {
1051 $set = "buy = $buy";
1052 } elsif ($sell != 0) {
1053 $set = "sell = $sell";
1056 if ($sth->fetchrow_array) {
1057 $query = qq|UPDATE exchangerate
1059 WHERE curr = '$curr'
1060 AND transdate = '$transdate'|;
1062 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1063 VALUES ('$curr', $buy, $sell, '$transdate')|;
1066 $dbh->do($query) || $self->dberror($query);
1068 $main::lxdebug->leave_sub();
1071 sub save_exchangerate {
1072 $main::lxdebug->enter_sub();
1074 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1076 my $dbh = $self->dbconnect($myconfig);
1078 my ($buy, $sell) = (0, 0);
1079 $buy = $rate if $fld eq 'buy';
1080 $sell = $rate if $fld eq 'sell';
1082 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1086 $main::lxdebug->leave_sub();
1089 sub get_exchangerate {
1090 $main::lxdebug->enter_sub();
1092 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1094 unless ($transdate) {
1095 $main::lxdebug->leave_sub();
1099 my $query = qq|SELECT e.$fld FROM exchangerate e
1100 WHERE e.curr = '$curr'
1101 AND e.transdate = '$transdate'|;
1102 my $sth = $dbh->prepare($query);
1103 $sth->execute || $self->dberror($query);
1105 my ($exchangerate) = $sth->fetchrow_array;
1108 if ($exchangerate == 0) {
1112 $main::lxdebug->leave_sub();
1114 return $exchangerate;
1117 sub set_payment_options {
1118 $main::lxdebug->enter_sub();
1120 my ($self, $myconfig, $transdate) = @_;
1122 if ($self->{payment_id}) {
1124 my $dbh = $self->dbconnect($myconfig);
1127 qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, | .
1128 qq|p.description_long | .
1129 qq|FROM payment_terms p | .
1132 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto},
1133 $self->{payment_terms}) =
1134 selectrow_query($self, $dbh, $query, $self->{payment_id});
1136 if ($transdate eq "") {
1137 if ($self->{invdate}) {
1138 $transdate = $self->{invdate};
1140 $transdate = $self->{transdate};
1145 qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date, | .
1146 qq|date '$transdate' + $self->{terms_skonto} AS skonto_date | .
1147 qq|FROM payment_terms LIMIT 1|;
1148 ($self->{netto_date}, $self->{skonto_date}) =
1149 selectrow_query($self, $dbh, $query);
1151 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1152 my $skonto_amount = $self->parse_amount($myconfig, $total) *
1153 $self->{percent_skonto};
1155 $self->{skonto_amount} =
1156 $self->format_amount($myconfig, $skonto_amount, 2);
1158 if ($self->{"language_id"}) {
1160 qq|SELECT t.description_long, | .
1161 qq|l.output_numberformat, l.output_dateformat, l.output_longdates | .
1162 qq|FROM translation_payment_terms t | .
1163 qq|LEFT JOIN language l ON t.language_id = l.id | .
1164 qq|WHERE (t.language_id = ?) AND (t.payment_terms_id = ?)|;
1165 my ($description_long, $output_numberformat, $output_dateformat,
1166 $output_longdates) =
1167 selectrow_query($self, $dbh, $query,
1168 $self->{"language_id"}, $self->{"payment_id"});
1170 $self->{payment_terms} = $description_long if ($description_long);
1172 if ($output_dateformat) {
1173 foreach my $key (qw(netto_date skonto_date)) {
1175 $main::locale->reformat_date($myconfig, $self->{$key},
1181 if ($output_numberformat &&
1182 ($output_numberformat ne $myconfig->{"numberformat"})) {
1183 my $saved_numberformat = $myconfig->{"numberformat"};
1184 $myconfig->{"numberformat"} = $output_numberformat;
1185 $self->{skonto_amount} =
1186 $self->format_amount($myconfig, $skonto_amount, 2);
1187 $myconfig->{"numberformat"} = $saved_numberformat;
1191 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1192 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1193 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1194 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1195 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1196 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1197 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1198 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1199 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1200 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1205 $main::lxdebug->leave_sub();
1209 sub check_exchangerate {
1210 $main::lxdebug->enter_sub();
1212 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1214 unless ($transdate) {
1215 $main::lxdebug->leave_sub();
1219 my $dbh = $self->dbconnect($myconfig);
1221 my $query = qq|SELECT e.$fld FROM exchangerate e
1222 WHERE e.curr = '$currency'
1223 AND e.transdate = '$transdate'|;
1224 my $sth = $dbh->prepare($query);
1225 $sth->execute || $self->dberror($query);
1227 my ($exchangerate) = $sth->fetchrow_array;
1231 $main::lxdebug->leave_sub();
1233 return $exchangerate;
1236 sub get_template_language {
1237 $main::lxdebug->enter_sub();
1239 my ($self, $myconfig) = @_;
1241 my $template_code = "";
1243 if ($self->{language_id}) {
1245 my $dbh = $self->dbconnect($myconfig);
1248 my $query = qq|SELECT l.template_code FROM language l
1249 WHERE l.id = $self->{language_id}|;
1250 my $sth = $dbh->prepare($query);
1251 $sth->execute || $self->dberror($query);
1253 ($template_code) = $sth->fetchrow_array;
1258 $main::lxdebug->leave_sub();
1260 return $template_code;
1263 sub get_printer_code {
1264 $main::lxdebug->enter_sub();
1266 my ($self, $myconfig) = @_;
1268 my $template_code = "";
1270 if ($self->{printer_id}) {
1272 my $dbh = $self->dbconnect($myconfig);
1275 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1276 WHERE p.id = $self->{printer_id}|;
1277 my $sth = $dbh->prepare($query);
1278 $sth->execute || $self->dberror($query);
1280 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1285 $main::lxdebug->leave_sub();
1287 return $template_code;
1291 $main::lxdebug->enter_sub();
1293 my ($self, $myconfig) = @_;
1295 my $template_code = "";
1297 if ($self->{shipto_id}) {
1299 my $dbh = $self->dbconnect($myconfig);
1302 my $query = qq|SELECT s.* FROM shipto s
1303 WHERE s.shipto_id = $self->{shipto_id}|;
1304 my $sth = $dbh->prepare($query);
1305 $sth->execute || $self->dberror($query);
1306 $ref = $sth->fetchrow_hashref(NAME_lc);
1307 map { $self->{$_} = $ref->{$_} } keys %$ref;
1312 $main::lxdebug->leave_sub();
1317 $main::lxdebug->enter_sub();
1319 my ($self, $dbh, $id, $module) = @_;
1323 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1325 if ($self->{"shipto$item"}) {
1326 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1328 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1331 if ($self->{shipto_id}) {
1332 my $query = qq| UPDATE shipto set
1333 shiptoname = '$self->{shiptoname}',
1334 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1335 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1336 shiptostreet = '$self->{shiptostreet}',
1337 shiptozipcode = '$self->{shiptozipcode}',
1338 shiptocity = '$self->{shiptocity}',
1339 shiptocountry = '$self->{shiptocountry}',
1340 shiptocontact = '$self->{shiptocontact}',
1341 shiptophone = '$self->{shiptophone}',
1342 shiptofax = '$self->{shiptofax}',
1343 shiptoemail = '$self->{shiptoemail}'
1344 WHERE shipto_id = $self->{shipto_id}|;
1345 $dbh->do($query) || $self->dberror($query);
1348 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1349 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1350 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1351 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1352 '$self->{shiptozipcode}', '$self->{shiptocity}',
1353 '$self->{shiptocountry}', '$self->{shiptocontact}',
1354 '$self->{shiptophone}', '$self->{shiptofax}',
1355 '$self->{shiptoemail}', '$module')|;
1356 $dbh->do($query) || $self->dberror($query);
1360 $main::lxdebug->leave_sub();
1364 $main::lxdebug->enter_sub();
1366 my ($self, $dbh) = @_;
1368 my $query = qq|SELECT e.id, e.name FROM employee e
1369 WHERE e.login = '$self->{login}'|;
1370 my $sth = $dbh->prepare($query);
1371 $sth->execute || $self->dberror($query);
1373 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1374 $self->{employee_id} *= 1;
1378 $main::lxdebug->leave_sub();
1382 $main::lxdebug->enter_sub();
1384 my ($self, $myconfig) = @_;
1386 my $dbh = $self->dbconnect($myconfig);
1387 my $query = qq|SELECT current_date+terms_netto FROM payment_terms
1388 WHERE id = '$self->{payment_id}'|;
1389 my $sth = $dbh->prepare($query);
1390 $sth->execute || $self->dberror($query);
1392 ($self->{duedate}) = $sth->fetchrow_array;
1396 $main::lxdebug->leave_sub();
1399 # get other contact for transaction and form - html/tex
1401 $main::lxdebug->enter_sub();
1403 my ($self, $dbh, $id) = @_;
1405 my $query = qq|SELECT c.*
1408 $sth = $dbh->prepare($query);
1409 $sth->execute || $self->dberror($query);
1411 $ref = $sth->fetchrow_hashref(NAME_lc);
1413 push @{ $self->{$_} }, $ref;
1416 $main::lxdebug->leave_sub();
1419 # get contacts for id, if no contact return {"","","","",""}
1421 $main::lxdebug->enter_sub();
1423 my ($self, $dbh, $id) = @_;
1425 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1427 WHERE cp_cv_id=$id|;
1428 my $sth = $dbh->prepare($query);
1429 $sth->execute || $self->dberror($query);
1432 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1433 push @{ $self->{all_contacts} }, $ref;
1438 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1441 $main::lxdebug->leave_sub();
1444 # this sub gets the id and name from $table
1446 $main::lxdebug->enter_sub();
1448 my ($self, $myconfig, $table) = @_;
1450 # connect to database
1451 my $dbh = $self->dbconnect($myconfig);
1453 my $name = $self->like(lc $self->{$table});
1454 my $customernumber = $self->like(lc $self->{customernumber});
1456 if ($self->{customernumber} ne "") {
1457 $query = qq~SELECT c.id, c.name,
1458 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1460 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1463 $query = qq~SELECT c.id, c.name,
1464 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1466 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1470 if ($self->{openinvoices}) {
1471 $query = qq~SELECT DISTINCT c.id, c.name,
1472 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1473 FROM $self->{arap} a
1474 JOIN $table c ON (a.${table}_id = c.id)
1475 WHERE NOT a.amount = a.paid
1476 AND lower(c.name) LIKE '$name'
1479 my $sth = $dbh->prepare($query);
1481 $sth->execute || $self->dberror($query);
1484 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1485 push(@{ $self->{name_list} }, $ref);
1491 $main::lxdebug->leave_sub();
1496 # the selection sub is used in the AR, AP, IS, IR and OE module
1499 $main::lxdebug->enter_sub();
1501 my ($self, $myconfig, $table, $module) = @_;
1504 my $dbh = $self->dbconnect($myconfig);
1506 my $query = qq|SELECT count(*) FROM $table|;
1507 my $sth = $dbh->prepare($query);
1508 $sth->execute || $self->dberror($query);
1509 my ($count) = $sth->fetchrow_array;
1512 # build selection list
1513 if ($count < $myconfig->{vclimit}) {
1514 $query = qq|SELECT id, name
1515 FROM $table WHERE not obsolete
1517 $sth = $dbh->prepare($query);
1518 $sth->execute || $self->dberror($query);
1520 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1521 push @{ $self->{"all_$table"} }, $ref;
1529 $self->get_employee($dbh);
1531 # setup sales contacts
1532 $query = qq|SELECT e.id, e.name
1535 AND NOT e.id = $self->{employee_id}|;
1536 $sth = $dbh->prepare($query);
1537 $sth->execute || $self->dberror($query);
1539 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1540 push @{ $self->{all_employees} }, $ref;
1545 push @{ $self->{all_employees} },
1546 { id => $self->{employee_id},
1547 name => $self->{employee} };
1549 # sort the whole thing
1550 @{ $self->{all_employees} } =
1551 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1553 if ($module eq 'AR') {
1555 # prepare query for departments
1556 $query = qq|SELECT d.id, d.description
1562 $query = qq|SELECT d.id, d.description
1567 $sth = $dbh->prepare($query);
1568 $sth->execute || $self->dberror($query);
1570 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1571 push @{ $self->{all_departments} }, $ref;
1576 $query = qq|SELECT id, description
1579 $sth = $dbh->prepare($query);
1580 $sth->execute || $self->dberror($query);
1582 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1583 push @{ $self->{languages} }, $ref;
1588 $query = qq|SELECT printer_description, id
1591 $sth = $dbh->prepare($query);
1592 $sth->execute || $self->dberror($query);
1594 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1595 push @{ $self->{printers} }, $ref;
1601 $query = qq|SELECT id, description
1604 $sth = $dbh->prepare($query);
1605 $sth->execute || $self->dberror($query);
1607 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1608 push @{ $self->{payment_terms} }, $ref;
1612 $main::lxdebug->leave_sub();
1616 sub language_payment {
1617 $main::lxdebug->enter_sub();
1619 my ($self, $myconfig) = @_;
1620 undef $self->{languages};
1621 undef $self->{payment_terms};
1622 undef $self->{printers};
1625 my $dbh = $self->dbconnect($myconfig);
1627 my $query = qq|SELECT id, description
1630 my $sth = $dbh->prepare($query);
1631 $sth->execute || $self->dberror($query);
1633 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1634 push @{ $self->{languages} }, $ref;
1639 $query = qq|SELECT printer_description, id
1642 $sth = $dbh->prepare($query);
1643 $sth->execute || $self->dberror($query);
1645 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1646 push @{ $self->{printers} }, $ref;
1651 $query = qq|SELECT id, description
1654 $sth = $dbh->prepare($query);
1655 $sth->execute || $self->dberror($query);
1657 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1658 push @{ $self->{payment_terms} }, $ref;
1662 # get buchungsgruppen
1663 $query = qq|SELECT id, description
1664 FROM buchungsgruppen|;
1665 $sth = $dbh->prepare($query);
1666 $sth->execute || $self->dberror($query);
1668 $self->{BUCHUNGSGRUPPEN} = [];
1669 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1670 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1675 $main::lxdebug->leave_sub();
1678 # this is only used for reports
1679 sub all_departments {
1680 $main::lxdebug->enter_sub();
1682 my ($self, $myconfig, $table) = @_;
1684 my $dbh = $self->dbconnect($myconfig);
1685 my $where = "1 = 1";
1687 if (defined $table) {
1688 if ($table eq 'customer') {
1689 $where = " d.role = 'P'";
1693 my $query = qq|SELECT d.id, d.description
1697 my $sth = $dbh->prepare($query);
1698 $sth->execute || $self->dberror($query);
1700 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1701 push @{ $self->{all_departments} }, $ref;
1707 $main::lxdebug->leave_sub();
1711 $main::lxdebug->enter_sub();
1713 my ($self, $module, $myconfig, $table) = @_;
1715 $self->all_vc($myconfig, $table, $module);
1717 # get last customers or vendors
1720 my $dbh = $self->dbconnect($myconfig);
1725 my $transdate = "current_date";
1726 if ($self->{transdate}) {
1727 $transdate = qq|'$self->{transdate}'|;
1730 # now get the account numbers
1731 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1732 FROM chart c, taxkeys tk
1733 WHERE c.link LIKE '%$module%' AND c.id=tk.chart_id AND tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
1736 $sth = $dbh->prepare($query);
1737 $sth->execute || $self->dberror($query);
1739 $self->{accounts} = "";
1740 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1742 foreach my $key (split(/:/, $ref->{link})) {
1743 if ($key =~ /$module/) {
1745 # cross reference for keys
1746 $xkeyref{ $ref->{accno} } = $key;
1748 push @{ $self->{"${module}_links"}{$key} },
1749 { accno => $ref->{accno},
1750 description => $ref->{description},
1751 taxkey => $ref->{taxkey_id},
1752 tax_id => $ref->{tax_id} };
1754 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1760 # get taxkeys and description
1761 $query = qq|SELECT id, taxkey, taxdescription
1763 $sth = $dbh->prepare($query);
1764 $sth->execute || $self->dberror($query);
1766 $ref = $sth->fetchrow_hashref(NAME_lc);
1768 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1769 push @{ $self->{TAXKEY} }, $ref;
1776 $query = qq|SELECT id, description
1778 $sth = $dbh->prepare($query);
1779 $sth->execute || $self->dberror($query);
1782 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1783 push @{ $self->{TAXZONE} }, $ref;
1787 if (($module eq "AP") || ($module eq "AR")) {
1789 # get tax rates and description
1790 $query = qq| SELECT * FROM tax t|;
1791 $sth = $dbh->prepare($query);
1792 $sth->execute || $self->dberror($query);
1794 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1795 push @{ $self->{TAX} }, $ref;
1801 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1803 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1804 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1805 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1806 c.name AS $table, a.department_id, d.description AS department,
1807 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1808 a.employee_id, e.name AS employee, a.gldate, a.type
1810 JOIN $table c ON (a.${table}_id = c.id)
1811 LEFT JOIN employee e ON (e.id = a.employee_id)
1812 LEFT JOIN department d ON (d.id = a.department_id)
1813 WHERE a.id = $self->{id}|;
1814 $sth = $dbh->prepare($query);
1815 $sth->execute || $self->dberror($query);
1817 $ref = $sth->fetchrow_hashref(NAME_lc);
1818 foreach $key (keys %$ref) {
1819 $self->{$key} = $ref->{$key};
1824 my $transdate = "current_date";
1825 if ($self->{transdate}) {
1826 $transdate = qq|'$self->{transdate}'|;
1829 # now get the account numbers
1830 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1831 FROM chart c, taxkeys tk
1832 WHERE c.link LIKE '%$module%' AND (((tk.chart_id=c.id) AND NOT(c.link like '%_tax%')) OR (NOT(tk.chart_id=c.id) AND (c.link like '%_tax%'))) AND (((tk.id = (SELECT id from taxkeys where taxkeys.chart_id =c.id AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)) AND NOT(c.link like '%_tax%')) OR (c.link like '%_tax%'))
1835 $sth = $dbh->prepare($query);
1836 $sth->execute || $self->dberror($query);
1838 $self->{accounts} = "";
1839 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1841 foreach my $key (split(/:/, $ref->{link})) {
1842 if ($key =~ /$module/) {
1844 # cross reference for keys
1845 $xkeyref{ $ref->{accno} } = $key;
1847 push @{ $self->{"${module}_links"}{$key} },
1848 { accno => $ref->{accno},
1849 description => $ref->{description},
1850 taxkey => $ref->{taxkey_id},
1851 tax_id => $ref->{tax_id} };
1853 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1859 # get amounts from individual entries
1860 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1861 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
1863 JOIN chart c ON (c.id = a.chart_id)
1864 LEFT JOIN project p ON (p.id = a.project_id)
1865 LEFT JOIN tax t ON (t.id=(SELECT tk.tax_id from taxkeys tk WHERE (tk.taxkey_id=a.taxkey) AND ((CASE WHEN a.chart_id IN (SELECT chart_id FROM taxkeys WHERE taxkey_id=a.taxkey) THEN tk.chart_id=a.chart_id ELSE 1=1 END) OR (c.link='%tax%')) AND startdate <=a.transdate ORDER BY startdate DESC LIMIT 1))
1866 WHERE a.trans_id = $self->{id}
1867 AND a.fx_transaction = '0'
1868 ORDER BY a.oid,a.transdate|;
1869 $sth = $dbh->prepare($query);
1870 $sth->execute || $self->dberror($query);
1872 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1874 # get exchangerate for currency
1875 $self->{exchangerate} =
1876 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1880 # store amounts in {acc_trans}{$key} for multiple accounts
1881 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1882 $ref->{exchangerate} =
1883 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1885 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1888 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1889 $ref->{amount} *= -1;
1891 $ref->{index} = $index;
1893 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1897 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1898 (SELECT c.accno FROM chart c
1899 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1900 (SELECT c.accno FROM chart c
1901 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1903 $sth = $dbh->prepare($query);
1904 $sth->execute || $self->dberror($query);
1906 $ref = $sth->fetchrow_hashref(NAME_lc);
1907 map { $self->{$_} = $ref->{$_} } keys %$ref;
1913 $query = qq|SELECT current_date AS transdate,
1914 d.curr AS currencies, d.closedto, d.revtrans,
1915 (SELECT c.accno FROM chart c
1916 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1917 (SELECT c.accno FROM chart c
1918 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1920 $sth = $dbh->prepare($query);
1921 $sth->execute || $self->dberror($query);
1923 $ref = $sth->fetchrow_hashref(NAME_lc);
1924 map { $self->{$_} = $ref->{$_} } keys %$ref;
1927 if ($self->{"$self->{vc}_id"}) {
1929 # only setup currency
1930 ($self->{currency}) = split(/:/, $self->{currencies});
1934 $self->lastname_used($dbh, $myconfig, $table, $module);
1936 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1938 # get exchangerate for currency
1939 $self->{exchangerate} =
1940 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1951 $main::lxdebug->leave_sub();
1955 $main::lxdebug->enter_sub();
1957 my ($self, $dbh, $myconfig, $table, $module) = @_;
1959 my $arap = ($table eq 'customer') ? "ar" : "ap";
1960 my $where = "1 = 1";
1962 if ($self->{type} =~ /_order/) {
1964 $where = "quotation = '0'";
1966 if ($self->{type} =~ /_quotation/) {
1968 $where = "quotation = '1'";
1971 my $query = qq|SELECT MAX(id) FROM $arap
1973 AND ${table}_id > 0|;
1974 my $sth = $dbh->prepare($query);
1975 $sth->execute || $self->dberror($query);
1977 my ($trans_id) = $sth->fetchrow_array;
1981 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1982 current_date + ct.terms AS duedate, a.department_id,
1983 d.description AS department
1985 JOIN $table ct ON (a.${table}_id = ct.id)
1986 LEFT JOIN department d ON (a.department_id = d.id)
1987 WHERE a.id = $trans_id|;
1988 $sth = $dbh->prepare($query);
1989 $sth->execute || $self->dberror($query);
1991 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1992 $self->{duedate}, $self->{department_id}, $self->{department})
1993 = $sth->fetchrow_array;
1996 $main::lxdebug->leave_sub();
2000 $main::lxdebug->enter_sub();
2002 my ($self, $myconfig, $thisdate, $days) = @_;
2004 my $dbh = $self->dbconnect($myconfig);
2009 my $dateformat = $myconfig->{dateformat};
2010 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2012 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
2014 $sth = $dbh->prepare($query);
2015 $sth->execute || $self->dberror($query);
2017 $query = qq|SELECT current_date AS thisdate
2019 $sth = $dbh->prepare($query);
2020 $sth->execute || $self->dberror($query);
2023 ($thisdate) = $sth->fetchrow_array;
2028 $main::lxdebug->leave_sub();
2034 $main::lxdebug->enter_sub();
2036 my ($self, $string) = @_;
2038 if ($string !~ /%/) {
2039 $string = "%$string%";
2042 $string =~ s/\'/\'\'/g;
2044 $main::lxdebug->leave_sub();
2050 $main::lxdebug->enter_sub();
2052 my ($self, $flds, $new, $count, $numrows) = @_;
2056 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2062 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2064 $j = $item->{ndx} - 1;
2065 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2069 for $i ($count + 1 .. $numrows) {
2070 map { delete $self->{"${_}_$i"} } @{$flds};
2073 $main::lxdebug->leave_sub();
2077 $main::lxdebug->enter_sub();
2079 my ($self, $myconfig) = @_;
2083 my $dbh = $self->dbconnect_noauto($myconfig);
2085 my $query = qq|DELETE FROM status
2086 WHERE formname = '$self->{formname}'
2088 my $sth = $dbh->prepare($query) || $self->dberror($query);
2090 if ($self->{formname} =~ /(check|receipt)/) {
2091 for $i (1 .. $self->{rowcount}) {
2092 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2096 $sth->execute($self->{id}) || $self->dberror($query);
2100 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2101 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2103 my %queued = split / /, $self->{queued};
2105 if ($self->{formname} =~ /(check|receipt)/) {
2107 # this is a check or receipt, add one entry for each lineitem
2108 my ($accno) = split /--/, $self->{account};
2109 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2110 chart_id) VALUES (?, '$printed',
2111 '$queued{$self->{formname}}', '$self->{prinform}',
2112 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2113 $sth = $dbh->prepare($query) || $self->dberror($query);
2115 for $i (1 .. $self->{rowcount}) {
2116 if ($self->{"checked_$i"}) {
2117 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2122 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2123 spoolfile, formname)
2124 VALUES ($self->{id}, '$printed', '$emailed',
2125 '$queued{$self->{formname}}', '$self->{formname}')|;
2126 $dbh->do($query) || $self->dberror($query);
2132 $main::lxdebug->leave_sub();
2136 $main::lxdebug->enter_sub();
2138 my ($self, $dbh) = @_;
2140 my ($query, $printed, $emailed);
2142 my $formnames = $self->{printed};
2143 my $emailforms = $self->{emailed};
2145 my $query = qq|DELETE FROM status
2146 WHERE formname = '$self->{formname}'
2147 AND trans_id = $self->{id}|;
2148 $dbh->do($query) || $self->dberror($query);
2150 # this only applies to the forms
2151 # checks and receipts are posted when printed or queued
2153 if ($self->{queued}) {
2154 my %queued = split / /, $self->{queued};
2156 foreach my $formname (keys %queued) {
2157 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2158 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2160 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2161 spoolfile, formname)
2162 VALUES ($self->{id}, '$printed', '$emailed',
2163 '$queued{$formname}', '$formname')|;
2164 $dbh->do($query) || $self->dberror($query);
2166 $formnames =~ s/$self->{formname}//;
2167 $emailforms =~ s/$self->{formname}//;
2172 # save printed, emailed info
2173 $formnames =~ s/^ +//g;
2174 $emailforms =~ s/^ +//g;
2177 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2178 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2180 foreach my $formname (keys %status) {
2181 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2182 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2184 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2185 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2186 $dbh->do($query) || $self->dberror($query);
2189 $main::lxdebug->leave_sub();
2192 sub update_defaults {
2193 $main::lxdebug->enter_sub();
2195 my ($self, $myconfig, $fld) = @_;
2197 my $dbh = $self->dbconnect_noauto($myconfig);
2198 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2199 my $sth = $dbh->prepare($query);
2201 $sth->execute || $self->dberror($query);
2202 my ($var) = $sth->fetchrow_array;
2207 $query = qq|UPDATE defaults
2209 $dbh->do($query) || $self->dberror($query);
2214 $main::lxdebug->leave_sub();
2219 sub update_business {
2220 $main::lxdebug->enter_sub();
2222 my ($self, $myconfig, $business_id) = @_;
2224 my $dbh = $self->dbconnect_noauto($myconfig);
2226 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2227 my $sth = $dbh->prepare($query);
2229 $sth->execute || $self->dberror($query);
2230 my ($var) = $sth->fetchrow_array;
2235 $query = qq|UPDATE business
2236 SET customernumberinit = '$var' WHERE id=$business_id|;
2237 $dbh->do($query) || $self->dberror($query);
2242 $main::lxdebug->leave_sub();
2248 $main::lxdebug->enter_sub();
2250 my ($self, $myconfig, $salesman) = @_;
2252 my $dbh = $self->dbconnect($myconfig);
2254 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2255 my $sth = $dbh->prepare($query);
2256 $sth->execute || $self->dberror($query);
2259 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2260 push(@{ $self->{salesman_list} }, $ref);
2264 $main::lxdebug->leave_sub();
2269 sub get_partsgroup {
2270 $main::lxdebug->enter_sub();
2272 my ($self, $myconfig, $p) = @_;
2274 my $dbh = $self->dbconnect($myconfig);
2276 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2278 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2280 if ($p->{searchitems} eq 'part') {
2282 WHERE p.inventory_accno_id > 0|;
2284 if ($p->{searchitems} eq 'service') {
2286 WHERE p.inventory_accno_id IS NULL|;
2288 if ($p->{searchitems} eq 'assembly') {
2290 WHERE p.assembly = '1'|;
2292 if ($p->{searchitems} eq 'labor') {
2294 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2298 ORDER BY partsgroup|;
2301 $query = qq|SELECT id, partsgroup FROM partsgroup
2302 ORDER BY partsgroup|;
2305 if ($p->{language_code}) {
2306 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2307 t.description AS translation
2309 JOIN parts p ON (p.partsgroup_id = pg.id)
2310 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2311 ORDER BY translation|;
2314 my $sth = $dbh->prepare($query);
2315 $sth->execute || $self->dberror($query);
2317 $self->{all_partsgroup} = ();
2318 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2319 push @{ $self->{all_partsgroup} }, $ref;
2323 $main::lxdebug->leave_sub();
2326 sub get_pricegroup {
2327 $main::lxdebug->enter_sub();
2329 my ($self, $myconfig, $p) = @_;
2331 my $dbh = $self->dbconnect($myconfig);
2333 my $query = qq|SELECT p.id, p.pricegroup
2337 ORDER BY pricegroup|;
2340 $query = qq|SELECT id, pricegroup FROM pricegroup
2341 ORDER BY pricegroup|;
2344 my $sth = $dbh->prepare($query);
2345 $sth->execute || $self->dberror($query);
2347 $self->{all_pricegroup} = ();
2348 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2349 push @{ $self->{all_pricegroup} }, $ref;
2354 $main::lxdebug->leave_sub();
2358 my ($self, $dbh, $myconfig, $audittrail) = @_;
2360 # table, $reference, $formname, $action, $id, $transdate) = @_;
2367 $dbh = $self->dbconnect($myconfig);
2371 # if we have an id add audittrail, otherwise get a new timestamp
2373 if ($audittrail->{id}) {
2375 $query = qq|SELECT audittrail FROM defaults|;
2377 if ($dbh->selectrow_array($query)) {
2378 my ($null, $employee_id) = $self->get_employee($dbh);
2380 if ($self->{audittrail} && !$myconfig) {
2381 chop $self->{audittrail};
2383 my @a = split /\|/, $self->{audittrail};
2387 my @flds = qw(tablename reference formname action transdate);
2389 # put into hash and remove dups
2391 $key = "$a[2]$a[3]";
2393 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2397 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2398 formname, action, employee_id, transdate)
2399 VALUES ($audittrail->{id}, ?, ?,
2400 ?, ?, $employee_id, ?)|;
2401 my $sth = $dbh->prepare($query) || $self->dberror($query);
2405 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2409 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2411 $sth->execute || $self->dberror;
2416 if ($audittrail->{transdate}) {
2417 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2418 formname, action, employee_id, transdate) VALUES (
2419 $audittrail->{id}, '$audittrail->{tablename}', |
2420 . $dbh->quote($audittrail->{reference}) . qq|,
2421 '$audittrail->{formname}', '$audittrail->{action}',
2422 $employee_id, '$audittrail->{transdate}')|;
2424 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2425 formname, action, employee_id) VALUES ($audittrail->{id},
2426 '$audittrail->{tablename}', |
2427 . $dbh->quote($audittrail->{reference}) . qq|,
2428 '$audittrail->{formname}', '$audittrail->{action}',
2435 $query = qq|SELECT current_timestamp FROM defaults|;
2436 my ($timestamp) = $dbh->selectrow_array($query);
2439 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2442 $dbh->disconnect if $disconnect;
2450 # usage $form->all_years($myconfig, [$dbh])
2451 # return list of all years where bookings found
2454 $main::lxdebug->enter_sub();
2456 my ($self, $myconfig, $dbh) = @_;
2460 $dbh = $self->dbconnect($myconfig);
2465 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
2466 (SELECT MAX(transdate) FROM acc_trans)
2468 my ($startdate, $enddate) = $dbh->selectrow_array($query);
2470 if ($myconfig->{dateformat} =~ /^yy/) {
2471 ($startdate) = split /\W/, $startdate;
2472 ($enddate) = split /\W/, $enddate;
2474 (@_) = split /\W/, $startdate;
2476 (@_) = split /\W/, $enddate;
2481 $startdate = substr($startdate,0,4);
2482 $enddate = substr($enddate,0,4);
2484 while ($enddate >= $startdate) {
2485 push @all_years, $enddate--;
2488 $dbh->disconnect if $disconnect;
2492 $main::lxdebug->leave_sub();