From 206c350d41aee6e21c860e33453f12e5a9df423f Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 15 May 2018 13:59:32 -0400 Subject: [PATCH] Update front end renewal form. If the user has unpaid invoices for their account remove them before creating another one. --- models/front/billing/renew.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/models/front/billing/renew.php b/models/front/billing/renew.php index 6065947..f0a3d95 100644 --- a/models/front/billing/renew.php +++ b/models/front/billing/renew.php @@ -236,6 +236,26 @@ class GlmMembersFront_billing_renew // extends GlmDataBilling $error = true; } + // Before we create an invoice for this member see if they already have one. + // If we find one then delete it. + $old_invoice_ids = $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE paid <> true + AND renewal = true + AND account = %d", + $accountID + ), + ARRAY_A + ); + if ( isset( $old_invoice_ids ) && $old_invoice_ids ) { + foreach ( $old_invoice_ids as $old_invoices ) { + if ( $inv_id = filter_var( $old_invoices['id'], FILTER_VALIDATE_INT ) ) { + $BillingSupport->removeInvoiceById( $inv_id ); + } + } + } // Create the invoice for this member. $invoice_id = $BillingSupport->createMemberInvoiceWithEmployees( $invoice_data ); if ( !$invoice_id ) { -- 2.17.1