1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Merge pull request #7363 from turbo124/v5-develop

CSV Exports via API
This commit is contained in:
David Bomba 2022-04-09 07:55:00 +10:00 committed by GitHub
commit bf03ba3780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1130 additions and 16 deletions

View File

@ -11,7 +11,6 @@
namespace App\Export\CSV;
use App\Http\Controllers\ClientPortal\setLocale;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
@ -112,8 +111,7 @@ class ClientExport
});
echo $this->csv->toString();
return $this->csv->toString();
}

View File

@ -0,0 +1,175 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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\ClientContact;
use App\Models\Company;
use App\Transformers\ClientContactTransformer;
use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class ContactExport
{
private $company;
private $report_keys;
private $client_transformer;
private $contact_transformer;
private array $entity_keys = [
'address1' => 'client.address1',
'address2' => 'client.address2',
'balance' => 'client.balance',
'city' => 'client.city',
'country' => 'client.country_id',
'credit_balance' => 'client.credit_balance',
'custom_value1' => 'client.custom_value1',
'custom_value2' => 'client.custom_value2',
'custom_value3' => 'client.custom_value3',
'custom_value4' => 'client.custom_value4',
'id_number' => 'client.id_number',
'industry' => 'client.industry_id',
'last_login' => 'client.last_login',
'name' => 'client.name',
'number' => 'client.number',
'paid_to_date' => 'client.paid_to_date',
'phone' => 'client.phone',
'postal_code' => 'client.postal_code',
'private_notes' => 'client.private_notes',
'public_notes' => 'client.public_notes',
'shipping_address1' => 'client.shipping_address1',
'shipping_address2' => 'client.shipping_address2',
'shipping_city' => 'client.shipping_city',
'shipping_country' => 'client.shipping_country_id',
'shipping_postal_code' => 'client.shipping_postal_code',
'shipping_state' => 'client.shipping_state',
'state' => 'client.state',
'vat_number' => 'client.vat_number',
'website' => 'client.website',
'currency' => 'client.currency',
'first_name' => 'contact.first_name',
'last_name' => 'contact.last_name',
'phone' => 'contact.phone',
'contact_custom_value1' => 'contact.custom_value1',
'contact_custom_value2' => 'contact.custom_value2',
'contact_custom_value3' => 'contact.custom_value3',
'contact_custom_value4' => 'contact.custom_value4',
'email' => 'contact.email',
];
private array $decorate_keys = [
'client.country_id',
'client.shipping_country_id',
'client.currency',
'client.industry',
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->client_transformer = new ClientTransformer();
$this->contact_transformer = new ClientContactTransformer();
}
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());
ClientContact::where('company_id', $this->company->id)
->cursor()
->each(function ($contact){
$this->csv->insertOne($this->buildRow($contact));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(ClientContact $contact) :array
{
$transformed_contact = false;
$transformed_client = $this->client_transformer->transform($contact->client);
$transformed_contact = $this->contact_transformer->transform($contact);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$parts = explode(".",$key);
$entity[$parts[1]] = "";
if($parts[0] == 'client') {
$entity[$parts[1]] = $transformed_client[$parts[1]];
}
elseif($parts[0] == 'contact') {
$entity[$parts[1]] = $transformed_contact[$parts[1]];
}
}
return $this->decorateAdvancedFields($contact->client, $entity);
}
private function decorateAdvancedFields(Client $client, array $entity) :array
{
if(array_key_exists('country_id', $entity))
$entity['country_id'] = $client->country ? ctrans("texts.country_{$client->country->name}") : "";
if(array_key_exists('shipping_country_id', $entity))
$entity['shipping_country_id'] = $client->shipping_country ? ctrans("texts.country_{$client->shipping_country->name}") : "";
if(array_key_exists('currency', $entity))
$entity['currency'] = $client->currency()->code;
if(array_key_exists('industry_id', $entity))
$entity['industry_id'] = $client->industry ? ctrans("texts.industry_{$client->industry->name}") : "";
return $entity;
}
}

View File

