]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/set_pbx_holidays.pl
LP#1830394: remove console noise from the holdings editor
[working/Evergreen.git] / Open-ILS / src / support-scripts / set_pbx_holidays.pl
1 #!/usr/bin/perl
2
3 require "/openils/bin/oils_header.pl";
4
5 use strict;
6 use warnings;
7 use OpenILS::Utils::DateTime qw/clean_ISO8601/;
8 use OpenSRF::Utils::Logger qw/$logger/;
9 use OpenSRF::Utils::SettingsClient;
10
11 use RPC::XML;
12 use RPC::XML::Client;
13 use DateTime;
14 use Getopt::Std;
15
16 sub unixify {
17     my ($stringy_ts) = @_;
18     return (new DateTime::Format::ISO8601)->parse_datetime(
19         clean_ISO8601($stringy_ts)
20     )->epoch;
21 }
22
23 sub get_closed_dates {
24     my ($ou) = @_;
25     my $editor =  new OpenILS::Utils::CStoreEditor;
26
27     my $rows = $editor->json_query({
28         "select" => {"aoucd" => ["close_start", "close_end"]},
29         "from" => "aoucd",
30         "where" => {"org_unit" => $ou},
31         "order_by" => [{class => "aoucd", field => "close_start", direction => "desc"}]
32     });
33
34     if (!$rows) {
35         $logger->error("get_closed_dates json_query failed for ou $ou !");
36         my $textcode = $editor->die_event->{textcode};
37         $logger->error("get_closed_dates json_query die_event: $textcode");
38         die;
39     }
40
41     $editor->disconnect;
42
43     my $result = [];
44     foreach (@$rows) {
45         push @$result, [
46             unixify($_->{"close_start"}), unixify($_->{"close_end"})
47         ];
48     }
49
50     return $result;
51 }
52
53
54 #############################################################################
55 ### main
56
57 my $opts = {};
58 getopts('c:o:u:', $opts);
59
60 my ($ou, $url);
61
62 if (!($ou = int($opts->{o}))) {
63     die("no ou specified.\n$0 -o 123     # where 123 is org unit id");
64 }
65
66 osrf_connect($opts->{c} || $ENV{SRF_CORE} || "/openils/conf/opensrf_core.xml");
67
68 if (!($url = $opts->{u})) {
69     my $settings = OpenSRF::Utils::SettingsClient->new;
70     my $mediator_host = $settings->config_value(notifications => telephony => "host");
71     my $mediator_port = $settings->config_value(notifications => telephony => "port");
72
73     $url = "http://$mediator_host:$mediator_port/";
74 }
75
76 my $closed_dates = get_closed_dates($ou);
77 my $rpc_client = new RPC::XML::Client($url);
78 my $result = $rpc_client->simple_request("set_holidays", $closed_dates);
79
80 my $logmeth = "info";
81 if ($result < 0) {
82     $logmeth = "error";
83 } elsif ($result != @$closed_dates) {
84     $logmeth = "warn"
85 }
86
87 $logger->$logmeth(
88     "after set_holidays() for " . scalar(@$closed_dates) .
89     " dates, mediator returned $result"
90 );
91
92 0;