From cfca47a542e038922b739af26ea1a8a6b5405e9b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 12 Sep 2023 13:55:37 +1000 Subject: [PATCH] Fixes for react builder --- app/Console/Commands/ReactBuilder.php | 3 + app/Export/CSV/BaseExport.php | 6 +- app/Export/CSV/TaskExport.php | 67 +++----------- app/Export/CSV/VendorExport.php | 90 +++++++++---------- .../Reports/VendorReportController.php | 21 ++++- 5 files changed, 77 insertions(+), 110 deletions(-) diff --git a/app/Console/Commands/ReactBuilder.php b/app/Console/Commands/ReactBuilder.php index 430b16d90542..4bec0cefb5c5 100644 --- a/app/Console/Commands/ReactBuilder.php +++ b/app/Console/Commands/ReactBuilder.php @@ -12,6 +12,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Facades\Storage; class ReactBuilder extends Command { @@ -47,6 +48,8 @@ class ReactBuilder extends Command public function handle() { $includes = ''; + + Storage::makeDirectory(public_path('react')); $directoryIterator = new \RecursiveDirectoryIterator(public_path('react'), \RecursiveDirectoryIterator::SKIP_DOTS); diff --git a/app/Export/CSV/BaseExport.php b/app/Export/CSV/BaseExport.php index 12f48e057b15..698752b08f79 100644 --- a/app/Export/CSV/BaseExport.php +++ b/app/Export/CSV/BaseExport.php @@ -33,6 +33,7 @@ use Illuminate\Database\Eloquent\Builder; use League\Fractal\Serializer\ArraySerializer; use App\Models\Product; use App\Models\Task; +use App\Models\Vendor; class BaseExport { @@ -656,7 +657,7 @@ class BaseExport // nlog("searching for {$column}"); $transformed_invoice = false; - if($transformer instanceof PaymentTransformer) { + if($transformer instanceof PaymentTransformer && ($entity->invoice ?? false)) { $transformed_invoices = $transformer->includeInvoices($entity); $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); if(!$transformed_invoice) @@ -1060,6 +1061,7 @@ class BaseExport Payment::class => $entity = 'payment', Product::class => $entity = 'product', Task::class => $entity = 'task', + Vendor::class => $entity = 'vendor', default => $entity = 'invoice', }; diff --git a/app/Export/CSV/TaskExport.php b/app/Export/CSV/TaskExport.php index d7cb21e9c235..30cf6379c02f 100644 --- a/app/Export/CSV/TaskExport.php +++ b/app/Export/CSV/TaskExport.php @@ -38,31 +38,6 @@ class TaskExport extends BaseExport 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) { $this->company = $company; @@ -140,7 +115,7 @@ class TaskExport extends BaseExport $this->storage_array = []; }); - + nlog($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); } - // $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['end_date'] = ''; - $entity['duration'] = ''; + $entity['task.start_date'] = ''; + $entity['task.end_date'] = ''; + $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)) { $this->storage_array[] = $entity; @@ -233,23 +190,23 @@ class TaskExport extends BaseExport $this->storage_array[] = $entity; - unset($entity['start_date']); - unset($entity['end_date']); - unset($entity['duration']); + unset($entity['task.start_date']); + unset($entity['task.end_date']); + unset($entity['task.duration']); } } private function decorateAdvancedFields(Task $task, array $entity) :array { - if (in_array('status_id', $this->input['report_keys'])) { - $entity['status'] = $task->status()->exists() ? $task->status->name : ''; + if (in_array('task.status_id', $this->input['report_keys'])) { + $entity['task.status_id'] = $task->status()->exists() ? $task->status->name : ''; } - if (in_array('project_id', $this->input['report_keys'])) { - $entity['project'] = $task->project()->exists() ? $task->project->name : ''; + if (in_array('task.project_id', $this->input['report_keys'])) { + $entity['task.project_id'] = $task->project()->exists() ? $task->project->name : ''; } - + return $entity; } } diff --git a/app/Export/CSV/VendorExport.php b/app/Export/CSV/VendorExport.php index 011484b054a3..834f73766082 100644 --- a/app/Export/CSV/VendorExport.php +++ b/app/Export/CSV/VendorExport.php @@ -17,6 +17,7 @@ use App\Models\Company; use App\Transformers\VendorContactTransformer; use App\Transformers\VendorTransformer; use App\Utils\Ninja; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\App; use League\Csv\Writer; @@ -31,42 +32,6 @@ class VendorExport extends BaseExport 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) { $this->company = $company; @@ -75,8 +40,9 @@ class VendorExport extends BaseExport $this->contact_transformer = new VendorContactTransformer(); } - public function run() + public function init(): Builder { + MultiDB::setDb($this->company->db); App::forgetInstance('translator'); App::setLocale($this->company->locale()); @@ -89,17 +55,45 @@ class VendorExport extends BaseExport if (count($this->input['report_keys']) == 0) { $this->input['report_keys'] = array_values($this->entity_keys); } - - //insert the header - $this->csv->insertOne($this->buildHeader()); - + $query = Vendor::query()->with('contacts') - ->withTrashed() - ->where('company_id', $this->company->id) - ->where('is_deleted', 0); + ->withTrashed() + ->where('company_id', $this->company->id) + ->where('is_deleted', 0); $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() ->each(function ($vendor) { $this->csv->insertOne($this->buildRow($vendor)); @@ -123,14 +117,12 @@ class VendorExport extends BaseExport foreach (array_values($this->input['report_keys']) as $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)) { - $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)) { - $entity[$keyval] = $transformed_contact[$parts[1]]; + $entity[$key] = $transformed_contact[$parts[1]]; } else { - $entity[$keyval] = ''; + $entity[$key] = $this->resolveKey($key, $vendor, $this->vendor_transformer); } } diff --git a/app/Http/Controllers/Reports/VendorReportController.php b/app/Http/Controllers/Reports/VendorReportController.php index 2494daadb923..bfd0f3b2298e 100644 --- a/app/Http/Controllers/Reports/VendorReportController.php +++ b/app/Http/Controllers/Reports/VendorReportController.php @@ -11,11 +11,12 @@ namespace App\Http\Controllers\Reports; +use App\Utils\Traits\MakesHash; use App\Export\CSV\VendorExport; +use App\Jobs\Report\SendToAdmin; +use App\Jobs\Report\PreviewReport; use App\Http\Controllers\BaseController; use App\Http\Requests\Report\GenericReportRequest; -use App\Jobs\Report\SendToAdmin; -use App\Utils\Traits\MakesHash; class VendorReportController extends BaseController { @@ -30,14 +31,26 @@ class VendorReportController extends BaseController public function __invoke(GenericReportRequest $request) { + /** @var \App\Models\User $user */ + $user = auth()->user(); + 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); } // 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();