From a907933d5736696db07e18ca69a4c0efce167cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 23 Dec 2020 11:17:14 +0100 Subject: [PATCH] transformlineitems -> switch --- app/Utils/Helpers.php | 41 +++++++++++++++++++++++++ app/Utils/Traits/MakesInvoiceValues.php | 28 +++++++++-------- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index 5245d109c890..5b026787a44e 100644 --- a/app/Utils/Helpers.php +++ b/app/Utils/Helpers.php @@ -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; + } + } } diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 79efc1c9e3bb..1273db1089a8 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -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;