X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/c9d5cc7f38af8db81fcfd11fac6cda62935d41c3..12eafc00a9cbd2c24fbbd6a7415e5ecb97ef02b7:/SL/Controller/Order.pm diff --git a/SL/Controller/Order.pm b/SL/Controller/Order.pm index 9d9624d1e..b00b089ad 100644 --- a/SL/Controller/Order.pm +++ b/SL/Controller/Order.pm @@ -6,6 +6,9 @@ use parent qw(SL::Controller::Base); use SL::Helper::Flash; use SL::ClientJS; use SL::Presenter; +use SL::Locale::String; +use SL::SessionFile::Random; +use SL::Form; use SL::DB::Order; use SL::DB::Customer; @@ -17,9 +20,11 @@ use SL::DB::Default; use SL::DB::Unit; use SL::Helper::DateTime; +use SL::Helper::CreatePDF qw(:all); use List::Util qw(max first); use List::MoreUtils qw(none pairwise); +use English qw(-no_match_vars); use Rose::Object::MakeMethods::Generic ( @@ -31,7 +36,7 @@ use Rose::Object::MakeMethods::Generic __PACKAGE__->run_before('_check_auth'); __PACKAGE__->run_before('_recalc', - only => [ qw(edit update save) ]); + only => [ qw(edit update save create_pdf) ]); # @@ -99,6 +104,62 @@ sub action_save { $self->redirect_to(@redirect_params); } +sub action_create_pdf { + my ($self) = @_; + + my $print_form = Form->new(''); + $print_form->{type} = 'sales_order'; + $print_form->{formname} = 'sales_order', + $print_form->{format} = 'pdf', + $print_form->{media} = 'file'; + + $self->order->flatten_to_form($print_form, format_amounts => 1); + + my $pdf; + my @errors; + $print_form->throw_on_error(sub { + eval { + $print_form->prepare_for_printing; + + $pdf = SL::Helper::CreatePDF->create_pdf( + template => SL::Helper::CreatePDF->find_template(name => $print_form->{formname}), + variables => $print_form, + variable_content_types => { + longdescription => 'html', + partnotes => 'html', + notes => 'html', + }, + ); + 1; + } || push @errors, ref($EVAL_ERROR) eq 'SL::X::FormError' ? $EVAL_ERROR->getMessage : $EVAL_ERROR; + }); + + if (scalar @errors) { + return $self->js->flash('error', t8('Conversion to PDF failed: #1', $errors[0]))->render($self); + } + + my $sfile = SL::SessionFile::Random->new(mode => "w"); + $sfile->fh->print($pdf); + $sfile->fh->close; + + my $tmp_filename = $sfile->file_name; + my $pdf_filename = t8('Sales Order') . '_' . $self->order->ordnumber . '.pdf'; + + $self->js + ->run('download_pdf', $tmp_filename, $pdf_filename) + ->flash('info', t8('The PDF has been created'))->render($self); +} + +sub action_download_pdf { + my ($self) = @_; + + return $self->send_file( + $::form->{tmp_filename}, + type => 'application/pdf', + name => $::form->{pdf_filename}, + ); +} + sub action_customer_vendor_changed { my ($self) = @_; @@ -203,6 +264,18 @@ sub _js_redisplay_linetotals { sub _js_redisplay_amounts_and_taxes { my ($self) = @_; + if (scalar @{ $self->{taxes} }) { + $self->js->show('#taxincluded_row_id'); + } else { + $self->js->hide('#taxincluded_row_id'); + } + + if ($self->order->taxincluded) { + $self->js->hide('#subtotal_row_id'); + } else { + $self->js->show('#subtotal_row_id'); + } + $self->js ->html('#netamount_id', $::form->format_amount(\%::myconfig, $self->order->netamount, -2)) ->html('#amount_id', $::form->format_amount(\%::myconfig, $self->order->amount, -2)) @@ -290,7 +363,7 @@ sub build_tax_rows { my $rows_as_html; foreach my $tax (@{ $self->{taxes} }) { - $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax); + $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax, TAXINCLUDED => $self->order->taxincluded); } return $rows_as_html; } @@ -319,11 +392,13 @@ sub _recalc { $self->order->currency_id($::instance_conf->get_currency_id()); my %pat = $self->order->calculate_prices_and_taxes(); - foreach my $tax_chart_id (keys %{ $pat{taxes} }) { my $tax = SL::DB::Manager::Tax->find_by(chart_id => $tax_chart_id); - push(@{ $self->{taxes} }, { amount => $pat{taxes}->{$tax_chart_id}, - tax => $tax }); + + my @amount_keys = grep { $pat{amounts}->{$_}->{tax_id} == $tax->id } keys %{ $pat{amounts} }; + push(@{ $self->{taxes} }, { amount => $pat{taxes}->{$tax_chart_id}, + netamount => $pat{amounts}->{$amount_keys[0]}->{amount}, + tax => $tax }); } pairwise { $a->{linetotal} = $b->{linetotal} } @{$self->order->items}, @{$pat{items}}; @@ -356,6 +431,9 @@ sub _pre_render { $self->{all_employees} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->employee_id, deleted => 0 ] ], sort_by => 'name'); + $self->{all_salesmen} = SL::DB::Manager::Employee->get_all(where => [ or => [ id => $self->order->salesman_id, + deleted => 0 ] ], + sort_by => 'name'); $self->{all_projects} = SL::DB::Manager::Project->get_all(where => [ or => [ id => $self->order->globalproject_id, active => 1 ] ], sort_by => 'projectnumber'); @@ -363,6 +441,8 @@ sub _pre_render { $self->{all_delivery_terms} = SL::DB::Manager::DeliveryTerm->get_all_sorted(); $self->{current_employee_id} = SL::DB::Manager::Employee->current->id; + + $::request->{layout}->use_javascript("${_}.js") for qw(ckeditor/ckeditor ckeditor/adapters/jquery); } sub _sales_order_type {