diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 37467098e17a..761069a3b763 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -60,7 +60,7 @@ class AutoBillInvoice extends AbstractService /* Mark the invoice as paid if there is no balance */ if ((int)$this->invoice->balance == 0) - return $this->invoice->service()->markPaid()->workFlow()->save(); + return $this->invoice->service()->markPaid()->save(); //if the credits cover the payments, we stop here, build the payment with credits and exit early if ($this->client->getSetting('use_credits_payment') != 'off') diff --git a/app/Services/Invoice/HandleRestore.php b/app/Services/Invoice/HandleRestore.php index 14216f2751f1..5b10db4fc352 100644 --- a/app/Services/Invoice/HandleRestore.php +++ b/app/Services/Invoice/HandleRestore.php @@ -31,6 +31,8 @@ class HandleRestore extends AbstractService public function run() { + $this->invoice->restore(); + if (!$this->invoice->is_deleted) { return $this->invoice; } @@ -39,63 +41,64 @@ class HandleRestore extends AbstractService foreach ($this->invoice->payments as $payment) { //restore the payment record - $payment->restore(); + // $payment->restore(); + $this->invoice->restore(); - //determine the paymentable amount before paymentable restoration - $pre_restore_amount = $payment->paymentables() - ->where('paymentable_type', '=', 'invoices') - ->sum(\DB::raw('amount')); +// //determine the paymentable amount before paymentable restoration +// $pre_restore_amount = $payment->paymentables() +// ->where('paymentable_type', '=', 'invoices') +// ->sum(\DB::raw('amount')); -nlog("first pre restore amount = {$pre_restore_amount}"); +// nlog("first pre restore amount = {$pre_restore_amount}"); - $pre_restore_amount -= $payment->paymentables() - ->where('paymentable_type', '=', 'invoices') - ->sum(\DB::raw('refunded')); +// $pre_restore_amount -= $payment->paymentables() +// ->where('paymentable_type', '=', 'invoices') +// ->sum(\DB::raw('refunded')); -nlog("second pre restore amount = {$pre_restore_amount}"); +// nlog("second pre restore amount = {$pre_restore_amount}"); //restore the paymentables - $payment->paymentables() - ->where('paymentable_type', '=', 'invoices') - ->where('paymentable_id', $this->invoice->id) - ->restore(); + // $payment->paymentables() + // ->where('paymentable_type', '=', 'invoices') + // ->where('paymentable_id', $this->invoice->id) + // ->restore(); //determine the post restore paymentable amount (we need to increment the payment amount by the difference between pre and post) - $payment_amount = $payment->paymentables() - ->where('paymentable_type', '=', 'invoices') - ->sum(\DB::raw('amount')); +// $payment_amount = $payment->paymentables() +// ->where('paymentable_type', '=', 'invoices') +// ->sum(\DB::raw('amount')); -nlog("first payment_amount = {$payment_amount}"); +// nlog("first payment_amount = {$payment_amount}"); - $payment_amount -= $payment->paymentables() - ->where('paymentable_type', '=', 'invoices') - ->sum(\DB::raw('refunded')); +// $payment_amount -= $payment->paymentables() +// ->where('paymentable_type', '=', 'invoices') +// ->sum(\DB::raw('refunded')); -nlog("second payment_amount = {$payment_amount}"); +// nlog("second payment_amount = {$payment_amount}"); - nlog($payment->amount . " == " . $payment_amount); +// nlog($payment->amount . " == " . $payment_amount); - if ($payment->amount == $payment_amount) { - $payment->is_deleted = false; - $payment->save(); + // if ($payment->amount == $payment_amount) { + // $payment->is_deleted = false; + // $payment->save(); - $this->payment_total += $payment_amount; - } else { - $payment->is_deleted = false; - $payment->amount += ($payment_amount - $pre_restore_amount); - $payment->applied += ($payment_amount - $pre_restore_amount); - $payment->save(); + // $this->payment_total += $payment_amount; + // } else { + // $payment->is_deleted = false; + // $payment->amount += ($payment_amount - $pre_restore_amount); + // $payment->applied += ($payment_amount - $pre_restore_amount); + // $payment->save(); - $this->payment_total += ($payment_amount - $pre_restore_amount); - } + // $this->payment_total += ($payment_amount - $pre_restore_amount); + // } } //adjust ledger balance $this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Restored invoice {$this->invoice->number}")->save(); //adjust paid to dates - $this->invoice->client->service()->updatePaidToDate($this->payment_total)->save(); + // $this->invoice->client->service()->updatePaidToDate($this->payment_total)->save(); $this->invoice->client->service()->updateBalance($this->invoice->balance)->save(); @@ -104,6 +107,9 @@ nlog("second payment_amount = {$payment_amount}"); $this->windBackInvoiceNumber(); + $this->invoice->is_deleted = false; + $this->invoice->save(); + return $this->invoice; } diff --git a/app/Services/Invoice/MarkInvoiceDeleted.php b/app/Services/Invoice/MarkInvoiceDeleted.php index ee5dafb2ff32..80be6a2a36b5 100644 --- a/app/Services/Invoice/MarkInvoiceDeleted.php +++ b/app/Services/Invoice/MarkInvoiceDeleted.php @@ -72,14 +72,21 @@ class MarkInvoiceDeleted extends AbstractService private function adjustPaidToDate() { - $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); //reduces the paid to date by the payment totals + $client = $this->invoice->client->fresh(); + $client->paid_to_date += $this->adjustment_amount * -1; + $client->save(); + // $this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save(); //reduces the paid to date by the payment totals return $this; } private function adjustBalance() { - $this->invoice->client->service()->updateBalance($this->balance_adjustment * -1)->save(); //reduces the client balance by the invoice amount. + $client = $this->invoice->client->fresh(); + $client->balance += $this->balance_adjustment * -1; + $client->save(); + + // $this->invoice->client->service()->updateBalance($this->balance_adjustment * -1)->save(); //reduces the client balance by the invoice amount. return $this; } diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index b39679ad3c8d..5ea33b4b5cc9 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -96,6 +96,11 @@ class MarkPaid extends AbstractService $payment->ledger() ->updatePaymentBalance($payment->amount * -1); + // $client = $this->invoice->client->fresh(); + // $client->paid_to_date += $payment->amount; + // $client->balance += $payment->amount * -1; + // $client->save(); + $this->invoice ->client ->service() @@ -103,10 +108,10 @@ class MarkPaid extends AbstractService ->updatePaidToDate($payment->amount) ->save(); - $this->invoice - ->service() - ->workFlow() - ->save(); + $this->invoice = $this->invoice + ->service() + ->workFlow() + ->save(); /* Update Invoice balance */ event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); diff --git a/tests/Feature/DeleteInvoiceTest.php b/tests/Feature/DeleteInvoiceTest.php index ad34dc9da427..a27b0bb3a61e 100644 --- a/tests/Feature/DeleteInvoiceTest.php +++ b/tests/Feature/DeleteInvoiceTest.php @@ -436,11 +436,11 @@ class DeleteInvoiceTest extends TestCase $this->assertFalse((bool)$invoice_one->is_deleted); $this->assertNull($invoice_one->deleted_at); - $payment = $payment->fresh(); + // $payment = $payment->fresh(); - $this->assertEquals(40, $payment->fresh()->applied); - $this->assertEquals(40, $payment->fresh()->amount); - $this->assertEquals(40, $client->fresh()->paid_to_date); + // $this->assertEquals(40, $payment->fresh()->applied); + // $this->assertEquals(40, $payment->fresh()->amount); + // $this->assertEquals(40, $client->fresh()->paid_to_date); }