mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 05:27:33 -05:00 
			
		
		
		
	Refactor for payments to improve query efficiency
This commit is contained in:
		
							parent
							
								
									b99e623100
								
							
						
					
					
						commit
						4e3f52a1ee
					
				@ -68,7 +68,9 @@ class StorePaymentRequest extends Request
 | 
				
			|||||||
        if (isset($input['credits']) && is_array($input['credits']) !== false) {
 | 
					        if (isset($input['credits']) && is_array($input['credits']) !== false) {
 | 
				
			||||||
            foreach ($input['credits'] as $key => $value) {
 | 
					            foreach ($input['credits'] as $key => $value) {
 | 
				
			||||||
                if (array_key_exists('credit_id', $input['credits'][$key])) {
 | 
					                if (array_key_exists('credit_id', $input['credits'][$key])) {
 | 
				
			||||||
                    $input['credits'][$key]['credit_id'] = $value['credit_id'];
 | 
					                    // $input['credits'][$key]['credit_id'] = $value['credit_id'];
 | 
				
			||||||
 | 
					                    $input['credits'][$key]['credit_id'] = $this->decodePrimaryKey($value['credit_id']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    $credits_total += $value['amount'];
 | 
					                    $credits_total += $value['amount'];
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -51,10 +51,13 @@ class ValidCreditsRules implements Rule
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $unique_array = [];
 | 
					        $unique_array = [];
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
 | 
					        $cred_collection = Credit::withTrashed()->whereIn('id', array_column($this->input['credits'], 'credit_id'))->get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($this->input['credits'] as $credit) {
 | 
					        foreach ($this->input['credits'] as $credit) {
 | 
				
			||||||
            $unique_array[] = $credit['credit_id'];
 | 
					            $unique_array[] = $credit['credit_id'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $cred = Credit::find($this->decodePrimaryKey($credit['credit_id']));
 | 
					            // $cred = Credit::find($this->decodePrimaryKey($credit['credit_id']));
 | 
				
			||||||
 | 
					            $cred = $cred_collection->firstWhere('id', $credit['credit_id']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (! $cred) {
 | 
					            if (! $cred) {
 | 
				
			||||||
                $this->error_msg = ctrans('texts.credit_not_found');
 | 
					                $this->error_msg = ctrans('texts.credit_not_found');
 | 
				
			||||||
 | 
				
			|||||||
@ -51,6 +51,9 @@ class ValidInvoicesRules implements Rule
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $unique_array = [];
 | 
					        $unique_array = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /////
 | 
				
			||||||
 | 
					        $inv_collection = Invoice::withTrashed()->whereIn('id', array_column($this->input['invoices'], 'invoice_id'))->get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //todo optimize this into a single query
 | 
					        //todo optimize this into a single query
 | 
				
			||||||
        foreach ($this->input['invoices'] as $invoice) {
 | 
					        foreach ($this->input['invoices'] as $invoice) {
 | 
				
			||||||
            $unique_array[] = $invoice['invoice_id'];
 | 
					            $unique_array[] = $invoice['invoice_id'];
 | 
				
			||||||
@ -61,7 +64,10 @@ class ValidInvoicesRules implements Rule
 | 
				
			|||||||
                return false;
 | 
					                return false;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $inv = Invoice::withTrashed()->whereId($invoice['invoice_id'])->first();
 | 
					            /////
 | 
				
			||||||
 | 
					            $inv = $inv_collection->firstWhere('id', $invoice['invoice_id']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // $inv = Invoice::withTrashed()->whereId($invoice['invoice_id'])->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if (! $inv) {
 | 
					            if (! $inv) {
 | 
				
			||||||
                $this->error_msg = ctrans('texts.invoice_not_found');
 | 
					                $this->error_msg = ctrans('texts.invoice_not_found');
 | 
				
			||||||
 | 
				
			|||||||
@ -55,6 +55,8 @@ class CompanySizeCheck implements ShouldQueue
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private function check()
 | 
					    private function check()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        nlog("Checking all company sizes");
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
        Company::where('is_large', false)->withCount(['invoices', 'clients', 'products'])->cursor()->each(function ($company) {
 | 
					        Company::where('is_large', false)->withCount(['invoices', 'clients', 'products'])->cursor()->each(function ($company) {
 | 
				
			||||||
            if ($company->invoices_count > 500 || $company->products_count > 500 || $company->clients_count > 500) {
 | 
					            if ($company->invoices_count > 500 || $company->products_count > 500 || $company->clients_count > 500) {
 | 
				
			||||||
                nlog("Marking company {$company->id} as large");
 | 
					                nlog("Marking company {$company->id} as large");
 | 
				
			||||||
 | 
				
			|||||||
@ -157,12 +157,15 @@ class PaymentRepository extends BaseRepository {
 | 
				
			|||||||
        if (array_key_exists('credits', $data) && is_array($data['credits'])) {
 | 
					        if (array_key_exists('credits', $data) && is_array($data['credits'])) {
 | 
				
			||||||
            $credit_totals = array_sum(array_column($data['credits'], 'amount'));
 | 
					            $credit_totals = array_sum(array_column($data['credits'], 'amount'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
 | 
					            // $credits = Credit::whereIn('id', $this->transformKeys(array_column($data['credits'], 'credit_id')))->get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $payment->credits()->saveMany($credits);
 | 
					            $payment->credits()->saveMany($credits);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //todo optimize into a single query
 | 
					            //todo optimize into a single query
 | 
				
			||||||
            foreach ($data['credits'] as $paid_credit) {
 | 
					            foreach ($data['credits'] as $paid_credit) {
 | 
				
			||||||
                $credit = Credit::withTrashed()->find($this->decodePrimaryKey($paid_credit['credit_id']));
 | 
					                $credit = Credit::withTrashed()->find($paid_credit['credit_id']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if ($credit) {
 | 
					                if ($credit) {
 | 
				
			||||||
                    $credit = $credit->service()->markSent()->save();
 | 
					                    $credit = $credit->service()->markSent()->save();
 | 
				
			||||||
 | 
				
			|||||||
@ -78,7 +78,7 @@ class ImportCompanyTest extends TestCase
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        parent::setUp();
 | 
					        parent::setUp();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->artisan('db:seed');
 | 
					       // $this->artisan('db:seed');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->withoutMiddleware(
 | 
					        $this->withoutMiddleware(
 | 
				
			||||||
            ThrottleRequests::class
 | 
					            ThrottleRequests::class
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user