Merge branch 'v5-develop' of https://github.com/turbo124/invoiceninja into v5-develop

This commit is contained in:
David Bomba 2023-11-08 08:21:56 +11:00
commit 269245c49a
13 changed files with 130 additions and 81 deletions

View File

@ -172,22 +172,14 @@ class SendRemindersCron extends Command
/**Refresh Invoice values*/
$invoice->calc()->getInvoice()->save();
$invoice->fresh();
// $invoice->service()->deletePdf()->save();
// if ($invoice->client->getSetting('enable_e_invoice')) {
// $invoice->service()->deleteEInvoice()->save();
// }
$invoice = $invoice->fresh();
/* Refresh the client here to ensure the balance is fresh */
$client = $invoice->client;
$client = $client->fresh();
$client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Update adjustment for invoice {$invoice->number}");
// nlog('adjusting client balance and invoice balance by '.($invoice->balance - $temp_invoice_balance));
// $client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
return $invoice;
}

View File

@ -46,19 +46,16 @@ class UpdateLedger implements ShouldQueue
$cl = CompanyLedger::find($this->company_ledger_id);
$ledger_item = CompanyLedger::query()
->where('company_id', $cl->company_id)
->where('client_id', $cl->client_id)
->where('company_ledgerable_id', $cl->company_ledgerable_id)
->where('company_ledgerable_type', $cl->company_ledgerable_type)
->exists();
$ledger_item = $cl->company_ledgerable->company_ledger()->count() == 1;
nlog($cl->company_ledgerable->company_ledger()->count());
if(!$cl)
return;
$entity = $cl->company_ledgerable;
$balance = $entity->calc()->getBalance();
$cl->adjustment = $ledger_item ? $balance - $this->start_amount : $balance;
$cl->adjustment = $ledger_item ? $balance : ($balance - $this->start_amount);
$parent_ledger = CompanyLedger::query()
->where('id', '<', $cl->id)

View File

@ -312,11 +312,10 @@ class SendReminders implements ShouldQueue
/**Refresh Invoice values*/
$invoice = $invoice->calc()->getInvoice();
$invoice->client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
// $invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance)->save();
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
return $invoice;
}

View File

@ -309,11 +309,8 @@ class ReminderJob implements ShouldQueue
// nlog('adjusting client balance and invoice balance by #'.$invoice->number.' '.($invoice->balance - $temp_invoice_balance));
// $invoice->client->service()->updateBalance($invoice->balance - $temp_invoice_balance);
// $invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
$invoice->ledger()->mutateInvoiceBalance($invoice->amount, "Late Fee ({$fee}) Adjustment for invoice {$invoice->number}");
$invoice->ledger()->updateInvoiceBalance($invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$invoice->number}");
$invoice->client->service()->calculateBalance();
return $invoice;
}

View File

@ -101,13 +101,11 @@ class AddGatewayFee extends AbstractService
// ->service()
// ->updateBalance($adjustment);
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee');
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment or Late Fee ({$adjustment}) Invoice {$this->invoice->number}");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment, 'Adjustment for adding gateway fee');
$this->invoice->client->service()->calculateBalance();
}
@ -153,14 +151,11 @@ $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustme
// ->updateBalance($adjustment * -1)
// ->save();
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment * -1, 'Adjustment for adding gateway DISCOUNT');
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment or Late Fee (-{$adjustment}) Invoice {$this->invoice->number}");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment * -1, 'Adjustment for adding gateway DISCOUNT');
$this->invoice->client->service()->calculateBalance();
}

View File

@ -34,23 +34,18 @@ class HandleCancellation extends AbstractService
return $this->invoice;
}
$adjustment = $this->invoice->balance * -1;
$adjustment =($this->invoice->balance < 0) ? abs($this->invoice->balance) : $this->invoice->balance * -1;
$this->backupCancellation($adjustment);
//set invoice balance to 0
// $this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} cancellation");
$this->invoice->balance = 0;
$this->invoice = $this->invoice->service()->setStatus(Invoice::STATUS_CANCELLED)->save();
// $this->invoice->client->service()->updateBalance($adjustment)->save();
$this->invoice->client->service()->calculateBalance();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for cancellation of Invoice {$this->invoice->number}");
$this->invoice->client->service()->calculateBalance();
$this->invoice->service()->workFlow()->save();
@ -69,11 +64,9 @@ $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustme
/* Will turn the negative cancellation amount to a positive adjustment*/
$adjustment = $cancellation->adjustment * -1;
// $this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} reversal");
$this->invoice->ledger()->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} reversal");
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} reversal");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} reversal");
$this->invoice->fresh();

View File

@ -49,9 +49,9 @@ class HandleRestore extends AbstractService
}
//adjust ledger balance
// $this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Restored invoice {$this->invoice->number}")->save();
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Restored invoice {$this->invoice->number}")->save();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Restored invoice {$this->invoice->number}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Restored invoice {$this->invoice->number}");
//@todo
$this->invoice->client

View File

