Working on payment exports

This commit is contained in:
David Bomba 2023-08-29 17:50:25 +10:00
parent 7b6515c8c2
commit c4c5547281

View File

@ -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];