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., 51 Franklin Street, Fifth Floor, Boston,
32 #======================================================================
33 # Utilities for parsing forms
34 # and supporting routines for linking account numbers
35 # used in AR, AP and IS, IR modules
37 #======================================================================
52 use POSIX qw(strftime);
62 use SL::DB::AdditionalBillingAddress;
65 use SL::DB::PaymentTerm;
68 use SL::Helper::Flash qw();
71 use SL::Layout::Dispatcher;
73 use SL::Locale::String;
76 use SL::MoreCommon qw(uri_encode uri_decode);
78 use SL::PrefixedNumber;
87 use List::Util qw(first max min sum);
88 use List::MoreUtils qw(all any apply);
90 use SL::Helper::File qw(:all);
91 use SL::Helper::Number;
92 use SL::Helper::CreatePDF qw(merge_pdfs);
97 SL::Version->get_version;
101 $main::lxdebug->enter_sub();
108 if ($LXDebug::watch_form) {
109 require SL::Watchdog;
110 tie %{ $self }, 'SL::Watchdog';
115 $main::lxdebug->leave_sub();
120 sub _flatten_variables_rec {
121 $main::lxdebug->enter_sub(2);
130 if ('' eq ref $curr->{$key}) {
131 @result = ({ 'key' => $prefix . $key, 'value' => $curr->{$key} });
133 } elsif ('HASH' eq ref $curr->{$key}) {
134 foreach my $hash_key (sort keys %{ $curr->{$key} }) {
135 push @result, $self->_flatten_variables_rec($curr->{$key}, $prefix . $key . '.', $hash_key);
139 foreach my $idx (0 .. scalar @{ $curr->{$key} } - 1) {
140 my $first_array_entry = 1;
142 my $element = $curr->{$key}[$idx];
144 if ('HASH' eq ref $element) {
145 foreach my $hash_key (sort keys %{ $element }) {
146 push @result, $self->_flatten_variables_rec($element, $prefix . $key . ($first_array_entry ? '[+].' : '[].'), $hash_key);
147 $first_array_entry = 0;
150 push @result, { 'key' => $prefix . $key . '[]', 'value' => $element };
155 $main::lxdebug->leave_sub(2);
160 sub flatten_variables {
161 $main::lxdebug->enter_sub(2);
169 push @variables, $self->_flatten_variables_rec($self, '', $_);
172 $main::lxdebug->leave_sub(2);
177 sub flatten_standard_variables {
178 $main::lxdebug->enter_sub(2);
181 my %skip_keys = map { $_ => 1 } (qw(login password header stylesheet titlebar), @_);
185 foreach (grep { ! $skip_keys{$_} } keys %{ $self }) {
186 push @variables, $self->_flatten_variables_rec($self, '', $_);
189 $main::lxdebug->leave_sub(2);
195 my ($self, $str) = @_;
197 return uri_encode($str);
201 my ($self, $str) = @_;
203 return uri_decode($str);
207 $main::lxdebug->enter_sub();
208 my ($self, $str) = @_;
210 if ($str && !ref($str)) {
211 $str =~ s/\"/"/g;
214 $main::lxdebug->leave_sub();
220 $main::lxdebug->enter_sub();
221 my ($self, $str) = @_;
223 if ($str && !ref($str)) {
224 $str =~ s/"/\"/g;
227 $main::lxdebug->leave_sub();
233 $main::lxdebug->enter_sub();
237 map({ print($::request->{cgi}->hidden("-name" => $_, "-default" => $self->{$_}) . "\n"); } @_);
239 for (sort keys %$self) {
240 next if (($_ eq "header") || (ref($self->{$_}) ne ""));
241 print($::request->{cgi}->hidden("-name" => $_, "-default" => $self->{$_}) . "\n");
244 $main::lxdebug->leave_sub();
248 my ($self, $code) = @_;
249 local $self->{__ERROR_HANDLER} = sub { SL::X::FormError->throw(error => $_[0]) };
254 $main::lxdebug->enter_sub();
256 $main::lxdebug->show_backtrace();
258 my ($self, $msg) = @_;
260 if ($self->{__ERROR_HANDLER}) {
261 $self->{__ERROR_HANDLER}->($msg);
263 } elsif ($ENV{HTTP_USER_AGENT}) {
265 $self->show_generic_error($msg);
268 confess "Error: $msg\n";
271 $main::lxdebug->leave_sub();
275 $main::lxdebug->enter_sub();
277 my ($self, $msg) = @_;
279 if ($ENV{HTTP_USER_AGENT}) {
281 print $self->parse_html_template('generic/form_info', { message => $msg });
283 } elsif ($self->{info_function}) {
284 &{ $self->{info_function} }($msg);
289 $main::lxdebug->leave_sub();
292 # calculates the number of rows in a textarea based on the content and column number
293 # can be capped with maxrows
295 $main::lxdebug->enter_sub();
296 my ($self, $str, $cols, $maxrows, $minrows) = @_;
300 my $rows = sum map { int((length() - 2) / $cols) + 1 } split /\r/, $str;
303 $main::lxdebug->leave_sub();
305 return max(min($rows, $maxrows), $minrows);
309 my ($self, $msg) = @_;
311 SL::X::DBError->throw(
313 db_error => $DBI::errstr,
318 $main::lxdebug->enter_sub();
320 my ($self, $name, $msg) = @_;
323 foreach my $part (split m/\./, $name) {
324 if (!$curr->{$part} || ($curr->{$part} =~ /^\s*$/)) {
327 $curr = $curr->{$part};
330 $main::lxdebug->leave_sub();
333 sub _get_request_uri {
336 return URI->new($ENV{HTTP_REFERER})->canonical() if $ENV{HTTP_X_FORWARDED_FOR};
337 return URI->new if !$ENV{REQUEST_URI}; # for testing
339 my $scheme = $::request->is_https ? 'https' : 'http';
340 my $port = $ENV{SERVER_PORT};
341 $port = undef if (($scheme eq 'http' ) && ($port == 80))
342 || (($scheme eq 'https') && ($port == 443));
344 my $uri = URI->new("${scheme}://");
345 $uri->scheme($scheme);
347 $uri->host($ENV{HTTP_HOST} || $ENV{SERVER_ADDR});
348 $uri->path_query($ENV{REQUEST_URI});
354 sub _add_to_request_uri {
357 my $relative_new_path = shift;
358 my $request_uri = shift || $self->_get_request_uri;
359 my $relative_new_uri = URI->new($relative_new_path);
360 my @request_segments = $request_uri->path_segments;
362 my $new_uri = $request_uri->clone;
363 $new_uri->path_segments(@request_segments[0..scalar(@request_segments) - 2], $relative_new_uri->path_segments);
368 sub create_http_response {
369 $main::lxdebug->enter_sub();
374 my $cgi = $::request->{cgi};
377 if (defined $main::auth) {
378 my $uri = $self->_get_request_uri;
379 my @segments = $uri->path_segments;
381 $uri->path_segments(@segments);
383 my $session_cookie_value = $main::auth->get_session_id();
385 if ($session_cookie_value) {
386 $session_cookie = $cgi->cookie('-name' => $main::auth->get_session_cookie_name(),
387 '-value' => $session_cookie_value,
388 '-path' => $uri->path,
389 '-expires' => '+' . $::auth->{session_timeout} . 'm',
390 '-secure' => $::request->is_https);
394 my %cgi_params = ('-type' => $params{content_type});
395 $cgi_params{'-charset'} = $params{charset} if ($params{charset});
396 $cgi_params{'-cookie'} = $session_cookie if ($session_cookie);
398 map { $cgi_params{'-' . $_} = $params{$_} if exists $params{$_} } qw(content_disposition content_length status);
400 my $output = $cgi->header(%cgi_params);
402 $main::lxdebug->leave_sub();
408 $::lxdebug->enter_sub;
410 my ($self, %params) = @_;
413 $::lxdebug->leave_sub and return if !$ENV{HTTP_USER_AGENT} || $self->{header}++;
415 if ($params{no_layout}) {
416 $::request->{layout} = SL::Layout::Dispatcher->new(style => 'none');
419 my $layout = $::request->{layout};
421 # standard css for all
422 # this should gradually move to the layouts that need it
423 $layout->use_stylesheet("$_.css") for qw(
424 common main menu list_accounts jquery.autocomplete
425 jquery.multiselect2side
426 ui-lightness/jquery-ui
428 tooltipster themes/tooltipster-light
431 $layout->use_javascript("$_.js") for (qw(
432 jquery jquery-ui jquery.cookie jquery.checkall jquery.download
433 jquery/jquery.form jquery/fixes client_js
434 jquery/jquery.tooltipster.min
435 common part_selection
436 ), "jquery/ui/i18n/jquery.ui.datepicker-$::myconfig{countrycode}");
438 $self->{favicon} ||= "favicon.ico";
439 $self->{titlebar} = join ' - ', grep $_, $self->{title}, $self->{login}, $::myconfig{dbname}, $self->read_version if $self->{title} || !$self->{titlebar};
442 if ($self->{refresh_url} || $self->{refresh_time}) {
443 my $refresh_time = $self->{refresh_time} || 3;
444 my $refresh_url = $self->{refresh_url} || $ENV{REFERER};
445 push @header, "<meta http-equiv='refresh' content='$refresh_time;$refresh_url'>";
448 my $auto_reload_resources_param = $layout->auto_reload_resources_param;
450 push @header, map { qq|<link rel="stylesheet" href="${_}${auto_reload_resources_param}" type="text/css" title="Stylesheet">| } $layout->stylesheets;
451 push @header, "<style type='text/css'>\@page { size:landscape; }</style> " if $self->{landscape};
452 push @header, "<link rel='shortcut icon' href='$self->{favicon}' type='image/x-icon'>" if -f $self->{favicon};
453 push @header, map { qq|<script type="text/javascript" src="${_}${auto_reload_resources_param}"></script>| } $layout->javascripts;
454 push @header, '<meta name="viewport" content="width=device-width, initial-scale=1">';
455 push @header, $self->{javascript} if $self->{javascript};
456 push @header, map { $_->show_javascript } @{ $self->{AJAX} || [] };
459 strict => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">|,
460 transitional => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">|,
461 frameset => qq|<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">|,
462 html5 => qq|<!DOCTYPE html>|,
466 print $self->create_http_response(content_type => 'text/html', charset => 'UTF-8');
467 print $doctypes{$params{doctype} || 'transitional'}, $/;
471 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
472 <title>$self->{titlebar}</title>
474 print " $_\n" for @header;
476 <meta name="robots" content="noindex,nofollow">
481 print $::request->{layout}->pre_content;
482 print $::request->{layout}->start_content;
484 $layout->header_done;
486 $::lxdebug->leave_sub;
490 return unless $::request->{layout}->need_footer;
492 print $::request->{layout}->end_content;
493 print $::request->{layout}->post_content;
495 if (my @inline_scripts = $::request->{layout}->javascripts_inline) {
496 print "<script type='text/javascript'>" . join("; ", @inline_scripts) . "</script>\n";
505 sub ajax_response_header {
506 $main::lxdebug->enter_sub();
510 my $output = $::request->{cgi}->header('-charset' => 'UTF-8');
512 $main::lxdebug->leave_sub();
517 sub redirect_header {
521 my $base_uri = $self->_get_request_uri;
522 my $new_uri = URI->new_abs($new_url, $base_uri);
524 die "Headers already sent" if $self->{header};
527 return $::request->{cgi}->redirect($new_uri);
530 sub set_standard_title {
531 $::lxdebug->enter_sub;
534 $self->{titlebar} = "kivitendo " . $::locale->text('Version') . " " . $self->read_version;
535 $self->{titlebar} .= "- $::myconfig{name}" if $::myconfig{name};
536 $self->{titlebar} .= "- $::myconfig{dbname}" if $::myconfig{name};
538 $::lxdebug->leave_sub;
541 sub _prepare_html_template {
542 $main::lxdebug->enter_sub();
544 my ($self, $file, $additional_params) = @_;
547 if (!%::myconfig || !$::myconfig{"countrycode"}) {
548 $language = $::lx_office_conf{system}->{language};
550 $language = $main::myconfig{"countrycode"};
552 $language = "de" unless ($language);
554 my $webpages_path = $::request->layout->webpages_path;
556 if (-f "${webpages_path}/${file}.html") {
557 $file = "${webpages_path}/${file}.html";
559 } elsif (ref $file eq 'SCALAR') {
560 # file is a scalarref, use inline mode
562 my $info = "Web page template '${file}' not found.\n";
564 print qq|<pre>$info</pre>|;
565 $::dispatcher->end_request;
568 $additional_params->{AUTH} = $::auth;
569 $additional_params->{INSTANCE_CONF} = $::instance_conf;
570 $additional_params->{LOCALE} = $::locale;
571 $additional_params->{LXCONFIG} = \%::lx_office_conf;
572 $additional_params->{LXDEBUG} = $::lxdebug;
573 $additional_params->{MYCONFIG} = \%::myconfig;
575 $main::lxdebug->leave_sub();
580 sub parse_html_template {
581 $main::lxdebug->enter_sub();
583 my ($self, $file, $additional_params) = @_;
585 $additional_params ||= { };
587 my $real_file = $self->_prepare_html_template($file, $additional_params);
588 my $template = $self->template;
590 map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
593 $template->process($real_file, $additional_params, \$output) || die $template->error;
595 $main::lxdebug->leave_sub();
600 sub template { $::request->presenter->get_template }
602 sub show_generic_error {
603 $main::lxdebug->enter_sub();
605 my ($self, $error, %params) = @_;
607 if ($self->{__ERROR_HANDLER}) {
608 $self->{__ERROR_HANDLER}->($error);
609 $main::lxdebug->leave_sub();
613 if ($::request->is_ajax) {
616 ->render(SL::Controller::Base->new);
617 $::dispatcher->end_request;
621 'title_error' => $params{title},
622 'label_error' => $error,
625 $self->{title} = $params{title} if $params{title};
627 for my $bar ($::request->layout->get('actionbar')) {
631 call => [ 'kivi.history_back' ],
632 accesskey => 'enter',
638 print $self->parse_html_template("generic/error", $add_params);
640 print STDERR "Error: $error\n";
642 $main::lxdebug->leave_sub();
644 $::dispatcher->end_request;
647 sub show_generic_information {
648 $main::lxdebug->enter_sub();
650 my ($self, $text, $title) = @_;
653 'title_information' => $title,
654 'label_information' => $text,
657 $self->{title} = $title if ($title);
660 print $self->parse_html_template("generic/information", $add_params);
662 $main::lxdebug->leave_sub();
664 $::dispatcher->end_request;
667 sub _store_redirect_info_in_session {
670 return unless $self->{callback} =~ m:^ ( [^\?/]+ \.pl ) \? (.+) :x;
672 my ($controller, $params) = ($1, $2);
673 my $form = { map { map { $self->unescape($_) } split /=/, $_, 2 } split m/\&/, $params };
674 $self->{callback} = "${controller}?RESTORE_FORM_FROM_SESSION_ID=" . $::auth->save_form_in_session(form => $form);
678 $main::lxdebug->enter_sub();
680 my ($self, $msg) = @_;
682 if (!$self->{callback}) {
686 SL::Helper::Flash::flash_later('info', $msg) if $msg;
687 $self->_store_redirect_info_in_session;
688 print $::form->redirect_header($self->{callback});
691 $::dispatcher->end_request;
693 $main::lxdebug->leave_sub();
696 # sort of columns removed - empty sub
698 $main::lxdebug->enter_sub();
700 my ($self, @columns) = @_;
702 $main::lxdebug->leave_sub();
709 my ($self, $myconfig, $amount, $places, $dash) = @_;
710 SL::Helper::Number::_format_number($amount, $places, %$myconfig, dash => $dash);
714 $main::lxdebug->enter_sub(2);
719 $input =~ s/(^|[^\#]) \# (\d+) /$1$_[$2 - 1]/gx;
720 $input =~ s/(^|[^\#]) \#\{(\d+)\}/$1$_[$2 - 1]/gx;
721 $input =~ s/\#\#/\#/g;
723 $main::lxdebug->leave_sub(2);
731 my ($self, $myconfig, $amount) = @_;
732 SL::Helper::Number::_parse_number($amount, %$myconfig);
735 sub round_amount { shift; goto &SL::Helper::Number::_round_number; }
738 $main::lxdebug->enter_sub();
740 my ($self, $myconfig) = @_;
741 my ($out, $out_mode);
745 my $defaults = SL::DB::Default->get;
747 my $keep_temp_files = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files};
748 $self->{cwd} = getcwd();
749 my $temp_dir = File::Temp->newdir(
750 "kivitendo-print-XXXXXX",
751 DIR => $self->{cwd} . "/" . $::lx_office_conf{paths}->{userspath},
752 CLEANUP => !$keep_temp_files,
755 my $userspath = File::Spec->abs2rel($temp_dir->dirname);
756 $self->{tmpdir} = $temp_dir->dirname;
761 if ($self->{"format"} =~ /(opendocument|oasis)/i) {
762 $template_type = 'OpenDocument';
763 $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
765 } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
766 $template_type = 'LaTeX';
767 $ext_for_format = 'pdf';
769 } elsif (($self->{"format"} =~ /html/i) || (!$self->{"format"} && ($self->{"IN"} =~ /html$/i))) {
770 $template_type = 'HTML';
771 $ext_for_format = 'html';
773 } elsif ( $self->{"format"} =~ /excel/i ) {
774 $template_type = 'Excel';
775 $ext_for_format = 'xls';
777 } elsif ( defined $self->{'format'}) {
778 $self->error("Outputformat not defined. This may be a future feature: $self->{'format'}");
780 } elsif ( $self->{'format'} eq '' ) {
781 $self->error("No Outputformat given: $self->{'format'}");
783 } else { #Catch the rest
784 $self->error("Outputformat not defined: $self->{'format'}");
787 my $template = SL::Template::create(type => $template_type,
788 file_name => $self->{IN},
790 myconfig => $myconfig,
791 userspath => $userspath,
792 %{ $self->{TEMPLATE_DRIVER_OPTIONS} || {} });
794 # Copy the notes from the invoice/sales order etc. back to the variable "notes" because that is where most templates expect it to be.
795 $self->{"notes"} = $self->{ $self->{"formname"} . "notes" } if exists $self->{ $self->{"formname"} . "notes" };
797 if (!$self->{employee_id}) {
798 $self->{"employee_${_}"} = $myconfig->{$_} for qw(email tel fax name signature);
799 $self->{"employee_${_}"} = $defaults->$_ for qw(address businessnumber co_ustid company duns sepa_creditor_id taxnumber);
802 $self->{"myconfig_${_}"} = $myconfig->{$_} for grep { $_ ne 'dbpasswd' } keys %{ $myconfig };
803 $self->{$_} = $defaults->$_ for qw(co_ustid);
804 $self->{"myconfig_${_}"} = $defaults->$_ for qw(address businessnumber co_ustid company duns sepa_creditor_id taxnumber);
805 $self->{AUTH} = $::auth;
806 $self->{INSTANCE_CONF} = $::instance_conf;
807 $self->{LOCALE} = $::locale;
808 $self->{LXCONFIG} = $::lx_office_conf;
809 $self->{LXDEBUG} = $::lxdebug;
810 $self->{MYCONFIG} = \%::myconfig;
812 $self->{copies} = 1 if (($self->{copies} *= 1) <= 0);
814 # OUT is used for the media, screen, printer, email
815 # for postscript we store a copy in a temporary file
817 my ($temp_fh, $suffix);
818 $suffix = $self->{IN};
820 ($temp_fh, $self->{tmpfile}) = File::Temp::tempfile(
821 strftime('kivitendo-print-%Y%m%d%H%M%S-XXXXXX', localtime()),
822 SUFFIX => '.' . ($suffix || 'tex'),
824 UNLINK => $keep_temp_files ? 0 : 1,
827 chmod 0644, $self->{tmpfile} if $keep_temp_files;
828 (undef, undef, $self->{template_meta}{tmpfile}) = File::Spec->splitpath( $self->{tmpfile} );
831 $out_mode = $self->{OUT_MODE} || '>';
832 $self->{OUT} = "$self->{tmpfile}";
833 $self->{OUT_MODE} = '>';
836 my $command_formatter = sub {
837 my ($out_mode, $out) = @_;
838 return $out_mode eq '|-' ? SL::Template::create(type => 'ShellCommand', form => $self)->parse($out) : $out;
842 $self->{OUT} = $command_formatter->($self->{OUT_MODE}, $self->{OUT});
843 open(OUT, $self->{OUT_MODE}, $self->{OUT}) or $self->error("error on opening $self->{OUT} with mode $self->{OUT_MODE} : $!");
845 *OUT = ($::dispatcher->get_standard_filehandles)[1];
849 if (!$template->parse(*OUT)) {
851 $self->error("$self->{IN} : " . $template->get_error());
854 close OUT if $self->{OUT};
855 # check only one flag (webdav_documents)
856 # therefore copy to webdav, even if we do not have the webdav feature enabled (just archive)
857 my $copy_to_webdav = $::instance_conf->get_webdav_documents && !$self->{preview} && $self->{tmpdir} && $self->{tmpfile} && $self->{type}
858 && $self->{type} ne 'statement';
859 if ( $ext_for_format eq 'pdf' && $self->doc_storage_enabled ) {
860 $self->append_general_pdf_attachments(filepath => $self->{tmpdir}."/".$self->{tmpfile},
861 type => $self->{type});
863 if ($self->{media} eq 'file') {
864 copy(join('/', $self->{cwd}, $userspath, $self->{tmpfile}), $out =~ m|^/| ? $out : join('/', $self->{cwd}, $out)) if $template->uses_temp_file;
866 if ($copy_to_webdav) {
867 if (my $error = Common::copy_file_to_webdav_folder($self)) {
868 chdir("$self->{cwd}");
869 $self->error($error);
873 if (!$self->{preview} && $self->{attachment_type} !~ m{^dunning} && $self->doc_storage_enabled)
875 $self->{attachment_filename} ||= $self->generate_attachment_filename;
876 $self->store_pdf($self);
879 chdir("$self->{cwd}");
881 $::lxdebug->leave_sub();
886 if ($copy_to_webdav) {
887 if (my $error = Common::copy_file_to_webdav_folder($self)) {
888 chdir("$self->{cwd}");
889 $self->error($error);
893 if ( !$self->{preview} && $ext_for_format eq 'pdf' && $self->{attachment_type} !~ m{^dunning} && $self->doc_storage_enabled) {
894 $self->{attachment_filename} ||= $self->generate_attachment_filename;
895 my $file_obj = $self->store_pdf($self);
896 $self->{print_file_id} = $file_obj->id if $file_obj;
898 if ($self->{media} eq 'email') {
899 if ( getcwd() eq $self->{"tmpdir"} ) {
900 # in the case of generating pdf we are in the tmpdir, but WHY ???
901 $self->{tmpfile} = $userspath."/".$self->{tmpfile};
902 chdir("$self->{cwd}");
904 $self->send_email(\%::myconfig,$ext_for_format);
908 $self->{OUT_MODE} = $out_mode;
909 $self->output_file($template->get_mime_type,$command_formatter);
911 delete $self->{print_file_id};
915 chdir("$self->{cwd}");
916 $main::lxdebug->leave_sub();
919 sub get_bcc_defaults {
920 my ($self, $myconfig, $mybcc) = @_;
921 if (SL::DB::Default->get->bcc_to_login) {
922 $mybcc .= ", " if $mybcc;
923 $mybcc .= $myconfig->{email};
925 my $otherbcc = SL::DB::Default->get->global_bcc;
927 $mybcc .= ", " if $mybcc;
934 $main::lxdebug->enter_sub();
935 my ($self, $myconfig, $ext_for_format) = @_;
936 my $mail = Mailer->new;
938 map { $mail->{$_} = $self->{$_} }
939 qw(cc subject message format);
941 if ($self->{cc_employee}) {
942 my ($user, $my_emp_cc);
943 $user = SL::DB::Manager::AuthUser->find_by(login => $self->{cc_employee});
944 $my_emp_cc = $user->get_config_value('email') if ref $user eq 'SL::DB::AuthUser';
945 $mail->{cc} .= ", " if $mail->{cc};
946 $mail->{cc} .= $my_emp_cc if $my_emp_cc;
949 $mail->{bcc} = $self->get_bcc_defaults($myconfig, $self->{bcc});
950 $mail->{to} = $self->{EMAIL_RECIPIENT} ? $self->{EMAIL_RECIPIENT} : $self->{email};
951 $mail->{from} = qq|"$myconfig->{name}" <$myconfig->{email}>|;
952 $mail->{fileid} = time() . '.' . $$ . '.';
953 my $full_signature = $self->create_email_signature();
954 $full_signature =~ s/\r//g;
956 $mail->{attachments} = [];
958 # if we send html or plain text inline
959 if (($self->{format} eq 'html') && ($self->{sendmode} eq 'inline')) {
960 $mail->{content_type} = "text/html";
961 $mail->{message} =~ s/\r//g;
962 $mail->{message} =~ s{\n}{<br>\n}g;
963 $full_signature =~ s{\n}{<br>\n}g;
964 $mail->{message} .= $full_signature;
966 open(IN, "<", $self->{tmpfile})
967 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
968 $mail->{message} .= $_ while <IN>;
971 } elsif (($self->{attachment_policy} // '') ne 'no_file') {
972 my $attachment_name = $self->{attachment_filename} || $self->{tmpfile};
973 $attachment_name =~ s{\.(.+?)$}{.${ext_for_format}} if ($ext_for_format);
975 if (($self->{attachment_policy} // '') eq 'old_file') {
976 my ( $attfile ) = SL::File->get_all(object_id => $self->{id},
977 object_type => $self->{formname},
978 file_type => 'document');
981 $attfile->{override_file_name} = $attachment_name if $attachment_name;
982 push @attfiles, $attfile;
986 push @{ $mail->{attachments} }, { path => $self->{tmpfile},
987 id => $self->{print_file_id},
988 type => "application/pdf",
989 name => $attachment_name };
995 map { SL::File->get(id => $_) }
996 @{ $self->{attach_file_ids} // [] };
998 foreach my $attfile ( @attfiles ) {
999 push @{ $mail->{attachments} }, {
1000 path => $attfile->get_file,
1002 type => $attfile->mime_type,
1003 name => $attfile->{override_file_name} // $attfile->file_name,
1004 content => $attfile->get_content ? ${ $attfile->get_content } : undef,
1008 $mail->{message} =~ s/\r//g;
1009 $mail->{message} .= $full_signature;
1010 $self->{emailerr} = $mail->send();
1012 if ($self->{emailerr}) {
1014 $self->error($::locale->text('The email was not sent due to the following error: #1.', $self->{emailerr}));
1017 $self->{email_journal_id} = $mail->{journalentry};
1018 $self->{snumbers} = "emailjournal" . "_" . $self->{email_journal_id};
1019 $self->{what_done} = $::form->{type};
1020 $self->{addition} = "MAILED";
1021 $self->save_history;
1023 #write back for message info and mail journal
1024 $self->{cc} = $mail->{cc};
1025 $self->{bcc} = $mail->{bcc};
1026 $self->{email} = $mail->{to};
1028 $main::lxdebug->leave_sub();
1032 $main::lxdebug->enter_sub();
1034 my ($self,$mimeType,$command_formatter) = @_;
1035 my $numbytes = (-s $self->{tmpfile});
1036 open(IN, "<", $self->{tmpfile})
1037 or $self->error($self->cleanup . "$self->{tmpfile} : $!");
1040 $self->{copies} = 1 unless $self->{media} eq 'printer';
1042 chdir("$self->{cwd}");
1043 for my $i (1 .. $self->{copies}) {
1045 $self->{OUT} = $command_formatter->($self->{OUT_MODE}, $self->{OUT});
1047 open OUT, $self->{OUT_MODE}, $self->{OUT} or $self->error($self->cleanup . "$self->{OUT} : $!");
1048 print OUT $_ while <IN>;
1053 my %headers = ('-type' => $mimeType,
1054 '-connection' => 'close',
1055 '-charset' => 'UTF-8');
1057 $self->{attachment_filename} ||= $self->generate_attachment_filename;
1059 if ($self->{attachment_filename}) {
1062 '-attachment' => $self->{attachment_filename},
1063 '-content-length' => $numbytes,
1068 print $::request->cgi->header(%headers);
1070 $::locale->with_raw_io(\*STDOUT, sub { print while <IN> });
1074 $main::lxdebug->leave_sub();
1077 sub get_formname_translation {
1078 $main::lxdebug->enter_sub();
1079 my ($self, $formname) = @_;
1081 $formname ||= $self->{formname};
1083 $self->{recipient_locale} ||= Locale->lang_to_locale($self->{language});
1084 local $::locale = Locale->new($self->{recipient_locale});
1086 my %formname_translations = (
1087 bin_list => $main::locale->text('Bin List'),
1088 credit_note => $main::locale->text('Credit Note'),
1089 invoice => $main::locale->text('Invoice'),
1090 invoice_copy => $main::locale->text('Invoice Copy'),
1091 pick_list => $main::locale->text('Pick List'),
1092 proforma => $main::locale->text('Proforma Invoice'),
1093 purchase_order => $main::locale->text('Purchase Order'),
1094 request_quotation => $main::locale->text('RFQ'),
1095 sales_order => $main::locale->text('Confirmation'),
1096 sales_quotation => $main::locale->text('Quotation'),
1097 storno_invoice => $main::locale->text('Storno Invoice'),
1098 sales_delivery_order => $main::locale->text('Delivery Order'),
1099 purchase_delivery_order => $main::locale->text('Delivery Order'),
1100 dunning => $main::locale->text('Dunning'),
1101 dunning1 => $main::locale->text('Payment Reminder'),
1102 dunning2 => $main::locale->text('Dunning'),
1103 dunning3 => $main::locale->text('Last Dunning'),
1104 dunning_invoice => $main::locale->text('Dunning Invoice'),
1105 letter => $main::locale->text('Letter'),
1106 ic_supply => $main::locale->text('Intra-Community supply'),
1107 statement => $main::locale->text('Statement'),
1110 $main::lxdebug->leave_sub();
1111 return $formname_translations{$formname};
1114 sub get_cusordnumber_translation {
1115 $main::lxdebug->enter_sub();
1116 my ($self, $formname) = @_;
1118 $formname ||= $self->{formname};
1120 $self->{recipient_locale} ||= Locale->lang_to_locale($self->{language});
1121 local $::locale = Locale->new($self->{recipient_locale});
1124 $main::lxdebug->leave_sub();
1125 return $main::locale->text('Your Order');
1128 sub get_number_prefix_for_type {
1129 $main::lxdebug->enter_sub();
1133 (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1134 : ($self->{type} =~ /_quotation$/) ? 'quo'
1135 : ($self->{type} =~ /_delivery_order$/) ? 'do'
1136 : ($self->{type} =~ /letter/) ? 'letter'
1139 # better default like this?
1140 # : ($self->{type} =~ /(sales|purcharse)_order/ : 'ord';
1141 # : 'prefix_undefined';
1143 $main::lxdebug->leave_sub();
1147 sub get_extension_for_format {
1148 $main::lxdebug->enter_sub();
1151 my $extension = $self->{format} =~ /pdf/i ? ".pdf"
1152 : $self->{format} =~ /postscript/i ? ".ps"
1153 : $self->{format} =~ /opendocument/i ? ".odt"
1154 : $self->{format} =~ /excel/i ? ".xls"
1155 : $self->{format} =~ /html/i ? ".html"
1158 $main::lxdebug->leave_sub();
1162 sub generate_attachment_filename {
1163 $main::lxdebug->enter_sub();
1166 $self->{recipient_locale} ||= Locale->lang_to_locale($self->{language});
1167 my $recipient_locale = Locale->new($self->{recipient_locale});
1169 my $attachment_filename = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1170 my $prefix = $self->get_number_prefix_for_type();
1172 if ($self->{preview} && (first { $self->{type} eq $_ } qw(invoice credit_note))) {
1173 $attachment_filename .= ' (' . $recipient_locale->text('Preview') . ')' . $self->get_extension_for_format();
1175 } elsif ($attachment_filename && $self->{"${prefix}number"}) {
1176 $attachment_filename .= "_" . $self->{"${prefix}number"} . $self->get_extension_for_format();
1178 } elsif ($attachment_filename) {
1179 $attachment_filename .= $self->get_extension_for_format();
1182 $attachment_filename = "";
1185 $attachment_filename = $main::locale->quote_special_chars('filenames', $attachment_filename);
1186 $attachment_filename =~ s|[\s/\\]+|_|g;
1188 $main::lxdebug->leave_sub();
1189 return $attachment_filename;
1192 sub generate_email_subject {
1193 $main::lxdebug->enter_sub();
1196 my $subject = $main::locale->unquote_special_chars('HTML', $self->get_formname_translation());
1197 my $prefix = $self->get_number_prefix_for_type();
1199 if ($subject && $self->{"${prefix}number"}) {
1200 $subject .= " " . $self->{"${prefix}number"}
1203 if ($self->{cusordnumber}) {
1204 $subject = $self->get_cusordnumber_translation() . ' ' . $self->{cusordnumber} . ' / ' . $subject;
1207 $main::lxdebug->leave_sub();
1211 sub generate_email_body {
1212 $main::lxdebug->enter_sub();
1213 my ($self, %params) = @_;
1214 # simple german and english will work grammatically (most european languages as well)
1215 # Dear Mr Alan Greenspan:
1216 # Sehr geehrte Frau Meyer,
1217 # A l’attention de Mme Villeroy,
1218 # Gentile Signora Ferrari,
1221 if ($self->{cp_id} && !$params{record_email}) {
1222 my $givenname = SL::DB::Contact->load_cached($self->{cp_id})->cp_givenname; # for qw(gender givename name);
1223 my $name = SL::DB::Contact->load_cached($self->{cp_id})->cp_name; # for qw(gender givename name);
1224 my $gender = SL::DB::Contact->load_cached($self->{cp_id})->cp_gender; # for qw(gender givename name);
1225 my $mf = $gender eq 'f' ? 'female' : 'male';
1226 $body = GenericTranslations->get(translation_type => "salutation_$mf", language_id => $self->{language_id});
1227 $body .= ' ' . $givenname . ' ' . $name if $body;
1229 $body = GenericTranslations->get(translation_type => "salutation_general", language_id => $self->{language_id});
1232 return undef unless $body;
1234 my $translation_type = $params{translation_type} // "preset_text_$self->{formname}";
1235 my $main_body = GenericTranslations->get(translation_type => $translation_type, language_id => $self->{language_id});
1236 $main_body = GenericTranslations->get(translation_type => $params{fallback_translation_type}, language_id => $self->{language_id}) if !$main_body && $params{fallback_translation_type};
1237 $body .= GenericTranslations->get(translation_type => "salutation_punctuation_mark", language_id => $self->{language_id}) . "\n\n";
1238 $body .= $main_body;
1240 $body = $main::locale->unquote_special_chars('HTML', $body);
1242 $main::lxdebug->leave_sub();
1247 $main::lxdebug->enter_sub();
1249 my ($self, $application) = @_;
1251 my $error_code = $?;
1253 chdir("$self->{tmpdir}");
1256 if ((-1 == $error_code) || (127 == (($error_code) >> 8))) {
1257 push @err, $::locale->text('The application "#1" was not found on the system.', $application || 'pdflatex') . ' ' . $::locale->text('Please contact your administrator.');
1259 } elsif (-f "$self->{tmpfile}.err") {
1260 open(FH, "<:encoding(UTF-8)", "$self->{tmpfile}.err");
1265 if ($self->{tmpfile} && !($::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files})) {
1266 $self->{tmpfile} =~ s|.*/||g;
1268 $self->{tmpfile} =~ s/\.\w+$//g;
1269 my $tmpfile = $self->{tmpfile};
1270 unlink(<$tmpfile.*>);
1273 chdir("$self->{cwd}");
1275 $main::lxdebug->leave_sub();
1281 $main::lxdebug->enter_sub();
1283 my ($self, $date, $myconfig) = @_;
1286 if ($date && $date =~ /\D/) {
1288 if ($myconfig->{dateformat} =~ /^yy/) {
1289 ($yy, $mm, $dd) = split /\D/, $date;
1291 if ($myconfig->{dateformat} =~ /^mm/) {
1292 ($mm, $dd, $yy) = split /\D/, $date;
1294 if ($myconfig->{dateformat} =~ /^dd/) {
1295 ($dd, $mm, $yy) = split /\D/, $date;
1300 $yy = ($yy < 70) ? $yy + 2000 : $yy;
1301 $yy = ($yy >= 70 && $yy <= 99) ? $yy + 1900 : $yy;
1303 $dd = "0$dd" if ($dd < 10);
1304 $mm = "0$mm" if ($mm < 10);
1306 $date = "$yy$mm$dd";
1309 $main::lxdebug->leave_sub();
1314 # Database routines used throughout
1315 # DB Handling got moved to SL::DB, these are only shims for compatibility
1318 SL::DB->client->dbh;
1321 sub get_standard_dbh {
1322 my $dbh = SL::DB->client->dbh;
1324 if ($dbh && !$dbh->{Active}) {
1325 $main::lxdebug->message(LXDebug->INFO(), "get_standard_dbh: \$dbh is defined but not Active anymore");
1326 SL::DB->client->dbh(undef);
1329 SL::DB->client->dbh;
1332 sub disconnect_standard_dbh {
1333 SL::DB->client->dbh->rollback;
1339 $main::lxdebug->enter_sub();
1341 my ($self, $date, $myconfig) = @_;
1342 my $dbh = $self->get_standard_dbh;
1344 my $query = "SELECT 1 FROM defaults WHERE ? < closedto";
1345 my $sth = prepare_execute_query($self, $dbh, $query, conv_date($date));
1347 # Falls $date = '' - Fehlermeldung aus der Datenbank. Ich denke,
1348 # es ist sicher ein conv_date vorher IMMER auszuführen.
1349 # Testfälle ohne definiertes closedto:
1350 # Leere Datumseingabe i.O.
1351 # SELECT 1 FROM defaults WHERE '' < closedto
1352 # normale Zahlungsbuchung über Rechnungsmaske i.O.
1353 # SELECT 1 FROM defaults WHERE '10.05.2011' < closedto
1354 # Testfälle mit definiertem closedto (30.04.2011):
1355 # Leere Datumseingabe i.O.
1356 # SELECT 1 FROM defaults WHERE '' < closedto
1357 # normale Buchung im geschloßenem Zeitraum i.O.
1358 # SELECT 1 FROM defaults WHERE '21.04.2011' < closedto
1359 # Fehlermeldung: Es können keine Zahlungen für abgeschlossene Bücher gebucht werden!
1360 # normale Buchung in aktiver Buchungsperiode i.O.
1361 # SELECT 1 FROM defaults WHERE '01.05.2011' < closedto
1363 my ($closed) = $sth->fetchrow_array;
1365 $main::lxdebug->leave_sub();
1370 # prevents bookings to the to far away future
1371 sub date_max_future {
1372 $main::lxdebug->enter_sub();
1374 my ($self, $date, $myconfig) = @_;
1375 my $dbh = $self->get_standard_dbh;
1377 my $query = "SELECT 1 FROM defaults WHERE ? - current_date > max_future_booking_interval";
1378 my $sth = prepare_execute_query($self, $dbh, $query, conv_date($date));
1380 my ($max_future_booking_interval) = $sth->fetchrow_array;
1382 $main::lxdebug->leave_sub();
1384 return $max_future_booking_interval;
1388 sub update_balance {
1389 $main::lxdebug->enter_sub();
1391 my ($self, $dbh, $table, $field, $where, $value, @values) = @_;
1393 # if we have a value, go do it
1396 # retrieve balance from table
1397 my $query = "SELECT $field FROM $table WHERE $where FOR UPDATE";
1398 my $sth = prepare_execute_query($self, $dbh, $query, @values);
1399 my ($balance) = $sth->fetchrow_array;
1405 $query = "UPDATE $table SET $field = $balance WHERE $where";
1406 do_query($self, $dbh, $query, @values);
1408 $main::lxdebug->leave_sub();
1411 sub update_exchangerate {
1412 $main::lxdebug->enter_sub();
1414 my ($self, $dbh, $curr, $transdate, $buy, $sell) = @_;
1416 # some sanity check for currency
1418 $main::lxdebug->leave_sub();
1421 $query = qq|SELECT name AS curr FROM currencies WHERE id=(SELECT currency_id FROM defaults)|;
1423 my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
1425 if ($curr eq $defaultcurrency) {
1426 $main::lxdebug->leave_sub();
1430 $query = qq|SELECT e.currency_id FROM exchangerate e
1431 WHERE e.currency_id = (SELECT cu.id FROM currencies cu WHERE cu.name=?) AND e.transdate = ?
1433 my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate);
1442 $buy = conv_i($buy, "NULL");
1443 $sell = conv_i($sell, "NULL");
1446 if ($buy != 0 && $sell != 0) {
1447 $set = "buy = $buy, sell = $sell";
1448 } elsif ($buy != 0) {
1449 $set = "buy = $buy";
1450 } elsif ($sell != 0) {
1451 $set = "sell = $sell";
1454 if ($sth->fetchrow_array) {
1455 $query = qq|UPDATE exchangerate
1457 WHERE currency_id = (SELECT id FROM currencies WHERE name = ?)
1461 $query = qq|INSERT INTO exchangerate (currency_id, buy, sell, transdate)
1462 VALUES ((SELECT id FROM currencies WHERE name = ?), $buy, $sell, ?)|;
1465 do_query($self, $dbh, $query, $curr, $transdate);
1467 $main::lxdebug->leave_sub();
1470 sub save_exchangerate {
1471 $main::lxdebug->enter_sub();
1473 my ($self, $myconfig, $currency, $transdate, $rate, $fld) = @_;
1475 SL::DB->client->with_transaction(sub {
1476 my $dbh = SL::DB->client->dbh;
1480 $buy = $rate if $fld eq 'buy';
1481 $sell = $rate if $fld eq 'sell';
1484 $self->update_exchangerate($dbh, $currency, $transdate, $buy, $sell);
1486 }) or do { die SL::DB->client->error };
1488 $main::lxdebug->leave_sub();
1491 sub get_exchangerate {
1492 $main::lxdebug->enter_sub();
1494 my ($self, $dbh, $curr, $transdate, $fld) = @_;
1497 unless ($transdate && $curr) {
1498 $main::lxdebug->leave_sub();
1502 $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|;
1504 my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
1506 if ($curr eq $defaultcurrency) {
1507 $main::lxdebug->leave_sub();
1511 $query = qq|SELECT e.$fld FROM exchangerate e
1512 WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|;
1513 my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate);
1517 $main::lxdebug->leave_sub();
1519 return $exchangerate;
1522 sub check_exchangerate {
1523 $main::lxdebug->enter_sub();
1525 my ($self, $myconfig, $currency, $transdate, $fld) = @_;
1527 if ($fld !~/^buy|sell$/) {
1528 $self->error('Fatal: check_exchangerate called with invalid buy/sell argument');
1531 unless ($transdate) {
1532 $main::lxdebug->leave_sub();
1536 my ($defaultcurrency) = $self->get_default_currency($myconfig);
1538 if ($currency eq $defaultcurrency) {
1539 $main::lxdebug->leave_sub();
1543 my $dbh = $self->get_standard_dbh($myconfig);
1544 my $query = qq|SELECT e.$fld FROM exchangerate e
1545 WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|;
1547 my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate);
1549 $main::lxdebug->leave_sub();
1551 return $exchangerate;
1554 sub get_all_currencies {
1555 $main::lxdebug->enter_sub();
1558 my $myconfig = shift || \%::myconfig;
1559 my $dbh = $self->get_standard_dbh($myconfig);
1561 my $query = qq|SELECT name FROM currencies|;
1562 my @currencies = map { $_->{name} } selectall_hashref_query($self, $dbh, $query);
1564 $main::lxdebug->leave_sub();
1569 sub get_default_currency {
1570 $main::lxdebug->enter_sub();
1572 my ($self, $myconfig) = @_;
1573 my $dbh = $self->get_standard_dbh($myconfig);
1574 my $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|;
1576 my ($defaultcurrency) = selectrow_query($self, $dbh, $query);
1578 $main::lxdebug->leave_sub();
1580 return $defaultcurrency;
1583 sub set_payment_options {
1584 my ($self, $myconfig, $transdate, $type) = @_;
1586 my $terms = $self->{payment_id} ? SL::DB::PaymentTerm->new(id => $self->{payment_id})->load : undef;
1589 my $is_invoice = $type =~ m{invoice}i;
1591 $transdate ||= $self->{invdate} || $self->{transdate};
1592 my $due_date = $self->{duedate} || $self->{reqdate};
1594 $self->{$_} = $terms->$_ for qw(terms_netto terms_skonto percent_skonto);
1595 $self->{payment_description} = $terms->description;
1596 $self->{netto_date} = $terms->calc_date(reference_date => $transdate, due_date => $due_date, terms => 'net')->to_kivitendo;
1597 $self->{skonto_date} = $terms->calc_date(reference_date => $transdate, due_date => $due_date, terms => 'discount')->to_kivitendo;
1599 my ($invtotal, $total);
1600 my (%amounts, %formatted_amounts);
1602 if ($self->{type} =~ /_order$/) {
1603 $amounts{invtotal} = $self->{ordtotal};
1604 $amounts{total} = $self->{ordtotal};
1606 } elsif ($self->{type} =~ /_quotation$/) {
1607 $amounts{invtotal} = $self->{quototal};
1608 $amounts{total} = $self->{quototal};
1611 $amounts{invtotal} = $self->{invtotal};
1612 $amounts{total} = $self->{total};
1614 map { $amounts{$_} = $self->parse_amount($myconfig, $amounts{$_}) } keys %amounts;
1616 $amounts{skonto_in_percent} = 100.0 * $self->{percent_skonto};
1617 $amounts{skonto_amount} = $amounts{invtotal} * $self->{percent_skonto};
1618 $amounts{invtotal_wo_skonto} = $amounts{invtotal} * (1 - $self->{percent_skonto});
1619 $amounts{total_wo_skonto} = $amounts{total} * (1 - $self->{percent_skonto});
1621 foreach (keys %amounts) {
1622 $amounts{$_} = $self->round_amount($amounts{$_}, 2);
1623 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}, 2);
1626 if ($self->{"language_id"}) {
1627 my $language = SL::DB::Language->new(id => $self->{language_id})->load;
1629 $self->{payment_terms} = $type =~ m{invoice}i ? $terms->translated_attribute('description_long_invoice', $language->id) : undef;
1630 $self->{payment_terms} ||= $terms->translated_attribute('description_long', $language->id);
1632 if ($language->output_dateformat) {
1633 foreach my $key (qw(netto_date skonto_date)) {
1634 $self->{$key} = $::locale->reformat_date($myconfig, $self->{$key}, $language->output_dateformat, $language->output_longdates);
1638 if ($language->output_numberformat && ($language->output_numberformat ne $myconfig->{numberformat})) {
1639 local $myconfig->{numberformat};
1640 $myconfig->{"numberformat"} = $language->output_numberformat;
1641 $formatted_amounts{$_} = $self->format_amount($myconfig, $amounts{$_}) for keys %amounts;
1645 $self->{payment_terms} = $self->{payment_terms} || ($is_invoice ? $terms->description_long_invoice : undef) || $terms->description_long;
1647 $self->{payment_terms} =~ s/<%netto_date%>/$self->{netto_date}/g;
1648 $self->{payment_terms} =~ s/<%skonto_date%>/$self->{skonto_date}/g;
1649 $self->{payment_terms} =~ s/<%currency%>/$self->{currency}/g;
1650 $self->{payment_terms} =~ s/<%terms_netto%>/$self->{terms_netto}/g;
1651 $self->{payment_terms} =~ s/<%account_number%>/$self->{account_number}/g;
1652 $self->{payment_terms} =~ s/<%bank%>/$self->{bank}/g;
1653 $self->{payment_terms} =~ s/<%bank_code%>/$self->{bank_code}/g;
1654 $self->{payment_terms} =~ s/<\%bic\%>/$self->{bic}/g;
1655 $self->{payment_terms} =~ s/<\%iban\%>/$self->{iban}/g;
1656 $self->{payment_terms} =~ s/<\%mandate_date_of_signature\%>/$self->{mandate_date_of_signature}/g;
1657 $self->{payment_terms} =~ s/<\%mandator_id\%>/$self->{mandator_id}/g;
1659 map { $self->{payment_terms} =~ s/<%${_}%>/$formatted_amounts{$_}/g; } keys %formatted_amounts;
1661 $self->{skonto_in_percent} = $formatted_amounts{skonto_in_percent};
1665 sub get_template_language {
1666 $main::lxdebug->enter_sub();
1668 my ($self, $myconfig) = @_;
1670 my $template_code = "";
1672 if ($self->{language_id}) {
1673 my $dbh = $self->get_standard_dbh($myconfig);
1674 my $query = qq|SELECT template_code FROM language WHERE id = ?|;
1675 ($template_code) = selectrow_query($self, $dbh, $query, $self->{language_id});
1678 $main::lxdebug->leave_sub();
1680 return $template_code;
1683 sub get_printer_code {
1684 $main::lxdebug->enter_sub();
1686 my ($self, $myconfig) = @_;
1688 my $template_code = "";
1690 if ($self->{printer_id}) {
1691 my $dbh = $self->get_standard_dbh($myconfig);
1692 my $query = qq|SELECT template_code, printer_command FROM printers WHERE id = ?|;
1693 ($template_code, $self->{printer_command}) = selectrow_query($self, $dbh, $query, $self->{printer_id});
1696 $main::lxdebug->leave_sub();
1698 return $template_code;
1702 $main::lxdebug->enter_sub();
1704 my ($self, $myconfig) = @_;
1706 my $template_code = "";
1708 if ($self->{shipto_id}) {
1709 my $dbh = $self->get_standard_dbh($myconfig);
1710 my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
1711 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{shipto_id});
1712 map({ $self->{$_} = $ref->{$_} } keys(%$ref));
1714 my $cvars = CVar->get_custom_variables(
1717 trans_id => $self->{shipto_id},
1719 $self->{"shiptocvar_$_->{name}"} = $_->{value} for @{ $cvars };
1722 $main::lxdebug->leave_sub();
1726 my ($self, $dbh, $id, $module) = @_;
1731 foreach my $item (qw(name department_1 department_2 street zipcode city country gln
1732 contact phone fax email)) {
1733 if ($self->{"shipto$item"}) {
1734 $shipto = 1 if ($self->{$item} ne $self->{"shipto$item"});
1736 push(@values, $self->{"shipto${item}"});
1741 # shiptocp_gender only makes sense, if any other shipto attribute is set.
1742 # Because shiptocp_gender is set to 'm' by default in forms
1743 # it must not be considered above to decide if shiptos has to be added or
1744 # updated, but must be inserted or updated as well in case.
1745 push(@values, $self->{shiptocp_gender});
1747 my $shipto_id = $self->{shipto_id};
1749 if ($self->{shipto_id}) {
1750 my $query = qq|UPDATE shipto set
1752 shiptodepartment_1 = ?,
1753 shiptodepartment_2 = ?,
1763 shiptocp_gender = ?,
1764 WHERE shipto_id = ?|;
1765 do_query($self, $dbh, $query, @values, $self->{shipto_id});
1767 my $query = qq|SELECT * FROM shipto
1768 WHERE shiptoname = ? AND
1769 shiptodepartment_1 = ? AND
1770 shiptodepartment_2 = ? AND
1771 shiptostreet = ? AND
1772 shiptozipcode = ? AND
1774 shiptocountry = ? AND
1776 shiptocontact = ? AND
1780 shiptocp_gender = ? AND
1783 my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1786 qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
1787 shiptostreet, shiptozipcode, shiptocity, shiptocountry, shiptogln,
1788 shiptocontact, shiptophone, shiptofax, shiptoemail, shiptocp_gender, module)
1789 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
1790 do_query($self, $dbh, $insert_query, $id, @values, $module);
1792 $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
1795 $shipto_id = $insert_check->{shipto_id};
1798 return unless $shipto_id;
1800 CVar->save_custom_variables(
1803 trans_id => $shipto_id,
1805 name_prefix => 'shipto',
1810 $main::lxdebug->enter_sub();
1812 my ($self, $dbh) = @_;
1814 $dbh ||= $self->get_standard_dbh(\%main::myconfig);
1816 my $query = qq|SELECT id, name FROM employee WHERE login = ?|;
1817 ($self->{"employee_id"}, $self->{"employee"}) = selectrow_query($self, $dbh, $query, $self->{login});
1818 $self->{"employee_id"} *= 1;
1820 $main::lxdebug->leave_sub();
1823 sub get_employee_data {
1824 $main::lxdebug->enter_sub();
1828 my $defaults = SL::DB::Default->get;
1830 Common::check_params(\%params, qw(prefix));
1831 Common::check_params_x(\%params, qw(id));
1834 $main::lxdebug->leave_sub();
1838 my $myconfig = \%main::myconfig;
1839 my $dbh = $params{dbh} || $self->get_standard_dbh($myconfig);
1841 my ($login, $deleted) = selectrow_query($self, $dbh, qq|SELECT login,deleted FROM employee WHERE id = ?|, conv_i($params{id}));
1844 # login already fetched and still the same client (mandant) | same for both cases (delete|!delete)
1845 $self->{$params{prefix} . '_login'} = $login;
1846 $self->{$params{prefix} . "_${_}"} = $defaults->$_ for qw(address businessnumber co_ustid company duns taxnumber);
1849 # get employee data from auth.user_config
1850 my $user = User->new(login => $login);
1851 $self->{$params{prefix} . "_${_}"} = $user->{$_} for qw(email fax name signature tel);
1853 # get saved employee data from employee
1854 my $employee = SL::DB::Manager::Employee->find_by(id => conv_i($params{id}));
1855 $self->{$params{prefix} . "_${_}"} = $employee->{"deleted_$_"} for qw(email fax signature tel);
1856 $self->{$params{prefix} . "_name"} = $employee->name;
1859 $main::lxdebug->leave_sub();
1863 $main::lxdebug->enter_sub();
1865 my ($self, $dbh, $id, $key) = @_;
1867 $key = "all_contacts" unless ($key);
1871 $main::lxdebug->leave_sub();
1876 qq|SELECT cp_id, cp_cv_id, cp_name, cp_givenname, cp_abteilung | .
1877 qq|FROM contacts | .
1878 qq|WHERE cp_cv_id = ? | .
1879 qq|ORDER BY lower(cp_name)|;
1881 $self->{$key} = selectall_hashref_query($self, $dbh, $query, $id);
1883 $main::lxdebug->leave_sub();
1887 $main::lxdebug->enter_sub();
1889 my ($self, $dbh, $key) = @_;
1891 my ($all, $old_id, $where, @values);
1893 if (ref($key) eq "HASH") {
1896 $key = "ALL_PROJECTS";
1898 foreach my $p (keys(%{$params})) {
1900 $all = $params->{$p};
1901 } elsif ($p eq "old_id") {
1902 $old_id = $params->{$p};
1903 } elsif ($p eq "key") {
1904 $key = $params->{$p};
1910 $where = "WHERE active ";
1912 if (ref($old_id) eq "ARRAY") {
1913 my @ids = grep({ $_ } @{$old_id});
1915 $where .= " OR id IN (" . join(",", map({ "?" } @ids)) . ") ";
1916 push(@values, @ids);
1919 $where .= " OR (id = ?) ";
1920 push(@values, $old_id);
1926 qq|SELECT id, projectnumber, description, active | .
1929 qq|ORDER BY lower(projectnumber)|;
1931 $self->{$key} = selectall_hashref_query($self, $dbh, $query, @values);
1933 $main::lxdebug->leave_sub();
1937 $main::lxdebug->enter_sub();
1939 my ($self, $dbh, $key) = @_;
1941 $key = "all_printers" unless ($key);
1943 my $query = qq|SELECT id, printer_description, printer_command, template_code FROM printers|;
1945 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1947 $main::lxdebug->leave_sub();
1951 $main::lxdebug->enter_sub();
1953 my ($self, $dbh, $params) = @_;
1956 $key = $params->{key};
1957 $key = "all_charts" unless ($key);
1959 my $transdate = quote_db_date($params->{transdate});
1962 qq|SELECT c.id, c.accno, c.description, c.link, c.charttype, tk.taxkey_id, tk.tax_id | .
1964 qq|LEFT JOIN taxkeys tk ON | .
1965 qq|(tk.id = (SELECT id FROM taxkeys | .
1966 qq| WHERE taxkeys.chart_id = c.id AND startdate <= $transdate | .
1967 qq| ORDER BY startdate DESC LIMIT 1)) | .
1968 qq|ORDER BY c.accno|;
1970 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1972 $main::lxdebug->leave_sub();
1976 $main::lxdebug->enter_sub();
1978 my ($self, $dbh, $key) = @_;
1980 $key = "all_taxzones" unless ($key);
1982 $tzfilter = "WHERE obsolete is FALSE" if $key eq 'ALL_ACTIVE_TAXZONES';
1984 my $query = qq|SELECT * FROM tax_zones $tzfilter ORDER BY sortkey|;
1986 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
1988 $main::lxdebug->leave_sub();
1991 sub _get_employees {
1992 $main::lxdebug->enter_sub();
1994 my ($self, $dbh, $params) = @_;
1999 if (ref $params eq 'HASH') {
2000 $key = $params->{key};
2001 $deleted = $params->{deleted};
2007 $key ||= "all_employees";
2008 my $filter = $deleted ? '' : 'WHERE NOT COALESCE(deleted, FALSE)';
2009 $self->{$key} = selectall_hashref_query($self, $dbh, qq|SELECT * FROM employee $filter ORDER BY lower(name)|);
2011 $main::lxdebug->leave_sub();
2014 sub _get_business_types {
2015 $main::lxdebug->enter_sub();
2017 my ($self, $dbh, $key) = @_;
2019 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2020 $options->{key} ||= "all_business_types";
2023 if (exists $options->{salesman}) {
2024 $where = 'WHERE ' . ($options->{salesman} ? '' : 'NOT ') . 'COALESCE(salesman)';
2027 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, qq|SELECT * FROM business $where ORDER BY lower(description)|);
2029 $main::lxdebug->leave_sub();
2032 sub _get_languages {
2033 $main::lxdebug->enter_sub();
2035 my ($self, $dbh, $key) = @_;
2037 $key = "all_languages" unless ($key);
2039 my $query = qq|SELECT * FROM language ORDER BY id|;
2041 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2043 $main::lxdebug->leave_sub();
2046 sub _get_dunning_configs {
2047 $main::lxdebug->enter_sub();
2049 my ($self, $dbh, $key) = @_;
2051 $key = "all_dunning_configs" unless ($key);
2053 my $query = qq|SELECT * FROM dunning_config ORDER BY dunning_level|;
2055 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2057 $main::lxdebug->leave_sub();
2060 sub _get_currencies {
2061 $main::lxdebug->enter_sub();
2063 my ($self, $dbh, $key) = @_;
2065 $key = "all_currencies" unless ($key);
2067 $self->{$key} = [$self->get_all_currencies()];
2069 $main::lxdebug->leave_sub();
2073 $main::lxdebug->enter_sub();
2075 my ($self, $dbh, $key) = @_;
2077 $key = "all_payments" unless ($key);
2079 my $query = qq|SELECT * FROM payment_terms ORDER BY sortkey|;
2081 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2083 $main::lxdebug->leave_sub();
2086 sub _get_customers {
2087 $main::lxdebug->enter_sub();
2089 my ($self, $dbh, $key) = @_;
2091 my $options = ref $key eq 'HASH' ? $key : { key => $key };
2092 $options->{key} ||= "all_customers";
2093 my $limit_clause = $options->{limit} ? "LIMIT $options->{limit}" : '';
2096 push @where, qq|business_id IN (SELECT id FROM business WHERE salesman)| if $options->{business_is_salesman};
2097 push @where, qq|NOT obsolete| if !$options->{with_obsolete};
2098 my $where_str = @where ? "WHERE " . join(" AND ", map { "($_)" } @where) : '';
2100 my $query = qq|SELECT * FROM customer $where_str ORDER BY name $limit_clause|;
2101 $self->{ $options->{key} } = selectall_hashref_query($self, $dbh, $query);
2103 $main::lxdebug->leave_sub();
2107 $main::lxdebug->enter_sub();
2109 my ($self, $dbh, $key) = @_;
2111 $key = "all_vendors" unless ($key);
2113 my $query = qq|SELECT * FROM vendor WHERE NOT obsolete ORDER BY name|;
2115 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2117 $main::lxdebug->leave_sub();
2120 sub _get_departments {
2121 $main::lxdebug->enter_sub();
2123 my ($self, $dbh, $key) = @_;
2125 $key = "all_departments" unless ($key);
2127 my $query = qq|SELECT * FROM department ORDER BY description|;
2129 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2131 $main::lxdebug->leave_sub();
2134 sub _get_warehouses {
2135 $main::lxdebug->enter_sub();
2137 my ($self, $dbh, $param) = @_;
2139 my ($key, $bins_key);
2141 if ('' eq ref $param) {
2145 $key = $param->{key};
2146 $bins_key = $param->{bins};
2149 my $query = qq|SELECT w.* FROM warehouse w
2150 WHERE (NOT w.invalid) AND
2151 ((SELECT COUNT(b.*) FROM bin b WHERE b.warehouse_id = w.id) > 0)
2152 ORDER BY w.sortkey|;
2154 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2157 $query = qq|SELECT id, description FROM bin WHERE warehouse_id = ?
2158 ORDER BY description|;
2159 my $sth = prepare_query($self, $dbh, $query);
2161 foreach my $warehouse (@{ $self->{$key} }) {
2162 do_statement($self, $sth, $query, $warehouse->{id});
2163 $warehouse->{$bins_key} = [];
2165 while (my $ref = $sth->fetchrow_hashref()) {
2166 push @{ $warehouse->{$bins_key} }, $ref;
2172 $main::lxdebug->leave_sub();
2176 $main::lxdebug->enter_sub();
2178 my ($self, $dbh, $table, $key, $sortkey) = @_;
2180 my $query = qq|SELECT * FROM $table|;
2181 $query .= qq| ORDER BY $sortkey| if ($sortkey);
2183 $self->{$key} = selectall_hashref_query($self, $dbh, $query);
2185 $main::lxdebug->leave_sub();
2189 $main::lxdebug->enter_sub();
2194 croak "get_lists: shipto is no longer supported" if $params{shipto};
2196 my $dbh = $self->get_standard_dbh(\%main::myconfig);
2197 my ($sth, $query, $ref);
2200 if ($params{contacts}) {
2201 $vc = 'customer' if $self->{"vc"} eq "customer";
2202 $vc = 'vendor' if $self->{"vc"} eq "vendor";
2203 die "invalid use of get_lists, need 'vc'" unless $vc;
2204 $vc_id = $self->{"${vc}_id"};
2207 if ($params{"contacts"}) {
2208 $self->_get_contacts($dbh, $vc_id, $params{"contacts"});
2211 if ($params{"projects"} || $params{"all_projects"}) {
2212 $self->_get_projects($dbh, $params{"all_projects"} ?
2213 $params{"all_projects"} : $params{"projects"},
2214 $params{"all_projects"} ? 1 : 0);
2217 if ($params{"printers"}) {
2218 $self->_get_printers($dbh, $params{"printers"});
2221 if ($params{"languages"}) {
2222 $self->_get_languages($dbh, $params{"languages"});
2225 if ($params{"charts"}) {
2226 $self->_get_charts($dbh, $params{"charts"});
2229 if ($params{"taxzones"}) {
2230 $self->_get_taxzones($dbh, $params{"taxzones"});
2233 if ($params{"employees"}) {
2234 $self->_get_employees($dbh, $params{"employees"});
2237 if ($params{"salesmen"}) {
2238 $self->_get_employees($dbh, $params{"salesmen"});
2241 if ($params{"business_types"}) {
2242 $self->_get_business_types($dbh, $params{"business_types"});
2245 if ($params{"dunning_configs"}) {
2246 $self->_get_dunning_configs($dbh, $params{"dunning_configs"});
2249 if($params{"currencies"}) {
2250 $self->_get_currencies($dbh, $params{"currencies"});
2253 if($params{"customers"}) {
2254 $self->_get_customers($dbh, $params{"customers"});
2257 if($params{"vendors"}) {
2258 if (ref $params{"vendors"} eq 'HASH') {
2259 $self->_get_vendors($dbh, $params{"vendors"}{key}, $params{"vendors"}{limit});
2261 $self->_get_vendors($dbh, $params{"vendors"});
2265 if($params{"payments"}) {
2266 $self->_get_payments($dbh, $params{"payments"});
2269 if($params{"departments"}) {
2270 $self->_get_departments($dbh, $params{"departments"});
2273 if ($params{price_factors}) {
2274 $self->_get_simple($dbh, 'price_factors', $params{price_factors}, 'sortkey');
2277 if ($params{warehouses}) {
2278 $self->_get_warehouses($dbh, $params{warehouses});
2281 if ($params{partsgroup}) {
2282 $self->get_partsgroup(\%main::myconfig, { all => 1, target => $params{partsgroup} });
2285 $main::lxdebug->leave_sub();
2288 # this sub gets the id and name from $table
2290 $main::lxdebug->enter_sub();
2292 my ($self, $myconfig, $table) = @_;
2294 # connect to database
2295 my $dbh = $self->get_standard_dbh($myconfig);
2297 $table = $table eq "customer" ? "customer" : "vendor";
2298 my $arap = $self->{arap} eq "ar" ? "ar" : "ap";
2300 my ($query, @values);
2302 if (!$self->{openinvoices}) {
2304 if ($self->{customernumber} ne "") {
2305 $where = qq|(vc.customernumber ILIKE ?)|;
2306 push(@values, like($self->{customernumber}));
2308 $where = qq|(vc.name ILIKE ?)|;
2309 push(@values, like($self->{$table}));
2313 qq~SELECT vc.id, vc.name,
2314 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2316 WHERE $where AND (NOT vc.obsolete)
2320 qq~SELECT DISTINCT vc.id, vc.name,
2321 vc.street || ' ' || vc.zipcode || ' ' || vc.city || ' ' || vc.country AS address
2323 JOIN $table vc ON (a.${table}_id = vc.id)
2324 WHERE NOT (a.amount = a.paid) AND (vc.name ILIKE ?)
2326 push(@values, like($self->{$table}));
2329 $self->{name_list} = selectall_hashref_query($self, $dbh, $query, @values);
2331 $main::lxdebug->leave_sub();
2333 return scalar(@{ $self->{name_list} });
2338 my ($self, $table, $provided_dbh) = @_;
2340 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh;
2341 return unless $self->{id};
2342 croak ("wrong call, no valid table defined") unless $table =~ /^(oe|ar|ap|delivery_orders|parts)$/;
2344 my $query = "SELECT mtime, itime FROM " . $table . " WHERE id = ?";
2345 my $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2346 $ref->{mtime} ||= $ref->{itime};
2347 $self->{lastmtime} = $ref->{mtime};
2351 sub mtime_ischanged {
2352 my ($self, $table, $option) = @_;
2354 return unless $self->{id};
2355 croak ("wrong call, no valid table defined") unless $table =~ /^(oe|ar|ap|delivery_orders|parts)$/;
2357 my $query = "SELECT mtime, itime FROM " . $table . " WHERE id = ?";
2358 my $ref = selectfirst_hashref_query($self, $self->get_standard_dbh, $query, $self->{id});
2359 $ref->{mtime} ||= $ref->{itime};
2361 if ($self->{lastmtime} && $self->{lastmtime} ne $ref->{mtime} ) {
2362 $self->error(($option eq 'mail') ?
2363 t8("The document has been changed by another user. No mail was sent. Please reopen it in another window and copy the changes to the new window") :
2364 t8("The document has been changed by another user. Please reopen it in another window and copy the changes to the new window")
2366 $::dispatcher->end_request;
2370 # language_payment duplicates some of the functionality of all_vc (language,
2371 # printer, payment_terms), and at least in the case of sales invoices both
2372 # all_vc and language_payment are called when adding new invoices
2373 sub language_payment {
2374 $main::lxdebug->enter_sub();
2376 my ($self, $myconfig) = @_;
2378 my $dbh = $self->get_standard_dbh($myconfig);
2380 my $query = qq|SELECT id, description
2384 $self->{languages} = selectall_hashref_query($self, $dbh, $query);
2387 $query = qq|SELECT printer_description, id
2389 ORDER BY printer_description|;
2391 $self->{printers} = selectall_hashref_query($self, $dbh, $query);
2394 $query = qq|SELECT id, description
2396 WHERE ( obsolete IS FALSE OR id = ? )
2398 $self->{payment_terms} = selectall_hashref_query($self, $dbh, $query, $self->{payment_id} || undef);
2400 # get buchungsgruppen
2401 $query = qq|SELECT id, description
2402 FROM buchungsgruppen|;
2404 $self->{BUCHUNGSGRUPPEN} = selectall_hashref_query($self, $dbh, $query);
2406 $main::lxdebug->leave_sub();
2409 # this is only used for reports
2410 sub all_departments {
2411 $main::lxdebug->enter_sub();
2413 my ($self, $myconfig, $table) = @_;
2415 my $dbh = $self->get_standard_dbh($myconfig);
2417 my $query = qq|SELECT id, description
2419 ORDER BY description|;
2420 $self->{all_departments} = selectall_hashref_query($self, $dbh, $query);
2422 delete($self->{all_departments}) unless (@{ $self->{all_departments} || [] });
2424 $main::lxdebug->leave_sub();
2428 $main::lxdebug->enter_sub();
2430 my ($self, $module, $myconfig, $table, $provided_dbh) = @_;
2433 if ($table eq "customer") {
2442 # get last customers or vendors
2443 my ($query, $sth, $ref);
2445 my $dbh = $provided_dbh ? $provided_dbh : $self->get_standard_dbh($myconfig);
2450 my $transdate = "current_date";
2451 if ($self->{transdate}) {
2452 $transdate = $dbh->quote($self->{transdate});
2455 # now get the account numbers
2457 SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk2.tax_id
2459 -- find newest entries in taxkeys
2461 SELECT chart_id, MAX(startdate) AS startdate
2463 WHERE (startdate <= $transdate)
2465 ) tk ON (c.id = tk.chart_id)
2466 -- and load all of those entries
2467 INNER JOIN taxkeys tk2
2468 ON (tk.chart_id = tk2.chart_id AND tk.startdate = tk2.startdate)
2469 WHERE (c.link LIKE ?)
2472 $sth = $dbh->prepare($query);
2474 do_statement($self, $sth, $query, like($module));
2476 $self->{accounts} = "";
2477 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2479 foreach my $key (split(/:/, $ref->{link})) {
2480 if ($key =~ /\Q$module\E/) {
2482 # cross reference for keys
2483 $xkeyref{ $ref->{accno} } = $key;
2485 push @{ $self->{"${module}_links"}{$key} },
2486 { accno => $ref->{accno},
2487 chart_id => $ref->{chart_id},
2488 description => $ref->{description},
2489 taxkey => $ref->{taxkey_id},
2490 tax_id => $ref->{tax_id} };
2492 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2498 # get taxkeys and description
2499 $query = qq|SELECT id, taxkey, taxdescription FROM tax|;
2500 $self->{TAXKEY} = selectall_hashref_query($self, $dbh, $query);
2502 if (($module eq "AP") || ($module eq "AR")) {
2503 # get tax rates and description
2504 $query = qq|SELECT * FROM tax|;
2505 $self->{TAX} = selectall_hashref_query($self, $dbh, $query);
2508 my $extra_columns = '';
2509 $extra_columns .= 'a.direct_debit, ' if ($module eq 'AR') || ($module eq 'AP');
2514 a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid, a.deliverydate,
2515 a.duedate, a.tax_point, a.ordnumber, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.notes,
2517 a.intnotes, a.department_id, a.amount AS oldinvtotal,
2518 a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type,
2519 a.globalproject_id, ${extra_columns}
2521 d.description AS department,
2524 JOIN $table c ON (a.${table}_id = c.id)
2525 LEFT JOIN employee e ON (e.id = a.employee_id)
2526 LEFT JOIN department d ON (d.id = a.department_id)
2528 $ref = selectfirst_hashref_query($self, $dbh, $query, $self->{id});
2530 foreach my $key (keys %$ref) {
2531 $self->{$key} = $ref->{$key};
2533 $self->{mtime} ||= $self->{itime};
2534 $self->{lastmtime} = $self->{mtime};
2535 my $transdate = "current_date";
2536 if ($self->{transdate}) {
2537 $transdate = $dbh->quote($self->{transdate});
2540 # now get the account numbers
2541 $query = qq|SELECT c.accno, c.description, c.link, c.taxkey_id, c.id AS chart_id, tk.tax_id
2543 LEFT JOIN taxkeys tk ON (tk.chart_id = c.id)
2545 AND (tk.id = (SELECT id FROM taxkeys WHERE taxkeys.chart_id = c.id AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1)
2546 OR c.link LIKE '%_tax%' OR c.taxkey_id IS NULL)
2549 $sth = $dbh->prepare($query);
2550 do_statement($self, $sth, $query, like($module));
2552 $self->{accounts} = "";
2553 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
2555 foreach my $key (split(/:/, $ref->{link})) {
2556 if ($key =~ /\Q$module\E/) {
2558 # cross reference for keys
2559 $xkeyref{ $ref->{accno} } = $key;
2561 push @{ $self->{"${module}_links"}{$key} },
2562 { accno => $ref->{accno},
2563 chart_id => $ref->{chart_id},
2564 description => $ref->{description},
2565 taxkey => $ref->{taxkey_id},
2566 tax_id => $ref->{tax_id} };
2568 $self->{accounts} .= "$ref->{accno} " unless $key =~ /tax/;
2574 # get amounts from individual entries
2577 c.accno, c.description,
2578 a.acc_trans_id, a.source, a.amount, a.memo, a.transdate, a.gldate, a.cleared, a.project_id, a.taxkey, a.chart_id,
2582 LEFT JOIN chart c ON (c.id = a.chart_id)
2583 LEFT JOIN project p ON (p.id = a.project_id)
2584 LEFT JOIN tax t ON (t.id= a.tax_id)
2585 WHERE a.trans_id = ?
2586 AND a.fx_transaction = '0'
2587 ORDER BY a.acc_trans_id, a.transdate|;
2588 $sth = $dbh->prepare($query);
2589 do_statement($self, $sth, $query, $self->{id});
2591 # get exchangerate for currency
2592 $self->{exchangerate} =
2593 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2596 # store amounts in {acc_trans}{$key} for multiple accounts
2597 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
2598 $ref->{exchangerate} =
2599 $self->get_exchangerate($dbh, $self->{currency}, $ref->{transdate}, $fld);
2600 if (!($xkeyref{ $ref->{accno} } =~ /tax/)) {
2603 if (($xkeyref{ $ref->{accno} } =~ /paid/) && ($self->{type} eq "credit_note")) {
2604 $ref->{amount} *= -1;
2606 $ref->{index} = $index;
2608 push @{ $self->{acc_trans}{ $xkeyref{ $ref->{accno} } } }, $ref;
2615 d.closedto, d.revtrans,
2616 (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency,
2617 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2618 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
2619 (SELECT c.accno FROM chart c WHERE d.rndgain_accno_id = c.id) AS rndgain_accno,
2620 (SELECT c.accno FROM chart c WHERE d.rndloss_accno_id = c.id) AS rndloss_accno
2622 $ref = selectfirst_hashref_query($self, $dbh, $query);
2623 map { $self->{$_} = $ref->{$_} } keys %$ref;
2630 current_date AS transdate, d.closedto, d.revtrans,
2631 (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency,
2632 (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
2633 (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
2634 (SELECT c.accno FROM chart c WHERE d.rndgain_accno_id = c.id) AS rndgain_accno,
2635 (SELECT c.accno FROM chart c WHERE d.rndloss_accno_id = c.id) AS rndloss_accno
2637 $ref = selectfirst_hashref_query($self, $dbh, $query);
2638 map { $self->{$_} = $ref->{$_} } keys %$ref;
2640 if ($self->{"$self->{vc}_id"}) {
2642 # only setup currency
2643 ($self->{currency}) = $self->{defaultcurrency} if !$self->{currency};
2647 $self->lastname_used($dbh, $myconfig, $table, $module);
2649 # get exchangerate for currency
2650 $self->{exchangerate} =
2651 $self->get_exchangerate($dbh, $self->{currency}, $self->{transdate}, $fld);
2657 $main::lxdebug->leave_sub();
2661 $main::lxdebug->enter_sub();
2663 my ($self, $dbh, $myconfig, $table, $module) = @_;
2667 $table = $table eq "customer" ? "customer" : "vendor";
2668 my %column_map = ("a.${table}_id" => "${table}_id",
2669 "a.department_id" => "department_id",
2670 "d.description" => "department",
2671 "ct.name" => $table,
2672 "cu.name" => "currency",
2675 if ($self->{type} =~ /delivery_order/) {
2676 $arap = 'delivery_orders';
2677 delete $column_map{"cu.currency"};
2679 } elsif ($self->{type} =~ /_order/) {
2681 $where = "quotation = '0'";
2683 } elsif ($self->{type} =~ /_quotation/) {
2685 $where = "quotation = '1'";
2687 } elsif ($table eq 'customer') {
2695 $where = "($where) AND" if ($where);
2696 my $query = qq|SELECT MAX(id) FROM $arap
2697 WHERE $where ${table}_id > 0|;
2698 my ($trans_id) = selectrow_query($self, $dbh, $query);
2701 my $column_spec = join(', ', map { "${_} AS $column_map{$_}" } keys %column_map);
2702 $query = qq|SELECT $column_spec
2704 LEFT JOIN $table ct ON (a.${table}_id = ct.id)
2705 LEFT JOIN department d ON (a.department_id = d.id)
2706 LEFT JOIN currencies cu ON (cu.id=ct.currency_id)
2708 my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id);
2710 map { $self->{$_} = $ref->{$_} } values %column_map;
2712 $main::lxdebug->leave_sub();
2715 sub get_variable_content_types {
2716 my %html_variables = (
2717 longdescription => 'html',
2718 partnotes => 'html',
2720 orignotes => 'html',
2725 header_text => 'html',
2726 footer_text => 'html',
2728 return \%html_variables;
2732 $main::lxdebug->enter_sub();
2735 my $myconfig = shift || \%::myconfig;
2736 my ($thisdate, $days) = @_;
2738 my $dbh = $self->get_standard_dbh($myconfig);
2743 my $dateformat = $myconfig->{dateformat};
2744 $dateformat .= "yy" if $myconfig->{dateformat} !~ /^y/;
2745 $thisdate = $dbh->quote($thisdate);
2746 $query = qq|SELECT to_date($thisdate, '$dateformat') + $days AS thisdate|;
2748 $query = qq|SELECT current_date AS thisdate|;
2751 ($thisdate) = selectrow_query($self, $dbh, $query);
2753 $main::lxdebug->leave_sub();
2759 $main::lxdebug->enter_sub();
2761 my ($self, $flds, $new, $count, $numrows) = @_;
2765 map { push @ndx, { num => $new->[$_ - 1]->{runningnumber}, ndx => $_ } } 1 .. $count;
2770 foreach my $item (sort { $a->{num} <=> $b->{num} } @ndx) {
2772 my $j = $item->{ndx} - 1;
2773 map { $self->{"${_}_$i"} = $new->[$j]->{$_} } @{$flds};
2777 for $i ($count + 1 .. $numrows) {
2778 map { delete $self->{"${_}_$i"} } @{$flds};
2781 $main::lxdebug->leave_sub();
2785 $main::lxdebug->enter_sub();
2787 my ($self, $myconfig) = @_;
2791 SL::DB->client->with_transaction(sub {
2792 my $dbh = SL::DB->client->dbh;
2794 my $query = qq|DELETE FROM status
2795 WHERE (formname = ?) AND (trans_id = ?)|;
2796 my $sth = prepare_query($self, $dbh, $query);
2798 if ($self->{formname} =~ /(check|receipt)/) {
2799 for $i (1 .. $self->{rowcount}) {
2800 do_statement($self, $sth, $query, $self->{formname}, $self->{"id_$i"} * 1);
2803 do_statement($self, $sth, $query, $self->{formname}, $self->{id});
2807 my $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2808 my $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2810 my %queued = split / /, $self->{queued};
2813 if ($self->{formname} =~ /(check|receipt)/) {
2815 # this is a check or receipt, add one entry for each lineitem
2816 my ($accno) = split /--/, $self->{account};
2817 $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname, chart_id)
2818 VALUES (?, ?, ?, ?, (SELECT c.id FROM chart c WHERE c.accno = ?))|;
2819 @values = ($printed, $queued{$self->{formname}}, $self->{prinform}, $accno);
2820 $sth = prepare_query($self, $dbh, $query);
2822 for $i (1 .. $self->{rowcount}) {
2823 if ($self->{"checked_$i"}) {
2824 do_statement($self, $sth, $query, $self->{"id_$i"}, @values);
2830 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2831 VALUES (?, ?, ?, ?, ?)|;
2832 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed,
2833 $queued{$self->{formname}}, $self->{formname});
2836 }) or do { die SL::DB->client->error };
2838 $main::lxdebug->leave_sub();
2842 $main::lxdebug->enter_sub();
2844 my ($self, $dbh) = @_;
2846 my ($query, $printed, $emailed);
2848 my $formnames = $self->{printed};
2849 my $emailforms = $self->{emailed};
2851 $query = qq|DELETE FROM status
2852 WHERE (formname = ?) AND (trans_id = ?)|;
2853 do_query($self, $dbh, $query, $self->{formname}, $self->{id});
2855 # this only applies to the forms
2856 # checks and receipts are posted when printed or queued
2858 if ($self->{queued}) {
2859 my %queued = split / /, $self->{queued};
2861 foreach my $formname (keys %queued) {
2862 $printed = ($self->{printed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2863 $emailed = ($self->{emailed} =~ /\Q$self->{formname}\E/) ? "1" : "0";
2865 $query = qq|INSERT INTO status (trans_id, printed, emailed, spoolfile, formname)
2866 VALUES (?, ?, ?, ?, ?)|;
2867 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $queued{$formname}, $formname);
2869 $formnames =~ s/\Q$self->{formname}\E//;
2870 $emailforms =~ s/\Q$self->{formname}\E//;
2875 # save printed, emailed info
2876 $formnames =~ s/^ +//g;
2877 $emailforms =~ s/^ +//g;
2880 map { $status{$_}{printed} = 1 } split / +/, $formnames;
2881 map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
2883 foreach my $formname (keys %status) {
2884 $printed = ($formnames =~ /\Q$self->{formname}\E/) ? "1" : "0";
2885 $emailed = ($emailforms =~ /\Q$self->{formname}\E/) ? "1" : "0";
2887 $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
2888 VALUES (?, ?, ?, ?)|;
2889 do_query($self, $dbh, $query, $self->{id}, $printed, $emailed, $formname);
2892 $main::lxdebug->leave_sub();
2896 # $main::locale->text('SAVED')
2897 # $main::locale->text('SCREENED')
2898 # $main::locale->text('DELETED')
2899 # $main::locale->text('ADDED')
2900 # $main::locale->text('PAYMENT POSTED')
2901 # $main::locale->text('POSTED')
2902 # $main::locale->text('POSTED AS NEW')
2903 # $main::locale->text('ELSE')
2904 # $main::locale->text('SAVED FOR DUNNING')
2905 # $main::locale->text('DUNNING STARTED')
2906 # $main::locale->text('PREVIEWED')
2907 # $main::locale->text('PRINTED')
2908 # $main::locale->text('MAILED')
2909 # $main::locale->text('SCREENED')
2910 # $main::locale->text('CANCELED')
2911 # $main::locale->text('IMPORT')
2912 # $main::locale->text('UNDO TRANSFER')
2913 # $main::locale->text('UNIMPORT')
2914 # $main::locale->text('invoice')
2915 # $main::locale->text('proforma')
2916 # $main::locale->text('sales_order')
2917 # $main::locale->text('pick_list')
2918 # $main::locale->text('purchase_order')
2919 # $main::locale->text('bin_list')
2920 # $main::locale->text('sales_quotation')
2921 # $main::locale->text('request_quotation')
2924 $main::lxdebug->enter_sub();
2927 my $dbh = shift || SL::DB->client->dbh;
2928 SL::DB->client->with_transaction(sub {
2930 if(!exists $self->{employee_id}) {
2931 &get_employee($self, $dbh);
2935 qq|INSERT INTO history_erp (trans_id, employee_id, addition, what_done, snumbers) | .
2936 qq|VALUES (?, (SELECT id FROM employee WHERE login = ?), ?, ?, ?)|;
2937 my @values = (conv_i($self->{id}), $self->{login},
2938 $self->{addition}, $self->{what_done}, "$self->{snumbers}");
2939 do_query($self, $dbh, $query, @values);
2941 }) or do { die SL::DB->client->error };
2943 $main::lxdebug->leave_sub();
2947 $main::lxdebug->enter_sub();
2949 my ($self, $dbh, $trans_id, $restriction, $order) = @_;
2950 my ($orderBy, $desc) = split(/\-\-/, $order);
2951 $order = " ORDER BY " . ($order eq "" ? " h.itime " : ($desc == 1 ? $orderBy . " DESC " : $orderBy . " "));
2954 if ($trans_id ne "") {
2956 qq|SELECT h.employee_id, h.itime::timestamp(0) AS itime, h.addition, h.what_done, emp.name, h.snumbers, h.trans_id AS id | .
2957 qq|FROM history_erp h | .
2958 qq|LEFT JOIN employee emp ON (emp.id = h.employee_id) | .
2959 qq|WHERE (trans_id = | . $trans_id . qq|) $restriction | .
2962 my $sth = $dbh->prepare($query) || $self->dberror($query);
2964 $sth->execute() || $self->dberror("$query");
2966 while(my $hash_ref = $sth->fetchrow_hashref()) {
2967 $hash_ref->{addition} = $main::locale->text($hash_ref->{addition});
2968 $hash_ref->{what_done} = $main::locale->text($hash_ref->{what_done});
2969 my ( $what, $number ) = split /_/, $hash_ref->{snumbers};
2970 $hash_ref->{snumbers} = $number;
2971 $hash_ref->{haslink} = 'controller.pl?action=EmailJournal/show&id='.$number if $what eq 'emailjournal';
2972 $hash_ref->{snumbers} = $main::locale->text("E-Mail").' '.$number if $what eq 'emailjournal';
2973 $tempArray[$i++] = $hash_ref;
2975 $main::lxdebug->leave_sub() and return \@tempArray
2976 if ($i > 0 && $tempArray[0] ne "");
2978 $main::lxdebug->leave_sub();
2982 sub get_partsgroup {
2983 $main::lxdebug->enter_sub();
2985 my ($self, $myconfig, $p) = @_;
2986 my $target = $p->{target} || 'all_partsgroup';
2988 my $dbh = $self->get_standard_dbh($myconfig);
2990 my $query = qq|SELECT DISTINCT pg.id, pg.partsgroup
2992 JOIN parts p ON (p.partsgroup_id = pg.id) |;
2995 if ($p->{searchitems} eq 'part') {
2996 $query .= qq|WHERE p.part_type = 'part'|;
2998 if ($p->{searchitems} eq 'service') {
2999 $query .= qq|WHERE p.part_type = 'service'|;
3001 if ($p->{searchitems} eq 'assembly') {
3002 $query .= qq|WHERE p.part_type = 'assembly'|;
3005 $query .= qq|ORDER BY partsgroup|;
3008 $query = qq|SELECT id, partsgroup FROM partsgroup
3009 ORDER BY partsgroup|;
3012 if ($p->{language_code}) {
3013 $query = qq|SELECT DISTINCT pg.id, pg.partsgroup,
3014 t.description AS translation
3016 JOIN parts p ON (p.partsgroup_id = pg.id)
3017 LEFT JOIN translation t ON ((t.trans_id = pg.id) AND (t.language_code = ?))
3018 ORDER BY translation|;
3019 @values = ($p->{language_code});
3022 $self->{$target} = selectall_hashref_query($self, $dbh, $query, @values);
3024 $main::lxdebug->leave_sub();
3027 sub get_pricegroup {
3028 $main::lxdebug->enter_sub();
3030 my ($self, $myconfig, $p) = @_;
3032 my $dbh = $self->get_standard_dbh($myconfig);
3034 my $query = qq|SELECT p.id, p.pricegroup
3037 $query .= qq| ORDER BY pricegroup|;
3040 $query = qq|SELECT id, pricegroup FROM pricegroup
3041 ORDER BY pricegroup|;
3044 $self->{all_pricegroup} = selectall_hashref_query($self, $dbh, $query);
3046 $main::lxdebug->leave_sub();
3050 # usage $form->all_years($myconfig, [$dbh])
3051 # return list of all years where bookings found
3054 $main::lxdebug->enter_sub();
3056 my ($self, $myconfig, $dbh) = @_;
3058 $dbh ||= $self->get_standard_dbh($myconfig);
3061 my $query = qq|SELECT (SELECT MIN(transdate) FROM acc_trans),
3062 (SELECT MAX(transdate) FROM acc_trans)|;
3063 my ($startdate, $enddate) = selectrow_query($self, $dbh, $query);
3065 if ($myconfig->{dateformat} =~ /^yy/) {
3066 ($startdate) = split /\W/, $startdate;
3067 ($enddate) = split /\W/, $enddate;
3069 (@_) = split /\W/, $startdate;
3071 (@_) = split /\W/, $enddate;
3076 $startdate = substr($startdate,0,4);
3077 $enddate = substr($enddate,0,4);
3079 while ($enddate >= $startdate) {
3080 push @all_years, $enddate--;
3085 $main::lxdebug->leave_sub();
3089 $main::lxdebug->enter_sub();
3093 map { $self->{_VAR_BACKUP}->{$_} = $self->{$_} if exists $self->{$_} } @vars;
3095 $main::lxdebug->leave_sub();
3099 $main::lxdebug->enter_sub();
3104 map { $self->{$_} = $self->{_VAR_BACKUP}->{$_} if exists $self->{_VAR_BACKUP}->{$_} } @vars;
3106 $main::lxdebug->leave_sub();
3109 sub prepare_for_printing {
3112 my $defaults = SL::DB::Default->get;
3114 $self->{templates} ||= $defaults->templates;
3115 $self->{formname} ||= $self->{type};
3116 $self->{media} ||= 'email';
3118 die "'media' other than 'email', 'file', 'printer' is not supported yet" unless $self->{media} =~ m/^(?:email|file|printer)$/;
3120 # Several fields that used to reside in %::myconfig (stored in
3121 # auth.user_config) are now stored in defaults. Copy them over for
3123 $self->{$_} = $defaults->$_ for qw(company address taxnumber co_ustid duns sepa_creditor_id);
3125 $self->{"myconfig_${_}"} = $::myconfig{$_} for grep { $_ ne 'dbpasswd' } keys %::myconfig;
3127 if (!$self->{employee_id}) {
3128 $self->{"employee_${_}"} = $::myconfig{$_} for qw(email tel fax name signature);
3129 $self->{"employee_${_}"} = $defaults->$_ for qw(address businessnumber co_ustid company duns sepa_creditor_id taxnumber);
3132 my $language = $self->{language} ? '_' . $self->{language} : '';
3134 my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
3135 if ($self->{language_id}) {
3136 ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) = AM->get_language_details(\%::myconfig, $self, $self->{language_id});
3139 $output_dateformat ||= $::myconfig{dateformat};
3140 $output_numberformat ||= $::myconfig{numberformat};
3141 $output_longdates //= 1;
3143 $self->{myconfig_output_dateformat} = $output_dateformat // $::myconfig{dateformat};
3144 $self->{myconfig_output_longdates} = $output_longdates // 1;
3145 $self->{myconfig_output_numberformat} = $output_numberformat // $::myconfig{numberformat};
3147 # Retrieve accounts for tax calculation.
3148 IC->retrieve_accounts(\%::myconfig, $self, map { $_ => $self->{"id_$_"} } 1 .. $self->{rowcount});
3150 if ($self->{type} =~ /_delivery_order$/) {
3151 DO->order_details(\%::myconfig, $self);
3152 } elsif ($self->{type} =~ /sales_order|sales_quotation|request_quotation|purchase_order/) {
3153 OE->order_details(\%::myconfig, $self);
3155 IS->invoice_details(\%::myconfig, $self, $::locale);
3158 $self->set_addition_billing_address_print_variables;
3160 # Chose extension & set source file name
3161 my $extension = 'html';
3162 if ($self->{format} eq 'postscript') {
3163 $self->{postscript} = 1;
3165 } elsif ($self->{"format"} =~ /pdf/) {
3167 $extension = $self->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
3168 } elsif ($self->{"format"} =~ /opendocument/) {
3169 $self->{opendocument} = 1;
3171 } elsif ($self->{"format"} =~ /excel/) {
3176 my $printer_code = $self->{printer_code} ? '_' . $self->{printer_code} : '';
3177 my $email_extension = $self->{media} eq 'email' && -f ($defaults->templates . "/$self->{formname}_email${language}.${extension}") ? '_email' : '';
3178 $self->{IN} = "$self->{formname}${email_extension}${language}${printer_code}.${extension}";
3181 $self->format_dates($output_dateformat, $output_longdates,
3182 qw(invdate orddate quodate pldate duedate reqdate transdate tax_point shippingdate deliverydate validitydate paymentdate datepaid
3183 transdate_oe deliverydate_oe employee_startdate employee_enddate),
3184 grep({ /^(?:datepaid|transdate_oe|reqdate|deliverydate|deliverydate_oe|transdate)_\d+$/ } keys(%{$self})));
3186 $self->reformat_numbers($output_numberformat, 2,
3187 qw(invtotal ordtotal quototal subtotal linetotal listprice sellprice netprice discount tax taxbase total paid),
3188 grep({ /^(?:linetotal|listprice|sellprice|netprice|taxbase|discount|paid|subtotal|total|tax)_\d+$/ } keys(%{$self})));
3190 $self->reformat_numbers($output_numberformat, undef, qw(qty price_factor), grep({ /^qty_\d+$/} keys(%{$self})));
3192 my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
3194 if (scalar @{ $cvar_date_fields }) {
3195 $self->format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
3198 while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
3199 $self->reformat_numbers($output_numberformat, $precision, @{ $field_list });
3203 if (($self->{language} // '') ne '') {
3204 my $template_arrays = $self->{TEMPLATE_ARRAYS} || $self;
3205 for my $idx (0..scalar(@{ $template_arrays->{unit} }) - 1) {
3206 $template_arrays->{unit}->[$idx] = AM->translate_units($self, $self->{language}, $template_arrays->{unit}->[$idx], $template_arrays->{qty}->[$idx])
3210 $self->{template_meta} = {
3211 formname => $self->{formname},
3212 language => SL::DB::Manager::Language->find_by_or_create(id => $self->{language_id} || undef),
3213 format => $self->{format},
3214 media => $self->{media},
3215 extension => $extension,
3216 printer => SL::DB::Manager::Printer->find_by_or_create(id => $self->{printer_id} || undef),
3217 today => DateTime->today,
3220 if ($defaults->print_interpolate_variables_in_positions) {
3221 $self->substitute_placeholders_in_template_arrays({ field => 'description', type => 'text' }, { field => 'longdescription', type => 'html' });
3227 sub set_addition_billing_address_print_variables {
3230 return if !$self->{billing_address_id};
3232 my $address = SL::DB::Manager::AdditionalBillingAddress->find_by(id => $self->{billing_address_id});
3233 return if !$address;
3235 $self->{"billing_address_${_}"} = $address->$_ for map { $_->name } @{ $address->meta->columns };
3238 sub substitute_placeholders_in_template_arrays {
3239 my ($self, @fields) = @_;
3241 foreach my $spec (@fields) {
3242 $spec = { field => $spec, type => 'text' } if !ref($spec);
3243 my $field = $spec->{field};
3245 next unless exists $self->{TEMPLATE_ARRAYS} && exists $self->{TEMPLATE_ARRAYS}->{$field};
3247 my $tag_start = $spec->{type} eq 'html' ? '<%' : '<%';
3248 my $tag_end = $spec->{type} eq 'html' ? '%>' : '%>';
3249 my $formatter = $spec->{type} eq 'html' ? sub { $::locale->quote_special_chars('html', $_[0] // '') } : sub { $_[0] };
3251 $self->{TEMPLATE_ARRAYS}->{$field} = [
3252 apply { s{${tag_start}(.+?)${tag_end}}{ $formatter->($self->{$1}) }eg }
3253 @{ $self->{TEMPLATE_ARRAYS}->{$field} }
3260 sub calculate_arap {
3261 my ($self,$buysell,$taxincluded,$exchangerate,$roundplaces) = @_;
3263 # this function is used to calculate netamount, total_tax and amount for AP and
3264 # AR transactions (Kreditoren-/Debitorenbuchungen) by going over all lines
3266 # Thus it needs a fully prepared $form to work on.
3267 # calculate_arap assumes $form->{amount_$i} entries still need to be parsed
3269 # The calculated total values are all rounded (default is to 2 places) and
3270 # returned as parameters rather than directly modifying form. The aim is to
3271 # make the calculation of AP and AR behave identically. There is a test-case
3272 # for this function in t/form/arap.t
3274 # While calculating the totals $form->{amount_$i} and $form->{tax_$i} are
3275 # modified and formatted and receive the correct sign for writing straight to
3276 # acc_trans, depending on whether they are ar or ap.
3279 die "taxincluded needed in Form->calculate_arap" unless defined $taxincluded;
3280 die "exchangerate needed in Form->calculate_arap" unless defined $exchangerate;
3281 die 'illegal buysell parameter, has to be \"buy\" or \"sell\" in Form->calculate_arap\n' unless $buysell =~ /^(buy|sell)$/;
3282 $roundplaces = 2 unless $roundplaces;
3284 my $sign = 1; # adjust final results for writing amount to acc_trans
3285 $sign = -1 if $buysell eq 'buy';
3287 my ($netamount,$total_tax,$amount);
3291 # parse and round amounts, setting correct sign for writing to acc_trans
3292 for my $i (1 .. $self->{rowcount}) {
3293 $self->{"amount_$i"} = $self->round_amount($self->parse_amount(\%::myconfig, $self->{"amount_$i"}) * $exchangerate * $sign, $roundplaces);
3295 $amount += $self->{"amount_$i"} * $sign;
3298 for my $i (1 .. $self->{rowcount}) {
3299 next unless $self->{"amount_$i"};
3300 ($self->{"tax_id_$i"}) = split /--/, $self->{"taxchart_$i"};
3301 my $tax_id = $self->{"tax_id_$i"};
3303 my $selected_tax = SL::DB::Manager::Tax->find_by(id => "$tax_id");
3305 if ( $selected_tax ) {
3307 if ( $buysell eq 'sell' ) {
3308 $self->{AR_amounts}{"tax_$i"} = $selected_tax->chart->accno if defined $selected_tax->chart;
3310 $self->{AP_amounts}{"tax_$i"} = $selected_tax->chart->accno if defined $selected_tax->chart;
3313 $self->{"taxkey_$i"} = $selected_tax->taxkey;
3314 $self->{"taxrate_$i"} = $selected_tax->rate;
3317 ($self->{"amount_$i"}, $self->{"tax_$i"}) = $self->calculate_tax($self->{"amount_$i"},$self->{"taxrate_$i"},$taxincluded,$roundplaces);
3319 $netamount += $self->{"amount_$i"};
3320 $total_tax += $self->{"tax_$i"};
3323 $amount = $netamount + $total_tax;
3325 # due to $sign amount_$i und tax_$i already have the right sign for acc_trans
3326 # but reverse sign of totals for writing amounts to ar
3327 if ( $buysell eq 'buy' ) {
3333 return($netamount,$total_tax,$amount);
3337 my ($self, $dateformat, $longformat, @indices) = @_;
3339 $dateformat ||= $::myconfig{dateformat};
3341 foreach my $idx (@indices) {
3342 if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
3343 for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
3344 $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $dateformat, $longformat);
3348 next unless defined $self->{$idx};
3350 if (!ref($self->{$idx})) {
3351 $self->{$idx} = $::locale->reformat_date(\%::myconfig, $self->{$idx}, $dateformat, $longformat);
3353 } elsif (ref($self->{$idx}) eq "ARRAY") {
3354 for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
3355 $self->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{$idx}->[$i], $dateformat, $longformat);
3361 sub reformat_numbers {
3362 my ($self, $numberformat, $places, @indices) = @_;
3364 return if !$numberformat || ($numberformat eq $::myconfig{numberformat});
3366 foreach my $idx (@indices) {
3367 if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
3368 for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
3369 $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
3373 next unless defined $self->{$idx};
3375 if (!ref($self->{$idx})) {
3376 $self->{$idx} = $self->parse_amount(\%::myconfig, $self->{$idx});
3378 } elsif (ref($self->{$idx}) eq "ARRAY") {
3379 for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
3380 $self->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{$idx}->[$i]);
3385 my $saved_numberformat = $::myconfig{numberformat};
3386 $::myconfig{numberformat} = $numberformat;
3388 foreach my $idx (@indices) {
3389 if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
3390 for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
3391 $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
3395 next unless defined $self->{$idx};
3397 if (!ref($self->{$idx})) {
3398 $self->{$idx} = $self->format_amount(\%::myconfig, $self->{$idx}, $places);
3400 } elsif (ref($self->{$idx}) eq "ARRAY") {
3401 for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
3402 $self->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{$idx}->[$i], $places);
3407 $::myconfig{numberformat} = $saved_numberformat;
3410 sub create_email_signature {
3412 my $client_signature = $::instance_conf->get_signature;
3413 my $user_signature = $::myconfig{signature};
3416 if ( $client_signature or $user_signature ) {
3417 $signature = "\n\n-- \n";
3418 $signature .= $user_signature . "\n" if $user_signature;
3419 $signature .= $client_signature . "\n" if $client_signature;
3426 # this function calculates the net amount and tax for the lines in ar, ap and
3427 # gl and is used for update as well as post. When used with update the return
3428 # value of amount isn't needed
3430 # calculate_tax should always work with positive values, or rather as the user inputs them
3431 # calculate_tax uses db/perl numberformat, i.e. parsed numbers
3432 # convert to negative numbers (when necessary) only when writing to acc_trans
3433 # the amount from $form for ap/ar/gl is currently always rounded to 2 decimals before it reaches here
3434 # for post_transaction amount already contains exchangerate and correct sign and is rounded
3435 # calculate_tax doesn't (need to) know anything about exchangerate
3437 my ($self,$amount,$taxrate,$taxincluded,$roundplaces) = @_;
3445 # calculate tax (unrounded), subtract from amount, round amount and round tax
3446 $tax = $amount - ($amount / ($taxrate + 1)); # equivalent to: taxrate * amount / (taxrate + 1)
3447 $amount = $self->round_amount($amount - $tax, $roundplaces);
3448 $tax = $self->round_amount($tax, $roundplaces);
3450 $tax = $amount * $taxrate;
3451 $tax = $self->round_amount($tax, $roundplaces);
3454 $tax = 0 unless $tax;
3456 return ($amount,$tax);
3465 SL::Form.pm - main data object.
3469 This is the main data object of kivitendo.
3470 Unfortunately it also acts as a god object for certain data retrieval procedures used in the entry points.
3471 Points of interest for a beginner are:
3473 - $form->error - renders a generic error in html. accepts an error message
3474 - $form->get_standard_dbh - returns a database connection for the
3476 =head1 SPECIAL FUNCTIONS
3478 =head2 C<redirect_header> $url
3480 Generates a HTTP redirection header for the new C<$url>. Constructs an
3481 absolute URL including scheme, host name and port. If C<$url> is a
3482 relative URL then it is considered relative to kivitendo base URL.
3484 This function C<die>s if headers have already been created with
3485 C<$::form-E<gt>header>.
3489 print $::form->redirect_header('oe.pl?action=edit&id=1234');
3490 print $::form->redirect_header('http://www.lx-office.org/');
3494 Generates a general purpose http/html header and includes most of the scripts
3495 and stylesheets needed. Stylesheets can be added with L<use_stylesheet>.
3497 Only one header will be generated. If the method was already called in this
3498 request it will not output anything and return undef. Also if no
3499 HTTP_USER_AGENT is found, no header is generated.
3501 Although header does not accept parameters itself, it will honor special
3502 hashkeys of its Form instance:
3510 If one of these is set, a http-equiv refresh is generated. Missing parameters
3511 default to 3 seconds and the refering url.
3515 Either a scalar or an array ref. Will be inlined into the header. Add
3516 stylesheets with the L<use_stylesheet> function.
3520 If true, a css snippet will be generated that sets the page in landscape mode.
3524 Used to override the default favicon.
3528 A html page title will be generated from this
3530 =item mtime_ischanged
3532 Tries to avoid concurrent write operations to records by checking the database mtime with a fetched one.
3534 Can be used / called with any table, that has itime and mtime attributes.
3535 Valid C<table> names are: oe, ar, ap, delivery_orders, parts.
3536 Can be called wit C<option> mail to generate a different error message.
3538 Returns undef if no save operation has been done yet ($self->{id} not present).
3539 Returns undef if no concurrent write process is detected otherwise a error message.