Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 5b44ba35

Von Tamino Steinert vor 12 Monaten hinzugefügt

  • ID 5b44ba351678452baf4fc299244c951e3b124c7c
  • Vorgänger 5e0c45f8
  • Nachfolger 8753c9b3

ZUGFeRD: Verschiebe komplettes Parsen aus ap.pl nach S:C:ZUGFeRD

Unterschiede anzeigen:

bin/mozilla/ap.pl
114 114
sub load_zugferd {
115 115
  $::auth->assert('ap_transactions');
116 116

  
117
  my $data;    # buffer for holding file contents
118

  
119
  my $form_defaults = $::form->{form_defaults};
120
  my $file = SL::SessionFile->new($form_defaults->{zugferd_session_file}, mode => '<');
121
  my $file_name = $file->file_name;
122

  
123
  $::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} };
117
  my $file_name = $::form->{zugferd_session_file};
124 118

  
125 119
  # Defaults
126
  $::form->{title}            = "Add";
127
  $::form->{paidaccounts}     = 1;
128

  
129
  $file->open('<');
130
  if ( ! defined($file->fh->read($data, -s $file->fh)) ) {
131
      SL::Helper::Flash::flash_later('error',
132
        t8('Could not open ZUGFeRD file for reading: #1', $!));
133
  } else {
134

  
135
      my %res;          # result data structure returned by SL::ZUGFeRD->extract_from_{pdf,xml}()
136
      my $parser;       # SL::XMLInvoice object created by SL::ZUGFeRD->extract_from_{pdf,xml}()
137
      my $template_ap;  # SL::DB::RecordTemplate object
138
      my $vendor;       # SL::DB::Vendor object
139
      my %metadata;     # structured data extracted from XML payload
140
      my @items;        # list of invoice items
141
      my $default_ap_amount_chart;
142

  
143
      if ( $data =~ m/^%PDF/ ) {
144
        %res = %{SL::ZUGFeRD->extract_from_pdf($data)};
145
      } else {
146
        %res = %{SL::ZUGFeRD->extract_from_xml($data)};
147
      }
148

  
149

  
150
      $parser = $res{'invoice_xml'};
151
      %metadata = %{$parser->metadata};
152
      @items = @{$parser->items};
153

  
154
      $default_ap_amount_chart = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_expense_accno_id);
155

  
156
      # Fallback if there's no default AP amount chart configured
157
      unless ( $default_ap_amount_chart ) {
158
        $default_ap_amount_chart = SL::DB::Manager::Chart->find_by(charttype => 'A');
159
      }
160

  
161
      my $row = 0;
162
      foreach my $i (@items) {
163
        $row++;
164

  
165
        my %item = %{$i};
120
  $::form->{title}      ||= "Add";
121
  $::form->{paidaccounts} = 1 if undef $::form->{paidaccounts};
166 122

  
167
        my $net_total = $::form->format_amount(\%::myconfig, $item{'subtotal'}, 2);
168
        my $desc = $item{'description'};
169
        my $tax_rate = $item{'tax_rate'} / 100; # XML data is usually in percent
170

  
171
        my $active_taxkey = $default_ap_amount_chart->taxkey_id;
172
        my $taxes         = SL::DB::Manager::Tax->get_all(
173
          where   => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' }],
174
          sort_by => 'taxkey, rate',
175
        );
176

  
177
        my $tax   = first { $tax_rate          == $_->rate } @{ $taxes };
178
        $tax    //= first { $active_taxkey->tax_id == $_->id } @{ $taxes };
179
        $tax    //= $taxes->[0];
180

  
181
        # If we really can't find any tax definition (a simple rounding error may
182
        # be sufficient for that to happen), grab the first tax fitting the default
183
        # AP amount chart, just like the AP form would do it for manual entry.
184
        if ( scalar @{$taxes} == 0 ) {
185
          $taxes = SL::DB::Manager::Tax->get_all(
186
            where   => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' } ],
187
          );
188
        }
189

  
190
        if (!$tax) {
191
          $row--;
192
          next;
193
        }
194

  
195
        $::form->{"AP_amount_chart_id_${row}"}          = $default_ap_amount_chart->id;
196
        $::form->{"previous_AP_amount_chart_id_${row}"} = $default_ap_amount_chart->id;
197
        $::form->{"amount_${row}"}                      = $net_total;
198
        $::form->{"taxchart_${row}"}                    = $tax->id . '--' . $tax->rate;
199
      }
200

  
201
      flash('info', $::locale->text("The ZUGFeRD/Factur-X invoice '#1' has been loaded.", $file_name));
202

  
203
      $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token;
204
      $::form->{rowcount}         = $row;
205

  
206
      update(
207
        keep_rows_without_amount => 1,
208
        dont_add_new_row         => 1,
209
      );
210
    }
123
  $::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token;
124
  flash('info', $::locale->text(
125
      "The ZUGFeRD/Factur-X invoice '#1' has been loaded.", $file_name));
126
  update(
127
    keep_rows_without_amount => 1,
128
    dont_add_new_row         => 1,
129
  );
211 130
}
212 131

  
213 132
sub load_record_template {

Auch abrufbar als: Unified diff