diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 14afd00d8b40..e8f3ad18a629 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -293,7 +293,7 @@ class Import implements ShouldQueue private function setInitialCompanyLedgerBalances() { - Client::cursor()->each(function ($client) { + Client::where('company_id', $this->company->id)->cursor()->each(function ($client) { $invoice_balances = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); diff --git a/app/Models/Presenters/CompanyPresenter.php b/app/Models/Presenters/CompanyPresenter.php index 6a0e5a9e6902..aa347a86709d 100644 --- a/app/Models/Presenters/CompanyPresenter.php +++ b/app/Models/Presenters/CompanyPresenter.php @@ -54,6 +54,9 @@ class CompanyPresenter extends EntityPresenter $settings = $this->entity->settings; } + if(config('ninja.is_docker')) + return $this->logo($settings); + $context_options =array( "ssl"=>array( "verify_peer"=>false, diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index 536125441af7..e1615b3ab16f 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -46,6 +46,15 @@ class HandleRestore extends AbstractService ->where('paymentable_type', '=', 'invoices') ->sum(\DB::raw('amount')); +nlog("first pre restore amount = {$pre_restore_amount}"); + + $pre_restore_amount -= $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->sum(\DB::raw('refunded')); + +nlog("second pre restore amount = {$pre_restore_amount}"); + + //restore the paymentables $payment->paymentables() ->where('paymentable_type', '=', 'invoices') @@ -57,7 +66,15 @@ class HandleRestore extends AbstractService ->where('paymentable_type', '=', 'invoices') ->sum(\DB::raw('amount')); - info($payment->amount . " == " . $payment_amount); +nlog("first payment_amount = {$payment_amount}"); + + $payment_amount -= $payment->paymentables() + ->where('paymentable_type', '=', 'invoices') + ->sum(\DB::raw('refunded')); + +nlog("second payment_amount = {$payment_amount}"); + + nlog($payment->amount . " == " . $payment_amount); if ($payment->amount == $payment_amount) { $payment->is_deleted = false; diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index e92a31457fbd..1fcf97268075 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -36,7 +36,7 @@ class MarkInvoiceDeleted extends AbstractService if ($this->invoice->is_deleted) { return $this->invoice; } - + $this->cleanup() ->setAdjustmentAmount() ->deletePaymentables() @@ -86,6 +86,11 @@ class MarkInvoiceDeleted extends AbstractService ->where('paymentable_id', $this->invoice->id) ->sum(DB::raw('amount')); + $payment_adjustment -= $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('refunded')); + $payment->amount -= $payment_adjustment; $payment->applied -= $payment_adjustment; $payment->save(); @@ -108,12 +113,19 @@ class MarkInvoiceDeleted extends AbstractService ->where('paymentable_type', '=', 'invoices') ->where('paymentable_id', $this->invoice->id) ->sum(DB::raw('amount')); - //->sum(DB::raw('amount - refunded')); + + $this->adjustment_amount -= $payment->paymentables + ->where('paymentable_type', '=', 'invoices') + ->where('paymentable_id', $this->invoice->id) + ->sum(DB::raw('refunded')); } - $this->total_payments = $this->invoice->payments->sum('amount'); - // $this->total_payments = $this->invoice->payments->sum('amount - refunded'); + $this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded');; + //$this->total_payments = $this->invoice->payments->sum('amount - refunded'); + +nlog("adjustment amount = {$this->adjustment_amount}"); +nlog("total payments = {$this->total_payments}"); return $this; } diff --git a/tests/Feature/DeleteInvoiceTest.php b/tests/Feature/DeleteInvoiceTest.php index 2a4ca60a7e45..ad34dc9da427 100644 --- a/tests/Feature/DeleteInvoiceTest.php +++ b/tests/Feature/DeleteInvoiceTest.php @@ -40,9 +40,6 @@ class DeleteInvoiceTest extends TestCase ); } - - - public function testInvoiceDeletionAfterCancellation() { $data = [ @@ -137,18 +134,26 @@ class DeleteInvoiceTest extends TestCase $payment->refund($data); + //test balances $this->assertEquals(10, $payment->fresh()->refunded); $this->assertEquals(10, $invoice->client->fresh()->paid_to_date); - - //test balances + $this->assertEquals(10, $invoice->fresh()->balance); //cancel invoice and paid_to_date + $invoice->fresh()->service()->handleCancellation()->save(); //test balances and paid_to_date + $this->assertEquals(0, $invoice->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->balance); + $this->assertEquals(10, $invoice->client->fresh()->paid_to_date); //delete invoice + $invoice->fresh()->service()->markDeleted()->save(); //test balances and paid_to_date + $this->assertEquals(0, $invoice->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->balance); + $this->assertEquals(0, $invoice->client->fresh()->paid_to_date); }