Merge pull request #8759 from turbo124/v5-develop

Report Previews
This commit is contained in:
David Bomba 2023-08-30 11:24:43 +10:00 committed by GitHub
commit b529998e00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 1726 additions and 960 deletions

View File

@ -20,6 +20,7 @@ use App\Libraries\MultiDB;
use App\Models\DateFormat;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Illuminate\Database\Eloquent\Builder;
use App\Transformers\ActivityTransformer;
class ActivityExport extends BaseExport
@ -39,10 +40,6 @@ class ActivityExport extends BaseExport
'address' => 'address',
];
private array $decorate_keys = [
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
@ -50,45 +47,27 @@ class ActivityExport extends BaseExport
$this->entity_transformer = new ActivityTransformer();
}
public function run()
public function returnJson()
{
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
App::setLocale($this->company->locale());
$t = app('translator');
$t->replace(Ninja::transformTranslations($this->company->settings));
$query = $this->init();
$this->date_format = DateFormat::find($this->company->settings->date_format_id)->format;
$headerdisplay = $this->buildHeader();
//load the CSV document from a string
$this->csv = Writer::createFromString();
$header = collect($this->input['report_keys'])->map(function ($key, $value) use($headerdisplay){
return ['identifier' => $value, 'display_value' => $headerdisplay[$value]];
})->toArray();
ksort($this->entity_keys);
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->entity_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Activity::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
$query->cursor()
->each(function ($entity) {
$this->buildRow($entity);
});
return $this->csv->toString();
$report = $query->cursor()
->map(function ($credit) {
return $this->buildActivityRow($credit);
})->toArray();
return array_merge(['columns' => $header], $report);
}
private function buildRow(Activity $activity)
private function buildActivityRow(Activity $activity): array
{
$this->csv->insertOne([
return [
Carbon::parse($activity->created_at)->format($this->date_format),
ctrans("texts.activity_{$activity->activity_type_id}",[
'client' => $activity->client ? $activity->client->present()->name() : '',
@ -108,7 +87,57 @@ class ActivityExport extends BaseExport
'recurring_expense' => $activity->recurring_expense ? $activity->recurring_expense->number : '',
]),
$activity->ip,
]);
];
}
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));
$this->date_format = DateFormat::find($this->company->settings->date_format_id)->format;
ksort($this->entity_keys);
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->entity_keys);
}
$query = Activity::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
return $query;
}
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->buildRow($entity);
});
return $this->csv->toString();
}
private function buildRow(Activity $activity)
{
$this->csv->insertOne($this->buildActivityRow($activity));
}

View File

@ -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,17 +183,23 @@ 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 = [
'amount' => 'purchase_order.amount',
'balance' => 'purchase_order.balance',
'vendor' => 'purchase_order.vendor_id',
// 'custom_surcharge1' => 'purchase_order.custom_surcharge1',
// 'custom_surcharge2' => 'purchase_order.custom_surcharge2',
// 'custom_surcharge3' => 'purchase_order.custom_surcharge3',
// 'custom_surcharge4' => 'purchase_order.custom_surcharge4',
'custom_value1' => 'purchase_order.custom_value1',
'custom_value2' => 'purchase_order.custom_value2',
'custom_value3' => 'purchase_order.custom_value3',
@ -210,17 +228,37 @@ class BaseExport
'currency_id' => 'purchase_order.currency_id',
];
protected array $product_report_keys = [
'project' => 'project_id',
'vendor' => 'vendor_id',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'product_key' => 'product_key',
'notes' => 'notes',
'cost' => 'cost',
'price' => 'price',
'quantity' => 'quantity',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
];
protected array $item_report_keys = [
"quantity" => "item.quantity",
"cost" => "item.cost",
"product_key" => "item.product_key",
"notes" => "item.notes",
"item_tax1" => "item.tax_name1",
"item_tax_rate1" => "item.tax_rate1",
"item_tax2" => "item.tax_name2",
"item_tax_rate2" => "item.tax_rate2",
"item_tax3" => "item.tax_name3",
"item_tax_rate3" => "item.tax_rate3",
"tax_name1" => "item.tax_name1",
"tax_rate1" => "item.tax_rate1",
"tax_name2" => "item.tax_name2",
"tax_rate2" => "item.tax_rate2",
"tax_name3" => "item.tax_name3",
"tax_rate3" => "item.tax_rate3",
"custom_value1" => "item.custom_value1",
"custom_value2" => "item.custom_value2",
"custom_value3" => "item.custom_value3",
@ -228,6 +266,9 @@ class BaseExport
"discount" => "item.discount",
"type" => "item.type_id",
"tax_category" => "item.tax_id",
'is_amount_discount' => 'item.is_amount_discount',
'line_total' => 'item.line_total',
'gross_line_total' => 'item.gross_line_total',
];
protected array $quote_report_keys = [
@ -249,6 +290,7 @@ class BaseExport
"private_notes" => "quote.private_notes",
"uses_inclusive_taxes" => "quote.uses_inclusive_taxes",
"is_amount_discount" => "quote.is_amount_discount",
"discount" => "quote.discount",
"partial" => "quote.partial",
"partial_due_date" => "quote.partial_due_date",
"surcharge1" => "quote.custom_surcharge1",
@ -259,6 +301,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 = [
@ -833,6 +881,19 @@ class BaseExport
return $query->whereBetween($this->date_key, [now()->startOfYear(), now()])->orderBy($this->date_key, 'ASC');
}
}
/**
* Returns the merged array of
* the entity with the matching
* item report keys
*
* @param string $entity_report_keys
* @return array
*/
public function mergeItemsKeys(string $entity_report_keys): array
{
return array_merge($this->{$entity_report_keys}, $this->item_report_keys);
}
public function buildHeader() :array
{
@ -841,10 +902,10 @@ class BaseExport
$header = [];
foreach ($this->input['report_keys'] as $value) {
$key = array_search($value, $this->entity_keys);
$original_key = $key;
// nlog("{$key} => {$value}");
$prefix = '';
@ -903,6 +964,11 @@ class BaseExport
$key = array_search($value, $this->purchase_order_report_keys);
}
if(!$key) {
$prefix = '';
$key = array_search($value, $this->product_report_keys);
}
if(!$key) {
$prefix = '';
}
@ -919,6 +985,7 @@ class BaseExport
$key = str_replace('contact.', '', $key);
$key = str_replace('payment.', '', $key);
$key = str_replace('expense.', '', $key);
$key = str_replace('product.', '', $key);
if(stripos($value, 'custom_value') !== false)
{
@ -957,7 +1024,7 @@ class BaseExport
}
}
nlog($header);
// nlog($header);
return $header;
}

View File

