Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 02174eb6

Von Tamino Steinert vor 10 Monaten hinzugefügt

  • ID 02174eb611a675e2d1029ca80c8b0fa4143ef49d
  • Vorgänger d453ab54
  • Nachfolger 37383722

EmailJournal: ZUGFeRD-Emailanhänge in Kreditorenbuchen konvertieren

Unterschiede anzeigen:

SL/DB/EmailJournal.pm
2 2

  
3 3
use strict;
4 4

  
5
use SL::Webdav;
6
use SL::File;
7

  
5 8
use SL::DB::MetaSetup::EmailJournal;
6 9
use SL::DB::Manager::EmailJournal;
7 10
use SL::DB::Helper::AttrSorted;
11
use SL::DB::Helper::LinkedRecords;
8 12

  
9 13
__PACKAGE__->meta->add_relationship(
10 14
  attachments  => {
......
29 33
  return $result || ($self->id <=> $other->id);
30 34
}
31 35

  
36
sub process_attachments_as_purchase_invoices {
37
  my ($self) = @_;
38

  
39
  my $attachments = $self->attachments_sorted;
40
  foreach my $attachment (@$attachments) {
41
    my $ap_invoice = $attachment->create_ap_invoice();
42
    next unless $ap_invoice;
43

  
44
    # link to email journal
45
    $self->link_to_record($ap_invoice);
46

  
47
    # copy file to webdav folder
48
    if ($::instance_conf->get_webdav_documents) {
49
      my $webdav = SL::Webdav->new(
50
        type     => 'accounts_payable',
51
        number   => $ap_invoice->invnumber,
52
      );
53
      my $webdav_file = SL::Webdav::File->new(
54
        webdav => $webdav,
55
        filename => $attachment->name,
56
      );
57
      eval {
58
        $webdav_file->store(data => \$attachment->content);
59
        1;
60
      } or do {
61
        die 'Storing the ZUGFeRD file to the WebDAV folder failed: ' . $@;
62
      };
63
    }
64
    # copy file to doc storage
65
    if ($::instance_conf->get_doc_storage) {
66
      eval {
67
        SL::File->save(
68
          object_id     => $ap_invoice->id,
69
          object_type   => 'purchase_invoice',
70
          mime_type     => 'application/pdf',
71
          source        => 'uploaded',
72
          file_type     => 'document',
73
          file_name     => $attachment->name,
74
          file_contents => $attachment->content,
75
        );
76
        1;
77
      } or do {
78
        die 'Storing the ZUGFeRD file in the storage backend failed: ' . $@;
79
      };
80
    }
81
  }
82

  
83
  my $new_ext_status = join('_', $self->extended_status, 'processed');
84
  $self->update({ extended_status => $new_ext_status});
85
}
86

  
32 87
1;
33 88

  
34 89
__END__
SL/DB/EmailJournalAttachment.pm
2 2

  
3 3
use strict;
4 4

  
5
use XML::LibXML;
6

  
7
use SL::ZUGFeRD;
8

  
9
use SL::DB::PurchaseInvoice;
5 10
use SL::DB::MetaSetup::EmailJournalAttachment;
6 11
use SL::DB::Manager::EmailJournalAttachment;
7 12
use SL::DB::Helper::ActsAsList (group_by => [ qw(email_journal_id) ]);
8 13

  
9 14
__PACKAGE__->meta->initialize;
10 15

  
16
sub create_ap_invoice {
17
  my ($self) = @_;
18

  
19
  my $content = $self->content; # scalar ref
20

  
21
  return unless $content =~ m/^%PDF/;
22

  
23
  my $zugferd_info = SL::ZUGFeRD->extract_from_pdf($content);
24
  return unless $zugferd_info->{result} == SL::ZUGFeRD::RES_OK();
25

  
26
  my $zugferd_xml = XML::LibXML->load_xml(string => $zugferd_info->{invoice_xml});
27

  
28
  return SL::DB::PurchaseInvoice->create_from_zugferd_xml($zugferd_xml)->save();
29
}
30

  
11 31
1;

Auch abrufbar als: Unified diff