Updating prorated pricing on invoices
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 11 Jan 2019 21:53:15 +0000 (16:53 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 11 Jan 2019 21:53:15 +0000 (16:53 -0500)
prorating now works.

classes/billingSupport.php
models/admin/member/billing.php

index b81e23b..1a23d08 100644 (file)
@@ -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 '<pre>$dateInterval: ' . print_r( $dateInterval, true ) . '</pre>';
-        $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 );
     }
index c9f2732..f71ebb2 100644 (file)
@@ -202,15 +202,15 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
         case 'createInvoice':
 
 
-            $dateOne = '10/01/2019';
-            $dateTwo = '10/01/2019';
-            echo '<pre>$dateTwo: ' . print_r( $dateTwo, true ) . '</pre>';
-            $nextDate = $BillingSupport->getNextInvoiceDate();
-            $nextD = date( 'm/d/Y', $nextDate );
-            echo '<pre>$nextD: ' . print_r( $nextD, true ) . '</pre>';
+            // $dateOne = '10/01/2019';
+            // $dateTwo = '10/01/2019';
+            // echo '<pre>$dateTwo: ' . print_r( $dateTwo, true ) . '</pre>';
+            // $nextDate = $BillingSupport->getNextInvoiceDate();
+            // $nextD = date( 'm/d/Y', $nextDate );
+            // echo '<pre>$nextD: ' . print_r( $nextD, true ) . '</pre>';
 
-            $dateInt = date_diff( new DateTime( $nextD ), new DateTime( $dateTwo ) );
-            echo '<pre>$dateInt: ' . print_r( $dateInt, true ) . '</pre>';
+            // $dateInt = date_diff( new DateTime( $nextD ), new DateTime( $dateTwo ) );
+            // echo '<pre>$dateInt: ' . print_r( $dateInt, true ) . '</pre>';
 
             $view            = 'createInvoice';
             $nextInvoiceDate = $BillingSupport->getNextInvoiceDate();
@@ -242,31 +242,23 @@ class GlmMembersAdmin_member_billing // extends GlmDataBilling
             break;
 
         case 'createNewInvoice':
-            $view = 'createInvoice';
-            // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
-
+            $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 '<pre>$memberId: ' . print_r( $memberId, true ) . '</pre>';
-            // echo '<pre>$accountId: ' . print_r( $accountId, true ) . '</pre>';
-            // echo '<pre>$dynamicAmount: ' . print_r( $dynamicAmount, true ) . '</pre>';
-            // echo '<pre>$invoiceDate: ' . print_r( $invoiceDate, true ) . '</pre>';
-
             $Accounts = new GlmDataAccounts( $this->wpdb, $this->config );
             $account = $Accounts->editEntry( $accountID );
             if ( $account && $account['fieldData']['invoice_type'] ) {
                 $invoiceTypeId = $account['fieldData']['invoice_type'];
             }
-            // echo '<pre>$invoiceTypeId: ' . print_r( $invoiceTypeId, true ) . '</pre>';
 
             // 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',