@ -17,6 +17,7 @@ use App\Models\Company;
use App\Transformers\ClientContactTransformer;
use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -72,13 +73,6 @@ class ClientExport extends BaseExport
'status' => 'status'
];
private array $decorate_keys = [
'client.country_id',
'client.shipping_country_id',
'client.currency',
'client.industry',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
@ -87,7 +81,27 @@ class ClientExport extends BaseExport
$this->contact_transformer = new ClientContactTransformer();
}
public function run()
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 ($client) {
return $this->buildRow($client);
})->toArray();
return array_merge(['columns' => $header], $report);
}
public function init(): Builder
{
MultiDB::setDb($this->company->db);
App::forgetInstance('translator');
@ -95,15 +109,9 @@ class ClientExport extends BaseExport
$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->client_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Client::query()->with('contacts')
->withTrashed()
@ -112,6 +120,20 @@ class ClientExport extends BaseExport
$query = $this->addDateRange($query);
return $query;
}
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 ($client) {
$this->csv->insertOne($this->buildRow($client));

View File

@ -18,6 +18,7 @@ use App\Models\Company;
use App\Transformers\ClientContactTransformer;
use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -32,54 +33,6 @@ class ContactExport extends BaseExport
public string $date_key = 'created_at';
public 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',
'client_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',
'contact_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 $input)
{
$this->company = $company;
@ -88,29 +41,39 @@ class ContactExport extends BaseExport
$this->contact_transformer = new ClientContactTransformer();
}
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->client_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = ClientContact::query()
->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
return $query;
}
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 ($contact) {
$this->csv->insertOne($this->buildRow($contact));
});
@ -118,6 +81,26 @@ class ContactExport extends BaseExport
return $this->csv->toString();
}
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 ($contact) {
return $this->buildRow($contact);
})->toArray();
return array_merge(['columns' => $header], $report);
}
private function buildRow(ClientContact $contact) :array
{
$transformed_contact = false;
@ -129,14 +112,13 @@ class ContactExport extends BaseExport
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
$keyval = array_search($key, $this->entity_keys);
if ($parts[0] == 'client' && array_key_exists($parts[1], $transformed_client)) {
$entity[$keyval] = $transformed_client[$parts[1]];
$entity[$key] = $transformed_client[$parts[1]];
} elseif ($parts[0] == 'contact' && array_key_exists($parts[1], $transformed_contact)) {
$entity[$keyval] = $transformed_contact[$parts[1]];
$entity[$key] = $transformed_contact[$parts[1]];
} else {
$entity[$keyval] = '';
$entity[$key] = '';
}
}

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\Document;
use App\Transformers\DocumentTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -30,16 +31,11 @@ class DocumentExport extends BaseExport
public 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 $input)
{
$this->company = $company;
@ -47,28 +43,55 @@ class DocumentExport extends BaseExport
$this->entity_transformer = new DocumentTransformer();
}
public function run()
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 ($document) {
$row = $this->buildRow($document);
})->toArray();
return array_merge(['columns' => $header], $report);
}
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);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Document::query()->where('company_id', $this->company->id);
$query = $this->addDateRange($query);
return $query;
}
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));

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\Expense;
use App\Transformers\ExpenseTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -28,51 +29,6 @@ class ExpenseExport extends BaseExport
public Writer $csv;
public array $entity_keys = [
'amount' => 'expense.amount',
'category' => 'expense.category',
'client' => 'expense.client_id',
'custom_value1' => 'expense.custom_value1',
'custom_value2' => 'expense.custom_value2',
'custom_value3' => 'expense.custom_value3',
'custom_value4' => 'expense.custom_value4',
'currency' => 'expense.currency_id',
'date' => 'expense.date',
'exchange_rate' => 'expense.exchange_rate',
'converted_amount' => 'expense.foreign_amount',
'invoice_currency_id' => 'expense.invoice_currency_id',
'payment_date' => 'expense.payment_date',
'number' => 'expense.number',
'payment_type_id' => 'expense.payment_type_id',
'private_notes' => 'expense.private_notes',
'project' => 'expense.project_id',
'public_notes' => 'expense.public_notes',
'tax_amount1' => 'expense.tax_amount1',
'tax_amount2' => 'expense.tax_amount2',
'tax_amount3' => 'expense.tax_amount3',
'tax_name1' => 'expense.tax_name1',
'tax_name2' => 'expense.tax_name2',
'tax_name3' => 'expense.tax_name3',
'tax_rate1' => 'expense.tax_rate1',
'tax_rate2' => 'expense.tax_rate2',
'tax_rate3' => 'expense.tax_rate3',
'transaction_reference' => 'expense.transaction_reference',
'vendor' => 'expense.vendor_id',
'invoice' => 'expense.invoice_id',
'user' => 'expense.user',
'assigned_user' => 'expense.assigned_user',
];
private array $decorate_keys = [
'client',
'currency',
'invoice',
'category',
'vendor',
'project',
'payment_type_id',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
@ -80,24 +36,38 @@ class ExpenseExport extends BaseExport
$this->expense_transformer = new ExpenseTransformer();
}
public function run()
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);
}
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->expense_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Expense::query()
->with('client')
->withTrashed()
@ -106,6 +76,20 @@ class ExpenseExport extends BaseExport
$query = $this->addDateRange($query);
return $query;
}
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 ($expense) {
$this->csv->insertOne($this->buildRow($expense));
@ -122,7 +106,6 @@ class ExpenseExport extends BaseExport
foreach (array_values($this->input['report_keys']) as $key) {
$parts = explode('.', $key);
$keyval = array_search($key, $this->entity_keys);
if (is_array($parts) && $parts[0] == 'expense' && array_key_exists($parts[1], $transformed_expense)) {
$entity[$key] = $transformed_expense[$parts[1]];

View File

@ -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,31 +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('currency_id', $this->input['report_keys'])) {
$entity['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('status_id', $this->input['report_keys'])) {
$entity['status'] = $invoice->stringStatus($invoice->status_id);
}
// $payment_exists = $invoice->payments()->exists();
if (in_array('invoice.country_id', $this->input['report_keys'])) {
$entity['invoice.country_id'] = $invoice->client->country ? ctrans("texts.country_{$invoice->client->country->name}") : '';
}
// $entity['payment_number'] = $payment_exists ? $invoice->payments()->pluck('number')->implode(',') : '';
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;
}
// $entity['payment_date'] = $payment_exists ? $invoice->payments()->pluck('date')->implode(',') : '';
if (in_array('invoice.client_id', $this->input['report_keys'])) {
$entity['invoice.client_id'] = $invoice->client->present()->name();
}
// $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() : "";
if (in_array('invoice.status', $this->input['report_keys'])) {
$entity['invoice.status'] = $invoice->stringStatus($invoice->status_id);
}
return $entity;
}

View File

@ -12,11 +12,11 @@
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\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -31,64 +31,7 @@ class InvoiceItemExport extends BaseExport
private bool $force_keys = false;
public array $entity_keys = [
'amount' => 'amount',
'balance' => 'balance',
'client' => 'client_id',
'client_number' => 'client.number',
'client_id_number' => 'client.id_number',
'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',
'quantity' => 'item.quantity',
'cost' => 'item.cost',
'product_key' => 'item.product_key',
'buy_price' => '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',
'tax_category' => 'item.tax_id',
'type' => 'item.type_id',
];
private array $storage_array = [];
private array $decorate_keys = [
'client',
@ -103,25 +46,20 @@ class InvoiceItemExport 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->force_keys = true;
$this->input['report_keys'] = array_values($this->entity_keys);
$this->input['report_keys'] = array_values($this->mergeItemsKeys('invoice_report_keys'));
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Invoice::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
@ -129,11 +67,46 @@ class InvoiceItemExport 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();
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
});
return array_merge(['columns' => $header], $this->storage_array);
}
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->iterateItems($invoice);
});
$this->csv->insertAll($this->storage_array);
return $this->csv->toString();
}
@ -146,46 +119,32 @@ class InvoiceItemExport extends BaseExport
foreach ($invoice->line_items as $item) {
$item_array = [];
foreach (array_values($this->input['report_keys']) as $key) { //items iterator produces item array
foreach (array_values(array_intersect($this->input['report_keys'], $this->item_report_keys)) as $key) { //items iterator produces item array
if (str_contains($key, "item.")) {
$key = str_replace("item.", "", $key);
$keyval = $key;
$keyval = str_replace("custom_value", "invoice", $key);
if($key == 'type_id')
$keyval = 'type';
$key = 'type';
if($key == 'tax_id')
$keyval = 'tax_category';
$key = 'tax_category';
if (property_exists($item, $key)) {
$item_array[$keyval] = $item->{$key};
} else {
$item_array[$keyval] = '';
$item_array[$key] = $item->{$key};
}
else {
$item_array[$key] = '';
}
}
}
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_items)) {
$entity[$keyval] = $transformed_items[$key];
} else {
$entity[$keyval] = "";
}
}
$transformed_items = array_merge($transformed_invoice, $item_array);
$entity = $this->decorateAdvancedFields($invoice, $transformed_items);
$this->csv->insertOne($entity);
$this->storage_array[] = $entity;
}
}
@ -196,23 +155,19 @@ class InvoiceItemExport 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("invoice.", "", $key), $this->entity_keys) ?? $key;
}
if(is_array($parts) && $parts[0] == 'item')
continue;
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);
if (is_array($parts) && $parts[0] == 'invoice' && array_key_exists($parts[1], $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$parts[1]];
}else if (array_key_exists($key, $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$key];
}
else {
$entity[$key] = $this->resolveKey($key, $invoice, $this->invoice_transformer);
}
}
@ -233,12 +188,12 @@ class InvoiceItemExport extends BaseExport
$entity['tax_category'] = $invoice->taxTypeString($entity['tax_category']);
}
if($this->force_keys) {
$entity['client'] = $invoice->client->present()->name();
$entity['client_id_number'] = $invoice->client->id_number;
$entity['client_number'] = $invoice->client->number;
$entity['status'] = $invoice->stringStatus($invoice->status_id);
}
// if($this->force_keys) {
// $entity['client'] = $invoice->client->present()->name();
// $entity['client_id_number'] = $invoice->client->id_number;
// $entity['client_number'] = $invoice->client->number;
// $entity['status'] = $invoice->stringStatus($invoice->status_id);
// }
return $entity;
}

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;
@ -27,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)
{
@ -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,24 +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 (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);
}
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);
}
}
return $this->decorateAdvancedFields($payment, $entity);

