2022-05-23 01:33:56 +02:00
|
|
|
<?php
|
2022-05-26 04:18:10 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
2022-05-23 01:33:56 +02:00
|
|
|
|
|
|
|
namespace App\Services\TaskScheduler;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Export\CSV\ClientExport;
|
|
|
|
use App\Export\CSV\ContactExport;
|
|
|
|
use App\Export\CSV\CreditExport;
|
|
|
|
use App\Export\CSV\DocumentExport;
|
|
|
|
use App\Export\CSV\ExpenseExport;
|
|
|
|
use App\Export\CSV\InvoiceExport;
|
|
|
|
use App\Export\CSV\InvoiceItemExport;
|
|
|
|
use App\Export\CSV\PaymentExport;
|
|
|
|
use App\Export\CSV\ProductExport;
|
|
|
|
use App\Export\CSV\QuoteExport;
|
|
|
|
use App\Export\CSV\QuoteItemExport;
|
|
|
|
use App\Export\CSV\RecurringInvoiceExport;
|
|
|
|
use App\Export\CSV\TaskExport;
|
|
|
|
use App\Http\Requests\Report\GenericReportRequest;
|
|
|
|
use App\Http\Requests\Report\ProfitLossRequest;
|
|
|
|
use App\Http\Requests\TaskScheduler\CreateScheduledTaskRequest;
|
|
|
|
use App\Http\Requests\TaskScheduler\UpdateScheduledJobRequest;
|
|
|
|
use App\Http\Requests\TaskScheduler\UpdateScheduleRequest;
|
|
|
|
use App\Jobs\Report\ProfitAndLoss;
|
2022-05-26 04:18:10 +02:00
|
|
|
use App\Models\Company;
|
2022-05-23 01:33:56 +02:00
|
|
|
use App\Models\ScheduledJob;
|
|
|
|
use App\Models\Scheduler;
|
2022-05-26 04:18:10 +02:00
|
|
|
use App\Utils\Ninja;
|
2022-05-23 01:33:56 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
class TaskSchedulerService
|
|
|
|
{
|
|
|
|
public Scheduler $scheduler;
|
|
|
|
|
|
|
|
public function __construct(Scheduler $scheduler)
|
|
|
|
{
|
|
|
|
$this->scheduler = $scheduler;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Scheduler $scheduler, CreateScheduledTaskRequest $request)
|
|
|
|
{
|
|
|
|
$scheduler->paused = $request->get('paused', 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();;
|
2022-05-23 21:57:48 +02:00
|
|
|
$scheduler->company_id = auth()->user()->company()->id;
|
2022-05-23 01:33:56 +02:00
|
|
|
$scheduler->save();
|
2022-05-27 05:10:32 +02:00
|
|
|
|
2022-05-26 04:18:10 +02:00
|
|
|
$this->createJob($request, $scheduler);
|
2022-05-23 01:33:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(Scheduler $scheduler, UpdateScheduleRequest $request)
|
|
|
|
{
|
2022-05-23 21:57:48 +02:00
|
|
|
|
2022-05-23 01:33:56 +02:00
|
|
|
$data = $request->validated();
|
2022-05-23 21:57:48 +02:00
|
|
|
|
2022-05-23 01:33:56 +02:00
|
|
|
$update = $this->scheduler->update($data);
|
|
|
|
if ($update) {
|
2022-05-23 21:57:48 +02:00
|
|
|
return response(['successfully_updated_scheduler'], 200);
|
2022-05-23 01:33:56 +02:00
|
|
|
}
|
|
|
|
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';
|
2022-05-27 09:01:15 +02:00
|
|
|
|
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_CLIENT_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(ClientExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_CLIENT_CONTACT_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_CLIENT_CONTACT_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(ContactExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_CREDIT_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_CREDIT_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(CreditExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_DOCUMENT_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_DOCUMENT_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(DocumentExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_EXPENSE_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_EXPENSE_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(ExpenseExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_INVOICE_ITEM_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_INVOICE_ITEM_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(InvoiceItemExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_INVOICE_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_INVOICE_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(InvoiceExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_PAYMENT_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_PAYMENT_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(PaymentExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_PRODUCT_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$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:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$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:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_QUOTE_ITEM_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(QuoteItemExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_QUOTE_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_QUOTE_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(QuoteExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_RECURRING_INVOICE_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$job->action_name = ScheduledJob::CREATE_RECURRING_INVOICE_REPORT;
|
|
|
|
$job->action_class = $this->getClassPath(RecurringInvoiceExport::class);
|
|
|
|
$job->parameters = $validatedJobData;
|
|
|
|
break;
|
|
|
|
case ScheduledJob::CREATE_TASK_REPORT:
|
2022-05-27 09:01:15 +02:00
|
|
|
$validator = GenericReportRequest::runFormRequest($request->all());
|
|
|
|
$validatedJobData = $validator->validate();
|
2022-05-23 01:33:56 +02:00
|
|
|
$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 updateJob(Scheduler $scheduler, UpdateScheduledJobRequest $request)
|
|
|
|
{
|
|
|
|
$job = $scheduler->job;
|
|
|
|
if (!$job) {
|
|
|
|
return abort(404);
|
|
|
|
}
|
|
|
|
$job = $this->setJobParameters($job, $request);
|
|
|
|
$job->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|