@ -0,0 +1,157 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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\Transformers\CreditTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class CreditExport
{
private $company;
private $report_keys;
private $credit_transformer;
private 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',
'country' => 'country_id',
'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',
'invoice' => 'invoice_id',
'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' => 'currency'
];
private array $decorate_keys = [
'country',
'client',
'invoice',
'currency',
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->credit_transformer = new CreditTransformer();
}
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());
Credit::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($credit){
$this->csv->insertOne($this->buildRow($credit));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(Credit $credit) :array
{
$transformed_credit = $this->credit_transformer->transform($credit);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$entity[$key] = $transformed_credit[$key];
}
return $this->decorateAdvancedFields($credit, $entity);
}
private function decorateAdvancedFields(Credit $credit, array $entity) :array
{
if(array_key_exists('country_id', $entity))
$entity['country_id'] = $credit->client->country ? ctrans("texts.country_{$credit->client->country->name}") : "";
if(array_key_exists('currency', $entity))
$entity['currency'] = $credit->client->currency()->code;
if(array_key_exists('invoice_id', $entity))
$entity['invoice_id'] = $credit->invoice ? $credit->invoice->number : "";
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $credit->client->present()->name();
return $entity;
}
}

View File

@ -0,0 +1,119 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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\Transformers\DocumentTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class DocumentExport
{
private $company;
private $report_keys;
private $entity_transformer;
private array $entity_keys = [
'record_type' => 'record_type',
'record_name' => 'record_name',
'name' => 'name',
'type' => 'type',
'created_at' => 'created_at',
];
private array $decorate_keys = [
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->entity_transformer = new DocumentTransformer();
}
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());
Document::where('company_id', $this->company->id)
->cursor()
->each(function ($entity){
$this->csv->insertOne($this->buildRow($entity));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(Document $document) :array
{
$transformed_entity = $this->entity_transformer->transform($document);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$entity[$key] = $transformed_entity[$key];
}
return $this->decorateAdvancedFields($document, $entity);
}
private function decorateAdvancedFields(Document $document, array $entity) :array
{
if(array_key_exists('record_type', $entity))
$entity['record_type'] = class_basename($document->documentable);
if(array_key_exists('record_name', $entity))
$entity['record_name'] = $document->hashed_id;
return $entity;
}
}

View File

