+sub prepare_for_printing {
+ my ($self) = @_;
+
+ my $defaults = SL::DB::Default->get;
+
+ $self->{templates} ||= $defaults->templates;
+ $self->{formname} ||= $self->{type};
+ $self->{media} ||= 'email';
+
+ die "'media' other than 'email', 'file', 'printer' is not supported yet" unless $self->{media} =~ m/^(?:email|file|printer)$/;
+
+ # Several fields that used to reside in %::myconfig (stored in
+ # auth.user_config) are now stored in defaults. Copy them over for
+ # compatibility.
+ $self->{$_} = $defaults->$_ for qw(company address taxnumber co_ustid duns sepa_creditor_id);
+
+ # set shipto from billto unless set
+ my $has_shipto = any { $self->{"shipto$_"} } qw(name street zipcode city country contact);
+ if (!$has_shipto && ($self->{type} =~ m/^(?:purchase_order|request_quotation)$/)) {
+ $self->{shiptoname} = $defaults->company;
+ $self->{shiptostreet} = $defaults->address;
+ }
+
+ my $language = $self->{language} ? '_' . $self->{language} : '';
+
+ my ($language_tc, $output_numberformat, $output_dateformat, $output_longdates);
+ if ($self->{language_id}) {
+ ($language_tc, $output_numberformat, $output_dateformat, $output_longdates) = AM->get_language_details(\%::myconfig, $self, $self->{language_id});
+ } else {
+ $output_dateformat = $::myconfig{dateformat};
+ $output_numberformat = $::myconfig{numberformat};
+ $output_longdates = 1;
+ }
+
+ # Retrieve accounts for tax calculation.
+ IC->retrieve_accounts(\%::myconfig, $self, map { $_ => $self->{"id_$_"} } 1 .. $self->{rowcount});
+
+ if ($self->{type} =~ /_delivery_order$/) {
+ DO->order_details(\%::myconfig, $self);
+ } elsif ($self->{type} =~ /sales_order|sales_quotation|request_quotation|purchase_order/) {
+ OE->order_details(\%::myconfig, $self);
+ } else {
+ IS->invoice_details(\%::myconfig, $self, $::locale);
+ }
+
+ # Chose extension & set source file name
+ my $extension = 'html';
+ if ($self->{format} eq 'postscript') {
+ $self->{postscript} = 1;
+ $extension = 'tex';
+ } elsif ($self->{"format"} =~ /pdf/) {
+ $self->{pdf} = 1;
+ $extension = $self->{'format'} =~ m/opendocument/i ? 'odt' : 'tex';
+ } elsif ($self->{"format"} =~ /opendocument/) {
+ $self->{opendocument} = 1;
+ $extension = 'odt';
+ } elsif ($self->{"format"} =~ /excel/) {
+ $self->{excel} = 1;
+ $extension = 'xls';
+ }
+
+ my $printer_code = $self->{printer_code} ? '_' . $self->{printer_code} : '';
+ my $email_extension = $self->{media} eq 'email' && -f ($defaults->templates . "/$self->{formname}_email${language}.${extension}") ? '_email' : '';
+ $self->{IN} = "$self->{formname}${email_extension}${language}${printer_code}.${extension}";
+
+ # Format dates.
+ $self->format_dates($output_dateformat, $output_longdates,
+ qw(invdate orddate quodate pldate duedate reqdate transdate shippingdate deliverydate validitydate paymentdate datepaid
+ transdate_oe deliverydate_oe employee_startdate employee_enddate),
+ grep({ /^(?:datepaid|transdate_oe|reqdate|deliverydate|deliverydate_oe|transdate)_\d+$/ } keys(%{$self})));
+
+ $self->reformat_numbers($output_numberformat, 2,
+ qw(invtotal ordtotal quototal subtotal linetotal listprice sellprice netprice discount tax taxbase total paid),
+ grep({ /^(?:linetotal|listprice|sellprice|netprice|taxbase|discount|paid|subtotal|total|tax)_\d+$/ } keys(%{$self})));
+
+ $self->reformat_numbers($output_numberformat, undef, qw(qty price_factor), grep({ /^qty_\d+$/} keys(%{$self})));
+
+ my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
+
+ if (scalar @{ $cvar_date_fields }) {
+ $self->format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
+ }
+
+ while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
+ $self->reformat_numbers($output_numberformat, $precision, @{ $field_list });
+ }
+
+ return $self;
+}
+
+sub format_dates {
+ my ($self, $dateformat, $longformat, @indices) = @_;
+
+ $dateformat ||= $::myconfig{dateformat};
+
+ foreach my $idx (@indices) {
+ if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
+ for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
+ $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $dateformat, $longformat);
+ }
+ }
+
+ next unless defined $self->{$idx};
+
+ if (!ref($self->{$idx})) {
+ $self->{$idx} = $::locale->reformat_date(\%::myconfig, $self->{$idx}, $dateformat, $longformat);
+
+ } elsif (ref($self->{$idx}) eq "ARRAY") {
+ for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
+ $self->{$idx}->[$i] = $::locale->reformat_date(\%::myconfig, $self->{$idx}->[$i], $dateformat, $longformat);
+ }
+ }
+ }
+}
+
+sub reformat_numbers {
+ my ($self, $numberformat, $places, @indices) = @_;
+
+ return if !$numberformat || ($numberformat eq $::myconfig{numberformat});
+
+ foreach my $idx (@indices) {
+ if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
+ for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
+ $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
+ }
+ }
+
+ next unless defined $self->{$idx};
+
+ if (!ref($self->{$idx})) {
+ $self->{$idx} = $self->parse_amount(\%::myconfig, $self->{$idx});
+
+ } elsif (ref($self->{$idx}) eq "ARRAY") {
+ for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
+ $self->{$idx}->[$i] = $self->parse_amount(\%::myconfig, $self->{$idx}->[$i]);
+ }
+ }
+ }
+
+ my $saved_numberformat = $::myconfig{numberformat};
+ $::myconfig{numberformat} = $numberformat;
+
+ foreach my $idx (@indices) {
+ if ($self->{TEMPLATE_ARRAYS} && (ref($self->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
+ for (my $i = 0; $i < scalar(@{ $self->{TEMPLATE_ARRAYS}->{$idx} }); $i++) {
+ $self->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
+ }
+ }
+
+ next unless defined $self->{$idx};
+
+ if (!ref($self->{$idx})) {
+ $self->{$idx} = $self->format_amount(\%::myconfig, $self->{$idx}, $places);
+
+ } elsif (ref($self->{$idx}) eq "ARRAY") {
+ for (my $i = 0; $i < scalar(@{ $self->{$idx} }); $i++) {
+ $self->{$idx}->[$i] = $self->format_amount(\%::myconfig, $self->{$idx}->[$i], $places);
+ }
+ }
+ }
+
+ $::myconfig{numberformat} = $saved_numberformat;
+}
+
+sub layout {
+ my ($self) = @_;
+ $::lxdebug->enter_sub;
+
+ my %style_to_script_map = (
+ v3 => 'v3',
+ neu => 'new',
+ );
+
+ my $menu_script = $style_to_script_map{$::myconfig{menustyle}} || '';
+
+ package main;
+ require "bin/mozilla/menu$menu_script.pl";
+ package Form;
+ require SL::Controller::FrameHeader;
+
+
+ my $layout = SL::Controller::FrameHeader->new->action_header . ::render();
+
+ $::lxdebug->leave_sub;
+ return $layout;
+}
+