mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 08:37:33 -04:00 
			
		
		
		
	Refactor for recurring invoices
This commit is contained in:
		
							parent
							
								
									8ac4aa56a0
								
							
						
					
					
						commit
						524fad7bf4
					
				
							
								
								
									
										70
									
								
								app/Jobs/Cron/CompanyRecurringCron.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								app/Jobs/Cron/CompanyRecurringCron.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * Invoice Ninja (https://invoiceninja.com). | ||||||
|  |  * | ||||||
|  |  * @link https://github.com/invoiceninja/invoiceninja source repository | ||||||
|  |  * | ||||||
|  |  * @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com) | ||||||
|  |  * | ||||||
|  |  * @license https://www.elastic.co/licensing/elastic-license | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | namespace App\Jobs\Cron; | ||||||
|  | 
 | ||||||
|  | use App\Jobs\RecurringInvoice\SendRecurring; | ||||||
|  | use App\Libraries\MultiDB; | ||||||
|  | use App\Models\Company; | ||||||
|  | use App\Models\RecurringInvoice; | ||||||
|  | use Illuminate\Foundation\Bus\Dispatchable; | ||||||
|  | use Illuminate\Support\Carbon; | ||||||
|  | 
 | ||||||
|  | /*@not used*/ | ||||||
|  | 
 | ||||||
|  | class CompanyRecurringCron | ||||||
|  | { | ||||||
|  |     use Dispatchable; | ||||||
|  | 
 | ||||||
|  |     public $tries = 1; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Create a new job instance. | ||||||
|  |      * | ||||||
|  |      * @return void | ||||||
|  |      */ | ||||||
|  |     public function __construct() | ||||||
|  |     { | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Execute the job. | ||||||
|  |      * | ||||||
|  |      * @return void | ||||||
|  |      */ | ||||||
|  |     public function handle() : void | ||||||
|  |     { | ||||||
|  |         //multiDB environment, need to
 | ||||||
|  |         foreach (MultiDB::$dbs as $db) { | ||||||
|  | 
 | ||||||
|  |             MultiDB::setDB($db); | ||||||
|  | 
 | ||||||
|  |             Company::where('is_disabled', 0) | ||||||
|  |                    ->whereHas('recurring_invoices', function ($query){ | ||||||
|  |                         $query->where('next_send_date', '<=', now()->toDateTimeString()) | ||||||
|  |                               ->whereNotNull('next_send_date') | ||||||
|  |                               ->whereNull('deleted_at') | ||||||
|  |                               ->where('is_deleted', false) | ||||||
|  |                               ->where('status_id', RecurringInvoice::STATUS_ACTIVE) | ||||||
|  |                               ->where('remaining_cycles', '!=', '0') | ||||||
|  |                               ->whereHas('client', function ($query) { | ||||||
|  |                                  $query->where('is_deleted', 0) | ||||||
|  |                                        ->where('deleted_at', null); | ||||||
|  |                             }); | ||||||
|  |                           }) | ||||||
|  |                     ->cursor()->each(function ($company){ | ||||||
|  | 
 | ||||||
|  |                     SendCompanyRecurring::dispatch($company->id, $company->db); | ||||||
|  | 
 | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										91
									
								
								app/Jobs/Cron/SendCompanyRecurring.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										91
									
								
								app/Jobs/Cron/SendCompanyRecurring.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,91 @@ | |||||||
|  | <?php | ||||||
|  | /** | ||||||
|  |  * Invoice Ninja (https://invoiceninja.com). | ||||||
|  |  * | ||||||
|  |  * @link https://github.com/invoiceninja/invoiceninja source repository | ||||||
|  |  * | ||||||
|  |  * @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com) | ||||||
|  |  * | ||||||
|  |  * @license https://www.elastic.co/licensing/elastic-license | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | namespace App\Jobs\Cron; | ||||||
|  | 
 | ||||||
|  | use App\Jobs\RecurringInvoice\SendRecurring; | ||||||
|  | use App\Libraries\MultiDB; | ||||||
|  | use App\Models\Company; | ||||||
|  | use App\Models\RecurringInvoice; | ||||||
|  | use Illuminate\Foundation\Bus\Dispatchable; | ||||||
|  | use Illuminate\Support\Carbon; | ||||||
|  | 
 | ||||||
|  | /*@not used*/ | ||||||
|  | 
 | ||||||
|  | class SendCompanyRecurring | ||||||
|  | { | ||||||
|  |     use Dispatchable; | ||||||
|  | 
 | ||||||
|  |     public $tries = 1; | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Create a new job instance. | ||||||
|  |      * | ||||||
|  |      * @return void | ||||||
|  |      */ | ||||||
|  | 
 | ||||||
|  |     public $company; | ||||||
|  | 
 | ||||||
|  |     public $db; | ||||||
|  | 
 | ||||||
|  |     public function __construct($company_id, $db) | ||||||
|  |     { | ||||||
|  |      | ||||||
|  |         $this->company_id = $company_id; | ||||||
|  | 
 | ||||||
|  |         $this->db = $db; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Execute the job. | ||||||
|  |      * | ||||||
|  |      * @return void | ||||||
|  |      */ | ||||||
|  |     public function handle() : void | ||||||
|  |     { | ||||||
|  | 
 | ||||||
|  |         MultiDB::setDB($this->db); | ||||||
|  | 
 | ||||||
|  |         $recurring_invoices = Company::where('id', $this->company_id) | ||||||
|  |                                      ->where('is_disabled', 0) | ||||||
|  |                                      ->whereHas('recurring_invoices', function ($query){ | ||||||
|  |                                         $query->where('next_send_date', '<=', now()->toDateTimeString()) | ||||||
|  |                                               ->whereNotNull('next_send_date') | ||||||
|  |                                               ->whereNull('deleted_at') | ||||||
|  |                                               ->where('is_deleted', false) | ||||||
|  |                                               ->where('status_id', RecurringInvoice::STATUS_ACTIVE) | ||||||
|  |                                               ->where('remaining_cycles', '!=', '0') | ||||||
|  |                                               ->whereHas('client', function ($query) { | ||||||
|  |                                                  $query->where('is_deleted', 0) | ||||||
|  |                                                        ->where('deleted_at', null); | ||||||
|  |                                             }); | ||||||
|  |                                           }) | ||||||
|  |                                       ->cursor()->each(function ($recurring_invoice){ | ||||||
|  | 
 | ||||||
|  |                                 nlog("Trying to send {$recurring_invoice->number}"); | ||||||
|  | 
 | ||||||
|  |                                 if ($recurring_invoice->company->stop_on_unpaid_recurring) { | ||||||
|  |                                     if ($recurring_invoice->invoices()->whereIn('status_id', [2, 3])->where('is_deleted', 0)->where('balance', '>', 0)->exists()) { | ||||||
|  |                                         return; | ||||||
|  |                                     } | ||||||
|  |                                 } | ||||||
|  | 
 | ||||||
|  |                                 try { | ||||||
|  |                                     (new SendRecurring($recurring_invoice, $recurring_invoice->company->db))->handle(); | ||||||
|  |                                 } catch (\Exception $e) { | ||||||
|  |                                     nlog("Unable to sending recurring invoice {$recurring_invoice->id} ".$e->getMessage()); | ||||||
|  |                                 } | ||||||
|  | 
 | ||||||
|  |                             }); | ||||||
|  |          | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user