680 lines
20 KiB
PHP
Raw Normal View History

2020-12-24 01:28:38 +03:00
<?php
2022-06-01 10:15:55 +03:00
namespace App\Abstracts\View\Components\Documents;
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
use App\Abstracts\View\Component;
2020-12-24 01:28:38 +03:00
use App\Models\Common\Media;
2021-02-01 16:39:21 +03:00
use App\Traits\DateTime;
use App\Traits\Documents;
2022-06-01 10:15:55 +03:00
use App\Traits\ViewComponents;
2021-01-29 11:04:15 +03:00
use Illuminate\Support\Facades\Log;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
2022-06-01 10:15:55 +03:00
use Intervention\Image\Exception\NotReadableException;
use Image;
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
abstract class Template extends Component
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
use DateTime, Documents, ViewComponents;
public const OBJECT_TYPE = 'document';
public const DEFAULT_TYPE = 'invoice';
public const DEFAULT_PLURAL_TYPE = 'invoices';
2020-12-24 01:28:38 +03:00
public $type;
2021-01-06 18:30:43 +03:00
public $item;
2020-12-24 01:28:38 +03:00
public $document;
/** @var string */
public $documentTemplate;
public $date_format;
public $logo;
public $backgroundColor;
2020-12-24 01:28:38 +03:00
public $hideFooter;
public $hideCompanyLogo;
public $hideCompanyDetails;
public $hideCompanyName;
public $hideCompanyAddress;
public $hideCompanyTaxNumber;
public $hideCompanyPhone;
public $hideCompanyEmail;
public $hideContactInfo;
public $hideContactName;
public $hideContactAddress;
public $hideContactTaxNumber;
public $hideContactPhone;
public $hideContactEmail;
public $hideOrderNumber;
public $hideDocumentNumber;
public $hideIssuedAt;
public $hideDueAt;
/** @var string */
public $textDocumentTitle;
/** @var string */
public $textDocumentSubheading;
2020-12-24 01:28:38 +03:00
public $textContactInfo;
/** @var string */
public $textIssuedAt;
/** @var string */
public $textDueAt;
/** @var string */
public $textDocumentNumber;
/** @var string */
public $textOrderNumber;
public $hideItems;
public $hideName;
public $hideDescription;
public $hideQuantity;
public $hidePrice;
public $hideDiscount;
2020-12-24 01:28:38 +03:00
public $hideAmount;
/** @var string */
public $textItems;
/** @var string */
public $textQuantity;
/** @var string */
public $textPrice;
/** @var string */
public $textAmount;
public $hideNote;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct(
$type, $document, $item = false, $documentTemplate = '', $logo = '', $backgroundColor = '',
2020-12-24 01:28:38 +03:00
bool $hideFooter = false, bool $hideCompanyLogo = false, bool $hideCompanyDetails = false,
bool $hideCompanyName = false, bool $hideCompanyAddress = false, bool $hideCompanyTaxNumber = false, bool $hideCompanyPhone = false, bool $hideCompanyEmail = false, bool $hideContactInfo = false,
bool $hideContactName = false, bool $hideContactAddress = false, bool $hideContactTaxNumber = false, bool $hideContactPhone = false, bool $hideContactEmail = false,
bool $hideOrderNumber = false, bool $hideDocumentNumber = false, bool $hideIssuedAt = false, bool $hideDueAt = false,
string $textDocumentTitle = '', string $textDocumentSubheading = '',
2020-12-24 01:28:38 +03:00
string $textContactInfo = '', string $textDocumentNumber = '', string $textOrderNumber = '', string $textIssuedAt = '', string $textDueAt = '',
bool $hideItems = false, bool $hideName = false, bool $hideDescription = false, bool $hideQuantity = false, bool $hidePrice = false, bool $hideDiscount = false, bool $hideAmount = false, bool $hideNote = false,
string $textItems = '', string $textQuantity = '', string $textPrice = '', string $textAmount = ''
) {
$this->type = $type;
2021-01-06 18:30:43 +03:00
$this->item = $item;
2020-12-24 01:28:38 +03:00
$this->document = $document;
$this->documentTemplate = $this->getDocumentTemplate($type, $documentTemplate);
$this->logo = $this->getLogo($logo);
$this->backgroundColor = $this->getBackgroundColor($type, $backgroundColor);
2020-12-24 01:28:38 +03:00
$this->hideFooter = $hideFooter;
$this->hideCompanyLogo = $hideCompanyLogo;
$this->hideCompanyDetails = $hideCompanyDetails;
$this->hideCompanyName = $hideCompanyName;
$this->hideCompanyAddress = $hideCompanyAddress;
$this->hideCompanyTaxNumber = $hideCompanyTaxNumber;
$this->hideCompanyPhone = $hideCompanyPhone;
$this->hideCompanyEmail = $hideCompanyEmail;
$this->hideContactInfo = $hideContactInfo;
$this->hideContactName = $hideContactName;
$this->hideContactAddress = $hideContactAddress;
$this->hideContactTaxNumber = $hideContactTaxNumber;
$this->hideContactPhone = $hideContactPhone;
$this->hideContactEmail = $hideContactEmail;
$this->hideOrderNumber = $hideOrderNumber;
$this->hideDocumentNumber = $hideDocumentNumber;
$this->hideIssuedAt = $hideIssuedAt;
$this->hideDueAt = $hideDueAt;
$this->textDocumentTitle = $this->getTextDocumentTitle($type, $textDocumentTitle);
$this->textDocumentSubheading = $this->gettextDocumentSubheading($type, $textDocumentSubheading);
2020-12-24 01:28:38 +03:00
$this->textContactInfo = $this->getTextContactInfo($type, $textContactInfo);
2020-12-28 22:10:43 +03:00
$this->textIssuedAt = $this->getTextIssuedAt($type, $textIssuedAt);
2020-12-24 01:28:38 +03:00
$this->textDocumentNumber = $this->getTextDocumentNumber($type, $textDocumentNumber);
$this->textDueAt = $this->getTextDueAt($type, $textDueAt);
$this->textOrderNumber = $this->getTextOrderNumber($type, $textOrderNumber);
$this->hideItems = $this->getHideItems($type, $hideItems, $hideName, $hideDescription);
$this->hideName = $this->getHideName($type, $hideName);
$this->hideDescription = $this->getHideDescription($type, $hideDescription);
$this->hideQuantity = $this->getHideQuantity($type, $hideQuantity);
$this->hidePrice = $this->getHidePrice($type, $hidePrice);
$this->hideDiscount = $this->getHideDiscount($type, $hideDiscount);
$this->hideAmount = $this->getHideAmount($type, $hideAmount);
$this->hideNote = $hideNote;
$this->textItems = $this->getTextItems($type, $textItems);
$this->textQuantity = $this->getTextQuantity($type, $textQuantity);
$this->textPrice = $this->getTextPrice($type, $textPrice);
$this->textAmount = $this->getTextAmount($type, $textAmount);
2022-06-01 10:15:55 +03:00
// Set Parent data
//$this->setParentData();
2020-12-24 01:28:38 +03:00
}
protected function getDocumentTemplate($type, $documentTemplate)
{
2022-06-01 10:15:55 +03:00
if (! empty($documentTemplate)) {
2020-12-24 01:28:38 +03:00
return $documentTemplate;
}
2022-06-01 10:15:55 +03:00
if ($template = config('type.document.' . $type . '.template', false)) {
2021-01-09 22:31:26 +03:00
return $template;
2020-12-24 01:28:38 +03:00
}
$documentTemplate = setting($this->getSettingKey($type, 'template'), 'default');
2021-01-09 22:31:26 +03:00
2020-12-24 01:28:38 +03:00
return $documentTemplate;
}
protected function getLogo($logo)
{
2022-06-01 10:15:55 +03:00
if (! empty($logo)) {
2020-12-24 01:28:38 +03:00
return $logo;
}
2022-06-01 10:15:55 +03:00
$media_id = (! empty($this->document->contact->logo) && ! empty($this->document->contact->logo->id)) ? $this->document->contact->logo->id : setting('company.logo');
$media = Media::find($media_id);
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if (! empty($media)) {
2021-05-30 19:28:31 +03:00
$path = $media->getDiskPath();
2020-12-24 01:28:38 +03:00
2021-05-30 19:28:31 +03:00
if (Storage::missing($path)) {
2020-12-24 01:28:38 +03:00
return $logo;
}
} else {
$path = base_path('public/img/company.png');
}
2021-01-29 11:04:15 +03:00
try {
$image = Image::cache(function($image) use ($media, $path) {
2021-01-29 11:04:15 +03:00
$width = setting('invoice.logo_size_width');
$height = setting('invoice.logo_size_height');
2020-12-24 01:28:38 +03:00
if ($media) {
2021-05-26 17:22:17 +03:00
$image->make(Storage::get($path))->resize($width, $height)->encode();
} else {
$image->make($path)->resize($width, $height)->encode();
}
2021-01-29 11:04:15 +03:00
});
} catch (NotReadableException | \Exception $e) {
2021-04-16 00:59:43 +03:00
Log::info('Company ID: ' . company_id() . ' components/documentshow.php exception.');
2021-01-29 11:04:15 +03:00
Log::info($e->getMessage());
$path = base_path('public/img/company.png');
$image = Image::cache(function($image) use ($path) {
$width = setting('invoice.logo_size_width');
$height = setting('invoice.logo_size_height');
$image->make($path)->resize($width, $height)->encode();
});
}
2020-12-24 01:28:38 +03:00
if (empty($image)) {
return $logo;
}
$extension = File::extension($path);
return 'data:image/' . $extension . ';base64,' . base64_encode($image);
}
protected function getBackgroundColor($type, $backgroundColor)
{
2022-06-01 10:15:55 +03:00
if (! empty($backgroundColor)) {
2020-12-24 01:28:38 +03:00
return $backgroundColor;
}
2022-06-01 10:15:55 +03:00
if ($background_color = config('type.document.' . $type . '.color', false)) {
2021-01-09 22:31:26 +03:00
return $background_color;
2020-12-24 01:28:38 +03:00
}
2022-06-01 10:15:55 +03:00
if (! empty($alias = config('type.document.' . $type . '.alias'))) {
$type = $alias . '.' . str_replace('-', '_', $type);
}
$backgroundColor = setting($this->getSettingKey($type, 'color'), '#55588b');
2021-01-09 22:31:26 +03:00
2020-12-24 01:28:38 +03:00
return $backgroundColor;
}
protected function getTextDocumentTitle($type, $textDocumentTitle)
{
2022-06-01 10:15:55 +03:00
if (! empty($textDocumentTitle)) {
return $textDocumentTitle;
}
2021-12-01 12:11:09 +00:00
$key = $this->getSettingKey($type, 'title');
2022-06-01 10:15:55 +03:00
if (! empty(setting($key))) {
2021-12-01 12:11:09 +00:00
return setting($key);
}
$translation = $this->getTextFromConfig($type, 'document_title', Str::plural($type));
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return trans_choice($translation, 1);
}
return setting('invoice.title');
}
protected function getTextDocumentSubheading($type, $textDocumentSubheading)
{
2022-06-01 10:15:55 +03:00
if (! empty($textDocumentSubheading)) {
return $textDocumentSubheading;
}
2021-12-01 12:11:09 +00:00
$key = $this->getSettingKey($type, 'subheading');
2022-06-01 10:15:55 +03:00
if (! empty(setting($key))) {
2021-12-01 12:11:09 +00:00
return setting($key);
}
$translation = $this->getTextFromConfig($type, 'document_subheading', 'subheading');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return trans($translation);
}
return false;
}
2020-12-24 01:28:38 +03:00
protected function getTextDocumentNumber($type, $textDocumentNumber)
{
2022-06-01 10:15:55 +03:00
if (! empty($textDocumentNumber)) {
2020-12-24 01:28:38 +03:00
return $textDocumentNumber;
}
2021-01-09 18:24:16 +03:00
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$default_key = 'bill_number';
break;
default:
$default_key = 'invoice_number';
break;
}
$translation = $this->getTextFromConfig($type, 'document_number', $default_key);
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'general.numbers';
2020-12-24 01:28:38 +03:00
}
protected function getTextOrderNumber($type, $textOrderNumber)
{
2022-06-01 10:15:55 +03:00
if (! empty($textOrderNumber)) {
2020-12-24 01:28:38 +03:00
return $textOrderNumber;
}
$translation = $this->getTextFromConfig($type, 'order_number');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'invoices.order_number';
2020-12-24 01:28:38 +03:00
}
protected function getTextContactInfo($type, $textContactInfo)
{
2022-06-01 10:15:55 +03:00
if (! empty($textContactInfo)) {
2020-12-24 01:28:38 +03:00
return $textContactInfo;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$default_key = 'bill_from';
2020-12-24 01:28:38 +03:00
break;
2020-12-28 22:10:43 +03:00
default:
$default_key = 'bill_to';
2020-12-28 22:10:43 +03:00
break;
2020-12-24 01:28:38 +03:00
}
$translation = $this->getTextFromConfig($type, 'contact_info', $default_key);
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
}
return 'invoices.bill_to';
2020-12-24 01:28:38 +03:00
}
2020-12-28 22:10:43 +03:00
protected function getTextIssuedAt($type, $textIssuedAt)
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
if (! empty($textIssuedAt)) {
2020-12-24 01:28:38 +03:00
return $textIssuedAt;
}
switch ($type) {
case 'bill':
case 'expense':
case 'purchase':
$default_key = 'bill_date';
2020-12-24 01:28:38 +03:00
break;
2020-12-28 22:10:43 +03:00
default:
$default_key = 'invoice_date';
2020-12-28 22:10:43 +03:00
break;
2020-12-24 01:28:38 +03:00
}
$translation = $this->getTextFromConfig($type, 'issued_at', $default_key);
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
}
return 'invoices.invoice_date';
2020-12-24 01:28:38 +03:00
}
protected function getTextDueAt($type, $textDueAt)
{
2022-06-01 10:15:55 +03:00
if (! empty($textDueAt)) {
2020-12-24 01:28:38 +03:00
return $textDueAt;
}
$translation = $this->getTextFromConfig($type, 'due_at', 'due_date');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'invoices.due_date';
2020-12-24 01:28:38 +03:00
}
protected function getTextItems($type, $textItems)
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
if (! empty($textItems)) {
return $textItems;
2020-12-24 01:28:38 +03:00
}
// if you use settting translation
if (setting($this->getSettingKey($type, 'item_name'), 'items') === 'custom') {
if (empty($textItems = setting($this->getSettingKey($type, 'item_name_input')))) {
$textItems = 'general.items';
}
return $textItems;
}
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if (setting($this->getSettingKey($type, 'item_name')) !== null
&& (trans(setting($this->getSettingKey($type, 'item_name'))) != setting($this->getSettingKey($type, 'item_name')))
) {
return setting($this->getSettingKey($type, 'item_name'));
}
$translation = $this->getTextFromConfig($type, 'items');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'general.items';
2020-12-24 01:28:38 +03:00
}
protected function getTextQuantity($type, $textQuantity)
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
if (! empty($textQuantity)) {
return $textQuantity;
2020-12-24 01:28:38 +03:00
}
// if you use settting translation
if (setting($this->getSettingKey($type, 'quantity_name'), 'quantity') === 'custom') {
if (empty($textQuantity = setting($this->getSettingKey($type, 'quantity_name_input')))) {
$textQuantity = 'invoices.quantity';
}
return $textQuantity;
}
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if (setting($this->getSettingKey($type, 'quantity_name')) !== null
&& (trans(setting($this->getSettingKey($type, 'quantity_name'))) != setting($this->getSettingKey($type, 'quantity_name')))
) {
return setting($this->getSettingKey($type, 'quantity_name'));
}
$translation = $this->getTextFromConfig($type, 'quantity');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'invoices.quantity';
2020-12-24 01:28:38 +03:00
}
protected function getTextPrice($type, $textPrice)
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
if (! empty($textPrice)) {
return $textPrice;
2020-12-24 01:28:38 +03:00
}
// if you use settting translation
if (setting($this->getSettingKey($type, 'price_name'), 'price') === 'custom') {
if (empty($textPrice = setting($this->getSettingKey($type, 'price_name_input')))) {
$textPrice = 'invoices.price';
}
return $textPrice;
}
2020-12-24 01:28:38 +03:00
2022-06-01 10:15:55 +03:00
if (setting($this->getSettingKey($type, 'price_name')) !== null
&& (trans(setting($this->getSettingKey($type, 'price_name'))) != setting($this->getSettingKey($type, 'price_name')))
) {
return setting($this->getSettingKey($type, 'price_name'));
}
$translation = $this->getTextFromConfig($type, 'price');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
2020-12-24 01:28:38 +03:00
}
return 'invoices.price';
2020-12-24 01:28:38 +03:00
}
protected function getTextAmount($type, $textAmount)
2020-12-24 01:28:38 +03:00
{
2022-06-01 10:15:55 +03:00
if (! empty($textAmount)) {
return $textAmount;
2020-12-24 01:28:38 +03:00
}
$translation = $this->getTextFromConfig($type, 'amount');
2022-06-01 10:15:55 +03:00
if (! empty($translation)) {
return $translation;
}
2020-12-24 01:28:38 +03:00
return 'general.amount';
2020-12-24 01:28:38 +03:00
}
protected function getHideItems($type, $hideItems, $hideName, $hideDescription)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideItems)) {
2020-12-24 01:28:38 +03:00
return $hideItems;
}
$hide = $this->getHideFromConfig($type, 'items');
if ($hide) {
return $hide;
}
2020-12-24 01:28:38 +03:00
$hideItems = ($this->getHideName($type, $hideName) & $this->getHideDescription($type, $hideDescription)) ? true : false;
2020-12-24 01:28:38 +03:00
return $hideItems;
}
protected function getHideName($type, $hideName)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideName)) {
2020-12-24 01:28:38 +03:00
return $hideName;
}
// if you use settting translation
if ($hideName = setting($this->getSettingKey($type, 'hide_item_name'), false)) {
return $hideName;
}
$hide = $this->getHideFromConfig($type, 'name');
if ($hide) {
return $hide;
2020-12-24 01:28:38 +03:00
}
// @todo what return value invoice or always false??
return setting('invoice.hide_item_name', $hideName);
2020-12-24 01:28:38 +03:00
}
protected function getHideDescription($type, $hideDescription)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideDescription)) {
2020-12-24 01:28:38 +03:00
return $hideDescription;
}
// if you use settting translation
if ($hideDescription = setting($this->getSettingKey($type, 'hide_item_description'), false)) {
return $hideDescription;
2020-12-24 01:28:38 +03:00
}
$hide = $this->getHideFromConfig($type, 'description');
if ($hide) {
return $hide;
}
// @todo what return value invoice or always false??
return setting('invoice.hide_item_description', $hideDescription);
2020-12-24 01:28:38 +03:00
}
protected function getHideQuantity($type, $hideQuantity)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideQuantity)) {
2020-12-24 01:28:38 +03:00
return $hideQuantity;
}
// if you use settting translation
if ($hideQuantity = setting($this->getSettingKey($type, 'hide_quantity'), false)) {
return $hideQuantity;
2020-12-24 01:28:38 +03:00
}
$hide = $this->getHideFromConfig($type, 'quantity');
if ($hide) {
return $hide;
}
// @todo what return value invoice or always false??
return setting('invoice.hide_quantity', $hideQuantity);
2020-12-24 01:28:38 +03:00
}
protected function getHidePrice($type, $hidePrice)
{
2022-06-01 10:15:55 +03:00
if (! empty($hidePrice)) {
2020-12-24 01:28:38 +03:00
return $hidePrice;
}
// if you use settting translation
if ($hidePrice = setting($this->getSettingKey($type, 'hide_price'), false)) {
return $hidePrice;
2020-12-24 01:28:38 +03:00
}
$hide = $this->getHideFromConfig($type, 'price');
if ($hide) {
return $hide;
}
// @todo what return value invoice or always false??
return setting('invoice.hide_price', $hidePrice);
2020-12-24 01:28:38 +03:00
}
protected function getHideDiscount($type, $hideDiscount)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideDiscount)) {
2020-12-24 01:28:38 +03:00
return $hideDiscount;
}
// if you use settting translation
if ($hideDiscount = setting($this->getSettingKey($type, 'hide_discount'), false)) {
return $hideDiscount;
2020-12-24 01:28:38 +03:00
}
$hide = $this->getHideFromConfig($type, 'discount');
if ($hide) {
return $hide;
}
// @todo what return value invoice or always false??
return setting('invoice.hide_discount', $hideDiscount);
2020-12-24 01:28:38 +03:00
}
2020-12-24 01:28:38 +03:00
protected function getHideAmount($type, $hideAmount)
{
2022-06-01 10:15:55 +03:00
if (! empty($hideAmount)) {
2020-12-24 01:28:38 +03:00
return $hideAmount;
}
// if you use settting translation
if ($hideAmount = setting($this->getSettingKey($type, 'hide_amount'), false)) {
return $hideAmount;
}
$hide = $this->getHideFromConfig($type, 'amount');
if ($hide) {
return $hide;
2020-12-24 01:28:38 +03:00
}
// @todo what return value invoice or always false??
return setting('invoice.hide_amount', $hideAmount);
2020-12-24 01:28:38 +03:00
}
}