Invoice.pm 1019 B

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