mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ class RecurringInvoiceTest extends TestCase
|
||||
$recurring_invoice->line_items = $line_items;
|
||||
|
||||
$recurring_invoice->calc()->getInvoice()->service()->start()->save()->fresh();
|
||||
|
||||
|
||||
(new UpdateRecurring([$recurring_invoice->id], $this->company, $this->user, 'increase_prices', 10))->handle();
|
||||
|
||||
$recurring_invoice->refresh();
|
||||
@ -114,7 +114,7 @@ class RecurringInvoiceTest extends TestCase
|
||||
$recurring_invoice->line_items = $line_items;
|
||||
|
||||
$recurring_invoice->calc()->getInvoice()->service()->start()->save()->fresh();
|
||||
|
||||
|
||||
(new UpdateRecurring([$recurring_invoice->id], $this->company, $this->user, 'update_prices'))->handle();
|
||||
|
||||
$recurring_invoice->refresh();
|
||||
@ -204,7 +204,7 @@ class RecurringInvoiceTest extends TestCase
|
||||
'tax_name3' => '',
|
||||
'tax_rate3' => 0,
|
||||
];
|
||||
|
||||
|
||||
$line_items[] = [
|
||||
'product_key' => 'pink',
|
||||
'notes' => 'test',
|
||||
@ -570,5 +570,87 @@ class RecurringInvoiceTest extends TestCase
|
||||
|
||||
$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