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;
|
|
|
|
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Models\Product;
|
|
|
|
use App\Transformers\ProductTransformer;
|
|
|
|
use App\Utils\Ninja;
|
2023-08-29 11:15:36 +02:00
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
2022-04-28 05:09:17 +02:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use League\Csv\Writer;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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-08-29 11:15:36 +02:00
|
|
|
public function returnJson()
|
|
|
|
{
|
|
|
|
$query = $this->init();
|
|
|
|
|
|
|
|
$headerdisplay = $this->buildHeader();
|
|
|
|
|
|
|
|
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
|
|
|
|
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
$report = $query->cursor()
|
|
|
|
->map(function ($resource) {
|
|
|
|
return $this->buildRow($resource);
|
|
|
|
})->toArray();
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
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-06-21 11:57:17 +02:00
|
|
|
}
|
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);
|
|
|
|
|
2023-08-29 11:15:36 +02:00
|
|
|
return $query;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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-28 05:09:17 +02:00
|
|
|
$query->cursor()
|
2022-06-21 11:57:17 +02:00
|
|
|
->each(function ($entity) {
|
|
|
|
$this->csv->insertOne($this->buildRow($entity));
|
|
|
|
});
|
2022-04-28 05:09:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $this->csv->toString();
|
2022-04-28 05:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildRow(Product $product) :array
|
|
|
|
{
|
|
|
|
$transformed_entity = $this->entity_transformer->transform($product);
|
|
|
|
|
|
|
|
$entity = [];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (array_key_exists($key, $transformed_entity)) {
|
2022-05-10 08:39:05 +02:00
|
|
|
$entity[$keyval] = $transformed_entity[$key];
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2022-05-10 08:39:05 +02:00
|
|
|
$entity[$keyval] = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-28 05:09:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->decorateAdvancedFields($product, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function decorateAdvancedFields(Product $product, array $entity) :array
|
|
|
|
{
|
2022-06-21 11:57: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-06-21 11:57:17 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|