X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FBackgroundJob%2FMassRecordCreationAndPrinting.pm;h=4cc2519d50a32d489e8232eb3cbe7b0534265b9e;hb=9447acbeb7b1cd0bb4691aa1da8f533ca9099064;hp=7c6f558abb354e354534f1485e37accedaa0433a;hpb=0878ce8165e72571703d06999a9972be4c5adb92;p=kivitendo-erp.git diff --git a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm index 7c6f558ab..4cc2519d5 100644 --- a/SL/BackgroundJob/MassRecordCreationAndPrinting.pm +++ b/SL/BackgroundJob/MassRecordCreationAndPrinting.pm @@ -11,13 +11,17 @@ use SL::DB::Invoice; use SL::DB::Printer; use SL::SessionFile; use SL::Template; +use SL::ARAP; use SL::Locale::String qw(t8); -use SL::Webdav; +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 constant WAITING_FOR_EXECUTION => 0; use constant CONVERTING_DELIVERY_ORDERS => 1; use constant PRINTING_INVOICES => 2; use constant DONE => 3; + # Data format: # my $data = { # record_ids => [ 123, 124, 127, ], @@ -30,6 +34,7 @@ use constant DONE => 3; # conversion_errors => [ { id => 124, number => 'A981723', message => "Stuff went boom" }, ], # print_errors => [ { id => 234, number => 'L87123123', message => "Printer is out of coffee" }, ], # pdf_file_name => 'qweqwe.pdf', +# session_id => $::auth->get_session_id, # }; sub create_invoices { @@ -37,6 +42,7 @@ sub create_invoices { my $job_obj = $self->{job_obj}; my $db = $job_obj->db; + my $dbh = $db->dbh; $job_obj->set_data(status => CONVERTING_DELIVERY_ORDERS())->save; @@ -45,16 +51,30 @@ sub create_invoices { my $data = $job_obj->data_as_hash; eval { - my $invoice; my $sales_delivery_order = SL::DB::DeliveryOrder->new(id => $delivery_order_id)->load; $number = $sales_delivery_order->donumber; + my %conversion_params = $data->{transdate} ? ('attributes' => { transdate => $data->{transdate} }) : (); + my $invoice = $sales_delivery_order->convert_to_invoice(%conversion_params); + + die $db->error if !$invoice; - if (!$db->do_transaction(sub { - $invoice = $sales_delivery_order->convert_to_invoice(sub { $data->{transdate} ? ('attributes' => { transdate => $data->{transdate} }) : - undef }->() ) || die $db->error; - 1; - })) { - die $db->error; + ARAP->close_orders_if_billed('dbh' => $dbh, + 'arap_id' => $invoice->id, + 'table' => 'ar',); + + # 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"); } $data->{num_created}++; @@ -79,123 +99,50 @@ sub convert_invoices_to_pdf { my $db = $job_obj->db; $job_obj->set_data(status => PRINTING_INVOICES())->save; + my $data = $job_obj->data_as_hash; - require SL::Controller::MassInvoiceCreatePrint; - - my $printer_id = $job_obj->data_as_hash->{printer_id}; - my $ctrl = SL::Controller::MassInvoiceCreatePrint->new; + my $printer_id = $data->{printer_id}; + if ( $data->{media} ne 'printer' ) { + undef $printer_id; + $data->{media} = 'file'; + } my %variables = ( type => 'invoice', formname => 'invoice', format => 'pdf', media => $printer_id ? 'printer' : 'file', + printer_id => $printer_id, ); my @pdf_file_names; foreach my $invoice (@{ $self->{invoices} }) { - my $data = $job_obj->data_as_hash; eval { - my %create_params = ( - template => $ctrl->find_template(name => 'invoice', printer_id => $printer_id), - variables => Form->new(''), + my @errors = (); + my %params = ( + variables => \%variables, return => 'file_name', - variable_content_types => { longdescription => 'html', - partnotes => 'html', - notes => 'html',} + document => $invoice, + errors => \@errors, ); - - - - $create_params{variables}->{$_} = $variables{$_} for keys %variables; - - $invoice->flatten_to_form($create_params{variables}, format_amounts => 1); - $create_params{variables}->prepare_for_printing; - - push @pdf_file_names, $ctrl->create_pdf(%create_params); - - # copy file to webdav folder - if ($::instance_conf->get_webdav_documents) { - my $webdav = SL::Webdav->new( - type => 'invoice', - number => $invoice->invnumber, - ); - my $webdav_file = SL::Webdav::File->new( - webdav => $webdav, - filename => t8('Invoice') . '_' . $invoice->invnumber . '.pdf', - ); - eval { - $webdav_file->store(file => $pdf_file_names[-1]); - 1; - } or do { - push @{ $data->{print_errors} }, { id => $invoice->id, number => $invoice->invnumber, message => $@ }; - } - } - + push @pdf_file_names, $self->create_massprint_pdf(%params); $data->{num_printed}++; - 1; - - } or do { - push @{ $data->{print_errors} }, { id => $invoice->id, number => $invoice->invnumber, message => $@ }; - }; - - $job_obj->update_attributes(data_as_hash => $data); - } - - if (@pdf_file_names) { - my $data = $job_obj->data_as_hash; - - eval { - $self->{merged_pdf} = $ctrl->merge_pdfs(file_names => \@pdf_file_names); - unlink @pdf_file_names; - - if (!$printer_id) { - my $file_name = 'mass_invoice' . $job_obj->id . '.pdf'; - my $sfile = SL::SessionFile->new($file_name, mode => 'w'); - $sfile->fh->print($self->{merged_pdf}); - $sfile->fh->close; - - $data->{pdf_file_name} = $file_name; + if (scalar @errors) { + push @{ $data->{print_errors} }, { id => $invoice->id, number => $invoice->invnumber, message => join(', ', @errors) }; } 1; } or do { - push @{ $data->{print_errors} }, { message => $@ }; + push @{ $data->{print_errors} }, { id => $invoice->id, number => $invoice->invnumber, message => $@ }; }; $job_obj->update_attributes(data_as_hash => $data); } -} - -sub print_pdfs { - my ($self) = @_; - - my $job_obj = $self->{job_obj}; - my $data = $job_obj->data_as_hash; - my $printer_id = $data->{printer_id}; - my $copy_printer_id = $data->{copy_printer_id}; - - return if !$printer_id; - - my $out; - - foreach my $local_printer_id ($printer_id, $copy_printer_id) { - next unless $local_printer_id; - my $printer = SL::DB::Printer->new(id => $local_printer_id)->load; - my $command = SL::Template::create(type => 'ShellCommand', form => Form->new(''))->parse($printer->printer_command); - if (!open $out, '|-', $command) { - push @{ $data->{print_errors} }, { message => $::locale->text('Could not execute printer command: #1', $!) }; - $job_obj->update_attributes(data_as_hash => $data); - return; - } - binmode $out; - print $out $self->{merged_pdf}; - close $out; - } + $self->merge_massprint_pdf(file_names => \@pdf_file_names, type => 'invoice' ) if scalar(@pdf_file_names) > 0; } sub run {