From: Steve Sutton Date: Wed, 19 Dec 2018 13:35:01 +0000 (-0500) Subject: Updates on Payments, invoices and Send Email screens. X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=528192450f0abc73eb50bd96807d02279c99028c;p=WP-Plugins%2Fglm-member-db-billing.git Updates on Payments, invoices and Send Email screens. Adding Send Emails screen to Invoicing. Adding link of member dashboard to payments screen. Adding Delete payments link in payments screen. Adding correct link in invoices screen if uptra payment form is set. --- diff --git a/classes/billingSupport.php b/classes/billingSupport.php index 9a68719..d2a7dfb 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -1532,7 +1532,7 @@ class GlmBillingSupport * @param int $invoice_id Id of invoice to delete. * * @access public - * @return int|false The number of invoices updated or false on error. + * @return int|false The number of invoices deleted or false on error. */ public function removeInvoiceById( $invoice_id ) { @@ -1554,6 +1554,63 @@ class GlmBillingSupport return $result; } + /** + * removePaymentById + * + * @param int $payment_id Id of the payment to delete. + * + * @access public + * @return int|false The number of payments deleted or false on error. + */ + public function removePaymentById( $payment_id ) + { + // Get the invoice number from invoice_payments first + $paymentInvoiceData = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT IP.id, IP.amount, I.amount_total, I.balance, IP.invoice + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoice_payments IP + LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices I ON (I.id = IP.invoice) + WHERE payment = %d", + $payment_id + ), + ARRAY_A + ); + if ( !empty( $paymentInvoiceData ) ) { + // Update the invoice with new balance. + $amount = (float)$paymentInvoiceData['amount']; + $amountTotal = (float)$paymentInvoiceData['amount_total']; + $balance = (float)$paymentInvoiceData['balance']; + $newBalance = ( ( $balance + $amount ) >= $amountTotal ) ? $amountTotal : $balance + $amount; + $paid = ( $newBalance == 0.00 ) ? 1 : 0; + + $this->wpdb->update( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices', + array( 'balance' => $newBalance, 'paid' => $paid ), + array( 'id' => $paymentInvoiceData['invoice'] ), + array( '%f' ), + array( '%d' ) + ); + $this->wpdb->delete( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoice_payments', + array( 'payment' => $payment_id ), + array( '%d' ) + ); + $result = $this->wpdb->delete( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'payments', + array( 'id' => $payment_id ), + array( '%d' ) + ); + $this->wpdb->delete( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'transactions', + array( 'type' => $this->config['transaction_numb']['Payment'], 'type_id' => $payment_id ), + array( '%d', '%d' ) + ); + return $result; + } else { + return false; + } + } + /** * updateAccountRenewalDate * diff --git a/classes/data/dataPayments.php b/classes/data/dataPayments.php index c563b25..54f3369 100644 --- a/classes/data/dataPayments.php +++ b/classes/data/dataPayments.php @@ -197,15 +197,22 @@ class GlmDataPayments extends GlmDataAbstract */ public function entryPostProcessing($r, $a) { - $r['member_name'] = $this->wpdb->get_var( + $member_data = $this->wpdb->get_row( $this->wpdb->prepare( - "SELECT A.ref_name + "SELECT A.ref_name,A.ref_dest FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "payments P LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A ON ( P.account = A.id ) WHERE P.id = %d", $r['id'] - ) + ), + ARRAY_A ); + if ( isset( $member_data ) && $member_data['ref_name'] ) { + $r['member_name'] = $member_data['ref_name']; + } + if ( isset( $member_data ) && $member_data['ref_dest'] ) { + $r['member_id'] = $member_data['ref_dest']; + } return $r; } diff --git a/classes/notifications.php b/classes/notifications.php index 5ecf746..8041e2f 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -63,7 +63,8 @@ class GlmNotifications * Send out the email notification based on type and the account. * * @param mixed $notification_type Id for notification type. - * @param mixed $account Id for the account. + * @param mixed $account_id Id for the account. + * @param mixed $data Additional Account Data for the email template * * @access public * @return void @@ -144,9 +145,27 @@ class GlmNotifications // Setup the invoice html $invoice_html = ''; - if ( isset( $data['type'] ) && $data['type'] == $this->config['transaction_numb']['Invoice'] ) { + // echo '
$billing_settings: ' . print_r( $this->config['settings'], true ) . '
'; + if ( isset( $data['type'] ) && $data['type'] == $this->config['transaction_numb']['Invoice'] && !$this->config['settings']['invoice_pdf_enabled'] ) { $invoice_html = $BillingSupport->viewInvoice( $data['type_id'] ); } + $fileName = false; + $attachments = false; + if ( isset( $data['type'] ) && $data['type'] == $this->config['transaction_numb']['Invoice'] && $this->config['settings']['invoice_pdf_enabled'] ) { + // Need to get the invoice and output to a file. + require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/lib/GlmPDFInvoice.php'; + $fullInvoice = $BillingSupport->getFullInvoiceData( $data['type_id'] ); + $pdf = new GlmPDFInvoice( $this->config, 'LETTER', 'portrait' ); + $fileData = $pdf->createPdf( array( $fullInvoice ), 'file' ); + $origName = tempnam( '/tmp', 'PDF' ); + $fp = fopen( $origName, 'w' ); + fwrite( $fp, $fileData ); + fclose( $fp ); + $fileName = $origName.'.pdf'; + rename( $origName, $fileName ); + + $attachments = array( $fileName ); + } $smarty->templateAssign( 'invoice_html', $invoice_html ); @@ -171,7 +190,14 @@ class GlmNotifications $header[] = 'Reply-To:' . $replyto; } - wp_mail( $to_email, $subject, $message, $header ); + if ( !$attachments ) { + wp_mail( $to_email, $subject, $message, $header ); + } else { + //wp_mail( $to_email, $subject, $message, $header, $attachments ); + wp_mail( 'steve@localhost', $subject, $message, $header, $attachments ); + // Remove the temp file now + // unlink( $fileName ); + } // remove the filter to avoid conflicts remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); diff --git a/config/plugin.ini b/config/plugin.ini index b11daca..b3ddf20 100644 --- a/config/plugin.ini +++ b/config/plugin.ini @@ -46,11 +46,13 @@ send_action[10] = "Create Invoice" send_action[20] = "Received Payment" send_action[30] = "New Member" send_action[40] = "New Member Admin Notice" +send_action[50] = "Send Emails" send_action_numb['Create Invoice'] = 10; send_action_numb['Received Payment'] = 20; send_action_numb['New Member'] = 30; send_action_numb['New Member Admin Notice'] = 40; +send_action_numb['Send Emails'] = 50; ; Send Date When send_date_when[10] = "Before" diff --git a/lib/GlmPDFInvoice.php b/lib/GlmPDFInvoice.php index 61f0949..4efe82b 100644 --- a/lib/GlmPDFInvoice.php +++ b/lib/GlmPDFInvoice.php @@ -66,7 +66,7 @@ class GlmPDFInvoice extends Cezpdf * * @return mixed */ - public function createPdf( $invoices ) + public function createPdf( $invoices, $outputStyle = 'stream' ) { $this->setupPages(); @@ -380,7 +380,11 @@ class GlmPDFInvoice extends Cezpdf } // Output PDF - $this->ezStream(); + if ( $outputStyle == 'stream' ) { + $this->ezStream(); + } else { + return $this->ezOutput(); + } } diff --git a/models/admin/billing/invoices.php b/models/admin/billing/invoices.php index 05bfa59..82b295b 100644 --- a/models/admin/billing/invoices.php +++ b/models/admin/billing/invoices.php @@ -352,24 +352,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } break; - // case 'edit': - // $invoices = $this->editEntry($this->invoice_id); - // - // // If we have a good invoices - // if ($invoices['status']) { - // $haveTransactions = true; - // } - // - // // If we're locked to a member as a contact user and the invoices member doesn't equal the contact member - // if ($lockedToMember && $invoices['fieldData']['ref_dest_id'] != $lockedToMember) { - // $haveTransactions = false; - // $invoices = false; - // } - // - // // Set the view file to editInvoice - // $view = 'editInvoice'; - // break; - case 'update': $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); @@ -388,7 +370,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $invoices = $this->editEntry($this->invoice_id); - echo '
' . print_r( $invoices, true) . '
'; + // echo '
' . print_r( $invoices, true) . '
'; // Remove the old line items. $this->wpdb->delete( @@ -411,19 +393,21 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'line_items', array( 'invoice' => $this->invoice_id, + 'account' => $invoices['fieldData']['account']['value'], 'line_item_type' => $line_item, 'name' => $_REQUEST['line_item_name'][$line_item], 'amount' => $_REQUEST['line_item_amount'][$line_item], 'quantity' => $_REQUEST['line_item_qty'][$line_item], 'total' => (float)$_REQUEST['line_item_qty'][$line_item] * (float)$_REQUEST['line_item_amount'][$line_item], 'created' => date('Y-m-d'), - 'first_due_date' => $_REQUEST['due_date'], - 'next_due_date' => $_REQUEST['due_date'], + 'first_due_date' => date('Y-m-d', $invoices['fieldData']['due_date']['timestamp'] ), + 'next_due_date' => date('Y-m-d', $invoices['fieldData']['due_date']['timestamp'] ), 'recurring' => $invoiceType['recurring']['value'], 'recurrence' => $invoiceType['recurrence'] ), array( '%d', // invoice + '%d', // account '%d', // line_item_type '%s', // name '%s', // amount @@ -455,7 +439,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices // Now we have a total for the invoice we can record the transaction $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); // Calling the support class to save the invoice to transaction table. - $BillingSupport->recordInvoice( $this->invoice_id, $_REQUEST['account'], $totals['amount_total'] ); + // $BillingSupport->recordInvoice( $this->invoice_id, $_REQUEST['account'], $totals['amount_total'] ); } else { $invoiceUpdateError = true; } @@ -465,6 +449,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $view = 'editInvoice'; break; + case 'view': // Call in the support class $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); @@ -497,23 +482,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices $where_params = array( 'true' ); - // 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 @@ -530,20 +498,6 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices } } - // 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/models/admin/billing/invoicing.php b/models/admin/billing/invoicing.php index 863eb0f..8feea0f 100644 --- a/models/admin/billing/invoicing.php +++ b/models/admin/billing/invoicing.php @@ -104,6 +104,7 @@ class GlmMembersAdmin_billing_invoicing //extends GlmDataAccounts $lastDisplayed = false; $totalAccounts = false; $option2 = false; + $successMsg = false; // Get any provided option if ( isset( $_REQUEST['option'] ) ) { @@ -113,8 +114,6 @@ class GlmMembersAdmin_billing_invoicing //extends GlmDataAccounts $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - if ( isset( $_REQUEST['invoice_types'] ) ) { $invoiceTypes = $_REQUEST['invoice_types']; @@ -164,8 +163,11 @@ class GlmMembersAdmin_billing_invoicing //extends GlmDataAccounts $view = 'invoicing'; if ( $option2 ) { + + // $where used in all places. + $where = implode( ' AND ', $wParts ); $accounts = $Accounts->getSimpleAccountList( $where ); - // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; + foreach ( $accounts as $account ) { // Get the invoice type $invoiceType = $BillingSupport->getInvoiceTypeById( $account['invoice_type'] ); @@ -183,6 +185,49 @@ class GlmMembersAdmin_billing_invoicing //extends GlmDataAccounts ) ); } + $successMsg = 'Invoices Created'; + } + + break; + + case 'sendEmails': + // Create Notification Object + $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + $view = 'invoicing'; + + $wParts[] = " T.id IN ( + SELECT account + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE paid <> true ) "; + $wParts[] = " ( T.email_invoice ) "; + + $type = $this->config['transaction_numb']['Invoice']; + + if ( $option2 ) { + $where = implode( ' AND ', $wParts ); + $accounts = $Accounts->getSimpleAccountList( $where ); + // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; + foreach ( $accounts as $account ) { + $invoices = $BillingSupport->getUnPaidInvoicesByAccount( $account['id'] ); + // echo '
$invoices: ' . print_r( $invoices, true ) . '
'; + if ( isset( $invoices ) && is_array( $invoices ) && !empty( $invoices ) ) { + foreach ( $invoices as $invoice ) { + $data = array( + 'type' => $type, + 'type_id' => $invoice['id'] + ); + $notices = $Notifications->getNotificationsWithSendByAction( $this->config['send_action_numb']['Send Emails'] ); + // echo '
$notices: ' . print_r( $notices, true ) . '
'; + if ( isset( $notices ) && is_array( $notices ) && !empty( $notices ) ) { + foreach ( $notices as $notice ) { + $Notifications->sendEmailNotification( $notice['id'], $account['id'], $data ); + echo '

Sending emails

'; + } + } + } + } + } + $successMsg = 'Invoices Sent'; } break; @@ -270,6 +315,7 @@ class GlmMembersAdmin_billing_invoicing //extends GlmDataAccounts 'numbDisplayed' => $numbDisplayed, 'lastDisplayed' => $lastDisplayed, 'totalAccounts' => $totalAccounts, + 'successMsg' => $successMsg, ); // Return status, any suggested view, and any data to controller diff --git a/models/admin/billing/payments.php b/models/admin/billing/payments.php index 53eefc2..e825942 100644 --- a/models/admin/billing/payments.php +++ b/models/admin/billing/payments.php @@ -197,9 +197,20 @@ class GlmMembersAdmin_billing_payments extends GlmDataPayments break; + case 'view': + $view = 'viewPayment'; + // Get the payment + $payment_id = filter_var( $_REQUEST['payment'], FILTER_VALIDATE_INT ); + $payments = $this->getEntry( $payment_id ); + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + break; + case 'delete': + $payment_id = filter_var( $_REQUEST['payment_id'], FILTER_VALIDATE_INT ); + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); // Need to remove any line items for the payments alse - // $payments = $this->deleteTransactions($this->payment_id); + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $payments = $BillingSupport->removePaymentById( $payment_id ); if ($payments) { $paymentsDeleted = true; @@ -207,13 +218,6 @@ class GlmMembersAdmin_billing_payments extends GlmDataPayments $paymentsDeleteError = true; } - case 'view': - $view = 'viewPayment'; - // Get the payment - $payment_id = filter_var( $_REQUEST['payment'], FILTER_VALIDATE_INT ); - $payments = $this->getEntry( $payment_id ); - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - break; case 'list': default: diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php index c0d70c6..c5d1f4e 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -728,7 +728,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $payment_method = filter_var( $_REQUEST['payment_method'], FILTER_VALIDATE_INT ); $payment_data = filter_var( $_REQUEST['payment_data'], FILTER_SANITIZE_STRING ); $invoices = filter_var( $_REQUEST['invoices'], FILTER_VALIDATE_INT, array( 'flags' => FILTER_REQUIRE_ARRAY ) ); - echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; if ( $account_id && $amount && $payment_method && $payment_data && !empty( $invoices ) ) { // Create new payment. diff --git a/todo.txt b/todo.txt index 1fee6d8..bffee6a 100644 --- a/todo.txt +++ b/todo.txt @@ -15,9 +15,11 @@ Invoicing: [x]-Create labels [ ] -Send Email (invoices) [ ] Ability to add adjustment -[ ] Ability to edit an invoice +[x] Ability to edit an invoice + [x] Edit is resetting some fields [x] Ability to delete an invoice -[ ] Ability to delete a payment +[x] Ability to delete a payment + [x] Update the original invoice balance due amount [x] Redo payment form for Uptravel.com [x] Find all places where there's a link to make payment to check for option. diff --git a/views/admin/billing/editInvoice.html b/views/admin/billing/editInvoice.html index 9521d42..fba1b41 100644 --- a/views/admin/billing/editInvoice.html +++ b/views/admin/billing/editInvoice.html @@ -14,6 +14,10 @@ + + + + {else} {/if} @@ -45,7 +49,7 @@ Notes
- +
diff --git a/views/admin/billing/invoices.html b/views/admin/billing/invoices.html index ade476a..65189f6 100644 --- a/views/admin/billing/invoices.html +++ b/views/admin/billing/invoices.html @@ -47,71 +47,65 @@ {/if}
-
-
-
- - - - - - - - - - - - {* - - *} - - {if $haveInvoices} - {foreach $invoices as $t} - - - - - - - - - - + + {/foreach} + {else} + + {/if} + +
Member NameInvoice ForTimeDue DateAmount TotalBalance
{$t.member_name} - {foreach $t.line_items as $item} - {$item.name} {if $item.recurring && $item.recurrence_string}( {$item.recurrence_string} ){/if} - {/foreach} - {$t.transaction_time.datetime} {$t.due_date.date} {$t.amount_total} {$t.balance}
(no Invoices listed)
+
{if $paging} diff --git a/views/admin/billing/invoicing.html b/views/admin/billing/invoicing.html index c4de3d2..eefa861 100644 --- a/views/admin/billing/invoicing.html +++ b/views/admin/billing/invoicing.html @@ -1,6 +1,7 @@ {include file='admin/billing/header.html'}

Invoicing

{include file='admin/billing/invoicingSubHeader.html'} +
@@ -54,12 +55,15 @@ {elseif $option == 'createLabels'} + {elseif $option == 'sendEmails'} + {/if}

Total found: {$totalAccounts}

+ {if $successMsg}

{$successMsg}

{/if} {* Paging *} {if $paging} diff --git a/views/admin/billing/invoicingSubHeader.html b/views/admin/billing/invoicingSubHeader.html index 21296ec..b1c993a 100644 --- a/views/admin/billing/invoicingSubHeader.html +++ b/views/admin/billing/invoicingSubHeader.html @@ -2,4 +2,5 @@ Create Invoices Print Invoices Create Labels + Send Emails diff --git a/views/admin/billing/payments.html b/views/admin/billing/payments.html index cebf085..d1a9749 100644 --- a/views/admin/billing/payments.html +++ b/views/admin/billing/payments.html @@ -37,39 +37,42 @@ {/if}
- - - - - - - - - - - - {if $havePayments} - {assign var="i" value="0"} - {foreach $payments as $t} - {if $i++ is odd by 1} - - {else} - - {/if} - - - - - - - - {/foreach} - {else} - - {/if} - -
IDMember NameTimeAmountPayment Method - Payment Notes
{$t.id} {$t.member_name} {$t.transaction_time.datetime} {$t.amount|string_format:"%.2f"} {$t.payment_method} {$t.payment_data}
(no Invoice Types listed)
+
+ + + + + + + + + + + {if $havePayments} + {assign var="i" value="0"} + {foreach $payments as $t} + + + + + + + + + + + {/foreach} + {else} + + {/if} + +
Member NameTimeAmountPayment Method + Payment Notes
{$t.member_name} {$t.transaction_time.datetime} {$t.amount|string_format:"%.2f"} {$t.payment_method} {$t.payment_data}
(no Payments listed)
+
{if $paging} @@ -79,34 +82,47 @@
{include file='admin/billing/exportPaymentModal.html'}