mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-02 19:14:35 -04:00
feat: Recurring Invoices product_key filter
This commit is contained in:
parent
4bd3bad6f0
commit
35251cd572
@ -121,4 +121,29 @@ class RecurringInvoiceFilters extends QueryFilters
|
|||||||
{
|
{
|
||||||
return $this->builder->company();
|
return $this->builder->company();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter based on line_items product_key
|
||||||
|
*
|
||||||
|
* @param string value Product keys
|
||||||
|
* @return Builder
|
||||||
|
*/
|
||||||
|
public function product_key(string $value = ''): Builder
|
||||||
|
{
|
||||||
|
if (strlen($value) == 0) {
|
||||||
|
return $this->builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
$key_parameters = explode(',', $value);
|
||||||
|
|
||||||
|
if (count($key_parameters)) {
|
||||||
|
return $this->builder->where(function ($query) use ($key_parameters) {
|
||||||
|
foreach ($key_parameters as $key) {
|
||||||
|
$query->orWhereJsonContains('line_items', ['product_key' => $key]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -571,4 +571,86 @@ class RecurringInvoiceTest extends TestCase
|
|||||||
$this->assertEquals(null, $invoice->subscription_id);
|
$this->assertEquals(null, $invoice->subscription_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFilterProductKey()
|
||||||
|
{
|
||||||
|
$p1 = Product::factory()->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'cost' => 10,
|
||||||
|
'price' => 10,
|
||||||
|
'product_key' => $this->faker->word,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$p2 = Product::factory()->create([
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'user_id' => $this->user->id,
|
||||||
|
'cost' => 20,
|
||||||
|
'price' => 20,
|
||||||
|
'product_key' => $this->faker->word,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$recurring_invoice = RecurringInvoiceFactory::create($this->company->id, $this->user->id);
|
||||||
|
$recurring_invoice->client_id = $this->client->id;
|
||||||
|
$recurring_invoice->line_items = [[
|
||||||
|
'product_key' => $p1->product_key,
|
||||||
|
'notes' => 'test',
|
||||||
|
'cost' => 20,
|
||||||
|
'quantity' => 1,
|
||||||
|
'tax_name1' => '',
|
||||||
|
'tax_rate1' => 0,
|
||||||
|
'tax_name2' => '',
|
||||||
|
'tax_rate2' => 0,
|
||||||
|
'tax_name3' => '',
|
||||||
|
'tax_rate3' => 0,
|
||||||
|
]];
|
||||||
|
|
||||||
|
$recurring_invoice->calc()->getInvoice()->service()->start()->save()->fresh();
|
||||||
|
|
||||||
|
$recurring_invoice2 = RecurringInvoiceFactory::create($this->company->id, $this->user->id);
|
||||||
|
$recurring_invoice2->client_id = $this->client->id;
|
||||||
|
$recurring_invoice2->line_items = [[
|
||||||
|
'product_key' => $p2->product_key,
|
||||||
|
'notes' => 'test',
|
||||||
|
'cost' => 10,
|
||||||
|
'quantity' => 1,
|
||||||
|
'tax_name1' => '',
|
||||||
|
'tax_rate1' => 0,
|
||||||
|
'tax_name2' => '',
|
||||||
|
'tax_rate2' => 0,
|
||||||
|
'tax_name3' => '',
|
||||||
|
'tax_rate3' => 0,
|
||||||
|
]];
|
||||||
|
|
||||||
|
$recurring_invoice2->calc()->getInvoice()->service()->start()->save()->fresh();
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->get('/api/v1/recurring_invoices?product_key=' . $this->faker->word)
|
||||||
|
->assertStatus(200);
|
||||||
|
|
||||||
|
$arr = $response->json();
|
||||||
|
|
||||||
|
$this->assertEquals('0', $arr['meta']['pagination']['total']);
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->get('/api/v1/recurring_invoices?product_key=' . $p1->product_key)
|
||||||
|
->assertStatus(200);
|
||||||
|
|
||||||
|
$arr = $response->json();
|
||||||
|
|
||||||
|
$this->assertEquals('1', $arr['meta']['pagination']['total']);
|
||||||
|
|
||||||
|
$response = $this->withHeaders([
|
||||||
|
'X-API-SECRET' => config('ninja.api_secret'),
|
||||||
|
'X-API-TOKEN' => $this->token,
|
||||||
|
])->get('/api/v1/recurring_invoices?product_key=' . $p1->product_key .',' . $p2->product_key)
|
||||||
|
->assertStatus(200);
|
||||||
|
|
||||||
|
$arr = $response->json();
|
||||||
|
|
||||||
|
$this->assertEquals('2', $arr['meta']['pagination']['total']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user