]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Invoice.pm
Post-2.5-m1 whitespace fixup
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Acq / Invoice.pm
1 package OpenILS::Application::Acq::Invoice;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
4
5 use OpenSRF::Utils::Logger qw(:logger);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::CStoreEditor q/:funcs/;
8 use OpenILS::Application::AppUtils;
9 use OpenILS::Event;
10 my $U = 'OpenILS::Application::AppUtils';
11
12
13 # return nothing on success, event on failure
14 sub _prepare_fund_debit_for_inv_item {
15     my ($debit, $item, $e) = @_;
16
17     $debit->fund($item->fund);
18     $debit->amount($item->amount_paid);
19     $debit->origin_amount($item->amount_paid);
20
21     # future: cache funds locally
22     my $fund = $e->retrieve_acq_fund($item->fund) or return $e->die_event;
23
24     $debit->origin_currency_type($fund->currency_type);
25     $debit->encumbrance('f');
26     $debit->debit_type('direct_charge');
27
28     return;
29 }
30
31 __PACKAGE__->register_method(
32     method => 'build_invoice_api',
33     api_name    => 'open-ils.acq.invoice.update',
34     signature => {
35         desc => q/Creates, updates, and deletes invoices, and related invoice entries, and invoice items/,
36         params => [
37             {desc => 'Authentication token', type => 'string'},
38             {desc => q/Invoice/, type => 'number'},
39             {desc => q/Entries.  Array of 'acqie' objects/, type => 'array'},
40             {desc => q/Items.  Array of 'acqii' objects/, type => 'array'},
41         ],
42         return => {desc => 'The invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
43     }
44 );
45
46
47 sub build_invoice_impl {
48     my ($e, $invoice, $entries, $items, $do_commit) = @_;
49
50     if ($invoice->isnew) {
51         $invoice->recv_method('PPR') unless $invoice->recv_method;
52         $invoice->recv_date('now') unless $invoice->recv_date;
53         $e->create_acq_invoice($invoice) or return $e->die_event;
54     } elsif ($invoice->isdeleted) {
55         $e->delete_acq_invoice($invoice) or return $e->die_event;
56     } else {
57         $e->update_acq_invoice($invoice) or return $e->die_event;
58     }
59
60     my $evt;
61
62     if ($entries) {
63         for my $entry (@$entries) {
64             $entry->invoice($invoice->id);
65
66             if ($entry->isnew) {
67                 $e->create_acq_invoice_entry($entry) or return $e->die_event;
68                 return $evt if $evt = uncancel_copies_as_needed($e, $entry);
69                 return $evt if $evt = update_entry_debits($e, $entry);
70             } elsif ($entry->isdeleted) {
71                 # XXX Deleting entries does not recancel anything previously
72                 # uncanceled.
73                 return $evt if $evt = rollback_entry_debits($e, $entry);
74                 $e->delete_acq_invoice_entry($entry) or return $e->die_event;
75             } elsif ($entry->ischanged) {
76                 my $orig_entry = $e->retrieve_acq_invoice_entry($entry->id) or
77                     return $e->die_event;
78
79                 if ($orig_entry->amount_paid != $entry->amount_paid or
80                     $entry->phys_item_count != $orig_entry->phys_item_count) {
81                     return $evt if $evt = rollback_entry_debits($e,$orig_entry);
82
83                     # XXX Updates can only uncancel more LIDs when
84                     # phys_item_count goes up, but cannot recancel them when
85                     # phys_item_count goes down.
86                     return $evt if $evt = uncancel_copies_as_needed($e, $entry);
87
88                     return $evt if $evt = update_entry_debits($e, $entry);
89                 }
90
91                 $e->update_acq_invoice_entry($entry) or return $e->die_event;
92             }
93         }
94     }
95
96     if ($items) {
97         for my $item (@$items) {
98             $item->invoice($invoice->id);
99
100             if ($item->isnew) {
101                 $e->create_acq_invoice_item($item) or return $e->die_event;
102
103                 # future: cache item types
104                 my $item_type = $e->retrieve_acq_invoice_item_type(
105                     $item->inv_item_type) or return $e->die_event;
106
107                 # This following complex conditional statement effecively means:
108                 #   1) Items with item_types that are prorate are handled
109                 #       differently.
110                 #   2) Only items with a po_item, or which are linked to a fund
111                 #       already, or which belong to invoices which we're trying
112                 #       to *close* will actually go through this fund_debit
113                 #       creation process.  In other cases, we'll consider it
114                 #       ok for an item to remain sans fund_debit for the time
115                 #       being.
116
117                 if (not $U->is_true($item_type->prorate) and
118                     ($item->po_item or $item->fund or
119                         $U->is_true($invoice->complete))) {
120
121                     my $debit;
122                     if ($item->po_item) {
123                         my $po_item = $e->retrieve_acq_po_item($item->po_item)
124                             or return $e->die_event;
125                         $debit = $e->retrieve_acq_fund_debit($po_item->fund_debit)
126                             or return $e->die_event;
127                     } else {
128                         $debit = Fieldmapper::acq::fund_debit->new;
129                         $debit->isnew(1);
130                     }
131
132                     return $evt if
133                         $evt = _prepare_fund_debit_for_inv_item($debit, $item, $e);
134
135                     if ($debit->isnew) {
136                         $e->create_acq_fund_debit($debit)
137                             or return $e->die_event;
138                     } else {
139                         $e->update_acq_fund_debit($debit)
140                             or return $e->die_event;
141                     }
142
143                     $item->fund_debit($debit->id);
144                     $e->update_acq_invoice_item($item) or return $e->die_event;
145                 }
146             } elsif ($item->isdeleted) {
147                 $e->delete_acq_invoice_item($item) or return $e->die_event;
148
149                 if ($item->po_item and
150                     $e->retrieve_acq_po_item($item->po_item)->fund_debit == $item->fund_debit) {
151                     # the debit is attached to the po_item. instead of
152                     # deleting it, roll it back to being an encumbrance.
153                     # Note: a prorated invoice_item that points to a
154                     # po_item could point to a different fund_debit. We
155                     # can't go back in time to collect all the prorated
156                     # invoice_items (nor is the caller asking us too),
157                     # so when that happens, just delete the extraneous
158                     # debit (in the else block).
159                     my $debit = $e->retrieve_acq_fund_debit($item->fund_debit);
160                     $debit->encumbrance('t');
161                     $e->update_acq_fund_debit($debit) or return $e->die_event;
162                 } elsif ($item->fund_debit) {
163                     $e->delete_acq_fund_debit($e->retrieve_acq_fund_debit($item->fund_debit))
164                         or return $e->die_event;
165                 }
166             } elsif ($item->ischanged) {
167                 my $debit;
168
169                 if (!$item->fund_debit) {
170                     # No fund_debit yet? Make one now.
171                     $debit = Fieldmapper::acq::fund_debit->new;
172                     $debit->isnew(1);
173
174                     return $evt if
175                         $evt = _prepare_fund_debit_for_inv_item($debit, $item, $e);
176                 } else {
177                     $debit = $e->retrieve_acq_fund_debit($item->fund_debit) or
178                         return $e->die_event;
179                 }
180
181                 $debit->amount($item->amount_paid);
182                 $debit->fund($item->fund);
183
184                 if ($debit->isnew) {
185                     # Making a new debit, so make it and link our item to it.
186                     $e->create_acq_fund_debit($debit) or return $e->die_event;
187                     $item->fund_debit($e->data->id);
188                 } else {
189                     $e->update_acq_fund_debit($debit) or return $e->die_event;
190                 }
191
192                 $e->update_acq_invoice_item($item) or return $e->die_event;
193             }
194         }
195     }
196
197     $invoice = fetch_invoice_impl($e, $invoice->id);
198     if ($do_commit) {
199         $e->commit or return $e->die_event;
200     }
201
202     return $invoice;
203 }
204
205 sub build_invoice_api {
206     my($self, $conn, $auth, $invoice, $entries, $items) = @_;
207
208     my $e = new_editor(xact => 1, authtoken=>$auth);
209     return $e->die_event unless $e->checkauth;
210
211     if (not ref $invoice) {
212         # caller only provided the ID
213         $invoice = $e->retrieve_acq_invoice($invoice) or return $e->die_event;
214     }
215
216     if (not $invoice->receiver and $invoice->isnew) {
217         $invoice->receiver($e->requestor->ws_ou);
218     }
219
220     return $e->die_event unless
221         $e->allowed('CREATE_INVOICE', $invoice->receiver);
222
223     return build_invoice_impl($e, $invoice, $entries, $items, 1);
224 }
225
226
227 sub rollback_entry_debits {
228     my($e, $entry) = @_;
229     my $debits = find_entry_debits($e, $entry, 'f', entry_amount_per_item($entry));
230     my $lineitem = $e->retrieve_acq_lineitem($entry->lineitem) or return $e->die_event;
231
232     for my $debit (@$debits) {
233         # revert to the original estimated amount re-encumber
234         $debit->encumbrance('t');
235         $debit->amount($lineitem->estimated_unit_price());
236         $e->update_acq_fund_debit($debit) or return $e->die_event;
237         update_copy_cost($e, $debit) or return $e->die_event; # clear the cost
238     }
239
240     return undef;
241 }
242
243 sub update_entry_debits {
244     my($e, $entry) = @_;
245
246     my $debits = find_entry_debits($e, $entry, 't');
247     return undef unless @$debits;
248
249     if($entry->phys_item_count > @$debits) {
250         $e->rollback;
251         # We can't invoice for more items than we have debits for
252         return OpenILS::Event->new(
253             'ACQ_INVOICE_ENTRY_COUNT_EXCEEDS_DEBITS', 
254             payload => {entry => $entry->id});
255     }
256
257     for my $debit (@$debits) {
258         my $amount = entry_amount_per_item($entry);
259         $debit->amount($amount);
260         $debit->encumbrance('f');
261         $e->update_acq_fund_debit($debit) or return $e->die_event;
262
263         # TODO: this does not reflect ancillary charges, like taxes, etc.
264         # We may need a way to indicate whether the amount attached to an 
265         # invoice_item should be prorated and included in the copy cost.
266         # Note that acq.invoice_item_type.prorate does not necessarily 
267         # mean a charge should be included in the copy price, only that 
268         # it should spread accross funds.
269         update_copy_cost($e, $debit, $amount) or return $e->die_event;
270     }
271
272     return undef;
273 }
274
275 # This was originally done only for EDI invoices, but needs added to the
276 # manual invoice-entering process for consistency's sake.
277 sub uncancel_copies_as_needed {
278     my ($e, $entry) = @_;
279
280     return unless $entry->lineitem and $entry->phys_item_count;
281
282     my $li = $e->retrieve_acq_lineitem($entry->lineitem) or
283         return $e->die_event;
284
285     # if an invoiced lineitem is marked as cancelled
286     # (e.g. back-order), invoicing the lineitem implies
287     # we need to un-cancel it
288
289     # collect the LIDs, starting with those that are
290     # not cancelled, followed by those that have keep-debits cancel_reasons,
291     # followed by non-keep-debit cancel reasons.
292
293     my $lid_ids = $e->json_query({
294         select => {acqlid => ['id']},
295         from => {
296             acqlid => {
297                 acqcr => {type => 'left'},
298                 acqfdeb => {type => 'left'}
299             }
300         },
301         where => {
302             '+acqlid' => {lineitem => $li->id},
303             '+acqfdeb' => {encumbrance => 't'}  # not-yet invoiced copies
304         },
305         order_by => [{
306             class => 'acqcr',
307             field => 'keep_debits',
308             direction => 'desc'
309         }],
310         limit => $entry->phys_item_count    # crucial
311     });
312
313     for my $lid_id (map {$_->{id}} @$lid_ids) {
314         my $lid = $e->retrieve_acq_lineitem_detail($lid_id);
315         next unless $lid->cancel_reason;
316
317         $logger->info(
318             "un-cancelling invoice lineitem " . $li->id .
319             " lineitem_detail " . $lid_id
320         );
321         $lid->clear_cancel_reason;
322         return $e->die_event unless $e->update_acq_lineitem_detail($lid);
323     }
324
325     $li->clear_cancel_reason;
326     $li->state("on-order") if $li->state eq "cancelled";    # sic
327     $li->edit_time("now");
328
329     unless ($e->update_acq_lineitem($li)) {
330         my $evt = $e->die_event;
331         $logger->error("couldn't clear li cancel reason: ". $evt->{textcode});
332         return $evt;
333     }
334
335     return;
336 }
337
338
339 # update the linked copy to reflect the amount paid for the item
340 # returns true on success, false on error
341 sub update_copy_cost {
342     my ($e, $debit, $amount) = @_;
343
344     my $lid = $e->search_acq_lineitem_detail([
345         {fund_debit => $debit->id},
346         {flesh => 1, flesh_fields => {acqlid => ['eg_copy_id']}}
347     ])->[0];
348
349     if($lid and my $copy = $lid->eg_copy_id) {
350         defined $amount and $copy->cost($amount) or $copy->clear_cost;
351
352         # XXX It would be nice to have a way to record that a copy was
353         # updated by a non-user mechanism, like EDI, but we don't have
354         # a clear way to do that here.
355         if ($e->requestor) {
356             $copy->editor($e->requestor->id);
357             $copy->edit_date('now');
358         }
359
360         $e->update_asset_copy($copy) or return 0;
361     }
362
363     return 1;
364 }
365
366
367 sub entry_amount_per_item {
368     my $entry = shift;
369     return $entry->amount_paid if $U->is_true($entry->billed_per_item);
370     return 0 if $entry->phys_item_count == 0;
371     return $entry->amount_paid / $entry->phys_item_count;
372 }
373
374 sub easy_money { # TODO XXX replace with something from a library
375     my ($val) = @_;
376
377     my $rounded = int($val * 100) / 100.0;
378     if ($rounded == $val) {
379         return sprintf("%.02f", $val);
380     } else {
381         return sprintf("%g", $val);
382     }
383 }
384
385 # 0 on failure (caller should call $e->die_event), array on success
386 sub amounts_spent_per_fund {
387     my ($e, $inv_id) = @_;
388
389     my $entries = $e->search_acq_invoice_entry({"invoice" => $inv_id}) or
390         return 0;
391
392     my $items = $e->search_acq_invoice_item({"invoice" => $inv_id}) or
393         return 0;
394
395     my %totals_by_fund;
396     foreach my $entry (@$entries) {
397         my $debits = find_entry_debits($e, $entry, "f") or return 0;
398         foreach (@$debits) {
399             $totals_by_fund{$_->fund} ||= 0.0;
400             $totals_by_fund{$_->fund} += $_->amount;
401         }
402     }
403
404     foreach my $item (@$items) {
405         next unless $item->fund and $item->amount_paid;
406         $totals_by_fund{$item->fund} ||= 0.0;
407         $totals_by_fund{$item->fund} += $item->amount_paid;
408     }
409
410     my @totals;
411     foreach my $fund_id (keys %totals_by_fund) {
412         my $fund = $e->retrieve_acq_fund($fund_id) or return 0;
413         push @totals, {
414             "fund" => $fund->to_bare_hash,
415             "total" => easy_money($totals_by_fund{$fund_id})
416         };
417     }
418
419     return \@totals;
420 }
421
422 # there is no direct link between invoice_entry and fund debits.
423 # when we need to retrieve the related debits, we have to do some searching
424 sub find_entry_debits {
425     my($e, $entry, $encumbrance, $amount) = @_;
426
427     my $query = {
428         select => {acqfdeb => ['id']},
429         from => {
430             acqfdeb => {
431                 acqlid => {
432                     join => {
433                         jub =>  {
434                             join => {
435                                 acqie => {
436                                     filter => {id => $entry->id}
437                                 }
438                             }
439                         }
440                     }
441                 }
442             }
443         },
444         where => {'+acqfdeb' => {encumbrance => $encumbrance}},
445         order_by => {'acqlid' => ['recv_time']}, # un-received items will sort to the end
446         limit => $entry->phys_item_count
447     };
448
449     $query->{where}->{'+acqfdeb'}->{amount} = $amount if $amount;
450
451     my $debits = $e->json_query($query);
452     my $debit_ids = [map { $_->{id} } @$debits];
453     return (@$debit_ids) ? $e->search_acq_fund_debit({id => $debit_ids}) : [];
454 }
455
456
457 __PACKAGE__->register_method(
458     method => 'build_invoice_api',
459     api_name    => 'open-ils.acq.invoice.retrieve',
460     authoritative => 1,
461     signature => {
462         desc => q/Creates a new stub invoice/,
463         params => [
464             {desc => 'Authentication token', type => 'string'},
465             {desc => q/Invoice Id/, type => 'number'},
466         ],
467         return => {desc => 'The new invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
468     }
469 );
470
471
472 sub fetch_invoice_api {
473     my($self, $conn, $auth, $invoice_id, $options) = @_;
474
475     my $e = new_editor(authtoken=>$auth);
476     return $e->event unless $e->checkauth;
477
478     my $invoice = fetch_invoice_impl($e, $invoice_id, $options) or
479         return $e->event;
480     return $e->event unless $e->allowed(['VIEW_INVOICE', 'CREATE_INVOICE'], $invoice->receiver);
481
482     return $invoice;
483 }
484
485 sub fetch_invoice_impl {
486     my ($e, $invoice_id, $options) = @_;
487
488     $options ||= {};
489
490     my $args = $options->{"no_flesh_misc"} ? $invoice_id : [
491         $invoice_id,
492         {
493             "flesh" => 6,
494             "flesh_fields" => {
495                 "acqinv" => ["entries", "items"],
496                 "acqii" => ["fund_debit", "purchase_order", "po_item"]
497             }
498         }
499     ];
500
501     return $e->retrieve_acq_invoice($args);
502 }
503
504 __PACKAGE__->register_method(
505     method => 'prorate_invoice',
506     api_name    => 'open-ils.acq.invoice.apply_prorate',
507     signature => {
508         desc => q/
509             For all invoice items that have the prorate flag set to true, this will create the necessary 
510             additional invoice_item's to prorate the cost across all affected funds by percent spent for each fund.
511         /,
512         params => [
513             {desc => 'Authentication token', type => 'string'},
514             {desc => q/Invoice Id/, type => 'number'},
515         ],
516         return => {desc => 'The updated invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
517     }
518 );
519
520
521 sub prorate_invoice {
522     my($self, $conn, $auth, $invoice_id) = @_;
523
524     my $e = new_editor(xact => 1, authtoken=>$auth);
525     return $e->die_event unless $e->checkauth;
526
527     my $invoice = fetch_invoice_impl($e, $invoice_id) or return $e->die_event;
528     return $e->die_event unless $e->allowed('CREATE_INVOICE', $invoice->receiver);
529
530     my @lid_debits;
531     push(@lid_debits, @{find_entry_debits($e, $_, 'f', entry_amount_per_item($_))}) for @{$invoice->entries};
532
533     my $inv_items = $e->search_acq_invoice_item([
534         {"invoice" => $invoice_id, "fund_debit" => {"!=" => undef}},
535         {"flesh" => 1, "flesh_fields" => {"acqii" => ["fund_debit"]}}
536     ]) or return $e->die_event;
537
538     my @item_debits = map { $_->fund_debit } @$inv_items;
539
540     my %fund_totals;
541     my $total_entry_paid = 0;
542     for my $debit (@lid_debits, @item_debits) {
543         $fund_totals{$debit->fund} = 0 unless $fund_totals{$debit->fund};
544         $fund_totals{$debit->fund} += $debit->amount;
545         $total_entry_paid += $debit->amount;
546     }
547
548     $logger->info("invoice: prorating against invoice amount $total_entry_paid");
549
550     for my $item (@{$invoice->items}) {
551
552         next if $item->fund_debit; # item has already been processed
553
554         # future: cache item types locally
555         my $item_type = $e->retrieve_acq_invoice_item_type($item->inv_item_type) or return $e->die_event;
556         next unless $U->is_true($item_type->prorate);
557
558         # Prorate charges across applicable funds
559         my $full_item_paid = $item->amount_paid; # total amount paid for this item before splitting
560         my $full_item_cost = $item->cost_billed; # total amount invoiced for this item before splitting
561         my $first_round = 1;
562         my $largest_debit;
563         my $largest_item;
564         my $total_debited = 0;
565         my $total_costed = 0;
566
567         for my $fund_id (keys %fund_totals) {
568
569             my $spent_for_fund = $fund_totals{$fund_id};
570             next unless $spent_for_fund > 0;
571
572             my $prorated_amount = ($spent_for_fund / $total_entry_paid) * $full_item_paid;
573             my $prorated_cost = ($spent_for_fund / $total_entry_paid) * $full_item_cost;
574             $logger->info("invoice: attaching prorated amount $prorated_amount to fund $fund_id for invoice $invoice_id");
575
576             my $debit;
577             if($first_round and $item->po_item) {
578                 # if this item is the result of a PO item, repurpose the original debit
579                 # for the first chunk of the prorated amount
580                 $debit = $e->retrieve_acq_fund_debit($item->po_item->fund_debit);
581             } else {
582                 $debit = Fieldmapper::acq::fund_debit->new;
583                 $debit->isnew(1);
584             }
585
586             $debit->fund($fund_id);
587             $debit->amount($prorated_amount);
588             $debit->origin_amount($prorated_amount);
589             $debit->origin_currency_type($e->retrieve_acq_fund($fund_id)->currency_type); # future: cache funds locally
590             $debit->encumbrance('f');
591             $debit->debit_type('prorated_charge');
592
593             if($debit->isnew) {
594                 $e->create_acq_fund_debit($debit) or return $e->die_event;
595             } else {
596                 $e->update_acq_fund_debit($debit) or return $e->die_event;
597             }
598
599             $total_debited += $prorated_amount;
600             $total_costed += $prorated_cost;
601             $largest_debit = $debit if !$largest_debit or $prorated_amount > $largest_debit->amount;
602
603             if($first_round) {
604
605                 # re-purpose the original invoice_item for the first prorated amount
606                 $item->fund($fund_id);
607                 $item->fund_debit($debit->id);
608                 $item->amount_paid($prorated_amount);
609                 $item->cost_billed($prorated_cost);
610                 $e->update_acq_invoice_item($item) or return $e->die_event;
611                 $largest_item = $item if !$largest_item or $prorated_amount > $largest_item->amount_paid;
612
613             } else {
614
615                 # for subsequent prorated amounts, create a new invoice_item
616                 my $new_item = $item->clone;
617                 $new_item->clear_id;
618                 $new_item->fund($fund_id);
619                 $new_item->fund_debit($debit->id);
620                 $new_item->amount_paid($prorated_amount);
621                 $new_item->cost_billed($prorated_cost);
622                 $e->create_acq_invoice_item($new_item) or return $e->die_event;
623                 $largest_item = $new_item if !$largest_item or $prorated_amount > $largest_item->amount_paid;
624             }
625
626             $first_round = 0;
627         }
628
629         # make sure the percentages didn't leave a small sliver of money over/under-debited
630         # if so, tweak the largest debit to smooth out the difference
631         if($total_debited != $full_item_paid or $total_costed != $full_item_cost) {
632             
633             my $paid_diff = $full_item_paid - $total_debited;
634             my $cost_diff = $full_item_cost - $total_debited;
635             $logger->info("invoice: repairing prorate descrepency of paid:$paid_diff and cost:$cost_diff");
636             my $new_paid = $largest_item->amount_paid + $paid_diff;
637             my $new_cost = $largest_item->cost_billed + $cost_diff;
638
639             $largest_debit = $e->retrieve_acq_fund_debit($largest_debit->id); # get latest copy
640             $largest_debit->amount($new_paid);
641             $e->update_acq_fund_debit($largest_debit) or return $e->die_event;
642
643             $largest_item = $e->retrieve_acq_invoice_item($largest_item->id); # get latest copy
644             $largest_item->amount_paid($new_paid);
645             $largest_item->cost_billed($new_cost);
646
647             $e->update_acq_invoice_item($largest_item) or return $e->die_event;
648         }
649     }
650
651     $invoice = fetch_invoice_impl($e, $invoice_id);
652     $e->commit;
653
654     return $invoice;
655 }
656
657
658 __PACKAGE__->register_method(
659     method      => "print_html_invoice",
660     api_name    => "open-ils.acq.invoice.print.html",
661     stream      => 1,
662     signature   => {
663         desc    => "Retrieve printable HTML vouchers for each given invoice",
664         params => [
665             {desc => "Authentication token", type => "string"},
666             {desc => "Invoice ID or a list of them", type => "mixed"},
667         ],
668         return => {
669             desc => q{One A/T event containing a printable HTML voucher for
670                 each given invoice},
671             type => "object", class => "atev"}
672     }
673 );
674
675
676 sub print_html_invoice {
677     my ($self, $conn, $auth, $id_list) = @_;
678
679     my $e = new_editor("authtoken" => $auth);
680     return $e->die_event unless $e->checkauth;
681
682     $id_list = [$id_list] unless ref $id_list;
683
684     my $invoices = $e->search_acq_invoice({"id" => $id_list}) or
685         return $e->die_event;
686
687     foreach my $invoice (@$invoices) {
688         return $e->die_event unless
689             $e->allowed("VIEW_INVOICE", $invoice->receiver);
690
691         my $amounts = amounts_spent_per_fund($e, $invoice->id) or
692             return $e->die_event;
693
694         $conn->respond(
695             $U->fire_object_event(
696                 undef, "format.acqinv.html", $invoice, $invoice->receiver,
697                 "print-on-demand", $amounts
698             )
699         );
700     }
701
702     $e->disconnect;
703     undef;
704 }
705
706 1;