@ -0,0 +1,164 @@
<?php
/**
* Expense Ninja (https://expenseninja.com).
*
* @link https://github.com/expenseninja/expenseninja source repository
*
* @copyright Copyright (c) 2021. Expense Ninja LLC (https://expenseninja.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\Expense;
use App\Transformers\ExpenseTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class ExpenseExport
{
private $company;
private $report_keys;
private $expense_transformer;
private array $entity_keys = [
'amount' => 'amount',
'category' => 'category_id',
'client' => 'client_id',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'currency' => 'currency_id',
'date' => 'date',
'exchange_rate' => 'exchange_rate',
'converted_amount' => 'foreign_amount',
'invoice_currency_id' => 'invoice_currency_id',
'payment_date' => 'payment_date',
'number' => 'number',
'payment_type_id' => 'payment_type_id',
'private_notes' => 'private_notes',
'project' => 'project_id',
'public_notes' => 'public_notes',
'tax_amount1' => 'tax_amount1',
'tax_amount2' => 'tax_amount2',
'tax_amount3' => 'tax_amount3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'transaction_reference' => 'transaction_reference',
'vendor' => 'vendor_id',
'invoice' => 'invoice_id',
];
private array $decorate_keys = [
'client',
'currency',
'invoice',
'category',
'vendor',
'project',
'payment_type_id',
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->expense_transformer = new ExpenseTransformer();
}
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());
Expense::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($expense){
$this->csv->insertOne($this->buildRow($expense));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(Expense $expense) :array
{
$transformed_expense = $this->expense_transformer->transform($expense);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$entity[$key] = $transformed_expense[$key];
}
return $this->decorateAdvancedFields($expense, $entity);
}
private function decorateAdvancedFields(Expense $expense, array $entity) :array
{
if(array_key_exists('currency_id', $entity))
$entity['currency_id'] = $expense->currency ? $expense->currency->code : "";
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $expense->client ? $expense->client->present()->name() : "";
if(array_key_exists('invoice_id', $entity))
$entity['invoice_id'] = $expense->invoice ? $expense->invoice->number : "";
if(array_key_exists('category_id', $entity))
$entity['category_id'] = $expense->category ? $expense->category->name : "";
if(array_key_exists('vendor_id', $entity))
$entity['vendor_id'] = $expense->vendor ? $expense->vendor->name : "";
if(array_key_exists('payment_type_id', $entity))
$entity['payment_type_id'] = $expense->payment_type ? $expense->payment_type->name : "";
if(array_key_exists('project_id', $entity))
$entity['project_id'] = $expense->project ? $expense->project->name : "";
return $entity;
}
}

View File

@ -11,28 +11,141 @@
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use Excel;
use App\Models\Invoice;
use App\Transformers\InvoiceTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class InvoiceExport
{
private $company;
public function __construct(Company $company)
private $report_keys;
private $invoice_transformer;
private 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' => 'client_id'
];
private array $decorate_keys = [
'country',
'client',
'currency',
'status',
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->invoice_transformer = new InvoiceTransformer();
}
public function export()
public function run()
{
// $fileName = 'test.csv';
// $data = $this->company->invoices->get();
// return Excel::create($fileName, function ($excel) use ($data) {
// $excel->sheet('', function ($sheet) use ($data) {
// $sheet->loadView('export', $data);
// });
// })->download('csv');
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());
Invoice::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($invoice){
$this->csv->insertOne($this->buildRow($invoice));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(Invoice $invoice) :array
{
$transformed_invoice = $this->invoice_transformer->transform($invoice);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$entity[$key] = $transformed_invoice[$key];
}
return $this->decorateAdvancedFields($invoice, $entity);
}
private function decorateAdvancedFields(Invoice $invoice, array $entity) :array
{
if(array_key_exists('currency', $entity))
$entity['currency'] = $invoice->client->currency()->code;
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $invoice->client->present()->name();
if(array_key_exists('status_id', $entity))
$entity['status_id'] = $invoice->stringStatus($invoice->status_id);
return $entity;
}
}

View File

@ -0,0 +1,207 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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\Invoice;
use App\Transformers\InvoiceTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class InvoiceItemExport
{
private $company;
private $report_keys;
private $invoice_transformer;
private 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' => 'currency_id',
'qty' => 'item.quantity',
'unit_cost' => 'item.cost',
'product_key' => 'item.product_key',
'cost' => 'item.product_cost',
'notes' => 'item.notes',
'discount' => 'item.discount',
'is_amount_discount' => 'item.is_amount_discount',
'tax_rate1' => 'item.tax_rate1',
'tax_rate2' => 'item.tax_rate2',
'tax_rate3' => 'item.tax_rate3',
'tax_name1' => 'item.tax_name1',
'tax_name2' => 'item.tax_name2',
'tax_name3' => 'item.tax_name3',
'line_total' => 'item.line_total',
'gross_line_total' => 'item.gross_line_total',
'invoice1' => 'item.custom_value1',
'invoice2' => 'item.custom_value2',
'invoice3' => 'item.custom_value3',
'invoice4' => 'item.custom_value4',
];
private array $decorate_keys = [
'client',
'currency',
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->invoice_transformer = new InvoiceTransformer();
}
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());
Invoice::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($invoice){
$this->iterateItems($invoice);
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function iterateItems(Invoice $invoice)
{
$transformed_invoice = $this->buildRow($invoice);
$transformed_items = [];
foreach($invoice->line_items as $item)
{
$item_array = [];
foreach(array_values($this->report_keys) as $key){
if(str_contains($key, "item.")){
$key = str_replace("item.", "", $key);
$item_array[$key] = $item->{$key};
}
}
$entity = [];
$transformed_items = array_merge($transformed_invoice, $item_array);
$transformed_items = $this->decorateAdvancedFields($invoice, $transformed_items);
foreach(array_values($this->report_keys) as $key)
{
$key = str_replace("item.", "", $key);
$entity[$key] = $transformed_items[$key];
}
$this->csv->insertOne($entity);
}
}
private function buildRow(Invoice $invoice) :array
{
$transformed_invoice = $this->invoice_transformer->transform($invoice);
$entity = [];
foreach(array_values($this->report_keys) as $key){
if(!str_contains($key, "item."))
$entity[$key] = $transformed_invoice[$key];
}
return $this->decorateAdvancedFields($invoice, $entity);
}
private function decorateAdvancedFields(Invoice $invoice, array $entity) :array
{
if(array_key_exists('currency_id', $entity))
$entity['currency_id'] = $invoice->client->currency()->code;
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $invoice->client->present()->name();
if(array_key_exists('status_id', $entity))
$entity['status_id'] = $invoice->stringStatus($invoice->status_id);
return $entity;
}
}

View File

@ -0,0 +1,151 @@
<?php
/**
* Quote Ninja (https://quoteninja.com).
*
* @link https://github.com/quoteninja/quoteninja source repository
*
* @copyright Copyright (c) 2021. Quote Ninja LLC (https://quoteninja.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\Quote;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
class QuoteExport
{
private $company;
private $report_keys;
private $quote_transformer;
private 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' => 'client_id',
'invoice' => 'invoice_id',
];
private array $decorate_keys = [
'client',
'currency',
'invoice'
];
public function __construct(Company $company, array $report_keys)
{
$this->company = $company;
$this->report_keys = $report_keys;
$this->quote_transformer = new QuoteTransformer();
}
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());
Quote::with('client')->where('company_id', $this->company->id)
->where('is_deleted',0)
->cursor()
->each(function ($quote){
$this->csv->insertOne($this->buildRow($quote));
});
return $this->csv->toString();
}
private function buildHeader() :array
{
$header = [];
foreach(array_keys($this->report_keys) as $key)
$header[] = ctrans("texts.{$key}");
return $header;
}
private function buildRow(Quote $quote) :array
{
$transformed_quote = $this->quote_transformer->transform($quote);
$entity = [];
foreach(array_values($this->report_keys) as $key){
$entity[$key] = $transformed_quote[$key];
}
return $this->decorateAdvancedFields($quote, $entity);
}
private function decorateAdvancedFields(Quote $quote, array $entity) :array
{
if(array_key_exists('currency', $entity))
$entity['currency'] = $quote->client->currency()->code;
if(array_key_exists('client_id', $entity))
$entity['client_id'] = $quote->client->present()->name();
if(array_key_exists('invoice', $entity))
$entity['invoice'] = $quote->invoice ? $quote->invoice->number : "";
return $entity;
}
}

View File

@ -81,13 +81,13 @@ class ValidInvoicesRules implements Rule
if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] <= $inv->amount){
//catch here nothing to do - we need this to prevent the last elseif triggering
}
else if($inv->status_id == Invoice::STATUS_DRAFT && $invoice['amount'] > $inv->amount){
else if($inv->status_id == Invoice::STATUS_DRAFT && floatval($invoice['amount']) > floatval($inv->amount)){
$this->error_msg = 'Amount cannot be greater than invoice balance';
return false;
}
else if($invoice['amount'] > $inv->balance) {
else if(floatval($invoice['amount']) > floatval($inv->balance)) {
$this->error_msg = ctrans('texts.amount_greater_than_balance_v5');

View File

@ -106,4 +106,24 @@ class Expense extends BaseModel
{
return ctrans('texts.expense');
}
public function currency()
{
return $this->belongsTo(Currency::class);
}
public function category()
{
return $this->belongsTo(ExpenseCategory::class);
}
public function payment_type()
{
return $this->belongsTo(PaymentType::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
}

View File

@ -13,11 +13,13 @@ namespace App\Transformers;
use App\Models\Activity;
use App\Models\Backup;
use App\Models\Client;
use App\Models\Document;
use App\Models\Invoice;
use App\Models\RecurringInvoice;
use App\Models\RecurringInvoiceInvitation;
use App\Transformers\ActivityTransformer;
use App\Transformers\ClientTransformer;
use App\Transformers\InvoiceHistoryTransformer;
use App\Utils\Traits\MakesHash;
@ -32,6 +34,7 @@ class RecurringInvoiceTransformer extends EntityTransformer
protected $availableIncludes = [
'activities',
'client',
];
public function includeHistory(RecurringInvoice $invoice)
@ -62,6 +65,13 @@ class RecurringInvoiceTransformer extends EntityTransformer
return $this->includeCollection($invoice->documents, $transformer, Document::class);
}
public function includeClient(RecurringInvoice $invoice)
{
$transformer = new ClientTransformer($this->serializer);
return $this->includeItem($invoice->client, $transformer, Client::class);
}
public function transform(RecurringInvoice $invoice)
{