View File

@ -17,6 +17,7 @@ use App\Models\Document;
use App\Models\Product;
use App\Transformers\ProductTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -28,31 +29,6 @@ class ProductExport extends BaseExport
public Writer $csv;
public array $entity_keys = [
'project' => 'project_id',
'vendor' => 'vendor_id',
'custom_value1' => 'custom_value1',
'custom_value2' => 'custom_value2',
'custom_value3' => 'custom_value3',
'custom_value4' => 'custom_value4',
'product_key' => 'product_key',
'notes' => 'notes',
'cost' => 'cost',
'price' => 'price',
'quantity' => 'quantity',
'tax_rate1' => 'tax_rate1',
'tax_rate2' => 'tax_rate2',
'tax_rate3' => 'tax_rate3',
'tax_name1' => 'tax_name1',
'tax_name2' => 'tax_name2',
'tax_name3' => 'tax_name3',
];
private array $decorate_keys = [
'vendor',
'project',
];
public function __construct(Company $company, array $input)
{
$this->company = $company;
@ -60,24 +36,37 @@ class ProductExport extends BaseExport
$this->entity_transformer = new ProductTransformer();
}
public function run()
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);
}
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->product_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Product::query()
->withTrashed()
->where('company_id', $this->company->id)
@ -85,6 +74,21 @@ class ProductExport extends BaseExport
$query = $this->addDateRange($query);
return $query;
}
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));
@ -100,7 +104,7 @@ class ProductExport extends BaseExport
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) {
$keyval = array_search($key, $this->entity_keys);
$keyval = array_search($key, $this->product_report_keys);
if (array_key_exists($key, $transformed_entity)) {
$entity[$keyval] = $transformed_entity[$key];

View File

@ -11,14 +11,15 @@
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\PurchaseOrder;
use App\Transformers\PurchaseOrderTransformer;
use App\Utils\Ninja;
use App\Utils\Number;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
use App\Models\Company;
use App\Libraries\MultiDB;
use App\Models\PurchaseOrder;
use Illuminate\Support\Facades\App;
use App\Transformers\PurchaseOrderTransformer;
use Illuminate\Contracts\Database\Eloquent\Builder;
class PurchaseOrderExport extends BaseExport
{
@ -81,24 +82,19 @@ class PurchaseOrderExport extends BaseExport
$this->purchase_order_transformer = new PurchaseOrderTransformer();
}
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->purchase_order_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = PurchaseOrder::query()
->withTrashed()
->with('vendor')
@ -107,9 +103,38 @@ class PurchaseOrderExport extends BaseExport
$query = $this->addDateRange($query);
// if(isset($this->input['status'])) {
// $query = $this->addPurchaseOrderStatusFilter($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 ($purchase_order) {
@ -126,23 +151,16 @@ class PurchaseOrderExport 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("purchase_order.", "", $key), $this->entity_keys) ?? $key;
}
$parts = explode('.', $key);
if(!$keyval) {
$keyval = $key;
}
if (array_key_exists($key, $transformed_purchase_order)) {
$entity[$keyval] = $transformed_purchase_order[$key];
} elseif (array_key_exists($keyval, $transformed_purchase_order)) {
$entity[$keyval] = $transformed_purchase_order[$keyval];
if (is_array($parts) && $parts[0] == 'purchase_order' && array_key_exists($parts[1], $transformed_purchase_order)) {
$entity[$key] = $transformed_purchase_order[$parts[1]];
} else {
$entity[$keyval] = $this->resolveKey($keyval, $purchase_order, $this->purchase_order_transformer);
$entity[$key] = $this->resolveKey($key, $purchase_order, $this->purchase_order_transformer);
}
}
return $this->decorateAdvancedFields($purchase_order, $entity);

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\PurchaseOrder;
use App\Transformers\PurchaseOrderTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -30,70 +31,7 @@ class PurchaseOrderItemExport extends BaseExport
private bool $force_keys = false;
public array $entity_keys = [
'amount' => 'amount',
'balance' => 'balance',
'vendor' => 'vendor_id',
'vendor_number' => 'vendor.number',
'vendor_id_number' => 'vendor.id_number',
// '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',
'quantity' => 'item.quantity',
'cost' => 'item.cost',
'product_key' => 'item.product_key',
'buy_price' => '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',
'purchase_order1' => 'item.custom_value1',
'purchase_order2' => 'item.custom_value2',
'purchase_order3' => 'item.custom_value3',
'purchase_order4' => 'item.custom_value4',
'tax_category' => 'item.tax_id',
'type' => 'item.type_id',
];
private array $decorate_keys = [
'client',
'currency_id',
'status'
];
private array $storage_array = [];
public function __construct(Company $company, array $input)
{
@ -102,25 +40,20 @@ class PurchaseOrderItemExport extends BaseExport
$this->purchase_order_transformer = new PurchaseOrderTransformer();
}
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->force_keys = true;
$this->input['report_keys'] = array_values($this->entity_keys);
// $this->force_keys = true;
$this->input['report_keys'] = array_values($this->mergeItemsKeys('purchase_order_report_keys'));
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = PurchaseOrder::query()
->withTrashed()
->with('vendor')->where('company_id', $this->company->id)
@ -128,12 +61,47 @@ class PurchaseOrderItemExport 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();
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
});
return array_merge(['columns' => $header], $this->storage_array);
}
public function run()
{
//load the CSV document from a string
$this->csv = Writer::createFromString();
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query->cursor()
->each(function ($purchase_order) {
$this->iterateItems($purchase_order);
});
$this->csv->insertAll($this->storage_array);
return $this->csv->toString();
}
private function iterateItems(PurchaseOrder $purchase_order)
@ -141,7 +109,7 @@ class PurchaseOrderItemExport extends BaseExport
$transformed_purchase_order = $this->buildRow($purchase_order);
$transformed_items = [];
nlog($purchase_order->toArray());
foreach ($purchase_order->line_items as $item) {
$item_array = [];
@ -151,10 +119,6 @@ class PurchaseOrderItemExport extends BaseExport
$key = str_replace("item.", "", $key);
$keyval = $key;
$keyval = str_replace("custom_value", "purchase_order", $key);
if($key == 'type_id') {
$keyval = 'type';
}
@ -164,29 +128,17 @@ class PurchaseOrderItemExport extends BaseExport
}
if (property_exists($item, $key)) {
$item_array[$keyval] = $item->{$key};
$item_array[$key] = $item->{$key};
} else {
$item_array[$keyval] = '';
$item_array[$key] = '';
}
}
}
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_items)) {
$entity[$keyval] = $transformed_items[$key];
} else {
$entity[$keyval] = "";
}
}
$transformed_items = array_merge($transformed_purchase_order, $item_array);
$entity = $this->decorateAdvancedFields($purchase_order, $transformed_items);
$this->csv->insertOne($entity);
$this->storage_array[] = $entity;
}
}
@ -197,22 +149,18 @@ class PurchaseOrderItemExport 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("purchase_order.", "", $key), $this->entity_keys) ?? $key;
if(is_array($parts) && $parts[0] == 'item') {
continue;
}
if(!$keyval) {
$keyval = $key;
}
if (array_key_exists($key, $transformed_purchase_order)) {
$entity[$keyval] = $transformed_purchase_order[$key];
} elseif (array_key_exists($keyval, $transformed_purchase_order)) {
$entity[$keyval] = $transformed_purchase_order[$keyval];
if (is_array($parts) && $parts[0] == 'purchase_order' && array_key_exists($parts[1], $transformed_purchase_order)) {
$entity[$key] = $transformed_purchase_order[$parts[1]];
} elseif (array_key_exists($key, $transformed_purchase_order)) {
$entity[$key] = $transformed_purchase_order[$key];
} else {
$entity[$keyval] = $this->resolveKey($keyval, $purchase_order, $this->purchase_order_transformer);
$entity[$key] = $this->resolveKey($key, $purchase_order, $this->purchase_order_transformer);
}
}

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\Quote;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -28,43 +29,6 @@ class QuoteExport 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',
'valid_until' => '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',
'invoice' => 'invoice_id',
];
private array $decorate_keys = [
'client',
'currency',
@ -78,24 +42,20 @@ class QuoteExport extends BaseExport
$this->quote_transformer = new QuoteTransformer();
}
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->quote_report_keys);
}
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Quote::query()
->withTrashed()
->with('client')
@ -104,6 +64,40 @@ class QuoteExport 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()
{
//load the CSV document from a string
$this->csv = Writer::createFromString();
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query->cursor()
->each(function ($quote) {
$this->csv->insertOne($this->buildRow($quote));
@ -114,50 +108,42 @@ class QuoteExport extends BaseExport
private function buildRow(Quote $quote) :array
{
$transformed_entity = $this->quote_transformer->transform($quote);
$transformed_invoice = $this->quote_transformer->transform($quote);
$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] == 'quote' && array_key_exists($parts[1], $transformed_invoice)) {
$entity[$key] = $transformed_invoice[$parts[1]];
} else {
$entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer);
}
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, $quote, $this->quote_transformer);
}
}
return $this->decorateAdvancedFields($quote, $entity);
}
private function decorateAdvancedFields(Quote $quote, array $entity) :array
{
if (in_array('currency_id', $this->input['report_keys'])) {
$entity['currency'] = $quote->client->currency()->code;
if (in_array('quote.currency_id', $this->input['report_keys'])) {
$entity['quote.currency'] = $quote->client->currency()->code;
}
if (in_array('client_id', $this->input['report_keys'])) {
$entity['client'] = $quote->client->present()->name();
if (in_array('quote.client_id', $this->input['report_keys'])) {
$entity['quote.client'] = $quote->client->present()->name();
}
if (in_array('status_id', $this->input['report_keys'])) {
$entity['status'] = $quote->stringStatus($quote->status_id);
if (in_array('quote.status', $this->input['report_keys'])) {
$entity['quote.status'] = $quote->stringStatus($quote->status_id);
}
if (in_array('invoice_id', $this->input['report_keys'])) {
$entity['invoice'] = $quote->invoice ? $quote->invoice->number : '';
if (in_array('quote.invoice_id', $this->input['report_keys'])) {
$entity['quote.invoice'] = $quote->invoice ? $quote->invoice->number : '';
}
return $entity;

View File

@ -16,6 +16,7 @@ use App\Models\Company;
use App\Models\Quote;
use App\Transformers\QuoteTransformer;
use App\Utils\Ninja;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use League\Csv\Writer;
@ -28,63 +29,7 @@ class QuoteItemExport 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' => 'currency_id',
'quantity' => 'item.quantity',
'cost' => 'item.cost',
'product_key' => 'item.product_key',
'buy_price' => 'item.product_cost',
'cost' => 'item.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',
'quote1' => 'item.custom_value1',
'quote2' => 'item.custom_value2',
'quote3' => 'item.custom_value3',
'quote4' => 'item.custom_value4',
'tax_category' => 'item.tax_id',
'type' => 'item.type_id',
];
private array $storage_array;
private array $decorate_keys = [
'client',
@ -98,37 +43,70 @@ class QuoteItemExport extends BaseExport
$this->quote_transformer = new QuoteTransformer();
}
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));
if (count($this->input['report_keys']) == 0) {
$this->input['report_keys'] = array_values($this->mergeItemsKeys('quote_report_keys'));
}
$query = Quote::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted', 0);
$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();
$query->cursor()
->each(function ($resource) {
$this->iterateItems($resource);
});
return array_merge(['columns' => $header], $this->storage_array);
}
public function run()
{
//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);
}
$query = $this->init();
//insert the header
$this->csv->insertOne($this->buildHeader());
$query = Quote::query()
->withTrashed()
->with('client')->where('company_id', $this->company->id)
->where('is_deleted', 0);
$query = $this->addDateRange($query);
$query->cursor()
->each(function ($quote) {
$this->iterateItems($quote);
});
$this->csv->insertAll($this->storage_array);
return $this->csv->toString();
}
private function iterateItems(Quote $quote)
@ -137,51 +115,34 @@ class QuoteItemExport extends BaseExport
$transformed_items = [];
$transformed_items = [];
foreach ($quote->line_items as $item) {
$item_array = [];
foreach (array_values($this->input['report_keys']) as $key) { //items iterator produces item array
foreach (array_values(array_intersect($this->input['report_keys'], $this->item_report_keys)) as $key) { //items iterator produces item array
if (str_contains($key, "item.")) {
$key = str_replace("item.", "", $key);
$keyval = $key;
$keyval = str_replace("custom_value", "quote", $key);
if($key == 'type_id')
$keyval = 'type';
$key = 'type';
if($key == 'tax_id')
$keyval = 'tax_category';
$key = 'tax_category';
if (property_exists($item, $key)) {
$item_array[$keyval] = $item->{$key};
} else {
$item_array[$keyval] = '';
$item_array[$key] = $item->{$key};
}
else {
$item_array[$key] = '';
}
}
}
$entity = [];
foreach (array_values($this->input['report_keys']) as $key) { //create an array of report keys only
$keyval = array_search($key, $this->entity_keys);
if (array_key_exists($key, $transformed_items)) {
$entity[$keyval] = $transformed_items[$key];
} else {
$entity[$keyval] = "";
}
}
$transformed_items = array_merge($transformed_quote, $item_array);
$entity = $this->decorateAdvancedFields($quote, $transformed_items);
$this->csv->insertOne($entity);
$this->storage_array[] = $entity;
}
}
@ -192,26 +153,23 @@ class QuoteItemExport 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("quote.", "", $key), $this->entity_keys) ?? $key;
if(is_array($parts) && $parts[0] == 'item') {
continue;
}
if(!$keyval) {
$keyval = $key;
}
if (array_key_exists($key, $transformed_quote)) {
$entity[$keyval] = $transformed_quote[$key];
} elseif (array_key_exists($keyval, $transformed_quote)) {
$entity[$keyval] = $transformed_quote[$keyval];
}
else {
$entity[$keyval] = $this->resolveKey($keyval, $quote, $this->quote_transformer);
if (is_array($parts) && $parts[0] == 'quote' && array_key_exists($parts[1], $transformed_quote)) {
$entity[$key] = $transformed_quote[$parts[1]];
} elseif (array_key_exists($key, $transformed_quote)) {
$entity[$key] = $transformed_quote[$key];
} else {
$entity[$key] = $this->resolveKey($key, $quote, $this->quote_transformer);
}
}
return $this->decorateAdvancedFields($quote, $entity);
}
private function decorateAdvancedFields(Quote $quote, array $entity) :array

