From c4c5547281ed085af4b96c5796d23b4e26963944 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 29 Aug 2023 17:50:25 +1000 Subject: [PATCH] Working on payment exports --- app/Export/CSV/PaymentExport.php | 61 ++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index dab8054c132f..8299cc4b59d6 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -16,6 +16,7 @@ use App\Models\Company; use App\Models\Payment; use App\Transformers\PaymentTransformer; use App\Utils\Ninja; +use Illuminate\Contracts\Database\Eloquent\Builder; use Illuminate\Support\Facades\App; use League\Csv\Writer; @@ -68,24 +69,19 @@ class PaymentExport extends BaseExport $this->entity_transformer = new PaymentTransformer(); } - public function run() + private function init(): Builder { + 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(); - if (count($this->input['report_keys']) == 0) { - $this->input['report_keys'] = array_values($this->entity_keys); + $this->input['report_keys'] = array_values($this->payment_report_keys); } - //insert the header - $this->csv->insertOne($this->buildHeader()); - $query = Payment::query() ->withTrashed() ->where('company_id', $this->company->id) @@ -93,6 +89,39 @@ class PaymentExport extends BaseExport $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) { + return $this->buildRow($resource); + })->toArray(); + + return array_merge(['columns' => $header], $report); + + + } + + 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()); + $query->cursor() ->each(function ($entity) { $this->csv->insertOne($this->buildRow($entity)); @@ -108,15 +137,17 @@ class PaymentExport extends BaseExport $entity = []; foreach (array_values($this->input['report_keys']) as $key) { - $keyval = array_search($key, $this->entity_keys); + // $keyval = array_search($key, $this->entity_keys); + + $parts = explode('.', $key); - if(!$keyval) { - $keyval = array_search(str_replace("payment.", "", $key), $this->entity_keys) ?? $key; - } + // if(!$keyval) { + // $keyval = array_search(str_replace("payment.", "", $key), $this->entity_keys) ?? $key; + // } - if(!$keyval) { - $keyval = $key; - } + // if(!$keyval) { + // $keyval = $key; + // } if (array_key_exists($key, $transformed_entity)) { $entity[$keyval] = $transformed_entity[$key];