transformlineitems -> switch

This commit is contained in:
Benjamin Beganović 2020-12-23 11:17:14 +01:00
parent 8fd560dcce
commit a907933d57
2 changed files with 57 additions and 12 deletions

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

@ -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 string $table_type
* @return array The formatted array of invoice items
*
* @param mixed $items
* @param string $table_type
* @param mixed|null $custom_fields
*
* @return array
*/
public function transformLineItems($items, $table_type = '$product') :array
{
@ -616,20 +620,20 @@ 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 . '.' . ltrim($table_type, '$') . '1'] = $item->custom_value1; // $product.product1
$data[$key][$table_type . '.' . ltrim($table_type, '$') . '2'] = $item->custom_value2;
$data[$key][$table_type . '.' . ltrim($table_type, '$') . '3'] = $item->custom_value3;
$data[$key][$table_type . '.' . ltrim($table_type, '$') . '4'] = $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;