@ -67,8 +67,8 @@ class HandleReversal extends AbstractService
/* Set invoice balance to 0 */
if ($this->invoice->balance != 0) {
// $this->invoice->ledger()->updateInvoiceBalance($balance_remaining * -1, $notes)->save();
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, $notes);
$this->invoice->ledger()->updateInvoiceBalance($balance_remaining * -1, $notes)->save();
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, $notes);
}
$this->invoice->balance = 0;

View File

@ -458,11 +458,11 @@ class InvoiceService
// ->updateBalance($adjustment * -1)
// ->save();
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee');
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment * -1, 'Adjustment for removing gateway fee');
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for removing gateway fee {$adjustment} Invoice {$this->invoice->number}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Adjustment for removing gateway fee {$adjustment} Invoice {$this->invoice->number}");
$this->invoice->client->service()->calculateBalance();
}

View File

@ -41,12 +41,12 @@ class MarkSent extends AbstractService
->save();
/*Update ledger*/
// $this->invoice
// ->ledger()
// ->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
$this->invoice
->ledger()
->updateInvoiceBalance($adjustment, "Invoice {$this->invoice->number} marked as sent.");
$this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} marked as sent => {$this->invoice->amount}");
// $this->invoice->ledger()->mutateInvoiceBalance($this->invoice->amount, "Invoice {$this->invoice->number} marked as sent => {$this->invoice->amount}");
$this->invoice->client->service()->calculateBalance();

View File

@ -97,12 +97,12 @@ class DeletePayment
->updatePaidToDate($net_deletable * -1)
->save();
// $paymentable_invoice->ledger()
// ->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
// ->save();
$paymentable_invoice->ledger()
->mutateInvoiceBalance($paymentable_invoice->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}");
->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save();
// $paymentable_invoice->ledger()
// ->mutateInvoiceBalance($paymentable_invoice->amount, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}");
//@todo refactor
$this->payment

View File

@ -290,12 +290,12 @@ class RefundPayment
->updatePaidToDate($refunded_invoice['amount'] * -1)
->save();
// $invoice->ledger()
// ->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
// ->save();
$invoice->ledger()
->mutateInvoiceBalance($invoice->amount, "Refund of payment # {$this->payment->number}");
->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")
->save();
// $invoice->ledger()
// ->mutateInvoiceBalance($invoice->amount, "Refund of payment # {$this->payment->number}");
if ($invoice->amount == $invoice->balance) {

View File

@ -11,21 +11,24 @@
namespace Tests\Integration;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use App\Models\Account;
use Tests\TestCase;
use App\Models\User;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Account;
use App\Models\Company;
use App\Models\CompanyToken;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\User;
use App\Models\CompanyToken;
use App\Models\ClientContact;
use App\Models\CompanyLedger;
use App\DataMapper\InvoiceItem;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Jobs\Ledger\UpdateLedger;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/** @test*/
class CompanyLedgerTest extends TestCase
@ -86,7 +89,7 @@ class CompanyLedgerTest extends TestCase
$user = User::whereEmail($fake_email)->first();
if (! $user) {
$user = User::factory()->create([
'email' => $fake_email,
@ -95,6 +98,7 @@ class CompanyLedgerTest extends TestCase
'confirmation_code' => $this->createDbHash(config('database.default')),
]);
}
$this->user = $user;
$cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id);
$cu->is_owner = true;
@ -126,6 +130,78 @@ class CompanyLedgerTest extends TestCase
]);
}
public function testLedgerAdjustments()
{
$c = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'balance' => 0,
'paid_to_date' => 0
]);
$i = Invoice::factory()->create([
'client_id' => $c->id,
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'status_id' => Invoice::STATUS_DRAFT,
'tax_name1' => '',
'tax_name2' => '',
'tax_name3' => '',
'tax_rate1' => 0,
'tax_rate2' => 0,
'tax_rate3' => 0,
'discount' => 0,
]);
$item = new InvoiceItem();
$item->cost = 10;
$item->quantity = 10;
$i->line_items = [$item];
$i->calc()->getInvoice();
$this->assertEquals(0, $c->balance);
$this->assertEquals(100, $i->amount);
$this->assertEquals(0, $i->balance);
$this->assertEquals(0, $i->company_ledger()->count());
// $i->service()->markSent()->save();
// $i = $i->fresh();
// // \Illuminate\Support\Facades\Bus::fake();
// // \Illuminate\Support\Facades\Bus::assertDispatched(UpdateLedger::class);
// $this->assertEquals(1, $i->company_ledger()->count());
// $cl = $i->company_ledger()->first();
// (new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
// $cl = $cl->fresh();
// $this->assertEquals(100, $cl->adjustment);
// $this->assertEquals(100, $cl->balance);
// $i->service()->applyPaymentAmount(40, "notes")->save();
// $i = $i->fresh();
// $cl = CompanyLedger::where('client_id', $i->client_id)
// ->orderBy('id', 'desc')
// ->first();
// $cl = $i->company_ledger()->orderBy('id','desc')->first();
// (new UpdateLedger($cl->id, $i->amount, $i->company->company_key, $i->company->db))->handle();
// $cl = $cl->fresh();
// nlog($cl->toArray());
// $this->assertEquals(-40, $cl->adjustment);
// $this->assertEquals(60, $cl->balance);
}
public function testBaseLine()
{
$this->assertEquals($this->company->invoices->count(), 0);