Invoice.pm 1.0 KB

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