mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Working on exports
This commit is contained in:
parent
bc4e6711d9
commit
19f400252b
@ -133,6 +133,7 @@ class BaseExport
|
||||
"private_notes" => "invoice.private_notes",
|
||||
"uses_inclusive_taxes" => "invoice.uses_inclusive_taxes",
|
||||
"is_amount_discount" => "invoice.is_amount_discount",
|
||||
"discount" => "invoice.discount",
|
||||
"partial" => "invoice.partial",
|
||||
"partial_due_date" => "invoice.partial_due_date",
|
||||
"surcharge1" => "invoice.custom_surcharge1",
|
||||
@ -143,6 +144,16 @@ class BaseExport
|
||||
"tax_amount" => "invoice.total_taxes",
|
||||
"assigned_user" => "invoice.assigned_user_id",
|
||||
"user" => "invoice.user_id",
|
||||
"custom_value1" => "invoice.custom_value1",
|
||||
"custom_value2" => "invoice.custom_value2",
|
||||
"custom_value3" => "invoice.custom_value3",
|
||||
"custom_value4" => "invoice.custom_value4",
|
||||
'tax_name1' => 'invoice.tax_name1',
|
||||
'tax_name2' => 'invoice.tax_name2',
|
||||
'tax_name3' => 'invoice.tax_name3',
|
||||
'tax_rate1' => 'invoice.tax_rate1',
|
||||
'tax_rate2' => 'invoice.tax_rate2',
|
||||
'tax_rate3' => 'invoice.tax_rate3',
|
||||
];
|
||||
|
||||
protected array $recurring_invoice_report_keys = [
|
||||
@ -160,6 +171,7 @@ class BaseExport
|
||||
"private_notes" => "recurring_invoice.private_notes",
|
||||
"uses_inclusive_taxes" => "recurring_invoice.uses_inclusive_taxes",
|
||||
"is_amount_discount" => "recurring_invoice.is_amount_discount",
|
||||
"discount" => "recurring_invoice.discount",
|
||||
"partial" => "recurring_invoice.partial",
|
||||
"partial_due_date" => "recurring_invoice.partial_due_date",
|
||||
"surcharge1" => "recurring_invoice.custom_surcharge1",
|
||||
@ -171,7 +183,17 @@ class BaseExport
|
||||
"assigned_user" => "recurring_invoice.assigned_user_id",
|
||||
"user" => "recurring_invoice.user_id",
|
||||
"frequency_id" => "recurring_invoice.frequency_id",
|
||||
"next_send_date" => "recurring_invoice.next_send_date"
|
||||
"next_send_date" => "recurring_invoice.next_send_date",
|
||||
"custom_value1" => "recurring_invoice.custom_value1",
|
||||
"custom_value2" => "recurring_invoice.custom_value2",
|
||||
"custom_value3" => "recurring_invoice.custom_value3",
|
||||
"custom_value4" => "recurring_invoice.custom_value4",
|
||||
'tax_name1' => 'recurring_invoice.tax_name1',
|
||||
'tax_name2' => 'recurring_invoice.tax_name2',
|
||||
'tax_name3' => 'recurring_invoice.tax_name3',
|
||||
'tax_rate1' => 'recurring_invoice.tax_rate1',
|
||||
'tax_rate2' => 'recurring_invoice.tax_rate2',
|
||||
'tax_rate3' => 'recurring_invoice.tax_rate3',
|
||||
];
|
||||
|
||||
protected array $purchase_order_report_keys = [
|
||||
@ -259,6 +281,12 @@ class BaseExport
|
||||
"tax_amount" => "quote.total_taxes",
|
||||
"assigned_user" => "quote.assigned_user_id",
|
||||
"user" => "quote.user_id",
|
||||
'tax_name1' => 'quote.tax_name1',
|
||||
'tax_name2' => 'quote.tax_name2',
|
||||
'tax_name3' => 'quote.tax_name3',
|
||||
'tax_rate1' => 'quote.tax_rate1',
|
||||
'tax_rate2' => 'quote.tax_rate2',
|
||||
'tax_rate3' => 'quote.tax_rate3',
|
||||
];
|
||||
|
||||
protected array $credit_report_keys = [
|
||||
@ -957,7 +985,7 @@ class BaseExport
|
||||
}
|
||||
}
|
||||
|
||||
nlog($header);
|
||||
// nlog($header);
|
||||
|
||||
return $header;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ use App\Libraries\MultiDB;
|
||||
use App\Export\CSV\BaseExport;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use App\Transformers\InvoiceTransformer;
|
||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||
|
||||
class InvoiceExport extends BaseExport
|
||||
{
|
||||
@ -29,56 +30,6 @@ class InvoiceExport extends BaseExport
|
||||
|
||||
public Writer $csv;
|
||||
|
||||
public array $entity_keys = [
|
||||
'amount' => 'amount',
|
||||
'balance' => 'balance',
|
||||
'client' => 'client_id',
|
||||
'custom_surcharge1' => 'custom_surcharge1',
|
||||
'custom_surcharge2' => 'custom_surcharge2',
|
||||
'custom_surcharge3' => 'custom_surcharge3',
|
||||
'custom_surcharge4' => 'custom_surcharge4',
|
||||
'custom_value1' => 'custom_value1',
|
||||
'custom_value2' => 'custom_value2',
|
||||
'custom_value3' => 'custom_value3',
|
||||
'custom_value4' => 'custom_value4',
|
||||
'date' => 'date',
|
||||
'discount' => 'discount',
|
||||
'due_date' => 'due_date',
|
||||
'exchange_rate' => 'exchange_rate',
|
||||
'footer' => 'footer',
|
||||
'number' => 'number',
|
||||
'paid_to_date' => 'paid_to_date',
|
||||
'partial' => 'partial',
|
||||
'partial_due_date' => 'partial_due_date',
|
||||
'po_number' => 'po_number',
|
||||
'private_notes' => 'private_notes',
|
||||
'public_notes' => 'public_notes',
|
||||
'status' => 'status_id',
|
||||
'tax_name1' => 'tax_name1',
|
||||
'tax_name2' => 'tax_name2',
|
||||
'tax_name3' => 'tax_name3',
|
||||
'tax_rate1' => 'tax_rate1',
|
||||
'tax_rate2' => 'tax_rate2',
|
||||
'tax_rate3' => 'tax_rate3',
|
||||
'terms' => 'terms',
|
||||
'total_taxes' => 'total_taxes',
|
||||
'currency_id' => 'currency_id',
|
||||
'payment_number' => 'payment_number',
|
||||
'payment_date' => 'payment_date',
|
||||
'payment_amount' => 'payment_amount',
|
||||
'method' => 'method',
|
||||
];
|
||||
|
||||
private array $decorate_keys = [
|
||||
'country',
|
||||
'client',
|
||||
'currency_id',
|
||||
'status',
|
||||
'vendor',
|
||||
'project',
|
||||
];
|
||||
|
||||
|
||||
public function __construct(Company $company, array $input)
|
||||
{
|
||||
$this->company = $company;
|
||||
@ -86,24 +37,19 @@ class InvoiceExport extends BaseExport
|
||||
$this->invoice_transformer = new InvoiceTransformer();
|
||||
}
|
||||
|
||||
public function run()
|
||||
public 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->invoice_report_keys);
|
||||
}
|
||||
|
||||
//insert the header
|
||||
$this->csv->insertOne($this->buildHeader());
|
||||
|
||||
$query = Invoice::query()
|
||||
->withTrashed()
|
||||
->with('client')
|
||||
@ -112,10 +58,43 @@ class InvoiceExport extends BaseExport
|
||||
|
||||
$query = $this->addDateRange($query);
|
||||
|
||||
if(isset($this->input['status'])){
|
||||
if(isset($this->input['status'])) {
|
||||
$query = $this->addInvoiceStatusFilter($query, $this->input['status']);
|
||||
}
|
||||
|
||||
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 ($invoice) {
|
||||
$this->csv->insertOne($this->buildRow($invoice));
|
||||
@ -131,24 +110,15 @@ class InvoiceExport extends BaseExport
|
||||
$entity = [];
|
||||
|
||||
foreach (array_values($this->input['report_keys']) as $key) {
|
||||
$keyval = array_search($key, $this->entity_keys);
|
||||
|
||||
if(!$keyval) {
|
||||
$keyval = array_search(str_replace("invoice.", "", $key), $this->entity_keys) ?? $key;
|
||||
$parts = explode('.', $key);
|
||||
|
||||
if (is_array($parts) && $parts[0] == 'invoice' && array_key_exists($parts[1], $transformed_invoice)) {
|
||||
$entity[$key] = $transformed_invoice[$parts[1]];
|
||||
} else {
|
||||
$entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
|
||||
}
|
||||
|
||||
if(!$keyval) {
|
||||
$keyval = $key;
|
||||
}
|
||||
|
||||
if (array_key_exists($key, $transformed_invoice)) {
|
||||
$entity[$keyval] = $transformed_invoice[$key];
|
||||
} elseif (array_key_exists($keyval, $transformed_invoice)) {
|
||||
$entity[$keyval] = $transformed_invoice[$keyval];
|
||||
}
|
||||
else {
|
||||
$entity[$keyval] = $this->resolveKey($keyval, $invoice, $this->invoice_transformer);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->decorateAdvancedFields($invoice, $entity);
|
||||
@ -156,32 +126,22 @@ class InvoiceExport extends BaseExport
|
||||
|
||||
private function decorateAdvancedFields(Invoice $invoice, array $entity) :array
|
||||
{
|
||||
if (in_array('country_id', $this->input['report_keys'])) {
|
||||
$entity['country'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
|
||||
if (in_array('invoice.country_id', $this->input['report_keys'])) {
|
||||
$entity['invoice.country_id'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
|
||||
}
|
||||
|
||||
if (in_array('currency_id', $this->input['report_keys'])) {
|
||||
$entity['currency_id'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
|
||||
if (in_array('invoice.currency_id', $this->input['report_keys'])) {
|
||||
$entity['invoice.currency_id'] = $invoice->client->currency() ? $invoice->client->currency()->code : $invoice->company->currency()->code;
|
||||
}
|
||||
|
||||
if (in_array('client_id', $this->input['report_keys'])) {
|
||||
$entity['client'] = $invoice->client->present()->name();
|
||||
if (in_array('invoice.client_id', $this->input['report_keys'])) {
|
||||
$entity['invoice.clientstatus_id'] = $invoice->client->present()->name();
|
||||
}
|
||||
|
||||
if (in_array('status_id', $this->input['report_keys'])) {
|
||||
$entity['status'] = $invoice->stringStatus($invoice->status_id);
|
||||
if (in_array('invoice.status', $this->input['report_keys'])) {
|
||||
$entity['invoice.status_id'] = $invoice->stringStatus($invoice->status_id);
|
||||
}
|
||||
|
||||
// $payment_exists = $invoice->payments()->exists();
|
||||
|
||||
// $entity['payment_number'] = $payment_exists ? $invoice->payments()->pluck('number')->implode(',') : '';
|
||||
|
||||
// $entity['payment_date'] = $payment_exists ? $invoice->payments()->pluck('date')->implode(',') : '';
|
||||
|
||||
// $entity['payment_amount'] = $payment_exists ? Number::formatMoney($invoice->payments()->sum('paymentables.amount'), $invoice->company) : ctrans('texts.unpaid');
|
||||
|
||||
// $entity['method'] = $payment_exists ? $invoice->payments()->first()->translatedType() : "";
|
||||
|
||||
return $entity;
|
||||
}
|
||||
}
|
||||
|
@ -849,7 +849,7 @@ nlog($csv);
|
||||
])->post('/api/v1/reports/invoices', $data);
|
||||
|
||||
$csv = $response->streamedContent();
|
||||
|
||||
nlog($csv);
|
||||
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
|
||||
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Invoice Invoice Number'));
|
||||
$this->assertEquals('Unpaid', $this->getFirstValueByColumn($csv, 'Payment Amount'));
|
||||
@ -1491,30 +1491,31 @@ nlog($csv);
|
||||
$response->assertStatus(200);
|
||||
|
||||
$csv = $response->streamedContent();
|
||||
nlog($csv);
|
||||
|
||||
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
|
||||
$this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Balance'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Discount'));
|
||||
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'PO Number'));
|
||||
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
|
||||
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
|
||||
$this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Terms'));
|
||||
$this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Date'));
|
||||
$this->assertEquals('2021-01-02', $this->getFirstValueByColumn($csv, 'Due Date'));
|
||||
$this->assertEquals('2021-01-03', $this->getFirstValueByColumn($csv, 'Partial Due Date'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Partial/Deposit'));
|
||||
$this->assertEquals('Custom 1', $this->getFirstValueByColumn($csv, 'Custom Value 1'));
|
||||
$this->assertEquals('Custom 2', $this->getFirstValueByColumn($csv, 'Custom Value 2'));
|
||||
$this->assertEquals('Custom 3', $this->getFirstValueByColumn($csv, 'Custom Value 3'));
|
||||
$this->assertEquals('Custom 4', $this->getFirstValueByColumn($csv, 'Custom Value 4'));
|
||||
$this->assertEquals('Footer', $this->getFirstValueByColumn($csv, 'Footer'));
|
||||
$this->assertEquals('Tax 1', $this->getFirstValueByColumn($csv, 'Tax Name 1'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1'));
|
||||
$this->assertEquals('Tax 2', $this->getFirstValueByColumn($csv, 'Tax Name 2'));
|
||||
$this->assertEquals('20', $this->getFirstValueByColumn($csv, 'Tax Rate 2'));
|
||||
$this->assertEquals('Tax 3', $this->getFirstValueByColumn($csv, 'Tax Name 3'));
|
||||
$this->assertEquals('30', $this->getFirstValueByColumn($csv, 'Tax Rate 3'));
|
||||
$this->assertEquals('Sent', $this->getFirstValueByColumn($csv, 'Status'));
|
||||
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Invoice Amount'));
|
||||
$this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Invoice Balance'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Invoice Discount'));
|
||||
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Invoice PO Number'));
|
||||
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Invoice Public Notes'));
|
||||
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Invoice Private Notes'));
|
||||
$this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Invoice Terms'));
|
||||
$this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Invoice Date'));
|
||||
$this->assertEquals('2021-01-02', $this->getFirstValueByColumn($csv, 'Invoice Due Date'));
|
||||
$this->assertEquals('2021-01-03', $this->getFirstValueByColumn($csv, 'Invoice Partial Due Date'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Invoice Partial/Deposit'));
|
||||
$this->assertEquals('Custom 1', $this->getFirstValueByColumn($csv, 'Invoice Custom Value 1'));
|
||||
$this->assertEquals('Custom 2', $this->getFirstValueByColumn($csv, 'Invoice Custom Value 2'));
|
||||
$this->assertEquals('Custom 3', $this->getFirstValueByColumn($csv, 'Invoice Custom Value 3'));
|
||||
$this->assertEquals('Custom 4', $this->getFirstValueByColumn($csv, 'Invoice Custom Value 4'));
|
||||
$this->assertEquals('Footer', $this->getFirstValueByColumn($csv, 'Invoice Footer'));
|
||||
$this->assertEquals('Tax 1', $this->getFirstValueByColumn($csv, 'Invoice Tax Name 1'));
|
||||
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Invoice Tax Rate 1'));
|
||||
$this->assertEquals('Tax 2', $this->getFirstValueByColumn($csv, 'Invoice Tax Name 2'));
|
||||
$this->assertEquals('20', $this->getFirstValueByColumn($csv, 'Invoice Tax Rate 2'));
|
||||
$this->assertEquals('Tax 3', $this->getFirstValueByColumn($csv, 'Invoice Tax Name 3'));
|
||||
$this->assertEquals('30', $this->getFirstValueByColumn($csv, 'Invoice Tax Rate 3'));
|
||||
$this->assertEquals('Sent', $this->getFirstValueByColumn($csv, 'Invoice Status'));
|
||||
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ use App\Export\CSV\ContactExport;
|
||||
use App\Export\CSV\ExpenseExport;
|
||||
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,35 @@ class ReportPreviewTest extends TestCase
|
||||
}
|
||||
|
||||
|
||||
public function testInvoiceJsonExport()
|
||||
{
|
||||
\App\Models\Invoice::factory()->count(5)->create([
|
||||
'company_id' => $this->company->id,
|
||||
'user_id' => $this->user->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/invoices?output=json', $data)
|
||||
->assertStatus(200);
|
||||
|
||||
$p = (new PreviewReport($this->company, $data, InvoiceExport::class, '123'))->handle();
|
||||
|
||||
$this->assertNull($p);
|
||||
|
||||
$r = Cache::pull('123');
|
||||
|
||||
$this->assertNotNull($r);
|
||||
nlog($r);
|
||||
}
|
||||
|
||||
public function testExpenseJsonExport()
|
||||
{
|
||||
Expense::factory()->count(5)->create([
|
||||
|
Loading…
x
Reference in New Issue
Block a user