From: Steve Sutton Date: Mon, 12 Mar 2018 20:22:56 +0000 (-0400) Subject: Work on payments and invoices X-Git-Tag: v1.0.0^2~153 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=a93cb0838701d1897bc2dd6954af2c51be3b540a;p=WP-Plugins%2Fglm-member-db-billing.git Work on payments and invoices Payments: Updating the payment processing to pass through the invoice id so it marks that invoice as being paid. Set select to use the balance instead of total_amount. Invoices: Fix issue where after adding an invoice the next page would not work and was acting like edit invoice page. --- diff --git a/classes/billingSupport.php b/classes/billingSupport.php index d9b8a22..0f70398 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -88,46 +88,76 @@ class GlmBillingSupport * Also need to record the payment to the invoice_payments * table so we know which payment was applied to the invoice. * - * @param mixed $invoice_id Id of the Invoice record - * @param mixed $payment Total amount of the Payment + * @param int $payment_id Payment id + * @param int $account Total amount of the Payment + * @param float $payment Payment amount + * @param int $invoice_id Id of the Invoice record * * @access public * @return void */ - public function recordPayment( $payment_id, $account, $payment ) + public function recordPayment( $payment_id, $account, $payment, $invoice_id = null ) { // record into the transaction table $this->recordTransaction( $this->config['transaction_numb']['Payment'], $payment_id, $account, null, $payment ); - // Get the unpaid invoices - $invoices = $this->getUnPaidInvoicesByAccount( $account ); - // echo '
$invoices: ' . print_r( $invoices, true ) . '
'; - if ( $invoices && is_array( $invoices ) && count( $invoices ) > 0 ) { - foreach ( $invoices as $invoice ) { - $balance = (float)$invoice['balance']; - if ( $payment == $invoice['balance'] ) { - // Mark this as paid then - $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); - // Record the payment to the invoice_payments table - $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] ); - // $payment is used up so break from the foreach loop - break; - } else if ( $payment > $invoice['balance'] ) { - $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); - $payment -= $invoice['balance']; - // Record the payment to the invoice_payments table - $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] ); - } else if ( $invoice['balance'] > $payment ) { - // Update the balance of the invoice - $balance = (float)$balance - (float)$payment; - $this->updateInvoiceBalance( $invoice['id'], (float)$balance ); - // Record the payment to the invoice_payments table - $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment ); - // $payment is used up so break from the foreach loop - break; + // If there's an invoice_id + // If the payment amount is over then go through the other invoices. + if ( $invoice_id ) { + // Get the invoice + $invoice = $this->getInvoiceById( $invoice_id ); + $balance = (float)$invoice['balance']; + if ( $payment == $invoice['balance'] ) { + // Mark this as paid then + $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); + // Record the payment to the invoice_payments table + $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] ); + } else if ( $payment > $invoice['balance'] ) { + $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); + $payment -= $invoice['balance']; + } else if ( $invoice['balance'] > $payment ) { + // Update the balance of the invoice + $balance = (float)$balance - (float)$payment; + $this->updateInvoiceBalance( $invoice['id'], (float)$balance ); + // Record the payment to the invoice_payments table + $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment ); + } + + } + + if ( $payment ) { + + // Get the unpaid invoices + $invoices = $this->getUnPaidInvoicesByAccount( $account ); + if ( $invoices && is_array( $invoices ) && count( $invoices ) > 0 ) { + foreach ( $invoices as $invoice ) { + $balance = (float)$invoice['balance']; + if ( $payment == $invoice['balance'] ) { + // Mark this as paid then + $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); + // Record the payment to the invoice_payments table + $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] ); + // $payment is used up so break from the foreach loop + break; + } else if ( $payment > $invoice['balance'] ) { + $this->updateInvoiceBalance( $invoice['id'], (float)0.00 ); + $payment -= $invoice['balance']; + // Record the payment to the invoice_payments table + $this->recordInvoicePayment( $invoice['id'], $payment_id, $invoice['balance'] ); + } else if ( $invoice['balance'] > $payment ) { + // Update the balance of the invoice + $balance = (float)$balance - (float)$payment; + $this->updateInvoiceBalance( $invoice['id'], (float)$balance ); + // Record the payment to the invoice_payments table + $this->recordInvoicePayment( $invoice['id'], $payment_id, (float)$payment ); + // $payment is used up so break from the foreach loop + break; + } } } + } + } public function createPayment( $account, $payment ) diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index b8d42e1..eea8fe4 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -147,7 +147,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); $billingAccount = $Accounts->newEntry(); $accounts = $Accounts->getList( '', 'ref_name' ); - // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; // Need a list of members that don't have an account. $nonAccountMembers = $this->wpdb->get_results( @@ -159,7 +158,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices ORDER BY name", ARRAY_A ); - // echo '
$nonAccountMembers: ' . print_r( $nonAccountMembers, true ) . '
'; if ( isset( $invoiceTypes ) ) { foreach ( $invoiceTypes as $invoiceType ) { @@ -226,6 +224,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } } } + $invoiceAdded = true; // Here we're going to generate the invoice total. $totals = $this->generateInvoiceTotal( $this->invoice_id ); // Now we have a total for the invoice we can record the transaction @@ -235,9 +234,29 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices // Set the view file to editInvoice $view = 'editInvoice'; - $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); - $invoiceTypes = $InvoiceTypesObj->getList(); - $invoiceTypes = $InvoiceTypesObj->sortParentChild($invoiceTypes); + + // Reset this form to add a new one again + unset( $invoices ); + $this->invoice_id = false; + $invoices = $this->newEntry(); + $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); + $invoiceTypes = $InvoiceTypesObj->getList(); + $invoiceTypes = $InvoiceTypesObj->sortParentChild($invoiceTypes); + // Need to get the accounts + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $billingAccount = $Accounts->newEntry(); + $accounts = $Accounts->getList( '', 'ref_name' ); + + // Need a list of members that don't have an account. + $nonAccountMembers = $this->wpdb->get_results( + "SELECT id,name + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE id NOT IN ( + SELECT distinct ref_dest + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts ) + ORDER BY name", + ARRAY_A + ); if ( isset( $invoiceTypes ) ) { foreach ( $invoiceTypes as $invoiceType ) { $invTypes[$invoiceType['id']] = array( diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php index ea45795..900e8de 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -117,6 +117,8 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $management = false; $messages = array(); $invoices = false; + $paymentSuccess = false; + $paymentError = false; // For lockedToMember. $lockedToMember = false; @@ -340,6 +342,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling } if (count($messages) == 0) { + $paymentSuccess = true; // Determine the directory of the payment processor to load and instatiate it. if ($ccProcessor && isset($this->config['proc_dir'][$ccProcessor])) { @@ -373,7 +376,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling // Create new payment. $payment_id = $BillingSupport->createPayment( $paymentData['account_id'], $paymentData['amount'] ); // Record the payment. - $BillingSupport->recordPayment( $payment_id, $paymentData['account_id'], $paymentData['amount'] ); + $BillingSupport->recordPayment( $payment_id, $paymentData['account_id'], $paymentData['amount'], $paymentData['invoice_id'] ); $messages[] = $ccResult['statusText']; $messages[] = $ccResult['description']; @@ -381,6 +384,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling } } else { + $paymentError = true; // Load DataClass for accounts. require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); @@ -501,6 +505,8 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling 'management' => $management, 'messages' => $messages, 'invoices' => $invoices, + 'paymentSuccess' => $paymentSuccess, + 'paymentError' => $paymentError, ); // Return status, any suggested view, and any data to controller. diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index a0b90ed..8e09e4c 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -2,10 +2,10 @@ {include file='admin/billing/subHeader.html'} -{if $invoiceUpdated}Notification Updated{/if} -{if $invoiceUpdateError}Notification Update Error{/if} -{if $invoiceInsertError}Notification Insert Error{/if} -{if $invoiceAdded}Notification Added{/if} +{if $invoiceUpdated}Invoice Updated{/if} +{if $invoiceUpdateError}Invoice Update Error{/if} +{if $invoiceInsertError}Invoice Insert Error{/if} +{if $invoiceAdded}Invoice Added{/if}
@@ -472,7 +472,7 @@ jQuery(document).ready(function($){ // Close the dialog newAccountDialog.dialog( 'close' ); } else { - console.log( 'return', msg ); + //console.log( 'return', msg ); } }); } diff --git a/views/admin/billing/makePayment.html b/views/admin/billing/makePayment.html index e578b70..0977c5c 100644 --- a/views/admin/billing/makePayment.html +++ b/views/admin/billing/makePayment.html @@ -1,14 +1,18 @@ {include file='admin/member/header.html'} {include file='admin/billing/memberBillingSubHeader.html'} -{if $messages} - {foreach $messages as $message} -
{$message}
- {/foreach} -{/if} +
+ + {if $paymentSuccess}Payment Completed{/if} + {if $paymentError}Error With Payment{/if} + + {if $messages} + {foreach $messages as $message} +
{$message}
+ {/foreach} + {/if} -
@@ -31,13 +35,16 @@
Select Invoice
- {if $invoices} {foreach $invoices as $invoice} + data-amount="{$invoice.balance}" + {if isset($smarty.request.invoice_id) && $smarty.request.invoice_id == $invoice.id} selected{/if}> + ${$invoice.balance} Due {$invoice.due_date} + {/foreach} {/if} @@ -50,6 +57,7 @@
+ + + {include file='admin/footer.html'} diff --git a/views/admin/billing/paymentProcess.html b/views/admin/billing/paymentProcess.html index ed62455..c201286 100644 --- a/views/admin/billing/paymentProcess.html +++ b/views/admin/billing/paymentProcess.html @@ -1,11 +1,21 @@ {include file='admin/member/header.html'} {include file='admin/billing/memberBillingSubHeader.html'} +{if $paymentSuccess}Payment Completed{/if} + {if $messages} {foreach $messages as $message} - {$message}
+
{$message}
{/foreach} {/if} + {include file='admin/footer.html'} diff --git a/views/admin/settings/invoiceTypes.html b/views/admin/settings/invoiceTypes.html index 40c3c25..9c38b1b 100644 --- a/views/admin/settings/invoiceTypes.html +++ b/views/admin/settings/invoiceTypes.html @@ -210,7 +210,7 @@ jQuery(document).ready(function($) { var invoiceRecurring = $(this).data('invoice-recurring'); var invoiceRecurrence = $(this).data('invoice-recurrence'); - console.log( 'invoiceRecurring: ', invoiceRecurring ); + //console.log( 'invoiceRecurring: ', invoiceRecurring ); // Set the values of the edit form for the selected invoiceType $('#edit-id').val( invoiceID );