1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Models/Traits/PresentsInvoice.php

397 lines
12 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models\Traits;
2016-09-02 16:53:16 +02:00
use Utils;
2016-09-02 16:53:16 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class PresentsInvoice.
2016-09-02 16:53:16 +02:00
*/
trait PresentsInvoice
{
public function getInvoiceFields()
{
2016-09-05 07:10:39 +02:00
if ($this->invoice_fields) {
$fields = json_decode($this->invoice_fields, true);
2017-01-30 20:40:43 +01:00
2017-10-27 10:46:08 +02:00
if (! isset($fields['product_fields'])) {
$fields['product_fields'] = [
'product.item',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.unit_cost',
'product.quantity',
'product.tax',
'product.line_total',
];
$fields['task_fields'] = [
'product.service',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.rate',
'product.hours',
'product.tax',
'product.line_total',
];
}
2016-09-05 14:28:59 +02:00
return $this->applyLabels($fields);
2016-09-05 07:10:39 +02:00
} else {
2016-09-05 14:28:59 +02:00
return $this->getDefaultInvoiceFields();
}
}
public function getDefaultInvoiceFields()
{
$fields = [
INVOICE_FIELDS_INVOICE => [
'invoice.invoice_number',
'invoice.po_number',
'invoice.invoice_date',
'invoice.due_date',
'invoice.balance_due',
'invoice.partial_due',
],
INVOICE_FIELDS_CLIENT => [
'client.client_name',
'client.id_number',
'client.vat_number',
'client.address1',
'client.address2',
'client.city_state_postal',
'client.country',
'client.email',
],
'account_fields1' => [
'account.company_name',
'account.id_number',
'account.vat_number',
'account.website',
'account.email',
'account.phone',
],
'account_fields2' => [
'account.address1',
'account.address2',
'account.city_state_postal',
'account.country',
],
2017-10-27 10:46:08 +02:00
'product_fields' => [
'product.item',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.unit_cost',
'product.quantity',
'product.tax',
'product.line_total',
],
'task_fields' => [
'product.service',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.rate',
'product.hours',
'product.tax',
'product.line_total',
]
2016-09-05 14:28:59 +02:00
];
2018-04-04 15:24:59 +02:00
if ($this->customLabel('invoice_text1')) {
2016-09-05 14:28:59 +02:00
$fields[INVOICE_FIELDS_INVOICE][] = 'invoice.custom_text_value1';
2016-09-05 07:10:39 +02:00
}
2018-04-04 15:24:59 +02:00
if ($this->customLabel('invoice_text2')) {
2016-09-05 14:28:59 +02:00
$fields[INVOICE_FIELDS_INVOICE][] = 'invoice.custom_text_value2';
}
2018-04-04 15:24:59 +02:00
if ($this->customLabel('client1')) {
2016-09-05 14:28:59 +02:00
$fields[INVOICE_FIELDS_CLIENT][] = 'client.custom_value1';
}
2018-04-04 15:24:59 +02:00
if ($this->customLabel('client2')) {
2016-09-05 14:28:59 +02:00
$fields[INVOICE_FIELDS_CLIENT][] = 'client.custom_value2';
}
2018-04-04 15:24:59 +02:00
if ($this->customLabel('contact1')) {
2017-04-16 13:31:14 +02:00
$fields[INVOICE_FIELDS_CLIENT][] = 'contact.custom_value1';
}
2018-04-04 15:24:59 +02:00
if ($this->customLabel('contact2')) {
2017-04-16 13:31:14 +02:00
$fields[INVOICE_FIELDS_CLIENT][] = 'contact.custom_value2';
}
2016-09-05 14:28:59 +02:00
if ($this->custom_label1) {
$fields['account_fields2'][] = 'account.custom_value1';
}
if ($this->custom_label2) {
$fields['account_fields2'][] = 'account.custom_value2';
}
2016-09-05 07:10:39 +02:00
return $this->applyLabels($fields);
}
2016-09-02 16:53:16 +02:00
2016-09-05 07:10:39 +02:00
public function getAllInvoiceFields()
{
2016-09-02 16:53:16 +02:00
$fields = [
INVOICE_FIELDS_INVOICE => [
2016-09-05 14:28:59 +02:00
'invoice.invoice_number',
'invoice.po_number',
'invoice.invoice_date',
'invoice.due_date',
'invoice.invoice_total',
2016-09-05 14:28:59 +02:00
'invoice.balance_due',
'invoice.partial_due',
'invoice.outstanding',
2016-09-05 14:28:59 +02:00
'invoice.custom_text_value1',
'invoice.custom_text_value2',
'.blank',
2016-09-02 16:53:16 +02:00
],
INVOICE_FIELDS_CLIENT => [
2016-09-05 14:28:59 +02:00
'client.client_name',
'client.id_number',
'client.vat_number',
'client.website',
'client.work_phone',
2016-09-05 14:28:59 +02:00
'client.address1',
'client.address2',
'client.city_state_postal',
'client.postal_city_state',
2016-09-05 14:28:59 +02:00
'client.country',
'client.contact_name',
2016-09-05 14:28:59 +02:00
'client.email',
2016-09-23 11:11:46 +02:00
'client.phone',
2016-09-05 14:28:59 +02:00
'client.custom_value1',
'client.custom_value2',
2017-04-16 13:31:14 +02:00
'contact.custom_value1',
'contact.custom_value2',
'.blank',
2016-09-02 16:53:16 +02:00
],
2016-09-05 14:28:59 +02:00
INVOICE_FIELDS_ACCOUNT => [
'account.company_name',
'account.id_number',
'account.vat_number',
'account.website',
'account.email',
'account.phone',
'account.address1',
'account.address2',
'account.city_state_postal',
'account.postal_city_state',
2016-09-05 14:28:59 +02:00
'account.country',
'account.custom_value1',
'account.custom_value2',
'.blank',
2017-01-30 20:40:43 +01:00
],
2017-10-27 10:46:08 +02:00
INVOICE_FIELDS_PRODUCT => [
'product.item',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.unit_cost',
'product.quantity',
2017-12-21 15:30:18 +01:00
'product.discount',
2017-10-27 10:46:08 +02:00
'product.tax',
'product.line_total',
],
INVOICE_FIELDS_TASK => [
'product.service',
'product.description',
'product.custom_value1',
'product.custom_value2',
'product.rate',
'product.hours',
2017-12-21 15:30:18 +01:00
'product.discount',
2017-10-27 10:46:08 +02:00
'product.tax',
'product.line_total',
],
2016-09-02 16:53:16 +02:00
];
2016-09-05 07:10:39 +02:00
return $this->applyLabels($fields);
}
private function applyLabels($fields)
{
$labels = $this->getInvoiceLabels();
2016-09-02 16:53:16 +02:00
foreach ($fields as $section => $sectionFields) {
foreach ($sectionFields as $index => $field) {
2016-09-05 14:28:59 +02:00
list($entityType, $fieldName) = explode('.', $field);
if (substr($fieldName, 0, 6) == 'custom') {
$fields[$section][$field] = $labels[$field];
} elseif (in_array($field, ['client.phone', 'client.email'])) {
$fields[$section][$field] = trans('texts.contact_' . $fieldName);
2016-09-05 14:28:59 +02:00
} else {
$fields[$section][$field] = $labels[$fieldName];
}
2016-09-02 16:53:16 +02:00
unset($fields[$section][$index]);
}
}
return $fields;
}
public function hasCustomLabel($field)
{
$custom = (array) json_decode($this->invoice_labels);
return isset($custom[$field]) && $custom[$field];
}
public function getLabel($field, $override = false)
{
$custom = (array) json_decode($this->invoice_labels);
if (isset($custom[$field]) && $custom[$field]) {
return $custom[$field];
} else {
if ($override) {
$field = $override;
}
return $this->isEnglish() ? uctrans("texts.$field") : trans("texts.$field");
}
}
2016-09-02 16:53:16 +02:00
/**
* @return array
*/
public function getInvoiceLabels()
{
$data = [];
$custom = (array) json_decode($this->invoice_labels);
$fields = [
'invoice',
'invoice_date',
'due_date',
'invoice_number',
'po_number',
'discount',
'taxes',
'tax',
'item',
'description',
'unit_cost',
'quantity',
'line_total',
'subtotal',
'paid_to_date',
'balance_due',
'partial_due',
'terms',
'your_invoice',
'quote',
'your_quote',
'quote_date',
'quote_number',
'total',
'invoice_issued_to',
'quote_issued_to',
'rate',
'hours',
'balance',
'from',
'to',
'invoice_to',
2016-09-12 08:38:02 +02:00
'quote_to',
2016-09-02 16:53:16 +02:00
'details',
'invoice_no',
'quote_no',
2016-09-02 16:53:16 +02:00
'valid_until',
2016-09-05 07:10:39 +02:00
'client_name',
'address1',
'address2',
'id_number',
'vat_number',
'city_state_postal',
'postal_city_state',
2016-09-05 07:10:39 +02:00
'country',
'email',
'contact_name',
'company_name',
'website',
'phone',
'blank',
2017-03-16 15:03:17 +01:00
'surcharge',
2017-01-08 18:43:59 +01:00
'tax_invoice',
'tax_quote',
2017-01-23 16:00:44 +01:00
'statement',
'statement_date',
'your_statement',
'statement_issued_to',
'statement_to',
2017-03-26 17:25:59 +02:00
'credit_note',
'credit_date',
'credit_number',
'credit_issued_to',
'credit_to',
'your_credit',
'work_phone',
'invoice_total',
'outstanding',
'invoice_due_date',
'quote_due_date',
'service',
2017-10-27 10:46:08 +02:00
'product_key',
'unit_cost',
2017-10-29 08:48:23 +01:00
'custom_value1',
'custom_value2',
'delivery_note',
2018-03-12 21:02:48 +01:00
'date',
2018-04-29 16:27:55 +02:00
'method',
'payment_date',
'reference',
'amount',
2018-04-30 11:34:51 +02:00
'amount_paid',
2016-09-02 16:53:16 +02:00
];
foreach ($fields as $field) {
$translated = $this->isEnglish() ? uctrans("texts.$field") : trans("texts.$field");
2016-09-02 16:53:16 +02:00
if (isset($custom[$field]) && $custom[$field]) {
$data[$field] = $custom[$field];
$data[$field . '_orig'] = $translated;
2016-09-02 16:53:16 +02:00
} else {
$data[$field] = $translated;
2016-09-02 16:53:16 +02:00
}
}
foreach (['item', 'quantity', 'unit_cost'] as $field) {
$data["{$field}_orig"] = $data[$field];
}
2016-09-05 07:10:39 +02:00
foreach ([
2018-04-04 15:24:59 +02:00
'account.custom_value1' => 'account1',
'account.custom_value2' => 'account2',
'invoice.custom_text_value1' => 'invoice_text1',
'invoice.custom_text_value2' => 'invoice_text2',
'client.custom_value1' => 'client1',
'client.custom_value2' => 'client2',
'contact.custom_value1' => 'contact1',
'contact.custom_value2' => 'contact2',
'product.custom_value1' => 'product1',
'product.custom_value2' => 'product2',
2016-09-05 14:28:59 +02:00
] as $field => $property) {
2018-04-04 15:24:59 +02:00
$data[$field] = e($this->present()->customLabel($property)) ?: trans('texts.custom_field');
2016-09-05 07:10:39 +02:00
}
2016-09-02 16:53:16 +02:00
return $data;
}
2017-06-01 18:13:13 +02:00
public function getCustomDesign($designId) {
if ($designId == CUSTOM_DESIGN1) {
return $this->custom_design1;
} elseif ($designId == CUSTOM_DESIGN2) {
return $this->custom_design2;
} elseif ($designId == CUSTOM_DESIGN3) {
return $this->custom_design3;
}
return null;
}
2017-10-29 11:39:15 +01:00
2017-12-21 15:30:18 +01:00
public function hasInvoiceField($type, $field) {
2017-10-29 11:39:15 +01:00
$fields = $this->getInvoiceFields();
2017-12-21 15:30:18 +01:00
return isset($fields[$type . '_fields'][$field]);
2017-10-29 11:39:15 +01:00
}
2016-09-02 16:53:16 +02:00
}