View File

@ -71,15 +71,21 @@ class BankTransactionController extends BaseController
public function create(CreateBankTransactionRequest $request)
{
$bank_transaction = BankTransactionFactory::create(auth()->user()->company()->id, auth()->user()->id);
/** @var \App\Models\User $user */
$user = auth()->user();
$bank_transaction = BankTransactionFactory::create($user->company()->id, $user->id);
return $this->itemResponse($bank_transaction);
}
public function store(StoreBankTransactionRequest $request)
{
/** @var \App\Models\User $user */
$user = auth()->user();
//stub to store the model
$bank_transaction = $this->bank_transaction_repo->save($request->all(), BankTransactionFactory::create(auth()->user()->company()->id, auth()->user()->id));
$bank_transaction = $this->bank_transaction_repo->save($request->all(), BankTransactionFactory::create($user->company()->id, $user->id));
return $this->itemResponse($bank_transaction);
}

View File

@ -14,6 +14,7 @@ namespace App\Http\Controllers\Reports;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Export\CSV\ActivityExport;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
@ -31,14 +32,27 @@ class ActivityReportController 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(), ActivityExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), ActivityExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new ActivityExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), ActivityExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new ActivityExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Response;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Export\CSV\ContactExport;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class ClientContactReportController extends BaseController
{
@ -62,13 +63,29 @@ class ClientContactReportController 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(), ContactExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), ContactExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new ContactExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), ContactExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
// expect a list of visible fields, or use the default
$export = new ContactExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,13 +11,14 @@
namespace App\Http\Controllers\Reports;
use App\Models\Client;
use Illuminate\Http\Response;
use App\Utils\Traits\MakesHash;
use App\Export\CSV\ClientExport;
use App\Jobs\Report\SendToAdmin;
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 ClientReportController extends BaseController
{
@ -63,14 +64,26 @@ class ClientReportController 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(), ClientExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), ClientExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new ClientExport(auth()->user()->company(), $request->all());
// expect a list of visible fields, or use the default
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), ClientExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new ClientExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -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();

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Response;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Jobs\Report\PreviewReport;
use App\Export\CSV\InvoiceItemExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class InvoiceItemReportController extends BaseController
{
@ -62,14 +63,26 @@ class InvoiceItemReportController 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(), InvoiceItemExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), InvoiceItemExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new InvoiceItemExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), InvoiceItemExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new InvoiceItemExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Response;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Export\CSV\InvoiceExport;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class InvoiceReportController extends BaseController
{
@ -62,14 +63,26 @@ class InvoiceReportController 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(), InvoiceExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), InvoiceExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new InvoiceExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), InvoiceExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
// expect a list of visible fields, or use the default
$export = new InvoiceExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -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();

