From 25b74064eb4b10b0e258594f7bbd47faf3682236 Mon Sep 17 00:00:00 2001 From: senator Date: Tue, 2 Nov 2010 16:27:23 +0000 Subject: [PATCH] Show money spent from each fund in an invoice voucher/print-out In the (near) future, we really ought to use a library like Money::Currency and refactor any Acq code that uses floating-point math. It's conceivable that numeric type fields in the acq schema need reviewed (should some of them be something like numeric(8,2) ?) git-svn-id: svn://svn.open-ils.org/ILS/trunk@18583 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../OpenILS/Application/Acq/Invoice.pm | 43 +++++- Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 18 ++- .../sql/Pg/upgrade/0457.data.acqinv.print.sql | 132 ++++++++++++++++++ 4 files changed, 188 insertions(+), 7 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0457.data.acqinv.print.sql diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm index fe40d0a0eb..79d10e772c 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm @@ -233,6 +233,44 @@ sub entry_amount_per_item { return $entry->amount_paid / $entry->phys_item_count; } +sub easy_money { # TODO XXX replace with something from a library + my ($val) = @_; + + my $rounded = int($val * 100) / 100.0; + if ($rounded == $val) { + return sprintf("%.02f", $val); + } else { + return sprintf("%g", $val); + } +} + +# 0 on failure (caller should call $e->die_event), array on success +sub amounts_spent_per_fund { + my ($e, $inv_id) = @_; + + my $entries = $e->search_acq_invoice_entry({"invoice" => $inv_id}) or + return 0; + + my %totals_by_fund; + foreach my $entry (@$entries) { + my $debits = find_entry_debits($e, $entry, "f") or return 0; + foreach (@$debits) { + $totals_by_fund{$_->fund} ||= 0.0; + $totals_by_fund{$_->fund} += $_->amount; + } + } + + my @totals; + foreach my $fund_id (keys %totals_by_fund) { + my $fund = $e->retrieve_acq_fund($fund_id) or return 0; + push @totals, { + "fund" => $fund->to_bare_hash, + "total" => easy_money($totals_by_fund{$fund_id}) + }; + } + + return \@totals; +} # there is no direct link between invoice_entry and fund debits. # when we need to retrieve the related debits, we have to do some searching @@ -496,10 +534,13 @@ sub print_html_invoice { return $e->die_event unless $e->allowed("VIEW_INVOICE", $invoice->receiver); + my $amounts = amounts_spent_per_fund($e, $invoice->id) or + return $e->die_event; + $conn->respond( $U->fire_object_event( undef, "format.acqinv.html", $invoice, $invoice->receiver, - "print-on-demand" + "print-on-demand", $amounts ) ); } diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index f0e4622e89..2c4bc6879a 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -70,7 +70,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0456'); -- gmc +INSERT INTO config.upgrade_log (version) VALUES ('0457'); -- senator CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql index f58aaf463d..07ec6d282a 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -4011,10 +4011,8 @@ INSERT INTO action_trigger.event_definition ( 'ProcessTemplate', 'print-on-demand', $$ -[% FILTER collapse %] [%- SET invoice = target -%] - +

Invoice

@@ -4125,9 +4123,19 @@ $$ [% END %] +
+ Amounts spent per fund: + + [% FOR blob IN user_data %] + + + + + [% END %] +
[% blob.fund.code %] ([% blob.fund.year %]):$[% blob.total %]
+
-[% END %] -$$ +[% END %]$$ ); diff --git a/Open-ILS/src/sql/Pg/upgrade/0457.data.acqinv.print.sql b/Open-ILS/src/sql/Pg/upgrade/0457.data.acqinv.print.sql new file mode 100644 index 0000000000..af7807260f --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0457.data.acqinv.print.sql @@ -0,0 +1,132 @@ +BEGIN; + +INSERT INTO config.upgrade_log VALUES ('0457'); + +UPDATE action_trigger.event_definition SET template = $$[% FILTER collapse %] +[%- SET invoice = target -%] + +
+

