1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Export/CSV/VendorExport.php

191 lines
5.8 KiB
PHP
Raw Normal View History

2023-07-13 07:23:57 +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-07-13 07:23:57 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
2023-12-02 04:09:50 +01:00
use App\Export\Decorators\Decorator;
2023-07-13 07:23:57 +02:00
use App\Libraries\MultiDB;
use App\Models\Company;
2023-10-26 04:57:44 +02:00
use App\Models\Vendor;
2023-07-13 07:23:57 +02:00
use App\Transformers\VendorContactTransformer;
use App\Transformers\VendorTransformer;
use App\Utils\Ninja;
2023-09-12 05:55:37 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-07-13 07:23:57 +02:00
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class VendorExport extends BaseExport
{
private $vendor_transformer;
private $contact_transformer;
public Writer $csv;
2023-12-02 04:09:50 +01:00
private Decorator $decorator;
2023-07-13 07:23:57 +02:00
public string $date_key = 'created_at';
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->vendor_transformer = new VendorTransformer();
$this->contact_transformer = new VendorContactTransformer();
2023-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
2023-07-13 07:23:57 +02:00
}
2023-09-12 05:55:37 +02:00
public function init(): Builder
2023-07-13 07:23:57 +02:00
{
2023-09-12 05:55:37 +02:00
2023-07-13 07:23:57 +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));
//load the CSV document from a string
$this->csv = Writer::createFromString();
2024-05-01 00:32:11 +02:00
\League\Csv\CharsetConverter::addTo($this->csv, 'UTF-8', 'UTF-8');
2023-07-13 07:23:57 +02:00
if (count($this->input['report_keys']) == 0) {
2023-09-12 06:21:42 +02:00
$this->input['report_keys'] = array_values($this->vendor_report_keys);
2023-07-13 07:23:57 +02:00
}
2024-01-14 05:05:00 +01:00
2023-07-13 07:23:57 +02:00
$query = Vendor::query()->with('contacts')
2023-09-12 05:55:37 +02:00
->withTrashed()
->where('company_id', $this->company->id);
2024-06-14 09:09:44 +02:00
if(!$this->input['include_deleted'] ?? false) {
$query->where('is_deleted', 0);
}
2023-07-13 07:23:57 +02:00
$query = $this->addDateRange($query, 'vendors');
2023-07-13 07:23:57 +02:00
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2023-09-12 05:55:37 +02:00
return $query;
}
public function returnJson()
{
$query = $this->init();
$headerdisplay = $this->buildHeader();
2023-10-26 04:57:44 +02:00
$header = collect($this->input['report_keys'])->map(function ($key, $value) use ($headerdisplay) {
return ['identifier' => $key, 'display_value' => $headerdisplay[$value]];
})->toArray();
2023-09-12 05:55:37 +02:00
$report = $query->cursor()
->map(function ($resource) {
2024-08-22 08:45:06 +02:00
2024-07-14 13:28:54 +02:00
/** @var \App\Models\Vendor $resource */
2023-09-12 05:55:37 +02:00
$row = $this->buildRow($resource);
return $this->processMetaData($row, $resource);
})->toArray();
2024-01-14 05:05:00 +01:00
2023-09-12 05:55:37 +02:00
return array_merge(['columns' => $header], $report);
}
public function run()
{
2024-01-14 05:05:00 +01:00
2023-09-12 05:55:37 +02:00
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
2023-07-13 07:23:57 +02:00
$query->cursor()
->each(function ($vendor) {
2024-08-22 08:45:06 +02:00
/** @var \App\Models\Vendor $vendor */
$this->csv->insertOne($this->buildRow($vendor));
2023-07-13 07:23:57 +02:00
});
return $this->csv->toString();
}
2024-01-14 05:05:00 +01:00
private function buildRow(Vendor $vendor): array
2023-07-13 07:23:57 +02:00
{
2023-09-12 06:21:42 +02:00
$transformed_contact = false;
2023-07-13 07:23:57 +02:00
$transformed_vendor = $this->vendor_transformer->transform($vendor);
if ($contact = $vendor->contacts()->first()) {
$transformed_contact = $this->contact_transformer->transform($contact);
}
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
if (is_array($parts) && $parts[0] == 'vendor' && array_key_exists($parts[1], $transformed_vendor)) {
2023-09-12 05:55:37 +02:00
$entity[$key] = $transformed_vendor[$parts[1]];
2023-09-12 09:23:52 +02:00
} elseif (is_array($parts) && $parts[0] == 'vendor_contact' && isset($transformed_contact[$parts[1]])) {
2023-09-12 05:55:37 +02:00
$entity[$key] = $transformed_contact[$parts[1]];
2023-07-13 07:23:57 +02:00
} else {
2023-12-12 00:46:23 +01:00
2023-12-02 04:09:50 +01:00
$entity[$key] = $this->decorator->transform($key, $vendor);
2023-07-13 07:23:57 +02:00
}
}
2023-12-12 00:46:23 +01:00
// return $entity;
return $this->decorateAdvancedFields($vendor, $entity);
2023-07-13 07:23:57 +02:00
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Vendor $vendor, array $entity): array
2023-07-13 07:23:57 +02:00
{
if (in_array('vendor.country_id', $this->input['report_keys'])) {
$entity['country'] = $vendor->country ? ctrans("texts.country_{$vendor->country->name}") : '';
}
if (in_array('vendor.currency', $this->input['report_keys'])) {
$entity['currency'] = $vendor->currency() ? $vendor->currency()->code : $vendor->company->currency()->code;
}
2023-09-28 22:48:50 +02:00
if (in_array('vendor.classification', $this->input['report_keys']) && isset($vendor->classification)) {
$entity['vendor.classification'] = ctrans("texts.{$vendor->classification}") ?? '';
}
2023-12-12 00:46:23 +01:00
if (in_array('vendor.user_id', $this->input['report_keys'])) {
$entity['vendor.user_id'] = $vendor->user ? $vendor->user->present()->name() : '';
}
if (in_array('vendor.assigned_user_id', $this->input['report_keys'])) {
$entity['vendor.assigned_user_id'] = $vendor->assigned_user ? $vendor->assigned_user->present()->name() : '';
}
// $entity['status'] = $this->calculateStatus($vendor);
2023-07-13 07:23:57 +02:00
return $entity;
}
2024-06-17 02:14:04 +02:00
// private function calculateStatus($vendor)
// {
// if ($vendor->is_deleted) {
// return ctrans('texts.deleted');
// }
// if ($vendor->deleted_at) {
// return ctrans('texts.archived');
// }
// return ctrans('texts.active');
// }
2023-07-13 07:23:57 +02:00
}