From: erickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Mon, 5 Apr 2010 18:58:45 +0000 (+0000)
Subject: initial invoice sub-module
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=b34818dc10eed6dad2652a2b74c5055c7bf58029;p=evergreen%2Fmasslnc.git

initial invoice sub-module

git-svn-id: svn://svn.open-ils.org/ILS/trunk@16133 dcc99617-32d9-48b4-a31d-7c20da2025e4
---

diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq.pm
index 5cb5ba0fec..04ba25485e 100644
--- a/Open-ILS/src/perlmods/OpenILS/Application/Acq.pm
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq.pm
@@ -10,5 +10,6 @@ use OpenILS::Application::Acq::Order;
 use OpenILS::Application::Acq::EDI;
 use OpenILS::Application::Acq::Search;
 use OpenILS::Application::Acq::Claims;
+use OpenILS::Application::Acq::Invoice;
 
 1;
diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm
new file mode 100644
index 0000000000..603d5f2635
--- /dev/null
+++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Invoice.pm
@@ -0,0 +1,123 @@
+package OpenILS::Application::Acq::Invoice;
+use base qw/OpenILS::Application/;
+use strict; use warnings;
+
+use OpenSRF::Utils::Logger qw(:logger);
+use OpenILS::Utils::Fieldmapper;
+use OpenILS::Utils::CStoreEditor q/:funcs/;
+use OpenILS::Application::AppUtils;
+use OpenILS::Event;
+my $U = 'OpenILS::Application::AppUtils';
+
+
+__PACKAGE__->register_method(
+	method => 'build_invoice_api',
+	api_name	=> 'open-ils.acq.invoice.create',
+	signature => {
+        desc => q/Creates a new stub invoice/,
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => q/Invoice Object/, type => 'object', class => 'acqinv'},
+        ],
+        return => {desc => 'The new invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
+    }
+);
+
+__PACKAGE__->register_method(
+	method => 'build_invoice_api',
+	api_name	=> 'open-ils.acq.invoice.attach',
+	signature => {
+        desc => q/Attach invoice entries and invoice items to an existing invoice/,
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => q/Invoice ID/, type => 'number'},
+            {desc => q/Entries/, type => 'array'},
+            {desc => q/Items/, type => 'array'},
+        ],
+        return => {desc => 'The invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
+    }
+);
+
+sub build_invoice_api {
+    my($self, $conn, $auth, $invoice, $entries, $items) = @_;
+
+    my $e = new_editor(xact => 1, authtoken=>$auth);
+    return $e->die_event unless $e->checkauth;
+
+
+    if($self->api_name =~ /create/) {
+        $invoice->receiver($e->requestor->ws_ou) unless $invoice->receiver;
+        $invoice->recv_method('PPR') unless $invoice->recv_method;
+        $invoice->recv_date('now') unless $invoice->recv_date;
+        $e->create_acq_invoice($invoice) or return $e->die_event;
+    } else {
+        $invoice = $e->retrieve_acq_invoice($invoice) or return $e->die_event;
+    }
+
+    return $e->die_event unless $e->allowed('CREATE_INVOICE', $invoice->receiver);
+
+    if($entries) {
+        for my $entry (@$entries) {
+            $entry->invoice($invoice->id);
+            $e->create_acq_invoice_entry($entry) or return $e->die_event;
+        }
+    }
+
+    if($items) {
+        for my $item (@$items) {
+            $item->invoice($invoice->id);
+            $e->create_acq_invoice_item($item) or return $e->die_event;
+        }
+    }
+
+    $invoice = fetch_invice_impl($e, $invoice->id);
+    $e->commit;
+
+    return $invoice;
+}
+
+__PACKAGE__->register_method(
+	method => 'build_invoice_api',
+	api_name	=> 'open-ils.acq.invoice.retrieve',
+	signature => {
+        desc => q/Creates a new stub invoice/,
+        params => [
+            {desc => 'Authentication token', type => 'string'},
+            {desc => q/Invoice Id/, type => 'number'},
+        ],
+        return => {desc => 'The new invoice w/ entries and items attached', type => 'object', class => 'acqinv'}
+    }
+);
+
+
+sub fetch_invice_api {
+    my($self, $conn, $auth, $invoice_id) = @_;
+
+    my $e = new_editor(authtoken=>$auth);
+    return $e->event unless $e->checkauth;
+
+    my $invoice = fetch_invoice_impl($e, $invoice_id) or return $e->event;
+    return $e->event unless $e->allowed(['VIEW_INVOICE', 'CREATE_INVOICE'], $invoice->receiver);
+
+    return $invoice;
+}
+
+sub fetch_invice_impl {
+    my $e = shift;
+    my $invoice_id = shift;
+
+    return $e->retrieve_acq_invoice([
+        $invoice_id,
+        {
+            flesh => 3,
+            flesh_fields => {
+                acqinv => ['entries', 'items'],
+                acqie => ['lineitem', 'purchase_order'],
+                jub => ['attributes']
+            }
+        }
+    ]);
+}
+
+
+1;