Odyssey
sCalendarNotify.php
1 #!/usr/local/bin/php
2 <?php
3 
4  // ** SET HOMECU FLAGS
5  $serviceMinimal = true;
6  $serviceShowInfo = false;
7  $serviceLoadMenu = false;
8  $serviceShowMenu = false;
9  $serviceLoadCuInfo = false;
10 
11  // ** INCLUDE MAIN GLOBAL SCRIPT -- Handles security / global variable values
12  // hcuService will be returning a status object: e.g. ["homecuErrors":{[{"message":"message1"}...{"message":"messageN"}}]
13  require_once('/var/www/html/banking/library/hcuService.i');
14 
15  // All the functions are here.
16  require_once("/var/www/html/system/library/sCalendarNotify.i" );
17 
18  $dbhost = getenv('DATABASE_HOST');
19  $dbport = getenv('DATABASE_PORT');
20  $dbname = getenv('DATABASE_NAME');
21  $dbuser = getenv('DATABASE_USER');
22  $dbpasswd = getenv('DATABASE_PASSWORD');
23  $dbplat = 'Postgres';
24  $pReplyEmail = "nobody\@homecu.net"; // used for the From when mailing CU
25 
26  // set up the logger
27  $logger = $HB_ENV["SYSENV"]["logger"];
28 
29  // Get options
30  // d -- run the file with a particular date. Format is YYYY-mm-dd. If it isn't set, then it runs with the current date (default.)
31  // help -- prints out the syntax structure and then exits.
32  $allowedOptions = "d::";
33  $longOptions = array("help", "dryrun");
34  $commandOptions = getopt( $allowedOptions, $longOptions );
35 
36  date_default_timezone_set ( "America/Denver" );
37 
38  $results = VerifyOpts($commandOptions);
39  if ($results["status"] !== "000") {
40  if ($results["status"] >= 0) {
41  $logger->error($results["error"]);
42  }
43  $logger->info("Usage: {$argv[0]} [-d\"YYYY-mm-dd\"]");
44  exit;
45  } else {
46  extract($results["opts"]);
47  }
48 
49  $today = date( "D M d, Y G:i", strtotime($date));
50  $currentDate = date ( "Y-m-d", strtotime($date)); // date formatted like sql date field
51  $processName = "Calendar Notify Process";
52  $currentYear = explode("-", $date);
53  $currentYear = intval($currentYear[0]);
54  $nextYear = $currentYear + 1;
55 
56  try {
57  $logger->info("$processName: $today $currentDate" );
58 
59  if ($dryrun) {
60  $logger->info("$processName is in dry run mode. The database will not be updated and no emails will be sent out.");
61  }
62 
63  $logger->info("$processName: Removing old calendar records.");
64  if (!$dryrun) {
65  $results = RemoveOldCalendars($dbh, $currentYear);
66  if ($results["status"] !== "000") {
67  throw new exception ("Old calendars could not be deleted.", 3);
68  }
69  }
70 
71  $results = DoICare($date);
72  if ($results["status"] !== "000") {
73  throw new exception ("CU List could not be created.", 2);
74  }
75  if (!$results["doICare"]) {
76  $msg = "$processName: Exited due to being too early in the year.";
77  $logger->info($msg);
78  } else {
79 
80  $fullResults = RetrieveCUEmailList($dbh, $currentYear, $nextYear);
81  if ($fullResults["status"] !== "000") {
82  throw new exception ("CU List could not be created.", 1);
83  }
84 
85  foreach($fullResults["data"]["lastNotifyList"] as $lastNotifyRow) {
86  $cu = trim($lastNotifyRow["cu"]);
87  $emails = $lastNotifyRow["emails"];
88  if (count($emails) == 0) { // This is an error but continue on.
89  $msg = "$processName: $cu does not have emails set up.";
90  $logger->error($msg);
91  }
92 
93  $results = ShouldISendEmail($currentDate, $lastNotifyRow["lastnotify"]);
94  if ($results["status"] !== "000") {
95  throw new exception ("$processName: Email logic is incorrect.", 3);
96  }
97 
98  if ($results["shouldISendEmail"]) {
99  $emailSent = false;
100  $msg = "$processName: Sending emails for $cu...";
101  $logger->info($msg);
102  if (!$dryrun) {
103  $results = SendCuEmail($cu, $emails, $nextYear);
104  if ($results["status"] !== "000") { // This is an error but continue on.
105  $logger->error("$processName: Email to $email for $cu failed.");
106  } else {
107  $emailSent = true;
108  }
109  }
110 
111  $logger->info("$processName: Updating calendar record for $cu.");
112  if (!$dryrun) {
113  $results = UpdateCuCalendar($dbh, $cu, $nextYear, $date);
114  if ($results["status"] !== "000") {
115  $logger->error("$processName: Failed updating calendar record for $cu.");
116  }
117  }
118  }
119  }
120  }
121 
122  $logger->info("$processName: Script was successful.");
123  exit;
124  } catch (exception $e) {
125 
126  $logger->error("$processName failed: " . $e->getMessage());
127  exit ($e->getCode());
128  }
129