1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Merge pull request #4553 from beganovich/v5-support-for-custom-columns-on-invoice

(v5) Support for custom columns on invoice
This commit is contained in:
Benjamin Beganović 2020-12-23 13:12:37 +01:00 committed by GitHub
commit 3251efeb40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 97 additions and 21 deletions

View File

@ -32,7 +32,6 @@ use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Browsershot;
/**
* Class SetupController.

View File

@ -110,10 +110,9 @@ class CSVImport implements ShouldQueue
'settings' => $this->company->settings
];
info(print_r($data,1));
info(print_r($data, 1));
MailRouter::dispatch(new ImportCompleted($data), $this->company, auth()->user());
}
public function failed($exception)
@ -179,20 +178,20 @@ info(print_r($data,1));
$invoice['line_items'] = $this->cleanItems($items);
$validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules());
if ($validator->fails()) {
$this->error_array['invoices'] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
} else {
$validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules());
if ($validator->fails()) {
$this->error_array[] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
$this->error_array['invoices'] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
} else {
$invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record)));
if ($validator->fails()) {
$this->error_array[] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
} else {
$invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record)));
$this->maps['invoices'][] = $invoice->id;
$this->maps['invoices'][] = $invoice->id;
$this->performInvoiceActions($invoice, $record, $invoice_repository);
$this->performInvoiceActions($invoice, $record, $invoice_repository);
}
}
}

View File

@ -96,7 +96,6 @@ class BaseMailerJob implements ShouldQueue
public function failed($exception = null)
{
info('the job failed');
info($exception->getMessage());

View File

@ -12,7 +12,6 @@
namespace App\Jobs\Mail;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\User;
@ -76,8 +75,9 @@ class MailRouter extends BaseMailerJob implements ShouldQueue
} catch (\Exception $e) {
$this->failed($e);
if($this->to_user instanceof ClientContact)
if ($this->to_user instanceof ClientContact) {
$this->logMailError($e->getMessage(), $this->to_user->client);
}
}
}
}

View File

@ -298,6 +298,7 @@ class Design extends BaseDesign
public function buildTableHeader(string $type): array
{
$this->processTaxColumns($type);
$this->processCustomColumns($type);
$elements = [];

View File

@ -266,4 +266,23 @@ trait DesignHelpers
return $logs;
}
public function processCustomColumns(string $type): void
{
$custom_columns = [];
foreach ((array) $this->client->company->custom_fields as $field => $value) {
info($field);
if (\Illuminate\Support\Str::startsWith($field, $type)) {
$custom_columns[] = '$' . $type . '.' . $field;
}
}
$key = array_search(sprintf('%s%s.description', '$', $type), $this->context['pdf_variables']["{$type}_columns"], true);
if ($key) {
array_splice($this->context['pdf_variables']["{$type}_columns"], $key + 1, 0, $custom_columns);
}
}
}

View File

@ -13,10 +13,13 @@
namespace App\Utils;
use App\Models\Client;
use App\Utils\Traits\MakesDates;
use stdClass;
class Helpers
{
use MakesDates;
public static function sharedEmailVariables(?Client $client, array $settings = null): array
{
if (!$client) {
@ -35,4 +38,42 @@ class Helpers
return $elements;
}
/**
* A centralised way to format the custom fields content.
*
* @param mixed $custom_fields
* @param mixed $field
* @param mixed $value
* @param null|\App\Models\Client $client
*
* @return null|string
*/
public function formatCustomFieldValue($custom_fields, $field, $value, ?Client $client): ?string
{
$custom_field = '';
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
if (count($custom_field_parts) >= 2) {
$custom_field = $custom_field_parts[1];
}
}
switch ($custom_field) {
case 'date':
return $this->formatDate($value, $client->date_format());
break;
case 'switch':
return trim($value) == 'yes' ? ctrans('texts.yes') : ctrans('texts.no');
break;
default:
return is_null($value) ? '' : $value;
break;
}
}
}

View File

@ -307,6 +307,10 @@ class HtmlEngine
$data['$product.line_total'] = ['value' => '', 'label' => ctrans('texts.line_total')];
$data['$product.description'] = ['value' => '', 'label' => ctrans('texts.description')];
$data['$product.unit_cost'] = ['value' => '', 'label' => ctrans('texts.unit_cost')];
$data['$product.product1'] = ['value' => '', 'label' => $this->makeCustomField('product1')];
$data['$product.product2'] = ['value' => '', 'label' => $this->makeCustomField('product2')];
$data['$product.product3'] = ['value' => '', 'label' => $this->makeCustomField('product3')];
$data['$product.product4'] = ['value' => '', 'label' => $this->makeCustomField('product4')];
$data['$task.date'] = ['value' => '', 'label' => ctrans('texts.date')];
$data['$task.discount'] = ['value' => '', 'label' => ctrans('texts.discount')];

View File

@ -15,6 +15,7 @@ use App\Models\Country;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Quote;
use App\Utils\Helpers;
use App\Utils\Number;
/**
@ -591,9 +592,12 @@ trait MakesInvoiceValues
/**
* Formats the line items for display.
* @param array $items The array of invoice items
*
* @param mixed $items
* @param string $table_type
* @return array The formatted array of invoice items
* @param mixed|null $custom_fields
*
* @return array
*/
public function transformLineItems($items, $table_type = '$product') :array
{
@ -616,14 +620,24 @@ trait MakesInvoiceValues
}
}
$helpers = new Helpers();
$_table_type = ltrim($table_type, '$'); // From $product -> product.
$data[$key][$table_type.'.product_key'] = $item->product_key;
$data[$key][$table_type.'.service'] = is_null(optional($item)->service) ? $item->product_key : $item->service;
$data[$key][$table_type.'.notes'] = $item->notes;
$data[$key][$table_type.'.description'] = $item->notes;
$data[$key][$table_type.'.custom_value1'] = $item->custom_value1;
$data[$key][$table_type.'.custom_value2'] = $item->custom_value2;
$data[$key][$table_type.'.custom_value3'] = $item->custom_value3;
$data[$key][$table_type.'.custom_value4'] = $item->custom_value4;
$data[$key][$table_type . ".{$_table_type}1"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}1", $item->custom_value1, $this->client);
$data[$key][$table_type . ".{$_table_type}2"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}2", $item->custom_value2, $this->client);
;
$data[$key][$table_type . ".{$_table_type}3"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}3", $item->custom_value3, $this->client);
;
$data[$key][$table_type . ".{$_table_type}4"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}4", $item->custom_value4, $this->client);
;
$data[$key][$table_type.'.quantity'] = $item->quantity;
$data[$key][$table_type.'.unit_cost'] = Number::formatMoney($item->cost, $this->client);