Update front end renewal form.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 15 May 2018 17:59:32 +0000 (13:59 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 15 May 2018 17:59:32 +0000 (13:59 -0400)
If the user has unpaid invoices for their account remove them before
creating another one.

models/front/billing/renew.php

index 6065947..f0a3d95 100644 (file)
@@ -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 ) {