Fixes for deleting payments and credit balances

This commit is contained in:
David Bomba 2024-04-28 15:00:12 +10:00
parent e1fe17cc3d
commit 169db5c491
4 changed files with 142 additions and 3 deletions

View File

@ -479,7 +479,7 @@ class CheckData extends Command
$this->logMessage("No contact present, so cannot add invitation for {$entity_key} - {$entity->id}");
try{
$entity->service()->createInvitations()->save();
$entity->service()->createInvitations()->save();
}
catch(\Exception $e){

View File

@ -156,7 +156,7 @@ class DeletePayment
$paymentable_credit->service()
->updateBalance($paymentable_credit->pivot->amount * $multiplier * -1)
->updatePaidToDate($paymentable_credit->pivot->amount * $multiplier)
->updatePaidToDate($paymentable_credit->pivot->amount * $multiplier * -1)
->setStatus(Credit::STATUS_SENT)
->save();

View File

@ -41,7 +41,7 @@ class CreateInvitations extends AbstractService
public function run()
{
$contacts = $this->purchase_order->vendor->contacts()->get();
$contacts = $this->purchase_order?->vendor?->contacts()->get();
if ($contacts->count() == 0) {
$this->createBlankContact();

View File

@ -11,6 +11,7 @@
namespace Tests\Feature;
use App\DataMapper\InvoiceItem;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Credit;
@ -42,6 +43,144 @@ class CreditTest extends TestCase
$this->makeTestData();
}
public function testCreditPaymentsPaidToDates()
{
$c = Client::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'balance' => 100,
]);
$ii = new InvoiceItem();
$ii->cost = 100;
$ii->quantity = 1;
$ii->product_key = 'xx';
$ii->notes = 'yy';
$i = \App\Models\Invoice::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $c->id,
'tax_name1' => '',
'tax_name2' => '',
'tax_name3' => '',
'tax_rate1' => 0,
'tax_rate2' => 0,
'tax_rate3' => 0,
'discount' => 0,
'line_items' => [
$ii
],
'status_id' => 1,
]);
$i->save();
$i->calc()->getInvoice();
$i->service()->markSent()->save();
$cr = Credit::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'client_id' => $c->id,
'tax_name1' => '',
'tax_name2' => '',
'tax_name3' => '',
'tax_rate1' => 0,
'tax_rate2' => 0,
'tax_rate3' => 0,
'discount' => 0,
'line_items' => [
$ii
],
'status_id' => 1,
]);
$cr->calc()->getCredit();
$cr->service()->markSent()->save();
$this->assertEquals(100, $i->balance);
$this->assertEquals(100, $i->amount);
$this->assertEquals(0, $i->paid_to_date);
$this->assertEquals(100, $cr->balance);
$this->assertEquals(100, $cr->amount);
$this->assertEquals(0, $cr->paid_to_date);
$this->assertEquals(100, $c->balance);
$this->assertEquals(0, $c->paid_to_date);
$data = [
'date' => '2020/12/12',
'client_id' => $c->hashed_id,
'invoices' => [
[
'invoice_id' => $i->hashed_id,
'amount' => 100
],
],
'credits' => [
[
'credit_id' => $cr->hashed_id,
'amount' => 100
]
],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->postJson('/api/v1/payments', $data);
$response->assertStatus(200);
$arr = $response->json();
$p_id = $arr['data']['id'];
$i = $i->fresh();
$cr = $cr->fresh();
$c = $c->fresh();
$this->assertEquals(0, $i->balance);
$this->assertEquals(100, $i->paid_to_date);
$this->assertEquals(0, $cr->balance);
$this->assertEquals(100, $cr->paid_to_date);
$this->assertEquals(100, $c->paid_to_date);
$this->assertEquals(0, $c->balance);
$p = \App\Models\Payment::find($this->decodePrimaryKey($p_id));
$this->assertEquals(0, $p->amount);
$this->assertEquals(0, $p->applied);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->deleteJson("/api/v1/payments/{$p_id}");
$response->assertStatus(200);
$i = $i->fresh();
$cr = $cr->fresh();
$c = $c->fresh();
$this->assertEquals(100, $i->balance);
$this->assertEquals(100, $i->amount);
$this->assertEquals(0, $i->paid_to_date);
$this->assertEquals(100, $cr->balance);
$this->assertEquals(100, $cr->amount);
$this->assertEquals(0, $cr->paid_to_date);
$this->assertEquals(100, $c->balance);
$this->assertEquals(0, $c->paid_to_date);
}
public function testApplicableFilters()
{
Credit::where('company_id', $this->company->id)->cursor()->each(function ($c) { $c->forceDelete(); });