1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Services/Scheduler/EmailReport.php

141 lines
6.0 KiB
PHP
Raw Normal View History

2023-04-13 05:31:19 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2023-04-13 05:31:19 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Scheduler;
use App\Models\Client;
use App\Models\Scheduler;
use App\Mail\DownloadReport;
use App\Export\CSV\TaskExport;
use App\Export\CSV\QuoteExport;
use App\Utils\Traits\MakesHash;
2023-04-14 08:44:04 +02:00
use App\Export\CSV\ClientExport;
2023-10-26 04:57:44 +02:00
use App\Export\CSV\CreditExport;
use App\Utils\Traits\MakesDates;
use App\Export\CSV\ContactExport;
2023-04-14 08:44:04 +02:00
use App\Export\CSV\ExpenseExport;
use App\Export\CSV\InvoiceExport;
use App\Export\CSV\PaymentExport;
use App\Export\CSV\ProductExport;
2023-10-26 04:57:44 +02:00
use App\Jobs\Mail\NinjaMailerJob;
use App\Export\CSV\ActivityExport;
use App\Export\CSV\DocumentExport;
use App\Export\CSV\QuoteItemExport;
use App\Services\Report\ProfitLoss;
2023-04-13 07:39:12 +02:00
use App\Jobs\Mail\NinjaMailerObject;
use App\Export\CSV\InvoiceItemExport;
use App\Export\CSV\ProductSalesExport;
2023-04-14 07:46:49 +02:00
use App\Services\Report\ARDetailReport;
use App\Services\Report\ARSummaryReport;
2023-10-26 04:57:44 +02:00
use App\Services\Report\UserSalesReport;
use App\Services\Report\TaxSummaryReport;
use App\Export\CSV\RecurringInvoiceExport;
use App\Services\Report\ClientSalesReport;
use App\Services\Report\ClientBalanceReport;
2023-04-14 07:46:49 +02:00
class EmailReport
2023-04-13 05:31:19 +02:00
{
use MakesHash;
use MakesDates;
private Client $client;
private bool $multiple_clients = false;
2023-04-14 07:46:49 +02:00
private string $file_name = 'file.csv';
2023-04-13 07:39:12 +02:00
2023-04-13 05:31:19 +02:00
public function __construct(public Scheduler $scheduler)
{
}
public function run()
{
2024-01-14 05:05:00 +01:00
2023-06-26 07:14:58 +02:00
$start_end_dates = $this->calculateStartAndEndDates($this->scheduler->parameters);
2024-02-04 05:06:24 +01:00
$data = $this->scheduler->parameters;
2024-01-14 05:05:00 +01:00
2024-02-04 05:06:24 +01:00
$data['start_date'] = $start_end_dates[0];
$data['end_date'] = $start_end_dates[1];
$data['date_range'] = $data['date_range'] ?? 'all';
$data['report_keys'] = $data['report_keys'] ?? [];
2024-02-13 05:25:18 +01:00
2023-04-14 07:46:49 +02:00
$export = false;
2023-10-26 04:57:44 +02:00
match($this->scheduler->parameters['report_name']) {
2023-05-17 02:45:56 +02:00
'product_sales' => $export = (new ProductSalesExport($this->scheduler->company, $data)),
'ar_detailed' => $export = (new ARDetailReport($this->scheduler->company, $data)),
'ar_summary' => $export = (new ARSummaryReport($this->scheduler->company, $data)),
'tax_summary' => $export = (new TaxSummaryReport($this->scheduler->company, $data)),
'client_balance' => $export = (new ClientBalanceReport($this->scheduler->company, $data)),
'client_sales' => $export = (new ClientSalesReport($this->scheduler->company, $data)),
'user_sales' => $export = (new UserSalesReport($this->scheduler->company, $data)),
2023-04-14 08:44:04 +02:00
'profitloss' => $export = (new ProfitLoss($this->scheduler->company, $data)),
'activity' => $export = (new ActivityExport($this->scheduler->company, $data)),
'activities' => $export = (new ActivityExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'client' => $export = (new ClientExport($this->scheduler->company, $data)),
'clients' => $export = (new ClientExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'client_contact' => $export = (new ContactExport($this->scheduler->company, $data)),
'client_contacts' => $export = (new ContactExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'credit' => $export = (new CreditExport($this->scheduler->company, $data)),
'credits' => $export = (new CreditExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'document' => $export = (new DocumentExport($this->scheduler->company, $data)),
'documents' => $export = (new DocumentExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'expense' => $export = (new ExpenseExport($this->scheduler->company, $data)),
'expenses' => $export = (new ExpenseExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'invoice' => $export = (new InvoiceExport($this->scheduler->company, $data)),
'invoices' => $export = (new InvoiceExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'invoice_item' => $export = (new InvoiceItemExport($this->scheduler->company, $data)),
'invoice_items' => $export = (new InvoiceItemExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'quote' => $export = (new QuoteExport($this->scheduler->company, $data)),
'quotes' => $export = (new QuoteExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'quote_item' => $export = (new QuoteItemExport($this->scheduler->company, $data)),
'quote_items' => $export = (new QuoteItemExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'recurring_invoice' => $export = (new RecurringInvoiceExport($this->scheduler->company, $data)),
'recurring_invoices' => $export = (new RecurringInvoiceExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'payment' => $export = (new PaymentExport($this->scheduler->company, $data)),
'payments' => $export = (new PaymentExport($this->scheduler->company, $data)),
2023-12-10 23:25:58 +01:00
'product' => $export = (new ProductExport($this->scheduler->company, $data)),
'products' => $export = (new ProductExport($this->scheduler->company, $data)),
'tasks' => $export = (new TaskExport($this->scheduler->company, $data)),
2023-04-14 07:46:49 +02:00
default => $export = false,
};
2024-01-14 05:05:00 +01:00
2023-04-14 07:46:49 +02:00
if(!$export) {
$this->cancelSchedule();
return;
}
2023-04-13 07:39:12 +02:00
$csv = $export->run();
2023-04-13 08:00:59 +02:00
//todo - potentially we send this to more than one user.
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
2023-04-13 07:39:12 +02:00
$nmo->mailable = new DownloadReport($this->scheduler->company, $csv, $this->file_name);
$nmo->company = $this->scheduler->company;
$nmo->settings = $this->scheduler->company->settings;
$nmo->to_user = $this->scheduler->user;
NinjaMailerJob::dispatch($nmo);
2023-04-13 05:31:19 +02:00
//calculate next run dates;
$this->scheduler->calculateNextRun();
2024-01-14 05:05:00 +01:00
2023-04-13 05:31:19 +02:00
}
2023-04-14 07:46:49 +02:00
private function cancelSchedule()
{
$this->scheduler->forceDelete();
}
2024-01-14 05:05:00 +01:00
2023-04-13 05:31:19 +02:00
}