Adjustments for credit filtering

This commit is contained in:
David Bomba 2023-11-11 15:20:04 +11:00
parent f7d033e46d
commit dd411b38ef
4 changed files with 124 additions and 1 deletions

View File

@ -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) {

View File

@ -18,6 +18,7 @@ use App\Services\AbstractService;
class GetInvoicePdf extends AbstractService
{
public function __construct(public Invoice $invoice, public ?ClientContact $contact = null)
{
}

View File

@ -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)
{
}

View File

@ -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()
{