Invoice

+
+ No. [% invoice.inv_ident %] + [% IF invoice.inv_type %] + / Type:[% invoice.inv_type %] + [% END %] +
+
+
+ [% BLOCK ent_with_address %] +
[% ent_label %]: [% ent.name %] ([% ent.code %])
+
+ [% IF ent.addresses.0 %] + [% SET addr = ent.addresses.0 %] + [% addr.street1 %]
+ [% IF addr.street2 %][% addr.street2 %]
[% END %] + [% addr.city %], + [% IF addr.county %] [% addr.county %], [% END %] + [% IF addr.state %] [% addr.state %] [% END %] + [% IF addr.post_code %][% addr.post_code %][% END %]
+ [% IF addr.country %] [% addr.country %] [% END %] + [% END %] +

+ [% IF ent.phone %] Phone: [% ent.phone %]
[% END %] + [% IF ent.fax_phone %] Fax: [% ent.fax_phone %]
[% END %] + [% IF ent.url %] URL: [% ent.url %]
[% END %] + [% IF ent.email %] E-mail: [% ent.email %] [% END %] +

+
+ [% END %] + [% INCLUDE ent_with_address + ent = invoice.provider + ent_label = "Provider" %] + [% INCLUDE ent_with_address + ent = invoice.shipper + ent_label = "Shipper" %] +
Receiver
+
+ [% invoice.receiver.name %] ([% invoice.receiver.shortname %]) +
+
Received
+
+ [% helpers.format_date(invoice.recv_date) %] by + [% invoice.recv_method %] +
+ [% IF invoice.note %] +
Note
+
+ [% invoice.note %] +
+ [% END %] +
+
+
    + [% FOR entry IN invoice.entries %] +
  • + [% IF entry.lineitem %] + Title: [% helpers.get_li_attr( + "title", "", entry.lineitem.attributes + ) %]
    + Author: [% helpers.get_li_attr( + "author", "", entry.lineitem.attributes + ) %] + [% END %] + [% IF entry.purchase_order %] + (PO: [% entry.purchase_order.name %]) + [% END %]
    + Invoice item count: [% entry.inv_item_count %] + [% IF entry.phys_item_count %] + / Physical item count: [% entry.phys_item_count %] + [% END %] +
    + [% IF entry.cost_billed %] + Cost billed: [% entry.cost_billed %] + [% IF entry.billed_per_item %](per item)[% END %] +
    + [% END %] + [% IF entry.actual_cost %] + Actual cost: [% entry.actual_cost %]
    + [% END %] + [% IF entry.amount_paid %] + Amount paid: [% entry.amount_paid %]
    + [% END %] + [% IF entry.note %]Note: [% entry.note %][% END %] +
  • + [% END %] + [% FOR item IN invoice.items %] +
  • + [% IF item.inv_item_type %] + Item Type: [% item.inv_item_type %]
    + [% END %] + [% IF item.title %]Title/Description: + [% item.title %]
    + [% END %] + [% IF item.author %]Author: [% item.author %]
    [% END %] + [% IF item.purchase_order %]PO: [% item.purchase_order %]
    [% END %] + [% IF item.note %]Note: [% item.note %]
    [% END %] + [% IF item.cost_billed %] + Cost billed: [% item.cost_billed %]
    + [% END %] + [% IF item.actual_cost %] + Actual cost: [% item.actual_cost %]
    + [% END %] + [% IF item.amount_paid %] + Amount paid: [% item.amount_paid %]
    + [% END %] +
  • + [% END %] +
+
+ Amounts spent per fund: + + [% FOR blob IN user_data %] + + + + + [% END %] +
[% blob.fund.code %] ([% blob.fund.year %]):$[% blob.total %]
+
+
+[% END %]$$ WHERE id = 22; + +COMMIT; -- 2.43.2