diff --git a/app/Filters/CreditFilters.php b/app/Filters/CreditFilters.php index a69daa60ddd4..a4dd3f33bf46 100644 --- a/app/Filters/CreditFilters.php +++ b/app/Filters/CreditFilters.php @@ -48,6 +48,10 @@ class CreditFilters extends QueryFilters $credit_filters[] = Credit::STATUS_DRAFT; } + if (in_array('sent', $status_parameters)) { + $credit_filters[] = Credit::STATUS_SENT; + } + if (in_array('partial', $status_parameters)) { $credit_filters[] = Credit::STATUS_PARTIAL; } @@ -97,6 +101,21 @@ class CreditFilters extends QueryFilters }); } + public function applicable(string $value = ''): Builder + { + if (strlen($value) == 0) { + return $this->builder; + } + + return $this->builder->where(function ($query){ + $query->whereIn('status_id', [Credit::STATUS_SENT, Credit::STATUS_PARTIAL]) + ->where('balance', '>', 0) + ->where(function ($q){ + $q->whereNull('due_date')->orWhere('due_date', '>', now()); + }); + }); + } + public function number(string $number = ''): Builder { if (strlen($number) == 0) { diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index 736df18c9d26..a520ef6d1430 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -18,6 +18,7 @@ use App\Services\AbstractService; class GetInvoicePdf extends AbstractService { + public function __construct(public Invoice $invoice, public ?ClientContact $contact = null) { } diff --git a/app/Services/Recurring/GetInvoicePdf.php b/app/Services/Recurring/GetInvoicePdf.php index 019d094e6fdc..baab02cd7438 100644 --- a/app/Services/Recurring/GetInvoicePdf.php +++ b/app/Services/Recurring/GetInvoicePdf.php @@ -18,7 +18,7 @@ use App\Services\AbstractService; class GetInvoicePdf extends AbstractService { - public function __construct(public $entity, public ClientContact $contact = null) + public function __construct(public $entity, public ?ClientContact $contact = null) { } diff --git a/tests/Feature/CreditTest.php b/tests/Feature/CreditTest.php index f67aa96db9b4..dd2fc2800771 100644 --- a/tests/Feature/CreditTest.php +++ b/tests/Feature/CreditTest.php @@ -42,7 +42,110 @@ class CreditTest extends TestCase $this->makeTestData(); } + public function testApplicableFilters() + { + Credit::where('company_id',$this->company->id)->cursor()->each(function ($c){ $c->forceDelete(); }); + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertCount(0, $arr['data']); + + $c = Credit::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'status_id' => Credit::STATUS_DRAFT, + 'due_date' => null, + 'date' => now(), + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits'); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertCount(1, $arr['data']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits?applicable=true'); + + $response->assertStatus(200); + $arr = $response->json(); + + $this->assertCount(0, $arr['data']); + + $c->status_id = Credit::STATUS_SENT; + $c->amount = 20; + $c->balance = 20; + $c->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits?applicable=true'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertCount(1, $arr['data']); + + $c->status_id = Credit::STATUS_SENT; + $c->amount = 20; + $c->balance = 20; + $c->due_date = now()->subYear(); + $c->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits?applicable=true'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertCount(0, $arr['data']); + + $c->status_id = Credit::STATUS_SENT; + $c->amount = 20; + $c->balance = 20; + $c->due_date = now()->addYear(); + $c->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits?applicable=true'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertCount(1, $arr['data']); + + $c->status_id = Credit::STATUS_APPLIED; + $c->amount = 20; + $c->balance = 20; + $c->due_date = now()->addYear(); + $c->save(); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/credits?applicable=true'); + + $response->assertStatus(200); + $arr = $response->json(); + $this->assertCount(0, $arr['data']); + + + + } public function testQuoteDownloadPDF() {