Invoice.pm 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package Trog::Enrich::Invoice;
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use List::Util();
  6. =head1 Trog::Enrich::Invoice
  7. The part of an invoice which has to be computed rather than merely fetched.
  8. Its payee and payor are ordinary relations, declared in invoice.json and
  9. resolved by Trog::Routes::HTML::enrich_post like any other type's. The
  10. running total is not -- it is read out of the line items themselves.
  11. =head2 enrich($post, $query) = %extra
  12. Sum the line items into a total. Each page of a multi-page invoice post is one
  13. line item, and a line beginning 'Price:' is what carries its cost; the
  14. denomination is taken from the first one that has any.
  15. =cut
  16. sub enrich ( $post, $ ) {
  17. return () unless ref $post->{data} eq 'ARRAY';
  18. my $denomination;
  19. my @prices = map {
  20. my $price = 0;
  21. ( $denomination, $price ) = m/^Price:\s*(\D)(\d+)/mix;
  22. $price;
  23. } @{ $post->{data} };
  24. $denomination //= '$';
  25. return ( total => $denomination . ( List::Util::sum(@prices) // 0 ) );
  26. }
  27. 1;