akaunting/app/Traits/Transactions.php

112 lines
2.9 KiB
PHP
Raw Normal View History

2020-08-26 15:14:16 +03:00
<?php
namespace App\Traits;
2021-06-27 12:17:41 +03:00
use App\Models\Banking\Transaction;
use Illuminate\Support\Str;
2020-08-26 15:14:16 +03:00
trait Transactions
{
public function isIncome()
{
$type = $this->type ?? $this->transaction->type ?? $this->model->type ?? 'income';
2020-08-26 15:14:16 +03:00
return in_array($type, $this->getIncomeTypes());
}
public function isExpense()
{
$type = $this->type ?? $this->transaction->type ?? $this->model->type ?? 'expense';
2020-08-26 15:14:16 +03:00
return in_array($type, $this->getExpenseTypes());
}
public function getIncomeTypes($return = 'array')
{
return $this->getTransactionTypes('income', $return);
}
public function getExpenseTypes($return = 'array')
{
return $this->getTransactionTypes('expense', $return);
}
public function getTransactionTypes($index, $return = 'array')
{
$types = (string) setting('transaction.type.' . $index);
return ($return == 'array') ? explode(',', $types) : $types;
}
public function addIncomeType($new_type)
{
$this->addTransactionType($new_type, 'income');
}
public function addExpenseType($new_type)
{
$this->addTransactionType($new_type, 'expense');
}
public function addTransactionType($new_type, $index)
{
$types = explode(',', setting('transaction.type.' . $index));
if (in_array($new_type, $types)) {
return;
}
$types[] = $new_type;
setting([
'transaction.type.' . $index => implode(',', $types),
])->save();
}
2021-06-24 13:52:49 +03:00
2021-06-27 12:17:41 +03:00
public function getTransactionFileName(Transaction $transaction, string $separator = '-', string $extension = 'pdf'): string
{
return $this->getSafeTransactionNumber($transaction, $separator) . $separator . time() . '.' . $extension;
}
public function getSafeTransactionNumber(Transaction $transaction, string $separator = '-'): string
{
return Str::slug($transaction->id, $separator, language()->getShortCode());
}
2021-06-24 13:52:49 +03:00
protected function getSettingKey($type, $setting_key)
{
$key = '';
$alias = config('type.' . $type . '.alias');
if (!empty($alias)) {
$key .= $alias . '.';
}
$prefix = config('type.' . $type . '.setting.prefix');
$key .= $prefix . '.' . $setting_key;
return $key;
}
public function storeTransactionPdfAndGetPath($transaction)
{
event(new \App\Events\Banking\TransactionPrinting($transaction));
$view = view($transaction->template_path, ['revenue' => $transaction, 'transaction' => $transaction])->render();
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
$pdf = app('dompdf.wrapper');
$pdf->loadHTML($html);
$file_name = $this->getTransactionFileName($transaction);
$pdf_path = storage_path('app/temp/' . $file_name);
// Save the PDF file into temp folder
$pdf->save($pdf_path);
return $pdf_path;
}
2020-08-26 15:14:16 +03:00
}