View File

@ -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\ProductExport;
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 ProductReportController extends BaseController
{
@ -63,14 +64,27 @@ class ProductReportController 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(), ProductExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), ProductExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new ProductExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), ProductExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new ProductExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,11 +11,12 @@
namespace App\Http\Controllers\Reports;
use App\Export\CSV\PurchaseOrderItemExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Export\CSV\PurchaseOrderItemExport;
use App\Http\Requests\Report\GenericReportRequest;
class PurchaseOrderItemReportController extends BaseController
{
@ -30,14 +31,26 @@ class PurchaseOrderItemReportController 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(), PurchaseOrderItemExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), PurchaseOrderItemExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new PurchaseOrderItemExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), PurchaseOrderItemExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new PurchaseOrderItemExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,11 +11,12 @@
namespace App\Http\Controllers\Reports;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Jobs\Report\PreviewReport;
use App\Export\CSV\PurchaseOrderExport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
class PurchaseOrderReportController extends BaseController
{
@ -30,14 +31,29 @@ class PurchaseOrderReportController 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(), PurchaseOrderExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), PurchaseOrderExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new PurchaseOrderExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), PurchaseOrderExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new PurchaseOrderExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -11,12 +11,13 @@
namespace App\Http\Controllers\Reports;
use Illuminate\Http\Response;
use App\Export\CSV\QuoteExport;
use App\Utils\Traits\MakesHash;
use App\Jobs\Report\SendToAdmin;
use App\Jobs\Report\PreviewReport;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Report\GenericReportRequest;
use App\Jobs\Report\SendToAdmin;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Response;
class QuoteReportController extends BaseController
{
@ -62,14 +63,26 @@ class QuoteReportController 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(), QuoteExport::class, $this->filename);
SendToAdmin::dispatch($user->company(), $request->all(), QuoteExport::class, $this->filename);
return response()->json(['message' => 'working...'], 200);
}
// expect a list of visible fields, or use the default
$export = new QuoteExport(auth()->user()->company(), $request->all());
if($request->has('output') && $request->input('output') == 'json') {
$hash = \Illuminate\Support\Str::uuid();
PreviewReport::dispatch($user->company(), $request->all(), QuoteExport::class, $hash);
return response()->json(['message' => $hash], 200);
}
$export = new QuoteExport($user->company(), $request->all());
$csv = $export->run();

