scheduler = $scheduler; } public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request) { $scheduler->paused = $request->get('paused', false); $scheduler->archived = (bool)$request->get('archived', false); $scheduler->start_from = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now(); $scheduler->repeat_every = $request->get('repeat_every'); $scheduler->scheduled_run = $request->get('start_from') ? Carbon::parse((int)$request->get('start_from')) : Carbon::now();; $scheduler->save(); if ($this->createJob($request, $scheduler)) { return response(['job_has_been_created'], 200); } return response(['failed_to_create_job'], 400); } public function update(Scheduler $scheduler, UpdateScheduleRequest $request) { $data = $request->validated(); if ($request->has('start_from')) { $data['start_from'] = Carbon::parse((int)$request->get('start_from')); $data['scheduled_run'] = Carbon::parse((int)$request->get('start_from')); } $update = $this->scheduler->update($data); if ($update) { return response(['successfully_updated_scheduler']); } return response(['failed_to_update_scheduler'], 400); } public function createJob(CreateScheduledTaskRequest $request, Scheduler $scheduler): bool { $job = new ScheduledJob(); $job = $this->setJobParameters($job, $request); $job->scheduler_id = $scheduler->id; $job->company_id = auth()->user()->company()->id; return $job->save(); } public function setJobParameters(ScheduledJob $job, $request): ScheduledJob { switch ($request->job) { case ScheduledJob::CREATE_CLIENT_REPORT: $rules = (new GenericReportRequest)->rules(); //custom rules for example here we require date_range but in genericRequest class we don't $rules['date_range'] = 'string|required'; $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_CLIENT_REPORT; $job->action_class = $this->getClassPath(ClientExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_CLIENT_CONTACT_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_CLIENT_CONTACT_REPORT; $job->action_class = $this->getClassPath(ContactExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_CREDIT_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_CREDIT_REPORT; $job->action_class = $this->getClassPath(CreditExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_DOCUMENT_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT; $job->action_class = $this->getClassPath(DocumentExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_EXPENSE_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT; $job->action_class = $this->getClassPath(ExpenseExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_INVOICE_ITEM_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT; $job->action_class = $this->getClassPath(InvoiceItemExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_INVOICE_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_INVOICE_REPORT; $job->action_class = $this->getClassPath(InvoiceExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_PAYMENT_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT; $job->action_class = $this->getClassPath(PaymentExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_PRODUCT_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_PRODUCT_REPORT; $job->action_class = $this->getClassPath(ProductExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT: $rules = (new ProfitLossRequest())->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_PROFIT_AND_LOSS_REPORT; $job->action_class = $this->getClassPath(ProfitAndLoss::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_QUOTE_ITEM_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT; $job->action_class = $this->getClassPath(QuoteItemExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_QUOTE_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_QUOTE_REPORT; $job->action_class = $this->getClassPath(QuoteExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT; $job->action_class = $this->getClassPath(RecurringInvoiceExport::class); $job->parameters = $validatedJobData; break; case ScheduledJob::CREATE_TASK_REPORT: $rules = (new GenericReportRequest)->rules(); $validatedJobData = $request->validate($rules); $job->action_name = ScheduledJob::CREATE_TASK_REPORT; $job->action_class = $this->getClassPath(TaskExport::class); $job->parameters = $validatedJobData; break; } return $job; } public function getClassPath($class): string { return $class = is_object($class) ? get_class($class) : $class; } public function destroy(Scheduler $scheduler) { $job = $scheduler->job; if ($job) { $job->delete(); } $scheduler->delete(); return response(['successfully_deleted_scheduler_and_job_associated_to_him']); } public function updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request) { $job = $scheduler->job; if (!$job) { return abort(404); } $job = $this->setJobParameters($job, $request); $job->save(); return response('job_successfully_updated'); } }