mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-02 22:57:33 -05:00 
			
		
		
		
	
						commit
						c157cc19e5
					
				@ -99,7 +99,7 @@ class CheckData extends Command
 | 
				
			|||||||
            config(['database.default' => $database]);
 | 
					            config(['database.default' => $database]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->checkInvoiceBalances();
 | 
					        // $this->checkInvoiceBalances();
 | 
				
			||||||
        $this->checkInvoicePayments();
 | 
					        $this->checkInvoicePayments();
 | 
				
			||||||
        $this->checkPaidToDates();
 | 
					        $this->checkPaidToDates();
 | 
				
			||||||
        // $this->checkPaidToCompanyDates();
 | 
					        // $this->checkPaidToCompanyDates();
 | 
				
			||||||
@ -496,8 +496,6 @@ class CheckData extends Command
 | 
				
			|||||||
                                    ->pluck('p')
 | 
					                                    ->pluck('p')
 | 
				
			||||||
                                    ->first();
 | 
					                                    ->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // $total_paid = $total_amount - $total_refund;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                $total_credit = $invoice->credits()->get()->sum('amount');
 | 
					                $total_credit = $invoice->credits()->get()->sum('amount');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit;
 | 
					                $calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit;
 | 
				
			||||||
@ -543,7 +541,32 @@ class CheckData extends Command
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function clientBalanceQuery()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $results = \DB::select( \DB::raw("
 | 
				
			||||||
 | 
					         SELECT 
 | 
				
			||||||
 | 
					         SUM(invoices.balance) as invoice_balance, 
 | 
				
			||||||
 | 
					         SUM(credits.balance) as credit_balance, 
 | 
				
			||||||
 | 
					         clients.id as client_id, 
 | 
				
			||||||
 | 
					         clients.balance as client_balance
 | 
				
			||||||
 | 
					         FROM invoices 
 | 
				
			||||||
 | 
					         INNER JOIN
 | 
				
			||||||
 | 
					         clients ON 
 | 
				
			||||||
 | 
					         clients.id=invoices.client_id 
 | 
				
			||||||
 | 
					         INNER JOIN
 | 
				
			||||||
 | 
					         credits ON
 | 
				
			||||||
 | 
					         credits.client_id = clients.id
 | 
				
			||||||
 | 
					         WHERE invoices.is_deleted = false 
 | 
				
			||||||
 | 
					         AND invoices.status_id > 1 
 | 
				
			||||||
 | 
					         AND credits.is_deleted = false
 | 
				
			||||||
 | 
					         AND credits.status_id > 1
 | 
				
			||||||
 | 
					         GROUP BY clients.id
 | 
				
			||||||
 | 
					         HAVING invoice_balance != clients.balance
 | 
				
			||||||
 | 
					         ORDER BY clients.id;
 | 
				
			||||||
 | 
					        ") );
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					        return $results;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -553,26 +576,60 @@ class CheckData extends Command
 | 
				
			|||||||
        $this->wrong_balances = 0;
 | 
					        $this->wrong_balances = 0;
 | 
				
			||||||
        $this->wrong_paid_to_dates = 0;
 | 
					        $this->wrong_paid_to_dates = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach (Client::cursor()->where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2)) as $client) {
 | 
					        $clients = $this->clientBalanceQuery();
 | 
				
			||||||
            //$invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance');
 | 
					 | 
				
			||||||
            $invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance');
 | 
					 | 
				
			||||||
            $credit_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            /*Legacy - V4 will add credits to the balance - we may need to reverse engineer this and remove the credits from the client balance otherwise we need this hack here and in the invoice balance check.*/
 | 
					        foreach($clients as $client)
 | 
				
			||||||
            if($client->balance != $invoice_balance)
 | 
					        {
 | 
				
			||||||
                $invoice_balance -= $credit_balance;
 | 
					            $invoice_balance = $client['invoice_balance'] - $client['credit_balance'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
 | 
					            $ledger = CompanyLedger::where('client_id', $client['client_id'])->orderBy('id', 'DESC')->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($ledger && (string) $invoice_balance != (string) $client->balance) {
 | 
					            if ($ledger && (string) $invoice_balance != (string) $client['client_balance']) {
 | 
				
			||||||
                $this->wrong_paid_to_dates++;
 | 
					                $this->wrong_paid_to_dates++;
 | 
				
			||||||
                $this->logMessage($client->present()->name.' - '.$client->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client->balance, '0'). " Ledger balance = {$ledger->balance}");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $this->isValid = false;
 | 
					                $client_object = Client::find($client['client_id']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $this->logMessage($client_object->present()->name.' - '.$client_object->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client['client_balance'], '0'). " Ledger balance = {$ledger->balance}");
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					     
 | 
				
			||||||
 | 
					                if($this->option('client_balance')){
 | 
				
			||||||
 | 
					                    
 | 
				
			||||||
 | 
					                    $this->logMessage("# {$client_object->id} " . $client_object->present()->name.' - '.$client_object->number." Fixing {$client_object->balance} to {$invoice_balance}");
 | 
				
			||||||
 | 
					                    $client->balance = $invoice_balance;
 | 
				
			||||||
 | 
					                    $client->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    $ledger->adjustment = $invoice_balance;
 | 
				
			||||||
 | 
					                    $ledger->balance = $invoice_balance;
 | 
				
			||||||
 | 
					                    $ledger->notes = 'Ledger Adjustment';
 | 
				
			||||||
 | 
					                    $ledger->save();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $this->isValid = false;
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // foreach (Client::cursor()->where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2)) as $client) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     $invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance');
 | 
				
			||||||
 | 
					        //     $credit_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     if($client->balance != $invoice_balance)
 | 
				
			||||||
 | 
					        //         $invoice_balance -= $credit_balance;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     if ($ledger && (string) $invoice_balance != (string) $client->balance) {
 | 
				
			||||||
 | 
					        //         $this->wrong_paid_to_dates++;
 | 
				
			||||||
 | 
					        //         $this->logMessage($client->present()->name.' - '.$client->id." - calculated client balances do not match Invoice Balances = {$invoice_balance} - Client Balance = ".rtrim($client->balance, '0'). " Ledger balance = {$ledger->balance}");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //         $this->isValid = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     }
 | 
				
			||||||
 | 
					        // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect client balances");
 | 
					        $this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect client balances");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -213,12 +213,18 @@ abstract class QueryFilters
 | 
				
			|||||||
    public function with_trashed($value)
 | 
					    public function with_trashed($value)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if($value == 'true'){
 | 
					        if($value == 'false'){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $this->builder->withTrashed();
 | 
					            return $this->builder->where('is_deleted', 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // if($value == 'true'){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //     $this->builder->withTrashed();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->builder;
 | 
					        return $this->builder;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -212,6 +212,8 @@ class NinjaMailerJob implements ShouldQueue
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            $google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
 | 
					            $google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            //need to slow down gmail requests otherwise we hit 429's
 | 
				
			||||||
 | 
					            sleep(1);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        catch(\Exception $e) {
 | 
					        catch(\Exception $e) {
 | 
				
			||||||
            $this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
 | 
					            $this->logMailError('Gmail Token Invalid', $this->company->clients()->first());
 | 
				
			||||||
@ -225,9 +227,6 @@ class NinjaMailerJob implements ShouldQueue
 | 
				
			|||||||
         *  just for this request.
 | 
					         *  just for this request.
 | 
				
			||||||
        */
 | 
					        */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // config(['mail.driver' => 'gmail']);
 | 
					 | 
				
			||||||
        // (new MailServiceProvider(app()))->register();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $token = $user->oauth_user_token->access_token;
 | 
					        $token = $user->oauth_user_token->access_token;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->nmo
 | 
					        $this->nmo
 | 
				
			||||||
 | 
				
			|||||||
@ -30,7 +30,7 @@ class PaymentObserver
 | 
				
			|||||||
                            ->exists();
 | 
					                            ->exists();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($subscriptions) {
 | 
					        if ($subscriptions) {
 | 
				
			||||||
            WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices');
 | 
					            WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company, 'invoices,client');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -57,7 +57,7 @@ class PaymentObserver
 | 
				
			|||||||
                        ->exists();
 | 
					                        ->exists();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($subscriptions) {
 | 
					        if ($subscriptions) {
 | 
				
			||||||
            WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company, 'invoices');
 | 
					            WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company, 'invoices,client');
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -27,25 +27,25 @@ class RefundUnitTest extends TestCase
 | 
				
			|||||||
        parent::setUp();
 | 
					        parent::setUp();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testProRataRefundMonthly()
 | 
					    // public function testProRataRefundMonthly()
 | 
				
			||||||
    {
 | 
					    // {
 | 
				
			||||||
        $pro_rata = new ProRata();
 | 
					    //     $pro_rata = new ProRata();
 | 
				
			||||||
        $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
 | 
					    //     $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_MONTHLY);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(9.68, $refund);
 | 
					    //     $this->assertEquals(9.68, $refund);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
 | 
					    //     $this->assertEquals(30, Carbon::parse('2021-01-01')->diffInDays(Carbon::parse('2021-01-31')));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testProRataRefundYearly()
 | 
					    // public function testProRataRefundYearly()
 | 
				
			||||||
    {
 | 
					    // {
 | 
				
			||||||
        $pro_rata = new ProRata();
 | 
					    //     $pro_rata = new ProRata();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
 | 
					    //     $refund = $pro_rata->refund(10, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-31'), RecurringInvoice::FREQUENCY_ANNUALLY);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(0.82, $refund);
 | 
					    //     $this->assertEquals(0.82, $refund);
 | 
				
			||||||
    }
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function testDiffInDays()
 | 
					    public function testDiffInDays()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
				
			|||||||
@ -94,13 +94,13 @@ class SubscriptionsCalcTest extends TestCase
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $refund = $pro_rata->refund($invoice->amount, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
 | 
					        $refund = $pro_rata->refund($invoice->amount, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(1.61, $refund);
 | 
					        $this->assertEquals(1.67, $refund);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $pro_rata = new ProRata;
 | 
					        $pro_rata = new ProRata;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $upgrade = $pro_rata->charge($target->price, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
 | 
					        $upgrade = $pro_rata->charge($target->price, Carbon::parse('2021-01-01'), Carbon::parse('2021-01-06'), $subscription->frequency_id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->assertEquals(3.23, $upgrade);
 | 
					        $this->assertEquals(3.33, $upgrade);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user