diff --git a/app/Export/CSV/PaymentExport.php b/app/Export/CSV/PaymentExport.php index 8299cc4b59d6..930be240b75f 100644 --- a/app/Export/CSV/PaymentExport.php +++ b/app/Export/CSV/PaymentExport.php @@ -28,39 +28,39 @@ class PaymentExport extends BaseExport public Writer $csv; - public array $entity_keys = [ - 'amount' => 'amount', - 'applied' => 'applied', - 'client' => 'client_id', - 'currency' => 'currency_id', - 'custom_value1' => 'custom_value1', - 'custom_value2' => 'custom_value2', - 'custom_value3' => 'custom_value3', - 'custom_value4' => 'custom_value4', - 'date' => 'date', - 'exchange_currency' => 'exchange_currency_id', - 'gateway' => 'gateway_type_id', - 'number' => 'number', - 'private_notes' => 'private_notes', - 'project' => 'project_id', - 'refunded' => 'refunded', - 'status' => 'status_id', - 'transaction_reference' => 'transaction_reference', - 'type' => 'type_id', - 'vendor' => 'vendor_id', - 'invoices' => 'invoices', - ]; + // public array $entity_keys = [ + // 'amount' => 'amount', + // 'applied' => 'applied', + // 'client' => 'client_id', + // 'currency' => 'currency_id', + // 'custom_value1' => 'custom_value1', + // 'custom_value2' => 'custom_value2', + // 'custom_value3' => 'custom_value3', + // 'custom_value4' => 'custom_value4', + // 'date' => 'date', + // 'exchange_currency' => 'exchange_currency_id', + // 'gateway' => 'gateway_type_id', + // 'number' => 'number', + // 'private_notes' => 'private_notes', + // 'project' => 'project_id', + // 'refunded' => 'refunded', + // 'status' => 'status_id', + // 'transaction_reference' => 'transaction_reference', + // 'type' => 'type_id', + // 'vendor' => 'vendor_id', + // 'invoices' => 'invoices', + // ]; - private array $decorate_keys = [ - 'vendor', - 'status', - 'project', - 'client', - 'currency', - 'exchange_currency', - 'type', - 'invoices', - ]; + // private array $decorate_keys = [ + // 'vendor', + // 'status', + // 'project', + // 'client', + // 'currency', + // 'exchange_currency', + // 'type', + // 'invoices', + // ]; public function __construct(Company $company, array $input) { @@ -137,26 +137,17 @@ class PaymentExport extends BaseExport $entity = []; foreach (array_values($this->input['report_keys']) as $key) { - // $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 = $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, $payment, $this->entity_transformer); + if (is_array($parts) && $parts[0] == 'payment' && array_key_exists($parts[1], $transformed_entity)) { + $entity[$key] = $transformed_entity[$parts[1]]; + } elseif (array_key_exists($key, $transformed_entity)) { + $entity[$key] = $transformed_entity[$key]; + } else { + $entity[$key] = $this->resolveKey($key, $payment, $this->entity_transformer); } + } return $this->decorateAdvancedFields($payment, $entity); diff --git a/app/Http/Controllers/Reports/ExpenseReportController.php b/app/Http/Controllers/Reports/ExpenseReportController.php index 2953f653a391..f1276cdaffdd 100644 --- a/app/Http/Controllers/Reports/ExpenseReportController.php +++ b/app/Http/Controllers/Reports/ExpenseReportController.php @@ -11,13 +11,14 @@ namespace App\Http\Controllers\Reports; +use App\Models\Client; +use Illuminate\Http\Response; +use App\Utils\Traits\MakesHash; +use App\Jobs\Report\SendToAdmin; use App\Export\CSV\ExpenseExport; +use App\Jobs\Report\PreviewReport; use App\Http\Controllers\BaseController; use App\Http\Requests\Report\GenericReportRequest; -use App\Jobs\Report\SendToAdmin; -use App\Models\Client; -use App\Utils\Traits\MakesHash; -use Illuminate\Http\Response; class ExpenseReportController extends BaseController { @@ -63,14 +64,27 @@ class ExpenseReportController 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(), ExpenseExport::class, $this->filename); + SendToAdmin::dispatch($user->company(), $request->all(), ExpenseExport::class, $this->filename); return response()->json(['message' => 'working...'], 200); } // expect a list of visible fields, or use the default - $export = new ExpenseExport(auth()->user()->company(), $request->all()); + if($request->has('output') && $request->input('output') == 'json') { + + $hash = \Illuminate\Support\Str::uuid(); + + PreviewReport::dispatch($user->company(), $request->all(), ExpenseExport::class, $hash); + + return response()->json(['message' => $hash], 200); + } + + $export = new ExpenseExport($user->company(), $request->all()); $csv = $export->run(); diff --git a/app/Http/Controllers/Reports/PaymentReportController.php b/app/Http/Controllers/Reports/PaymentReportController.php index 44ea8d5e2059..cca6925ef98a 100644 --- a/app/Http/Controllers/Reports/PaymentReportController.php +++ b/app/Http/Controllers/Reports/PaymentReportController.php @@ -11,13 +11,14 @@ namespace App\Http\Controllers\Reports; +use App\Models\Client; +use Illuminate\Http\Response; +use App\Utils\Traits\MakesHash; +use App\Jobs\Report\SendToAdmin; use App\Export\CSV\PaymentExport; +use App\Jobs\Report\PreviewReport; use App\Http\Controllers\BaseController; use App\Http\Requests\Report\GenericReportRequest; -use App\Jobs\Report\SendToAdmin; -use App\Models\Client; -use App\Utils\Traits\MakesHash; -use Illuminate\Http\Response; class PaymentReportController extends BaseController { @@ -63,14 +64,26 @@ class PaymentReportController 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(), PaymentExport::class, $this->filename); + SendToAdmin::dispatch($user->company(), $request->all(), PaymentExport::class, $this->filename); return response()->json(['message' => 'working...'], 200); } // expect a list of visible fields, or use the default - $export = new PaymentExport(auth()->user()->company(), $request->all()); + if($request->has('output') && $request->input('output') == 'json') { + + $hash = \Illuminate\Support\Str::uuid(); + + PreviewReport::dispatch($user->company(), $request->all(), PaymentExport::class, $hash); + + return response()->json(['message' => $hash], 200); + } + + $export = new PaymentExport($user->company(), $request->all()); $csv = $export->run(); diff --git a/tests/Feature/Export/ReportApiTest.php b/tests/Feature/Export/ReportApiTest.php index 6b91468241e9..2846cdd70799 100644 --- a/tests/Feature/Export/ReportApiTest.php +++ b/tests/Feature/Export/ReportApiTest.php @@ -59,8 +59,6 @@ class ReportApiTest extends TestCase } - - public function testUserSalesReportApiRoute() { $data = [ diff --git a/tests/Feature/Export/ReportCsvGenerationTest.php b/tests/Feature/Export/ReportCsvGenerationTest.php index a6868262d10f..d2b236e91d4b 100644 --- a/tests/Feature/Export/ReportCsvGenerationTest.php +++ b/tests/Feature/Export/ReportCsvGenerationTest.php @@ -649,11 +649,11 @@ class ReportCsvGenerationTest extends TestCase $csv = $response->streamedContent(); - $this->assertEquals(500, $this->getFirstValueByColumn($csv, 'Amount')); - $this->assertEquals(0, $this->getFirstValueByColumn($csv, 'Applied')); - $this->assertEquals(0, $this->getFirstValueByColumn($csv, 'Refunded')); - $this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Date')); - $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Transaction Reference')); + $this->assertEquals(500, $this->getFirstValueByColumn($csv, 'Payment Amount')); + $this->assertEquals(0, $this->getFirstValueByColumn($csv, 'Payment Applied')); + $this->assertEquals(0, $this->getFirstValueByColumn($csv, 'Payment Refunded')); + $this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Payment Date')); + $this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Payment Transaction Reference')); } @@ -1491,7 +1491,6 @@ nlog($csv); $response->assertStatus(200); $csv = $response->streamedContent(); -nlog($csv); $this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Invoice Amount')); $this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Invoice Balance')); diff --git a/tests/Feature/Export/ReportPreviewTest.php b/tests/Feature/Export/ReportPreviewTest.php index 50e7b3e2eb7c..df9163834c58 100644 --- a/tests/Feature/Export/ReportPreviewTest.php +++ b/tests/Feature/Export/ReportPreviewTest.php @@ -21,9 +21,10 @@ use App\Export\CSV\ClientExport; use App\Export\CSV\CreditExport; use App\Export\CSV\ContactExport; use App\Export\CSV\ExpenseExport; +use App\Export\CSV\InvoiceExport; +use App\Export\CSV\PaymentExport; use App\Export\CSV\ActivityExport; use App\Export\CSV\DocumentExport; -use App\Export\CSV\InvoiceExport; use App\Jobs\Report\PreviewReport; use Illuminate\Support\Facades\Cache; use Illuminate\Routing\Middleware\ThrottleRequests; @@ -54,6 +55,37 @@ class ReportPreviewTest extends TestCase } + + public function testPaymentJsonExport() + { + \App\Models\Payment::factory()->count(5)->create([ + 'company_id' => $this->company->id, + 'user_id' => $this->user->id, + 'client_id' => $this->client->id, + ]); + + $data = [ + 'send_email' => false, + 'date_range' => 'all', + 'report_keys' => [], + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/reports/payments?output=json', $data) + ->assertStatus(200); + + $p = (new PreviewReport($this->company, $data, PaymentExport::class, '123'))->handle(); + + $this->assertNull($p); + + $r = Cache::pull('123'); + + $this->assertNotNull($r); + + } + public function testInvoiceItemJsonExport() { \App\Models\Invoice::factory()->count(5)->create([