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.0";
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();
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 if ($self->{favicon} && (-f "$self->{favicon}")) {
371 qq|<LINK REL="shortcut icon" HREF="$self->{favicon}" TYPE="image/x-icon">
375 if ($self->{charset}) {
377 qq|<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=$self->{charset}">
380 if ($self->{landscape}) {
381 $pagelayout = qq|<style type="text/css">
382 \@page { size:landscape; }
385 if ($self->{fokus}) {
386 $fokus = qq|<script type="text/javascript">
388 function fokus(){document.$self->{fokus}.focus();}
395 if ($self->{jsscript} == 1) {
398 <style type="text/css">\@import url(js/jscalendar/calendar-win2k-1.css);</style>
399 <script type="text/javascript" src="js/jscalendar/calendar.js"></script>
400 <script type="text/javascript" src="js/jscalendar/lang/calendar-de.js"></script>
401 <script type="text/javascript" src="js/jscalendar/calendar-setup.js"></script>
408 ? "$self->{title} - $self->{titlebar}"
411 foreach $item (@ { $self->{AJAX} }) {
412 $ajax .= $item->show_javascript();
414 print qq|Content-Type: text/html
418 <title>$self->{titlebar}</title>
426 <meta name="robots" content="noindex,nofollow" />
427 <script type="text/javascript" src="js/highlight_input.js"></script>
428 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
430 <script type="text/javascript" src="js/tabcontent.js">
432 /***********************************************
433 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
434 * This notice MUST stay intact for legal use
435 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
436 ***********************************************/
446 $main::lxdebug->leave_sub();
449 sub parse_html_template {
450 $main::lxdebug->enter_sub();
452 my ($self, $file, $additional_params) = @_;
455 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
456 $language = $main::language;
458 $language = $main::myconfig{"countrycode"};
461 if (-f "templates/webpages/${file}_${language}.html") {
462 if ((-f ".developer") &&
463 (-f "templates/webpages/${file}_master.html") &&
464 ((stat("templates/webpages/${file}_master.html"))[9] >
465 (stat("templates/webpages/${file}_${language}.html"))[9])) {
466 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
467 "Please re-run 'locales.pl' in 'locale/${language}'.";
468 print(qq|<pre>$info</pre>|);
472 $file = "templates/webpages/${file}_${language}.html";
473 } elsif (-f "templates/webpages/${file}.html") {
474 $file = "templates/webpages/${file}.html";
476 my $info = "Web page template '${file}' not found.\n" .
477 "Please re-run 'locales.pl' in 'locale/${language}'.";
478 print(qq|<pre>$info</pre>|);
482 my $template = HTML::Template->new("filename" => $file,
483 "die_on_bad_params" => 0,
485 "case_sensitive" => 1,
486 "loop_context_vars" => 1,
489 $additional_params = {} unless ($additional_params);
490 if ($self->{"DEBUG"}) {
491 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
494 if ($additional_params->{"DEBUG"}) {
495 $additional_params->{"DEBUG"} =
496 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
499 if (%main::myconfig) {
500 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
501 my $jsc_dateformat = $main::myconfig{"dateformat"};
502 $jsc_dateformat =~ s/d+/\%d/gi;
503 $jsc_dateformat =~ s/m+/\%m/gi;
504 $jsc_dateformat =~ s/y+/\%Y/gi;
505 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
508 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
509 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
510 $additional_params->{"conf_latex_templates"} = $main::latex;
511 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
513 my @additional_param_names = keys(%{$additional_params});
514 foreach my $key ($template->param()) {
515 my $param = $self->{$key};
516 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
517 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
518 $template->param($key => $param);
521 my $output = $template->output();
523 $main::lxdebug->leave_sub();
528 sub show_generic_error {
529 my ($self, $error, $title, $action) = @_;
532 $add_params->{"title"} = $title if ($title);
533 $self->{"label_error"} = $error;
537 map({ delete($self->{$_}); } qw(action));
538 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
539 if (!ref($self->{$_})); }
541 $add_params->{"SHOW_BUTTON"} = 1;
542 $add_params->{"BUTTON_LABEL"} = $action;
544 $add_params->{"VARIABLES"} = \@vars;
547 print($self->parse_html_template("generic/error", $add_params));
549 die("Error: $error\n");
552 sub show_generic_information {
553 my ($self, $error, $title) = @_;
556 $add_params->{"title"} = $title if ($title);
557 $self->{"label_information"} = $error;
560 print($self->parse_html_template("generic/information", $add_params));
562 die("Information: $error\n");
565 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
566 # changed it to accept an arbitrary number of triggers - sschoeling
568 $main::lxdebug->enter_sub();
571 my $myconfig = shift;
574 # set dateform for jsscript
576 $ifFormat = "%d.%m.%Y";
577 if ($myconfig->{dateformat} eq "dd.mm.yy") {
578 $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 "mm/dd/yy") {
587 $ifFormat = "%m/%d/%Y";
589 if ($myconfig->{dateformat} eq "mm-dd-yy") {
590 $ifFormat = "%m-%d-%Y";
592 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
593 $ifFormat = "%Y-%m-%d";
605 inputField : "| . (shift) . qq|",
606 ifFormat :"$ifFormat",
607 align : "| . (shift) . qq|",
608 button : "| . (shift) . qq|"
614 <script type="text/javascript">
615 <!--| . join("", @triggers) . qq|//-->
619 $main::lxdebug->leave_sub();
622 } #end sub write_trigger
625 $main::lxdebug->enter_sub();
627 my ($self, $msg) = @_;
629 if ($self->{callback}) {
631 ($script, $argv) = split(/\?/, $self->{callback});
632 exec("perl", "$script", $argv);
640 $main::lxdebug->leave_sub();
643 # sort of columns removed - empty sub
645 $main::lxdebug->enter_sub();
647 my ($self, @columns) = @_;
649 $main::lxdebug->leave_sub();
655 $main::lxdebug->enter_sub(2);
657 my ($self, $myconfig, $amount, $places, $dash) = @_;
662 my $neg = ($amount =~ s/-//);
664 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
666 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
667 my @p = split(/\./, $amount); # split amount at decimal point
669 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
672 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
675 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
676 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
677 ($neg ? "-$amount" : "$amount" ) ;
681 $main::lxdebug->leave_sub(2);
686 $main::lxdebug->enter_sub(2);
688 my ($self, $myconfig, $amount) = @_;
690 if ($myconfig->{in_numberformat} == 1) {
691 # Extra input number format 1000.00 or 1000,00
693 $amount = scalar reverse $amount;
694 $amount =~ s/\./DOT/;
696 $amount =~ s/DOT/\./;
697 $amount = scalar reverse $amount;
698 $main::lxdebug->leave_sub(2);
699 return ($amount * 1);
702 if ( ($myconfig->{numberformat} eq '1.000,00')
703 || ($myconfig->{numberformat} eq '1000,00')) {
708 if ($myconfig->{numberformat} eq "1'000.00") {
714 $main::lxdebug->leave_sub(2);
716 return ($amount * 1);
720 $main::lxdebug->enter_sub(2);
722 my ($self, $amount, $places) = @_;
725 # Rounding like "Kaufmannsrunden"
726 # Descr. http://de.wikipedia.org/wiki/Rundung
728 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
731 $amount = $amount * (10**($places));
732 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
734 $main::lxdebug->leave_sub(2);
736 return $round_amount;
741 $main::lxdebug->enter_sub();
743 my ($self, $myconfig, $userspath) = @_;
746 $self->{"cwd"} = getcwd();
747 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
749 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
750 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
751 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
752 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
753 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
754 } elsif (($self->{"format"} =~ /html/i) ||
755 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
756 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
757 } elsif (($self->{"format"} =~ /xml/i) ||
758 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
759 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
760 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
761 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
762 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
763 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
764 } elsif ( defined $self->{'format'}) {
765 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
766 } elsif ( $self->{'format'} eq '' ) {
767 $self->error("No Outputformat given: $self->{'format'}");
768 } else { #Catch the rest
769 $self->error("Outputformat not defined: $self->{'format'}");
772 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
773 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
775 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
776 qw(email tel fax name signature company address businessnumber));
777 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
778 qw(company address signature));
780 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
782 # OUT is used for the media, screen, printer, email
783 # for postscript we store a copy in a temporary file
785 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
786 if ($template->uses_temp_file() || $self->{media} eq 'email') {
788 $self->{OUT} = ">$self->{tmpfile}";
792 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
794 open(OUT, ">-") or $self->error("STDOUT : $!");
798 if (!$template->parse(*OUT)) {
800 $self->error("$self->{IN} : " . $template->get_error());
805 if ($template->uses_temp_file() || $self->{media} eq 'email') {
807 if ($self->{media} eq 'email') {
811 my $mail = new Mailer;
813 map { $mail->{$_} = $self->{$_} }
814 qw(cc bcc subject message version format charset);
815 $mail->{to} = qq|$self->{email}|;
816 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
817 $mail->{fileid} = "$fileid.";
818 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
820 # if we send html or plain text inline
821 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
822 $mail->{contenttype} = "text/html";
824 $mail->{message} =~ s/\r\n/<br>\n/g;
825 $myconfig->{signature} =~ s/\\n/<br>\n/g;
826 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
828 open(IN, $self->{tmpfile})
829 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
831 $mail->{message} .= $_;
838 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
840 $mail->{message} =~ s/\r\n/\n/g;
841 $myconfig->{signature} =~ s/\\n/\n/g;
842 $mail->{message} .= "\n-- \n$myconfig->{signature}";
846 my $err = $mail->send($out);
847 $self->error($self->cleanup . "$err") if ($err);
853 my $numbytes = (-s $self->{tmpfile});
854 open(IN, $self->{tmpfile})
855 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
857 $self->{copies} = 1 unless $self->{media} eq 'printer';
859 chdir("$self->{cwd}");
860 #print(STDERR "Kopien $self->{copies}\n");
861 #print(STDERR "OUT $self->{OUT}\n");
862 for my $i (1 .. $self->{copies}) {
864 open(OUT, $self->{OUT})
865 or $self->error($self->cleanup . "$self->{OUT} : $!");
869 print qq|Content-Type: | . $template->get_mime_type() . qq|
870 Content-Disposition: attachment; filename="$self->{tmpfile}"
871 Content-Length: $numbytes
875 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
895 chdir("$self->{cwd}");
896 $main::lxdebug->leave_sub();
900 $main::lxdebug->enter_sub();
904 chdir("$self->{tmpdir}");
907 if (-f "$self->{tmpfile}.err") {
908 open(FH, "$self->{tmpfile}.err");
913 if ($self->{tmpfile}) {
914 $self->{tmpfile} =~ s|.*/||g;
916 $self->{tmpfile} =~ s/\.\w+$//g;
917 my $tmpfile = $self->{tmpfile};
918 unlink(<$tmpfile.*>);
921 chdir("$self->{cwd}");
923 $main::lxdebug->leave_sub();
929 $main::lxdebug->enter_sub();
931 my ($self, $date, $myconfig) = @_;
933 if ($date && $date =~ /\D/) {
935 if ($myconfig->{dateformat} =~ /^yy/) {
936 ($yy, $mm, $dd) = split /\D/, $date;
938 if ($myconfig->{dateformat} =~ /^mm/) {
939 ($mm, $dd, $yy) = split /\D/, $date;
941 if ($myconfig->{dateformat} =~ /^dd/) {
942 ($dd, $mm, $yy) = split /\D/, $date;
947 $yy = ($yy < 70) ? $yy + 2000 : $yy;
948 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
950 $dd = "0$dd" if ($dd < 10);
951 $mm = "0$mm" if ($mm < 10);
956 $main::lxdebug->leave_sub();
961 # Database routines used throughout
964 $main::lxdebug->enter_sub();
966 my ($self, $myconfig) = @_;
968 # connect to database
970 DBI->connect($myconfig->{dbconnect},
971 $myconfig->{dbuser}, $myconfig->{dbpasswd})
975 if ($myconfig->{dboptions}) {
976 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
979 $main::lxdebug->leave_sub();
984 sub dbconnect_noauto {
985 $main::lxdebug->enter_sub();
987 my ($self, $myconfig) = @_;
989 # connect to database
991 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
992 $myconfig->{dbpasswd}, { AutoCommit => 0 })
996 if ($myconfig->{dboptions}) {
997 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1000 $main::lxdebug->leave_sub();
1005 sub update_balance {
1006 $main::lxdebug->enter_sub();
1008 my ($self, $dbh, $table, $field, $where, $value) = @_;
1010 # if we have a value, go do it
1013 # retrieve balance from table
1014 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1015 my $sth = $dbh->prepare($query);
1017 $sth->execute || $self->dberror($query);
1018 my ($balance) = $sth->fetchrow_array;
1024 $query = "UPDATE $table SET $field = $balance WHERE $where";
1025 $dbh->do($query) || $self->dberror($query);
1027 $main::lxdebug->leave_sub();
1030 sub update_exchangerate {
1031 $main::lxdebug->enter_sub();
1033 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1035 # some sanity check for currency
1037 $main::lxdebug->leave_sub();
1041 my $query = qq|SELECT e.curr FROM exchangerate e
1042 WHERE e.curr = '$curr'
1043 AND e.transdate = '$transdate'
1045 my $sth = $dbh->prepare($query);
1046 $sth->execute || $self->dberror($query);
1049 if ($buy != 0 && $sell != 0) {
1050 $set = "buy = $buy, sell = $sell";
1051 } elsif ($buy != 0) {
1052 $set = "buy = $buy";
1053 } elsif ($sell != 0) {
1054 $set = "sell = $sell";
1057 if ($sth->fetchrow_array) {
1058 $query = qq|UPDATE exchangerate
1060 WHERE curr = '$curr'
1061 AND transdate = '$transdate'|;
1063 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1064 VALUES ('$curr', $buy, $sell, '$transdate')|;
1067 $dbh->do($query) || $self->dberror($query);
1069 $main::lxdebug->leave_sub();
1072 sub save_exchangerate {
1073 $main::lxdebug->enter_sub();
1075 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1077 my $dbh = $self->dbconnect($myconfig);
1079 my ($buy, $sell) = (0, 0);
1080 $buy = $rate if $fld eq 'buy';
1081 $sell = $rate if $fld eq 'sell';
1083 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1087 $main::lxdebug->leave_sub();
1090 sub get_exchangerate {
1091 $main::lxdebug->enter_sub();
1093 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1095 unless ($transdate) {
1096 $main::lxdebug->leave_sub();
1100 my $query = qq|SELECT e.$fld FROM exchangerate e
1101 WHERE e.curr = '$curr'
1102 AND e.transdate = '$transdate'|;
1103 my $sth = $dbh->prepare($query);
1104 $sth->execute || $self->dberror($query);
1106 my ($exchangerate) = $sth->fetchrow_array;
1109 if ($exchangerate == 0) {
1113 $main::lxdebug->leave_sub();
1115 return $exchangerate;
1118 sub set_payment_options {
1119 $main::lxdebug->enter_sub();
1121 my ($self, $myconfig, $transdate) = @_;
1123 if ($self->{payment_id}) {
1125 my $dbh = $self->dbconnect($myconfig);
1128 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1129 WHERE p.id = $self->{payment_id}|;
1130 my $sth = $dbh->prepare($query);
1131 $sth->execute || $self->dberror($query);
1133 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1136 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1138 my $sth = $dbh->prepare($query);
1139 $sth->execute || $self->dberror($query);
1140 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1143 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1145 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1146 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1147 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1152 $main::lxdebug->leave_sub();
1156 sub check_exchangerate {
1157 $main::lxdebug->enter_sub();
1159 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1161 unless ($transdate) {
1162 $main::lxdebug->leave_sub();
1166 my $dbh = $self->dbconnect($myconfig);
1168 my $query = qq|SELECT e.$fld FROM exchangerate e
1169 WHERE e.curr = '$currency'
1170 AND e.transdate = '$transdate'|;
1171 my $sth = $dbh->prepare($query);
1172 $sth->execute || $self->dberror($query);
1174 my ($exchangerate) = $sth->fetchrow_array;
1178 $main::lxdebug->leave_sub();
1180 return $exchangerate;
1183 sub get_template_language {
1184 $main::lxdebug->enter_sub();
1186 my ($self, $myconfig) = @_;
1188 my $template_code = "";
1190 if ($self->{language_id}) {
1192 my $dbh = $self->dbconnect($myconfig);
1195 my $query = qq|SELECT l.template_code FROM language l
1196 WHERE l.id = $self->{language_id}|;
1197 my $sth = $dbh->prepare($query);
1198 $sth->execute || $self->dberror($query);
1200 ($template_code) = $sth->fetchrow_array;
1205 $main::lxdebug->leave_sub();
1207 return $template_code;
1210 sub get_printer_code {
1211 $main::lxdebug->enter_sub();
1213 my ($self, $myconfig) = @_;
1215 my $template_code = "";
1217 if ($self->{printer_id}) {
1219 my $dbh = $self->dbconnect($myconfig);
1222 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1223 WHERE p.id = $self->{printer_id}|;
1224 my $sth = $dbh->prepare($query);
1225 $sth->execute || $self->dberror($query);
1227 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1232 $main::lxdebug->leave_sub();
1234 return $template_code;
1238 $main::lxdebug->enter_sub();
1240 my ($self, $myconfig) = @_;
1242 my $template_code = "";
1244 if ($self->{shipto_id}) {
1246 my $dbh = $self->dbconnect($myconfig);
1249 my $query = qq|SELECT s.* FROM shipto s
1250 WHERE s.shipto_id = $self->{shipto_id}|;
1251 my $sth = $dbh->prepare($query);
1252 $sth->execute || $self->dberror($query);
1253 $ref = $sth->fetchrow_hashref(NAME_lc);
1254 map { $form->{$_} = $ref->{$_} } keys %$ref;
1259 $main::lxdebug->leave_sub();
1264 $main::lxdebug->enter_sub();
1266 my ($self, $dbh, $id, $module) = @_;
1270 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1272 if ($self->{"shipto$item"}) {
1273 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1275 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1278 if ($self->{shipto_id}) {
1279 my $query = qq| UPDATE shipto set
1280 shiptoname = '$self->{shiptoname}',
1281 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1282 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1283 shiptostreet = '$self->{shiptostreet}',
1284 shiptozipcode = '$self->{shiptozipcode}',
1285 shiptocity = '$self->{shiptocity}',
1286 shiptocountry = '$self->{shiptocountry}',
1287 shiptocontact = '$self->{shiptocontact}',
1288 shiptophone = '$self->{shiptophone}',
1289 shiptofax = '$self->{shiptofax}',
1290 shiptoemail = '$self->{shiptoemail}'
1291 WHERE shipto_id = $self->{shipto_id}|;
1292 $dbh->do($query) || $self->dberror($query);
1295 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1296 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1297 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1298 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1299 '$self->{shiptozipcode}', '$self->{shiptocity}',
1300 '$self->{shiptocountry}', '$self->{shiptocontact}',
1301 '$self->{shiptophone}', '$self->{shiptofax}',
1302 '$self->{shiptoemail}', '$module')|;
1303 $dbh->do($query) || $self->dberror($query);
1307 $main::lxdebug->leave_sub();
1311 $main::lxdebug->enter_sub();
1313 my ($self, $dbh) = @_;
1315 my $query = qq|SELECT e.id, e.name FROM employee e
1316 WHERE e.login = '$self->{login}'|;
1317 my $sth = $dbh->prepare($query);
1318 $sth->execute || $self->dberror($query);
1320 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1321 $self->{employee_id} *= 1;
1325 $main::lxdebug->leave_sub();
1329 $main::lxdebug->enter_sub();
1331 my ($self, $myconfig) = @_;
1333 my $dbh = $self->dbconnect($myconfig);
1334 my $query = qq|SELECT current_date+terms_netto FROM payment_terms
1335 WHERE id = '$self->{payment_id}'|;
1336 my $sth = $dbh->prepare($query);
1337 $sth->execute || $self->dberror($query);
1339 ($self->{duedate}) = $sth->fetchrow_array;
1343 $main::lxdebug->leave_sub();
1346 # get other contact for transaction and form - html/tex
1348 $main::lxdebug->enter_sub();
1350 my ($self, $dbh, $id) = @_;
1352 my $query = qq|SELECT c.*
1355 $sth = $dbh->prepare($query);
1356 $sth->execute || $self->dberror($query);
1358 $ref = $sth->fetchrow_hashref(NAME_lc);
1360 push @{ $self->{$_} }, $ref;
1363 $main::lxdebug->leave_sub();
1366 # get contacts for id, if no contact return {"","","","",""}
1368 $main::lxdebug->enter_sub();
1370 my ($self, $dbh, $id) = @_;
1372 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1374 WHERE cp_cv_id=$id|;
1375 my $sth = $dbh->prepare($query);
1376 $sth->execute || $self->dberror($query);
1379 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1380 push @{ $self->{all_contacts} }, $ref;
1385 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1388 $main::lxdebug->leave_sub();
1391 # this sub gets the id and name from $table
1393 $main::lxdebug->enter_sub();
1395 my ($self, $myconfig, $table) = @_;
1397 # connect to database
1398 my $dbh = $self->dbconnect($myconfig);
1400 my $name = $self->like(lc $self->{$table});
1401 my $customernumber = $self->like(lc $self->{customernumber});
1403 if ($self->{customernumber} ne "") {
1404 $query = qq~SELECT c.id, c.name,
1405 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1407 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1410 $query = qq~SELECT c.id, c.name,
1411 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1413 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1417 if ($self->{openinvoices}) {
1418 $query = qq~SELECT DISTINCT c.id, c.name,
1419 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1420 FROM $self->{arap} a
1421 JOIN $table c ON (a.${table}_id = c.id)
1422 WHERE NOT a.amount = a.paid
1423 AND lower(c.name) LIKE '$name'
1426 my $sth = $dbh->prepare($query);
1428 $sth->execute || $self->dberror($query);
1431 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1432 push(@{ $self->{name_list} }, $ref);
1438 $main::lxdebug->leave_sub();
1443 # the selection sub is used in the AR, AP, IS, IR and OE module
1446 $main::lxdebug->enter_sub();
1448 my ($self, $myconfig, $table, $module) = @_;
1451 my $dbh = $self->dbconnect($myconfig);
1453 my $query = qq|SELECT count(*) FROM $table|;
1454 my $sth = $dbh->prepare($query);
1455 $sth->execute || $self->dberror($query);
1456 my ($count) = $sth->fetchrow_array;
1459 # build selection list
1460 if ($count < $myconfig->{vclimit}) {
1461 $query = qq|SELECT id, name
1462 FROM $table WHERE not obsolete
1464 $sth = $dbh->prepare($query);
1465 $sth->execute || $self->dberror($query);
1467 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1468 push @{ $self->{"all_$table"} }, $ref;
1476 $self->get_employee($dbh);
1478 # setup sales contacts
1479 $query = qq|SELECT e.id, e.name
1482 AND NOT e.id = $self->{employee_id}|;
1483 $sth = $dbh->prepare($query);
1484 $sth->execute || $self->dberror($query);
1486 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1487 push @{ $self->{all_employees} }, $ref;
1492 push @{ $self->{all_employees} },
1493 { id => $self->{employee_id},
1494 name => $self->{employee} };
1496 # sort the whole thing
1497 @{ $self->{all_employees} } =
1498 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1500 if ($module eq 'AR') {
1502 # prepare query for departments
1503 $query = qq|SELECT d.id, d.description
1509 $query = qq|SELECT d.id, d.description
1514 $sth = $dbh->prepare($query);
1515 $sth->execute || $self->dberror($query);
1517 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1518 push @{ $self->{all_departments} }, $ref;
1523 $query = qq|SELECT id, description
1526 $sth = $dbh->prepare($query);
1527 $sth->execute || $form->dberror($query);
1529 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1530 push @{ $self->{languages} }, $ref;
1535 $query = qq|SELECT printer_description, id
1538 $sth = $dbh->prepare($query);
1539 $sth->execute || $form->dberror($query);
1541 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1542 push @{ $self->{printers} }, $ref;
1548 $query = qq|SELECT id, description
1551 $sth = $dbh->prepare($query);
1552 $sth->execute || $form->dberror($query);
1554 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1555 push @{ $self->{payment_terms} }, $ref;
1559 $main::lxdebug->leave_sub();
1563 sub language_payment {
1564 $main::lxdebug->enter_sub();
1566 my ($self, $myconfig) = @_;
1567 undef $self->{languages};
1568 undef $self->{payment_terms};
1569 undef $self->{printers};
1572 my $dbh = $self->dbconnect($myconfig);
1574 my $query = qq|SELECT id, description
1577 my $sth = $dbh->prepare($query);
1578 $sth->execute || $form->dberror($query);
1580 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1581 push @{ $self->{languages} }, $ref;
1586 $query = qq|SELECT printer_description, id
1589 $sth = $dbh->prepare($query);
1590 $sth->execute || $form->dberror($query);
1592 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1593 push @{ $self->{printers} }, $ref;
1598 $query = qq|SELECT id, description
1601 $sth = $dbh->prepare($query);
1602 $sth->execute || $form->dberror($query);
1604 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1605 push @{ $self->{payment_terms} }, $ref;
1609 # get buchungsgruppen
1610 $query = qq|SELECT id, description
1611 FROM buchungsgruppen|;
1612 $sth = $dbh->prepare($query);
1613 $sth->execute || $form->dberror($query);
1615 $self->{BUCHUNGSGRUPPEN} = [];
1616 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1617 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1622 $main::lxdebug->leave_sub();
1625 # this is only used for reports
1626 sub all_departments {
1627 $main::lxdebug->enter_sub();
1629 my ($self, $myconfig, $table) = @_;
1631 my $dbh = $self->dbconnect($myconfig);
1632 my $where = "1 = 1";
1634 if (defined $table) {
1635 if ($table eq 'customer') {
1636 $where = " d.role = 'P'";
1640 my $query = qq|SELECT d.id, d.description
1644 my $sth = $dbh->prepare($query);
1645 $sth->execute || $self->dberror($query);
1647 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1648 push @{ $self->{all_departments} }, $ref;
1654 $main::lxdebug->leave_sub();
1658 $main::lxdebug->enter_sub();
1660 my ($self, $module, $myconfig, $table) = @_;
1662 $self->all_vc($myconfig, $table, $module);
1664 # get last customers or vendors
1667 my $dbh = $self->dbconnect($myconfig);
1672 my $transdate = "current_date";
1673 if ($self->{transdate}) {
1674 $transdate = qq|'$self->{transdate}'|;
1677 # now get the account numbers
1678 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1679 FROM chart c, taxkeys tk
1680 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)
1683 $sth = $dbh->prepare($query);
1684 $sth->execute || $self->dberror($query);
1686 $self->{accounts} = "";
1687 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1689 foreach my $key (split(/:/, $ref->{link})) {
1690 if ($key =~ /$module/) {
1692 # cross reference for keys
1693 $xkeyref{ $ref->{accno} } = $key;
1695 push @{ $self->{"${module}_links"}{$key} },
1696 { accno => $ref->{accno},
1697 description => $ref->{description},
1698 taxkey => $ref->{taxkey_id},
1699 tax_id => $ref->{tax_id} };
1701 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1707 # get taxkeys and description
1708 $query = qq|SELECT id, taxkey, taxdescription
1710 $sth = $dbh->prepare($query);
1711 $sth->execute || $self->dberror($query);
1713 $ref = $sth->fetchrow_hashref(NAME_lc);
1715 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1716 push @{ $self->{TAXKEY} }, $ref;
1723 $query = qq|SELECT id, description
1725 $sth = $dbh->prepare($query);
1726 $sth->execute || $form->dberror($query);
1729 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1730 push @{ $self->{TAXZONE} }, $ref;
1734 if (($module eq "AP") || ($module eq "AR")) {
1736 # get tax rates and description
1737 $query = qq| SELECT * FROM tax t|;
1738 $sth = $dbh->prepare($query);
1739 $sth->execute || $self->dberror($query);
1741 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1742 push @{ $self->{TAX} }, $ref;
1748 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1750 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1751 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1752 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1753 c.name AS $table, a.department_id, d.description AS department,
1754 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1755 a.employee_id, e.name AS employee, a.gldate, a.type
1757 JOIN $table c ON (a.${table}_id = c.id)
1758 LEFT JOIN employee e ON (e.id = a.employee_id)
1759 LEFT JOIN department d ON (d.id = a.department_id)
1760 WHERE a.id = $self->{id}|;
1761 $sth = $dbh->prepare($query);
1762 $sth->execute || $self->dberror($query);
1764 $ref = $sth->fetchrow_hashref(NAME_lc);
1765 foreach $key (keys %$ref) {
1766 $self->{$key} = $ref->{$key};
1771 my $transdate = "current_date";
1772 if ($self->{transdate}) {
1773 $transdate = qq|'$self->{transdate}'|;
1776 # now get the account numbers
1777 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1778 FROM chart c, taxkeys tk
1779 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%'))
1782 $sth = $dbh->prepare($query);
1783 $sth->execute || $self->dberror($query);
1785 $self->{accounts} = "";
1786 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1788 foreach my $key (split(/:/, $ref->{link})) {
1789 if ($key =~ /$module/) {
1791 # cross reference for keys
1792 $xkeyref{ $ref->{accno} } = $key;
1794 push @{ $self->{"${module}_links"}{$key} },
1795 { accno => $ref->{accno},
1796 description => $ref->{description},
1797 taxkey => $ref->{taxkey_id},
1798 tax_id => $ref->{tax_id} };
1800 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1806 # get amounts from individual entries
1807 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1808 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
1810 JOIN chart c ON (c.id = a.chart_id)
1811 LEFT JOIN project p ON (p.id = a.project_id)
1812 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))
1813 WHERE a.trans_id = $self->{id}
1814 AND a.fx_transaction = '0'
1815 ORDER BY a.oid,a.transdate|;
1816 $sth = $dbh->prepare($query);
1817 $sth->execute || $self->dberror($query);
1819 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1821 # get exchangerate for currency
1822 $self->{exchangerate} =
1823 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1827 # store amounts in {acc_trans}{$key} for multiple accounts
1828 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1829 $ref->{exchangerate} =
1830 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1832 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1835 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1836 $ref->{amount} *= -1;
1838 $ref->{index} = $index;
1840 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1844 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1845 (SELECT c.accno FROM chart c
1846 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1847 (SELECT c.accno FROM chart c
1848 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1850 $sth = $dbh->prepare($query);
1851 $sth->execute || $self->dberror($query);
1853 $ref = $sth->fetchrow_hashref(NAME_lc);
1854 map { $self->{$_} = $ref->{$_} } keys %$ref;
1860 $query = qq|SELECT current_date AS transdate,
1861 d.curr AS currencies, d.closedto, d.revtrans,
1862 (SELECT c.accno FROM chart c
1863 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1864 (SELECT c.accno FROM chart c
1865 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1867 $sth = $dbh->prepare($query);
1868 $sth->execute || $self->dberror($query);
1870 $ref = $sth->fetchrow_hashref(NAME_lc);
1871 map { $self->{$_} = $ref->{$_} } keys %$ref;
1874 if ($self->{"$self->{vc}_id"}) {
1876 # only setup currency
1877 ($self->{currency}) = split(/:/, $self->{currencies});
1881 $self->lastname_used($dbh, $myconfig, $table, $module);
1883 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1885 # get exchangerate for currency
1886 $self->{exchangerate} =
1887 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1898 $main::lxdebug->leave_sub();
1902 $main::lxdebug->enter_sub();
1904 my ($self, $dbh, $myconfig, $table, $module) = @_;
1906 my $arap = ($table eq 'customer') ? "ar" : "ap";
1907 my $where = "1 = 1";
1909 if ($self->{type} =~ /_order/) {
1911 $where = "quotation = '0'";
1913 if ($self->{type} =~ /_quotation/) {
1915 $where = "quotation = '1'";
1918 my $query = qq|SELECT MAX(id) FROM $arap
1920 AND ${table}_id > 0|;
1921 my $sth = $dbh->prepare($query);
1922 $sth->execute || $self->dberror($query);
1924 my ($trans_id) = $sth->fetchrow_array;
1928 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1929 current_date + ct.terms AS duedate, a.department_id,
1930 d.description AS department
1932 JOIN $table ct ON (a.${table}_id = ct.id)
1933 LEFT JOIN department d ON (a.department_id = d.id)
1934 WHERE a.id = $trans_id|;
1935 $sth = $dbh->prepare($query);
1936 $sth->execute || $self->dberror($query);
1938 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1939 $self->{duedate}, $self->{department_id}, $self->{department})
1940 = $sth->fetchrow_array;
1943 $main::lxdebug->leave_sub();
1947 $main::lxdebug->enter_sub();
1949 my ($self, $myconfig, $thisdate, $days) = @_;
1951 my $dbh = $self->dbconnect($myconfig);
1956 my $dateformat = $myconfig->{dateformat};
1957 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1959 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1961 $sth = $dbh->prepare($query);
1962 $sth->execute || $self->dberror($query);
1964 $query = qq|SELECT current_date AS thisdate
1966 $sth = $dbh->prepare($query);
1967 $sth->execute || $self->dberror($query);
1970 ($thisdate) = $sth->fetchrow_array;
1975 $main::lxdebug->leave_sub();
1981 $main::lxdebug->enter_sub();
1983 my ($self, $string) = @_;
1985 if ($string !~ /%/) {
1986 $string = "%$string%";
1989 $string =~ s/\'/\'\'/g;
1991 $main::lxdebug->leave_sub();
1997 $main::lxdebug->enter_sub();
1999 my ($self, $flds, $new, $count, $numrows) = @_;
2003 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2009 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2011 $j = $item->{ndx} - 1;
2012 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2016 for $i ($count + 1 .. $numrows) {
2017 map { delete $self->{"${_}_$i"} } @{$flds};
2020 $main::lxdebug->leave_sub();
2024 $main::lxdebug->enter_sub();
2026 my ($self, $myconfig) = @_;
2030 my $dbh = $self->dbconnect_noauto($myconfig);
2032 my $query = qq|DELETE FROM status
2033 WHERE formname = '$self->{formname}'
2035 my $sth = $dbh->prepare($query) || $self->dberror($query);
2037 if ($self->{formname} =~ /(check|receipt)/) {
2038 for $i (1 .. $self->{rowcount}) {
2039 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2043 $sth->execute($self->{id}) || $self->dberror($query);
2047 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2048 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2050 my %queued = split / /, $self->{queued};
2052 if ($self->{formname} =~ /(check|receipt)/) {
2054 # this is a check or receipt, add one entry for each lineitem
2055 my ($accno) = split /--/, $self->{account};
2056 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2057 chart_id) VALUES (?, '$printed',
2058 '$queued{$self->{formname}}', '$self->{prinform}',
2059 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2060 $sth = $dbh->prepare($query) || $self->dberror($query);
2062 for $i (1 .. $self->{rowcount}) {
2063 if ($self->{"checked_$i"}) {
2064 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2069 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2070 spoolfile, formname)
2071 VALUES ($self->{id}, '$printed', '$emailed',
2072 '$queued{$self->{formname}}', '$self->{formname}')|;
2073 $dbh->do($query) || $self->dberror($query);
2079 $main::lxdebug->leave_sub();
2083 $main::lxdebug->enter_sub();
2085 my ($self, $dbh) = @_;
2087 my ($query, $printed, $emailed);
2089 my $formnames = $self->{printed};
2090 my $emailforms = $self->{emailed};
2092 my $query = qq|DELETE FROM status
2093 WHERE formname = '$self->{formname}'
2094 AND trans_id = $self->{id}|;
2095 $dbh->do($query) || $self->dberror($query);
2097 # this only applies to the forms
2098 # checks and receipts are posted when printed or queued
2100 if ($self->{queued}) {
2101 my %queued = split / /, $self->{queued};
2103 foreach my $formname (keys %queued) {
2104 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2105 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2107 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2108 spoolfile, formname)
2109 VALUES ($self->{id}, '$printed', '$emailed',
2110 '$queued{$formname}', '$formname')|;
2111 $dbh->do($query) || $self->dberror($query);
2113 $formnames =~ s/$self->{formname}//;
2114 $emailforms =~ s/$self->{formname}//;
2119 # save printed, emailed info
2120 $formnames =~ s/^ +//g;
2121 $emailforms =~ s/^ +//g;
2124 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2125 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2127 foreach my $formname (keys %status) {
2128 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2129 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2131 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2132 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2133 $dbh->do($query) || $self->dberror($query);
2136 $main::lxdebug->leave_sub();
2139 sub update_defaults {
2140 $main::lxdebug->enter_sub();
2142 my ($self, $myconfig, $fld) = @_;
2144 my $dbh = $self->dbconnect_noauto($myconfig);
2145 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2146 my $sth = $dbh->prepare($query);
2148 $sth->execute || $self->dberror($query);
2149 my ($var) = $sth->fetchrow_array;
2154 $query = qq|UPDATE defaults
2156 $dbh->do($query) || $self->dberror($query);
2161 $main::lxdebug->leave_sub();
2166 sub update_business {
2167 $main::lxdebug->enter_sub();
2169 my ($self, $myconfig, $business_id) = @_;
2171 my $dbh = $self->dbconnect_noauto($myconfig);
2173 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2174 my $sth = $dbh->prepare($query);
2176 $sth->execute || $self->dberror($query);
2177 my ($var) = $sth->fetchrow_array;
2182 $query = qq|UPDATE business
2183 SET customernumberinit = '$var' WHERE id=$business_id|;
2184 $dbh->do($query) || $self->dberror($query);
2189 $main::lxdebug->leave_sub();
2195 $main::lxdebug->enter_sub();
2197 my ($self, $myconfig, $salesman) = @_;
2199 my $dbh = $self->dbconnect($myconfig);
2201 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2202 my $sth = $dbh->prepare($query);
2203 $sth->execute || $self->dberror($query);
2206 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2207 push(@{ $self->{salesman_list} }, $ref);
2211 $main::lxdebug->leave_sub();
2216 sub get_partsgroup {
2217 $main::lxdebug->enter_sub();
2219 my ($self, $myconfig, $p) = @_;
2221 my $dbh = $self->dbconnect($myconfig);
2223 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2225 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2227 if ($p->{searchitems} eq 'part') {
2229 WHERE p.inventory_accno_id > 0|;
2231 if ($p->{searchitems} eq 'service') {
2233 WHERE p.inventory_accno_id IS NULL|;
2235 if ($p->{searchitems} eq 'assembly') {
2237 WHERE p.assembly = '1'|;
2239 if ($p->{searchitems} eq 'labor') {
2241 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2245 ORDER BY partsgroup|;
2248 $query = qq|SELECT id, partsgroup FROM partsgroup
2249 ORDER BY partsgroup|;
2252 if ($p->{language_code}) {
2253 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2254 t.description AS translation
2256 JOIN parts p ON (p.partsgroup_id = pg.id)
2257 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2258 ORDER BY translation|;
2261 my $sth = $dbh->prepare($query);
2262 $sth->execute || $self->dberror($query);
2264 $self->{all_partsgroup} = ();
2265 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2266 push @{ $self->{all_partsgroup} }, $ref;
2270 $main::lxdebug->leave_sub();
2273 sub get_pricegroup {
2274 $main::lxdebug->enter_sub();
2276 my ($self, $myconfig, $p) = @_;
2278 my $dbh = $self->dbconnect($myconfig);
2280 my $query = qq|SELECT p.id, p.pricegroup
2284 ORDER BY pricegroup|;
2287 $query = qq|SELECT id, pricegroup FROM pricegroup
2288 ORDER BY pricegroup|;
2291 my $sth = $dbh->prepare($query);
2292 $sth->execute || $self->dberror($query);
2294 $self->{all_pricegroup} = ();
2295 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2296 push @{ $self->{all_pricegroup} }, $ref;
2301 $main::lxdebug->leave_sub();
2305 my ($self, $dbh, $myconfig, $audittrail) = @_;
2307 # table, $reference, $formname, $action, $id, $transdate) = @_;
2314 $dbh = $self->dbconnect($myconfig);
2318 # if we have an id add audittrail, otherwise get a new timestamp
2320 if ($audittrail->{id}) {
2322 $query = qq|SELECT audittrail FROM defaults|;
2324 if ($dbh->selectrow_array($query)) {
2325 my ($null, $employee_id) = $self->get_employee($dbh);
2327 if ($self->{audittrail} && !$myconfig) {
2328 chop $self->{audittrail};
2330 my @a = split /\|/, $self->{audittrail};
2334 my @flds = qw(tablename reference formname action transdate);
2336 # put into hash and remove dups
2338 $key = "$a[2]$a[3]";
2340 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2344 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2345 formname, action, employee_id, transdate)
2346 VALUES ($audittrail->{id}, ?, ?,
2347 ?, ?, $employee_id, ?)|;
2348 my $sth = $dbh->prepare($query) || $self->dberror($query);
2352 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2356 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2358 $sth->execute || $self->dberror;
2363 if ($audittrail->{transdate}) {
2364 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2365 formname, action, employee_id, transdate) VALUES (
2366 $audittrail->{id}, '$audittrail->{tablename}', |
2367 . $dbh->quote($audittrail->{reference}) . qq|,
2368 '$audittrail->{formname}', '$audittrail->{action}',
2369 $employee_id, '$audittrail->{transdate}')|;
2371 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2372 formname, action, employee_id) VALUES ($audittrail->{id},
2373 '$audittrail->{tablename}', |
2374 . $dbh->quote($audittrail->{reference}) . qq|,
2375 '$audittrail->{formname}', '$audittrail->{action}',
2382 $query = qq|SELECT current_timestamp FROM defaults|;
2383 my ($timestamp) = $dbh->selectrow_array($query);
2386 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2389 $dbh->disconnect if $disconnect;
2398 $main::lxdebug->enter_sub();
2400 my ($type, $country, $NLS_file) = @_;
2403 if ($country && -d "locale/$country") {
2405 $self->{countrycode} = $country;
2406 if (open(IN, "locale/$country/$NLS_file")) {
2407 my $code = join("", <IN>);
2413 $self->{NLS_file} = $NLS_file;
2415 push @{ $self->{LONG_MONTH} },
2416 ("January", "February", "March", "April",
2417 "May ", "June", "July", "August",
2418 "September", "October", "November", "December");
2419 push @{ $self->{SHORT_MONTH} },
2420 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2422 $main::lxdebug->leave_sub();
2428 my ($self, $text) = @_;
2430 return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
2434 $main::lxdebug->enter_sub();
2436 my ($self, $text) = @_;
2438 if (exists $self->{subs}{$text}) {
2439 $text = $self->{subs}{$text};
2441 if ($self->{countrycode} && $self->{NLS_file}) {
2443 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2447 $main::lxdebug->leave_sub();
2453 $main::lxdebug->enter_sub();
2455 my ($self, $myconfig, $date, $longformat) = @_;
2458 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2463 $spc = $myconfig->{dateformat};
2465 $spc = substr($spc, 1, 1);
2467 if ($date =~ /\D/) {
2468 if ($myconfig->{dateformat} =~ /^yy/) {
2469 ($yy, $mm, $dd) = split /\D/, $date;
2471 if ($myconfig->{dateformat} =~ /^mm/) {
2472 ($mm, $dd, $yy) = split /\D/, $date;
2474 if ($myconfig->{dateformat} =~ /^dd/) {
2475 ($dd, $mm, $yy) = split /\D/, $date;
2478 $date = substr($date, 2);
2479 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2484 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2485 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2487 if ($myconfig->{dateformat} =~ /^dd/) {
2488 if (defined $longformat && $longformat == 0) {
2490 $dd = "0$dd" if ($dd < 10);
2491 $mm = "0$mm" if ($mm < 10);
2492 $longdate = "$dd$spc$mm$spc$yy";
2495 $longdate .= ($spc eq '.') ? ". " : " ";
2496 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2498 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2500 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2501 # Lx-Office is mainly used in Germany or German speaking countries.
2502 if (defined $longformat && $longformat == 0) {
2504 $dd = "0$dd" if ($dd < 10);
2505 $mm = "0$mm" if ($mm < 10);
2506 $longdate = "$yy-$mm-$dd";
2508 $longdate = "$dd. ";
2509 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2512 if (defined $longformat && $longformat == 0) {
2514 $dd = "0$dd" if ($dd < 10);
2515 $mm = "0$mm" if ($mm < 10);
2516 $longdate = "$mm$spc$dd$spc$yy";
2518 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2524 $main::lxdebug->leave_sub();
2530 $main::lxdebug->enter_sub();
2532 my ($self, $myconfig, $date, $longformat) = @_;
2535 $main::lxdebug->leave_sub();
2540 $spc = $myconfig->{dateformat};
2542 $spc = substr($spc, 1, 1);
2544 if ($date =~ /\D/) {
2545 if ($myconfig->{dateformat} =~ /^yy/) {
2546 ($yy, $mm, $dd) = split /\D/, $date;
2547 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2548 ($mm, $dd, $yy) = split /\D/, $date;
2549 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2550 ($dd, $mm, $yy) = split /\D/, $date;
2553 $date = substr($date, 2);
2554 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2559 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2560 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2562 $main::lxdebug->leave_sub();
2563 return ($yy, $mm, $dd);
2567 $main::lxdebug->enter_sub();
2569 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
2571 $main::lxdebug->leave_sub() and return "" unless ($date);
2573 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
2575 $output_format =~ /d+/;
2576 substr($output_format, $-[0], $+[0] - $-[0]) =
2577 sprintf("%0" . (length($&)) . "d", $dd);
2579 $output_format =~ /m+/;
2580 substr($output_format, $-[0], $+[0] - $-[0]) =
2581 sprintf("%0" . (length($&)) . "d", $mm);
2583 $output_format =~ /y+/;
2584 if (length($&) == 2) {
2585 $yy -= $yy >= 2000 ? 2000 : 1900;
2587 substr($output_format, $-[0], $+[0] - $-[0]) =
2588 sprintf("%0" . (length($&)) . "d", $yy);
2590 $main::lxdebug->leave_sub();
2592 return $output_format;