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 #======================================================================
49 $main::lxdebug->enter_sub(2);
53 my @pairs = split(/&/, $input);
56 my ($name, $value) = split(/=/, $_, 2);
57 $in{$name} = unescape(undef, $value);
60 $main::lxdebug->leave_sub(2);
65 sub _request_to_hash {
66 $main::lxdebug->enter_sub(2);
69 my ($i, $loc, $key, $val);
70 my (%ATTACH, $f, $header, $header_body, $len, $buf);
71 my ($boundary, @list, $size, $body, $x, $blah, $name);
73 if ($ENV{'CONTENT_TYPE'}
74 && ($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)) {
75 $boundary = quotemeta('--' . $1);
76 @list = split(/$boundary/, $input);
78 # For some reason there are always 2 extra, that are empty
81 for ($x = 1; $x <= $size; $x++) {
82 $header_body = $list[$x];
83 $header_body =~ /\r\n\r\n|\n\n/;
85 # Here we split the header and body
90 # Now we try to get the file name
92 $name =~ /name=\"(.+)\"/;
93 ($name, $blah) = split(/\"/, $1);
95 # If the form name is not attach, then we need to parse this like
97 if ($name ne "attach") {
98 $body =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
99 $ATTACH{$name} = $body;
101 # Otherwise it is an attachment and we need to finish it up
102 } elsif ($name eq "attach") {
103 $header =~ /filename=\"(.+)\"/;
104 $ATTACH{'FILE_NAME'} = $1;
105 $ATTACH{'FILE_NAME'} =~ s/\"//g;
106 $ATTACH{'FILE_NAME'} =~ s/\s//g;
107 $ATTACH{'FILE_CONTENT'} = $body;
109 for ($i = $x; $list[$i]; $i++) {
110 $list[$i] =~ s/^.+name=$//;
111 $list[$i] =~ /\"(\w+)\"/;
117 $main::lxdebug->leave_sub(2);
121 $main::lxdebug->leave_sub(2);
122 return _input_to_hash($input);
127 $main::lxdebug->enter_sub();
133 read(STDIN, $_, $ENV{CONTENT_LENGTH});
135 if ($ENV{QUERY_STRING}) {
136 $_ = $ENV{QUERY_STRING};
143 my %parameters = _request_to_hash($_);
144 map({ $self->{$_} = $parameters{$_}; } keys(%parameters));
146 $self->{action} = lc $self->{action};
147 $self->{action} =~ s/( |-|,|\#)/_/g;
149 $self->{version} = "2.4.1";
151 $main::lxdebug->leave_sub();
157 $main::lxdebug->enter_sub();
163 map { print "$_ = $self->{$_}\n" } (sort keys %{$self});
165 $main::lxdebug->leave_sub();
169 $main::lxdebug->enter_sub(2);
171 my ($self, $str, $beenthere) = @_;
173 # for Apache 2 we escape strings twice
174 #if (($ENV{SERVER_SOFTWARE} =~ /Apache\/2/) && !$beenthere) {
175 # $str = $self->escape($str, 1);
178 $str =~ s/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge;
180 $main::lxdebug->leave_sub(2);
186 $main::lxdebug->enter_sub(2);
188 my ($self, $str) = @_;
193 $str =~ s/%([0-9a-fA-Z]{2})/pack("c",hex($1))/eg;
195 $main::lxdebug->leave_sub(2);
201 my ($self, $str) = @_;
203 if ($str && !ref($str)) {
204 $str =~ s/\"/"/g;
212 my ($self, $str) = @_;
214 if ($str && !ref($str)) {
215 $str =~ s/"/\"/g;
223 $main::lxdebug->enter_sub(2);
225 my ($self, $str) = @_;
228 ('order' => ['"', '<', '>'],
234 map({ $str =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
236 $main::lxdebug->leave_sub(2);
246 print qq|<input type=hidden name="$_" value="|
247 . $self->quote($self->{$_})
251 delete $self->{header};
252 for (sort keys %$self) {
253 print qq|<input type=hidden name="$_" value="|
254 . $self->quote($self->{$_})
262 $main::lxdebug->enter_sub();
264 my ($self, $msg) = @_;
265 if ($ENV{HTTP_USER_AGENT}) {
267 $self->show_generic_error($msg);
271 if ($self->{error_function}) {
272 &{ $self->{error_function} }($msg);
278 $main::lxdebug->leave_sub();
282 $main::lxdebug->enter_sub();
284 my ($self, $msg) = @_;
286 if ($ENV{HTTP_USER_AGENT}) {
289 if (!$self->{header}) {
302 if ($self->{info_function}) {
303 &{ $self->{info_function} }($msg);
309 $main::lxdebug->leave_sub();
313 $main::lxdebug->enter_sub();
315 my ($self, $str, $cols, $maxrows) = @_;
319 map { $rows += int(((length) - 2) / $cols) + 1 } split /\r/, $str;
321 $maxrows = $rows unless defined $maxrows;
323 $main::lxdebug->leave_sub();
325 return ($rows > $maxrows) ? $maxrows : $rows;
329 $main::lxdebug->enter_sub();
331 my ($self, $msg) = @_;
333 $self->error("$msg\n" . $DBI::errstr);
335 $main::lxdebug->leave_sub();
339 $main::lxdebug->enter_sub();
341 my ($self, $name, $msg) = @_;
343 if ($self->{$name} =~ /^\s*$/) {
346 $main::lxdebug->leave_sub();
350 $main::lxdebug->enter_sub();
352 my ($self, $extra_code) = @_;
354 if ($self->{header}) {
355 $main::lxdebug->leave_sub();
359 my ($stylesheet, $favicon, $charset);
361 if ($ENV{HTTP_USER_AGENT}) {
363 if ($self->{stylesheet} && (-f "css/$self->{stylesheet}")) {
365 qq|<LINK REL="stylesheet" HREF="css/$self->{stylesheet}" TYPE="text/css" TITLE="Lx-Office stylesheet">
369 $self->{favicon} = "favicon.ico" unless $self->{favicon};
371 if ($self->{favicon} && (-f "$self->{favicon}")) {
373 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
377 if ($self->{charset}) {
379 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
382 if ($self->{landscape}) {
383 $pagelayout = qq|<style type="text/css">
384 \@page { size:landscape; }
387 if ($self->{fokus}) {
388 $fokus = qq|<script type="text/javascript">
390 function fokus(){document.$self->{fokus}.focus();}
397 if ($self->{jsscript} == 1) {
400 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
401 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
402 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
403 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
410 ? "$self->{title} - $self->{titlebar}"
413 foreach $item (@ { $self->{AJAX} }) {
414 $ajax .= $item->show_javascript();
416 print qq|Content-Type: text/html
420 <title>$self->{titlebar}</title>
428 <meta name="robots" content="noindex,nofollow" />
429 <script type="text/javascript" src="js/highlight_input.js"></script>
430 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
432 <script type="text/javascript" src="js/tabcontent.js">
434 /***********************************************
435 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
436 * This notice MUST stay intact for legal use
437 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
438 ***********************************************/
449 $main::lxdebug->leave_sub();
452 sub parse_html_template {
453 $main::lxdebug->enter_sub();
455 my ($self, $file, $additional_params) = @_;
458 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
459 $language = $main::language;
461 $language = $main::myconfig{"countrycode"};
464 if (-f "templates/webpages/${file}_${language}.html") {
465 if ((-f ".developer") &&
466 (-f "templates/webpages/${file}_master.html") &&
467 ((stat("templates/webpages/${file}_master.html"))[9] >
468 (stat("templates/webpages/${file}_${language}.html"))[9])) {
469 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
470 "Please re-run 'locales.pl' in 'locale/${language}'.";
471 print(qq|<pre>$info</pre>|);
475 $file = "templates/webpages/${file}_${language}.html";
476 } elsif (-f "templates/webpages/${file}.html") {
477 $file = "templates/webpages/${file}.html";
479 my $info = "Web page template '${file}' not found.\n" .
480 "Please re-run 'locales.pl' in 'locale/${language}'.";
481 print(qq|<pre>$info</pre>|);
485 my $template = HTML::Template->new("filename" => $file,
486 "die_on_bad_params" => 0,
488 "case_sensitive" => 1,
489 "loop_context_vars" => 1,
492 $additional_params = {} unless ($additional_params);
493 if ($self->{"DEBUG"}) {
494 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
497 if ($additional_params->{"DEBUG"}) {
498 $additional_params->{"DEBUG"} =
499 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
502 if (%main::myconfig) {
503 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
504 my $jsc_dateformat = $main::myconfig{"dateformat"};
505 $jsc_dateformat =~ s/d+/\%d/gi;
506 $jsc_dateformat =~ s/m+/\%m/gi;
507 $jsc_dateformat =~ s/y+/\%Y/gi;
508 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
511 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
512 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
513 $additional_params->{"conf_latex_templates"} = $main::latex;
514 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
516 my @additional_param_names = keys(%{$additional_params});
517 foreach my $key ($template->param()) {
518 my $param = $self->{$key};
519 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
520 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
521 $template->param($key => $param);
524 my $output = $template->output();
526 $main::lxdebug->leave_sub();
531 sub show_generic_error {
532 my ($self, $error, $title, $action) = @_;
535 $add_params->{"title"} = $title if ($title);
536 $self->{"label_error"} = $error;
540 map({ delete($self->{$_}); } qw(action));
541 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
542 if (!ref($self->{$_})); }
544 $add_params->{"SHOW_BUTTON"} = 1;
545 $add_params->{"BUTTON_LABEL"} = $action;
547 $add_params->{"VARIABLES"} = \@vars;
550 print($self->parse_html_template("generic/error", $add_params));
552 die("Error: $error\n");
555 sub show_generic_information {
556 my ($self, $error, $title) = @_;
559 $add_params->{"title"} = $title if ($title);
560 $self->{"label_information"} = $error;
563 print($self->parse_html_template("generic/information", $add_params));
565 die("Information: $error\n");
568 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
569 # changed it to accept an arbitrary number of triggers - sschoeling
571 $main::lxdebug->enter_sub();
574 my $myconfig = shift;
577 # set dateform for jsscript
579 $ifFormat = "%d.%m.%Y";
580 if ($myconfig->{dateformat} eq "dd.mm.yy") {
581 $ifFormat = "%d.%m.%Y";
583 if ($myconfig->{dateformat} eq "dd-mm-yy") {
584 $ifFormat = "%d-%m-%Y";
586 if ($myconfig->{dateformat} eq "dd/mm/yy") {
587 $ifFormat = "%d/%m/%Y";
589 if ($myconfig->{dateformat} eq "mm/dd/yy") {
590 $ifFormat = "%m/%d/%Y";
592 if ($myconfig->{dateformat} eq "mm-dd-yy") {
593 $ifFormat = "%m-%d-%Y";
595 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
596 $ifFormat = "%Y-%m-%d";
608 inputField : "| . (shift) . qq|",
609 ifFormat :"$ifFormat",
610 align : "| . (shift) . qq|",
611 button : "| . (shift) . qq|"
617 <script type="text/javascript">
618 <!--| . join("", @triggers) . qq|//-->
622 $main::lxdebug->leave_sub();
625 } #end sub write_trigger
628 $main::lxdebug->enter_sub();
630 my ($self, $msg) = @_;
632 if ($self->{callback}) {
634 ($script, $argv) = split(/\?/, $self->{callback});
635 exec("perl", "$script", $argv);
643 $main::lxdebug->leave_sub();
646 # sort of columns removed - empty sub
648 $main::lxdebug->enter_sub();
650 my ($self, @columns) = @_;
652 $main::lxdebug->leave_sub();
658 $main::lxdebug->enter_sub(2);
660 my ($self, $myconfig, $amount, $places, $dash) = @_;
665 my $neg = ($amount =~ s/-//);
667 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
669 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
670 my @p = split(/\./, $amount); # split amount at decimal point
672 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
675 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
678 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
679 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
680 ($neg ? "-$amount" : "$amount" ) ;
684 $main::lxdebug->leave_sub(2);
689 $main::lxdebug->enter_sub(2);
691 my ($self, $myconfig, $amount) = @_;
693 if ($myconfig->{in_numberformat} == 1) {
694 # Extra input number format 1000.00 or 1000,00
696 $amount = scalar reverse $amount;
697 $amount =~ s/\./DOT/;
699 $amount =~ s/DOT/\./;
700 $amount = scalar reverse $amount;
701 $main::lxdebug->leave_sub(2);
702 return ($amount * 1);
705 if ( ($myconfig->{numberformat} eq '1.000,00')
706 || ($myconfig->{numberformat} eq '1000,00')) {
711 if ($myconfig->{numberformat} eq "1'000.00") {
717 $main::lxdebug->leave_sub(2);
719 return ($amount * 1);
723 $main::lxdebug->enter_sub(2);
725 my ($self, $amount, $places) = @_;
728 # Rounding like "Kaufmannsrunden"
729 # Descr. http://de.wikipedia.org/wiki/Rundung
731 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
734 $amount = $amount * (10**($places));
735 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
737 $main::lxdebug->leave_sub(2);
739 return $round_amount;
744 $main::lxdebug->enter_sub();
746 my ($self, $myconfig, $userspath) = @_;
749 $self->{"cwd"} = getcwd();
750 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
752 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
753 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
755 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
756 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
757 } elsif (($self->{"format"} =~ /html/i) ||
758 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
759 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
760 } elsif (($self->{"format"} =~ /xml/i) ||
761 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
762 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
763 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
764 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
765 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
766 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
767 } elsif ( defined $self->{'format'}) {
768 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
769 } elsif ( $self->{'format'} eq '' ) {
770 $self->error("No Outputformat given: $self->{'format'}");
771 } else { #Catch the rest
772 $self->error("Outputformat not defined: $self->{'format'}");
775 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
776 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
778 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
779 qw(email tel fax name signature company address businessnumber
780 co_ustid taxnumber duns));
781 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
782 qw(company address signature));
784 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
786 # OUT is used for the media, screen, printer, email
787 # for postscript we store a copy in a temporary file
789 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
790 if ($template->uses_temp_file() || $self->{media} eq 'email') {
792 $self->{OUT} = ">$self->{tmpfile}";
796 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
798 open(OUT, ">-") or $self->error("STDOUT : $!");
802 if (!$template->parse(*OUT)) {
804 $self->error("$self->{IN} : " . $template->get_error());
809 if ($template->uses_temp_file() || $self->{media} eq 'email') {
811 if ($self->{media} eq 'email') {
815 my $mail = new Mailer;
817 map { $mail->{$_} = $self->{$_} }
818 qw(cc bcc subject message version format charset);
819 $mail->{to} = qq|$self->{email}|;
820 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
821 $mail->{fileid} = "$fileid.";
822 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
824 # if we send html or plain text inline
825 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
826 $mail->{contenttype} = "text/html";
828 $mail->{message} =~ s/\r\n/<br>\n/g;
829 $myconfig->{signature} =~ s/\\n/<br>\n/g;
830 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
832 open(IN, $self->{tmpfile})
833 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
835 $mail->{message} .= $_;
842 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($self->{do_not_attach});
844 $mail->{message} =~ s/\r\n/\n/g;
845 $myconfig->{signature} =~ s/\\n/\n/g;
846 $mail->{message} .= "\n-- \n$myconfig->{signature}";
850 my $err = $mail->send($out);
851 $self->error($self->cleanup . "$err") if ($err);
857 my $numbytes = (-s $self->{tmpfile});
858 open(IN, $self->{tmpfile})
859 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
861 $self->{copies} = 1 unless $self->{media} eq 'printer';
863 chdir("$self->{cwd}");
864 #print(STDERR "Kopien $self->{copies}\n");
865 #print(STDERR "OUT $self->{OUT}\n");
866 for my $i (1 .. $self->{copies}) {
868 open(OUT, $self->{OUT})
869 or $self->error($self->cleanup . "$self->{OUT} : $!");
871 $self->{attachment_filename} = $self->{tmpfile} if ($self->{attachment_filename} eq '');
873 print qq|Content-Type: | . $template->get_mime_type() . qq|
874 Content-Disposition: attachment; filename="$self->{attachment_filename}"
875 Content-Length: $numbytes
879 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
899 chdir("$self->{cwd}");
900 $main::lxdebug->leave_sub();
904 $main::lxdebug->enter_sub();
908 chdir("$self->{tmpdir}");
911 if (-f "$self->{tmpfile}.err") {
912 open(FH, "$self->{tmpfile}.err");
917 if ($self->{tmpfile}) {
918 $self->{tmpfile} =~ s|.*/||g;
920 $self->{tmpfile} =~ s/\.\w+$//g;
921 my $tmpfile = $self->{tmpfile};
922 unlink(<$tmpfile.*>);
925 chdir("$self->{cwd}");
927 $main::lxdebug->leave_sub();
933 $main::lxdebug->enter_sub();
935 my ($self, $date, $myconfig) = @_;
937 if ($date && $date =~ /\D/) {
939 if ($myconfig->{dateformat} =~ /^yy/) {
940 ($yy, $mm, $dd) = split /\D/, $date;
942 if ($myconfig->{dateformat} =~ /^mm/) {
943 ($mm, $dd, $yy) = split /\D/, $date;
945 if ($myconfig->{dateformat} =~ /^dd/) {
946 ($dd, $mm, $yy) = split /\D/, $date;
951 $yy = ($yy < 70) ? $yy + 2000 : $yy;
952 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
954 $dd = "0$dd" if ($dd < 10);
955 $mm = "0$mm" if ($mm < 10);
960 $main::lxdebug->leave_sub();
965 # Database routines used throughout
968 $main::lxdebug->enter_sub();
970 my ($self, $myconfig) = @_;
972 # connect to database
974 DBI->connect($myconfig->{dbconnect},
975 $myconfig->{dbuser}, $myconfig->{dbpasswd})
979 if ($myconfig->{dboptions}) {
980 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
983 $main::lxdebug->leave_sub();
988 sub dbconnect_noauto {
989 $main::lxdebug->enter_sub();
991 my ($self, $myconfig) = @_;
993 # connect to database
995 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
996 $myconfig->{dbpasswd}, { AutoCommit => 0 })
1000 if ($myconfig->{dboptions}) {
1001 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1004 $main::lxdebug->leave_sub();
1009 sub update_balance {
1010 $main::lxdebug->enter_sub();
1012 my ($self, $dbh, $table, $field, $where, $value) = @_;
1014 # if we have a value, go do it
1017 # retrieve balance from table
1018 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1019 my $sth = $dbh->prepare($query);
1021 $sth->execute || $self->dberror($query);
1022 my ($balance) = $sth->fetchrow_array;
1028 $query = "UPDATE $table SET $field = $balance WHERE $where";
1029 $dbh->do($query) || $self->dberror($query);
1031 $main::lxdebug->leave_sub();
1034 sub update_exchangerate {
1035 $main::lxdebug->enter_sub();
1037 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1039 # some sanity check for currency
1041 $main::lxdebug->leave_sub();
1045 my $query = qq|SELECT e.curr FROM exchangerate e
1046 WHERE e.curr = '$curr'
1047 AND e.transdate = '$transdate'
1049 my $sth = $dbh->prepare($query);
1050 $sth->execute || $self->dberror($query);
1053 if ($buy != 0 && $sell != 0) {
1054 $set = "buy = $buy, sell = $sell";
1055 } elsif ($buy != 0) {
1056 $set = "buy = $buy";
1057 } elsif ($sell != 0) {
1058 $set = "sell = $sell";
1061 if ($sth->fetchrow_array) {
1062 $query = qq|UPDATE exchangerate
1064 WHERE curr = '$curr'
1065 AND transdate = '$transdate'|;
1067 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1068 VALUES ('$curr', $buy, $sell, '$transdate')|;
1071 $dbh->do($query) || $self->dberror($query);
1073 $main::lxdebug->leave_sub();
1076 sub save_exchangerate {
1077 $main::lxdebug->enter_sub();
1079 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1081 my $dbh = $self->dbconnect($myconfig);
1083 my ($buy, $sell) = (0, 0);
1084 $buy = $rate if $fld eq 'buy';
1085 $sell = $rate if $fld eq 'sell';
1087 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1091 $main::lxdebug->leave_sub();
1094 sub get_exchangerate {
1095 $main::lxdebug->enter_sub();
1097 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1099 unless ($transdate) {
1100 $main::lxdebug->leave_sub();
1104 my $query = qq|SELECT e.$fld FROM exchangerate e
1105 WHERE e.curr = '$curr'
1106 AND e.transdate = '$transdate'|;
1107 my $sth = $dbh->prepare($query);
1108 $sth->execute || $self->dberror($query);
1110 my ($exchangerate) = $sth->fetchrow_array;
1113 if ($exchangerate == 0) {
1117 $main::lxdebug->leave_sub();
1119 return $exchangerate;
1122 sub set_payment_options {
1123 $main::lxdebug->enter_sub();
1125 my ($self, $myconfig, $transdate) = @_;
1127 if ($self->{payment_id}) {
1129 my $dbh = $self->dbconnect($myconfig);
1132 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1133 WHERE p.id = $self->{payment_id}|;
1134 my $sth = $dbh->prepare($query);
1135 $sth->execute || $self->dberror($query);
1137 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1139 if ($transdate eq "") {
1140 if ($self->{invdate}) {
1141 $transdate = $self->{invdate};
1143 $transdate = $self->{transdate};
1148 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1150 my $sth = $dbh->prepare($query);
1151 $sth->execute || $self->dberror($query);
1152 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1155 my $total = ($self->{invtotal}) ? $self->{invtotal} : $self->{ordtotal};
1157 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $total) * $self->{percent_skonto}), 2);
1159 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1160 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1161 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1162 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1163 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1164 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1165 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1166 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1167 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1168 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1173 $main::lxdebug->leave_sub();
1177 sub check_exchangerate {
1178 $main::lxdebug->enter_sub();
1180 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1182 unless ($transdate) {
1183 $main::lxdebug->leave_sub();
1187 my $dbh = $self->dbconnect($myconfig);
1189 my $query = qq|SELECT e.$fld FROM exchangerate e
1190 WHERE e.curr = '$currency'
1191 AND e.transdate = '$transdate'|;
1192 my $sth = $dbh->prepare($query);
1193 $sth->execute || $self->dberror($query);
1195 my ($exchangerate) = $sth->fetchrow_array;
1199 $main::lxdebug->leave_sub();
1201 return $exchangerate;
1204 sub get_template_language {
1205 $main::lxdebug->enter_sub();
1207 my ($self, $myconfig) = @_;
1209 my $template_code = "";
1211 if ($self->{language_id}) {
1213 my $dbh = $self->dbconnect($myconfig);
1216 my $query = qq|SELECT l.template_code FROM language l
1217 WHERE l.id = $self->{language_id}|;
1218 my $sth = $dbh->prepare($query);
1219 $sth->execute || $self->dberror($query);
1221 ($template_code) = $sth->fetchrow_array;
1226 $main::lxdebug->leave_sub();
1228 return $template_code;
1231 sub get_printer_code {
1232 $main::lxdebug->enter_sub();
1234 my ($self, $myconfig) = @_;
1236 my $template_code = "";
1238 if ($self->{printer_id}) {
1240 my $dbh = $self->dbconnect($myconfig);
1243 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1244 WHERE p.id = $self->{printer_id}|;
1245 my $sth = $dbh->prepare($query);
1246 $sth->execute || $self->dberror($query);
1248 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1253 $main::lxdebug->leave_sub();
1255 return $template_code;
1259 $main::lxdebug->enter_sub();
1261 my ($self, $myconfig) = @_;
1263 my $template_code = "";
1265 if ($self->{shipto_id}) {
1267 my $dbh = $self->dbconnect($myconfig);
1270 my $query = qq|SELECT s.* FROM shipto s
1271 WHERE s.shipto_id = $self->{shipto_id}|;
1272 my $sth = $dbh->prepare($query);
1273 $sth->execute || $self->dberror($query);
1274 $ref = $sth->fetchrow_hashref(NAME_lc);
1275 map { $self->{$_} = $ref->{$_} } keys %$ref;
1280 $main::lxdebug->leave_sub();
1285 $main::lxdebug->enter_sub();
1287 my ($self, $dbh, $id, $module) = @_;
1291 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1293 if ($self->{"shipto$item"}) {
1294 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1296 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1299 if ($self->{shipto_id}) {
1300 my $query = qq| UPDATE shipto set
1301 shiptoname = '$self->{shiptoname}',
1302 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1303 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1304 shiptostreet = '$self->{shiptostreet}',
1305 shiptozipcode = '$self->{shiptozipcode}',
1306 shiptocity = '$self->{shiptocity}',
1307 shiptocountry = '$self->{shiptocountry}',
1308 shiptocontact = '$self->{shiptocontact}',
1309 shiptophone = '$self->{shiptophone}',
1310 shiptofax = '$self->{shiptofax}',
1311 shiptoemail = '$self->{shiptoemail}'
1312 WHERE shipto_id = $self->{shipto_id}|;
1313 $dbh->do($query) || $self->dberror($query);
1316 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1317 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1318 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1319 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1320 '$self->{shiptozipcode}', '$self->{shiptocity}',
1321 '$self->{shiptocountry}', '$self->{shiptocontact}',
1322 '$self->{shiptophone}', '$self->{shiptofax}',
1323 '$self->{shiptoemail}', '$module')|;
1324 $dbh->do($query) || $self->dberror($query);
1328 $main::lxdebug->leave_sub();
1332 $main::lxdebug->enter_sub();
1334 my ($self, $dbh) = @_;
1336 my $query = qq|SELECT e.id, e.name FROM employee e
1337 WHERE e.login = '$self->{login}'|;
1338 my $sth = $dbh->prepare($query);
1339 $sth->execute || $self->dberror($query);
1341 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1342 $self->{employee_id} *= 1;
1346 $main::lxdebug->leave_sub();
1350 $main::lxdebug->enter_sub();
1352 my ($self, $myconfig) = @_;
1354 my $dbh = $self->dbconnect($myconfig);
1355 my $query = qq|SELECT current_date+terms_netto FROM payment_terms
1356 WHERE id = '$self->{payment_id}'|;
1357 my $sth = $dbh->prepare($query);
1358 $sth->execute || $self->dberror($query);
1360 ($self->{duedate}) = $sth->fetchrow_array;
1364 $main::lxdebug->leave_sub();
1367 # get other contact for transaction and form - html/tex
1369 $main::lxdebug->enter_sub();
1371 my ($self, $dbh, $id) = @_;
1373 my $query = qq|SELECT c.*
1376 $sth = $dbh->prepare($query);
1377 $sth->execute || $self->dberror($query);
1379 $ref = $sth->fetchrow_hashref(NAME_lc);
1381 push @{ $self->{$_} }, $ref;
1384 $main::lxdebug->leave_sub();
1387 # get contacts for id, if no contact return {"","","","",""}
1389 $main::lxdebug->enter_sub();
1391 my ($self, $dbh, $id) = @_;
1393 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1395 WHERE cp_cv_id=$id|;
1396 my $sth = $dbh->prepare($query);
1397 $sth->execute || $self->dberror($query);
1400 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1401 push @{ $self->{all_contacts} }, $ref;
1406 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1409 $main::lxdebug->leave_sub();
1412 # this sub gets the id and name from $table
1414 $main::lxdebug->enter_sub();
1416 my ($self, $myconfig, $table) = @_;
1418 # connect to database
1419 my $dbh = $self->dbconnect($myconfig);
1421 my $name = $self->like(lc $self->{$table});
1422 my $customernumber = $self->like(lc $self->{customernumber});
1424 if ($self->{customernumber} ne "") {
1425 $query = qq~SELECT c.id, c.name,
1426 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1428 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1431 $query = qq~SELECT c.id, c.name,
1432 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1434 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1438 if ($self->{openinvoices}) {
1439 $query = qq~SELECT DISTINCT c.id, c.name,
1440 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1441 FROM $self->{arap} a
1442 JOIN $table c ON (a.${table}_id = c.id)
1443 WHERE NOT a.amount = a.paid
1444 AND lower(c.name) LIKE '$name'
1447 my $sth = $dbh->prepare($query);
1449 $sth->execute || $self->dberror($query);
1452 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1453 push(@{ $self->{name_list} }, $ref);
1459 $main::lxdebug->leave_sub();
1464 # the selection sub is used in the AR, AP, IS, IR and OE module
1467 $main::lxdebug->enter_sub();
1469 my ($self, $myconfig, $table, $module) = @_;
1472 my $dbh = $self->dbconnect($myconfig);
1474 my $query = qq|SELECT count(*) FROM $table|;
1475 my $sth = $dbh->prepare($query);
1476 $sth->execute || $self->dberror($query);
1477 my ($count) = $sth->fetchrow_array;
1480 # build selection list
1481 if ($count < $myconfig->{vclimit}) {
1482 $query = qq|SELECT id, name
1483 FROM $table WHERE not obsolete
1485 $sth = $dbh->prepare($query);
1486 $sth->execute || $self->dberror($query);
1488 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1489 push @{ $self->{"all_$table"} }, $ref;
1497 $self->get_employee($dbh);
1499 # setup sales contacts
1500 $query = qq|SELECT e.id, e.name
1503 AND NOT e.id = $self->{employee_id}|;
1504 $sth = $dbh->prepare($query);
1505 $sth->execute || $self->dberror($query);
1507 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1508 push @{ $self->{all_employees} }, $ref;
1513 push @{ $self->{all_employees} },
1514 { id => $self->{employee_id},
1515 name => $self->{employee} };
1517 # sort the whole thing
1518 @{ $self->{all_employees} } =
1519 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1521 if ($module eq 'AR') {
1523 # prepare query for departments
1524 $query = qq|SELECT d.id, d.description
1530 $query = qq|SELECT d.id, d.description
1535 $sth = $dbh->prepare($query);
1536 $sth->execute || $self->dberror($query);
1538 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1539 push @{ $self->{all_departments} }, $ref;
1544 $query = qq|SELECT id, description
1547 $sth = $dbh->prepare($query);
1548 $sth->execute || $self->dberror($query);
1550 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1551 push @{ $self->{languages} }, $ref;
1556 $query = qq|SELECT printer_description, id
1559 $sth = $dbh->prepare($query);
1560 $sth->execute || $self->dberror($query);
1562 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1563 push @{ $self->{printers} }, $ref;
1569 $query = qq|SELECT id, description
1572 $sth = $dbh->prepare($query);
1573 $sth->execute || $self->dberror($query);
1575 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1576 push @{ $self->{payment_terms} }, $ref;
1580 $main::lxdebug->leave_sub();
1584 sub language_payment {
1585 $main::lxdebug->enter_sub();
1587 my ($self, $myconfig) = @_;
1588 undef $self->{languages};
1589 undef $self->{payment_terms};
1590 undef $self->{printers};
1593 my $dbh = $self->dbconnect($myconfig);
1595 my $query = qq|SELECT id, description
1598 my $sth = $dbh->prepare($query);
1599 $sth->execute || $self->dberror($query);
1601 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1602 push @{ $self->{languages} }, $ref;
1607 $query = qq|SELECT printer_description, id
1610 $sth = $dbh->prepare($query);
1611 $sth->execute || $self->dberror($query);
1613 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1614 push @{ $self->{printers} }, $ref;
1619 $query = qq|SELECT id, description
1622 $sth = $dbh->prepare($query);
1623 $sth->execute || $self->dberror($query);
1625 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1626 push @{ $self->{payment_terms} }, $ref;
1630 # get buchungsgruppen
1631 $query = qq|SELECT id, description
1632 FROM buchungsgruppen|;
1633 $sth = $dbh->prepare($query);
1634 $sth->execute || $self->dberror($query);
1636 $self->{BUCHUNGSGRUPPEN} = [];
1637 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1638 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1643 $main::lxdebug->leave_sub();
1646 # this is only used for reports
1647 sub all_departments {
1648 $main::lxdebug->enter_sub();
1650 my ($self, $myconfig, $table) = @_;
1652 my $dbh = $self->dbconnect($myconfig);
1653 my $where = "1 = 1";
1655 if (defined $table) {
1656 if ($table eq 'customer') {
1657 $where = " d.role = 'P'";
1661 my $query = qq|SELECT d.id, d.description
1665 my $sth = $dbh->prepare($query);
1666 $sth->execute || $self->dberror($query);
1668 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1669 push @{ $self->{all_departments} }, $ref;
1675 $main::lxdebug->leave_sub();
1679 $main::lxdebug->enter_sub();
1681 my ($self, $module, $myconfig, $table) = @_;
1683 $self->all_vc($myconfig, $table, $module);
1685 # get last customers or vendors
1688 my $dbh = $self->dbconnect($myconfig);
1693 my $transdate = "current_date";
1694 if ($self->{transdate}) {
1695 $transdate = qq|'$self->{transdate}'|;
1698 # now get the account numbers
1699 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1700 FROM chart c, taxkeys tk
1701 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)
1704 $sth = $dbh->prepare($query);
1705 $sth->execute || $self->dberror($query);
1707 $self->{accounts} = "";
1708 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1710 foreach my $key (split(/:/, $ref->{link})) {
1711 if ($key =~ /$module/) {
1713 # cross reference for keys
1714 $xkeyref{ $ref->{accno} } = $key;
1716 push @{ $self->{"${module}_links"}{$key} },
1717 { accno => $ref->{accno},
1718 description => $ref->{description},
1719 taxkey => $ref->{taxkey_id},
1720 tax_id => $ref->{tax_id} };
1722 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1728 # get taxkeys and description
1729 $query = qq|SELECT id, taxkey, taxdescription
1731 $sth = $dbh->prepare($query);
1732 $sth->execute || $self->dberror($query);
1734 $ref = $sth->fetchrow_hashref(NAME_lc);
1736 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1737 push @{ $self->{TAXKEY} }, $ref;
1744 $query = qq|SELECT id, description
1746 $sth = $dbh->prepare($query);
1747 $sth->execute || $self->dberror($query);
1750 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1751 push @{ $self->{TAXZONE} }, $ref;
1755 if (($module eq "AP") || ($module eq "AR")) {
1757 # get tax rates and description
1758 $query = qq| SELECT * FROM tax t|;
1759 $sth = $dbh->prepare($query);
1760 $sth->execute || $self->dberror($query);
1762 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1763 push @{ $self->{TAX} }, $ref;
1769 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1771 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1772 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1773 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1774 c.name AS $table, a.department_id, d.description AS department,
1775 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1776 a.employee_id, e.name AS employee, a.gldate, a.type
1778 JOIN $table c ON (a.${table}_id = c.id)
1779 LEFT JOIN employee e ON (e.id = a.employee_id)
1780 LEFT JOIN department d ON (d.id = a.department_id)
1781 WHERE a.id = $self->{id}|;
1782 $sth = $dbh->prepare($query);
1783 $sth->execute || $self->dberror($query);
1785 $ref = $sth->fetchrow_hashref(NAME_lc);
1786 foreach $key (keys %$ref) {
1787 $self->{$key} = $ref->{$key};
1792 my $transdate = "current_date";
1793 if ($self->{transdate}) {
1794 $transdate = qq|'$self->{transdate}'|;
1797 # now get the account numbers
1798 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1799 FROM chart c, taxkeys tk
1800 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%'))
1803 $sth = $dbh->prepare($query);
1804 $sth->execute || $self->dberror($query);
1806 $self->{accounts} = "";
1807 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1809 foreach my $key (split(/:/, $ref->{link})) {
1810 if ($key =~ /$module/) {
1812 # cross reference for keys
1813 $xkeyref{ $ref->{accno} } = $key;
1815 push @{ $self->{"${module}_links"}{$key} },
1816 { accno => $ref->{accno},
1817 description => $ref->{description},
1818 taxkey => $ref->{taxkey_id},
1819 tax_id => $ref->{tax_id} };
1821 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1827 # get amounts from individual entries
1828 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1829 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
1831 JOIN chart c ON (c.id = a.chart_id)
1832 LEFT JOIN project p ON (p.id = a.project_id)
1833 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))
1834 WHERE a.trans_id = $self->{id}
1835 AND a.fx_transaction = '0'
1836 ORDER BY a.oid,a.transdate|;
1837 $sth = $dbh->prepare($query);
1838 $sth->execute || $self->dberror($query);
1840 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1842 # get exchangerate for currency
1843 $self->{exchangerate} =
1844 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1848 # store amounts in {acc_trans}{$key} for multiple accounts
1849 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1850 $ref->{exchangerate} =
1851 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1853 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1856 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1857 $ref->{amount} *= -1;
1859 $ref->{index} = $index;
1861 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1865 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1866 (SELECT c.accno FROM chart c
1867 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1868 (SELECT c.accno FROM chart c
1869 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1871 $sth = $dbh->prepare($query);
1872 $sth->execute || $self->dberror($query);
1874 $ref = $sth->fetchrow_hashref(NAME_lc);
1875 map { $self->{$_} = $ref->{$_} } keys %$ref;
1881 $query = qq|SELECT current_date AS transdate,
1882 d.curr AS currencies, d.closedto, d.revtrans,
1883 (SELECT c.accno FROM chart c
1884 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1885 (SELECT c.accno FROM chart c
1886 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1888 $sth = $dbh->prepare($query);
1889 $sth->execute || $self->dberror($query);
1891 $ref = $sth->fetchrow_hashref(NAME_lc);
1892 map { $self->{$_} = $ref->{$_} } keys %$ref;
1895 if ($self->{"$self->{vc}_id"}) {
1897 # only setup currency
1898 ($self->{currency}) = split(/:/, $self->{currencies});
1902 $self->lastname_used($dbh, $myconfig, $table, $module);
1904 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1906 # get exchangerate for currency
1907 $self->{exchangerate} =
1908 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1919 $main::lxdebug->leave_sub();
1923 $main::lxdebug->enter_sub();
1925 my ($self, $dbh, $myconfig, $table, $module) = @_;
1927 my $arap = ($table eq 'customer') ? "ar" : "ap";
1928 my $where = "1 = 1";
1930 if ($self->{type} =~ /_order/) {
1932 $where = "quotation = '0'";
1934 if ($self->{type} =~ /_quotation/) {
1936 $where = "quotation = '1'";
1939 my $query = qq|SELECT MAX(id) FROM $arap
1941 AND ${table}_id > 0|;
1942 my $sth = $dbh->prepare($query);
1943 $sth->execute || $self->dberror($query);
1945 my ($trans_id) = $sth->fetchrow_array;
1949 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1950 current_date + ct.terms AS duedate, a.department_id,
1951 d.description AS department
1953 JOIN $table ct ON (a.${table}_id = ct.id)
1954 LEFT JOIN department d ON (a.department_id = d.id)
1955 WHERE a.id = $trans_id|;
1956 $sth = $dbh->prepare($query);
1957 $sth->execute || $self->dberror($query);
1959 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1960 $self->{duedate}, $self->{department_id}, $self->{department})
1961 = $sth->fetchrow_array;
1964 $main::lxdebug->leave_sub();
1968 $main::lxdebug->enter_sub();
1970 my ($self, $myconfig, $thisdate, $days) = @_;
1972 my $dbh = $self->dbconnect($myconfig);
1977 my $dateformat = $myconfig->{dateformat};
1978 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1980 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1982 $sth = $dbh->prepare($query);
1983 $sth->execute || $self->dberror($query);
1985 $query = qq|SELECT current_date AS thisdate
1987 $sth = $dbh->prepare($query);
1988 $sth->execute || $self->dberror($query);
1991 ($thisdate) = $sth->fetchrow_array;
1996 $main::lxdebug->leave_sub();
2002 $main::lxdebug->enter_sub();
2004 my ($self, $string) = @_;
2006 if ($string !~ /%/) {
2007 $string = "%$string%";
2010 $string =~ s/\'/\'\'/g;
2012 $main::lxdebug->leave_sub();
2018 $main::lxdebug->enter_sub();
2020 my ($self, $flds, $new, $count, $numrows) = @_;
2024 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2030 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2032 $j = $item->{ndx} - 1;
2033 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2037 for $i ($count + 1 .. $numrows) {
2038 map { delete $self->{"${_}_$i"} } @{$flds};
2041 $main::lxdebug->leave_sub();
2045 $main::lxdebug->enter_sub();
2047 my ($self, $myconfig) = @_;
2051 my $dbh = $self->dbconnect_noauto($myconfig);
2053 my $query = qq|DELETE FROM status
2054 WHERE formname = '$self->{formname}'
2056 my $sth = $dbh->prepare($query) || $self->dberror($query);
2058 if ($self->{formname} =~ /(check|receipt)/) {
2059 for $i (1 .. $self->{rowcount}) {
2060 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2064 $sth->execute($self->{id}) || $self->dberror($query);
2068 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2069 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2071 my %queued = split / /, $self->{queued};
2073 if ($self->{formname} =~ /(check|receipt)/) {
2075 # this is a check or receipt, add one entry for each lineitem
2076 my ($accno) = split /--/, $self->{account};
2077 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2078 chart_id) VALUES (?, '$printed',
2079 '$queued{$self->{formname}}', '$self->{prinform}',
2080 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2081 $sth = $dbh->prepare($query) || $self->dberror($query);
2083 for $i (1 .. $self->{rowcount}) {
2084 if ($self->{"checked_$i"}) {
2085 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2090 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2091 spoolfile, formname)
2092 VALUES ($self->{id}, '$printed', '$emailed',
2093 '$queued{$self->{formname}}', '$self->{formname}')|;
2094 $dbh->do($query) || $self->dberror($query);
2100 $main::lxdebug->leave_sub();
2104 $main::lxdebug->enter_sub();
2106 my ($self, $dbh) = @_;
2108 my ($query, $printed, $emailed);
2110 my $formnames = $self->{printed};
2111 my $emailforms = $self->{emailed};
2113 my $query = qq|DELETE FROM status
2114 WHERE formname = '$self->{formname}'
2115 AND trans_id = $self->{id}|;
2116 $dbh->do($query) || $self->dberror($query);
2118 # this only applies to the forms
2119 # checks and receipts are posted when printed or queued
2121 if ($self->{queued}) {
2122 my %queued = split / /, $self->{queued};
2124 foreach my $formname (keys %queued) {
2125 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2126 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2128 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2129 spoolfile, formname)
2130 VALUES ($self->{id}, '$printed', '$emailed',
2131 '$queued{$formname}', '$formname')|;
2132 $dbh->do($query) || $self->dberror($query);
2134 $formnames =~ s/$self->{formname}//;
2135 $emailforms =~ s/$self->{formname}//;
2140 # save printed, emailed info
2141 $formnames =~ s/^ +//g;
2142 $emailforms =~ s/^ +//g;
2145 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2146 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2148 foreach my $formname (keys %status) {
2149 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2150 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2152 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2153 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2154 $dbh->do($query) || $self->dberror($query);
2157 $main::lxdebug->leave_sub();
2160 sub update_defaults {
2161 $main::lxdebug->enter_sub();
2163 my ($self, $myconfig, $fld) = @_;
2165 my $dbh = $self->dbconnect_noauto($myconfig);
2166 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2167 my $sth = $dbh->prepare($query);
2169 $sth->execute || $self->dberror($query);
2170 my ($var) = $sth->fetchrow_array;
2175 $query = qq|UPDATE defaults
2177 $dbh->do($query) || $self->dberror($query);
2182 $main::lxdebug->leave_sub();
2187 sub update_business {
2188 $main::lxdebug->enter_sub();
2190 my ($self, $myconfig, $business_id) = @_;
2192 my $dbh = $self->dbconnect_noauto($myconfig);
2194 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2195 my $sth = $dbh->prepare($query);
2197 $sth->execute || $self->dberror($query);
2198 my ($var) = $sth->fetchrow_array;
2203 $query = qq|UPDATE business
2204 SET customernumberinit = '$var' WHERE id=$business_id|;
2205 $dbh->do($query) || $self->dberror($query);
2210 $main::lxdebug->leave_sub();
2216 $main::lxdebug->enter_sub();
2218 my ($self, $myconfig, $salesman) = @_;
2220 my $dbh = $self->dbconnect($myconfig);
2222 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2223 my $sth = $dbh->prepare($query);
2224 $sth->execute || $self->dberror($query);
2227 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2228 push(@{ $self->{salesman_list} }, $ref);
2232 $main::lxdebug->leave_sub();
2237 sub get_partsgroup {
2238 $main::lxdebug->enter_sub();
2240 my ($self, $myconfig, $p) = @_;
2242 my $dbh = $self->dbconnect($myconfig);
2244 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2246 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2248 if ($p->{searchitems} eq 'part') {
2250 WHERE p.inventory_accno_id > 0|;
2252 if ($p->{searchitems} eq 'service') {
2254 WHERE p.inventory_accno_id IS NULL|;
2256 if ($p->{searchitems} eq 'assembly') {
2258 WHERE p.assembly = '1'|;
2260 if ($p->{searchitems} eq 'labor') {
2262 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2266 ORDER BY partsgroup|;
2269 $query = qq|SELECT id, partsgroup FROM partsgroup
2270 ORDER BY partsgroup|;
2273 if ($p->{language_code}) {
2274 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2275 t.description AS translation
2277 JOIN parts p ON (p.partsgroup_id = pg.id)
2278 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2279 ORDER BY translation|;
2282 my $sth = $dbh->prepare($query);
2283 $sth->execute || $self->dberror($query);
2285 $self->{all_partsgroup} = ();
2286 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2287 push @{ $self->{all_partsgroup} }, $ref;
2291 $main::lxdebug->leave_sub();
2294 sub get_pricegroup {
2295 $main::lxdebug->enter_sub();
2297 my ($self, $myconfig, $p) = @_;
2299 my $dbh = $self->dbconnect($myconfig);
2301 my $query = qq|SELECT p.id, p.pricegroup
2305 ORDER BY pricegroup|;
2308 $query = qq|SELECT id, pricegroup FROM pricegroup
2309 ORDER BY pricegroup|;
2312 my $sth = $dbh->prepare($query);
2313 $sth->execute || $self->dberror($query);
2315 $self->{all_pricegroup} = ();
2316 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2317 push @{ $self->{all_pricegroup} }, $ref;
2322 $main::lxdebug->leave_sub();
2326 my ($self, $dbh, $myconfig, $audittrail) = @_;
2328 # table, $reference, $formname, $action, $id, $transdate) = @_;
2335 $dbh = $self->dbconnect($myconfig);
2339 # if we have an id add audittrail, otherwise get a new timestamp
2341 if ($audittrail->{id}) {
2343 $query = qq|SELECT audittrail FROM defaults|;
2345 if ($dbh->selectrow_array($query)) {
2346 my ($null, $employee_id) = $self->get_employee($dbh);
2348 if ($self->{audittrail} && !$myconfig) {
2349 chop $self->{audittrail};
2351 my @a = split /\|/, $self->{audittrail};
2355 my @flds = qw(tablename reference formname action transdate);
2357 # put into hash and remove dups
2359 $key = "$a[2]$a[3]";
2361 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2365 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2366 formname, action, employee_id, transdate)
2367 VALUES ($audittrail->{id}, ?, ?,
2368 ?, ?, $employee_id, ?)|;
2369 my $sth = $dbh->prepare($query) || $self->dberror($query);
2373 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2377 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2379 $sth->execute || $self->dberror;
2384 if ($audittrail->{transdate}) {
2385 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2386 formname, action, employee_id, transdate) VALUES (
2387 $audittrail->{id}, '$audittrail->{tablename}', |
2388 . $dbh->quote($audittrail->{reference}) . qq|,
2389 '$audittrail->{formname}', '$audittrail->{action}',
2390 $employee_id, '$audittrail->{transdate}')|;
2392 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2393 formname, action, employee_id) VALUES ($audittrail->{id},
2394 '$audittrail->{tablename}', |
2395 . $dbh->quote($audittrail->{reference}) . qq|,
2396 '$audittrail->{formname}', '$audittrail->{action}',
2403 $query = qq|SELECT current_timestamp FROM defaults|;
2404 my ($timestamp) = $dbh->selectrow_array($query);
2407 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2410 $dbh->disconnect if $disconnect;
2419 $main::lxdebug->enter_sub();
2421 my ($type, $country, $NLS_file) = @_;
2424 if ($country && -d "locale/$country") {
2426 $self->{countrycode} = $country;
2427 if (open(IN, "locale/$country/$NLS_file")) {
2428 my $code = join("", <IN>);
2434 $self->{NLS_file} = $NLS_file;
2436 push @{ $self->{LONG_MONTH} },
2437 ("January", "February", "March", "April",
2438 "May ", "June", "July", "August",
2439 "September", "October", "November", "December");
2440 push @{ $self->{SHORT_MONTH} },
2441 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2443 $main::lxdebug->leave_sub();
2449 my ($self, $text) = @_;
2451 return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
2455 $main::lxdebug->enter_sub();
2457 my ($self, $text) = @_;
2459 if (exists $self->{subs}{$text}) {
2460 $text = $self->{subs}{$text};
2462 if ($self->{countrycode} && $self->{NLS_file}) {
2464 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2468 $main::lxdebug->leave_sub();
2474 $main::lxdebug->enter_sub();
2476 my ($self, $myconfig, $date, $longformat) = @_;
2479 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2484 $spc = $myconfig->{dateformat};
2486 $spc = substr($spc, 1, 1);
2488 if ($date =~ /\D/) {
2489 if ($myconfig->{dateformat} =~ /^yy/) {
2490 ($yy, $mm, $dd) = split /\D/, $date;
2492 if ($myconfig->{dateformat} =~ /^mm/) {
2493 ($mm, $dd, $yy) = split /\D/, $date;
2495 if ($myconfig->{dateformat} =~ /^dd/) {
2496 ($dd, $mm, $yy) = split /\D/, $date;
2499 $date = substr($date, 2);
2500 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2505 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2506 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2508 if ($myconfig->{dateformat} =~ /^dd/) {
2509 if (defined $longformat && $longformat == 0) {
2511 $dd = "0$dd" if ($dd < 10);
2512 $mm = "0$mm" if ($mm < 10);
2513 $longdate = "$dd$spc$mm$spc$yy";
2516 $longdate .= ($spc eq '.') ? ". " : " ";
2517 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2519 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2521 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2522 # Lx-Office is mainly used in Germany or German speaking countries.
2523 if (defined $longformat && $longformat == 0) {
2525 $dd = "0$dd" if ($dd < 10);
2526 $mm = "0$mm" if ($mm < 10);
2527 $longdate = "$yy-$mm-$dd";
2529 $longdate = "$dd. ";
2530 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2533 if (defined $longformat && $longformat == 0) {
2535 $dd = "0$dd" if ($dd < 10);
2536 $mm = "0$mm" if ($mm < 10);
2537 $longdate = "$mm$spc$dd$spc$yy";
2539 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2545 $main::lxdebug->leave_sub();
2551 $main::lxdebug->enter_sub();
2553 my ($self, $myconfig, $date, $longformat) = @_;
2556 $main::lxdebug->leave_sub();
2561 $spc = $myconfig->{dateformat};
2563 $spc = substr($spc, 1, 1);
2565 if ($date =~ /\D/) {
2566 if ($myconfig->{dateformat} =~ /^yy/) {
2567 ($yy, $mm, $dd) = split /\D/, $date;
2568 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2569 ($mm, $dd, $yy) = split /\D/, $date;
2570 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2571 ($dd, $mm, $yy) = split /\D/, $date;
2574 $date = substr($date, 2);
2575 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2580 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2581 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2583 $main::lxdebug->leave_sub();
2584 return ($yy, $mm, $dd);
2588 $main::lxdebug->enter_sub();
2590 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
2592 $main::lxdebug->leave_sub() and return "" unless ($date);
2594 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
2596 $output_format =~ /d+/;
2597 substr($output_format, $-[0], $+[0] - $-[0]) =
2598 sprintf("%0" . (length($&)) . "d", $dd);
2600 $output_format =~ /m+/;
2601 substr($output_format, $-[0], $+[0] - $-[0]) =
2602 sprintf("%0" . (length($&)) . "d", $mm);
2604 $output_format =~ /y+/;
2605 if (length($&) == 2) {
2606 $yy -= $yy >= 2000 ? 2000 : 1900;
2608 substr($output_format, $-[0], $+[0] - $-[0]) =
2609 sprintf("%0" . (length($&)) . "d", $yy);
2611 $main::lxdebug->leave_sub();
2613 return $output_format;