Show money spent from each fund in an invoice voucher/print-out
authorsenator <senator@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 2 Nov 2010 16:27:23 +0000 (16:27 +0000)
committersenator <senator@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 2 Nov 2010 16:27:23 +0000 (16:27 +0000)
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

Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm
Open-ILS/src/sql/Pg/002.schema.config.sql
Open-ILS/src/sql/Pg/950.data.seed-values.sql
Open-ILS/src/sql/Pg/upgrade/0457.data.acqinv.print.sql [new file with mode: 0644]

index fe40d0a..79d10e7 100644 (file)
@@ -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
             )
         );
     }
index f0e4622..2c4bc68 100644 (file)
@@ -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,
index f58aaf4..07ec6d2 100644 (file)
@@ -4011,10 +4011,8 @@ INSERT INTO action_trigger.event_definition (
         'ProcessTemplate',
         'print-on-demand',
 $$
-[% FILTER collapse %]
 [%- SET invoice = target -%]
-<!-- This lacks totals, info about funds (for invoice entries,
-    funds are per-LID!), and general refinement -->
+<!-- This lacks general refinement -->
 <div class="acq-invoice-voucher">
     <h1>Invoice</h1>
     <div>
@@ -4125,9 +4123,19 @@ $$
             </li>
         [% END %]
     </ul>
+    <div>
+        Amounts spent per fund:
+        <table>
+        [% FOR blob IN user_data %]
+            <tr>
+                <th style="text-align: left;">[% blob.fund.code %] ([% blob.fund.year %]):</th>
+                <td>$[% blob.total %]</td>
+            </tr>
+        [% END %]
+        </table>
+    </div>
 </div>
-[% 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 (file)
index 0000000..af78072
--- /dev/null
@@ -0,0 +1,132 @@
+BEGIN;
+
+INSERT INTO config.upgrade_log VALUES ('0457');
+
+UPDATE action_trigger.event_definition SET template = $$[% FILTER collapse %]
+[%- SET invoice = target -%]
+<!-- This lacks general refinement -->
+<div class="acq-invoice-voucher">
+    <h1>Invoice</h1>
+    <div>
+        <strong>No.</strong> [% invoice.inv_ident %]
+        [% IF invoice.inv_type %]
+            / <strong>Type:</strong>[% invoice.inv_type %]
+        [% END %]
+    </div>
+    <div>
+        <dl>
+            [% BLOCK ent_with_address %]
+            <dt>[% ent_label %]: [% ent.name %] ([% ent.code %])</dt>
+            <dd>
+                [% IF ent.addresses.0 %]
+                    [% SET addr = ent.addresses.0 %]
+                    [% addr.street1 %]<br />
+                    [% IF addr.street2 %][% addr.street2 %]<br />[% END %]
+                    [% addr.city %],
+                    [% IF addr.county %] [% addr.county %], [% END %]
+                    [% IF addr.state %] [% addr.state %] [% END %]
+                    [% IF addr.post_code %][% addr.post_code %][% END %]<br />
+                    [% IF addr.country %] [% addr.country %] [% END %]
+                [% END %]
+                <p>
+                    [% IF ent.phone %] Phone: [% ent.phone %]<br />[% END %]
+                    [% IF ent.fax_phone %] Fax: [% ent.fax_phone %]<br />[% END %]
+                    [% IF ent.url %] URL: [% ent.url %]<br />[% END %]
+                    [% IF ent.email %] E-mail: [% ent.email %] [% END %]
+                </p>
+            </dd>
+            [% END %]
+            [% INCLUDE ent_with_address
+                ent = invoice.provider
+                ent_label = "Provider" %]
+            [% INCLUDE ent_with_address
+                ent = invoice.shipper
+                ent_label = "Shipper" %]
+            <dt>Receiver</dt>
+            <dd>
+                [% invoice.receiver.name %] ([% invoice.receiver.shortname %])
+            </dd>
+            <dt>Received</dt>
+            <dd>
+                [% helpers.format_date(invoice.recv_date) %] by
+                [% invoice.recv_method %]
+            </dd>
+            [% IF invoice.note %]
+                <dt>Note</dt>
+                <dd>
+                    [% invoice.note %]
+                </dd>
+            [% END %]
+        </dl>
+    </div>
+    <ul>
+        [% FOR entry IN invoice.entries %]
+            <li>
+                [% IF entry.lineitem %]
+                    Title: [% helpers.get_li_attr(
+                        "title", "", entry.lineitem.attributes
+                    ) %]<br />
+                    Author: [% helpers.get_li_attr(
+                        "author", "", entry.lineitem.attributes
+                    ) %]
+                [% END %]
+                [% IF entry.purchase_order %]
+                    (PO: [% entry.purchase_order.name %])
+                [% END %]<br />
+                Invoice item count: [% entry.inv_item_count %]
+                [% IF entry.phys_item_count %]
+                    / Physical item count: [% entry.phys_item_count %]
+                [% END %]
+                <br />
+                [% IF entry.cost_billed %]
+                    Cost billed: [% entry.cost_billed %]
+                    [% IF entry.billed_per_item %](per item)[% END %]
+                    <br />
+                [% END %]
+                [% IF entry.actual_cost %]
+                    Actual cost: [% entry.actual_cost %]<br />
+                [% END %]
+                [% IF entry.amount_paid %]
+                    Amount paid: [% entry.amount_paid %]<br />
+                [% END %]
+                [% IF entry.note %]Note: [% entry.note %][% END %]
+            </li>
+        [% END %]
+        [% FOR item IN invoice.items %]
+            <li>
+                [% IF item.inv_item_type %]
+                    Item Type: [% item.inv_item_type %]<br />
+                [% END %]
+                [% IF item.title %]Title/Description:
+                    [% item.title %]<br />
+                [% END %]
+                [% IF item.author %]Author: [% item.author %]<br />[% END %]
+                [% IF item.purchase_order %]PO: [% item.purchase_order %]<br />[% END %]
+                [% IF item.note %]Note: [% item.note %]<br />[% END %]
+                [% IF item.cost_billed %]
+                    Cost billed: [% item.cost_billed %]<br />
+                [% END %]
+                [% IF item.actual_cost %]
+                    Actual cost: [% item.actual_cost %]<br />
+                [% END %]
+                [% IF item.amount_paid %]
+                    Amount paid: [% item.amount_paid %]<br />
+                [% END %]
+            </li>
+        [% END %]
+    </ul>
+    <div>
+        Amounts spent per fund:
+        <table>
+        [% FOR blob IN user_data %]
+            <tr>
+                <th style="text-align: left;">[% blob.fund.code %] ([% blob.fund.year %]):</th>
+                <td>$[% blob.total %]</td>
+            </tr>
+        [% END %]
+        </table>
+    </div>
+</div>
+[% END %]$$ WHERE id = 22;
+
+COMMIT;