diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 4e2473bd2054..eb6e5aa7c9bb 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -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. diff --git a/app/Jobs/Import/CSVImport.php b/app/Jobs/Import/CSVImport.php index f54016ac8532..391d160549ab 100644 --- a/app/Jobs/Import/CSVImport.php +++ b/app/Jobs/Import/CSVImport.php @@ -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); + } } } diff --git a/app/Jobs/Mail/BaseMailerJob.php b/app/Jobs/Mail/BaseMailerJob.php index c337e939ce90..ae3fb9711c73 100644 --- a/app/Jobs/Mail/BaseMailerJob.php +++ b/app/Jobs/Mail/BaseMailerJob.php @@ -96,7 +96,6 @@ class BaseMailerJob implements ShouldQueue public function failed($exception = null) { - info('the job failed'); info($exception->getMessage()); diff --git a/app/Jobs/Mail/MailRouter.php b/app/Jobs/Mail/MailRouter.php index dabb8a456f28..14510e0d8b82 100644 --- a/app/Jobs/Mail/MailRouter.php +++ b/app/Jobs/Mail/MailRouter.php @@ -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); + } } } } diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index 14c872a34447..5cdd222c6cef 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -298,6 +298,7 @@ class Design extends BaseDesign public function buildTableHeader(string $type): array { $this->processTaxColumns($type); + $this->processCustomColumns($type); $elements = []; diff --git a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php index d80c6c4b1a4d..2ce308e25ccc 100644 --- a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php +++ b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php @@ -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); + } + } } diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index 5245d109c890..889d75e29f3f 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/HtmlEngine.php b/app/Utils/HtmlEngine.php index 6dd82e753438..5d733fbe9013 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -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')]; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index a10a4783c228..5ad73c662210 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 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);