From eecd61b17e77a20ee4e20d3da763e21f3ff8fde3 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 28 Aug 2019 14:32:51 -0400 Subject: [PATCH] Add ability to delete account Give ability to delete an account. --- classes/billingSupport.php | 2 +- classes/data/dataAccounts.php | 14 +++++ models/admin/ajax/ajaxBillingAccounts.php | 65 +++++++++++++++++++++++ models/admin/billing/accounts.php | 49 +++++------------ setup/standardTemplateParams.php | 1 - setup/validActions.php | 1 + views/admin/billing/accounts.html | 40 ++++++++++++-- 7 files changed, 130 insertions(+), 42 deletions(-) create mode 100644 models/admin/ajax/ajaxBillingAccounts.php diff --git a/classes/billingSupport.php b/classes/billingSupport.php index 80f361e..48913d9 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -1141,7 +1141,7 @@ class GlmBillingSupport 'recurrence' => $new_member_invoice_type['recurrence'], ) ); - echo '
$invoice_id: ' . print_r( $invoice_id, true ) . '
'; + // echo '
$invoice_id: ' . print_r( $invoice_id, true ) . '
'; if ( $invoice_id ) { if ( isset( $employees ) && is_array( $employees ) ) { // Add line items for each employee. diff --git a/classes/data/dataAccounts.php b/classes/data/dataAccounts.php index 9a254f8..c3326c8 100644 --- a/classes/data/dataAccounts.php +++ b/classes/data/dataAccounts.php @@ -479,4 +479,18 @@ class GlmDataAccounts extends GlmDataAbstract return $accountList; } + + public function deleteAccount( $id ) + { + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoice_payments WHERE invoice IN + (SELECT id FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices WHERE account = $id)" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "line_items WHERE account = $id" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices WHERE account = $id" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "payments WHERE account = $id" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "transactions WHERE account = $id" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notifications WHERE account = $id" ); + $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_queue WHERE account = $id" ); + $ret = $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts WHERE id = $id" ); + return $ret; + } } diff --git a/models/admin/ajax/ajaxBillingAccounts.php b/models/admin/ajax/ajaxBillingAccounts.php new file mode 100644 index 0000000..0a9ccf9 --- /dev/null +++ b/models/admin/ajax/ajaxBillingAccounts.php @@ -0,0 +1,65 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/models/admin/billing/accounts.php'; + +/** + * Steve Note + * + * You can get to this using the following URL. + * + * + {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=runQueue + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +/** + * This class handles the work of creating new invoices based on. + * 1) Member Type of member matching a paid invoiceType + * 2) Member renewal date past + * 3) Member has Billing Account + * 4) Member has no active Invoice + * 5) Renewal date is within the next 30 Days + * + */ +class GlmMembersAdmin_ajax_ajaxBillingAccounts extends GlmMembersAdmin_billing_accounts +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public $ajaxSide = true; + + +} diff --git a/models/admin/billing/accounts.php b/models/admin/billing/accounts.php index 49073b7..bba0a31 100644 --- a/models/admin/billing/accounts.php +++ b/models/admin/billing/accounts.php @@ -287,15 +287,20 @@ class GlmMembersAdmin_billing_accounts extends GlmDataAccounts break; case 'delete': - // Need to remove any line items for the invoice also - // $accounts = $this->deleteTransactions($this->account_id); - - if ( $accounts ) { - $invoiceDeleted = true; - } else { - $invoiceDeleteError = true; + $success = false; + // This is done by ajax call only + $account_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); + if ( $account_id ) { + // Remove the account and any related data + // Need to remove any line items for the invoice also + $success = $this->deleteAccount( $account_id ); } + header( 'Content-type:application/json;charset=utf-8', true ); + echo json_encode( [ 'success' => $success ], true ); + exit; + break; + case 'list': default: @@ -304,23 +309,6 @@ class GlmMembersAdmin_billing_accounts extends GlmDataAccounts $where_params = array( 'true' ); $sub_where_parts = array(); - // 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'] ); - // } - // Check if we're doing paging if (isset($_REQUEST['pageSelect'])) { // If request is for Next @@ -337,19 +325,6 @@ class GlmMembersAdmin_billing_accounts extends GlmDataAccounts } } - // 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; - // } - // } // Get any search parameters. $reg_options = array( 'options' => array( diff --git a/setup/standardTemplateParams.php b/setup/standardTemplateParams.php index 7f09750..5cc75f5 100644 --- a/setup/standardTemplateParams.php +++ b/setup/standardTemplateParams.php @@ -23,7 +23,6 @@ */ // Common to both admin and front-end -$smarty->templateAssign( 'glmPluginName', GLM_MEMBERS_BILLING_PLUGIN_NAME ); $smarty->templateAssign( 'pluginAssetsUrl', GLM_MEMBERS_BILLING_PLUGIN_BASE_URL . '/assets/' ); if ( current_user_can( 'glm_members_management' ) ) { diff --git a/setup/validActions.php b/setup/validActions.php index ddc0edb..f1b56f0 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -77,6 +77,7 @@ $glmMembersBillingAddOnValidActions = array( 'billingFlagExpiredUsers' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'billingFixActiveUsers' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'ajaxBillingInfo' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'ajaxBillingAccounts' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'ajaxBillingSettings' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'ajaxNotificationPreview' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), diff --git a/views/admin/billing/accounts.html b/views/admin/billing/accounts.html index 25821a3..f018a31 100644 --- a/views/admin/billing/accounts.html +++ b/views/admin/billing/accounts.html @@ -18,7 +18,7 @@ {* Search Form *} {include file='admin/billing/accountSearchForm.html'} -

Total found: {$totalAccounts}

+

Total found: {$totalAccounts}

{if $paging} @@ -41,7 +41,7 @@ {if $haveAccounts} {foreach $accounts as $t} - + {$t.id} {$t.ref_name} {if $settings.allow_employees} @@ -61,7 +61,10 @@ View Statements | - + | + + + {if $t.boss.value} @@ -132,6 +135,37 @@ $( '#account-container-' + accountHoverId ).addClass( 'hide-for-large' ); }); + $('.account-delete').on( 'click', function(e){ + e.preventDefault(); + var answer = confirm( 'This account will be deleted! Are you sure?' ); + if ( answer ) { + var id = $(this).data('id'); + $.ajax({ + url: '{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=ajaxBillingAccounts&option=delete&id=' + id, + cache: false, + dataType: 'json', + }).done(function(resp){ + //console.log(resp); + if ( resp.success ) { + //console.log( 'Success: Good', resp.success ); + $('#glm-account-' + id).remove(); + alert( 'Account ' + id + ' Deleted' ); + decreaseTotal(); + } else { + //console.log( 'Success: Bad', resp.success ); + } + }); + } + }); + + function decreaseTotal() { + var totalObj = $('#glm-billing-account-total'); + var total = parseInt(totalObj.data('total')); + total = total - 1; + totalObj.html( total ); + totalObj.data('total', total); + } + $('.account-employees').on( 'click', function(e){ e.preventDefault(); accountLinksEmployeesSelected(); -- 2.17.1