2022-04-27 10:05:54 +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)
|
2022-04-27 10:05:54 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Export\CSV;
|
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Export\Decorators\Decorator;
|
|
|
|
use App\Libraries\MultiDB;
|
2022-04-27 10:05:54 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Transformers\PaymentTransformer;
|
2023-11-26 08:41:42 +01:00
|
|
|
use App\Utils\Ninja;
|
2023-09-12 03:55:11 +02:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-11-26 08:41:42 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
2022-04-27 10:05:54 +02:00
|
|
|
|
|
|
|
class PaymentExport extends BaseExport
|
|
|
|
{
|
|
|
|
private $entity_transformer;
|
|
|
|
|
2023-03-03 06:44:56 +01:00
|
|
|
public string $date_key = 'date';
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-03-08 21:49:18 +01:00
|
|
|
public Writer $csv;
|
|
|
|
|
2023-11-25 02:38:18 +01:00
|
|
|
private Decorator $decorator;
|
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
public function __construct(Company $company, array $input)
|
|
|
|
{
|
|
|
|
$this->company = $company;
|
|
|
|
$this->input = $input;
|
|
|
|
$this->entity_transformer = new PaymentTransformer();
|
2023-11-25 02:38:18 +01:00
|
|
|
$this->decorator = new Decorator();
|
2022-04-27 10:05:54 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-29 09:50:25 +02:00
|
|
|
private function init(): Builder
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
2023-08-29 09:50:25 +02:00
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
App::forgetInstance('translator');
|
|
|
|
App::setLocale($this->company->locale());
|
|
|
|
$t = app('translator');
|
|
|
|
$t->replace(Ninja::transformTranslations($this->company->settings));
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (count($this->input['report_keys']) == 0) {
|
2023-08-29 09:50:25 +02:00
|
|
|
$this->input['report_keys'] = array_values($this->payment_report_keys);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-05-07 09:10:23 +02:00
|
|
|
|
2023-10-18 06:22:38 +02:00
|
|
|
$this->input['report_keys'] = array_merge($this->input['report_keys'], array_diff($this->forced_client_fields, $this->input['report_keys']));
|
|
|
|
|
2022-11-04 20:54:05 +01:00
|
|
|
$query = Payment::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('company_id', $this->company->id)
|
|
|
|
->where('is_deleted', 0);
|
2022-04-27 10:05:54 +02:00
|
|
|
|
|
|
|
$query = $this->addDateRange($query);
|
2024-02-13 05:25:18 +01:00
|
|
|
|
2024-03-26 04:12:32 +01:00
|
|
|
$clients = &$this->input['client_id'];
|
|
|
|
|
|
|
|
if($clients) {
|
|
|
|
$query = $this->addClientFilter($query, $clients);
|
|
|
|
}
|
|
|
|
|
2024-03-05 03:58:26 +01:00
|
|
|
$query = $this->addPaymentStatusFilters($query, $this->input['status'] ?? '');
|
|
|
|
|
2024-01-29 03:03:54 +01:00
|
|
|
if($this->input['document_email_attachment'] ?? false) {
|
|
|
|
$this->queueDocuments($query);
|
|
|
|
}
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-08-29 09:50:25 +02:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function returnJson()
|
|
|
|
{
|
|
|
|
|
|
|
|
$query = $this->init();
|
|
|
|
|
|
|
|
$headerdisplay = $this->buildHeader();
|
|
|
|
|
|
|
|
$header = collect($this->input['report_keys'])->map(function ($key, $value) use ($headerdisplay) {
|
2023-10-06 18:43:02 +02:00
|
|
|
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
|
2023-08-29 09:50:25 +02:00
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$report = $query->cursor()
|
|
|
|
->map(function ($resource) {
|
2023-09-12 03:55:11 +02:00
|
|
|
$row = $this->buildRow($resource);
|
|
|
|
return $this->processMetaData($row, $resource);
|
2023-08-29 09:50:25 +02:00
|
|
|
})->toArray();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-29 09:50:25 +02:00
|
|
|
return array_merge(['columns' => $header], $report);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$query = $this->init();
|
|
|
|
//load the CSV document from a string
|
|
|
|
$this->csv = Writer::createFromString();
|
|
|
|
|
|
|
|
//insert the header
|
|
|
|
$this->csv->insertOne($this->buildHeader());
|
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
$query->cursor()
|
2022-06-21 11:57:17 +02:00
|
|
|
->each(function ($entity) {
|
|
|
|
$this->csv->insertOne($this->buildRow($entity));
|
|
|
|
});
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this->csv->toString();
|
2022-04-27 10:05:54 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function buildRow(Payment $payment): array
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
|
|
|
$transformed_entity = $this->entity_transformer->transform($payment);
|
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
foreach (array_values($this->input['report_keys']) as $key) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-29 09:50:25 +02:00
|
|
|
$parts = explode('.', $key);
|
2022-05-10 08:27:04 +02:00
|
|
|
|
2023-08-29 10:46:38 +02:00
|
|
|
if (is_array($parts) && $parts[0] == 'payment' && array_key_exists($parts[1], $transformed_entity)) {
|
|
|
|
$entity[$key] = $transformed_entity[$parts[1]];
|
|
|
|
} elseif (array_key_exists($key, $transformed_entity)) {
|
|
|
|
$entity[$key] = $transformed_entity[$key];
|
|
|
|
} else {
|
2023-11-25 02:45:17 +01:00
|
|
|
|
2023-12-02 04:09:50 +01:00
|
|
|
// nlog($key);
|
|
|
|
$entity[$key] = $this->decorator->transform($key, $payment);
|
|
|
|
// $entity[$key] = $this->resolveKey($key, $payment, $this->entity_transformer);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2023-08-29 10:46:38 +02:00
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
}
|
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// return $entity;
|
|
|
|
return $this->decorateAdvancedFields($payment, $entity);
|
2022-04-27 10:05:54 +02:00
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
private function decorateAdvancedFields(Payment $payment, array $entity): array
|
2022-04-27 10:05:54 +02:00
|
|
|
{
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('status_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['status'] = $payment->stringStatus($payment->status_id);
|
|
|
|
// }
|
2022-05-10 08:27:04 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('vendor_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['vendor'] = $payment->vendor()->exists() ? $payment->vendor->name : '';
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('project_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['project'] = $payment->project()->exists() ? $payment->project->name : '';
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('currency_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['currency'] = $payment->currency()->exists() ? $payment->currency->code : '';
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('payment.currency', $this->input['report_keys'])) {
|
|
|
|
// $entity['payment.currency'] = $payment->currency()->exists() ? $payment->currency->code : '';
|
|
|
|
// }
|
2023-07-05 08:33:03 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('exchange_currency_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['exchange_currency'] = $payment->exchange_currency()->exists() ? $payment->exchange_currency->code : '';
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('client_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['client'] = $payment->client->present()->name();
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('type_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['type'] = $payment->translatedType();
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('payment.method', $this->input['report_keys'])) {
|
|
|
|
// $entity['payment.method'] = $payment->translatedType();
|
|
|
|
// }
|
2023-07-05 08:33:03 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('payment.status', $this->input['report_keys'])) {
|
|
|
|
// $entity['payment.status'] = $payment->stringStatus($payment->status_id);
|
|
|
|
// }
|
2023-07-05 08:33:03 +02:00
|
|
|
|
2023-12-12 00:46:23 +01:00
|
|
|
// if (in_array('gateway_type_id', $this->input['report_keys'])) {
|
|
|
|
// $entity['gateway'] = $payment->gateway_type ? $payment->gateway_type->name : 'Unknown Type';
|
|
|
|
// }
|
2022-04-27 10:05:54 +02:00
|
|
|
|
2023-11-22 07:52:02 +01:00
|
|
|
if (in_array('payment.assigned_user_id', $this->input['report_keys'])) {
|
|
|
|
$entity['payment.assigned_user_id'] = $payment->assigned_user ? $payment->assigned_user->present()->name() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('payment.user_id', $this->input['report_keys'])) {
|
|
|
|
$entity['payment.user_id'] = $payment->user ? $payment->user->present()->name() : '';
|
|
|
|
}
|
|
|
|
|
2023-07-05 08:33:03 +02:00
|
|
|
// $entity['invoices'] = $payment->invoices()->exists() ? $payment->invoices->pluck('number')->implode(',') : '';
|
2023-04-24 05:04:52 +02:00
|
|
|
|
2022-04-27 10:05:54 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
}
|