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.3.9";
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 <script type="text/javascript" src="js/highlight_input.js"></script>
427 <link rel="stylesheet" type="text/css" href="css/tabcontent.css" />
429 <script type="text/javascript" src="js/tabcontent.js">
431 /***********************************************
432 * Tab Content script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
433 * This notice MUST stay intact for legal use
434 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
435 ***********************************************/
445 $main::lxdebug->leave_sub();
448 sub parse_html_template {
449 $main::lxdebug->enter_sub();
451 my ($self, $file, $additional_params) = @_;
454 if (!defined($main::myconfig) || !defined($main::myconfig{"countrycode"})) {
455 $language = $main::language;
457 $language = $main::myconfig{"countrycode"};
460 if (-f "templates/webpages/${file}_${language}.html") {
461 if ((-f ".developer") &&
462 (-f "templates/webpages/${file}_master.html") &&
463 ((stat("templates/webpages/${file}_master.html"))[9] >
464 (stat("templates/webpages/${file}_${language}.html"))[9])) {
465 my $info = "Developper information: templates/webpages/${file}_master.html is newer than the localized version.\n" .
466 "Please re-run 'locales.pl' in 'locale/${language}'.";
467 print(qq|<pre>$info</pre>|);
471 $file = "templates/webpages/${file}_${language}.html";
472 } elsif (-f "templates/webpages/${file}.html") {
473 $file = "templates/webpages/${file}.html";
475 my $info = "Web page template '${file}' not found.\n" .
476 "Please re-run 'locales.pl' in 'locale/${language}'.";
477 print(qq|<pre>$info</pre>|);
481 my $template = HTML::Template->new("filename" => $file,
482 "die_on_bad_params" => 0,
484 "case_sensitive" => 1,
485 "loop_context_vars" => 1,
488 $additional_params = {} unless ($additional_params);
489 if ($self->{"DEBUG"}) {
490 $additional_params->{"DEBUG"} = $self->{"DEBUG"};
493 if ($additional_params->{"DEBUG"}) {
494 $additional_params->{"DEBUG"} =
495 "<br><em>DEBUG INFORMATION:</em><pre>" . $additional_params->{"DEBUG"} . "</pre>";
498 if (%main::myconfig) {
499 map({ $additional_params->{"myconfig_${_}"} = $main::myconfig{$_}; } keys(%main::myconfig));
500 my $jsc_dateformat = $main::myconfig{"dateformat"};
501 $jsc_dateformat =~ s/d+/\%d/gi;
502 $jsc_dateformat =~ s/m+/\%m/gi;
503 $jsc_dateformat =~ s/y+/\%Y/gi;
504 $additional_params->{"myconfig_jsc_dateformat"} = $jsc_dateformat;
507 $additional_params->{"conf_jscalendar"} = $main::jscalendar;
508 $additional_params->{"conf_lizenzen"} = $main::lizenzen;
509 $additional_params->{"conf_latex_templates"} = $main::latex;
510 $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
512 my @additional_param_names = keys(%{$additional_params});
513 foreach my $key ($template->param()) {
514 my $param = $self->{$key};
515 $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
516 $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
517 $template->param($key => $param);
520 my $output = $template->output();
522 $main::lxdebug->leave_sub();
527 sub show_generic_error {
528 my ($self, $error, $title, $action) = @_;
531 $add_params->{"title"} = $title if ($title);
532 $self->{"label_error"} = $error;
536 map({ delete($self->{$_}); } qw(action));
537 map({ push(@vars, { "name" => $_, "value" => $self->{$_} })
538 if (!ref($self->{$_})); }
540 $add_params->{"SHOW_BUTTON"} = 1;
541 $add_params->{"BUTTON_LABEL"} = $action;
543 $add_params->{"VARIABLES"} = \@vars;
546 print($self->parse_html_template("generic/error", $add_params));
548 die("Error: $error\n");
551 sub show_generic_information {
552 my ($self, $error, $title) = @_;
555 $add_params->{"title"} = $title if ($title);
556 $self->{"label_information"} = $error;
559 print($self->parse_html_template("generic/information", $add_params));
561 die("Information: $error\n");
564 # write Trigger JavaScript-Code ($qty = quantity of Triggers)
565 # changed it to accept an arbitrary number of triggers - sschoeling
567 $main::lxdebug->enter_sub();
570 my $myconfig = shift;
573 # set dateform for jsscript
575 $ifFormat = "%d.%m.%Y";
576 if ($myconfig->{dateformat} eq "dd.mm.yy") {
577 $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 "mm/dd/yy") {
586 $ifFormat = "%m/%d/%Y";
588 if ($myconfig->{dateformat} eq "mm-dd-yy") {
589 $ifFormat = "%m-%d-%Y";
591 if ($myconfig->{dateformat} eq "yyyy-mm-dd") {
592 $ifFormat = "%Y-%m-%d";
604 inputField : "| . (shift) . qq|",
605 ifFormat :"$ifFormat",
606 align : "| . (shift) . qq|",
607 button : "| . (shift) . qq|"
613 <script type="text/javascript">
614 <!--| . join("", @triggers) . qq|//-->
618 $main::lxdebug->leave_sub();
621 } #end sub write_trigger
624 $main::lxdebug->enter_sub();
626 my ($self, $msg) = @_;
628 if ($self->{callback}) {
630 ($script, $argv) = split(/\?/, $self->{callback});
631 exec("perl", "$script", $argv);
639 $main::lxdebug->leave_sub();
642 # sort of columns removed - empty sub
644 $main::lxdebug->enter_sub();
646 my ($self, @columns) = @_;
648 $main::lxdebug->leave_sub();
654 $main::lxdebug->enter_sub(2);
656 my ($self, $myconfig, $amount, $places, $dash) = @_;
661 my $neg = ($amount =~ s/-//);
663 $amount = $self->round_amount($amount, $places) if ($places =~ /\d/);
665 my @d = map { s/\d//g; reverse split // } my $tmp = $myconfig->{numberformat}; # get delim chars
666 my @p = split /\./, $amount ; # split amount at decimal point
668 $p[0] =~ s/\B(?=(...)*$)/$d[1]/g if $d[1]; # add 1,000 delimiters
671 $amount .= $d[0].$p[1].(0 x ($places - length $p[1])) if ($places || $p[1] ne '');
674 ($dash =~ /-/) ? ($neg ? "($amount)" : "$amount" ) :
675 ($dash =~ /DRCR/) ? ($neg ? "$amount DR" : "$amount CR" ) :
676 ($neg ? "-$amount" : "$amount" ) ;
680 $main::lxdebug->leave_sub(2);
685 $main::lxdebug->enter_sub(2);
687 my ($self, $myconfig, $amount) = @_;
689 if ($myconfig->{in_numberformat} == 1) {
690 # Extra input number format 1000.00 or 1000,00
692 $amount = scalar reverse $amount;
693 $amount =~ s/\./DOT/;
695 $amount =~ s/DOT/\./;
696 $amount = scalar reverse $amount;
697 $main::lxdebug->leave_sub(2);
698 return ($amount * 1);
701 if ( ($myconfig->{numberformat} eq '1.000,00')
702 || ($myconfig->{numberformat} eq '1000,00')) {
707 if ($myconfig->{numberformat} eq "1'000.00") {
713 $main::lxdebug->leave_sub(2);
715 return ($amount * 1);
719 $main::lxdebug->enter_sub(2);
721 my ($self, $amount, $places) = @_;
724 # Rounding like "Kaufmannsrunden"
725 # Descr. http://de.wikipedia.org/wiki/Rundung
727 # http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.13.html
730 $amount = $amount * (10**($places));
731 $round_amount = int($amount + .5 * ($amount <=> 0)) / (10**($places));
733 $main::lxdebug->leave_sub(2);
735 return $round_amount;
740 $main::lxdebug->enter_sub();
742 my ($self, $myconfig, $userspath) = @_;
745 $self->{"cwd"} = getcwd();
746 $self->{"tmpdir"} = $self->{cwd} . "/${userspath}";
748 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
749 $template = OpenDocumentTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
750 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
751 $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
752 $template = LaTeXTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
753 } elsif (($self->{"format"} =~ /html/i) ||
754 (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
755 $template = HTMLTemplate->new($self->{"IN"}, $self, $myconfig, $userspath);
758 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
759 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" };
761 map({ $self->{"employee_${_}"} = $myconfig->{$_}; }
762 qw(email tel fax name signature company address businessnumber));
764 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
766 # OUT is used for the media, screen, printer, email
767 # for postscript we store a copy in a temporary file
769 $self->{tmpfile} = "$userspath/${fileid}.$self->{IN}";
770 if ($template->uses_temp_file() || $self->{media} eq 'email') {
772 $self->{OUT} = ">$self->{tmpfile}";
776 open(OUT, "$self->{OUT}") or $self->error("$self->{OUT} : $!");
778 open(OUT, ">-") or $self->error("STDOUT : $!");
782 if (!$template->parse(*OUT)) {
784 $self->error("$self->{IN} : " . $template->get_error());
790 #print(STDERR Dumper($self));
792 if ($template->uses_temp_file() || $self->{media} eq 'email') {
794 if ($self->{media} eq 'email') {
798 my $mail = new Mailer;
800 map { $mail->{$_} = $self->{$_} }
801 qw(cc bcc subject message version format charset);
802 $mail->{to} = qq|$self->{email}|;
803 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
804 $mail->{fileid} = "$fileid.";
805 $myconfig->{signature} =~ s/\\r\\n/\\n/g;
807 # if we send html or plain text inline
808 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
809 $mail->{contenttype} = "text/html";
811 $mail->{message} =~ s/\r\n/<br>\n/g;
812 $myconfig->{signature} =~ s/\\n/<br>\n/g;
813 $mail->{message} .= "<br>\n-- <br>\n$myconfig->{signature}\n<br>";
815 open(IN, $self->{tmpfile})
816 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
818 $mail->{message} .= $_;
825 @{ $mail->{attachments} } = ($self->{tmpfile}) unless ($form->{do_not_attach});
827 $mail->{message} =~ s/\r\n/\n/g;
828 $myconfig->{signature} =~ s/\\n/\n/g;
829 $mail->{message} .= "\n-- \n$myconfig->{signature}";
833 my $err = $mail->send($out);
834 $self->error($self->cleanup . "$err") if ($err);
840 my $numbytes = (-s $self->{tmpfile});
841 open(IN, $self->{tmpfile})
842 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
844 $self->{copies} = 1 unless $self->{media} eq 'printer';
846 chdir("$self->{cwd}");
847 #print(STDERR "Kopien $self->{copies}\n");
848 #print(STDERR "OUT $self->{OUT}\n");
849 for my $i (1 .. $self->{copies}) {
851 open(OUT, $self->{OUT})
852 or $self->error($self->cleanup . "$self->{OUT} : $!");
856 print qq|Content-Type: | . $template->get_mime_type() . qq|
857 Content-Disposition: attachment; filename="$self->{tmpfile}"
858 Content-Length: $numbytes
862 open(OUT, ">-") or $self->error($self->cleanup . "$!: STDOUT");
882 chdir("$self->{cwd}");
883 $main::lxdebug->leave_sub();
887 $main::lxdebug->enter_sub();
891 chdir("$self->{tmpdir}");
894 if (-f "$self->{tmpfile}.err") {
895 open(FH, "$self->{tmpfile}.err");
900 if ($self->{tmpfile}) {
901 $self->{tmpfile} =~ s|.*/||g;
903 $self->{tmpfile} =~ s/\.\w+$//g;
904 my $tmpfile = $self->{tmpfile};
905 unlink(<$tmpfile.*>);
908 chdir("$self->{cwd}");
910 $main::lxdebug->leave_sub();
916 $main::lxdebug->enter_sub();
918 my ($self, $date, $myconfig) = @_;
920 if ($date && $date =~ /\D/) {
922 if ($myconfig->{dateformat} =~ /^yy/) {
923 ($yy, $mm, $dd) = split /\D/, $date;
925 if ($myconfig->{dateformat} =~ /^mm/) {
926 ($mm, $dd, $yy) = split /\D/, $date;
928 if ($myconfig->{dateformat} =~ /^dd/) {
929 ($dd, $mm, $yy) = split /\D/, $date;
934 $yy = ($yy < 70) ? $yy + 2000 : $yy;
935 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
937 $dd = "0$dd" if ($dd < 10);
938 $mm = "0$mm" if ($mm < 10);
943 $main::lxdebug->leave_sub();
948 # Database routines used throughout
951 $main::lxdebug->enter_sub();
953 my ($self, $myconfig) = @_;
955 # connect to database
957 DBI->connect($myconfig->{dbconnect},
958 $myconfig->{dbuser}, $myconfig->{dbpasswd})
962 if ($myconfig->{dboptions}) {
963 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
966 $main::lxdebug->leave_sub();
971 sub dbconnect_noauto {
972 $main::lxdebug->enter_sub();
974 my ($self, $myconfig) = @_;
976 # connect to database
978 DBI->connect($myconfig->{dbconnect}, $myconfig->{dbuser},
979 $myconfig->{dbpasswd}, { AutoCommit => 0 })
983 if ($myconfig->{dboptions}) {
984 $dbh->do($myconfig->{dboptions}) || $self->dberror($myconfig->{dboptions});
987 $main::lxdebug->leave_sub();
993 $main::lxdebug->enter_sub();
995 my ($self, $dbh, $table, $field, $where, $value) = @_;
997 # if we have a value, go do it
1000 # retrieve balance from table
1001 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1002 my $sth = $dbh->prepare($query);
1004 $sth->execute || $self->dberror($query);
1005 my ($balance) = $sth->fetchrow_array;
1011 $query = "UPDATE $table SET $field = $balance WHERE $where";
1012 $dbh->do($query) || $self->dberror($query);
1014 $main::lxdebug->leave_sub();
1017 sub update_exchangerate {
1018 $main::lxdebug->enter_sub();
1020 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1022 # some sanity check for currency
1024 $main::lxdebug->leave_sub();
1028 my $query = qq|SELECT e.curr FROM exchangerate e
1029 WHERE e.curr = '$curr'
1030 AND e.transdate = '$transdate'
1032 my $sth = $dbh->prepare($query);
1033 $sth->execute || $self->dberror($query);
1036 if ($buy != 0 && $sell != 0) {
1037 $set = "buy = $buy, sell = $sell";
1038 } elsif ($buy != 0) {
1039 $set = "buy = $buy";
1040 } elsif ($sell != 0) {
1041 $set = "sell = $sell";
1044 if ($sth->fetchrow_array) {
1045 $query = qq|UPDATE exchangerate
1047 WHERE curr = '$curr'
1048 AND transdate = '$transdate'|;
1050 $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate)
1051 VALUES ('$curr', $buy, $sell, '$transdate')|;
1054 $dbh->do($query) || $self->dberror($query);
1056 $main::lxdebug->leave_sub();
1059 sub save_exchangerate {
1060 $main::lxdebug->enter_sub();
1062 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1064 my $dbh = $self->dbconnect($myconfig);
1066 my ($buy, $sell) = (0, 0);
1067 $buy = $rate if $fld eq 'buy';
1068 $sell = $rate if $fld eq 'sell';
1070 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1074 $main::lxdebug->leave_sub();
1077 sub get_exchangerate {
1078 $main::lxdebug->enter_sub();
1080 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1082 unless ($transdate) {
1083 $main::lxdebug->leave_sub();
1087 my $query = qq|SELECT e.$fld FROM exchangerate e
1088 WHERE e.curr = '$curr'
1089 AND e.transdate = '$transdate'|;
1090 my $sth = $dbh->prepare($query);
1091 $sth->execute || $self->dberror($query);
1093 my ($exchangerate) = $sth->fetchrow_array;
1096 if ($exchangerate == 0) {
1100 $main::lxdebug->leave_sub();
1102 return $exchangerate;
1105 sub set_payment_options {
1106 $main::lxdebug->enter_sub();
1108 my ($self, $myconfig, $transdate) = @_;
1110 if ($self->{payment_id}) {
1112 my $dbh = $self->dbconnect($myconfig);
1115 my $query = qq|SELECT p.terms_netto, p.terms_skonto, p.percent_skonto, p.description_long FROM payment_terms p
1116 WHERE p.id = $self->{payment_id}|;
1117 my $sth = $dbh->prepare($query);
1118 $sth->execute || $self->dberror($query);
1120 ($self->{terms_netto}, $self->{terms_skonto}, $self->{percent_skonto}, $self->{payment_terms}) = $sth->fetchrow_array;
1123 my $query = qq|SELECT date '$transdate' + $self->{terms_netto} AS netto_date,date '$transdate' + $self->{terms_skonto} AS skonto_date FROM payment_terms
1125 my $sth = $dbh->prepare($query);
1126 $sth->execute || $self->dberror($query);
1127 ($self->{netto_date}, $self->{skonto_date}) = $sth->fetchrow_array;
1130 $self->{skonto_amount} = $self->format_amount($myconfig, ($self->parse_amount($myconfig, $self->{subtotal}) * $self->{percent_skonto}), 2);
1132 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1133 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1134 $self->{payment_terms} =~ s/<%skonto_amount%>/$self->{skonto_amount}/g;
1139 $main::lxdebug->leave_sub();
1143 sub check_exchangerate {
1144 $main::lxdebug->enter_sub();
1146 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1148 unless ($transdate) {
1149 $main::lxdebug->leave_sub();
1153 my $dbh = $self->dbconnect($myconfig);
1155 my $query = qq|SELECT e.$fld FROM exchangerate e
1156 WHERE e.curr = '$currency'
1157 AND e.transdate = '$transdate'|;
1158 my $sth = $dbh->prepare($query);
1159 $sth->execute || $self->dberror($query);
1161 my ($exchangerate) = $sth->fetchrow_array;
1165 $main::lxdebug->leave_sub();
1167 return $exchangerate;
1170 sub get_template_language {
1171 $main::lxdebug->enter_sub();
1173 my ($self, $myconfig) = @_;
1175 my $template_code = "";
1177 if ($self->{language_id}) {
1179 my $dbh = $self->dbconnect($myconfig);
1182 my $query = qq|SELECT l.template_code FROM language l
1183 WHERE l.id = $self->{language_id}|;
1184 my $sth = $dbh->prepare($query);
1185 $sth->execute || $self->dberror($query);
1187 ($template_code) = $sth->fetchrow_array;
1192 $main::lxdebug->leave_sub();
1194 return $template_code;
1197 sub get_printer_code {
1198 $main::lxdebug->enter_sub();
1200 my ($self, $myconfig) = @_;
1202 my $template_code = "";
1204 if ($self->{printer_id}) {
1206 my $dbh = $self->dbconnect($myconfig);
1209 my $query = qq|SELECT p.template_code,p.printer_command FROM printers p
1210 WHERE p.id = $self->{printer_id}|;
1211 my $sth = $dbh->prepare($query);
1212 $sth->execute || $self->dberror($query);
1214 ($template_code, $self->{printer_command}) = $sth->fetchrow_array;
1219 $main::lxdebug->leave_sub();
1221 return $template_code;
1225 $main::lxdebug->enter_sub();
1227 my ($self, $myconfig) = @_;
1229 my $template_code = "";
1231 if ($self->{shipto_id}) {
1233 my $dbh = $self->dbconnect($myconfig);
1236 my $query = qq|SELECT s.* FROM shipto s
1237 WHERE s.shipto_id = $self->{shipto_id}|;
1238 my $sth = $dbh->prepare($query);
1239 $sth->execute || $self->dberror($query);
1240 $ref = $sth->fetchrow_hashref(NAME_lc);
1241 map { $form->{$_} = $ref->{$_} } keys %$ref;
1246 $main::lxdebug->leave_sub();
1251 $main::lxdebug->enter_sub();
1253 my ($self, $dbh, $id, $module) = @_;
1257 qw(name department_1 department_2 street zipcode city country contact phone fax email)
1259 if ($self->{"shipto$item"}) {
1260 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1262 $self->{"shipto$item"} =~ s/\'/\'\'/g;
1265 if ($self->{shipto_id}) {
1266 my $query = qq| UPDATE shipto set
1267 shiptoname = '$self->{shiptoname}',
1268 shiptodepartment_1 = '$self->{shiptodepartment_1}',
1269 shiptodepartment_2 = '$self->{shiptodepartment_2}',
1270 shiptostreet = '$self->{shiptostreet}',
1271 shiptozipcode = '$self->{shiptozipcode}',
1272 shiptocity = '$self->{shiptocity}',
1273 shiptocountry = '$self->{shiptocountry}',
1274 shiptocontact = '$self->{shiptocontact}',
1275 shiptophone = '$self->{shiptophone}',
1276 shiptofax = '$self->{shiptofax}',
1277 shiptoemail = '$self->{shiptoemail}'
1278 WHERE shipto_id = $self->{shipto_id}|;
1279 $dbh->do($query) || $self->dberror($query);
1282 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2, shiptostreet,
1283 shiptozipcode, shiptocity, shiptocountry, shiptocontact,
1284 shiptophone, shiptofax, shiptoemail, module) VALUES ($id,
1285 '$self->{shiptoname}', '$self->{shiptodepartment_1}', '$self->{shiptodepartment_2}', '$self->{shiptostreet}',
1286 '$self->{shiptozipcode}', '$self->{shiptocity}',
1287 '$self->{shiptocountry}', '$self->{shiptocontact}',
1288 '$self->{shiptophone}', '$self->{shiptofax}',
1289 '$self->{shiptoemail}', '$module')|;
1290 $dbh->do($query) || $self->dberror($query);
1294 $main::lxdebug->leave_sub();
1298 $main::lxdebug->enter_sub();
1300 my ($self, $dbh) = @_;
1302 my $query = qq|SELECT e.id, e.name FROM employee e
1303 WHERE e.login = '$self->{login}'|;
1304 my $sth = $dbh->prepare($query);
1305 $sth->execute || $self->dberror($query);
1307 ($self->{employee_id}, $self->{employee}) = $sth->fetchrow_array;
1308 $self->{employee_id} *= 1;
1312 $main::lxdebug->leave_sub();
1315 # get other contact for transaction and form - html/tex
1317 $main::lxdebug->enter_sub();
1319 my ($self, $dbh, $id) = @_;
1321 my $query = qq|SELECT c.*
1324 $sth = $dbh->prepare($query);
1325 $sth->execute || $self->dberror($query);
1327 $ref = $sth->fetchrow_hashref(NAME_lc);
1329 push @{ $self->{$_} }, $ref;
1332 $main::lxdebug->leave_sub();
1335 # get contacts for id, if no contact return {"","","","",""}
1337 $main::lxdebug->enter_sub();
1339 my ($self, $dbh, $id) = @_;
1341 my $query = qq|SELECT c.cp_id, c.cp_cv_id, c.cp_name, c.cp_givenname, c.cp_abteilung
1343 WHERE cp_cv_id=$id|;
1344 my $sth = $dbh->prepare($query);
1345 $sth->execute || $self->dberror($query);
1348 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1349 push @{ $self->{all_contacts} }, $ref;
1354 push @{ $self->{all_contacts} }, { { "", "", "", "", "", "" } };
1357 $main::lxdebug->leave_sub();
1360 # this sub gets the id and name from $table
1362 $main::lxdebug->enter_sub();
1364 my ($self, $myconfig, $table) = @_;
1366 # connect to database
1367 my $dbh = $self->dbconnect($myconfig);
1369 my $name = $self->like(lc $self->{$table});
1370 my $customernumber = $self->like(lc $self->{customernumber});
1372 if ($self->{customernumber} ne "") {
1373 $query = qq~SELECT c.id, c.name,
1374 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1376 WHERE (lower(c.customernumber) LIKE '$customernumber') AND (not c.obsolete)
1379 $query = qq~SELECT c.id, c.name,
1380 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1382 WHERE (lower(c.name) LIKE '$name') AND (not c.obsolete)
1386 if ($self->{openinvoices}) {
1387 $query = qq~SELECT DISTINCT c.id, c.name,
1388 c.street || ' ' || c.zipcode || ' ' || c.city || ' ' || c.country AS address
1389 FROM $self->{arap} a
1390 JOIN $table c ON (a.${table}_id = c.id)
1391 WHERE NOT a.amount = a.paid
1392 AND lower(c.name) LIKE '$name'
1395 my $sth = $dbh->prepare($query);
1397 $sth->execute || $self->dberror($query);
1400 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1401 push(@{ $self->{name_list} }, $ref);
1407 $main::lxdebug->leave_sub();
1412 # the selection sub is used in the AR, AP, IS, IR and OE module
1415 $main::lxdebug->enter_sub();
1417 my ($self, $myconfig, $table, $module) = @_;
1420 my $dbh = $self->dbconnect($myconfig);
1422 my $query = qq|SELECT count(*) FROM $table|;
1423 my $sth = $dbh->prepare($query);
1424 $sth->execute || $self->dberror($query);
1425 my ($count) = $sth->fetchrow_array;
1428 # build selection list
1429 if ($count < $myconfig->{vclimit}) {
1430 $query = qq|SELECT id, name
1431 FROM $table WHERE not obsolete
1433 $sth = $dbh->prepare($query);
1434 $sth->execute || $self->dberror($query);
1436 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1437 push @{ $self->{"all_$table"} }, $ref;
1445 $self->get_employee($dbh);
1447 # setup sales contacts
1448 $query = qq|SELECT e.id, e.name
1451 AND NOT e.id = $self->{employee_id}|;
1452 $sth = $dbh->prepare($query);
1453 $sth->execute || $self->dberror($query);
1455 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1456 push @{ $self->{all_employees} }, $ref;
1461 push @{ $self->{all_employees} },
1462 { id => $self->{employee_id},
1463 name => $self->{employee} };
1465 # sort the whole thing
1466 @{ $self->{all_employees} } =
1467 sort { $a->{name} cmp $b->{name} } @{ $self->{all_employees} };
1469 if ($module eq 'AR') {
1471 # prepare query for departments
1472 $query = qq|SELECT d.id, d.description
1478 $query = qq|SELECT d.id, d.description
1483 $sth = $dbh->prepare($query);
1484 $sth->execute || $self->dberror($query);
1486 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1487 push @{ $self->{all_departments} }, $ref;
1492 $query = qq|SELECT id, description
1495 $sth = $dbh->prepare($query);
1496 $sth->execute || $form->dberror($query);
1498 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1499 push @{ $self->{languages} }, $ref;
1504 $query = qq|SELECT printer_description, id
1507 $sth = $dbh->prepare($query);
1508 $sth->execute || $form->dberror($query);
1510 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1511 push @{ $self->{printers} }, $ref;
1517 $query = qq|SELECT id, description
1520 $sth = $dbh->prepare($query);
1521 $sth->execute || $form->dberror($query);
1523 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1524 push @{ $self->{payment_terms} }, $ref;
1528 $main::lxdebug->leave_sub();
1532 sub language_payment {
1533 $main::lxdebug->enter_sub();
1535 my ($self, $myconfig) = @_;
1536 undef $self->{languages};
1537 undef $self->{payment_terms};
1538 undef $self->{printers};
1541 my $dbh = $self->dbconnect($myconfig);
1543 my $query = qq|SELECT id, description
1546 my $sth = $dbh->prepare($query);
1547 $sth->execute || $form->dberror($query);
1549 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1550 push @{ $self->{languages} }, $ref;
1555 $query = qq|SELECT printer_description, id
1558 $sth = $dbh->prepare($query);
1559 $sth->execute || $form->dberror($query);
1561 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1562 push @{ $self->{printers} }, $ref;
1567 $query = qq|SELECT id, description
1570 $sth = $dbh->prepare($query);
1571 $sth->execute || $form->dberror($query);
1573 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1574 push @{ $self->{payment_terms} }, $ref;
1578 # get buchungsgruppen
1579 $query = qq|SELECT id, description
1580 FROM buchungsgruppen|;
1581 $sth = $dbh->prepare($query);
1582 $sth->execute || $form->dberror($query);
1584 $self->{BUCHUNGSGRUPPEN} = [];
1585 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1586 push @{ $self->{BUCHUNGSGRUPPEN} }, $ref;
1591 $main::lxdebug->leave_sub();
1594 # this is only used for reports
1595 sub all_departments {
1596 $main::lxdebug->enter_sub();
1598 my ($self, $myconfig, $table) = @_;
1600 my $dbh = $self->dbconnect($myconfig);
1601 my $where = "1 = 1";
1603 if (defined $table) {
1604 if ($table eq 'customer') {
1605 $where = " d.role = 'P'";
1609 my $query = qq|SELECT d.id, d.description
1613 my $sth = $dbh->prepare($query);
1614 $sth->execute || $self->dberror($query);
1616 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1617 push @{ $self->{all_departments} }, $ref;
1623 $main::lxdebug->leave_sub();
1627 $main::lxdebug->enter_sub();
1629 my ($self, $module, $myconfig, $table) = @_;
1631 $self->all_vc($myconfig, $table, $module);
1633 # get last customers or vendors
1636 my $dbh = $self->dbconnect($myconfig);
1641 my $transdate = "current_date";
1642 if ($self->{transdate}) {
1643 $transdate = qq|'$self->{transdate}'|;
1646 # now get the account numbers
1647 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1648 FROM chart c, taxkeys tk
1649 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)
1652 $sth = $dbh->prepare($query);
1653 $sth->execute || $self->dberror($query);
1655 $self->{accounts} = "";
1656 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1658 foreach my $key (split /:/, $ref->{link}) {
1659 if ($key =~ /$module/) {
1661 # cross reference for keys
1662 $xkeyref{ $ref->{accno} } = $key;
1664 push @{ $self->{"${module}_links"}{$key} },
1665 { accno => $ref->{accno},
1666 description => $ref->{description},
1667 taxkey => $ref->{taxkey_id},
1668 tax_id => $ref->{tax_id} };
1670 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1676 # get taxkeys and description
1677 $query = qq|SELECT id, taxkey, taxdescription
1679 $sth = $dbh->prepare($query);
1680 $sth->execute || $self->dberror($query);
1682 $ref = $sth->fetchrow_hashref(NAME_lc);
1684 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1685 push @{ $self->{TAXKEY} }, $ref;
1692 $query = qq|SELECT id, description
1694 $sth = $dbh->prepare($query);
1695 $sth->execute || $form->dberror($query);
1698 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1699 push @{ $self->{TAXZONE} }, $ref;
1703 if (($module eq "AP") || ($module eq "AR")) {
1705 # get tax rates and description
1706 $query = qq| SELECT * FROM tax t|;
1707 $sth = $dbh->prepare($query);
1708 $sth->execute || $self->dberror($query);
1710 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1711 push @{ $self->{TAX} }, $ref;
1717 my $arap = ($table eq 'customer') ? 'ar' : 'ap';
1719 $query = qq|SELECT a.cp_id, a.invnumber, a.transdate,
1720 a.${table}_id, a.datepaid, a.duedate, a.ordnumber,
1721 a.taxincluded, a.curr AS currency, a.notes, a.intnotes,
1722 c.name AS $table, a.department_id, d.description AS department,
1723 a.amount AS oldinvtotal, a.paid AS oldtotalpaid,
1724 a.employee_id, e.name AS employee, a.gldate, a.type
1726 JOIN $table c ON (a.${table}_id = c.id)
1727 LEFT JOIN employee e ON (e.id = a.employee_id)
1728 LEFT JOIN department d ON (d.id = a.department_id)
1729 WHERE a.id = $self->{id}|;
1730 $sth = $dbh->prepare($query);
1731 $sth->execute || $self->dberror($query);
1733 $ref = $sth->fetchrow_hashref(NAME_lc);
1734 foreach $key (keys %$ref) {
1735 $self->{$key} = $ref->{$key};
1740 my $transdate = "current_date";
1741 if ($self->{transdate}) {
1742 $transdate = qq|'$self->{transdate}'|;
1745 # now get the account numbers
1746 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, tk.tax_id
1747 FROM chart c, taxkeys tk
1748 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%'))
1751 $sth = $dbh->prepare($query);
1752 $sth->execute || $self->dberror($query);
1754 $self->{accounts} = "";
1755 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1757 foreach my $key (split /:/, $ref->{link}) {
1758 if ($key =~ /$module/) {
1760 # cross reference for keys
1761 $xkeyref{ $ref->{accno} } = $key;
1763 push @{ $self->{"${module}_links"}{$key} },
1764 { accno => $ref->{accno},
1765 description => $ref->{description},
1766 taxkey => $ref->{taxkey_id},
1767 tax_id => $ref->{tax_id} };
1769 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
1775 # get amounts from individual entries
1776 $query = qq|SELECT c.accno, c.description, a.source, a.amount, a.memo,
1777 a.transdate, a.cleared, a.project_id, p.projectnumber, a.taxkey, t.rate, t.id
1779 JOIN chart c ON (c.id = a.chart_id)
1780 LEFT JOIN project p ON (p.id = a.project_id)
1781 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))
1782 WHERE a.trans_id = $self->{id}
1783 AND a.fx_transaction = '0'
1784 ORDER BY a.oid,a.transdate|;
1785 $sth = $dbh->prepare($query);
1786 $sth->execute || $self->dberror($query);
1788 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1790 # get exchangerate for currency
1791 $self->{exchangerate} =
1792 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1796 # store amounts in {acc_trans}{$key} for multiple accounts
1797 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1798 $ref->{exchangerate} =
1799 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate},
1801 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
1804 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
1805 $ref->{amount} *= -1;
1807 $ref->{index} = $index;
1809 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
1813 $query = qq|SELECT d.curr AS currencies, d.closedto, d.revtrans,
1814 (SELECT c.accno FROM chart c
1815 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1816 (SELECT c.accno FROM chart c
1817 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1819 $sth = $dbh->prepare($query);
1820 $sth->execute || $self->dberror($query);
1822 $ref = $sth->fetchrow_hashref(NAME_lc);
1823 map { $self->{$_} = $ref->{$_} } keys %$ref;
1829 $query = qq|SELECT current_date AS transdate,
1830 d.curr AS currencies, d.closedto, d.revtrans,
1831 (SELECT c.accno FROM chart c
1832 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
1833 (SELECT c.accno FROM chart c
1834 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno
1836 $sth = $dbh->prepare($query);
1837 $sth->execute || $self->dberror($query);
1839 $ref = $sth->fetchrow_hashref(NAME_lc);
1840 map { $self->{$_} = $ref->{$_} } keys %$ref;
1843 if ($self->{"$self->{vc}_id"}) {
1845 # only setup currency
1846 ($self->{currency}) = split /:/, $self->{currencies};
1850 $self->lastname_used($dbh, $myconfig, $table, $module);
1852 my $fld = ($table eq 'customer') ? 'buy' : 'sell';
1854 # get exchangerate for currency
1855 $self->{exchangerate} =
1856 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate},
1867 $main::lxdebug->leave_sub();
1871 $main::lxdebug->enter_sub();
1873 my ($self, $dbh, $myconfig, $table, $module) = @_;
1875 my $arap = ($table eq 'customer') ? "ar" : "ap";
1876 my $where = "1 = 1";
1878 if ($self->{type} =~ /_order/) {
1880 $where = "quotation = '0'";
1882 if ($self->{type} =~ /_quotation/) {
1884 $where = "quotation = '1'";
1887 my $query = qq|SELECT MAX(id) FROM $arap
1889 AND ${table}_id > 0|;
1890 my $sth = $dbh->prepare($query);
1891 $sth->execute || $self->dberror($query);
1893 my ($trans_id) = $sth->fetchrow_array;
1897 $query = qq|SELECT ct.name, a.curr, a.${table}_id,
1898 current_date + ct.terms AS duedate, a.department_id,
1899 d.description AS department
1901 JOIN $table ct ON (a.${table}_id = ct.id)
1902 LEFT JOIN department d ON (a.department_id = d.id)
1903 WHERE a.id = $trans_id|;
1904 $sth = $dbh->prepare($query);
1905 $sth->execute || $self->dberror($query);
1907 ($self->{$table}, $self->{currency}, $self->{"${table}_id"},
1908 $self->{duedate}, $self->{department_id}, $self->{department})
1909 = $sth->fetchrow_array;
1912 $main::lxdebug->leave_sub();
1916 $main::lxdebug->enter_sub();
1918 my ($self, $myconfig, $thisdate, $days) = @_;
1920 my $dbh = $self->dbconnect($myconfig);
1925 my $dateformat = $myconfig->{dateformat};
1926 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
1928 $query = qq|SELECT to_date('$thisdate', '$dateformat') + $days AS thisdate
1930 $sth = $dbh->prepare($query);
1931 $sth->execute || $self->dberror($query);
1933 $query = qq|SELECT current_date AS thisdate
1935 $sth = $dbh->prepare($query);
1936 $sth->execute || $self->dberror($query);
1939 ($thisdate) = $sth->fetchrow_array;
1944 $main::lxdebug->leave_sub();
1950 $main::lxdebug->enter_sub();
1952 my ($self, $string) = @_;
1954 if ($string !~ /%/) {
1955 $string = "%$string%";
1958 $string =~ s/\'/\'\'/g;
1960 $main::lxdebug->leave_sub();
1966 $main::lxdebug->enter_sub();
1968 my ($self, $flds, $new, $count, $numrows) = @_;
1972 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } }
1978 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
1980 $j = $item->{ndx} - 1;
1981 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
1985 for $i ($count + 1 .. $numrows) {
1986 map { delete $self->{"${_}_$i"} } @{$flds};
1989 $main::lxdebug->leave_sub();
1993 $main::lxdebug->enter_sub();
1995 my ($self, $myconfig) = @_;
1999 my $dbh = $self->dbconnect_noauto($myconfig);
2001 my $query = qq|DELETE FROM status
2002 WHERE formname = '$self->{formname}'
2004 my $sth = $dbh->prepare($query) || $self->dberror($query);
2006 if ($self->{formname} =~ /(check|receipt)/) {
2007 for $i (1 .. $self->{rowcount}) {
2008 $sth->execute($self->{"id_$i"} * 1) || $self->dberror($query);
2012 $sth->execute($self->{id}) || $self->dberror($query);
2016 my $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2017 my $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2019 my %queued = split / /, $self->{queued};
2021 if ($self->{formname} =~ /(check|receipt)/) {
2023 # this is a check or receipt, add one entry for each lineitem
2024 my ($accno) = split /--/, $self->{account};
2025 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
2026 chart_id) VALUES (?, '$printed',
2027 '$queued{$self->{formname}}', '$self->{prinform}',
2028 (SELECT c.id FROM chart c WHERE c.accno = '$accno'))|;
2029 $sth = $dbh->prepare($query) || $self->dberror($query);
2031 for $i (1 .. $self->{rowcount}) {
2032 if ($self->{"checked_$i"}) {
2033 $sth->execute($self->{"id_$i"}) || $self->dberror($query);
2038 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2039 spoolfile, formname)
2040 VALUES ($self->{id}, '$printed', '$emailed',
2041 '$queued{$self->{formname}}', '$self->{formname}')|;
2042 $dbh->do($query) || $self->dberror($query);
2048 $main::lxdebug->leave_sub();
2052 $main::lxdebug->enter_sub();
2054 my ($self, $dbh) = @_;
2056 my ($query, $printed, $emailed);
2058 my $formnames = $self->{printed};
2059 my $emailforms = $self->{emailed};
2061 my $query = qq|DELETE FROM status
2062 WHERE formname = '$self->{formname}'
2063 AND trans_id = $self->{id}|;
2064 $dbh->do($query) || $self->dberror($query);
2066 # this only applies to the forms
2067 # checks and receipts are posted when printed or queued
2069 if ($self->{queued}) {
2070 my %queued = split / /, $self->{queued};
2072 foreach my $formname (keys %queued) {
2073 $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
2074 $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
2076 $query = qq|INSERT INTO status (trans_id, printed, emailed,
2077 spoolfile, formname)
2078 VALUES ($self->{id}, '$printed', '$emailed',
2079 '$queued{$formname}', '$formname')|;
2080 $dbh->do($query) || $self->dberror($query);
2082 $formnames =~ s/$self->{formname}//;
2083 $emailforms =~ s/$self->{formname}//;
2088 # save printed, emailed info
2089 $formnames =~ s/^ +//g;
2090 $emailforms =~ s/^ +//g;
2093 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2094 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2096 foreach my $formname (keys %status) {
2097 $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
2098 $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
2100 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2101 VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
2102 $dbh->do($query) || $self->dberror($query);
2105 $main::lxdebug->leave_sub();
2108 sub update_defaults {
2109 $main::lxdebug->enter_sub();
2111 my ($self, $myconfig, $fld) = @_;
2113 my $dbh = $self->dbconnect_noauto($myconfig);
2114 my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
2115 my $sth = $dbh->prepare($query);
2117 $sth->execute || $self->dberror($query);
2118 my ($var) = $sth->fetchrow_array;
2123 $query = qq|UPDATE defaults
2125 $dbh->do($query) || $self->dberror($query);
2130 $main::lxdebug->leave_sub();
2135 sub update_business {
2136 $main::lxdebug->enter_sub();
2138 my ($self, $myconfig, $business_id) = @_;
2140 my $dbh = $self->dbconnect_noauto($myconfig);
2142 qq|SELECT customernumberinit FROM business WHERE id=$business_id FOR UPDATE|;
2143 my $sth = $dbh->prepare($query);
2145 $sth->execute || $self->dberror($query);
2146 my ($var) = $sth->fetchrow_array;
2151 $query = qq|UPDATE business
2152 SET customernumberinit = '$var' WHERE id=$business_id|;
2153 $dbh->do($query) || $self->dberror($query);
2158 $main::lxdebug->leave_sub();
2164 $main::lxdebug->enter_sub();
2166 my ($self, $myconfig, $salesman) = @_;
2168 my $dbh = $self->dbconnect($myconfig);
2170 qq|SELECT id, name FROM customer WHERE (customernumber ilike '%$salesman%' OR name ilike '%$salesman%') AND business_id in (SELECT id from business WHERE salesman)|;
2171 my $sth = $dbh->prepare($query);
2172 $sth->execute || $self->dberror($query);
2175 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
2176 push(@{ $self->{salesman_list} }, $ref);
2180 $main::lxdebug->leave_sub();
2185 sub get_partsgroup {
2186 $main::lxdebug->enter_sub();
2188 my ($self, $myconfig, $p) = @_;
2190 my $dbh = $self->dbconnect($myconfig);
2192 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2194 JOIN parts p ON (p.partsgroup_id = pg.id)|;
2196 if ($p->{searchitems} eq 'part') {
2198 WHERE p.inventory_accno_id > 0|;
2200 if ($p->{searchitems} eq 'service') {
2202 WHERE p.inventory_accno_id IS NULL|;
2204 if ($p->{searchitems} eq 'assembly') {
2206 WHERE p.assembly = '1'|;
2208 if ($p->{searchitems} eq 'labor') {
2210 WHERE p.inventory_accno_id > 0 AND p.income_accno_id IS NULL|;
2214 ORDER BY partsgroup|;
2217 $query = qq|SELECT id, partsgroup FROM partsgroup
2218 ORDER BY partsgroup|;
2221 if ($p->{language_code}) {
2222 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
2223 t.description AS translation
2225 JOIN parts p ON (p.partsgroup_id = pg.id)
2226 LEFT JOIN translation t ON (t.trans_id = pg.id AND t.language_code = '$p->{language_code}')
2227 ORDER BY translation|;
2230 my $sth = $dbh->prepare($query);
2231 $sth->execute || $self->dberror($query);
2233 $self->{all_partsgroup} = ();
2234 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2235 push @{ $self->{all_partsgroup} }, $ref;
2239 $main::lxdebug->leave_sub();
2242 sub get_pricegroup {
2243 $main::lxdebug->enter_sub();
2245 my ($self, $myconfig, $p) = @_;
2247 my $dbh = $self->dbconnect($myconfig);
2249 my $query = qq|SELECT p.id, p.pricegroup
2253 ORDER BY pricegroup|;
2256 $query = qq|SELECT id, pricegroup FROM pricegroup
2257 ORDER BY pricegroup|;
2260 my $sth = $dbh->prepare($query);
2261 $sth->execute || $self->dberror($query);
2263 $self->{all_pricegroup} = ();
2264 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
2265 push @{ $self->{all_pricegroup} }, $ref;
2270 $main::lxdebug->leave_sub();
2274 my ($self, $dbh, $myconfig, $audittrail) = @_;
2276 # table, $reference, $formname, $action, $id, $transdate) = @_;
2283 $dbh = $self->dbconnect($myconfig);
2287 # if we have an id add audittrail, otherwise get a new timestamp
2289 if ($audittrail->{id}) {
2291 $query = qq|SELECT audittrail FROM defaults|;
2293 if ($dbh->selectrow_array($query)) {
2294 my ($null, $employee_id) = $self->get_employee($dbh);
2296 if ($self->{audittrail} && !$myconfig) {
2297 chop $self->{audittrail};
2299 my @a = split /\|/, $self->{audittrail};
2303 my @flds = qw(tablename reference formname action transdate);
2305 # put into hash and remove dups
2307 $key = "$a[2]$a[3]";
2309 $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
2313 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2314 formname, action, employee_id, transdate)
2315 VALUES ($audittrail->{id}, ?, ?,
2316 ?, ?, $employee_id, ?)|;
2317 my $sth = $dbh->prepare($query) || $self->dberror($query);
2321 $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate}
2325 for (@flds) { $sth->bind_param($i++, $newtrail{$key}{$_}) }
2327 $sth->execute || $self->dberror;
2332 if ($audittrail->{transdate}) {
2333 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2334 formname, action, employee_id, transdate) VALUES (
2335 $audittrail->{id}, '$audittrail->{tablename}', |
2336 . $dbh->quote($audittrail->{reference}) . qq|,
2337 '$audittrail->{formname}', '$audittrail->{action}',
2338 $employee_id, '$audittrail->{transdate}')|;
2340 $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
2341 formname, action, employee_id) VALUES ($audittrail->{id},
2342 '$audittrail->{tablename}', |
2343 . $dbh->quote($audittrail->{reference}) . qq|,
2344 '$audittrail->{formname}', '$audittrail->{action}',
2351 $query = qq|SELECT current_timestamp FROM defaults|;
2352 my ($timestamp) = $dbh->selectrow_array($query);
2355 "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
2358 $dbh->disconnect if $disconnect;
2367 $main::lxdebug->enter_sub();
2369 my ($type, $country, $NLS_file) = @_;
2372 if ($country && -d "locale/$country") {
2374 $self->{countrycode} = $country;
2375 if (open(IN, "locale/$country/$NLS_file")) {
2376 my $code = join("", <IN>);
2382 $self->{NLS_file} = $NLS_file;
2384 push @{ $self->{LONG_MONTH} },
2385 ("January", "February", "March", "April",
2386 "May ", "June", "July", "August",
2387 "September", "October", "November", "December");
2388 push @{ $self->{SHORT_MONTH} },
2389 (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec));
2391 $main::lxdebug->leave_sub();
2397 my ($self, $text) = @_;
2399 return (exists $self->{texts}{$text}) ? $self->{texts}{$text} : $text;
2403 $main::lxdebug->enter_sub();
2405 my ($self, $text) = @_;
2407 if (exists $self->{subs}{$text}) {
2408 $text = $self->{subs}{$text};
2410 if ($self->{countrycode} && $self->{NLS_file}) {
2412 "$text not defined in locale/$self->{countrycode}/$self->{NLS_file}");
2416 $main::lxdebug->leave_sub();
2422 $main::lxdebug->enter_sub();
2424 my ($self, $myconfig, $date, $longformat) = @_;
2427 my $longmonth = ($longformat) ? 'LONG_MONTH' : 'SHORT_MONTH';
2432 $spc = $myconfig->{dateformat};
2434 $spc = substr($spc, 1, 1);
2436 if ($date =~ /\D/) {
2437 if ($myconfig->{dateformat} =~ /^yy/) {
2438 ($yy, $mm, $dd) = split /\D/, $date;
2440 if ($myconfig->{dateformat} =~ /^mm/) {
2441 ($mm, $dd, $yy) = split /\D/, $date;
2443 if ($myconfig->{dateformat} =~ /^dd/) {
2444 ($dd, $mm, $yy) = split /\D/, $date;
2447 $date = substr($date, 2);
2448 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2453 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2454 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2456 if ($myconfig->{dateformat} =~ /^dd/) {
2457 if (defined $longformat && $longformat == 0) {
2459 $dd = "0$dd" if ($dd < 10);
2460 $mm = "0$mm" if ($mm < 10);
2461 $longdate = "$dd$spc$mm$spc$yy";
2464 $longdate .= ($spc eq '.') ? ". " : " ";
2465 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2467 } elsif ($myconfig->{dateformat} eq "yyyy-mm-dd") {
2469 # Use German syntax with the ISO date style "yyyy-mm-dd" because
2470 # Lx-Office is mainly used in Germany or German speaking countries.
2471 if (defined $longformat && $longformat == 0) {
2473 $dd = "0$dd" if ($dd < 10);
2474 $mm = "0$mm" if ($mm < 10);
2475 $longdate = "$yy-$mm-$dd";
2477 $longdate = "$dd. ";
2478 $longdate .= &text($self, $self->{$longmonth}[$mm]) . " $yy";
2481 if (defined $longformat && $longformat == 0) {
2483 $dd = "0$dd" if ($dd < 10);
2484 $mm = "0$mm" if ($mm < 10);
2485 $longdate = "$mm$spc$dd$spc$yy";
2487 $longdate = &text($self, $self->{$longmonth}[$mm]) . " $dd, $yy";
2493 $main::lxdebug->leave_sub();
2499 $main::lxdebug->enter_sub();
2501 my ($self, $myconfig, $date, $longformat) = @_;
2504 $main::lxdebug->leave_sub();
2509 $spc = $myconfig->{dateformat};
2511 $spc = substr($spc, 1, 1);
2513 if ($date =~ /\D/) {
2514 if ($myconfig->{dateformat} =~ /^yy/) {
2515 ($yy, $mm, $dd) = split /\D/, $date;
2516 } elsif ($myconfig->{dateformat} =~ /^mm/) {
2517 ($mm, $dd, $yy) = split /\D/, $date;
2518 } elsif ($myconfig->{dateformat} =~ /^dd/) {
2519 ($dd, $mm, $yy) = split /\D/, $date;
2522 $date = substr($date, 2);
2523 ($yy, $mm, $dd) = ($date =~ /(..)(..)(..)/);
2528 $yy = ($yy < 70) ? $yy + 2000 : $yy;
2529 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
2531 $main::lxdebug->leave_sub();
2532 return ($yy, $mm, $dd);