diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 6ddd7abb539e..17a450aeb6d9 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -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){ diff --git a/app/Services/Payment/DeletePayment.php b/app/Services/Payment/DeletePayment.php index 7c5a775a9946..8d371f4efed4 100644 --- a/app/Services/Payment/DeletePayment.php +++ b/app/Services/Payment/DeletePayment.php @@ -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(); diff --git a/app/Services/PurchaseOrder/CreateInvitations.php b/app/Services/PurchaseOrder/CreateInvitations.php index 7db470ed94e8..12bdfc60cf01 100644 --- a/app/Services/PurchaseOrder/CreateInvitations.php +++ b/app/Services/PurchaseOrder/CreateInvitations.php @@ -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(); diff --git a/tests/Feature/CreditTest.php b/tests/Feature/CreditTest.php index cdf9e7b65de9..e899979a8d64 100644 --- a/tests/Feature/CreditTest.php +++ b/tests/Feature/CreditTest.php @@ -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(); });