1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Utils/Traits/MakesInvoiceValues.php

588 lines
16 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils\Traits;
use App\Models\Invoice;
2020-12-23 11:17:14 +01:00
use App\Utils\Helpers;
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;
/**
* Class MakesInvoiceValues.
*/
trait MakesInvoiceValues
{
/**
* Master list of columns used
* for invoice tables.
* @var array
*/
private static $master_columns = [
'date',
'discount',
'product_key',
'notes',
'cost',
'quantity',
'tax_name1',
'tax_name2',
'tax_name3',
'line_total',
'custom_label1',
'custom_label2',
'custom_label3',
'custom_label4',
];
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
];
private function findCustomType($field)
{
$custom_fields = $this->company->custom_fields;
if ($custom_fields && property_exists($custom_fields, $field)) {
$custom_field = $custom_fields->{$field};
$custom_field_parts = explode('|', $custom_field);
return $custom_field_parts[1];
}
return '';
}
2024-01-14 05:05:00 +01:00
public function makeLabels($contact = null): array
{
$data = [];
$values = $this->makeLabelsAndValues($contact);
foreach ($values as $key => $value) {
$data[$key.'_label'] = $value['label'];
}
return $data;
}
/**
* Transforms all placeholders
* to invoice values.
*
2020-10-28 11:10:49 +01:00
* @param null $contact
* @return array returns an array
2019-09-04 23:50:16 +02:00
* of keyed labels (appended with _label)
*/
2024-01-14 05:05:00 +01:00
public function makeValues($contact = null): array
{
$data = [];
$values = $this->makeLabelsAndValues($contact);
foreach ($values as $key => $value) {
$data[$key] = $value['value'];
}
return $data;
}
/**
* V2 of building a table header for PDFs.
* @param array $columns The array (or string of column headers)
* @return string injectable HTML string
*/
2024-01-14 05:05:00 +01:00
public function buildTableHeader($columns): ?string
{
$data = $this->makeLabels();
$table_header = '<tr>';
foreach ($columns as $key => $column) {
$table_header .= '<td class="table_header_td_class">'.$column.'_label</td>';
}
$table_header .= '</tr>';
$table_header = strtr($table_header, $data); // str_replace(array_keys($data), array_values($data), $table_header);
return $table_header;
}
/**
* 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
* @return string injectable HTML string
*/
2024-01-14 05:05:00 +01:00
public function buildTableBody(array $default_columns, $user_columns, string $table_prefix): ?string
{
$items = $this->transformLineItems($this->line_items, $table_prefix);
if (count($items) == 0) {
return '';
}
$data = $this->makeValues();
$output = '';
if (strlen($user_columns) > 1) {
foreach ($items as $key => $item) {
$tmp = strtr($user_columns, $data);
$tmp = strtr($tmp, $item);
$output .= $tmp;
}
} else {
$table_row = '<tr>';
foreach ($default_columns as $key => $column) {
$table_row .= '<td class="table_header_td_class">'.$column.'</td>';
}
$table_row .= '</tr>';
foreach ($items as $key => $item) {
$tmp = strtr($table_row, $item);
$tmp = strtr($tmp, $data);
$output .= $tmp;
}
}
return $output;
}
2019-09-03 08:01:40 +02:00
/**
* Transform the column headers into translated header values.
*
* @param array $columns The column header values
* @return array The new column header variables
*/
2024-01-14 05:05:00 +01:00
private function transformColumnsForHeader(array $columns): array
{
if (count($columns) == 0) {
return [];
}
2019-09-04 07:10:10 +02:00
$pre_columns = $columns;
$columns = array_intersect($columns, self::$master_columns);
return str_replace(
[
'tax_name1',
'tax_name2',
'tax_name3',
2022-11-10 11:57:55 +01:00
'tax_amount',
],
[
'tax',
'tax',
'tax',
2022-11-10 09:16:22 +01:00
'tax',
],
$columns
);
}
/**
* Transform the column headers into invoice variables.
*
2019-09-03 08:01:40 +02:00
* @param array $columns The column header values
* @return array The invoice variables
*/
2024-01-14 05:05:00 +01:00
private function transformColumnsForLineItems(array $columns): array
2019-09-03 08:01:40 +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);
return str_replace(
[
'custom_invoice_label1',
'custom_invoice_label2',
'custom_invoice_label3',
'custom_invoice_label4',
'tax_name1',
'tax_name2',
'tax_name3',
],
[
2019-09-04 00:16:53 +02:00
'custom_invoice_value1',
'custom_invoice_value2',
'custom_invoice_value3',
'custom_invoice_value4',
'tax_rate1',
'tax_rate2',
'tax_rate3',
],
$columns
);
2019-09-03 08:01:40 +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
*/
2024-01-14 05:05:00 +01:00
public function transformLineItems($items, $table_type = '$product'): array
{ //$start = microtime(true);
$entity = $this->client ? $this->client : $this->vendor;
2022-06-10 09:04:16 +02:00
$data = [];
if (! is_array($items)) {
}
$locale_info = localeconv();
$entity_currency = $entity->currency();
foreach ($items as $key => $item) {
if ($table_type == '$product' && $item->type_id != 1) {
2021-07-21 15:07:01 +02:00
if ($item->type_id != 4 && $item->type_id != 6 && $item->type_id != 5) {
2020-09-11 09:23:16 +02:00
continue;
2020-10-28 11:10:49 +01:00
}
}
if ($table_type == '$task' && $item->type_id != 2) {
// if ($item->type_id != 4 && $item->type_id != 5) {
2023-02-16 02:36:09 +01:00
continue;
// }
}
2020-12-23 11:17:14 +01:00
$helpers = new Helpers();
$_table_type = ltrim($table_type, '$'); // From $product -> product.
2022-06-23 01:24:56 +02:00
$data[$key][$table_type.'.product_key'] = is_null(optional($item)->product_key) ? $item->item : $item->product_key;
$data[$key][$table_type.'.item'] = is_null(optional($item)->item) ? $item->product_key : $item->item;
$data[$key][$table_type.'.service'] = is_null(optional($item)->service) ? $item->product_key : $item->service;
2021-01-27 18:01:47 +01:00
$currentDateTime = null;
if (isset($this->entity->next_send_date)) {
$currentDateTime = Carbon::parse($this->entity->next_send_date);
}
$data[$key][$table_type.'.notes'] = Helpers::processReservedKeywords($item->notes, $entity, $currentDateTime);
$data[$key][$table_type.'.description'] = Helpers::processReservedKeywords($item->notes, $entity, $currentDateTime);
2022-04-26 08:53:41 +02:00
2022-10-12 02:27:14 +02:00
$data[$key][$table_type.".{$_table_type}1"] = strlen($item->custom_value1) >= 1 ? $helpers->formatCustomFieldValue($this->company->custom_fields, "{$_table_type}1", $item->custom_value1, $entity) : '';
$data[$key][$table_type.".{$_table_type}2"] = strlen($item->custom_value2) >= 1 ? $helpers->formatCustomFieldValue($this->company->custom_fields, "{$_table_type}2", $item->custom_value2, $entity) : '';
$data[$key][$table_type.".{$_table_type}3"] = strlen($item->custom_value3) >= 1 ? $helpers->formatCustomFieldValue($this->company->custom_fields, "{$_table_type}3", $item->custom_value3, $entity) : '';
$data[$key][$table_type.".{$_table_type}4"] = strlen($item->custom_value4) >= 1 ? $helpers->formatCustomFieldValue($this->company->custom_fields, "{$_table_type}4", $item->custom_value4, $entity) : '';
if ($item->quantity > 0 || $item->cost > 0) {
$data[$key][$table_type.'.quantity'] = Number::formatValueNoTrailingZeroes($item->quantity, $entity);
2022-06-10 09:04:16 +02:00
$data[$key][$table_type.'.unit_cost'] = Number::formatMoneyNoRounding($item->cost, $entity);
2022-06-10 09:04:16 +02:00
$data[$key][$table_type.'.cost'] = Number::formatMoney($item->cost, $entity);
$data[$key][$table_type.'.line_total'] = Number::formatMoney($item->line_total, $entity);
} else {
2022-04-26 08:53:41 +02:00
$data[$key][$table_type.'.quantity'] = '';
2022-04-26 08:53:41 +02:00
$data[$key][$table_type.'.unit_cost'] = '';
$data[$key][$table_type.'.cost'] = '';
$data[$key][$table_type.'.line_total'] = '';
2022-04-26 08:53:41 +02:00
}
2019-09-04 00:16:53 +02:00
if (property_exists($item, 'gross_line_total')) {
$data[$key][$table_type.'.gross_line_total'] = ($item->gross_line_total == 0) ? '' : Number::formatMoney($item->gross_line_total, $entity);
} else {
$data[$key][$table_type.'.gross_line_total'] = '';
}
2022-11-10 11:57:55 +01:00
if (property_exists($item, 'tax_amount')) {
$data[$key][$table_type.'.tax_amount'] = ($item->tax_amount == 0) ? '' : Number::formatMoney($item->tax_amount, $entity);
2022-11-10 09:16:22 +01:00
} else {
2022-11-10 11:57:55 +01:00
$data[$key][$table_type.'.tax_amount'] = '';
2022-11-10 09:16:22 +01:00
}
if (isset($item->discount) && $item->discount > 0) {
if ($item->is_amount_discount) {
2022-06-10 09:04:16 +02:00
$data[$key][$table_type.'.discount'] = Number::formatMoney($item->discount, $entity);
} else {
$data[$key][$table_type.'.discount'] = floatval($item->discount).'%';
}
} else {
$data[$key][$table_type.'.discount'] = '';
}
// Previously we used to check for tax_rate value,
// but that's no longer necessary.
if (isset($item->tax_rate1)) {
$data[$key][$table_type.'.tax_rate1'] = floatval($item->tax_rate1).'%';
$data[$key][$table_type.'.tax1'] = &$data[$key][$table_type.'.tax_rate1'];
}
if (isset($item->tax_rate2)) {
$data[$key][$table_type.'.tax_rate2'] = floatval($item->tax_rate2).'%';
$data[$key][$table_type.'.tax2'] = &$data[$key][$table_type.'.tax_rate2'];
}
if (isset($item->tax_rate3)) {
$data[$key][$table_type.'.tax_rate3'] = floatval($item->tax_rate3).'%';
$data[$key][$table_type.'.tax3'] = &$data[$key][$table_type.'.tax_rate3'];
}
2022-06-24 05:15:56 +02:00
$data[$key]['task_id'] = property_exists($item, 'task_id') ? $item->task_id : '';
2019-09-04 00:16:53 +02:00
}
//nlog(microtime(true) - $start);
2024-01-14 05:05:00 +01:00
return $data;
2019-09-03 08:01:40 +02:00
}
2019-09-04 14:01:19 +02: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
* of line taxes where there are multiple rows,
* we use this function to format a section of rows.
*
* @return string a collection of <tr> rows with line item
2019-09-04 14:01:19 +02:00
* aggregate data
*/
2024-01-14 05:05:00 +01:00
private function makeLineTaxes(): string
2019-09-04 14:01:19 +02:00
{
$tax_map = $this->calc()->getTaxMap();
$entity = $this->client ? $this->client : $this->vendor;
2019-09-04 14:01:19 +02:00
$data = '';
foreach ($tax_map as $tax) {
2019-09-04 14:01:19 +02:00
$data .= '<tr class="line_taxes">';
$data .= '<td>'.$tax['name'].'</td>';
2022-06-10 09:04:16 +02:00
$data .= '<td>'.Number::formatMoney($tax['total'], $entity).'</td></tr>';
2019-09-04 14:01:19 +02:00
}
return $data;
}
/**
* @return string a collectino of <tr> with
* itemised total tax data
*/
2024-01-14 05:05:00 +01:00
private function makeTotalTaxes(): string
{
$data = '';
$entity = $this->client ? $this->client : $this->vendor;
if (! $this->calc()->getTotalTaxMap()) {
return $data;
}
foreach ($this->calc()->getTotalTaxMap() as $tax) {
$data .= '<tr class="total_taxes">';
$data .= '<td>'.$tax['name'].'</td>';
2022-06-10 09:04:16 +02:00
$data .= '<td>'.Number::formatMoney($tax['total'], $entity).'</td></tr>';
}
return $data;
}
2024-01-14 05:05:00 +01:00
private function totalTaxLabels(): string
{
$data = '';
if (! $this->calc()->getTotalTaxMap()) {
return $data;
}
foreach ($this->calc()->getTotalTaxMap() as $tax) {
$data .= '<span>'.$tax['name'].'</span>';
}
return $data;
}
2024-01-14 05:05:00 +01:00
private function totalTaxValues(): string
{
$data = '';
$entity = $this->client ? $this->client : $this->vendor;
if (! $this->calc()->getTotalTaxMap()) {
return $data;
}
foreach ($this->calc()->getTotalTaxMap() as $tax) {
2022-06-10 09:04:16 +02:00
$data .= '<span>'.Number::formatMoney($tax['total'], $entity).'</span>';
}
return $data;
}
2024-01-14 05:05:00 +01:00
private function lineTaxLabels(): string
{
$tax_map = $this->calc()->getTaxMap();
$data = '';
foreach ($tax_map as $tax) {
$data .= '<span>'.$tax['name'].'</span>';
}
return $data;
}
2024-01-14 05:05:00 +01:00
private function lineTaxValues(): string
{
$tax_map = $this->calc()->getTaxMap();
$entity = $this->client ? $this->client : $this->vendor;
$data = '';
foreach ($tax_map as $tax) {
2022-06-10 09:04:16 +02:00
$data .= '<span>'.Number::formatMoney($tax['total'], $entity).'</span>';
}
return $data;
}
/*
| 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-04-13 03:49:58 +02:00
/**
* Builds CSS to assist with the generation
* of Repeating headers and footers on the PDF.
2020-04-13 03:49:58 +02:00
* @return string The css string
*/
2024-01-14 05:05:00 +01:00
public function generateCustomCSS(): string
{
$settings = $this->client ? $this->client->getMergedSettings() : $this->company->settings;
$header_and_footer = '
.header, .header-space {
height: 160px;
}
.footer, .footer-space {
height: 160px;
}
.footer {
position: fixed;
bottom: 0;
width: 100%;
}
.header {
position: fixed;
top: 0mm;
width: 100%;
}
@media print {
2020-10-28 11:10:49 +01:00
thead {display: table-header-group;}
tfoot {display: table-footer-group;}
button {display: none;}
body {margin: 0;}
}';
$header = '
.header, .header-space {
height: 160px;
}
.header {
position: fixed;
top: 0mm;
width: 100%;
}
@media print {
2020-10-28 11:10:49 +01:00
thead {display: table-header-group;}
button {display: none;}
body {margin: 0;}
}';
$footer = '
.footer, .footer-space {
height: 160px;
}
.footer {
position: fixed;
bottom: 0;
width: 100%;
}
@media print {
tfoot {display: table-footer-group;}
button {display: none;}
body {margin: 0;}
}';
$css = '';
if ($settings->all_pages_header && $settings->all_pages_footer) {
$css .= $header_and_footer;
} elseif ($settings->all_pages_header && ! $settings->all_pages_footer) {
$css .= $header;
} elseif (! $settings->all_pages_header && $settings->all_pages_footer) {
$css .= $footer;
}
$css .= '
.page {
page-break-after: always;
}
@page {
margin: 0mm
}
html {
';
$css .= 'font-size:'.$settings->font_size.'px;';
2023-10-26 04:57:44 +02:00
// $css .= 'font-size:14px;';
$css .= '}';
return $css;
}
}