2019-09-02 07:08:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-09-02 07:08:26 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-09-02 07:08:26 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2019-12-16 12:34:38 +01:00
|
|
|
use App\Models\Country;
|
2020-02-26 05:11:17 +01:00
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Quote;
|
2020-12-23 11:17:14 +01:00
|
|
|
use App\Utils\Helpers;
|
2019-09-02 07:08:26 +02:00
|
|
|
use App\Utils\Number;
|
2021-02-23 14:56:16 +01:00
|
|
|
use Carbon\Carbon;
|
2021-01-27 18:01:47 +01:00
|
|
|
use Illuminate\Support\Str;
|
2019-09-02 07:08:26 +02:00
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class MakesInvoiceValues.
|
2019-09-02 07:08:26 +02:00
|
|
|
*/
|
|
|
|
trait MakesInvoiceValues
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
|
|
|
* Master list of columns used
|
2020-09-06 11:38:10 +02:00
|
|
|
* for invoice tables.
|
2019-12-30 22:59:12 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2019-09-04 01:16:23 +02:00
|
|
|
private static $master_columns = [
|
|
|
|
'date',
|
|
|
|
'discount',
|
|
|
|
'product_key',
|
|
|
|
'notes',
|
|
|
|
'cost',
|
|
|
|
'quantity',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_name2',
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_name3',
|
2019-09-04 01:16:23 +02:00
|
|
|
'line_total',
|
|
|
|
'custom_label1',
|
|
|
|
'custom_label2',
|
|
|
|
'custom_label3',
|
|
|
|
'custom_label4',
|
|
|
|
];
|
|
|
|
|
2020-02-10 10:53:02 +01:00
|
|
|
private static $custom_label_fields = [
|
|
|
|
'invoice1',
|
|
|
|
'invoice2',
|
|
|
|
'invoice3',
|
|
|
|
'invoice4',
|
|
|
|
'surcharge1',
|
|
|
|
'surcharge2',
|
|
|
|
'surcharge3',
|
|
|
|
'surcharge4',
|
|
|
|
'client1',
|
|
|
|
'client2',
|
|
|
|
'client3',
|
|
|
|
'client4',
|
|
|
|
'contact1',
|
|
|
|
'contact2',
|
|
|
|
'contact3',
|
|
|
|
'contact4',
|
|
|
|
'company1',
|
|
|
|
'company2',
|
|
|
|
'company3',
|
|
|
|
'company4',
|
2019-09-04 03:45:53 +02:00
|
|
|
];
|
|
|
|
|
2020-04-11 03:41:43 +02:00
|
|
|
private function findCustomType($field)
|
|
|
|
{
|
|
|
|
$custom_fields = $this->company->custom_fields;
|
|
|
|
|
|
|
|
if ($custom_fields && property_exists($custom_fields, $field)) {
|
|
|
|
$custom_field = $custom_fields->{$field};
|
2020-09-06 11:38:10 +02:00
|
|
|
$custom_field_parts = explode('|', $custom_field);
|
2020-04-11 03:41:43 +02:00
|
|
|
|
|
|
|
return $custom_field_parts[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
public function makeLabels($contact = null) :array
|
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$values = $this->makeLabelsAndValues($contact);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($values as $key => $value) {
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key.'_label'] = $value['label'];
|
|
|
|
}
|
2020-02-06 10:35:51 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2019-09-03 01:00:52 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
|
|
|
* Transforms all placeholders
|
2020-09-06 11:38:10 +02:00
|
|
|
* to invoice values.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param null $contact
|
2019-12-30 22:59:12 +01:00
|
|
|
* @return array returns an array
|
2019-09-04 23:50:16 +02:00
|
|
|
* of keyed labels (appended with _label)
|
2019-09-03 01:00:52 +02:00
|
|
|
*/
|
2019-12-24 22:55:29 +01:00
|
|
|
public function makeValues($contact = null) :array
|
2020-03-16 11:12:10 +01:00
|
|
|
{
|
|
|
|
$data = [];
|
|
|
|
|
|
|
|
$values = $this->makeLabelsAndValues($contact);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($values as $key => $value) {
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key] = $value['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-09-03 01:00:52 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* V2 of building a table header for PDFs.
|
2020-03-16 11:12:10 +01:00
|
|
|
* @param array $columns The array (or string of column headers)
|
|
|
|
* @return string injectable HTML string
|
2019-09-03 01:00:52 +02:00
|
|
|
*/
|
2020-03-16 11:12:10 +01:00
|
|
|
public function buildTableHeader($columns) :?string
|
2019-09-03 01:00:52 +02:00
|
|
|
{
|
2020-03-16 11:12:10 +01:00
|
|
|
$data = $this->makeLabels();
|
2019-09-03 01:00:52 +02:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$table_header = '<tr>';
|
2019-09-03 01:00:52 +02:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
foreach ($columns as $key => $column) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$table_header .= '<td class="table_header_td_class">'.$column.'_label</td>';
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-09 21:07:46 +01:00
|
|
|
$table_header .= '</tr>';
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$table_header = strtr($table_header, $data); // str_replace(array_keys($data), array_values($data), $table_header);
|
2020-03-16 11:12:10 +01:00
|
|
|
|
2020-02-05 11:28:56 +01:00
|
|
|
return $table_header;
|
|
|
|
}
|
|
|
|
|
2020-03-07 07:31:26 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* V2 of building a table body for PDFs.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $default_columns
|
|
|
|
* @param $user_columns
|
|
|
|
* @param string $table_prefix
|
2020-03-16 11:12:10 +01:00
|
|
|
* @return string injectable HTML string
|
2020-03-07 07:31:26 +01:00
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
public function buildTableBody(array $default_columns, $user_columns, string $table_prefix) :?string
|
2020-02-05 11:28:56 +01:00
|
|
|
{
|
2020-03-16 11:12:10 +01:00
|
|
|
$items = $this->transformLineItems($this->line_items, $table_prefix);
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (count($items) == 0) {
|
2020-03-16 11:12:10 +01:00
|
|
|
return '';
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$data = $this->makeValues();
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$output = '';
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (strlen($user_columns) > 1) {
|
|
|
|
foreach ($items as $key => $item) {
|
2020-04-15 02:30:52 +02:00
|
|
|
// $tmp = str_replace(array_keys($data), array_values($data), $user_columns);
|
|
|
|
// $tmp = str_replace(array_keys($item), array_values($item), $tmp);
|
|
|
|
$tmp = strtr($user_columns, $data);
|
|
|
|
$tmp = strtr($tmp, $item);
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$output .= $tmp;
|
2020-02-05 11:28:56 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-03-16 11:12:10 +01:00
|
|
|
$table_row = '<tr>';
|
2020-02-05 11:28:56 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
foreach ($default_columns as $key => $column) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$table_row .= '<td class="table_header_td_class">'.$column.'</td>';
|
2020-03-16 11:12:10 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$table_row .= '</tr>';
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($items as $key => $item) {
|
2020-04-15 02:30:52 +02:00
|
|
|
// $tmp = str_replace(array_keys($item), array_values($item), $table_row);
|
|
|
|
// $tmp = str_replace(array_keys($data), array_values($data), $tmp);
|
|
|
|
$tmp = strtr($table_row, $item);
|
|
|
|
$tmp = strtr($tmp, $data);
|
2020-03-16 11:12:10 +01:00
|
|
|
|
|
|
|
$output .= $tmp;
|
|
|
|
}
|
2020-02-05 11:28:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
return $output;
|
2020-02-05 11:28:56 +01:00
|
|
|
}
|
|
|
|
|
2019-09-03 08:01:40 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Transform the column headers into translated header values.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-09-04 01:16:23 +02:00
|
|
|
* @param array $columns The column header values
|
|
|
|
* @return array The new column header variables
|
|
|
|
*/
|
|
|
|
private function transformColumnsForHeader(array $columns) :array
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
if (count($columns) == 0) {
|
2020-03-05 08:14:57 +01:00
|
|
|
return [];
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-05 08:14:57 +01:00
|
|
|
|
2019-09-04 07:10:10 +02:00
|
|
|
$pre_columns = $columns;
|
|
|
|
$columns = array_intersect($columns, self::$master_columns);
|
2019-09-04 01:16:23 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return str_replace(
|
|
|
|
[
|
2019-09-04 01:16:23 +02:00
|
|
|
'tax_name1',
|
2020-02-19 21:44:12 +01:00
|
|
|
'tax_name2',
|
2020-09-06 11:38:10 +02:00
|
|
|
'tax_name3',
|
2019-12-30 22:59:12 +01:00
|
|
|
],
|
2019-09-04 01:16:23 +02:00
|
|
|
[
|
|
|
|
'tax',
|
|
|
|
'tax',
|
2020-09-06 11:38:10 +02:00
|
|
|
'tax',
|
2019-12-30 22:59:12 +01:00
|
|
|
],
|
|
|
|
$columns
|
|
|
|
);
|
2019-09-04 01:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Transform the column headers into invoice variables.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-09-03 08:01:40 +02:00
|
|
|
* @param array $columns The column header values
|
|
|
|
* @return array The invoice variables
|
|
|
|
*/
|
2019-09-04 01:16:23 +02:00
|
|
|
private function transformColumnsForLineItems(array $columns) :array
|
2019-09-03 08:01:40 +02:00
|
|
|
{
|
2019-09-04 01:16:23 +02:00
|
|
|
/* Removes any invalid columns the user has entered. */
|
2019-09-04 07:10:10 +02:00
|
|
|
$columns = array_intersect($columns, self::$master_columns);
|
2019-09-04 01:16:23 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
return str_replace(
|
|
|
|
[
|
|
|
|
'custom_invoice_label1',
|
|
|
|
'custom_invoice_label2',
|
|
|
|
'custom_invoice_label3',
|
|
|
|
'custom_invoice_label4',
|
|
|
|
'tax_name1',
|
2020-02-19 21:44:12 +01:00
|
|
|
'tax_name2',
|
2020-09-06 11:38:10 +02:00
|
|
|
'tax_name3',
|
2019-12-30 22:59:12 +01:00
|
|
|
],
|
|
|
|
[
|
2019-09-04 00:16:53 +02:00
|
|
|
'custom_invoice_value1',
|
|
|
|
'custom_invoice_value2',
|
|
|
|
'custom_invoice_value3',
|
|
|
|
'custom_invoice_value4',
|
|
|
|
'tax_rate1',
|
2020-02-19 21:44:12 +01:00
|
|
|
'tax_rate2',
|
2020-09-06 11:38:10 +02:00
|
|
|
'tax_rate3',
|
2019-12-30 22:59:12 +01:00
|
|
|
],
|
|
|
|
$columns
|
|
|
|
);
|
2019-09-03 08:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Formats the line items for display.
|
2020-12-23 11:41:57 +01:00
|
|
|
*
|
|
|
|
* @param mixed $items
|
|
|
|
* @param string $table_type
|
|
|
|
* @param mixed|null $custom_fields
|
|
|
|
*
|
|
|
|
* @return array
|
2019-09-03 08:01:40 +02:00
|
|
|
*/
|
2020-07-30 17:47:40 +02:00
|
|
|
public function transformLineItems($items, $table_type = '$product') :array
|
2019-09-03 08:01:40 +02:00
|
|
|
{
|
2020-03-16 11:12:10 +01:00
|
|
|
$data = [];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
if (! is_array($items)) {
|
2020-03-16 11:12:10 +01:00
|
|
|
$data;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-17 10:37:44 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
foreach ($items as $key => $item) {
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($table_type == '$product' && $item->type_id != 1) {
|
2021-04-05 14:21:49 +02:00
|
|
|
if ($item->type_id != 4 && $item->type_id != 6) {
|
2020-09-11 09:23:16 +02:00
|
|
|
continue;
|
2020-10-28 11:10:49 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-16 11:12:10 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($table_type == '$task' && $item->type_id != 2) {
|
2020-09-11 09:23:16 +02:00
|
|
|
if ($item->type_id != 4) {
|
|
|
|
continue;
|
2020-10-28 11:10:49 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-03-16 11:12:10 +01:00
|
|
|
|
2020-12-23 11:17:14 +01:00
|
|
|
$helpers = new Helpers();
|
|
|
|
$_table_type = ltrim($table_type, '$'); // From $product -> product.
|
|
|
|
|
|
|
|
|
2020-12-23 15:51:29 +01:00
|
|
|
$data[$key][$table_type.'.product_key'] = is_null(optional($item)->product_key) ? $item->item : $item->product_key;
|
2020-12-23 14:25:12 +01:00
|
|
|
$data[$key][$table_type.'.item'] = is_null(optional($item)->item) ? $item->product_key : $item->item;
|
2020-12-08 16:10:29 +01:00
|
|
|
$data[$key][$table_type.'.service'] = is_null(optional($item)->service) ? $item->product_key : $item->service;
|
2021-01-27 18:01:47 +01:00
|
|
|
|
|
|
|
$data[$key][$table_type.'.notes'] = $this->processReservedKeywords($item->notes);
|
|
|
|
$data[$key][$table_type.'.description'] = $this->processReservedKeywords($item->notes);
|
2020-12-23 11:17:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
$data[$key][$table_type . ".{$_table_type}1"] = $helpers->formatCustomFieldValue($this->client->company->custom_fields, "{$_table_type}1", $item->custom_value1, $this->client);
|
2020-12-23 11:41:57 +01:00
|
|
|
$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);
|
2020-12-22 13:55:29 +01:00
|
|
|
|
2021-02-26 07:05:44 +01:00
|
|
|
$data[$key][$table_type.'.quantity'] = Number::formatValue($item->quantity, $this->client->currency());
|
2020-03-16 11:12:10 +01:00
|
|
|
|
2020-11-15 09:24:57 +01:00
|
|
|
$data[$key][$table_type.'.unit_cost'] = Number::formatMoney($item->cost, $this->client);
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.cost'] = Number::formatMoney($item->cost, $this->client);
|
2020-11-16 02:20:44 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.line_total'] = Number::formatMoney($item->line_total, $this->client);
|
2019-09-04 00:16:53 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($item->discount) && $item->discount > 0) {
|
|
|
|
if ($item->is_amount_discount) {
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.discount'] = Number::formatMoney($item->discount, $this->client);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2021-01-06 23:43:08 +01:00
|
|
|
$data[$key][$table_type.'.discount'] = floatval($item->discount).'%';
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
} else {
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.discount'] = '';
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2021-05-06 12:46:02 +02:00
|
|
|
// Previously we used to check for tax_rate value,
|
|
|
|
// but that's no longer necessary.
|
|
|
|
|
|
|
|
if (isset($item->tax_rate1)) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data[$key][$table_type.'.tax_rate1'] = round($item->tax_rate1, 2).'%';
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.tax1'] = &$data[$key][$table_type.'.tax_rate1'];
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2021-05-06 12:46:02 +02:00
|
|
|
if (isset($item->tax_rate2)) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data[$key][$table_type.'.tax_rate2'] = round($item->tax_rate2, 2).'%';
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.tax2'] = &$data[$key][$table_type.'.tax_rate2'];
|
|
|
|
}
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2021-05-06 12:46:02 +02:00
|
|
|
if (isset($item->tax_rate3)) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data[$key][$table_type.'.tax_rate3'] = round($item->tax_rate3, 2).'%';
|
2020-03-16 11:12:10 +01:00
|
|
|
$data[$key][$table_type.'.tax3'] = &$data[$key][$table_type.'.tax_rate3'];
|
|
|
|
}
|
2020-02-10 10:53:02 +01:00
|
|
|
|
2020-11-06 13:13:13 +01:00
|
|
|
$data[$key]['task_id'] = optional($item)->task_id;
|
2019-09-04 00:16:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
return $data;
|
2019-09-03 08:01:40 +02:00
|
|
|
}
|
2019-09-04 14:01:19 +02:00
|
|
|
|
2021-01-27 18:01:47 +01:00
|
|
|
/**
|
|
|
|
* Process reserved words like :MONTH :YEAR :QUARTER
|
|
|
|
* as well as their operations.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2021-02-25 12:00:01 +01:00
|
|
|
private function processReservedKeywords(string $value): ?string
|
2021-01-27 18:01:47 +01:00
|
|
|
{
|
2021-02-25 12:00:01 +01:00
|
|
|
Carbon::setLocale($this->client->locale());
|
|
|
|
|
2021-01-27 18:01:47 +01:00
|
|
|
$replacements = [
|
|
|
|
'literal' => [
|
2021-03-30 14:44:33 +02:00
|
|
|
':MONTH' => Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F'),
|
2021-01-27 18:01:47 +01:00
|
|
|
':YEAR' => now()->year,
|
|
|
|
':QUARTER' => 'Q' . now()->quarter,
|
|
|
|
],
|
|
|
|
'raw' => [
|
|
|
|
':MONTH' => now()->month,
|
|
|
|
':YEAR' => now()->year,
|
|
|
|
':QUARTER' => now()->quarter,
|
|
|
|
],
|
2021-02-23 14:56:16 +01:00
|
|
|
'ranges' => [
|
|
|
|
'MONTHYEAR' => Carbon::createFromDate(now()->year, now()->month),
|
|
|
|
],
|
|
|
|
'ranges_raw' => [
|
|
|
|
'MONTH' => now()->month,
|
|
|
|
'YEAR' => now()->year,
|
|
|
|
],
|
2021-01-27 18:01:47 +01:00
|
|
|
];
|
|
|
|
|
2021-02-22 17:01:01 +01:00
|
|
|
// First case, with ranges.
|
|
|
|
preg_match_all('/\[(.*?)]/', $value, $ranges);
|
|
|
|
|
|
|
|
$matches = array_shift($ranges);
|
|
|
|
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
if (!Str::contains($match, '|')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Str::contains($match, '|')) {
|
2021-02-23 14:56:16 +01:00
|
|
|
$parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ]
|
|
|
|
|
|
|
|
$left = substr($parts[0], 1); // 'MONTH'
|
|
|
|
$right = substr($parts[1], 0, -1); // MONTH+2
|
|
|
|
|
|
|
|
// If left side is not part of replacements, skip.
|
|
|
|
if (!array_key_exists($left, $replacements['ranges'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-22 17:01:01 +01:00
|
|
|
|
2021-02-25 12:00:01 +01:00
|
|
|
$_left = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
2021-02-23 14:56:16 +01:00
|
|
|
$_right = '';
|
|
|
|
|
|
|
|
// If right side doesn't have any calculations, replace with raw ranges keyword.
|
|
|
|
if (!Str::contains($right, ['-', '+', '/', '*'])) {
|
2021-02-25 12:00:01 +01:00
|
|
|
$_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y');
|
2021-02-23 14:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If right side contains one of math operations, calculate.
|
|
|
|
if (Str::contains($right, ['+'])) {
|
|
|
|
$operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $right, $_matches);
|
|
|
|
|
|
|
|
$_operation = array_shift($_matches)[0]; // + -
|
|
|
|
|
|
|
|
$_value = explode($_operation, $right); // [MONTHYEAR, 4]
|
|
|
|
|
2021-02-25 12:00:01 +01:00
|
|
|
$_right = Carbon::createFromDate(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y');
|
2021-02-23 14:56:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$replacement = sprintf('%s to %s', $_left, $_right);
|
2021-02-22 17:01:01 +01:00
|
|
|
|
|
|
|
$value = preg_replace(
|
|
|
|
sprintf('/%s/', preg_quote($match)), $replacement, $value, 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 14:56:16 +01:00
|
|
|
|
2021-02-22 17:01:01 +01:00
|
|
|
// Second case with more common calculations.
|
|
|
|
preg_match_all('/:([^:\s]+)/', $value, $common);
|
2021-01-27 18:01:47 +01:00
|
|
|
|
2021-02-22 17:01:01 +01:00
|
|
|
$matches = array_shift($common);
|
2021-01-27 18:01:47 +01:00
|
|
|
|
|
|
|
foreach ($matches as $match) {
|
2021-01-28 15:18:18 +01:00
|
|
|
$matches = collect($replacements['literal'])->filter(function ($value, $key) use ($match) {
|
|
|
|
return Str::startsWith($match, $key);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($matches->count() === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-27 18:01:47 +01:00
|
|
|
if (!Str::contains($match, ['-', '+', '/', '*'])) {
|
2021-01-28 15:18:18 +01:00
|
|
|
$value = preg_replace(
|
|
|
|
sprintf('/%s/', $matches->keys()->first()), $replacements['literal'][$matches->keys()->first()], $value, 1
|
|
|
|
);
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Str::contains($match, ['-', '+', '/', '*'])) {
|
|
|
|
$operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $match, $_matches);
|
2021-01-28 15:18:18 +01:00
|
|
|
|
2021-01-27 18:01:47 +01:00
|
|
|
$_operation = array_shift($_matches)[0];
|
|
|
|
|
|
|
|
$_value = explode($_operation, $match); // [:MONTH, 4]
|
|
|
|
|
2021-01-28 15:18:18 +01:00
|
|
|
$raw = strtr($matches->keys()->first(), $replacements['raw']); // :MONTH => 1
|
|
|
|
|
|
|
|
$number = $res = preg_replace("/[^0-9]/", '', $_value[1]); // :MONTH+1. || :MONTH+2! => 1 || 2
|
2021-01-27 18:01:47 +01:00
|
|
|
|
2021-01-28 15:18:18 +01:00
|
|
|
$target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1
|
|
|
|
|
|
|
|
$output = (int) $raw + (int)$_value[1];
|
|
|
|
|
|
|
|
if ($operation == '+') {
|
|
|
|
$output = (int) $raw + (int)$_value[1]; // 1 (:MONTH) + 4
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($_operation == '-') {
|
2021-01-28 15:18:18 +01:00
|
|
|
$output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($_operation == '/') {
|
2021-01-28 15:18:18 +01:00
|
|
|
$output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($_operation == '*') {
|
2021-01-28 15:18:18 +01:00
|
|
|
$output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
|
2021-01-28 15:18:18 +01:00
|
|
|
if ($matches->keys()->first() == ':MONTH') {
|
|
|
|
$output = \Carbon\Carbon::create()->month($output)->localeMonth;
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
2021-01-28 15:18:18 +01:00
|
|
|
|
|
|
|
$value = preg_replace(
|
|
|
|
$target, $output, $value, 1
|
|
|
|
);
|
2021-01-27 18:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2019-09-04 14:01:19 +02:00
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* Due to the way we are compiling the blade template we
|
2019-09-04 14:01:19 +02:00
|
|
|
* have no ability to iterate, so in the case
|
2019-12-30 22:59:12 +01:00
|
|
|
* of line taxes where there are multiple rows,
|
2020-09-06 11:38:10 +02:00
|
|
|
* we use this function to format a section of rows.
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
|
|
|
* @return string a collection of <tr> rows with line item
|
2019-09-04 14:01:19 +02:00
|
|
|
* aggregate data
|
|
|
|
*/
|
|
|
|
private function makeLineTaxes() :string
|
|
|
|
{
|
|
|
|
$tax_map = $this->calc()->getTaxMap();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-09-04 14:01:19 +02:00
|
|
|
$data = '';
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($tax_map as $tax) {
|
2019-09-04 14:01:19 +02:00
|
|
|
$data .= '<tr class="line_taxes">';
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<td>'.$tax['name'].'</td>';
|
|
|
|
$data .= '<td>'.Number::formatMoney($tax['total'], $this->client).'</td></tr>';
|
2019-09-04 14:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2019-10-13 12:59:55 +02:00
|
|
|
|
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* @return string a collectino of <tr> with
|
2019-10-13 12:59:55 +02:00
|
|
|
* itemised total tax data
|
|
|
|
*/
|
|
|
|
private function makeTotalTaxes() :string
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->calc()->getTotalTaxMap()) {
|
2019-10-16 11:28:52 +02:00
|
|
|
return $data;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-10-16 11:28:52 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
foreach ($this->calc()->getTotalTaxMap() as $tax) {
|
2019-10-13 12:59:55 +02:00
|
|
|
$data .= '<tr class="total_taxes">';
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<td>'.$tax['name'].'</td>';
|
|
|
|
$data .= '<td>'.Number::formatMoney($tax['total'], $this->client).'</td></tr>';
|
2019-10-13 12:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2020-02-06 10:35:51 +01:00
|
|
|
|
|
|
|
private function totalTaxLabels() :string
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->calc()->getTotalTaxMap()) {
|
2020-02-06 10:35:51 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->calc()->getTotalTaxMap() as $tax) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<span>'.$tax['name'].'</span>';
|
2020-02-06 10:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function totalTaxValues() :string
|
|
|
|
{
|
|
|
|
$data = '';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->calc()->getTotalTaxMap()) {
|
2020-02-06 10:35:51 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->calc()->getTotalTaxMap() as $tax) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<span>'.Number::formatMoney($tax['total'], $this->client).'</span>';
|
2020-02-06 10:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function lineTaxLabels() :string
|
|
|
|
{
|
|
|
|
$tax_map = $this->calc()->getTaxMap();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-06 10:35:51 +01:00
|
|
|
$data = '';
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($tax_map as $tax) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<span>'.$tax['name'].'</span>';
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-06 10:35:51 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function lineTaxValues() :string
|
|
|
|
{
|
|
|
|
$tax_map = $this->calc()->getTaxMap();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-06 10:35:51 +01:00
|
|
|
$data = '';
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($tax_map as $tax) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$data .= '<span>'.Number::formatMoney($tax['total'], $this->client).'</span>';
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-02-06 10:35:51 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2020-03-16 11:12:10 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Ensures the URL doesn't have duplicated trailing slash
|
|
|
|
*/
|
|
|
|
public function generateAppUrl()
|
|
|
|
{
|
2020-07-14 11:55:28 +02:00
|
|
|
//return rtrim(config('ninja.app_url'), "/");
|
|
|
|
return config('ninja.app_url');
|
2020-03-16 11:12:10 +01:00
|
|
|
}
|
2020-04-12 13:51:27 +02:00
|
|
|
|
2020-04-13 03:49:58 +02:00
|
|
|
/**
|
|
|
|
* Builds CSS to assist with the generation
|
2020-09-06 11:38:10 +02:00
|
|
|
* of Repeating headers and footers on the PDF.
|
2020-04-13 03:49:58 +02:00
|
|
|
* @return string The css string
|
|
|
|
*/
|
|
|
|
public function generateCustomCSS() :string
|
2020-04-12 13:51:27 +02:00
|
|
|
{
|
|
|
|
$settings = $this->client->getMergedSettings();
|
|
|
|
|
|
|
|
$header_and_footer = '
|
2020-04-13 02:24:02 +02:00
|
|
|
.header, .header-space {
|
|
|
|
height: 160px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.footer, .footer-space {
|
|
|
|
height: 160px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.footer {
|
|
|
|
position: fixed;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
.header {
|
|
|
|
position: fixed;
|
|
|
|
top: 0mm;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:27 +02:00
|
|
|
@media print {
|
2020-10-28 11:10:49 +01:00
|
|
|
thead {display: table-header-group;}
|
2020-04-12 13:51:27 +02:00
|
|
|
tfoot {display: table-footer-group;}
|
|
|
|
button {display: none;}
|
|
|
|
body {margin: 0;}
|
2020-04-15 02:30:52 +02:00
|
|
|
}';
|
2020-04-12 13:51:27 +02:00
|
|
|
|
|
|
|
$header = '
|
2020-04-13 02:24:02 +02:00
|
|
|
.header, .header-space {
|
|
|
|
height: 160px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.header {
|
|
|
|
position: fixed;
|
|
|
|
top: 0mm;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:27 +02:00
|
|
|
@media print {
|
2020-10-28 11:10:49 +01:00
|
|
|
thead {display: table-header-group;}
|
2020-04-12 13:51:27 +02:00
|
|
|
button {display: none;}
|
|
|
|
body {margin: 0;}
|
2020-04-15 02:30:52 +02:00
|
|
|
}';
|
2020-04-12 13:51:27 +02:00
|
|
|
|
|
|
|
$footer = '
|
2020-04-13 02:24:02 +02:00
|
|
|
|
|
|
|
.footer, .footer-space {
|
|
|
|
height: 160px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.footer {
|
|
|
|
position: fixed;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:27 +02:00
|
|
|
@media print {
|
|
|
|
tfoot {display: table-footer-group;}
|
|
|
|
button {display: none;}
|
|
|
|
body {margin: 0;}
|
2020-04-15 02:30:52 +02:00
|
|
|
}';
|
2020-04-12 13:51:27 +02:00
|
|
|
$css = '';
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
if ($settings->all_pages_header && $settings->all_pages_footer) {
|
2020-04-12 13:51:27 +02:00
|
|
|
$css .= $header_and_footer;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($settings->all_pages_header && ! $settings->all_pages_footer) {
|
2020-04-12 13:51:27 +02:00
|
|
|
$css .= $header;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (! $settings->all_pages_header && $settings->all_pages_footer) {
|
2020-04-12 13:51:27 +02:00
|
|
|
$css .= $footer;
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
2020-04-12 13:51:27 +02:00
|
|
|
|
|
|
|
$css .= '
|
|
|
|
.page {
|
|
|
|
page-break-after: always;
|
|
|
|
}
|
|
|
|
|
|
|
|
@page {
|
|
|
|
margin: 0mm
|
|
|
|
}
|
|
|
|
|
|
|
|
html {
|
|
|
|
';
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$css .= 'font-size:'.$settings->font_size.'px;';
|
2020-04-13 03:49:58 +02:00
|
|
|
// $css .= 'font-size:14px;';
|
2020-04-12 13:51:27 +02:00
|
|
|
|
|
|
|
$css .= '}';
|
|
|
|
|
|
|
|
return $css;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|