From c90d930f8935e633a9592cef2e904a5fa96ae92c Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 14 Apr 2023 10:27:00 +1000 Subject: [PATCH] Fixes for Product Sales Reports --- app/Export/CSV/BaseExport.php | 2 +- app/Export/CSV/ProductSalesExport.php | 4 +- .../ValidationRules/Account/BlackListRule.php | 3 +- .../Scheduler/EmailProductSalesReport.php | 8 +- tests/Feature/Scheduler/SchedulerTest.php | 94 ++++++++++++++++++- 5 files changed, 102 insertions(+), 9 deletions(-) diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 1efe9808efc5..89af8e654ab8 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -39,10 +39,10 @@ class BaseExport return $query->where('client_id', $this->input['client_id']); } elseif(isset($this->input['clients']) && count($this->input['clients']) > 0) { + $this->client_description = 'Multiple Clients'; return $query->whereIn('client_id', $this->input['clients']); } - return $query; } diff --git a/app/Export/CSV/ProductSalesExport.php b/app/Export/CSV/ProductSalesExport.php index b6110dd480f5..e933f06d38da 100644 --- a/app/Export/CSV/ProductSalesExport.php +++ b/app/Export/CSV/ProductSalesExport.php @@ -97,8 +97,6 @@ class ProductSalesExport extends BaseExport } //insert the header - $this->csv->insertOne($this->buildHeader()); - $query = Invoice::query() ->withTrashed() ->where('company_id', $this->company->id) @@ -109,6 +107,8 @@ class ProductSalesExport extends BaseExport $query = $this->filterByClients($query); + $this->csv->insertOne($this->buildHeader()); + $query->cursor() ->each(function ($invoice) { foreach ($invoice->line_items as $item) { diff --git a/app/Http/ValidationRules/Account/BlackListRule.php b/app/Http/ValidationRules/Account/BlackListRule.php index c79d1270afef..740c269a8fef 100644 --- a/app/Http/ValidationRules/Account/BlackListRule.php +++ b/app/Http/ValidationRules/Account/BlackListRule.php @@ -28,7 +28,8 @@ class BlackListRule implements Rule 'dataservices.space', 'karenkey.com', 'sharklasers.com', - '100072641.help' + '100072641.help', + 'yandex.com', ]; /** diff --git a/app/Services/Scheduler/EmailProductSalesReport.php b/app/Services/Scheduler/EmailProductSalesReport.php index d1c3c19dd3a1..596308601bd0 100644 --- a/app/Services/Scheduler/EmailProductSalesReport.php +++ b/app/Services/Scheduler/EmailProductSalesReport.php @@ -42,10 +42,6 @@ class EmailProductSalesReport $start_end_dates = $this->calculateStartAndEndDates(); $data = []; - if (count($this->scheduler->parameters['clients']) >= 1) { - $data['clients'] = $this->transformKeys($this->scheduler->parameters['clients']); - } - $data = [ 'start_date' => $start_end_dates[0], 'end_date' => $start_end_dates[1], @@ -54,6 +50,10 @@ class EmailProductSalesReport 'report_keys' => [] ]; + if (count($this->scheduler->parameters['clients']) >= 1) { + $data['clients'] = $this->transformKeys($this->scheduler->parameters['clients']); + } + $export = (new ProductSalesExport($this->scheduler->company, $data)); $csv = $export->run(); diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index 7f3d3b0b8912..fad7c0e76b53 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -22,11 +22,11 @@ use App\Factory\SchedulerFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Session; use App\DataMapper\Schedule\EmailStatement; -use App\Services\Scheduler\SchedulerService; use Illuminate\Validation\ValidationException; use Illuminate\Foundation\Testing\WithoutEvents; use App\Services\Scheduler\EmailStatementService; use App\Services\Scheduler\EmailProductSalesReport; +use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Routing\Middleware\ThrottleRequests; /** @@ -38,6 +38,7 @@ class SchedulerTest extends TestCase use MakesHash; use MockAccountData; use WithoutEvents; + use DatabaseTransactions; protected function setUp(): void { @@ -58,6 +59,97 @@ class SchedulerTest extends TestCase // $this->withoutExceptionHandling(); } + public function testProductSalesReportGenerationOneClientSeparateParam() + { + $data = [ + 'name' => 'A test product sales scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'email_product_sales_report', + 'parameters' => [ + 'date_range' => EmailStatement::LAST_MONTH, + 'clients' => [], + 'report_keys' => [], + 'client_id' => $this->client->hashed_id, + + ], + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(200); + } + catch(\Exception $e){ + nlog($e->getMessage()); + } + + $arr = $response->json(); + + $id = $this->decodePrimaryKey($arr['data']['id']); + $scheduler = Scheduler::find($id); + $user = $scheduler->user; + $user->email = "{rand(5,555555}@gmail.com"; + $user->save(); + + $this->assertNotNull($scheduler); + + $export = (new EmailProductSalesReport($scheduler))->run(); + + $this->assertEquals(now()->addMonth()->format('Y-m-d'), $scheduler->next_run->format('Y-m-d')); + + } + + public function testProductSalesReportGenerationOneClient() + { + $data = [ + 'name' => 'A test product sales scheduler', + 'frequency_id' => RecurringInvoice::FREQUENCY_MONTHLY, + 'next_run' => now()->format('Y-m-d'), + 'template' => 'email_product_sales_report', + 'parameters' => [ + 'date_range' => EmailStatement::LAST_MONTH, + 'clients' => [$this->client->hashed_id], + 'report_keys' => [], + 'client_id' => null, + + ], + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(200); + } + catch(\Exception $e){ + nlog($e->getMessage()); + } + + $arr = $response->json(); + + $id = $this->decodePrimaryKey($arr['data']['id']); + $scheduler = Scheduler::find($id); + $user = $scheduler->user; + $user->email = "{rand(5,555555}@gmail.com"; + $user->save(); + + $this->assertNotNull($scheduler); + + $export = (new EmailProductSalesReport($scheduler))->run(); + + $this->assertEquals(now()->addMonth()->format('Y-m-d'), $scheduler->next_run->format('Y-m-d')); + + } public function testProductSalesReportGeneration() {