From 1ee7cf37a1fff9e77e912919cd2027c8fe8c8cf1 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 11 Jan 2019 16:53:15 -0500 Subject: [PATCH] Updating prorated pricing on invoices prorating now works. --- classes/billingSupport.php | 19 +++++++++---------- models/admin/member/billing.php | 28 ++++++++++------------------ 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/classes/billingSupport.php b/classes/billingSupport.php index b81e23b..1a23d08 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -1912,30 +1912,29 @@ class GlmBillingSupport public function getProRatedPrice( $invoiceData, $date, $dynamicAmount ) { $nextInvoiceDate = $this->getNextInvoiceDate(); - $invDate = new DateTime( date( 'c', $nextInvoiceDate ) ); + $invDate = new DateTime( date( 'm/d/Y', $nextInvoiceDate ) ); if ( $invoiceData['dynamic_amount'] && filter_var( $dynamicAmount, FILTER_VALIDATE_FLOAT ) ) { return (float)$dynamicAmount; } - $transactionDate = new DateTime( date( 'c', strtotime( $date ) ) ); + $transactionDate = new DateTime( $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 ); + $cDate = date_diff( $invDate, $transactionDate ); if ( $cDate->invert ) { return (float)$invoiceData['amount']; - } else if ( !$cDate->invert ) { + } else if ( !$cDate->invert && $cDate->days ) { // 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 ) ); + if ( $nextYear ) { + $invDate = new DateTime( date( 'm/d/Y', $nextYear ) ); } - } else if ($cDate == 1) { + } else if ( $cDate->days == 0 ) { return $invoiceData['amount']; } // Get the number of days difference - $days = round( $cDate->days ); + $dateDiff = date_diff( $transactionDate, $invDate ); + $days = round( $dateDiff->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 c9f2732..f71ebb2 100644 --- a/models/admin/member/billing.php +++ b/models/admin/member/billing.php @@ -202,15 +202,15 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling 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 ) . '
'; + // $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 ) . '
'; + // $dateInt = date_diff( new DateTime( $nextD ), new DateTime( $dateTwo ) ); + // echo '
$dateInt: ' . print_r( $dateInt, true ) . '
'; $view = 'createInvoice'; $nextInvoiceDate = $BillingSupport->getNextInvoiceDate(); @@ -242,31 +242,23 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling break; case 'createNewInvoice': - $view = 'createInvoice'; - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - + $view = 'createInvoice'; $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 ); + $amount = $BillingSupport->getProRatedPrice( $invoiceData, $invoiceDate, $dynamicAmount ); // Create invoice. $this->wpdb->insert( GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'invoices', -- 2.17.1