Revision 3818b392
Von Tamino Steinert vor 12 Monaten hinzugefügt
bin/mozilla/ap.pl | ||
---|---|---|
113 | 113 |
sub load_zugferd { |
114 | 114 |
$::auth->assert('ap_transactions'); |
115 | 115 |
|
116 |
my $data; # buffer for holding file contents |
|
117 |
|
|
118 |
my $form_defaults = $::form->{form_defaults}; |
|
119 |
my $file = SL::SessionFile->new($form_defaults->{zugferd_session_file}, mode => '<'); |
|
120 |
my $file_name = $file->file_name; |
|
121 |
|
|
122 |
$::form->{$_} = $form_defaults->{$_} for keys %{ $form_defaults // {} }; |
|
116 |
my $file_name = $::form->{zugferd_session_file}; |
|
123 | 117 |
|
124 | 118 |
# Defaults |
125 |
$::form->{title} = "Add"; |
|
126 |
$::form->{paidaccounts} = 1; |
|
127 |
|
|
128 |
$file->open('<'); |
|
129 |
if ( ! defined($file->fh->read($data, -s $file->fh)) ) { |
|
130 |
SL::Helper::Flash::flash_later('error', |
|
131 |
t8('Could not open ZUGFeRD file for reading: #1', $!)); |
|
132 |
} else { |
|
133 |
|
|
134 |
my %res; # result data structure returned by SL::ZUGFeRD->extract_from_{pdf,xml}() |
|
135 |
my $parser; # SL::XMLInvoice object created by SL::ZUGFeRD->extract_from_{pdf,xml}() |
|
136 |
my $template_ap; # SL::DB::RecordTemplate object |
|
137 |
my $vendor; # SL::DB::Vendor object |
|
138 |
my %metadata; # structured data extracted from XML payload |
|
139 |
my @items; # list of invoice items |
|
140 |
my $default_ap_amount_chart; |
|
141 |
|
|
142 |
if ( $data =~ m/^%PDF/ ) { |
|
143 |
%res = %{SL::ZUGFeRD->extract_from_pdf($data)}; |
|
144 |
} else { |
|
145 |
%res = %{SL::ZUGFeRD->extract_from_xml($data)}; |
|
146 |
} |
|
147 |
|
|
148 |
|
|
149 |
$parser = $res{'invoice_xml'}; |
|
150 |
%metadata = %{$parser->metadata}; |
|
151 |
@items = @{$parser->items}; |
|
152 |
|
|
153 |
$default_ap_amount_chart = SL::DB::Manager::Chart->find_by(id => $::instance_conf->get_expense_accno_id); |
|
154 |
|
|
155 |
# Fallback if there's no default AP amount chart configured |
|
156 |
unless ( $default_ap_amount_chart ) { |
|
157 |
$default_ap_amount_chart = SL::DB::Manager::Chart->find_by(charttype => 'A'); |
|
158 |
} |
|
159 |
|
|
160 |
my $row = 0; |
|
161 |
foreach my $i (@items) { |
|
162 |
$row++; |
|
163 |
|
|
164 |
my %item = %{$i}; |
|
119 |
$::form->{title} ||= "Add"; |
|
120 |
$::form->{paidaccounts} = 1 if undef $::form->{paidaccounts}; |
|
165 | 121 |
|
166 |
my $net_total = $::form->format_amount(\%::myconfig, $item{'subtotal'}, 2); |
|
167 |
my $desc = $item{'description'}; |
|
168 |
my $tax_rate = $item{'tax_rate'} / 100; # XML data is usually in percent |
|
169 |
|
|
170 |
my $active_taxkey = $default_ap_amount_chart->taxkey_id; |
|
171 |
my $taxes = SL::DB::Manager::Tax->get_all( |
|
172 |
where => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' }], |
|
173 |
sort_by => 'taxkey, rate', |
|
174 |
); |
|
175 |
|
|
176 |
my $tax = first { $tax_rate == $_->rate } @{ $taxes }; |
|
177 |
$tax //= first { $active_taxkey->tax_id == $_->id } @{ $taxes }; |
|
178 |
$tax //= $taxes->[0]; |
|
179 |
|
|
180 |
# If we really can't find any tax definition (a simple rounding error may |
|
181 |
# be sufficient for that to happen), grab the first tax fitting the default |
|
182 |
# AP amount chart, just like the AP form would do it for manual entry. |
|
183 |
if ( scalar @{$taxes} == 0 ) { |
|
184 |
$taxes = SL::DB::Manager::Tax->get_all( |
|
185 |
where => [ chart_categories => { like => '%' . $default_ap_amount_chart->category . '%' } ], |
|
186 |
); |
|
187 |
} |
|
188 |
|
|
189 |
if (!$tax) { |
|
190 |
$row--; |
|
191 |
next; |
|
192 |
} |
|
193 |
|
|
194 |
$::form->{"AP_amount_chart_id_${row}"} = $default_ap_amount_chart->id; |
|
195 |
$::form->{"previous_AP_amount_chart_id_${row}"} = $default_ap_amount_chart->id; |
|
196 |
$::form->{"amount_${row}"} = $net_total; |
|
197 |
$::form->{"taxchart_${row}"} = $tax->id . '--' . $tax->rate; |
|
198 |
} |
|
199 |
|
|
200 |
flash('info', $::locale->text("The ZUGFeRD/Factur-X invoice '#1' has been loaded.", $file_name)); |
|
201 |
|
|
202 |
$::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token; |
|
203 |
$::form->{rowcount} = $row; |
|
204 |
|
|
205 |
update( |
|
206 |
keep_rows_without_amount => 1, |
|
207 |
dont_add_new_row => 1, |
|
208 |
); |
|
209 |
} |
|
122 |
$::form->{form_validity_token} = SL::DB::ValidityToken->create(scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST())->token; |
|
123 |
flash('info', $::locale->text( |
|
124 |
"The ZUGFeRD/Factur-X invoice '#1' has been loaded.", $file_name)); |
|
125 |
update( |
|
126 |
keep_rows_without_amount => 1, |
|
127 |
dont_add_new_row => 1, |
|
128 |
); |
|
210 | 129 |
} |
211 | 130 |
|
212 | 131 |
sub load_record_template { |
Auch abrufbar als: Unified diff
ZUGFeRD: Verschiebe komplettes Parsen aus ap.pl nach S:C:ZUGFeRD