View File

@ -39,7 +39,8 @@ class PreviewReport implements ShouldQueue
/** @var \App\Export\CSV\CreditExport $export */
$export = new $this->report_class($this->company, $this->request);
$report = $export->returnJson();
nlog($report);
nlog($this->report_class);
// nlog($report);
Cache::put($this->hash, $report, 60 * 60);
}

View File

@ -159,6 +159,7 @@ class PaymentEmailEngine extends BaseEmailEngine
$data['$entity'] = ['value' => '', 'label' => ctrans('texts.payment')];
$data['$payment.amount'] = ['value' => Number::formatMoney($this->payment->amount, $this->client) ?: ' ', 'label' => ctrans('texts.amount')];
$data['$payment.refunded'] = ['value' => Number::formatMoney($this->payment->refunded, $this->client) ?: ' ', 'label' => ctrans('texts.refund')];
$data['$payment.unapplied'] = ['value' => Number::formatMoney(($this->payment->amount - $this->payment->refunded - $this->payment->applied), $this->client) ?: ' ', 'label' => ctrans('texts.refund')];
$data['$amount'] = &$data['$payment.amount'];
$data['$payment.date'] = ['value' => $this->translateDate($this->payment->date, $this->client->date_format(), $this->client->locale()), 'label' => ctrans('texts.payment_date')];
$data['$transaction_reference'] = ['value' => $this->payment->transaction_reference, 'label' => ctrans('texts.transaction_reference')];

View File

@ -153,7 +153,7 @@ class BaseRepository
$model->client_id = $data['client_id'];
}
$client = Client::with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail();
$client = Client::query()->with('group_settings')->where('id', $model->client_id)->withTrashed()->firstOrFail();
$state = [];

View File

@ -113,6 +113,11 @@ class ExpenseRepository extends BaseRepository
$expense->saveQuietly();
$expense->transaction->expense_id = $exp_ids;
if(strlen($exp_ids) <= 2) {
$expense->transaction->status_id = 1;
}
$expense->transaction->saveQuietly();
}

View File

@ -60,6 +60,7 @@ class DeletePayment
BankTransaction::query()->where('payment_id', $this->payment->id)->cursor()->each(function ($bt){
$bt->payment_id = null;
$bt->status_id = 1;
$bt->save();
});

View File

