mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 23:27:31 -05:00 
			
		
		
		
	Fix renewals; fix plan edge cases
This commit is contained in:
		
							parent
							
								
									089349b591
								
							
						
					
					
						commit
						030d4ed4da
					
				@ -5,7 +5,7 @@ use DateTime;
 | 
				
			|||||||
use Illuminate\Console\Command;
 | 
					use Illuminate\Console\Command;
 | 
				
			||||||
use Symfony\Component\Console\Input\InputOption;
 | 
					use Symfony\Component\Console\Input\InputOption;
 | 
				
			||||||
use Symfony\Component\Console\Input\InputArgument;
 | 
					use Symfony\Component\Console\Input\InputArgument;
 | 
				
			||||||
use App\Models\Account;
 | 
					use App\Models\Company;
 | 
				
			||||||
use App\Ninja\Mailers\ContactMailer as Mailer;
 | 
					use App\Ninja\Mailers\ContactMailer as Mailer;
 | 
				
			||||||
use App\Ninja\Repositories\AccountRepository;
 | 
					use App\Ninja\Repositories\AccountRepository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,24 +30,32 @@ class SendRenewalInvoices extends Command
 | 
				
			|||||||
        $today = new DateTime();
 | 
					        $today = new DateTime();
 | 
				
			||||||
        $sentTo = [];
 | 
					        $sentTo = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // get all accounts with pro plans expiring in 10 days
 | 
					        // get all accounts with plans expiring in 10 days
 | 
				
			||||||
        $accounts = Account::whereRaw('datediff(curdate(), pro_plan_paid) = 355')
 | 
					        $companies = Company::whereRaw('datediff(plan_expires, curdate()) = 10')
 | 
				
			||||||
                        ->orderBy('id')
 | 
					                        ->orderBy('id')
 | 
				
			||||||
                        ->get();
 | 
					                        ->get();
 | 
				
			||||||
        $this->info(count($accounts).' accounts found');
 | 
					        $this->info(count($companies).' companies found');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($accounts as $account) {
 | 
					        foreach ($companies as $company) {
 | 
				
			||||||
            // don't send multiple invoices to multi-company users
 | 
					            if (!count($company->accounts)) {
 | 
				
			||||||
            if ($userAccountId = $this->accountRepo->getUserAccountId($account)) {
 | 
					                continue;
 | 
				
			||||||
                if (isset($sentTo[$userAccountId])) {
 | 
					            }
 | 
				
			||||||
                    continue;
 | 
					            
 | 
				
			||||||
                } else {
 | 
					            $account = $company->accounts->sortBy('id')->first();
 | 
				
			||||||
                    $sentTo[$userAccountId] = true;
 | 
					            $plan = $company->plan;
 | 
				
			||||||
                }
 | 
					            $term = $company->plan_term;
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if ($company->pending_plan) {
 | 
				
			||||||
 | 
					                $plan = $company->pending_plan;
 | 
				
			||||||
 | 
					                $term = $company->pending_term;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					            if ($plan == PLAN_FREE || !$plan || !$term ){
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            $client = $this->accountRepo->getNinjaClient($account);
 | 
					            $client = $this->accountRepo->getNinjaClient($account);
 | 
				
			||||||
            $invitation = $this->accountRepo->createNinjaInvoice($client, $account);
 | 
					            $invitation = $this->accountRepo->createNinjaInvoice($client, $account, $plan, $term);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // set the due date to 10 days from now
 | 
					            // set the due date to 10 days from now
 | 
				
			||||||
            $invoice = $invitation->invoice;
 | 
					            $invoice = $invitation->invoice;
 | 
				
			||||||
 | 
				
			|||||||
@ -170,7 +170,7 @@ class AccountController extends BaseController
 | 
				
			|||||||
                $refund_deadline = clone $planDetails['started'];
 | 
					                $refund_deadline = clone $planDetails['started'];
 | 
				
			||||||
                $refund_deadline->modify('+30 days');
 | 
					                $refund_deadline->modify('+30 days');
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
                if ($plan == PLAN_FREE && $refund_deadline > date_create()) {
 | 
					                if ($plan == PLAN_FREE && $refund_deadline >= date_create()) {
 | 
				
			||||||
                    // Refund
 | 
					                    // Refund
 | 
				
			||||||
                    $account->company->plan = null;
 | 
					                    $account->company->plan = null;
 | 
				
			||||||
                    $account->company->plan_term = null;
 | 
					                    $account->company->plan_term = null;
 | 
				
			||||||
@ -207,7 +207,17 @@ class AccountController extends BaseController
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            if (!empty($new_plan)) {
 | 
					            if (!empty($new_plan)) {
 | 
				
			||||||
                $percent_used = $planDetails['paid']->diff(date_create())->days / $planDetails['paid']->diff($planDetails['expires'])->days;
 | 
					                $time_used = $planDetails['paid']->diff(date_create());
 | 
				
			||||||
 | 
					                $days_used = $time_used->days;
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                if ($time_used->invert) {
 | 
				
			||||||
 | 
					                    // They paid in advance
 | 
				
			||||||
 | 
					                    $days_used *= -1;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                $days_total = $planDetails['paid']->diff($planDetails['expires'])->days;
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                $percent_used = $days_used / $days_total;
 | 
				
			||||||
                $old_plan_price = Account::$plan_prices[$planDetails['plan']][$planDetails['term']];
 | 
					                $old_plan_price = Account::$plan_prices[$planDetails['plan']][$planDetails['term']];
 | 
				
			||||||
                $credit = $old_plan_price * (1 - $percent_used);
 | 
					                $credit = $old_plan_price * (1 - $percent_used);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -843,7 +843,7 @@ class Account extends Eloquent
 | 
				
			|||||||
            $trial_expires = clone $trial_started;
 | 
					            $trial_expires = clone $trial_started;
 | 
				
			||||||
            $trial_expires->modify('+2 weeks');
 | 
					            $trial_expires->modify('+2 weeks');
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            if ($trial_expires > date_create()) {
 | 
					            if ($trial_expires >= date_create()) {
 | 
				
			||||||
               $trial_active = true;
 | 
					               $trial_active = true;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -855,7 +855,7 @@ class Account extends Eloquent
 | 
				
			|||||||
                $plan_expires = false;
 | 
					                $plan_expires = false;
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                $plan_expires = DateTime::createFromFormat('Y-m-d', $this->company->plan_expires);
 | 
					                $plan_expires = DateTime::createFromFormat('Y-m-d', $this->company->plan_expires);
 | 
				
			||||||
                if ($plan_expires > date_create()) {
 | 
					                if ($plan_expires >= date_create()) {
 | 
				
			||||||
                    $plan_active = true;
 | 
					                    $plan_active = true;
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -246,11 +246,32 @@ class PaymentService extends BaseService
 | 
				
			|||||||
            
 | 
					            
 | 
				
			||||||
            if (!empty($plan)) { 
 | 
					            if (!empty($plan)) { 
 | 
				
			||||||
                $account = Account::with('users')->find($invoice->client->public_id);
 | 
					                $account = Account::with('users')->find($invoice->client->public_id);
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                if(
 | 
				
			||||||
 | 
					                    $account->company->plan != $plan
 | 
				
			||||||
 | 
					                    || DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create('-7 days')
 | 
				
			||||||
 | 
					                ) {
 | 
				
			||||||
 | 
					                    // Either this is a different plan, or the subscription expired more than a week ago
 | 
				
			||||||
 | 
					                    // Reset any grandfathering
 | 
				
			||||||
 | 
					                    $account->company->plan_started = date_create()->format('Y-m-d');
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                            
 | 
				
			||||||
 | 
					                if (
 | 
				
			||||||
 | 
					                    $account->company->plan == $plan
 | 
				
			||||||
 | 
					                    && $account->company->plan_term == $term 
 | 
				
			||||||
 | 
					                    && DateTime::createFromFormat('Y-m-d', $account->company->plan_expires) >= date_create()
 | 
				
			||||||
 | 
					                ) {
 | 
				
			||||||
 | 
					                    // This is a renewal; mark it paid as of when this term expires
 | 
				
			||||||
 | 
					                    $account->company->plan_paid = $account->company->plan_expires;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    $account->company->plan_paid = date_create()->format('Y-m-d');
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
                $account->company->payment_id = $payment->id;
 | 
					                $account->company->payment_id = $payment->id;
 | 
				
			||||||
                $account->company->plan = $plan;
 | 
					                $account->company->plan = $plan;
 | 
				
			||||||
                $account->company->plan_term = $term;
 | 
					                $account->company->plan_term = $term;
 | 
				
			||||||
                $account->company->plan_paid = $account->company->plan_started = date_create()->format('Y-m-d');
 | 
					                $account->company->plan_expires = DateTime::createFromFormat('Y-m-d', $account->company->plan_paid)
 | 
				
			||||||
                $account->company->plan_expires = date_create($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
 | 
					                    ->modify($term == PLAN_TERM_MONTHLY ? '+1 month' : '+1 year')->format('Y-m-d');
 | 
				
			||||||
                                
 | 
					                                
 | 
				
			||||||
                if (!empty($pending_monthly)) {
 | 
					                if (!empty($pending_monthly)) {
 | 
				
			||||||
                    $account->company->pending_plan = $plan;
 | 
					                    $account->company->pending_plan = $plan;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user