mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Payment Export CSV
This commit is contained in:
parent
8b1a0381b0
commit
6a148676db
@ -64,4 +64,17 @@ class BaseExport
|
||||
|
||||
}
|
||||
|
||||
protected function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach($this->input['report_keys'] as $value){
|
||||
$key = array_search ($value, $this->entity_keys);
|
||||
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
}
|
||||
|
||||
return $header;
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ class ClientExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'address1' => 'client.address1',
|
||||
'address2' => 'client.address2',
|
||||
'balance' => 'client.balance',
|
||||
@ -125,17 +125,6 @@ class ClientExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Client $client) :array
|
||||
{
|
||||
|
||||
|
@ -33,7 +33,7 @@ class ContactExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'address1' => 'client.address1',
|
||||
'address2' => 'client.address2',
|
||||
'balance' => 'client.balance',
|
||||
@ -120,17 +120,6 @@ class ContactExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(ClientContact $contact) :array
|
||||
{
|
||||
|
||||
|
@ -30,7 +30,7 @@ class CreditExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
@ -115,17 +115,6 @@ class CreditExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Credit $credit) :array
|
||||
{
|
||||
|
||||
|
@ -31,7 +31,7 @@ class DocumentExport extends BaseExport
|
||||
|
||||
protected $date_key = 'created_at';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'record_type' => 'record_type',
|
||||
'record_name' => 'record_name',
|
||||
'name' => 'name',
|
||||
@ -80,17 +80,6 @@ class DocumentExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Document $document) :array
|
||||
{
|
||||
|
||||
|
@ -30,7 +30,7 @@ class ExpenseExport extends BaseExport
|
||||
|
||||
protected $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'category' => 'category_id',
|
||||
'client' => 'client_id',
|
||||
@ -115,17 +115,6 @@ class ExpenseExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Expense $expense) :array
|
||||
{
|
||||
|
||||
|
@ -30,7 +30,7 @@ class InvoiceExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
@ -113,17 +113,6 @@ class InvoiceExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Invoice $invoice) :array
|
||||
{
|
||||
|
||||
|
@ -30,7 +30,7 @@ class InvoiceItemExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
@ -129,17 +129,6 @@ class InvoiceItemExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function iterateItems(Invoice $invoice)
|
||||
{
|
||||
$transformed_invoice = $this->buildRow($invoice);
|
||||
|
148
app/Export/CSV/PaymentExport.php
Normal file
148
app/Export/CSV/PaymentExport.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Export\CSV;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\Credit;
|
||||
use App\Models\Document;
|
||||
use App\Models\Payment;
|
||||
use App\Transformers\PaymentTransformer;
|
||||
use App\Utils\Ninja;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use League\Csv\Writer;
|
||||
|
||||
class PaymentExport extends BaseExport
|
||||
{
|
||||
private Company $company;
|
||||
|
||||
protected array $input;
|
||||
|
||||
private $entity_transformer;
|
||||
|
||||
protected $date_key = 'date';
|
||||
|
||||
protected 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_type' => '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',
|
||||
];
|
||||
|
||||
private array $decorate_keys = [
|
||||
'vendor',
|
||||
'status',
|
||||
'project',
|
||||
'client',
|
||||
'currency',
|
||||
'exchange_currency',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function __construct(Company $company, array $input)
|
||||
{
|
||||
$this->company = $company;
|
||||
$this->input = $input;
|
||||
$this->entity_transformer = new PaymentTransformer();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
|
||||
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();
|
||||
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
$query = Payment::query()->where('company_id', $this->company->id);
|
||||
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
$query->cursor()
|
||||
->each(function ($entity){
|
||||
|
||||
$this->csv->insertOne($this->buildRow($entity));
|
||||
|
||||
});
|
||||
|
||||
return $this->csv->toString();
|
||||
|
||||
}
|
||||
|
||||
private function buildRow(Payment $payment) :array
|
||||
{
|
||||
|
||||
$transformed_entity = $this->entity_transformer->transform($payment);
|
||||
|
||||
$entity = [];
|
||||
|
||||
foreach(array_values($this->input['report_keys']) as $key){
|
||||
|
||||
$entity[$key] = $transformed_entity[$key];
|
||||
|
||||
}
|
||||
|
||||
return $this->decorateAdvancedFields($payment, $entity);
|
||||
|
||||
}
|
||||
|
||||
private function decorateAdvancedFields(Payment $payment, array $entity) :array
|
||||
{
|
||||
|
||||
if(array_key_exists('status_id', $entity))
|
||||
$entity['status_id'] = $payment->stringStatus($payment->status_id);
|
||||
|
||||
if(array_key_exists('vendor_id', $entity))
|
||||
$entity['vendor_id'] = $payment->vendor()->exists() ? $payment->vendor->name : '';
|
||||
|
||||
if(array_key_exists('project_id', $entity))
|
||||
$entity['project_id'] = $payment->project()->exists() ? $payment->project->name : '';
|
||||
|
||||
if(array_key_exists('currency_id', $entity))
|
||||
$entity['currency_id'] = $payment->currency()->exists() ? $payment->currency->code : '';
|
||||
|
||||
if(array_key_exists('exchange_currency_id', $entity))
|
||||
$entity['exchange_currency_id'] = $payment->exchange_currency()->exists() ? $payment->exchange_currency->code : '';
|
||||
|
||||
if(array_key_exists('client_id', $entity))
|
||||
$entity['client_id'] = $payment->client->present()->name();
|
||||
|
||||
if(array_key_exists('type_id', $entity))
|
||||
$entity['type_id'] = $payment->translatedType();
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
}
|
@ -30,7 +30,7 @@ class QuoteExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
@ -113,17 +113,6 @@ class QuoteExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(Quote $quote) :array
|
||||
{
|
||||
|
||||
|
@ -30,7 +30,7 @@ class RecurringInvoiceExport extends BaseExport
|
||||
|
||||
protected string $date_key = 'date';
|
||||
|
||||
private array $entity_keys = [
|
||||
protected array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
@ -117,17 +117,6 @@ class RecurringInvoiceExport extends BaseExport
|
||||
|
||||
}
|
||||
|
||||
private function buildHeader() :array
|
||||
{
|
||||
|
||||
$header = [];
|
||||
|
||||
foreach(array_keys($this->input['report_keys']) as $key)
|
||||
$header[] = ctrans("texts.{$key}");
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
||||
private function buildRow(RecurringInvoice $invoice) :array
|
||||
{
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Export\CSV\ContactExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\GenericReportSchema;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
@ -60,7 +60,7 @@ class ClientContactReportController extends BaseController
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function __invoke(GenericReportSchema $request)
|
||||
public function __invoke(GenericReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
|
@ -40,7 +40,7 @@ class ClientReportController extends BaseController
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
||||
* @OA\RequestBody(
|
||||
* required=true,
|
||||
* @OA\JsonContent(ref="#/components/schemas/ClientReportSchema")
|
||||
* @OA\JsonContent(ref="#/components/schemas/GenericReportSchema")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
|
@ -13,7 +13,7 @@ namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Export\CSV\DocumentExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\ClientContactReportRequest;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
@ -60,7 +60,7 @@ class DocumentReportController extends BaseController
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function __invoke(ClientContactReportRequest $request)
|
||||
public function __invoke(GenericReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
|
85
app/Http/Controllers/Reports/PaymentReportController.php
Normal file
85
app/Http/Controllers/Reports/PaymentReportController.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://www.elastic.co/licensing/elastic-license
|
||||
*/
|
||||
|
||||
namespace App\Http\Controllers\Reports;
|
||||
|
||||
use App\Export\CSV\PaymentExport;
|
||||
use App\Http\Controllers\BaseController;
|
||||
use App\Http\Requests\Report\GenericReportRequest;
|
||||
use App\Models\Client;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class PaymentReportController extends BaseController
|
||||
{
|
||||
use MakesHash;
|
||||
|
||||
private string $filename = 'payments.csv';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/reports/payments",
|
||||
* operationId="getPaymentReport",
|
||||
* tags={"reports"},
|
||||
* summary="Payment reports",
|
||||
* description="Export payment reports",
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
||||
* @OA\RequestBody(
|
||||
* required=true,
|
||||
* @OA\JsonContent(ref="#/components/schemas/GenericReportSchema")
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="success",
|
||||
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
||||
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
||||
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="default",
|
||||
* description="Unexpected Error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
||||
* ),
|
||||
* )
|
||||
*/
|
||||
public function __invoke(GenericReportRequest $request)
|
||||
{
|
||||
// expect a list of visible fields, or use the default
|
||||
|
||||
$export = new PaymentExport(auth()->user()->company(), $request->all());
|
||||
|
||||
$csv = $export->run();
|
||||
|
||||
$headers = array(
|
||||
'Content-Disposition' => 'attachment',
|
||||
'Content-Type' => 'text/csv',
|
||||
);
|
||||
|
||||
return response()->streamDownload(function () use ($csv) {
|
||||
echo $csv;
|
||||
}, $this->filename, $headers);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -150,6 +150,26 @@ class Payment extends BaseModel
|
||||
return $this->belongsTo(PaymentType::class);
|
||||
}
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->belongsTo(Currency::class);
|
||||
}
|
||||
|
||||
public function exchange_currency()
|
||||
{
|
||||
return $this->belongsTo(Currency::class, 'exchange_currency_id', 'id');
|
||||
}
|
||||
|
||||
public function vendor()
|
||||
{
|
||||
return $this->belongsTo(Vendor::class);
|
||||
}
|
||||
|
||||
public function project()
|
||||
{
|
||||
return $this->belongsTo(Project::class);
|
||||
}
|
||||
|
||||
public function translatedType()
|
||||
{
|
||||
if(!$this->type)
|
||||
@ -212,6 +232,34 @@ class Payment extends BaseModel
|
||||
}
|
||||
}
|
||||
|
||||
public static function stringStatus(int $status)
|
||||
{
|
||||
switch ($status) {
|
||||
case self::STATUS_PENDING:
|
||||
return ctrans('texts.payment_status_1');
|
||||
break;
|
||||
case self::STATUS_CANCELLED:
|
||||
return ctrans('texts.payment_status_2');
|
||||
break;
|
||||
case self::STATUS_FAILED:
|
||||
return ctrans('texts.payment_status_3');
|
||||
break;
|
||||
case self::STATUS_COMPLETED:
|
||||
return ctrans('texts.payment_status_4');
|
||||
break;
|
||||
case self::STATUS_PARTIALLY_REFUNDED:
|
||||
return ctrans('texts.payment_status_5');
|
||||
break;
|
||||
case self::STATUS_REFUNDED:
|
||||
return ctrans('texts.payment_status_6');
|
||||
break;
|
||||
default:
|
||||
return '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function ledger()
|
||||
{
|
||||
return new LedgerService($this);
|
||||
|
@ -4567,6 +4567,7 @@ $LANG = array(
|
||||
'restore_company_gateway' => 'Restore payment gateway',
|
||||
'archive_company_gateway' => 'Archive payment gateway',
|
||||
'delete_company_gateway' => 'Delete payment gateway',
|
||||
'exchange_currency' => 'Exchange currency',
|
||||
);
|
||||
|
||||
return $LANG;
|
||||
|
@ -163,6 +163,7 @@ Route::group(['middleware' => ['throttle:300,1', 'api_db', 'token_auth', 'locale
|
||||
Route::post('reports/invoice_items', 'Reports\InvoiceItemReportController');
|
||||
Route::post('reports/quotes', 'Reports\QuoteReportController');
|
||||
Route::post('reports/recurring_invoices', 'Reports\RecurringInvoiceReportController');
|
||||
Route::post('reports/payments', 'Reports\PaymentReportController');
|
||||
|
||||
|
||||
Route::get('scheduler', 'SchedulerController@index');
|
||||
|
Loading…
x
Reference in New Issue
Block a user