@ -42,6 +42,23 @@ class ReportApiTest extends TestCase
}
public function testActivityCSVExport()
{
$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/activities', $data)
->assertStatus(200);
}
public function testUserSalesReportApiRoute()
{
$data = [
@ -125,6 +142,8 @@ class ReportApiTest extends TestCase
}
public function testClientBalanceReportApiRoute()
{
$data = [

View File

@ -527,7 +527,7 @@ class ReportCsvGenerationTest extends TestCase
])->post('/api/v1/reports/products', $data);
$csv = $response->streamedContent();
// nlog($csv);
$this->assertEquals('product_key', $this->getFirstValueByColumn($csv, 'Product'));
$this->assertEquals('notes', $this->getFirstValueByColumn($csv, 'Notes'));
$this->assertEquals(100, $this->getFirstValueByColumn($csv, 'Cost'));
@ -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'));
}
@ -673,7 +673,7 @@ class ReportCsvGenerationTest extends TestCase
])->post('/api/v1/reports/clients', $data);
$csv = $response->streamedContent();
nlog($csv);
// nlog($csv);
$reader = Reader::createFromString($csv);
$reader->setHeaderOffset(0);
@ -748,7 +748,7 @@ nlog($csv);
$arr = $response->json();
nlog($arr['message']);
// nlog($arr['message']);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
@ -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'));
@ -985,20 +985,20 @@ nlog($csv);
])->post('/api/v1/reports/invoice_items', $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'));
$this->assertEquals('', $this->getFirstValueByColumn($csv, 'Payment Date'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Item Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Item Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Item Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Item Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Item Product'));
$this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Item Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Tax Rate 1'));
$data = [
@ -1082,15 +1082,15 @@ nlog($csv);
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote Number'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Item Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Item Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Item Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Item Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Item Product'));
$this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Item Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Tax Rate 1'));
$data = [
@ -1222,15 +1222,15 @@ nlog($csv);
$this->assertEquals('Vendor 1', $this->getFirstValueByColumn($csv, 'Vendor Name'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Purchase Order Number'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Product'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Quantity'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Item Cost'));
$this->assertEquals('1000', $this->getFirstValueByColumn($csv, 'Item Line Total'));
$this->assertEquals('0', $this->getFirstValueByColumn($csv, 'Item Discount'));
$this->assertEquals('item notes', $this->getFirstValueByColumn($csv, 'Item Notes'));
$this->assertEquals('product key', $this->getFirstValueByColumn($csv, 'Item Product'));
$this->assertEquals('custom 1', $this->getFirstValueByColumn($csv, 'Item Custom Value 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Tax Rate 1'));
$this->assertEquals('GST', $this->getFirstValueByColumn($csv, 'Item Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Item Tax Rate 1'));
}
@ -1347,17 +1347,17 @@ nlog($csv);
$reader = Reader::createFromString($csv);
$reader->setHeaderOffset(0);
$res = $reader->fetchColumnByName('First Name');
$res = $reader->fetchColumnByName('Contact First Name');
$res = iterator_to_array($res, true);
$this->assertEquals('john', $res[1]);
$res = $reader->fetchColumnByName('Last Name');
$res = $reader->fetchColumnByName('Contact Last Name');
$res = iterator_to_array($res, true);
$this->assertEquals('doe', $res[1]);
$res = $reader->fetchColumnByName('Email');
$res = $reader->fetchColumnByName('Contact Email');
$res = iterator_to_array($res, true);
$this->assertEquals('john@doe.com', $res[1]);
@ -1492,29 +1492,29 @@ nlog($csv);
$csv = $response->streamedContent();
$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'));
}
@ -1642,30 +1642,32 @@ 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('2020-01-01', $this->getFirstValueByColumn($csv, 'Valid Until'));
$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('Expired', $this->getFirstValueByColumn($csv, 'Status'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Quote Amount'));
$this->assertEquals('50', $this->getFirstValueByColumn($csv, 'Quote Balance'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quote Discount'));
$this->assertEquals('1234', $this->getFirstValueByColumn($csv, 'Quote PO Number'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Quote Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Quote Private Notes'));
$this->assertEquals('Terms', $this->getFirstValueByColumn($csv, 'Quote Terms'));
$this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Quote Date'));
$this->assertEquals('2020-01-01', $this->getFirstValueByColumn($csv, 'Quote Valid Until'));
$this->assertEquals('2021-01-03', $this->getFirstValueByColumn($csv, 'Quote Partial Due Date'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quote Partial/Deposit'));
$this->assertEquals('Custom 1', $this->getFirstValueByColumn($csv, 'Quote Custom Value 1'));
$this->assertEquals('Custom 2', $this->getFirstValueByColumn($csv, 'Quote Custom Value 2'));
$this->assertEquals('Custom 3', $this->getFirstValueByColumn($csv, 'Quote Custom Value 3'));
$this->assertEquals('Custom 4', $this->getFirstValueByColumn($csv, 'Quote Custom Value 4'));
$this->assertEquals('Footer', $this->getFirstValueByColumn($csv, 'Quote Footer'));
$this->assertEquals('Tax 1', $this->getFirstValueByColumn($csv, 'Quote Tax Name 1'));
$this->assertEquals('10', $this->getFirstValueByColumn($csv, 'Quote Tax Rate 1'));
$this->assertEquals('Tax 2', $this->getFirstValueByColumn($csv, 'Quote Tax Name 2'));
$this->assertEquals('20', $this->getFirstValueByColumn($csv, 'Quote Tax Rate 2'));
$this->assertEquals('Tax 3', $this->getFirstValueByColumn($csv, 'Quote Tax Name 3'));
$this->assertEquals('30', $this->getFirstValueByColumn($csv, 'Quote Tax Rate 3'));
$this->assertEquals('Expired', $this->getFirstValueByColumn($csv, 'Quote Status'));
}
@ -1696,10 +1698,10 @@ nlog($csv);
$csv = $response->streamedContent();
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Private Notes'));
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'User'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Expense Amount'));
$this->assertEquals('Public', $this->getFirstValueByColumn($csv, 'Expense Public Notes'));
$this->assertEquals('Private', $this->getFirstValueByColumn($csv, 'Expense Private Notes'));
$this->assertEquals($this->user->present()->name(), $this->getFirstValueByColumn($csv, 'Expense User'));
$data = [
@ -1758,8 +1760,8 @@ nlog($csv);
$this->assertEquals('bob', $this->getFirstValueByColumn($csv, 'Client Name'));
$this->assertEquals('Vendor 1', $this->getFirstValueByColumn($csv, 'Vendor Name'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Amount'));
$this->assertEquals('USD', $this->getFirstValueByColumn($csv, 'Currency'));
$this->assertEquals('100', $this->getFirstValueByColumn($csv, 'Expense Amount'));
$this->assertEquals('USD', $this->getFirstValueByColumn($csv, 'Expense Currency'));
}

View File

@ -12,10 +12,24 @@
namespace Tests\Feature\Export;
use Tests\TestCase;
use App\Models\Client;
use App\Models\Expense;
use App\Models\Document;
use Tests\MockAccountData;
use App\Export\CSV\QuoteExport;
use App\Utils\Traits\MakesHash;
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\ProductExport;
use App\Export\CSV\ActivityExport;
use App\Export\CSV\DocumentExport;
use App\Jobs\Report\PreviewReport;
use Illuminate\Support\Facades\Cache;
use App\Export\CSV\PurchaseOrderExport;
use Illuminate\Routing\Middleware\ThrottleRequests;
/**
@ -44,6 +58,407 @@ class ReportPreviewTest extends TestCase
}
public function testProductJsonExport()
{
\App\Models\Product::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/products?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, ProductExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
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 testPurchaseOrderItemJsonExport()
{
\App\Models\PurchaseOrder::factory()->count(5)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'vendor_id' => $this->vendor->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/purchase_order_items?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, \App\Export\CSV\PurchaseOrderItemExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
//nlog($r);
}
public function testQuoteItemJsonExport()
{
\App\Models\Quote::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/quote_items?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, \App\Export\CSV\QuoteItemExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
//nlog($r);
}
public function testInvoiceItemJsonExport()
{
\App\Models\Invoice::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/invoice_items?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, \App\Export\CSV\InvoiceItemExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
//nlog($r);
}
public function testPurchaseOrderJsonExport()
{
\App\Models\PurchaseOrder::factory()->count(5)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'vendor_id' => $this->vendor->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/purchase_orders?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, PurchaseOrderExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testQuoteJsonExport()
{
\App\Models\Quote::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/quotes?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, QuoteExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testInvoiceJsonExport()
{
\App\Models\Invoice::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/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);
}
public function testExpenseJsonExport()
{
Expense::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/expenses?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, ExpenseExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
//nlog($r);
}
public function testDocumentJsonExport()
{
Document::factory()->count(5)->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'documentable_type' => Client::class,
'documentable_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/documents?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, DocumentExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
//nlog($r);
}
public function testClientExportJson()
{
$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/clients?output=json', $data)
->assertStatus(200);
$data = [
'send_email' => false,
'date_range' => 'all',
'report_keys' => ['client.name','client.balance'],
];
$p = (new PreviewReport($this->company, $data, ClientExport::class, 'client_export1'))->handle();
$this->assertNull($p);
$r = Cache::pull('client_export1');
$this->assertNotNull($r);
}
public function testClientContactExportJsonLimitedKeys()
{
$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/client_contacts?output=json', $data)
->assertStatus(200);
$data = [
'send_email' => false,
'date_range' => 'all',
'report_keys' => ['client.name','client.balance','contact.email'],
];
$p = (new PreviewReport($this->company, $data, ContactExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testActivityCSVExportJson()
{
$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/activities?output=json', $data)
->assertStatus(200);
$p = (new PreviewReport($this->company, $data, ActivityExport::class, '123'))->handle();
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testCreditExportPreview()
{
@ -57,6 +472,10 @@ class ReportPreviewTest extends TestCase
$this->assertNull($p);
$r = Cache::pull('123');
$this->assertNotNull($r);
}
public function testCreditPreview()

View File

@ -11,15 +11,23 @@
namespace Tests\Feature;
use App\Jobs\Util\ReminderJob;
use App\Models\Invoice;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Support\Carbon;
use Tests\MockAccountData;
use Tests\TestCase;
use App\Models\User;
use App\Models\Client;
use App\Models\Account;
use App\Models\Company;
use App\Models\Invoice;
use Tests\MockAccountData;
use App\Models\CompanyToken;
use App\Models\ClientContact;
use App\Jobs\Util\ReminderJob;
use Illuminate\Support\Carbon;
use App\Utils\Traits\MakesHash;
use App\DataMapper\CompanySettings;
use App\Factory\CompanyUserFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -49,10 +57,201 @@ class ReminderTest extends TestCase
$this->withoutExceptionHandling();
}
public $company;
public $user;
public $payload;
public $account;
public $client;
public $token;
public $cu;
public $invoice;
private function buildData($settings = null)
{
$this->account = Account::factory()->create([
'hosted_client_count' => 1000,
'hosted_company_count' => 1000,
]);
$this->account->num_users = 3;
$this->account->save();
$this->user = User::factory()->create([
'account_id' => $this->account->id,
'confirmation_code' => 'xyz123',
'email' => $this->faker->unique()->safeEmail(),
]);
if(!$settings)
{
$settings = CompanySettings::defaults();
$settings->client_online_payment_notification = false;
$settings->client_manual_payment_notification = false;
}
$this->company = Company::factory()->create([
'account_id' => $this->account->id,
'settings' => $settings,
]);
$this->company->settings = $settings;
$this->company->save();
$this->cu = CompanyUserFactory::create($this->user->id, $this->company->id, $this->account->id);
$this->cu->is_owner = true;
$this->cu->is_admin = true;
$this->cu->is_locked = false;
$this->cu->save();
$this->token = \Illuminate\Support\Str::random(64);
$company_token = new CompanyToken;
$company_token->user_id = $this->user->id;
$company_token->company_id = $this->company->id;
$company_token->account_id = $this->account->id;
$company_token->name = 'test token';
$company_token->token = $this->token;
$company_token->is_system = true;
$company_token->save();
$this->client = Client::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'is_deleted' => 0,
'name' => 'bob',
'address1' => '1234',
'balance' => 100,
'paid_to_date' => 50,
]);
ClientContact::factory()->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'is_primary' => 1,
'first_name' => 'john',
'last_name' => 'doe',
'email' => 'john@doe.com'
]);
$this->invoice = Invoice::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_id' => $this->client->id,
'date' => now()->addSeconds($this->client->timezone_offset())->format('Y-m-d'),
'next_send_date' => null,
'due_date' => Carbon::now()->addSeconds($this->client->timezone_offset())->addDays(5)->format('Y-m-d'),
'last_sent_date' => now()->addSeconds($this->client->timezone_offset()),
'reminder_last_sent' => null,
'status_id' => 2,
'amount' => 10,
'balance' => 10,
]);
}
public function testsForTranslationsInReminders()
{
$translations = new \stdClass;
$translations->late_fee_added = "Fee added :date";
$settings = $this->company->settings;
$settings->enable_reminder1 = true;
$settings->schedule_reminder1 = 'after_invoice_date';
$settings->num_days_reminder1 = 1;
$settings->enable_reminder2 = true;
$settings->schedule_reminder2 = 'after_invoice_date';
$settings->num_days_reminder2 = 2;
$settings->enable_reminder3 = true;
$settings->schedule_reminder3 = 'after_invoice_date';
$settings->num_days_reminder3 = 3;
$settings->timezone_id = '29';
$settings->entity_send_time = 0;
$settings->endless_reminder_frequency_id = '';
$settings->enable_reminder_endless = false;
$settings->translations = $translations;
$settings->late_fee_amount1 = '101';
$settings->late_fee_amount2 = '102';
$settings->late_fee_amount3 = '103';
$this->buildData(($settings));
$this->assertEquals("Fee added :date", $this->company->settings->translations->late_fee_added);
$fetched_settings = $this->client->getMergedSettings();
$this->assertEquals("Fee added :date", $fetched_settings->translations->late_fee_added);
$this->invoice->service()->setReminder($settings)->save();
$this->invoice = $this->invoice->fresh();
$this->assertEquals(now()->addSeconds($this->client->timezone_offset())->format('Y-m-d'), $this->invoice->date);
$this->assertNotNull($this->invoice->next_send_date);
$this->assertEquals(now()->addDay()->addSeconds($this->client->timezone_offset())->format('Y-m-d 00:00:00'), $this->invoice->next_send_date);
$this->travelTo(now()->addDay()->startOfDay()->addHour());
(new ReminderJob())->handle();
$this->invoice = $this->invoice->fresh();
$this->assertNotNull($this->invoice->reminder1_sent);
$this->assertNotNull($this->invoice->reminder_last_sent);
$fee = collect($this->invoice->line_items)->where('type_id', 5)->first();
$this->assertEquals(101, $fee->cost);
$this->assertEquals('Fee added '.now()->format('d/M/Y'), $fee->notes);
$this->travelTo(now()->addDay()->startOfDay()->addHour());
(new ReminderJob())->handle();
$this->invoice = $this->invoice->fresh();
$this->assertNotNull($this->invoice->reminder2_sent);
$this->assertNotNull($this->invoice->reminder_last_sent);
$fee = collect($this->invoice->line_items)->where('cost', 102)->first();
$this->assertEquals(102, $fee->cost);
$this->assertEquals('Fee added '.now()->format('d/M/Y'), $fee->notes);
$this->travelTo(now()->addDay()->startOfDay()->addHour());
(new ReminderJob())->handle();
$this->invoice = $this->invoice->fresh();
$this->assertNotNull($this->invoice->reminder3_sent);
$this->assertNotNull($this->invoice->reminder_last_sent);
$fee = collect($this->invoice->line_items)->where('cost', 103)->first();
$this->assertEquals(103, $fee->cost);
$this->assertEquals('Fee added '.now()->format('d/M/Y'), $fee->notes);
// $this->travelTo(now()->addHours(1));
// }
$this->travelBack();
}
public function testForReminderFiringCorrectly()
{
$this->invoice->status_id = 2;
$this->invoice->amount = 10;
$this->invoice->balance = 10;
$this->invoice->next_send_date = null;
$this->invoice->date = now()->format('Y-m-d');
$this->invoice->last_sent_date = now();

View File

@ -574,6 +574,8 @@ trait MockAccountData
$this->purchase_order->tax_rate2 = 0;
$this->purchase_order->tax_rate3 = 0;
$this->purchase_order->line_items = InvoiceItemFactory::generate(5);
$this->purchase_order->uses_inclusive_taxes = false;
$this->purchase_order->save();