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 $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 ***********************************************/
448 $main::lxdebug->leave_sub();
451 sub parse_html_template {
452 $main::lxdebug->enter_sub();
454 my ($self, $file, $additional_params) = @_;
457 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
458 $language = $main::language;
460 $language = $main::myconfig{"countrycode"};
463 if (-f "templates/webpages/${file}_${language}.html") {
464 if ((-f ".developer") &&
465 (-f "templates/webpages/${file}_master.html") &&
466 ((stat("templates/webpages/${file}_master.html"))[9] >
467 (stat("templates/webpages/${file}_${language}.html"))[9])) {
468 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
469 "Please re-run 'locales.pl' in 'locale/${language}'.";
470 print(qq|<pre>$info</pre>|);
474 $file = "templates/webpages/${file}_${language}.html";
475 } elsif (-f "templates/webpages/${file}.html") {
476 $file = "templates/webpages/${file}.html";
478 my $info = "Web page template '${file}' not found.\n" .
479 "Please re-run 'locales.pl' in 'locale/${language}'.";
480 print(qq|<pre>$info</pre>|);
484 my $template = HTML::Template->new("filename" => $file,
485 "die_on_bad_params" => 0,
487 "case_sensitive" => 1,
488 "loop_context_vars" => 1,
491 $additional_params = {} unless ($additional_params);
492 if ($self->{"DEBUG"}) {
493 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
496 if ($additional_params->{"DEBUG"}) {
497 $additional_params->{"DEBUG"} =
498 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
501 if (%main::myconfig) {
502 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
503 my $jsc_dateformat = $main::myconfig{"dateformat"};
504 $jsc_dateformat =~ s/d+/\%d/gi;
505 $jsc_dateformat =~ s/m+/\%m/gi;
506 $jsc_dateformat =~ s/y+/\%Y/gi;
507 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
510 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
511 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
512 $additional_params->{"conf_latex_templates"} = $main::latex;
513 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
515 my @additional_param_names = keys(%{$additional_params});
516 foreach my $key ($template->param()) {
517 my $param = $self->{$key};
518 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
519 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
520 $template->param($key => $param);
523 my $output = $template->output();
525 $main::lxdebug->leave_sub();
530 sub show_generic_error {
531 my ($self, $error, $title, $action) = @_;
534 $add_params->{"title"} = $title if ($title);
535 $self->{"label_error"} = $error;
539 map({ delete($self->{$_}); } qw(action));
540 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
541 if (!ref($self->{$_})); }
543 $add_params->{"SHOW_BUTTON"} = 1;
544 $add_params->{"BUTTON_LABEL"} = $action;
546 $add_params->{"VARIABLES"} = \@vars;
549 print($self->parse_html_template("generic/error", $add_params));
551 die("Error: $error\n");
554 sub show_generic_information {
555 my ($self, $error, $title) = @_;
558 $add_params->{"title"} = $title if ($title);
559 $self->{"label_information"} = $error;
562 print($self->parse_html_template("generic/information", $add_params));
564 die("Information: $error\n");
567 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
568 # changed it to accept an arbitrary number of triggers - sschoeling
570 $main::lxdebug->enter_sub();
573 my $myconfig = shift;
576 # set dateform for jsscript
578 $ifFormat = "%d.%m.%Y";
579 if ($myconfig->{dateformat} eq "dd.mm.yy") {
580 $ifFormat = "%d.%m.%Y";
582 if ($myconfig->{dateformat} eq "dd-mm-yy") {
583 $ifFormat = "%d-%m-%Y";
585 if ($myconfig->{dateformat} eq "dd/mm/yy") {
586 $ifFormat = "%d/%m/%Y";
588 if ($myconfig->{dateformat} eq "mm/dd/yy") {
589 $ifFormat = "%m/%d/%Y";
591 if ($myconfig->{dateformat} eq "mm-dd-yy") {
592 $ifFormat = "%m-%d-%Y";
594 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
595 $ifFormat = "%Y-%m-%d";
607 inputField : "| . (shift) . qq|",
608 ifFormat :"$ifFormat",
609 align : "| . (shift) . qq|",
610 button : "| . (shift) . qq|"
616 <script type="text/javascript">
617 <!--| . join("", @triggers) . qq|//-->
621 $main::lxdebug->leave_sub();
624 } #end sub write_trigger
627 $main::lxdebug->enter_sub();
629 my ($self, $msg) = @_;
631 if ($self->{callback}) {
633 ($script, $argv) = split(/\?/, $self->{callback});
634 exec("perl", "$script", $argv);
642 $main::lxdebug->leave_sub();
645 # sort of columns removed - empty sub
647 $main::lxdebug->enter_sub();
649 my ($self, @columns) = @_;
651 $main::lxdebug->leave_sub();
657 $main::lxdebug->enter_sub(2);
659 my ($self, $myconfig, $amount, $places, $dash) = @_;
664 my $neg = ($amount =~ s/-//);
666 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
668 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
669 my @p = split(/\./, $amount); # split amount at decimal point
671 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
674 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
677 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
678 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
679 ($neg ? "-$amount" : "$amount" ) ;
683 $main::lxdebug->leave_sub(2);
688 $main::lxdebug->enter_sub(2);
690 my ($self, $myconfig, $amount) = @_;
692 if ($myconfig->{in_numberformat} == 1) {
693 # Extra input number format 1000.00 or 1000,00
695 $amount = scalar reverse $amount;
696 $amount =~ s/\./DOT/;
698 $amount =~ s/DOT/\./;
699 $amount = scalar reverse $amount;
700 $main::lxdebug->leave_sub(2);
701 return ($amount * 1);
704 if ( ($myconfig->{numberformat} eq '1.000,00')
705 || ($myconfig->{numberformat} eq '1000,00')) {
710 if ($myconfig->{numberformat} eq "1'000.00") {
716 $main::lxdebug->leave_sub(2);
718 return ($amount * 1);
722 $main::lxdebug->enter_sub(2);
724 my ($self, $amount, $places) = @_;
727 # Rounding like "Kaufmannsrunden"
728 # Descr. http://de.wikipedia.org/wiki/Rundung
730 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
733 $amount = $amount * (10**($places));
734 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
736 $main::lxdebug->leave_sub(2);
738 return $round_amount;
743 $main::lxdebug->enter_sub();
745 my ($self, $myconfig, $userspath) = @_;
748 $self->{"cwd"} = getcwd();
749 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
751 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
752 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
753 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
754 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
755 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
756 } elsif (($self->{"format"} =~ /html/i) ||
757 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
758 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
759 } elsif (($self->{"format"} =~ /xml/i) ||
760 (!$self->{"format"} && ($self->{"IN"} =~ /xml$/i))) {
761 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
762 } elsif ( $self->{"format"} =~ /elsterwinston/i ) {
763 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
764 } elsif ( $self->{"format"} =~ /elstertaxbird/i ) {
765 $template = XMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
766 } elsif ( defined $self->{'format'}) {
767 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
768 } elsif ( $self->{'format'} eq '' ) {
769 $self->error("No Outputformat given: $self->{'format'}");
770 } else { #Catch the rest
771 $self->error("Outputformat not defined: $self->{'format'}");
774 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
775 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
777 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
778 qw(email tel fax name signature company address businessnumber
779 co_ustid taxnumber duns));
780 map({ $self->{"employee_${_}"} =~ s/\\n/\n/g; }
781 qw(company address signature));
783 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
785 # OUT is used for the media, screen, printer, email
786 # for postscript we store a copy in a temporary file
788 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}" if ( $self->{tmpfile} eq '' );
789 if ($template->uses_temp_file() || $self->{media} eq 'email') {
791 $self->{OUT} = ">$self->{tmpfile}";
795 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
797 open(OUT, ">-") or $self->error("STDOUT : $!");
801 if (!$template->parse(*OUT)) {
803 $self->error("$self->{IN} : " . $template->get_error());
808 if ($template->uses_temp_file() || $self->{media} eq 'email') {
810 if ($self->{media} eq 'email') {
814 my $mail = new Mailer;
816 map { $mail->{$_} = $self->{$_} }
817 qw(cc bcc subject message version format charset);
818 $mail->{to} = qq|$self->{email}|;
819 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
820 $mail->{fileid} = "$fileid.";
821 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
823 # if we send html or plain text inline
824 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
825 $mail->{contenttype} = "text/html";
827 $mail->{message} =~ s/\r\n/<br>\n/g;
828 $myconfig->{signature} =~ s/\\n/<br>\n/g;
829 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
831 open(IN, $self->{tmpfile})
832 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
834 $mail->{message} .= $_;
841 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
843 $mail->{message} =~ s/\r\n/\n/g;
844 $myconfig->{signature} =~ s/\\n/\n/g;
845 $mail->{message} .= "\n-- \n$myconfig->{signature}";
849 my $err = $mail->send($out);
850 $self->error($self->cleanup . "$err") if ($err);
856 my $numbytes = (-s $self->{tmpfile});
857 open(IN, $self->{tmpfile})
858 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
860 $self->{copies} = 1 unless $self->{media} eq 'printer';
862 chdir("$self->{cwd}");
863 #print(STDERR "Kopien $self->{copies}\n");
864 #print(STDERR "OUT $self->{OUT}\n");
865 for my $i (1 .. $self->{copies}) {
867 open(OUT, $self->{OUT})
868 or $self->error($self->cleanup . "$self->{OUT} : $!");
872 print qq|Content-Type: | . $template->get_mime_type() . qq|
873 Content-Disposition: attachment; filename="$self->{tmpfile}"
874 Content-Length: $numbytes
878 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
898 chdir("$self->{cwd}");
899 $main::lxdebug->leave_sub();
903 $main::lxdebug->enter_sub();
907 chdir("$self->{tmpdir}");
910 if (-f "$self->{tmpfile}.err") {
911 open(FH, "$self->{tmpfile}.err");
916 if ($self->{tmpfile}) {
917 $self->{tmpfile} =~ s|.*/||g;
919 $self->{tmpfile} =~ s/\.\w+$//g;
920 my $tmpfile = $self->{tmpfile};
921 unlink(<$tmpfile.*>);
924 chdir("$self->{cwd}");
926 $main::lxdebug->leave_sub();
932 $main::lxdebug->enter_sub();
934 my ($self, $date, $myconfig) = @_;
936 if ($date && $date =~ /\D/) {
938 if ($myconfig->{dateformat} =~ /^yy/) {
939 ($yy, $mm, $dd) = split /\D/, $date;
941 if ($myconfig->{dateformat} =~ /^mm/) {
942 ($mm, $dd, $yy) = split /\D/, $date;
944 if ($myconfig->{dateformat} =~ /^dd/) {
945 ($dd, $mm, $yy) = split /\D/, $date;
950 $yy = ($yy < 70) ? $yy + 2000 : $yy;
951 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
953 $dd = "0$dd" if ($dd < 10);
954 $mm = "0$mm" if ($mm < 10);
959 $main::lxdebug->leave_sub();
964 # Database routines used throughout
967 $main::lxdebug->enter_sub();
969 my ($self, $myconfig) = @_;
971 # connect to database
973 DBI->connect($myconfig->{dbconnect},
974 $myconfig->{dbuser}, $myconfig->{dbpasswd})
978 if ($myconfig->{dboptions}) {
979 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
982 $main::lxdebug->leave_sub();
987 sub dbconnect_noauto {
988 $main::lxdebug->enter_sub();
990 my ($self, $myconfig) = @_;
992 # connect to database
994 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
995 $myconfig->{dbpasswd}, { AutoCommit => 0 })
999 if ($myconfig->{dboptions}) {
1000 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
1003 $main::lxdebug->leave_sub();
1008 sub update_balance {
1009 $main::lxdebug->enter_sub();
1011 my ($self, $dbh, $table, $field, $where, $value) = @_;
1013 # if we have a value, go do it
1016 # retrieve balance from table
1017 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1018 my $sth = $dbh->prepare($query);
1020 $sth->execute || $self->dberror($query);
1021 my ($balance) = $sth->fetchrow_array;
1027 $query = "UPDATE $table SET $field = $balance WHERE $where";
1028 $dbh->do($query) || $self->dberror($query);
1030 $main::lxdebug->leave_sub();
1033 sub update_exchangerate {
1034 $main::lxdebug->enter_sub();
1036 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1038 # some sanity check for currency
1040 $main::lxdebug->leave_sub();
1044 my $query = qq|SELECT e.curr FROM exchangerate e
1045 WHERE e.curr = '$curr'
1046 AND e.transdate = '$transdate'
1048 my $sth = $dbh->prepare($query);
1049 $sth->execute || $self->dberror($query);
1052 if ($buy != 0 && $sell != 0) {
1053 $set = "buy = $buy, sell = $sell";
1054 } elsif ($buy != 0) {
1055 $set = "buy = $buy";
1056 } elsif ($sell != 0) {
1057 $set = "sell = $sell";
1060 if ($sth->fetchrow_array) {
1061 $query = qq|UPDATE exchangerate
1063 WHERE curr = '$curr'
1064 AND transdate = '$transdate'|;
1066 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1067 VALUES ('$curr', $buy, $sell, '$transdate')|;
1070 $dbh->do($query) || $self->dberror($query);
1072 $main::lxdebug->leave_sub();
1075 sub save_exchangerate {
1076 $main::lxdebug->enter_sub();
1078 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1080 my $dbh = $self->dbconnect($myconfig);
1082 my ($buy, $sell) = (0, 0);
1083 $buy = $rate if $fld eq 'buy';
1084 $sell = $rate if $fld eq 'sell';
1086 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1090 $main::lxdebug->leave_sub();
1093 sub get_exchangerate {
1094 $main::lxdebug->enter_sub();
1096 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1098 unless ($transdate) {
1099 $main::lxdebug->leave_sub();
1103 my $query = qq|SELECT e.$fld FROM exchangerate e
1104 WHERE e.curr = '$curr'
1105 AND e.transdate = '$transdate'|;
1106 my $sth = $dbh->prepare($query);
1107 $sth->execute || $self->dberror($query);
1109 my ($exchangerate) = $sth->fetchrow_array;
1112 if ($exchangerate == 0) {
1116 $main::lxdebug->leave_sub();
1118 return $exchangerate;
1121 sub set_payment_options {
1122 $main::lxdebug->enter_sub();
1124 my ($self, $myconfig, $transdate) = @_;
1126 if ($self->{payment_id}) {
1128 my $dbh = $self->dbconnect($myconfig);
1131 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1132 WHERE p.id = $self->{payment_id}|;
1133 my $sth = $dbh->prepare($query);
1134 $sth->execute || $self->dberror($query);
1136 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1139 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1141 my $sth = $dbh->prepare($query);
1142 $sth->execute || $self->dberror($query);
1143 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1146 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1148 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1149 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1150 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1151 $self->{payment_terms} =~ s/<%total%>/$self->{total}/g;
1152 $self->{payment_terms} =~ s/<%invtotal%>/$self->{invtotal}/g;
1153 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1154 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1155 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1156 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1157 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1162 $main::lxdebug->leave_sub();
1166 sub check_exchangerate {
1167 $main::lxdebug->enter_sub();
1169 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1171 unless ($transdate) {
1172 $main::lxdebug->leave_sub();
1176 my $dbh = $self->dbconnect($myconfig);
1178 my $query = qq|SELECT e.$fld FROM exchangerate e
1179 WHERE e.curr = '$currency'
1180 AND e.transdate = '$transdate'|;
1181 my $sth = $dbh->prepare($query);
1182 $sth->execute || $self->dberror($query);
1184 my ($exchangerate) = $sth->fetchrow_array;
1188 $main::lxdebug->leave_sub();
1190 return $exchangerate;
1193 sub get_template_language {
1194 $main::lxdebug->enter_sub();
1196 my ($self, $myconfig) = @_;
1198 my $template_code = "";
1200 if ($self->{language_id}) {
1202 my $dbh = $self->dbconnect($myconfig);
1205 my $query = qq|SELECT l.template_code FROM language l
1206 WHERE l.id = $self->{language_id}|;
1207 my $sth = $dbh->prepare($query);
1208 $sth->execute || $self->dberror($query);
1210 ($template_code) = $sth->fetchrow_array;
1215 $main::lxdebug->leave_sub();
1217 return $template_code;
1220 sub get_printer_code {
1221 $main::lxdebug->enter_sub();
1223 my ($self, $myconfig) = @_;
1225 my $template_code = "";
1227 if ($self->{printer_id}) {
1229 my $dbh = $self->dbconnect($myconfig);
1232 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1233 WHERE p.id = $self->{printer_id}|;
1234 my $sth = $dbh->prepare($query);
1235 $sth->execute || $self->dberror($query);
1237 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1242 $main::lxdebug->leave_sub();
1244 return $template_code;
1248 $main::lxdebug->enter_sub();
1250 my ($self, $myconfig) = @_;
1252 my $template_code = "";
1254 if ($self->{shipto_id}) {
1256 my $dbh = $self->dbconnect($myconfig);
1259 my $query = qq|SELECT s.* FROM shipto s
1260 WHERE s.shipto_id = $self->{shipto_id}|;
1261 my $sth = $dbh->prepare($query);
1262 $sth->execute || $self->dberror($query);
1263 $ref = $sth->fetchrow_hashref(NAME_lc);
1264 map { $form->{$_} = $ref->{$_} } keys %$ref;
1269 $main::lxdebug->leave_sub();
1274 $main::lxdebug->enter_sub();
1276 my ($self, $dbh, $id, $module) = @_;
1280 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1282 if ($self->{"shipto$item"}) {
1283 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1285 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1288 if ($self->{shipto_id}) {
1289 my $query = qq| UPDATE shipto set
1290 shiptoname = '$self->{shiptoname}',
1291 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1292 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1293 shiptostreet = '$self->{shiptostreet}',
1294 shiptozipcode = '$self->{shiptozipcode}',
1295 shiptocity = '$self->{shiptocity}',
1296 shiptocountry = '$self->{shiptocountry}',
1297 shiptocontact = '$self->{shiptocontact}',
1298 shiptophone = '$self->{shiptophone}',
1299 shiptofax = '$self->{shiptofax}',
1300 shiptoemail = '$self->{shiptoemail}'
1301 WHERE shipto_id = $self->{shipto_id}|;
1302 $dbh->do($query) || $self->dberror($query);
1305 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1306 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1307 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1308 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1309 '$self->{shiptozipcode}', '$self->{shiptocity}',
1310 '$self->{shiptocountry}', '$self->{shiptocontact}',
1311 '$self->{shiptophone}', '$self->{shiptofax}',
1312 '$self->{shiptoemail}', '$module')|;
1313 $dbh->do($query) || $self->dberror($query);
1317 $main::lxdebug->leave_sub();
1321 $main::lxdebug->enter_sub();
1323 my ($self, $dbh) = @_;
1325 my $query = qq|SELECT e.id, e.name FROM employee e
1326 WHERE e.login = '$self->{login}'|;
1327 my $sth = $dbh->prepare($query);
1328 $sth->execute || $self->dberror($query);
1330 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1331 $self->{employee_id} *= 1;
1335 $main::lxdebug->leave_sub();
1339 $main::lxdebug->enter_sub();
1341 my ($self, $myconfig) = @_;
1343 my $dbh = $self->dbconnect($myconfig);
1344 my $query = qq|SELECT current_date+terms_netto FROM payment_terms
1345 WHERE id = '$self->{payment_id}'|;
1346 my $sth = $dbh->prepare($query);
1347 $sth->execute || $self->dberror($query);
1349 ($self->{duedate}) = $sth->fetchrow_array;
1353 $main::lxdebug->leave_sub();
1356 # get other contact for transaction and form - html/tex
1358 $main::lxdebug->enter_sub();
1360 my ($self, $dbh, $id) = @_;
1362 my $query = qq|SELECT c.*
1365 $sth = $dbh->prepare($query);
1366 $sth->execute || $self->dberror($query);
1368 $ref = $sth->fetchrow_hashref(NAME_lc);
1370 push @{ $self->{$_} }, $ref;
1373 $main::lxdebug->leave_sub();
1376 # get contacts for id, if no contact return {"","","","",""}
1378 $main::lxdebug->enter_sub();
1380 my ($self, $dbh, $id) = @_;
1382 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1384 WHERE cp_cv_id=$id|;
1385 my $sth = $dbh->prepare($query);
1386 $sth->execute || $self->dberror($query);
1389 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1390 push @{ $self->{all_contacts} }, $ref;
1395 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1398 $main::lxdebug->leave_sub();
1401 # this sub gets the id and name from $table
1403 $main::lxdebug->enter_sub();
1405 my ($self, $myconfig, $table) = @_;
1407 # connect to database
1408 my $dbh = $self->dbconnect($myconfig);
1410 my $name = $self->like(lc $self->{$table});
1411 my $customernumber = $self->like(lc $self->{customernumber});
1413 if ($self->{customernumber} ne "") {
1414 $query = qq~SELECT c.id, c.name,
1415 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1417 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1420 $query = qq~SELECT c.id, c.name,
1421 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1423 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1427 if ($self->{openinvoices}) {
1428 $query = qq~SELECT DISTINCT c.id, c.name,
1429 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1430 FROM $self->{arap} a
1431 JOIN $table c ON (a.${table}_id = c.id)
1432 WHERE NOT a.amount = a.paid
1433 AND lower(c.name) LIKE '$name'
1436 my $sth = $dbh->prepare($query);
1438 $sth->execute || $self->dberror($query);
1441 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1442 push(@{ $self->{name_list} }, $ref);
1448 $main::lxdebug->leave_sub();
1453 # the selection sub is used in the AR, AP, IS, IR and OE module
1456 $main::lxdebug->enter_sub();
1458 my ($self, $myconfig, $table, $module) = @_;
1461 my $dbh = $self->dbconnect($myconfig);
1463 my $query = qq|SELECT count(*) FROM $table|;
1464 my $sth = $dbh->prepare($query);
1465 $sth->execute || $self->dberror($query);
1466 my ($count) = $sth->fetchrow_array;
1469 # build selection list
1470 if ($count < $myconfig->{vclimit}) {
1471 $query = qq|SELECT id, name
1472 FROM $table WHERE not obsolete
1474 $sth = $dbh->prepare($query);
1475 $sth->execute || $self->dberror($query);
1477 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1478 push @{ $self->{"all_$table"} }, $ref;
1486 $self->get_employee($dbh);
1488 # setup sales contacts
1489 $query = qq|SELECT e.id, e.name
1492 AND NOT e.id = $self->{employee_id}|;
1493 $sth = $dbh->prepare($query);
1494 $sth->execute || $self->dberror($query);
1496 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1497 push @{ $self->{all_employees} }, $ref;
1502 push @{ $self->{all_employees} },
1503 { id => $self->{employee_id},
1504 name => $self->{employee} };
1506 # sort the whole thing
1507 @{ $self->{all_employees} } =
1508 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1510 if ($module eq 'AR') {
1512 # prepare query for departments
1513 $query = qq|SELECT d.id, d.description
1519 $query = qq|SELECT d.id, d.description
1524 $sth = $dbh->prepare($query);
1525 $sth->execute || $self->dberror($query);
1527 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1528 push @{ $self->{all_departments} }, $ref;
1533 $query = qq|SELECT id, description
1536 $sth = $dbh->prepare($query);
1537 $sth->execute || $form->dberror($query);
1539 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1540 push @{ $self->{languages} }, $ref;
1545 $query = qq|SELECT printer_description, id
1548 $sth = $dbh->prepare($query);
1549 $sth->execute || $form->dberror($query);
1551 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1552 push @{ $self->{printers} }, $ref;
1558 $query = qq|SELECT id, description
1561 $sth = $dbh->prepare($query);
1562 $sth->execute || $form->dberror($query);
1564 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1565 push @{ $self->{payment_terms} }, $ref;
1569 $main::lxdebug->leave_sub();
1573 sub language_payment {
1574 $main::lxdebug->enter_sub();
1576 my ($self, $myconfig) = @_;
1577 undef $self->{languages};
1578 undef $self->{payment_terms};
1579 undef $self->{printers};
1582 my $dbh = $self->dbconnect($myconfig);
1584 my $query = qq|SELECT id, description
1587 my $sth = $dbh->prepare($query);
1588 $sth->execute || $form->dberror($query);
1590 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1591 push @{ $self->{languages} }, $ref;
1596 $query = qq|SELECT printer_description, id
1599 $sth = $dbh->prepare($query);
1600 $sth->execute || $form->dberror($query);
1602 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1603 push @{ $self->{printers} }, $ref;
1608 $query = qq|SELECT id, description
1611 $sth = $dbh->prepare($query);
1612 $sth->execute || $form->dberror($query);
1614 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1615 push @{ $self->{payment_terms} }, $ref;
1619 # get buchungsgruppen
1620 $query = qq|SELECT id, description
1621 FROM buchungsgruppen|;
1622 $sth = $dbh->prepare($query);
1623 $sth->execute || $form->dberror($query);
1625 $self->{BUCHUNGSGRUPPEN} = [];
1626 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1627 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1632 $main::lxdebug->leave_sub();
1635 # this is only used for reports
1636 sub all_departments {
1637 $main::lxdebug->enter_sub();
1639 my ($self, $myconfig, $table) = @_;
1641 my $dbh = $self->dbconnect($myconfig);
1642 my $where = "1 = 1";
1644 if (defined $table) {
1645 if ($table eq 'customer') {
1646 $where = " d.role = 'P'";
1650 my $query = qq|SELECT d.id, d.description
1654 my $sth = $dbh->prepare($query);
1655 $sth->execute || $self->dberror($query);
1657 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1658 push @{ $self->{all_departments} }, $ref;
1664 $main::lxdebug->leave_sub();
1668 $main::lxdebug->enter_sub();
1670 my ($self, $module, $myconfig, $table) = @_;
1672 $self->all_vc($myconfig, $table, $module);
1674 # get last customers or vendors
1677 my $dbh = $self->dbconnect($myconfig);
1682 my $transdate = "current_date";
1683 if ($self->{transdate}) {
1684 $transdate = qq|'$self->{transdate}'|;
1687 # now get the account numbers
1688 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1689 FROM chart c, taxkeys tk
1690 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)
1693 $sth = $dbh->prepare($query);
1694 $sth->execute || $self->dberror($query);
1696 $self->{accounts} = "";
1697 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1699 foreach my $key (split(/:/, $ref->{link})) {
1700 if ($key =~ /$module/) {
1702 # cross reference for keys
1703 $xkeyref{ $ref->{accno} } = $key;
1705 push @{ $self->{"${module}_links"}{$key} },
1706 { accno => $ref->{accno},
1707 description => $ref->{description},
1708 taxkey => $ref->{taxkey_id},
1709 tax_id => $ref->{tax_id} };
1711 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1717 # get taxkeys and description
1718 $query = qq|SELECT id, taxkey, taxdescription
1720 $sth = $dbh->prepare($query);
1721 $sth->execute || $self->dberror($query);
1723 $ref = $sth->fetchrow_hashref(NAME_lc);
1725 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1726 push @{ $self->{TAXKEY} }, $ref;
1733 $query = qq|SELECT id, description
1735 $sth = $dbh->prepare($query);
1736 $sth->execute || $form->dberror($query);
1739 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1740 push @{ $self->{TAXZONE} }, $ref;
1744 if (($module eq "AP") || ($module eq "AR")) {
1746 # get tax rates and description
1747 $query = qq| SELECT * FROM tax t|;
1748 $sth = $dbh->prepare($query);
1749 $sth->execute || $self->dberror($query);
1751 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1752 push @{ $self->{TAX} }, $ref;
1758 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1760 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1761 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1762 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1763 c.name AS $table, a.department_id, d.description AS department,
1764 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1765 a.employee_id, e.name AS employee, a.gldate, a.type
1767 JOIN $table c ON (a.${table}_id = c.id)
1768 LEFT JOIN employee e ON (e.id = a.employee_id)
1769 LEFT JOIN department d ON (d.id = a.department_id)
1770 WHERE a.id = $self->{id}|;
1771 $sth = $dbh->prepare($query);
1772 $sth->execute || $self->dberror($query);
1774 $ref = $sth->fetchrow_hashref(NAME_lc);
1775 foreach $key (keys %$ref) {
1776 $self->{$key} = $ref->{$key};
1781 my $transdate = "current_date";
1782 if ($self->{transdate}) {
1783 $transdate = qq|'$self->{transdate}'|;
1786 # now get the account numbers
1787 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1788 FROM chart c, taxkeys tk
1789 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%'))
1792 $sth = $dbh->prepare($query);
1793 $sth->execute || $self->dberror($query);
1795 $self->{accounts} = "";
1796 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1798 foreach my $key (split(/:/, $ref->{link})) {
1799 if ($key =~ /$module/) {
1801 # cross reference for keys
1802 $xkeyref{ $ref->{accno} } = $key;
1804 push @{ $self->{"${module}_links"}{$key} },
1805 { accno => $ref->{accno},
1806 description => $ref->{description},
1807 taxkey => $ref->{taxkey_id},
1808 tax_id => $ref->{tax_id} };
1810 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1816 # get amounts from individual entries
1817 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1818 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
1820 JOIN chart c ON (c.id = a.chart_id)
1821 LEFT JOIN project p ON (p.id = a.project_id)
1822 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))
1823 WHERE a.trans_id = $self->{id}
1824 AND a.fx_transaction = '0'
1825 ORDER BY a.oid,a.transdate|;
1826 $sth = $dbh->prepare($query);
1827 $sth->execute || $self->dberror($query);
1829 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1831 # get exchangerate for currency
1832 $self->{exchangerate} =
1833 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1837 # store amounts in {acc_trans}{$key} for multiple accounts
1838 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1839 $ref->{exchangerate} =
1840 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1842 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1845 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1846 $ref->{amount} *= -1;
1848 $ref->{index} = $index;
1850 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1854 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1855 (SELECT c.accno FROM chart c
1856 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1857 (SELECT c.accno FROM chart c
1858 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1860 $sth = $dbh->prepare($query);
1861 $sth->execute || $self->dberror($query);
1863 $ref = $sth->fetchrow_hashref(NAME_lc);
1864 map { $self->{$_} = $ref->{$_} } keys %$ref;
1870 $query = qq|SELECT current_date AS transdate,
1871 d.curr AS currencies, d.closedto, d.revtrans,
1872 (SELECT c.accno FROM chart c
1873 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1874 (SELECT c.accno FROM chart c
1875 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1877 $sth = $dbh->prepare($query);
1878 $sth->execute || $self->dberror($query);
1880 $ref = $sth->fetchrow_hashref(NAME_lc);
1881 map { $self->{$_} = $ref->{$_} } keys %$ref;
1884 if ($self->{"$self->{vc}_id"}) {
1886 # only setup currency
1887 ($self->{currency}) = split(/:/, $self->{currencies});
1891 $self->lastname_used($dbh, $myconfig, $table, $module);
1893 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1895 # get exchangerate for currency
1896 $self->{exchangerate} =
1897 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1908 $main::lxdebug->leave_sub();
1912 $main::lxdebug->enter_sub();
1914 my ($self, $dbh, $myconfig, $table, $module) = @_;
1916 my $arap = ($table eq 'customer') ? "ar" : "ap";
1917 my $where = "1 = 1";
1919 if ($self->{type} =~ /_order/) {
1921 $where = "quotation = '0'";
1923 if ($self->{type} =~ /_quotation/) {
1925 $where = "quotation = '1'";
1928 my $query = qq|SELECT MAX(id) FROM $arap
1930 AND ${table}_id > 0|;
1931 my $sth = $dbh->prepare($query);
1932 $sth->execute || $self->dberror($query);
1934 my ($trans_id) = $sth->fetchrow_array;
1938 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1939 current_date + ct.terms AS duedate, a.department_id,
1940 d.description AS department
1942 JOIN $table ct ON (a.${table}_id = ct.id)
1943 LEFT JOIN department d ON (a.department_id = d.id)
1944 WHERE a.id = $trans_id|;
1945 $sth = $dbh->prepare($query);
1946 $sth->execute || $self->dberror($query);
1948 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1949 $self->{duedate}, $self->{department_id}, $self->{department})
1950 = $sth->fetchrow_array;
1953 $main::lxdebug->leave_sub();
1957 $main::lxdebug->enter_sub();
1959 my ($self, $myconfig, $thisdate, $days) = @_;
1961 my $dbh = $self->dbconnect($myconfig);
1966 my $dateformat = $myconfig->{dateformat};
1967 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1969 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1971 $sth = $dbh->prepare($query);
1972 $sth->execute || $self->dberror($query);
1974 $query = qq|SELECT current_date AS thisdate
1976 $sth = $dbh->prepare($query);
1977 $sth->execute || $self->dberror($query);
1980 ($thisdate) = $sth->fetchrow_array;
1985 $main::lxdebug->leave_sub();
1991 $main::lxdebug->enter_sub();
1993 my ($self, $string) = @_;
1995 if ($string !~ /%/) {
1996 $string = "%$string%";
1999 $string =~ s/\'/\'\'/g;
2001 $main::lxdebug->leave_sub();
2007 $main::lxdebug->enter_sub();
2009 my ($self, $flds, $new, $count, $numrows) = @_;
2013 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
2019 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2021 $j = $item->{ndx} - 1;
2022 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2026 for $i ($count + 1 .. $numrows) {
2027 map { delete $self->{"${_}_$i"} } @{$flds};
2030 $main::lxdebug->leave_sub();
2034 $main::lxdebug->enter_sub();
2036 my ($self, $myconfig) = @_;
2040 my $dbh = $self->dbconnect_noauto($myconfig);
2042 my $query = qq|DELETE FROM status
2043 WHERE formname = '$self->{formname}'
2045 my $sth = $dbh->prepare($query) || $self->dberror($query);
2047 if ($self->{formname} =~ /(check|receipt)/) {
2048 for $i (1 .. $self->{rowcount}) {
2049 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2053 $sth->execute($self->{id}) || $self->dberror($query);
2057 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2058 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2060 my %queued = split / /, $self->{queued};
2062 if ($self->{formname} =~ /(check|receipt)/) {
2064 # this is a check or receipt, add one entry for each lineitem
2065 my ($accno) = split /--/, $self->{account};
2066 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2067 chart_id) VALUES (?, '$printed',
2068 '$queued{$self->{formname}}', '$self->{prinform}',
2069 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2070 $sth = $dbh->prepare($query) || $self->dberror($query);
2072 for $i (1 .. $self->{rowcount}) {
2073 if ($self->{"checked_$i"}) {
2074 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2079 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2080 spoolfile, formname)
2081 VALUES ($self->{id}, '$printed', '$emailed',
2082 '$queued{$self->{formname}}', '$self->{formname}')|;
2083 $dbh->do($query) || $self->dberror($query);
2089 $main::lxdebug->leave_sub();
2093 $main::lxdebug->enter_sub();
2095 my ($self, $dbh) = @_;
2097 my ($query, $printed, $emailed);
2099 my $formnames = $self->{printed};
2100 my $emailforms = $self->{emailed};
2102 my $query = qq|DELETE FROM status
2103 WHERE formname = '$self->{formname}'
2104 AND trans_id = $self->{id}|;
2105 $dbh->do($query) || $self->dberror($query);
2107 # this only applies to the forms
2108 # checks and receipts are posted when printed or queued
2110 if ($self->{queued}) {
2111 my %queued = split / /, $self->{queued};
2113 foreach my $formname (keys %queued) {
2114 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2115 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2117 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2118 spoolfile, formname)
2119 VALUES ($self->{id}, '$printed', '$emailed',
2120 '$queued{$formname}', '$formname')|;
2121 $dbh->do($query) || $self->dberror($query);
2123 $formnames =~ s/$self->{formname}//;
2124 $emailforms =~ s/$self->{formname}//;
2129 # save printed, emailed info
2130 $formnames =~ s/^ +//g;
2131 $emailforms =~ s/^ +//g;
2134 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2135 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2137 foreach my $formname (keys %status) {
2138 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2139 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2141 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2142 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2143 $dbh->do($query) || $self->dberror($query);
2146 $main::lxdebug->leave_sub();
2149 sub update_defaults {
2150 $main::lxdebug->enter_sub();
2152 my ($self, $myconfig, $fld) = @_;
2154 my $dbh = $self->dbconnect_noauto($myconfig);
2155 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2156 my $sth = $dbh->prepare($query);
2158 $sth->execute || $self->dberror($query);
2159 my ($var) = $sth->fetchrow_array;
2164 $query = qq|UPDATE defaults
2166 $dbh->do($query) || $self->dberror($query);
2171 $main::lxdebug->leave_sub();
2176 sub update_business {
2177 $main::lxdebug->enter_sub();
2179 my ($self, $myconfig, $business_id) = @_;
2181 my $dbh = $self->dbconnect_noauto($myconfig);
2183 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2184 my $sth = $dbh->prepare($query);
2186 $sth->execute || $self->dberror($query);
2187 my ($var) = $sth->fetchrow_array;
2192 $query = qq|UPDATE business
2193 SET customernumberinit = '$var' WHERE id=$business_id|;
2194 $dbh->do($query) || $self->dberror($query);
2199 $main::lxdebug->leave_sub();
2205 $main::lxdebug->enter_sub();
2207 my ($self, $myconfig, $salesman) = @_;
2209 my $dbh = $self->dbconnect($myconfig);
2211 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2212 my $sth = $dbh->prepare($query);
2213 $sth->execute || $self->dberror($query);
2216 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2217 push(@{ $self->{salesman_list} }, $ref);
2221 $main::lxdebug->leave_sub();
2226 sub get_partsgroup {
2227 $main::lxdebug->enter_sub();
2229 my ($self, $myconfig, $p) = @_;
2231 my $dbh = $self->dbconnect($myconfig);
2233 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2235 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2237 if ($p->{searchitems} eq 'part') {
2239 WHERE p.inventory_accno_id > 0|;
2241 if ($p->{searchitems} eq 'service') {
2243 WHERE p.inventory_accno_id IS NULL|;
2245 if ($p->{searchitems} eq 'assembly') {
2247 WHERE p.assembly = '1'|;
2249 if ($p->{searchitems} eq 'labor') {
2251 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2255 ORDER BY partsgroup|;
2258 $query = qq|SELECT id, partsgroup FROM partsgroup
2259 ORDER BY partsgroup|;
2262 if ($p->{language_code}) {
2263 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2264 t.description AS translation
2266 JOIN parts p ON (p.partsgroup_id = pg.id)
2267 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2268 ORDER BY translation|;
2271 my $sth = $dbh->prepare($query);
2272 $sth->execute || $self->dberror($query);
2274 $self->{all_partsgroup} = ();
2275 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2276 push @{ $self->{all_partsgroup} }, $ref;
2280 $main::lxdebug->leave_sub();
2283 sub get_pricegroup {
2284 $main::lxdebug->enter_sub();
2286 my ($self, $myconfig, $p) = @_;
2288 my $dbh = $self->dbconnect($myconfig);
2290 my $query = qq|SELECT p.id, p.pricegroup
2294 ORDER BY pricegroup|;
2297 $query = qq|SELECT id, pricegroup FROM pricegroup
2298 ORDER BY pricegroup|;
2301 my $sth = $dbh->prepare($query);
2302 $sth->execute || $self->dberror($query);
2304 $self->{all_pricegroup} = ();
2305 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2306 push @{ $self->{all_pricegroup} }, $ref;
2311 $main::lxdebug->leave_sub();
2315 my ($self, $dbh, $myconfig, $audittrail) = @_;
2317 # table, $reference, $formname, $action, $id, $transdate) = @_;
2324 $dbh = $self->dbconnect($myconfig);
2328 # if we have an id add audittrail, otherwise get a new timestamp
2330 if ($audittrail->{id}) {
2332 $query = qq|SELECT audittrail FROM defaults|;
2334 if ($dbh->selectrow_array($query)) {
2335 my ($null, $employee_id) = $self->get_employee($dbh);
2337 if ($self->{audittrail} && !$myconfig) {
2338 chop $self->{audittrail};
2340 my @a = split /\|/, $self->{audittrail};
2344 my @flds = qw(tablename reference formname action transdate);
2346 # put into hash and remove dups
2348 $key = "$a[2]$a[3]";
2350 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2354 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2355 formname, action, employee_id, transdate)
2356 VALUES ($audittrail->{id}, ?, ?,
2357 ?, ?, $employee_id, ?)|;
2358 my $sth = $dbh->prepare($query) || $self->dberror($query);
2362 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2366 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2368 $sth->execute || $self->dberror;
2373 if ($audittrail->{transdate}) {
2374 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2375 formname, action, employee_id, transdate) VALUES (
2376 $audittrail->{id}, '$audittrail->{tablename}', |
2377 . $dbh->quote($audittrail->{reference}) . qq|,
2378 '$audittrail->{formname}', '$audittrail->{action}',
2379 $employee_id, '$audittrail->{transdate}')|;
2381 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2382 formname, action, employee_id) VALUES ($audittrail->{id},
2383 '$audittrail->{tablename}', |
2384 . $dbh->quote($audittrail->{reference}) . qq|,
2385 '$audittrail->{formname}', '$audittrail->{action}',
2392 $query = qq|SELECT current_timestamp FROM defaults|;
2393 my ($timestamp) = $dbh->selectrow_array($query);
2396 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2399 $dbh->disconnect if $disconnect;
2408 $main::lxdebug->enter_sub();
2410 my ($type, $country, $NLS_file) = @_;
2413 if ($country && -d "locale/$country") {
2415 $self->{countrycode} = $country;
2416 if (open(IN, "locale/$country/$NLS_file")) {
2417 my $code = join("", <IN>);
2423 $self->{NLS_file} = $NLS_file;
2425 push @{ $self->{LONG_MONTH} },
2426 ("January", "February", "March", "April",
2427 "May ", "June", "July", "August",
2428 "September", "October", "November", "December");
2429 push @{ $self->{SHORT_MONTH} },
2430 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2432 $main::lxdebug->leave_sub();
2438 my ($self, $text) = @_;
2440 return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
2444 $main::lxdebug->enter_sub();
2446 my ($self, $text) = @_;
2448 if (exists $self->{subs}{$text}) {
2449 $text = $self->{subs}{$text};
2451 if ($self->{countrycode} && $self->{NLS_file}) {
2453 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2457 $main::lxdebug->leave_sub();
2463 $main::lxdebug->enter_sub();
2465 my ($self, $myconfig, $date, $longformat) = @_;
2468 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2473 $spc = $myconfig->{dateformat};
2475 $spc = substr($spc, 1, 1);
2477 if ($date =~ /\D/) {
2478 if ($myconfig->{dateformat} =~ /^yy/) {
2479 ($yy, $mm, $dd) = split /\D/, $date;
2481 if ($myconfig->{dateformat} =~ /^mm/) {
2482 ($mm, $dd, $yy) = split /\D/, $date;
2484 if ($myconfig->{dateformat} =~ /^dd/) {
2485 ($dd, $mm, $yy) = split /\D/, $date;
2488 $date = substr($date, 2);
2489 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2494 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2495 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2497 if ($myconfig->{dateformat} =~ /^dd/) {
2498 if (defined $longformat && $longformat == 0) {
2500 $dd = "0$dd" if ($dd < 10);
2501 $mm = "0$mm" if ($mm < 10);
2502 $longdate = "$dd$spc$mm$spc$yy";
2505 $longdate .= ($spc eq '.') ? ". " : " ";
2506 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2508 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2510 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2511 # Lx-Office is mainly used in Germany or German speaking countries.
2512 if (defined $longformat && $longformat == 0) {
2514 $dd = "0$dd" if ($dd < 10);
2515 $mm = "0$mm" if ($mm < 10);
2516 $longdate = "$yy-$mm-$dd";
2518 $longdate = "$dd. ";
2519 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2522 if (defined $longformat && $longformat == 0) {
2524 $dd = "0$dd" if ($dd < 10);
2525 $mm = "0$mm" if ($mm < 10);
2526 $longdate = "$mm$spc$dd$spc$yy";
2528 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2534 $main::lxdebug->leave_sub();
2540 $main::lxdebug->enter_sub();
2542 my ($self, $myconfig, $date, $longformat) = @_;
2545 $main::lxdebug->leave_sub();
2550 $spc = $myconfig->{dateformat};
2552 $spc = substr($spc, 1, 1);
2554 if ($date =~ /\D/) {
2555 if ($myconfig->{dateformat} =~ /^yy/) {
2556 ($yy, $mm, $dd) = split /\D/, $date;
2557 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2558 ($mm, $dd, $yy) = split /\D/, $date;
2559 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2560 ($dd, $mm, $yy) = split /\D/, $date;
2563 $date = substr($date, 2);
2564 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2569 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2570 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2572 $main::lxdebug->leave_sub();
2573 return ($yy, $mm, $dd);
2577 $main::lxdebug->enter_sub();
2579 my ($self, $myconfig, $date, $output_format, $longformat) = @_;
2581 $main::lxdebug->leave_sub() and return "" unless ($date);
2583 my ($yy, $mm, $dd) = $self->parse_date($myconfig, $date);
2585 $output_format =~ /d+/;
2586 substr($output_format, $-[0], $+[0] - $-[0]) =
2587 sprintf("%0" . (length($&)) . "d", $dd);
2589 $output_format =~ /m+/;
2590 substr($output_format, $-[0], $+[0] - $-[0]) =
2591 sprintf("%0" . (length($&)) . "d", $mm);
2593 $output_format =~ /y+/;
2594 if (length($&) == 2) {
2595 $yy -= $yy >= 2000 ? 2000 : 1900;
2597 substr($output_format, $-[0], $+[0] - $-[0]) =
2598 sprintf("%0" . (length($&)) . "d", $yy);
2600 $main::lxdebug->leave_sub();
2602 return $output_format;