From 79dec24bb1369f6cb9ca64f27c71edb08e2a507b Mon Sep 17 00:00:00 2001 From: erickson Date: Thu, 13 May 2010 14:23:14 +0000 Subject: [PATCH] moved to generic acqpo.activate hook instead of the format.po.jedi hook added validator to determine of a PO should create JEDI added JEDI creation reactor. it behaves like ProcessTemplate, but gives us something more specific to filter acqpo.activate events on update PO JEDI reactor to use new hook and reactor update the edi_pusher to filter out events for PO's that have already been processed git-svn-id: svn://svn.open-ils.org/ILS/trunk@16425 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../src/perlmods/OpenILS/Application/Acq/Order.pm | 3 + .../Trigger/Reactor/GeneratePurchaseOrderJEDI.pm | 22 ++++++++ .../Validator/Acq/PurchaseOrderEDIRequired.pm | 25 +++++++++ Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/400.schema.action_trigger.sql | 2 +- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 27 ++++++++- .../upgrade/0260.data.acq.po_activate_and_jedi.sql | 47 ++++++++++++++++ Open-ILS/src/support-scripts/edi_pusher.pl | 64 ++++++++++++++++++---- 8 files changed, 178 insertions(+), 14 deletions(-) create mode 100644 Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/GeneratePurchaseOrderJEDI.pm create mode 100644 Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator/Acq/PurchaseOrderEDIRequired.pm create mode 100644 Open-ILS/src/sql/Pg/upgrade/0260.data.acq.po_activate_and_jedi.sql diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm index 76ae409b9..c40f4f475 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Acq/Order.pm @@ -2214,6 +2214,9 @@ sub activate_purchase_order_impl { $mgr->respond; } + # tell the world we activated a PO + $U->create_events_for_hook('acqpo.activate', $po, $po->ordering_agency); + return undef; } diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/GeneratePurchaseOrderJEDI.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/GeneratePurchaseOrderJEDI.pm new file mode 100644 index 000000000..f7e9fc543 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/GeneratePurchaseOrderJEDI.pm @@ -0,0 +1,22 @@ +package OpenILS::Application::Trigger::Reactor::GeneratePurchaseOrderJEDI; +use base 'OpenILS::Application::Trigger::Reactor'; +use strict; use warnings; +use OpenSRF::Utils::Logger qw/:logger/; + +sub ABOUT { + return <run_TT($env); + return 0; +} + +1; + diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator/Acq/PurchaseOrderEDIRequired.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator/Acq/PurchaseOrderEDIRequired.pm new file mode 100644 index 000000000..028134f77 --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Validator/Acq/PurchaseOrderEDIRequired.pm @@ -0,0 +1,25 @@ +package OpenILS::Application::Trigger::Validator::Acq::PurchaseOrderEDIRequired; +use strict; use warnings; +use OpenSRF::Utils::Logger qw/:logger/; +use OpenILS::Application::AppUtils; +my $U = 'OpenILS::Application::AppUtils'; + +sub handler { + my $self = shift; + my $env = shift; + my $po = $env->{target}; + + my $provider = + ref($po->provider) ? + $po->provider : + $self->editor->retrieve_acq_provider($po->provider); + + return 1 if + $po->state eq 'on-order' and + $provider->edi_default and + $U->is_true($provider->active); + + return 0; +} + +1; diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index d20770bf7..ec005fab1 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -65,7 +65,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0259'); -- Scott McKellar +INSERT INTO config.upgrade_log (version) VALUES ('0260'); -- berick CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql index 871dafa8c..481cec063 100644 --- a/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql +++ b/Open-ILS/src/sql/Pg/400.schema.action_trigger.sql @@ -48,7 +48,7 @@ INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('pen INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_OVERDUE_COUNT','ausp','Patron has exceeded allowed overdue count',TRUE); INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_CHECKOUT_COUNT','ausp','Patron has exceeded allowed checkout count',TRUE); INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('penalty.PATRON_EXCEEDS_COLLECTIONS_WARNING','ausp','Patron has exceeded maximum fine amount for collections department warning',TRUE); -INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.jedi','acqpo','Formats a Purchase Order as a JEDI document',TRUE); +INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('acqpo.activated','acqpo','Purchase order was activated',FALSE); INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.html','acqpo','Formats a Purchase Order as an HTML document',TRUE); INSERT INTO action_trigger.hook (key,core_type,description,passive) VALUES ('format.po.pdf','acqpo','Formats a Purchase Order as a PDF document',TRUE); INSERT INTO action_trigger.hook (key,core_type,description) VALUES ('damaged','acp','Item marked damaged'); 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 464b81528..f4b5e6e9c 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -4689,7 +4689,32 @@ INSERT INTO action_trigger.environment (event_def, path) VALUES INSERT INTO action_trigger.environment ( event_def, path) VALUES ( 13, 'open_billable_transactions_summary.circulation' ); -INSERT INTO action_trigger.event_definition (id, active, owner, name, hook, validator, reactor, cleanup_success, cleanup_failure, delay, delay_field, group_field, template) VALUES (23, true, 1, 'PO JEDI', 'format.po.jedi', 'NOOP_True', 'ProcessTemplate', NULL, NULL, '00:05:00', NULL, NULL, + +INSERT INTO action_trigger.validator (module, description) + VALUES ( + 'Acq::PurchaseOrderEDIRequired', + oils_i18n_gettext( + 'Acq::PurchaseOrderEDIRequired', + 'Purchase order is delivered via EDI', + 'atval', + 'description' + ) + ); + +INSERT INTO action_trigger.reactor (module, description) + VALUES ( + 'GeneratePurchaseOrderJEDI', + oils_i18n_gettext( + 'GeneratePurchaseOrderJEDI', + 'Creates purchase order JEDI (JSON EDI) for subsequent EDI processing', + 'atreact', + 'description' + ) + ); + + +INSERT INTO action_trigger.event_definition (id, active, owner, name, hook, validator, reactor, cleanup_success, cleanup_failure, delay, delay_field, group_field, template) + VALUES (23, true, 1, 'PO JEDI', 'acqpo.activated', 'Acq::PurchaseOrderEDIRequired', 'GeneratePurchaseOrderJEDI', NULL, NULL, '00:05:00', NULL, NULL, $$[%- USE date -%] [%# start JEDI document -%] [%- BLOCK big_block -%] diff --git a/Open-ILS/src/sql/Pg/upgrade/0260.data.acq.po_activate_and_jedi.sql b/Open-ILS/src/sql/Pg/upgrade/0260.data.acq.po_activate_and_jedi.sql new file mode 100644 index 000000000..e012d7f29 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0260.data.acq.po_activate_and_jedi.sql @@ -0,0 +1,47 @@ +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0260'); + +INSERT INTO action_trigger.validator (module, description) + VALUES ( + 'Acq::PurchaseOrderEDIRequired', + oils_i18n_gettext( + 'Acq::PurchaseOrderEDIRequired', + 'Purchase order is delivered via EDI', + 'atval', + 'description' + ) + ); + +INSERT INTO action_trigger.reactor (module, description) + VALUES ( + 'GeneratePurchaseOrderJEDI', + oils_i18n_gettext( + 'GeneratePurchaseOrderJEDI', + 'Creates purchase order JEDI (JSON EDI) for subsequent EDI processing', + 'atreact', + 'description' + ) + ); + +UPDATE action_trigger.hook + SET + key = 'acqpo.activated', + passive = FALSE, + description = oils_i18n_gettext( + 'acqpo.activated', + 'Purchase order was activated', + 'ath', + 'description' + ) + WHERE key = 'format.po.jedi'; + +UPDATE action_trigger.event_definition + SET + hook = 'acqpo.activated', + validator = 'Acq::PurchaseOrderEDIRequired', + reactor = 'GeneratePurchaseOrderJEDI' + WHERE id = 23; + +COMMIT; + diff --git a/Open-ILS/src/support-scripts/edi_pusher.pl b/Open-ILS/src/support-scripts/edi_pusher.pl index 885659c1b..6eeed72f4 100644 --- a/Open-ILS/src/support-scripts/edi_pusher.pl +++ b/Open-ILS/src/support-scripts/edi_pusher.pl @@ -32,9 +32,12 @@ INIT { my %opts = ( 'quiet' => 0, + 'max-batch-size=i' => -1 ); -OpenILS::Utils::Cronscript->new()->session('open-ils.acq') or die "No session created"; +my $cs = OpenILS::Utils::Cronscript->new(\%opts); +$cs->session('open-ils.acq') or die "No session created"; + OpenILS::Utils::CStoreEditor::init(); sub editor { @@ -43,26 +46,63 @@ sub editor { } my $e = editor(); -my $hook = 'format.po.jedi'; -my $defs = $e->search_action_trigger_event_definition({hook => $hook}); +my $hook = 'acqpo.activated'; +my $defs = $e->search_action_trigger_event_definition({ + hook => $hook, + reactor => 'GeneratePurchaseOrderJEDI', + active => 't' +}); + # print Dumper($defs); print "\nHook '$hook' is used in ", scalar(@$defs), " event definition(s):\n"; $Data::Dumper::Indent = 1; +my $remaining = $cs->first_defined('max-batch-size'); foreach my $def (@$defs) { + last if $remaining == 0; printf "%3s - '%s'\n", $def->id, $def->name; - my $events = $e->search_action_trigger_event([ - {event_def => $def->id}, - {flesh => 1, flesh_fields => { atev => ['template_output'] }} - ]); + + # give me all completed JEDI events that link to purchase_orders + # that have not already been delivered to the vendor + my $query = { + select => {atev => ['id']}, + from => 'atev', + where => { + event_def => $def->id, + state => 'complete', + target => { + in => { + select => {acqpo => ['id']}, + from => 'acqpo', + where => { + id => { + 'not in' => { + select => {acqedim => ['purchase_order']}, + from => 'acqedim', + where => {purchase_order => {'!=' => undef}} + } + } + } + } + } + }, + order_by => {atev => 'add_time'} + }; + + $query->{limit} = $remaining if $remaining > 0; + + my $events = $e->json_query($query); + $remaining -= scalar(@$events); + print "Event definition ", $def->id, " has ", scalar(@$events), " event(s)\n"; foreach (@$events) { + my $event = $e->retrieve_action_trigger_event($_); my $message = Fieldmapper::acq::edi_message->new; $message->create_time('NOW'); # will need this later when we try to update from the object - print "Event ", $_->id, " targets PO ", $_->target, ":\n"; # target is an opaque identifier, so we cannot flesh it - print Dumper($_), "\n"; + print "Event ", $event->id, " targets PO ", $event->target, ":\n"; # target is an opaque identifier, so we cannot flesh it + print Dumper($event), "\n"; my $target = $e->retrieve_acq_purchase_order([ # instead we retrieve it separately - $_->target, { + $event->target, { flesh => 2, flesh_fields => { acqpo => ['provider'], @@ -71,12 +111,14 @@ foreach my $def (@$defs) { } ]); + $message->purchase_order($target->id); + $debug and print "Target: ", Dumper($target), "\n"; my $logstr = sprintf "provider %s (%s)", $target->provider->id, $target->provider->name; unless ($target->provider->edi_default and $message->account($target->provider->edi_default->id)) { printf STDERR "ERROR: No edi_default account found for $logstr. File will not be sent!\n"; } - $message->jedi($_->template_output()->data); + $message->jedi($event->template_output()->data); print "\ntarget->provider->edi_default->id: ", $target->provider->edi_default->id, "\n"; print "\nNow calling attempt_translation\n\n"; unless (OpenILS::Application::Acq::EDI->attempt_translation($message, 1)) { -- 2.11.0