From: Steve Sutton Date: Wed, 9 Jan 2019 21:45:16 +0000 (-0500) Subject: Add create invoice page to members billing X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=626d0714590daadadbd3bec8a74cc33bccd22508;p=WP-Plugins%2Fglm-member-db-billing.git Add create invoice page to members billing This is to add an invoice from members billing tab. Prorating the invoice. WIP - prorate --- diff --git a/classes/billingSupport.php b/classes/billingSupport.php index bf7c836..b81e23b 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -1865,5 +1865,79 @@ class GlmBillingSupport return true; } + /** + * Returns a timestamp of current invoice date if there's settings for invoice date + */ + public function getCurrentInvoiceDate() + { + $currentInvoiceDate = false; + $renewalDayStatic = $this->config['settings']['renewal_day_static']; + $renewalDay = $this->config['settings']['renewal_day']; + $renewalMonth = $this->config['settings']['renewal_month']; + if ( $renewalDayStatic && $renewalDay && $renewalMonth ) { + $currentInvoiceDate = mktime( 0, 0, 1, $renewalMonth, $renewalDay, date( 'Y' ) ); + if ( $currentInvoiceDate > time() ) { + $currentInvoiceDate = mktime( 0, 0, 1, $renewalMonth, $renewalDay, date( 'Y' ) - 1 ); + } + } + return $currentInvoiceDate; + } + /** + * Returns a timestamp of next invoice date if there's settings for invoice date + */ + public function getNextInvoiceDate() + { + $nextInvoiceDate = false; + $renewalDayStatic = $this->config['settings']['renewal_day_static']; + $renewalDay = $this->config['settings']['renewal_day']; + $renewalMonth = $this->config['settings']['renewal_month']; + if ( $renewalDayStatic && $renewalDay && $renewalMonth ) { + $nextInvoiceDate = mktime( 12, 59, 59, $renewalMonth, $renewalDay, date( 'Y' ) ); + if ( $nextInvoiceDate <= time() ) { + $nextInvoiceDate = mktime( 12, 59, 59, $renewalMonth, $renewalDay, date( 'Y' ) + 1 ); + } + } + return $nextInvoiceDate; + } + /** + * Get a prorated price for this member based on the date field from + * the form (billing) submit + * + * @param array $invoiceData Member Data + * @param string $date The date for pro rating + * @param string $dynamicAmount The dynamic amount to use + * + * @return float + */ + public function getProRatedPrice( $invoiceData, $date, $dynamicAmount ) + { + $nextInvoiceDate = $this->getNextInvoiceDate(); + $invDate = new DateTime( date( 'c', $nextInvoiceDate ) ); + if ( $invoiceData['dynamic_amount'] && filter_var( $dynamicAmount, FILTER_VALIDATE_FLOAT ) ) { + return (float)$dynamicAmount; + } + $transactionDate = new DateTime( date( 'c', strtotime( $date ) ) ); + // Need to know when the invDate is before or after the transactionDate + $dateInterval = date_diff( $invDate, $transactionDate ); + // echo '
$dateInterval: ' . print_r( $dateInterval, true ) . '
'; + $cDate = date_diff( $invDate, $nextInvoiceDate ); + if ( $cDate->invert ) { + return (float)$invoiceData['amount']; + } else if ( !$cDate->invert ) { + // pro-rated for next year + // increase the invoice date by one year + $timestamp = $invDate->getTimestamp(); + $nextYear = strtotime( '+ 1 year', $timestamp ); + if ($nextYear) { + $invDate = new DateTime( date( 'c', $nextYear ) ); + } + } else if ($cDate == 1) { + return $invoiceData['amount']; + } + // Get the number of days difference + $days = round( $cDate->days ); + $dailyPrice = (float)( $invoiceData['amount'] / 365 ); + return (float)round( ( $days * $dailyPrice ), 2 ); + } } diff --git a/models/admin/member/billing.php b/models/admin/member/billing.php index ee8ece7..c9f2732 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -135,6 +135,9 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $lockedWhereT = 'true'; $lockedWhere = 'true'; + // For invoice create page + $nextInvoiceDate = false; + // Call in the support class $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); @@ -196,24 +199,135 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $account_status = apply_filters( 'glm-billing-get-account-status', '', $accountID ); switch ( $option ) { + case 'createInvoice': + + + $dateOne = '10/01/2019'; + $dateTwo = '10/01/2019'; + echo '
$dateTwo: ' . print_r( $dateTwo, true ) . '
'; + $nextDate = $BillingSupport->getNextInvoiceDate(); + $nextD = date( 'm/d/Y', $nextDate ); + echo '
$nextD: ' . print_r( $nextD, true ) . '
'; + + $dateInt = date_diff( new DateTime( $nextD ), new DateTime( $dateTwo ) ); + echo '
$dateInt: ' . print_r( $dateInt, true ) . '
'; + + $view = 'createInvoice'; + $nextInvoiceDate = $BillingSupport->getNextInvoiceDate(); + if ( $nextInvoiceDate ) { + $nextInvoiceDate = date( 'm/d/Y', $nextInvoiceDate ); + } + + // Get list of payable invoice_types + $payable_types = $BillingSupport->getAllPayableInvoiceTypes(); + + // Load DataClass for Management. + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataManagement.php'; + $Management = new GlmDataBillingManagement( $this->wpdb, $this->config ); + $management = $Management->getEntry( 1 ); + + // Need to see if there's an account for this member. + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataAccounts.php'; + $account = $Accounts->editEntry( $accountID ); + if ( $account && $account['fieldData'] ) { + $invoiceTypeId = $account['fieldData']['invoice_type']; + + if ( $invoiceTypeId ) { + $member_invoice = $BillingSupport->getInvoiceTypeById( $invoiceTypeId ); + } + } + + $invoiceTypes = $BillingSupport->getAllPayableInvoiceTypes(); + + break; + + case 'createNewInvoice': + $view = 'createInvoice'; + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + + $memberId = filter_var( $_REQUEST['member'], FILTER_VALIDATE_INT ); + $accountId = filter_var( $_REQUEST['account_id'], FILTER_VALIDATE_INT ); + $dynamicAmount = filter_var( $_REQUEST['dynamic_amount'], FILTER_VALIDATE_FLOAT ); + $invoiceDate = filter_var( $_REQUEST['invoice_date'], FILTER_SANITIZE_STRING ); + + // echo '
$memberId: ' . print_r( $memberId, true ) . '
'; + // echo '
$accountId: ' . print_r( $accountId, true ) . '
'; + // echo '
$dynamicAmount: ' . print_r( $dynamicAmount, true ) . '
'; + // echo '
$invoiceDate: ' . print_r( $invoiceDate, true ) . '
'; + + $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); + $account = $Accounts->editEntry( $accountID ); + if ( $account && $account['fieldData']['invoice_type'] ) { + $invoiceTypeId = $account['fieldData']['invoice_type']; + } + // echo '
$invoiceTypeId: ' . print_r( $invoiceTypeId, true ) . '
'; + + // Create an invoice w/line item for this accounts payment type + if ( $invoiceTypeId && $memberId && $accountId ) { + $invoiceData = $BillingSupport->getInvoiceTypeById( $invoiceTypeId ); + // Amount maybe pro-rated + $amount = $BillingSupport->getProRatedPrice( $invoiceData, $invoiceData, $dynamicAmount ); + // Create invoice. + $this->wpdb->insert( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices', + array( + 'transaction_time' => date( 'Y-m-d H:i:s' ), + 'account' => $accountId, + 'amount_total' => $amount, + 'balance' => $amount, + 'due_date' => date( 'Y-m-d', strtotime( $invoiceDate ) ), + 'paid' => 0, + 'notes' => '', + 'renewal' => true, + 'recurring' => true, + 'recurrence' => 20 + ), + array( + '%s', // transaction_time + '%d', // account + '%s', // amount + '%s', // balance + '%s', // due_date + '%s', // paid + '%s', // notes + '%s', // renewal + '%s', // recurring + '%s', // recurrence + ) + ); + $invoiceId = $this->wpdb->insert_id; + $BillingSupport->createLineItemForInvoice( + array( + 'invoice_id' => $invoiceId, + 'line_item_type' => $invoiceData['id'], + 'account' => $accountId, + 'name' => $invoiceData['name'], + 'amount' => $amount, + 'due_date' => date( 'Y-m-d', strtotime( $invoiceDate ) ), + 'recurring' => $invoiceData['recurring'], + 'recurrence' => $invoiceData['recurrence'], + ) + ); + $BillingSupport->recordInvoice( $invoiceId, $accountId, $amount ); + } + + break; + case 'renew': $view = 'renew'; // Get list of payable invoice_types $payable_types = $BillingSupport->getAllPayableInvoiceTypes(); - // echo '
$payable_types: ' . print_r( $payable_types, true ) . '
'; $member_invoice_id = $BillingSupport->getMembersInvoiceTypeByRefDest( $this->memberID ); if ( $member_invoice_id ) { $member_invoice = $BillingSupport->getInvoiceTypeById( $member_invoice_id ); } - // echo '
$member_invoice: ' . print_r( $member_invoice, true ) . '
'; // TODO: If there's no member_invoice then we can't create an invoice for renewal. // Get a list of this accounts employees. If they have any. $employees = $BillingSupport->getListOfAccountEmployees( $this->memberID ); - // echo '
$employees: ' . print_r( $employees, true ) . '
'; // Load DataClass for Management. require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataManagement.php'; @@ -227,10 +341,8 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling // Need to get the accounts $Accounts = new GlmDataAccounts( $this->wpdb, $this->config ); $accounts = $Accounts->getSimpleAccountList( "T.boss <> true AND T.boss IS NOT NULL AND T.invoice_type != 0", 'ref_name' ); - // echo '
$accounts: ' . print_r( $accounts, true ) . '
'; $invoiceTypes = $BillingSupport->getAllPayableInvoiceTypes(); - // echo '
$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '
'; break; @@ -409,9 +521,6 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config ); $invoiceTypes = $InvoiceTypesObj->getList(); - // echo '
$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '
'; - - // Need to see if there's an account for this member. $accountID = $this->wpdb->get_var( $this->wpdb->prepare( @@ -422,22 +531,6 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling ) ); - // $contactID = $this->wpdb->get_var( - // $this->wpdb->prepare( - // "SELECT id - // FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts - // WHERE ref_dest = %d", - // $this->memberID - // ) - // ); - // echo '
$contactID: ' . print_r( $contactID, true ) . '
'; - // require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH . '/data/dataContacts.php'; - // $Contact = new GlmDataContacts( $this->wpdb, $this->config ); - // $user_id = $Contact->getWPUserId( $contactID ); - // echo '
$user_id: ' . print_r( $user_id, true ) . '
'; - // $Contact->updateContactRole( $contactID, 'glm_members_member_contact', 'glm_members_own_entity_manager' ); - - // echo '
$accountID: ' . print_r( $accountID, true ) . '
'; // Grab the employee data if ( $accountID ) { // Get a list of this accounts employees. If they have any. @@ -448,7 +541,6 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling // Check to see if we're adding an account or editing one. if ( isset( $_REQUEST['ref_name'] ) ) { $_REQUEST['anniversary_date'] = date('Y-m-d', strtotime($_REQUEST['anniversary_date'])); - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; // if there's no id then add account. if ( !isset( $_REQUEST['id'] ) ) { $account = $Accounts->insertEntry(); @@ -516,7 +608,6 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling unset( $employees[$employee_id] ); } } - // echo '
$employees: ' . print_r( $employees, true ) . '
'; // If there's employees data then add them to this account if ( isset($accountID) && $accountID && isset( $_REQUEST['employees'] ) && is_array( $_REQUEST['employees'] ) && !empty( $_REQUEST['employees'] ) ) { foreach ( $_REQUEST['employees'] as $employee_id => $hasKey ) { @@ -793,6 +884,7 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling 'invoiceTypes' => $invoiceTypes, 'paymentMethods' => $this->config['alt_payment_method'], 'renewalFormSession' => $renewalFormSession, + 'nextInvoiceDate' => $nextInvoiceDate, ); // Return status, any suggested view, and any data to controller. diff --git a/setup/commonHooks.php b/setup/commonHooks.php index 6dc155d..533bd2c 100644 --- a/setup/commonHooks.php +++ b/setup/commonHooks.php @@ -106,3 +106,26 @@ add_filter( 'glm-billing-account-has-renewal', function( $content, $account_id ) ) ); }, 10, 2 ); + +add_filter( 'glm-billing-account-has-invoice', function( $content, $account_id ){ + $hasInvoice = false; + + require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH.'/billingSupport.php'; + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + $currentInvoiceDate = $BillingSupport->getCurrentInvoiceDate(); + $nextInvoiceDate = $BillingSupport->getNextInvoiceDate(); + if ( $currentInvoiceDate && $nextInvoiceDate ) { + $hasInvoice = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT count(id) + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE account = %d + AND due_date BETWEEN %s and %s", + $account_id, + date( 'Y-m-d' , $currentInvoiceDate ), + date( 'Y-m-d', $nextInvoiceDate ) + ) + ); + } + return $hasInvoice; +}, 10, 2 ); diff --git a/views/admin/billing/createInvoice.html b/views/admin/billing/createInvoice.html new file mode 100644 index 0000000..218b4e2 --- /dev/null +++ b/views/admin/billing/createInvoice.html @@ -0,0 +1,78 @@ +{include file='admin/member/header.html'} +{include file='admin/billing/memberBillingSubHeader.html'} + +

Membership Renewal

+
+ + {if $paymentSuccess}Payment Completed{/if} + {if $paymentError}Error With Payment{/if} + + {if $messages} + {foreach $messages as $message} +
{$message}
+ {/foreach} + {/if} + +
+ + + + + + + {if $member_invoice} +
+
+ Payment Type +
+
+ {$member_invoice.name} ${$member_invoice.amount} +
+
+
+
+ Invoice Date +
+
+ +
+
+
+
+ Dynamic Amount (dollar amount without $) +
+
+ +
+
+ {/if} + + + +
+
+ + + + +{include file='admin/footer.html'} diff --git a/views/admin/billing/memberBillingSubHeader.html b/views/admin/billing/memberBillingSubHeader.html index 7365fbb..6545521 100644 --- a/views/admin/billing/memberBillingSubHeader.html +++ b/views/admin/billing/memberBillingSubHeader.html @@ -28,8 +28,13 @@ {/if} {if isset( $billing_settings.uptravel_payment_form ) && $billing_settings.uptravel_payment_form} + {$hasInvoice = apply_filters('glm-billing-account-has-invoice', false, $accountID )}
  • - Make A Payment + {if $hasInvoice} + Make A Payment + {else} + Create Invoice + {/if}
  • {else}