diff --git a/app/Filters/InvoiceFilters.php b/app/Filters/InvoiceFilters.php index 8ade4be3a6a1..91e426192fde 100644 --- a/app/Filters/InvoiceFilters.php +++ b/app/Filters/InvoiceFilters.php @@ -229,6 +229,32 @@ class InvoiceFilters extends QueryFilters return $this->builder->where('due_date', '>=', $date); } + public function date_range(string $date_range = ''): Builder + { + $parts = explode(",", $date_range); + + if (count($parts) != 3) { + return $this->builder; + } + + if(!in_array($parts[0], ['date','due_date'])) { + return $this->builder; + } + + try{ + + $start_date = Carbon::parse($parts[1]); + $end_date = Carbon::parse($parts[2]); + + return $this->builder->whereBetween($parts[0], [$start_date, $end_date]); + } + + catch(\Exception $e){ + return $this->builder; + } + + return $this->builder; + } /** * Sorts the list based on $sort. diff --git a/app/Filters/PaymentFilters.php b/app/Filters/PaymentFilters.php index b6c82430baad..af094202aaed 100644 --- a/app/Filters/PaymentFilters.php +++ b/app/Filters/PaymentFilters.php @@ -12,8 +12,9 @@ namespace App\Filters; use App\Models\Payment; -use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Support\Carbon; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder; /** * PaymentFilters. @@ -177,6 +178,33 @@ class PaymentFilters extends QueryFilters return $this->builder->orderBy($sort_col[0], $sort_col[1]); } + public function date_range(string $date_range = ''): Builder + { + $parts = explode(",", $date_range); + + if (count($parts) != 3) { + return $this->builder; + } + + if(!in_array($parts[0], ['date'])) { + return $this->builder; + } + + try{ + + $start_date = Carbon::parse($parts[1]); + $end_date = Carbon::parse($parts[2]); + + return $this->builder->whereBetween($parts[0], [$start_date, $end_date]); + } + + catch(\Exception $e){ + return $this->builder; + } + + return $this->builder; + } + /** * Filters the query by the users company ID. * diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index 8730566b12b0..e0530f5b2056 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -48,6 +48,70 @@ class InvoiceTest extends TestCase $this->makeTestData(); } + public function testInvoiceGetDatesBetween() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=date,2023-01-01,2023-01-01', ) + ->assertStatus(200); + } + + public function testInvoiceGetDatesBetween2() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=date', ) + ->assertStatus(200); + } + + public function testInvoiceGetDatesBetween3() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=x', ) + ->assertStatus(200); + } + + public function testInvoiceGetDatesBetween4() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=date,2023223123,312312321', ) + ->assertStatus(200); + } + + public function testInvoiceGetDatesBetween5() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=date,x,23423', ) + ->assertStatus(200); + } + + public function testInvoiceGetDatesBetween6() + { + Invoice::factory()->count(10)->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $this->client->id, + 'date' => '1971-01-02', + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/invoices?date_range=date,1971-01-01,1971-01-03', ) + ->assertStatus(200); + + $arr = $response->json(); + + $this->assertCount(10, $arr['data']); + } public function testInvoiceGetPaidReversedInvoice() { @@ -66,7 +130,6 @@ class InvoiceTest extends TestCase $this->assertCount(1, $arr['data']); } - public function testInvoiceGetPaidInvoices() { $response = $this->withHeaders([ diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index 7a69e0b0a3ab..25cd22f894b2 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -60,7 +60,80 @@ class PaymentTest extends TestCase ); } - public function testPatymentGetClientStatus() + public function testPaymentGetBetweenQuery1() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range=date,2023-01-01,2023-02-01'); + + $response->assertStatus(200); + } + + public function testPaymentGetBetweenQuery2() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range='); + + $response->assertStatus(200); + } + + public function testPaymentGetBetweenQuery3() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range=1,1,1,1,1'); + + $response->assertStatus(200); + } + + public function testPaymentGetBetweenQuery4() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range=date,34343,34343434343'); + + $response->assertStatus(200); + } + + public function testPaymentGetBetweenQuery5() + { + Payment::factory()->count(10)->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $this->client->id, + 'date' => '2023-01-02', + ]); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range=date,2023-01-01,2023-01-03'); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertCount(10, $arr['data']); + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?date_range=date,2053-10-01,2053-10-03'); + + $response->assertStatus(200); + + $arr = $response->json(); + + $this->assertCount(0, $arr['data']); + + } + + public function testPaymentGetClientStatus() { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'),