Fixes for react builder

This commit is contained in:
David Bomba 2023-09-12 13:55:37 +10:00
parent bbe138ef79
commit cfca47a542
5 changed files with 77 additions and 110 deletions

View File

@ -12,6 +12,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class ReactBuilder extends Command class ReactBuilder extends Command
{ {
@ -47,6 +48,8 @@ class ReactBuilder extends Command
public function handle() public function handle()
{ {
$includes = ''; $includes = '';
Storage::makeDirectory(public_path('react'));
$directoryIterator = new \RecursiveDirectoryIterator(public_path('react'), \RecursiveDirectoryIterator::SKIP_DOTS); $directoryIterator = new \RecursiveDirectoryIterator(public_path('react'), \RecursiveDirectoryIterator::SKIP_DOTS);

View File

@ -33,6 +33,7 @@ use Illuminate\Database\Eloquent\Builder;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use App\Models\Product; use App\Models\Product;
use App\Models\Task; use App\Models\Task;
use App\Models\Vendor;
class BaseExport class BaseExport
{ {
@ -656,7 +657,7 @@ class BaseExport
// nlog("searching for {$column}"); // nlog("searching for {$column}");
$transformed_invoice = false; $transformed_invoice = false;
if($transformer instanceof PaymentTransformer) { if($transformer instanceof PaymentTransformer && ($entity->invoice ?? false)) {
$transformed_invoices = $transformer->includeInvoices($entity); $transformed_invoices = $transformer->includeInvoices($entity);
$manager = new Manager(); $manager = new Manager();
@ -678,7 +679,7 @@ class BaseExport
} }
if($transformer instanceof TaskTransformer) { if($transformer instanceof TaskTransformer && ($entity->invoice ?? false)) {
$transformed_invoice = $transformer->includeInvoice($entity); $transformed_invoice = $transformer->includeInvoice($entity);
if(!$transformed_invoice) if(!$transformed_invoice)
@ -1060,6 +1061,7 @@ class BaseExport
Payment::class => $entity = 'payment', Payment::class => $entity = 'payment',
Product::class => $entity = 'product', Product::class => $entity = 'product',
Task::class => $entity = 'task', Task::class => $entity = 'task',
Vendor::class => $entity = 'vendor',
default => $entity = 'invoice', default => $entity = 'invoice',
}; };

View File

@ -38,31 +38,6 @@ class TaskExport extends BaseExport
private array $storage_item_array = []; private array $storage_item_array = [];
public array $entity_keys = [
'start_date' => 'start_date',
'end_date' => 'end_date',
'duration' => 'duration',
'rate' => 'rate',
'number' => 'number',
'description' => 'description',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'status' => 'status_id',
'project' => 'project_id',
];
private array $decorate_keys = [
'status',
'project',
'client',
'invoice',
'start_date',
'end_date',
'duration',
];
public function __construct(Company $company, array $input) public function __construct(Company $company, array $input)
{ {
$this->company = $company; $this->company = $company;
@ -140,7 +115,7 @@ class TaskExport extends BaseExport
$this->storage_array = []; $this->storage_array = [];
}); });
nlog($this->storage_item_array);
return array_merge(['columns' => $header], $this->storage_item_array); return array_merge(['columns' => $header], $this->storage_item_array);
} }
@ -161,29 +136,11 @@ class TaskExport extends BaseExport
$entity[$key] = $this->resolveKey($key, $task, $this->entity_transformer); $entity[$key] = $this->resolveKey($key, $task, $this->entity_transformer);
} }
// $keyval = array_search($key, $this->entity_keys);
// if(!$keyval) {
// $keyval = array_search(str_replace("task.", "", $key), $this->entity_keys) ?? $key;
// }
// if(!$keyval) {
// $keyval = $key;
// }
// if (array_key_exists($key, $transformed_entity)) {
// $entity[$keyval] = $transformed_entity[$key];
// } elseif (array_key_exists($keyval, $transformed_entity)) {
// $entity[$keyval] = $transformed_entity[$keyval];
// }
// else {
// $entity[$keyval] = $this->resolveKey($keyval, $task, $this->entity_transformer);
// }
} }
$entity['start_date'] = ''; $entity['task.start_date'] = '';
$entity['end_date'] = ''; $entity['task.end_date'] = '';
$entity['duration'] = ''; $entity['task.duration'] = '';
if (is_null($task->time_log) || (is_array(json_decode($task->time_log, 1)) && count(json_decode($task->time_log, 1)) == 0)) { if (is_null($task->time_log) || (is_array(json_decode($task->time_log, 1)) && count(json_decode($task->time_log, 1)) == 0)) {
$this->storage_array[] = $entity; $this->storage_array[] = $entity;
@ -233,23 +190,23 @@ class TaskExport extends BaseExport
$this->storage_array[] = $entity; $this->storage_array[] = $entity;
unset($entity['start_date']); unset($entity['task.start_date']);
unset($entity['end_date']); unset($entity['task.end_date']);
unset($entity['duration']); unset($entity['task.duration']);
} }
} }
private function decorateAdvancedFields(Task $task, array $entity) :array private function decorateAdvancedFields(Task $task, array $entity) :array
{ {
if (in_array('status_id', $this->input['report_keys'])) { if (in_array('task.status_id', $this->input['report_keys'])) {
$entity['status'] = $task->status()->exists() ? $task->status->name : ''; $entity['task.status_id'] = $task->status()->exists() ? $task->status->name : '';
} }
if (in_array('project_id', $this->input['report_keys'])) { if (in_array('task.project_id', $this->input['report_keys'])) {
$entity['project'] = $task->project()->exists() ? $task->project->name : ''; $entity['task.project_id'] = $task->project()->exists() ? $task->project->name : '';
} }
return $entity; return $entity;
} }
} }

