X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FMassInvoiceCreatePrint.pm;h=626a6633dc4c685e2817969864a88a0ef4f54585;hb=c11697d16352fc16dc0f70d7f555877d4936cd8c;hp=54313a6a24506456dca7a8eed9dba83c8fd27f32;hpb=31f989254ce9f2ab881af7aee5d07dd63df0e004;p=kivitendo-erp.git diff --git a/SL/Controller/MassInvoiceCreatePrint.pm b/SL/Controller/MassInvoiceCreatePrint.pm index 54313a6a2..626a6633d 100644 --- a/SL/Controller/MassInvoiceCreatePrint.pm +++ b/SL/Controller/MassInvoiceCreatePrint.pm @@ -15,13 +15,15 @@ use SL::DB::Order; use SL::DB::Printer; use SL::Helper::MassPrintCreatePDF qw(:all); use SL::Helper::CreatePDF qw(:all); +use SL::Helper::File qw(store_pdf append_general_pdf_attachments doc_storage_enabled); use SL::Helper::Flash; use SL::Locale::String; use SL::SessionFile; +use SL::ARAP; use SL::System::TaskServer; use Rose::Object::MakeMethods::Generic ( - 'scalar --get_set_init' => [ qw(invoice_models invoice_ids sales_delivery_order_models printers default_printer_id today) ], + 'scalar --get_set_init' => [ qw(invoice_models invoice_ids sales_delivery_order_models printers default_printer_id today all_businesses) ], ); __PACKAGE__->run_before('setup'); @@ -56,14 +58,30 @@ sub action_create_invoices { } my $db = SL::DB::Invoice->new->db; + my $dbh = $db->dbh; my @invoices; + my @already_closed_delivery_orders; if (!$db->with_transaction(sub { foreach my $id (@sales_delivery_order_ids) { my $delivery_order = SL::DB::DeliveryOrder->new(id => $id)->load; - my $invoice = $delivery_order->convert_to_invoice() || die $db->error; - push @invoices, $invoice; + # Only process open delivery orders. In this list should only be open + # delivery orders, but if the user clicked browser back, a new creation + # of invoices for delivery orders which are closed now can be triggered. + # Prevent this. + if ($delivery_order->closed) { + push @already_closed_delivery_orders, $delivery_order; + + } else { + my $invoice = $delivery_order->convert_to_invoice() || die $db->error; + + ARAP->close_orders_if_billed('dbh' => $dbh, + 'arap_id' => $invoice->id, + 'table' => 'ar',); + + push @invoices, $invoice; + } } 1; @@ -72,10 +90,33 @@ sub action_create_invoices { $::form->error($db->error); } + foreach my $invoice( @invoices ) { + # update shop status + my @linked_shop_orders = $invoice->linked_records( + from => 'ShopOrder', + via => [ 'DeliveryOrder', 'Order' ], + ); + #if (scalar @linked_shop_orders[0][0] >= 1){ + #do update + my $shop_order = $linked_shop_orders[0][0]; + if ($shop_order){ + require SL::Shop; + my $shop_config = SL::DB::Manager::Shop->get_first( query => [ id => $shop_order->shop_id ] ); + my $shop = SL::Shop->new( config => $shop_config ); + $shop->connector->set_orderstatus($shop_order->shop_trans_id, "completed"); + } + } + my $key = sprintf('%d-%d', Time::HiRes::gettimeofday()); $::auth->set_session_value("MassInvoiceCreatePrint::ids-${key}" => [ map { $_->id } @invoices ]); - flash_later('info', t8('The invoices have been created. They\'re pre-selected below.')); + if (@already_closed_delivery_orders) { + my $dos_list = join ' ', map { $_->donumber } @already_closed_delivery_orders; + flash_later('error', t8('The following delivery orders could not be processed because they are already closed: #1', $dos_list)); + } + + flash_later('info', t8('The invoices have been created. They\'re pre-selected below.')) if @invoices; + $self->redirect_to(action => 'list_invoices', ids => $key); } @@ -88,6 +129,11 @@ sub action_list_invoices { if ($::form->{ids}) { my $key = 'MassInvoiceCreatePrint::ids-' . $::form->{ids}; $self->invoice_ids($::auth->get_session_value($key) || []); + + # Prevent models->get to retrieve any invoices if session key is there + # but no ids are given. + $self->invoice_ids([0]) if !@{$self->invoice_ids}; + $self->invoice_models->add_additional_url_params(ids => $::form->{ids}); } @@ -112,7 +158,7 @@ sub action_print { return $self->redirect_to(action => 'list_invoices'); } - $self->download_or_print_documents(printer_id => $::form->{printer_id}, invoices => \@invoices); + $self->download_or_print_documents(printer_id => $::form->{printer_id}, invoices => \@invoices, bothsided => $::form->{bothsided}); } sub action_create_print_all_start { @@ -213,7 +259,7 @@ sub _init_sales_delivery_order_models { }, customer => t8('Customer'), employee => t8('Employee'), - transdate => t8('Date'), + transdate => t8('Delivery Order Date'), donumber => t8('Delivery Order Number'), ordnumber => t8('Order Number'), }, @@ -261,6 +307,10 @@ sub init_default_printer_id { return $pr ? $pr->id : undef; } +sub init_all_businesses { + return SL::DB::Manager::Business->get_all_sorted; +} + sub setup { my ($self) = @_; $::auth->assert('invoice_edit'); @@ -290,7 +340,7 @@ sub download_or_print_documents { }); @pdf_file_names = $self->create_pdfs(%pdf_params); - my $merged_pdf = $self->merge_pdfs(file_names => \@pdf_file_names); + my $merged_pdf = $self->merge_pdfs(file_names => \@pdf_file_names, bothsided => $params{bothsided}); unlink @pdf_file_names; if (!$params{printer_id}) { @@ -324,8 +374,8 @@ sub make_filter_summary { my @filters = ( [ $filter->{customer}{"name:substr::ilike"}, t8('Customer') ], - [ $filter->{"transdate:date::ge"}, t8('Transdate') . " " . t8('From Date') ], - [ $filter->{"transdate:date::le"}, t8('Transdate') . " " . t8('To Date') ], + [ $filter->{"transdate:date::ge"}, t8('Delivery Order Date') . " " . t8('From Date') ], + [ $filter->{"transdate:date::le"}, t8('Delivery Order Date') . " " . t8('To Date') ], ); for (@filters) { @@ -341,14 +391,10 @@ sub setup_list_invoices_action_bar { for my $bar ($::request->layout->get('actionbar')) { $bar->add( action => [ - t8('Search'), + t8('Update'), submit => [ '#search_form', { action => 'MassInvoiceCreatePrint/list_invoices' } ], accesskey => 'enter', ], - action => [ - t8('Reset'), - call => [ 'kivi.call_jquery', '#search_form', 'resetForm' ], - ], action => [ $::locale->text('Print'), call => [ 'kivi.MassInvoiceCreatePrint.showMassPrintOptionsOrDownloadDirectly' ], @@ -364,14 +410,10 @@ sub setup_list_sales_delivery_orders_action_bar { for my $bar ($::request->layout->get('actionbar')) { $bar->add( action => [ - t8('Search'), + $params{show_creation_buttons} ? t8('Update') : t8('Search'), submit => [ '#search_form', { action => 'MassInvoiceCreatePrint/list_sales_delivery_orders' } ], accesskey => 'enter', ], - action => [ - t8('Reset'), - call => [ 'kivi.call_jquery', '#search_form', 'resetForm' ], - ], combobox => [ action => [ @@ -379,17 +421,17 @@ sub setup_list_sales_delivery_orders_action_bar { tooltip => t8("Create and print invoices") ], action => [ - t8('for selected entries'), - call => [ 'kivi.MassInvoiceCreatePrint.submitMassCreationForm' ], - tooltip => t8("Create and print invoices for all selected delivery orders"), - disabled => !$params{num_rows} ? $::locale->text('The report doesn\'t contain entries.') : undef, - only_if => $params{show_creation_buttons}, + t8("Create and print invoices for all selected delivery orders"), + submit => [ 'form', { action => 'MassInvoiceCreatePrint/create_invoices' } ], + disabled => !$params{num_rows} ? $::locale->text('The report doesn\'t contain entries.') : undef, + only_if => $params{show_creation_buttons}, + checks => [ 'kivi.MassInvoiceCreatePrint.checkDeliveryOrderSelection' ], + only_once => 1, ], action => [ - t8('for all entries'), + t8("Create and print invoices for all delivery orders matching the filter"), call => [ 'kivi.MassInvoiceCreatePrint.createPrintAllInitialize' ], - tooltip => t8("Create and print invoices for all delivery orders matching the filter"), disabled => !$params{num_rows} ? $::locale->text('The report doesn\'t contain entries.') : undef, only_if => $params{show_creation_buttons}, ],