diff --git a/app/DataMapper/Schedule/EmailProductSalesReport.php b/app/DataMapper/Schedule/EmailProductSalesReport.php new file mode 100644 index 000000000000..c8b06253db26 --- /dev/null +++ b/app/DataMapper/Schedule/EmailProductSalesReport.php @@ -0,0 +1,70 @@ +where('company_id', $this->scheduler->company_id) + ->where('is_deleted', 0); + + //Email only the selected clients + + $start_end_dates = $this->calculateStartAndEndDates(); + + if (count($this->scheduler->parameters['clients']) >= 1) { + $query->whereIn('id', $this->transformKeys($this->scheduler->parameters['clients'])); + } + + + $data = [ + 'start_date' => $start_end_dates[0], + 'end_date' => $start_end_dates[1], + 'date_range' => 'custom', + 'client_id' => + ]; + + $export = new ProductSalesExport($this->scheduler->company, $data); + + + //calculate next run dates; + $this->scheduler->calculateNextRun(); + + } + + + + + +} diff --git a/app/Services/Scheduler/EmailStatementService.php b/app/Services/Scheduler/EmailStatementService.php index c1c93ba941c5..8080ef39281d 100644 --- a/app/Services/Scheduler/EmailStatementService.php +++ b/app/Services/Scheduler/EmailStatementService.php @@ -15,10 +15,12 @@ use App\Models\Client; use App\Models\Scheduler; use App\Utils\Traits\MakesHash; use App\DataMapper\Schedule\EmailStatement; +use App\Utils\Traits\MakesDates; class EmailStatementService { use MakesHash; + use MakesDates; private Client $client; diff --git a/app/Services/Scheduler/SchedulerService.php b/app/Services/Scheduler/SchedulerService.php index 1533d26a7485..a40f7fd0e4a2 100644 --- a/app/Services/Scheduler/SchedulerService.php +++ b/app/Services/Scheduler/SchedulerService.php @@ -12,8 +12,9 @@ namespace App\Services\Scheduler; use App\Models\Scheduler; -use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; +use App\Utils\Traits\MakesDates; +use App\Services\Scheduler\EmailProductSalesReport; class SchedulerService { @@ -43,12 +44,15 @@ class SchedulerService (new EmailRecord($this->scheduler))->run(); } - private function email_statement() { (new EmailStatementService($this->scheduler))->run(); } + private function email_product_sales_report() + { + (new EmailProductSalesReport($this->scheduler))->run(); + } /** * Sets the next run date of the scheduled task diff --git a/app/Utils/Traits/MakesDates.php b/app/Utils/Traits/MakesDates.php index 4ff7571ede3c..f63977932db2 100644 --- a/app/Utils/Traits/MakesDates.php +++ b/app/Utils/Traits/MakesDates.php @@ -11,9 +11,10 @@ namespace App\Utils\Traits; -use Carbon\Carbon; use DateTime; use DateTimeZone; +use Carbon\Carbon; +use App\DataMapper\Schedule\EmailStatement; /** * Class MakesDates. @@ -112,4 +113,27 @@ trait MakesDates return 'Invalid date!'; } } + + /** + * Start and end date of the statement + * + * @return array [$start_date, $end_date]; + */ + public function calculateStartAndEndDates(): array + { + return match ($this->scheduler->parameters['date_range']) { + EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + EmailStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + EmailStatement::LAST365 => [now()->startOfDay()->subDays(365)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')], + EmailStatement::THIS_MONTH => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], + EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')], + EmailStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')], + EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')], + EmailStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')], + EmailStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')], + EmailStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']], + default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')], + }; + } + }