From 492605a15cad20f7b8066eff4ae63f561bb116a8 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 20 Mar 2019 16:41:56 -0400 Subject: [PATCH] Re Sending email notifications from log page Log page has link to re-send notification. two new fields for notifications are type and type_id which help to re-create the send email notification call with same parameters for sending out the exact same email. (with invoice or payment data) --- classes/data/dataNotifications.php | 14 ++ classes/notifications.php | 55 ++++- index.php | 2 +- models/admin/ajax/invoices.php | 10 +- models/admin/billing/logs.php | 196 ++++++++++-------- models/admin/billing/payments.php | 7 +- ...0.0.34.sql => create_database_V0.0.35.sql} | 2 + setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.35.sql | 14 ++ views/admin/billing/editPayment.html | 18 +- views/admin/billing/logs.html | 93 ++++++--- .../admin/settings/editNotificationType.html | 8 +- 12 files changed, 279 insertions(+), 141 deletions(-) rename setup/databaseScripts/{create_database_V0.0.34.sql => create_database_V0.0.35.sql} (99%) create mode 100644 setup/databaseScripts/update_database_V0.0.35.sql diff --git a/classes/data/dataNotifications.php b/classes/data/dataNotifications.php index c24fea0..64ef542 100644 --- a/classes/data/dataNotifications.php +++ b/classes/data/dataNotifications.php @@ -169,6 +169,20 @@ class GlmDataNotifications extends GlmDataAbstract 'use' => 'a', ), + // type + 'type' => array( + 'field' => 'type', + 'type' => 'integer', + 'use' => 'a', + ), + + // type id + 'type_id' => array( + 'field' => 'type_id', + 'type' => 'integer', + 'use' => 'a', + ), + ); diff --git a/classes/notifications.php b/classes/notifications.php index 51a512e..5ac1743 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -200,9 +200,6 @@ class GlmNotifications wp_mail( $to_email, $subject, $message, $header ); } else { wp_mail( $to_email, $subject, $message, $header, $attachments ); - // wp_mail( 'steve@localhost', $subject, $message, $header, $attachments ); - // Remove the temp file now - // unlink( $fileName ); } // remove the filter to avoid conflicts @@ -217,7 +214,13 @@ class GlmNotifications 'message' => $message, 'email_sent' => $to_email, ); - $this->recordNotification( $notice_data ); + $type = 0; + $type_id = 0; + if ( isset( $data['type'] ) && $data['type'] == $this->config['transaction_numb']['Invoice'] ) { + $type = $data['type']; + $type_id = $data['type_id']; + } + $this->recordNotification( $notice_data, $type, $type_id ); } /** @@ -259,7 +262,7 @@ class GlmNotifications * @access public * @return void */ - public function recordNotification( $notification ) + public function recordNotification( $notification, $type = 0, $type_id = 0 ) { if ( !$notification['notification_type'] || !$notification['account'] || !$notification['from_replyto'] || !$notification['subject'] @@ -277,6 +280,8 @@ class GlmNotifications 'message' => $notification['message'], 'date_sent' => date('Y-m-d H:i;s'), 'email_sent' => $notification['email_sent'], + 'type' => $type, + 'type_id' => $type_id, ), array( '%d', // notification_type @@ -286,11 +291,51 @@ class GlmNotifications '%s', // message '%s', // date_sent '%s', // email_sent + '%d', // type + '%d', // type_id ) ); return true; } + public function resendNotificationById( $id ) + { + $notification = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notifications + WHERE id = %d", + $id + ), + ARRAY_A + ); + $notify_type_id = $notification['notification_type']; + $account = $notification['account']; + $invoice_total = 0; + $payment_total = 0; + // Get amounts + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + switch ( $notification['type'] ) { + case $this->config['transaction_numb']['Invoice']: + $invoice = $BillingSupport->getInvoiceById( $notification['type_id'] ); + $invoice_total = $invoice['balance']; + break; + case $this->config['transaction_numb']['Payment']: + $payment = $BillingSupport->getInvoiceById( $notification['type_id'] ); + $payment_total = $payment['amount']; + break; + } + $data = array( + 'type' => $notification['type'], + 'type_id' => $notification['type_id'], + 'account' => $notification['account'], + 'amount' => $invoice_total, + 'payment' => $payment_total, + ); + $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + $Notifications->sendEmailNotification( $notify_type_id, $account, $data ); + } + /** * getNotificationsByType * diff --git a/index.php b/index.php index a490a00..9f53a5e 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '1.0.25'); -define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.34'); +define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.35'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/models/admin/ajax/invoices.php b/models/admin/ajax/invoices.php index d586de3..16b0e99 100644 --- a/models/admin/ajax/invoices.php +++ b/models/admin/ajax/invoices.php @@ -76,22 +76,28 @@ class GlmMembersAdmin_ajax_invoices extends GlmDataInvoices public function modelAction( $actionData = false ) { $return = false; + $option = false; - $option = filter_var( $_REQUEST['option'], FILTER_SANITIZE_STRING ); + if ( isset( $_REQUEST['option'] ) ) { + $option = filter_var( $_REQUEST['option'], FILTER_SANITIZE_STRING ); + } trigger_error( print_r( $_REQUEST, E_USER_NOTICE ) ); switch ( $option ) { case 'list': default: $account = filter_var( $_REQUEST['account'], FILTER_VALIDATE_INT ); + $this->line_items_post = true; $invoices = $this->getList( "T.paid <> true AND T.account = $account" ); + $this->line_items_post = false; $return = array(); foreach ( $invoices as $invoice ) { $return[] = array( 'id' => $invoice['id'], 'transaction_time' => $invoice['transaction_time'], 'balance' => $invoice['balance'], - 'due_date' => $invoice['due_date'] + 'due_date' => $invoice['due_date'], + 'line_items' => $invoice['line_items'] ); } diff --git a/models/admin/billing/logs.php b/models/admin/billing/logs.php index 425ce34..8263f93 100644 --- a/models/admin/billing/logs.php +++ b/models/admin/billing/logs.php @@ -16,6 +16,8 @@ // Load Contacts data class require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataNotifications.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; class GlmMembersAdmin_billing_logs extends GlmDataNotifications { @@ -82,20 +84,22 @@ class GlmMembersAdmin_billing_logs extends GlmDataNotifications public function modelAction($actionData = false) { - $option = 'list'; - $haveLogs = false; - $view = 'logs'; - $fromDate = false; - $toDate = false; - $numbDisplayed = false; - $lastDisplayed = false; - $paging = true; - $prevStart = false; - $nextStart = false; - $start = 1; - $limit = 20; // Set to the number of listings per page - $invTypes = array(); - $accounts = false; + $option = 'list'; + $haveLogs = false; + $view = 'logs'; + $fromDate = false; + $toDate = false; + $numbDisplayed = false; + $lastDisplayed = false; + $paging = true; + $prevStart = false; + $nextStart = false; + $start = 1; + $limit = 20; // Set to the number of listings per page + $invTypes = array(); + $accounts = false; + $emailSent = false; + $emailSentError = false; // Get any provided option if (isset($_REQUEST['option'])) { @@ -105,104 +109,114 @@ class GlmMembersAdmin_billing_logs extends GlmDataNotifications // Do selected option switch ($option) { + case 'resend': + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + if ( isset( $_REQUEST['notification_id'] ) && $notification_id = filter_var( $_REQUEST['notification_id'], FILTER_VALIDATE_INT ) ) { + + $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + $Notifications->resendNotificationById( $notification_id ); + $emailSent = true; + } else { + $emailSentError = true; + } + // pull the notification and re send it. + break; + case 'list': default: + break; - $where = 'true'; + } - // Check for paging - if ( isset( $_REQUEST['pageSelect'] ) ) { - $_SESSION['search']['pageSelect'] = $_REQUEST['pageSelect']; - } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['pageSelect'] ) ) { - unset( $_SESSION['search']['pageSelect'] ); - } - if ( isset( $_REQUEST['nextStart'] ) ) { - $_SESSION['search']['nextStart'] = $_REQUEST['nextStart']; - } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['nextStart'] ) ) { - unset( $_SESSION['search']['nextStart'] ); - } - if ( isset( $_REQUEST['prevStart'] ) ) { - $_SESSION['search']['prevStart'] = $_REQUEST['prevStart']; - } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['prevStart'] ) ) { - unset( $_SESSION['search']['prevStart'] ); - } + $where = 'true'; - // Check if we're doing paging - if (isset($_REQUEST['pageSelect'])) { - // If request is for Next - if ($_REQUEST['pageSelect'][0] == 'N') { - $newStart = $_REQUEST['nextStart'] - 0; + // Check for paging + if ( isset( $_REQUEST['pageSelect'] ) ) { + $_SESSION['search']['pageSelect'] = $_REQUEST['pageSelect']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['pageSelect'] ) ) { + unset( $_SESSION['search']['pageSelect'] ); + } + if ( isset( $_REQUEST['nextStart'] ) ) { + $_SESSION['search']['nextStart'] = $_REQUEST['nextStart']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['nextStart'] ) ) { + unset( $_SESSION['search']['nextStart'] ); + } + if ( isset( $_REQUEST['prevStart'] ) ) { + $_SESSION['search']['prevStart'] = $_REQUEST['prevStart']; + } else if ( isset( $_REQUEST['searched'] ) && !isset( $_REQUEST['prevStart'] ) ) { + unset( $_SESSION['search']['prevStart'] ); + } - // Otherwise it must be Previous - } else { - $newStart = $_REQUEST['prevStart'] - 0; - } + // Check if we're doing paging + if (isset($_REQUEST['pageSelect'])) { + // If request is for Next + if ($_REQUEST['pageSelect'][0] == 'N') { + $newStart = $_REQUEST['nextStart'] - 0; - if ($newStart > 0) { - $start = $newStart; - } + // Otherwise it must be Previous + } else { + $newStart = $_REQUEST['prevStart'] - 0; } - if( isset($_SESSION['search']['pageSelect']) ){ - // If request is for Next - if ($_SESSION['search']['pageSelect'][0] == 'N') { - $newStart = $_SESSION['search']['nextStart'] - 0; - - // Otherwise it must be Previous - } else { - $newStart = $_SESSION['search']['prevStart'] - 0; - } - if ($newStart > 0) { - $start = $newStart; - } + if ($newStart > 0) { + $start = $newStart; } + } - // Get the list of logs and determine number of logs in list - $orderBy = 'date_sent asc'; - $logsResult = $this->getList($where, $orderBy, true, 'id', $start, $limit); - // echo '
$logsResult: ' . print_r( $logsResult, true ) . '
'; + if( isset($_SESSION['search']['pageSelect']) ){ + // If request is for Next + if ($_SESSION['search']['pageSelect'][0] == 'N') { + $newStart = $_SESSION['search']['nextStart'] - 0; - // Get paging results - $numbDisplayed = $logsResult['returned']; - $lastDisplayed = $logsResult['last']; - if ($start == 1) { - $prevStart = false; + // Otherwise it must be Previous } else { - $prevStart = $start - $limit; - if ($start < 1) { - $start = 1; - } + $newStart = $_SESSION['search']['prevStart'] - 0; } - if ($logsResult['returned'] == $limit) { - $nextStart = $start + $limit; + if ($newStart > 0) { + $start = $newStart; } + } - // since we're doing paging, we have to break out just the logs data - $logs = $logsResult['list']; - if (count($logs)>0) { - $haveLogs = true; + // Get the list of logs and determine number of logs in list + $orderBy = 'date_sent desc'; + $logsResult = $this->getList($where, $orderBy, true, 'id', $start, $limit); + + // Get paging results + $numbDisplayed = $logsResult['returned']; + $lastDisplayed = $logsResult['last']; + if ($start == 1) { + $prevStart = false; + } else { + $prevStart = $start - $limit; + if ($start < 1) { + $start = 1; } - unset($logsResult); - - // echo '
$logs: ' . print_r( $logs, true ) . '
'; - - break; - + } + if ($logsResult['returned'] == $limit) { + $nextStart = $start + $limit; } + // since we're doing paging, we have to break out just the logs data + $logs = $logsResult['list']; + if (count($logs)>0) { + $haveLogs = true; + } + unset($logsResult); $templateData = array( - 'option' => $option, - 'logs' => $logs, - 'haveLogs' => $haveLogs, - 'numbDisplayed' => $numbDisplayed, - 'lastDisplayed' => $lastDisplayed, - 'paging' => $paging, - 'prevStart' => $prevStart, - 'nextStart' => $nextStart, - 'start' => $start = 1, - 'limit' => $limit, - 'accounts' => $accounts, + 'option' => $option, + 'logs' => $logs, + 'haveLogs' => $haveLogs, + 'numbDisplayed' => $numbDisplayed, + 'lastDisplayed' => $lastDisplayed, + 'paging' => $paging, + 'prevStart' => $prevStart, + 'nextStart' => $nextStart, + 'start' => $start = 1, + 'limit' => $limit, + 'accounts' => $accounts, + 'emailSent' => $emailSent, + 'emailSentError' => $emailSentError, ); // Return status, any suggested view, and any data to controller diff --git a/models/admin/billing/payments.php b/models/admin/billing/payments.php index f85d884..9a6041b 100644 --- a/models/admin/billing/payments.php +++ b/models/admin/billing/payments.php @@ -135,9 +135,10 @@ class GlmMembersAdmin_billing_payments extends GlmDataPayments // Get only accounts that have invoices $accounts = $Accounts->getSimpleAccountList( - "T.id IN ( SELECT DISTINCT account - FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices - WHERE paid <> true )" + "T.id IN ( + SELECT DISTINCT account + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE paid <> true )" ); break; diff --git a/setup/databaseScripts/create_database_V0.0.34.sql b/setup/databaseScripts/create_database_V0.0.35.sql similarity index 99% rename from setup/databaseScripts/create_database_V0.0.34.sql rename to setup/databaseScripts/create_database_V0.0.35.sql index b4027f9..2e3c361 100644 --- a/setup/databaseScripts/create_database_V0.0.34.sql +++ b/setup/databaseScripts/create_database_V0.0.35.sql @@ -188,6 +188,8 @@ CREATE TABLE {prefix}notifications ( message TEXT NOT NULL, -- message date_sent DATETIME NOT NULL, -- Date the notice was sent email_sent TINYTEXT NOT NULL, -- email used + type INT NULL, -- Invoice Type + type_id INT NULL, -- type id (invoice or payment) PRIMARY KEY (id) ); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index df2274b..3c80121 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -48,5 +48,6 @@ $glmMembersBillingDbVersions = array( '0.0.32' => array('version' => '0.0.32', 'tables' => 15, 'date' => '12/26/2018'), '0.0.33' => array('version' => '0.0.33', 'tables' => 15, 'date' => '02/19/2019'), '0.0.34' => array('version' => '0.0.34', 'tables' => 16, 'date' => '03/19/2019'), + '0.0.35' => array('version' => '0.0.35', 'tables' => 16, 'date' => '03/20/2019'), ); diff --git a/setup/databaseScripts/update_database_V0.0.35.sql b/setup/databaseScripts/update_database_V0.0.35.sql new file mode 100644 index 0000000..cd56fc3 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.35.sql @@ -0,0 +1,14 @@ +-- Gaslight Media Billing Database +-- File Created: 3/19/2019 +-- Database Version: 0.0.35 +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + +-- Update the notification table +ALTER TABLE {prefix}notifications ADD type INT NULL; -- Invoice Type + +---- + +ALTER TABLE {prefix}notifications ADD type_id INT NULL; -- type id (invoice or payment) + diff --git a/views/admin/billing/editPayment.html b/views/admin/billing/editPayment.html index b280bc8..4e2a90a 100644 --- a/views/admin/billing/editPayment.html +++ b/views/admin/billing/editPayment.html @@ -74,9 +74,19 @@ jQuery(document).ready(function($){ // Clear the invoices $('#glm-payment-invoices').html(''); for ( var i = 0; i < invoices.length; i++ ) { - $('#glm-payment-invoices').append( '
' ); + var invoice = invoices[i]; + var rowHtml = '
'; + $('#glm-payment-invoices').append( rowHtml ); } } @@ -112,7 +122,7 @@ jQuery(document).ready(function($){ }, success: function( results ) { updateInvoiceList( results ); - //console.log( results ); + console.log( results ); }, }); }, diff --git a/views/admin/billing/logs.html b/views/admin/billing/logs.html index 482d07f..659d38e 100644 --- a/views/admin/billing/logs.html +++ b/views/admin/billing/logs.html @@ -3,41 +3,72 @@

Notification Logs

- - - - - - - - - - - - {if $haveLogs} - {assign var="i" value="0"} - {foreach $logs as $t} - {if $i++ is odd by 1} - - {else} - - {/if} - - - - - - - {/foreach} - {else} - - {/if} - -
IDDate SentSubjectMember NameEmail
{$t.id} {$t.date_sent.date} {$t.subject} {$t.member_name} {$t.email_sent}
(no Invoice Types listed)
+{if $emailSent}Email Sent{/if} +{if $emailSentError}There was an error sending out the email{/if} + +
+ +
+ + + + + + + + {if $paging} + + + {/if} +
+ + + + + + + + + + + + + + {if $haveLogs} + {assign var="i" value="0"} + {foreach $logs as $t} + + + + + + + + + {/foreach} + {else} + + {/if} + +
IDDate SentSubjectMember NameEmail 
{$t.id} {$t.date_sent.date} {$t.subject} {$t.member_name} {$t.email_sent} + {if $t.type_id} + Re Send + {/if} +
(no Invoice Types listed)
+ + {if $paging} + + + {/if} + +
diff --git a/views/admin/settings/editNotificationType.html b/views/admin/settings/editNotificationType.html index 9512be7..a566710 100644 --- a/views/admin/settings/editNotificationType.html +++ b/views/admin/settings/editNotificationType.html @@ -20,7 +20,7 @@ Name - + {if $notification.fieldFail.name}

{$notification.fieldFail.name}

{/if}
@@ -96,15 +96,15 @@ Subject - + {if $notification.fieldFail.subject}

{$notification.fieldFail.subject}

{/if}
- From + From (email address) - + {if $notification.fieldFail.from_header}

{$notification.fieldFail.from_header}

{/if}
-- 2.17.1