1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Export/CSV/ProductExport.php

144 lines
4.0 KiB
PHP
Raw Normal View History

2022-04-28 05:09:17 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-04-28 05:09:17 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Export\CSV;
2023-12-02 04:09:50 +01:00
use App\Utils\Ninja;
use League\Csv\Writer;
2022-04-28 05:09:17 +02:00
use App\Models\Company;
use App\Models\Product;
2023-12-02 04:09:50 +01:00
use App\Libraries\MultiDB;
use Illuminate\Support\Facades\App;
use App\Export\Decorators\Decorator;
2022-04-28 05:09:17 +02:00
use App\Transformers\ProductTransformer;
2023-09-12 03:55:11 +02:00
use Illuminate\Database\Eloquent\Builder;
2022-04-28 05:09:17 +02:00
class ProductExport extends BaseExport
{
private $entity_transformer;
2023-03-08 08:02:04 +01:00
public string $date_key = 'created_at';
2022-04-28 05:09:17 +02:00
2023-03-08 21:49:18 +01:00
public Writer $csv;
2023-12-02 04:09:50 +01:00
private Decorator $decorator;
2022-04-28 05:09:17 +02:00
public function __construct(Company $company, array $input)
{
$this->company = $company;
$this->input = $input;
$this->entity_transformer = new ProductTransformer();
2023-12-02 04:09:50 +01:00
$this->decorator = new Decorator();
2022-04-28 05:09:17 +02:00
}
2023-08-29 11:15:36 +02:00
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-08-29 11:15:36 +02:00
$report = $query->cursor()
->map(function ($resource) {
2023-09-12 03:55:11 +02:00
$row = $this->buildRow($resource);
2023-10-26 04:57:44 +02:00
return $this->processMetaData($row, $resource);
2023-08-29 11:15:36 +02:00
})->toArray();
2024-01-14 05:05:00 +01:00
2023-08-29 11:15:36 +02:00
return array_merge(['columns' => $header], $report);
}
private function init(): Builder
2022-04-28 05:09:17 +02:00
{
2023-08-29 11:15:36 +02:00
2022-04-28 05:09:17 +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));
if (count($this->input['report_keys']) == 0) {
2023-08-29 11:15:36 +02:00
$this->input['report_keys'] = array_values($this->product_report_keys);
}
2022-05-07 09:10:23 +02:00
2022-11-04 20:54:05 +01:00
$query = Product::query()
->withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0);
2022-04-28 05:09:17 +02:00
$query = $this->addDateRange($query);
if($this->input['document_email_attachment'] ?? false) {
$this->queueDocuments($query);
}
2022-04-28 05:09:17 +02:00
2023-08-29 11:15:36 +02:00
return $query;
}
public function run()
{
2024-01-14 05:05:00 +01:00
2023-08-29 11:15:36 +02:00
$query = $this->init();
//load the CSV document from a string
$this->csv = Writer::createFromString();
//insert the header
$this->csv->insertOne($this->buildHeader());
2022-04-28 05:09:17 +02:00
$query->cursor()
->each(function ($entity) {
$this->csv->insertOne($this->buildRow($entity));
});
2022-04-28 05:09:17 +02:00
return $this->csv->toString();
2022-04-28 05:09:17 +02:00
}
2024-01-14 05:05:00 +01:00
private function buildRow(Product $product): array
2022-04-28 05:09:17 +02:00
{
$transformed_entity = $this->entity_transformer->transform($product);
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
2023-08-29 11:15:36 +02:00
$keyval = array_search($key, $this->product_report_keys);
2022-05-10 08:39:05 +02:00
if (array_key_exists($key, $transformed_entity)) {
2022-05-10 08:39:05 +02:00
$entity[$keyval] = $transformed_entity[$key];
} else {
2023-12-02 04:09:50 +01:00
// nlog($key);
$entity[$key] = $this->decorator->transform($key, $product);
// $entity[$key] = '';
}
2022-04-28 05:09:17 +02:00
}
2023-12-02 04:09:50 +01:00
return $entity;
// return $this->decorateAdvancedFields($product, $entity);
2022-04-28 05:09:17 +02:00
}
2024-01-14 05:05:00 +01:00
private function decorateAdvancedFields(Product $product, array $entity): array
2022-04-28 05:09:17 +02:00
{
if (in_array('vendor_id', $this->input['report_keys'])) {
2022-05-10 08:39:05 +02:00
$entity['vendor'] = $product->vendor()->exists() ? $product->vendor->name : '';
}
2022-04-28 05:09:17 +02:00
2023-04-26 09:41:30 +02:00
// if (array_key_exists('project_id', $this->input['report_keys'])) {
// $entity['project'] = $product->project()->exists() ? $product->project->name : '';
// }
2022-04-28 05:09:17 +02:00
return $entity;
}
}