View File

@ -17,6 +17,7 @@ use App\Models\Company;
use App\Transformers\VendorContactTransformer; use App\Transformers\VendorContactTransformer;
use App\Transformers\VendorTransformer; use App\Transformers\VendorTransformer;
use App\Utils\Ninja; use App\Utils\Ninja;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use League\Csv\Writer; use League\Csv\Writer;
@ -31,42 +32,6 @@ class VendorExport extends BaseExport
public string $date_key = 'created_at'; public string $date_key = 'created_at';
public array $entity_keys = [
'address1' => 'vendor.address1',
'address2' => 'vendor.address2',
'city' => 'vendor.city',
'country' => 'vendor.country_id',
'custom_value1' => 'vendor.custom_value1',
'custom_value2' => 'vendor.custom_value2',
'custom_value3' => 'vendor.custom_value3',
'custom_value4' => 'vendor.custom_value4',
'id_number' => 'vendor.id_number',
'name' => 'vendor.name',
'number' => 'vendor.number',
'phone' => 'vendor.phone',
'postal_code' => 'vendor.postal_code',
'private_notes' => 'vendor.private_notes',
'public_notes' => 'vendor.public_notes',
'state' => 'vendor.state',
'vat_number' => 'vendor.vat_number',
'website' => 'vendor.website',
'currency' => 'vendor.currency',
'first_name' => 'vendor_contact.first_name',
'last_name' => 'vendor_contact.last_name',
'contact_phone' => 'vendor_contact.phone',
'contact_custom_value1' => 'vendor_contact.custom_value1',
'contact_custom_value2' => 'vendor_contact.custom_value2',
'contact_custom_value3' => 'vendor_contact.custom_value3',
'contact_custom_value4' => 'vendor_contact.custom_value4',
'email' => 'vendor_contact.email',
'status' => 'vendor.status'
];
private array $decorate_keys = [
'vendor.country_id',
'vendor.currency',
];
public function __construct(Company $company, array $input) public function __construct(Company $company, array $input)
{ {
$this->company = $company; $this->company = $company;
@ -75,8 +40,9 @@ class VendorExport extends BaseExport
$this->contact_transformer = new VendorContactTransformer(); $this->contact_transformer = new VendorContactTransformer();
} }
public function run() public function init(): Builder
{ {
MultiDB::setDb($this->company->db); MultiDB::setDb($this->company->db);
App::forgetInstance('translator'); App::forgetInstance('translator');
App::setLocale($this->company->locale()); App::setLocale($this->company->locale());
@ -89,17 +55,45 @@ class VendorExport extends BaseExport
if (count($this->input['report_keys']) == 0) { if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->entity_keys); $this->input['report_keys'] = array_values($this->entity_keys);
} }
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Vendor::query()->with('contacts') $query = Vendor::query()->with('contacts')
->withTrashed() ->withTrashed()
->where('company_id', $this->company->id) ->where('company_id', $this->company->id)
->where('is_deleted', 0); ->where('is_deleted', 0);
$query = $this->addDateRange($query); $query = $this->addDateRange($query);
return $query;
}
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) {
$row = $this->buildRow($resource);
return $this->processMetaData($row, $resource);
})->toArray();
return array_merge(['columns' => $header], $report);
}
public function run()
{
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query->cursor() $query->cursor()
->each(function ($vendor) { ->each(function ($vendor) {
$this->csv->insertOne($this->buildRow($vendor)); $this->csv->insertOne($this->buildRow($vendor));
@ -123,14 +117,12 @@ class VendorExport extends BaseExport
foreach (array_values($this->input['report_keys']) as $key) { foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key); $parts = explode('.', $key);
$keyval = array_search($key, $this->entity_keys);
if (is_array($parts) && $parts[0] == 'vendor' && array_key_exists($parts[1], $transformed_vendor)) { if (is_array($parts) && $parts[0] == 'vendor' && array_key_exists($parts[1], $transformed_vendor)) {
$entity[$keyval] = $transformed_vendor[$parts[1]]; $entity[$key] = $transformed_vendor[$parts[1]];
} elseif (is_array($parts) && $parts[0] == 'vendor_contact' && array_key_exists($parts[1], $transformed_contact)) { } elseif (is_array($parts) && $parts[0] == 'vendor_contact' && array_key_exists($parts[1], $transformed_contact)) {
$entity[$keyval] = $transformed_contact[$parts[1]]; $entity[$key] = $transformed_contact[$parts[1]];
} else { } else {
$entity[$keyval] = ''; $entity[$key] = $this->resolveKey($key, $vendor, $this->vendor_transformer);
} }
} }

View File

@ -11,11 +11,12 @@
namespace App\Http\Controllers\Reports; namespace App\Http\Controllers\Reports;
use App\Utils\Traits\MakesHash;
use App\Export\CSV\VendorExport; use App\Export\CSV\VendorExport;
use App\Jobs\Report\SendToAdmin;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController; use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest; use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
class VendorReportController extends BaseController class VendorReportController extends BaseController
{ {
@ -30,14 +31,26 @@ class VendorReportController extends BaseController
public function __invoke(GenericReportRequest $request) public function __invoke(GenericReportRequest $request)
{ {
/** @var \App\Models\User $user */
$user = auth()->user();
if ($request->has('send_email') && $request->get('send_email')) { if ($request->has('send_email') && $request->get('send_email')) {
SendToAdmin::dispatch(auth()->user()->company(), $request->all(), VendorExport::class, $this->filename); SendToAdmin::dispatch($user->company(), $request->all(), VendorExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200); return response()->json(['message' => 'working...'], 200);
} }
// expect a list of visible fields, or use the default // expect a list of visible fields, or use the default
$export = new VendorExport(auth()->user()->company(), $request->all()); if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), VendorExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new VendorExport($user->company(), $request->all());
$csv = $export->run(); $csv = $export->run();