Add ability to delete account
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 28 Aug 2019 18:32:51 +0000 (14:32 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 28 Aug 2019 18:32:51 +0000 (14:32 -0400)
Give ability to delete an account.

classes/billingSupport.php
classes/data/dataAccounts.php
models/admin/ajax/ajaxBillingAccounts.php [new file with mode: 0644]
models/admin/billing/accounts.php
setup/standardTemplateParams.php
setup/validActions.php
views/admin/billing/accounts.html

index 80f361e..48913d9 100644 (file)
@@ -1141,7 +1141,7 @@ class GlmBillingSupport
                 'recurrence'     => $new_member_invoice_type['recurrence'],
             )
         );
-        echo '<pre>$invoice_id: ' . print_r( $invoice_id, true ) . '</pre>';
+        // echo '<pre>$invoice_id: ' . print_r( $invoice_id, true ) . '</pre>';
         if ( $invoice_id ) {
             if ( isset( $employees ) && is_array( $employees ) ) {
                 // Add line items for each employee.
index 9a254f8..c3326c8 100644 (file)
@@ -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 (file)
index 0000000..0a9ccf9
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * PDF Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @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;
+
+
+}
index 49073b7..bba0a31 100644 (file)
@@ -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(
index 7f09750..5cc75f5 100644 (file)
@@ -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' ) ) {
index ddc0edb..f1b56f0 100644 (file)
@@ -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,
         ),
index 25821a3..f018a31 100644 (file)
@@ -18,7 +18,7 @@
 {* Search Form *}
 {include file='admin/billing/accountSearchForm.html'}
 
-<p>Total found: <span class="label">{$totalAccounts}</span></p>
+<p>Total found: <span class="label" id="glm-billing-account-total" data-total="{$totalAccounts}">{$totalAccounts}</span></p>
 
 {if $paging}
     <input type="Submit" name="pageSelect" value="Previous {$limit} Accounts" class="button button-secondary glm-button"{if !$prevStart} disabled{/if}>
@@ -41,7 +41,7 @@
     <tbody>
         {if $haveAccounts}
             {foreach $accounts as $t}
-                <tr class="glm-account-row{if $t@iteration is div by 2} alternate{/if}" data-id="{$t.id}">
+                <tr class="glm-account-row{if $t@iteration is div by 2} alternate{/if}" data-id="{$t.id}" id="glm-account-{$t.id}">
                     <td> {$t.id} </td>
                     <td> <b id="account-ref-dest-{$t.id}">{$t.ref_name}</b> </td>
                     {if $settings.allow_employees}
                             <a class="account-member-statements" data-member="{$t.ref_dest}" href="{$adminUrl}?page=glm-members-admin-menu-member&glm_action=billing&member={$t.ref_dest}">View Statements</a> |
                         </span>
                         <span class="account-dashboard-link">
-                            <a class="account-billing" href="{$adminUrl}?page=glm-members-admin-menu-member&glm_action=billing&option=account&member={$t.ref_dest}" data-member="{$t.ref_dest}">Billing Info</a>
+                            <a class="account-billing" href="{$adminUrl}?page=glm-members-admin-menu-member&glm_action=billing&option=account&member={$t.ref_dest}" data-member="{$t.ref_dest}">Billing Info</a> |
+                        </span>
+                        <span>
+                            <a class="account-delete" href="#" data-id="{$t.id}">Delete Account</a>
                         </span>
                         {if $t.boss.value}
                         <span class="account-dashboard-link">
             $( '#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();