diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index 4fcdee6d1afc..00e2797d8a55 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -272,9 +272,9 @@ class BaseTransformer /** * @param $email * - * @return ?Contact + * @return ?ClientContact */ - public function getContact($email) + public function getContact($email): ?ClientContact { $contact = ClientContact::where('company_id', $this->company->id) ->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [ diff --git a/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php b/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php index b7b35926a44e..86b9d22d8e17 100644 --- a/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php +++ b/app/Import/Transformer/Invoice2Go/InvoiceTransformer.php @@ -61,8 +61,14 @@ class InvoiceTransformer extends BaseTransformer ], ]; - $client_id = - $this->getClient($this->getString($invoice_data, 'Name'), $this->getString($invoice_data, 'EmailRecipient')); + $client_id = null; + + if($this->hasClient($this->getString($invoice_data, 'Name') || $this->getContact($this->getString($invoice_data, 'EmailRecipient')))) + { + + $client_id = $this->getClient($this->getString($invoice_data, 'Name'), $this->getString($invoice_data, 'EmailRecipient')); + + } if ($client_id) { $transformed['client_id'] = $client_id; diff --git a/app/Services/Scheduler/EmailProductSalesReport.php b/app/Services/Scheduler/EmailProductSalesReport.php index b9b0a18099a2..19c6d5b173df 100644 --- a/app/Services/Scheduler/EmailProductSalesReport.php +++ b/app/Services/Scheduler/EmailProductSalesReport.php @@ -46,7 +46,6 @@ class EmailProductSalesReport $data['clients'] = $this->transformKeys($this->scheduler->parameters['clients']); } - $data = [ 'start_date' => $start_end_dates[0], 'end_date' => $start_end_dates[1], @@ -57,6 +56,8 @@ class EmailProductSalesReport $export = (new ProductSalesExport($this->scheduler->company, $data)); $csv = $export->run(); + //todo - potentially we send this to more than one user. + $nmo = new NinjaMailerObject; $nmo->mailable = new DownloadReport($this->scheduler->company, $csv, $this->file_name); $nmo->company = $this->scheduler->company; @@ -65,7 +66,6 @@ class EmailProductSalesReport NinjaMailerJob::dispatch($nmo); - //calculate next run dates; $this->scheduler->calculateNextRun(); diff --git a/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php b/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php index d88313f0cf93..f1f442153422 100644 --- a/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php +++ b/tests/Feature/Import/Invoice2Go/Invoice2GoTest.php @@ -122,6 +122,7 @@ class Invoice2GoTest extends TestCase $this->assertInstanceOf(Client::class, $client); $this->assertEquals('840', $client->country_id); + $this->assertEquals('wade@projectx.net', $client->contacts->first()->email); $this->assertEquals('2584 Sesame Street', $client->address1); $this->assertTrue($base_transformer->hasInvoice('1')); diff --git a/tests/Feature/Scheduler/SchedulerTest.php b/tests/Feature/Scheduler/SchedulerTest.php index 222c21f5e4a6..018855888b95 100644 --- a/tests/Feature/Scheduler/SchedulerTest.php +++ b/tests/Feature/Scheduler/SchedulerTest.php @@ -26,6 +26,7 @@ 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\Routing\Middleware\ThrottleRequests; /** @@ -75,6 +76,39 @@ class SchedulerTest extends TestCase ])->postJson('/api/v1/task_schedulers', $data); $response->assertStatus(200); + + $arr = $response->json(); + + $id = $this->decodePrimaryKey($arr['data']['id']); + $scheduler = Scheduler::find($id); + + $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 testProductSalesReportStore() + { + $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' => [], + ], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/task_schedulers', $data); + + $response->assertStatus(200); }