Merge pull request #2966 from CihanSenturk/add-transaction-number-interface
added transaction number interface
This commit is contained in:
commit
f57a3488ee
@ -101,7 +101,7 @@ class Transactions extends Controller
|
|||||||
$type = request()->get('type', 'income');
|
$type = request()->get('type', 'income');
|
||||||
$real_type = $this->getRealTypeTransaction($type);
|
$real_type = $this->getRealTypeTransaction($type);
|
||||||
|
|
||||||
$number = $this->getNextTransactionNumber();
|
$number = $this->getNextTransactionNumber($type);
|
||||||
|
|
||||||
$contact_type = config('type.transaction.' . $type . '.contact_type');
|
$contact_type = config('type.transaction.' . $type . '.contact_type');
|
||||||
|
|
||||||
|
12
app/Interfaces/Utility/TransactionNumber.php
Normal file
12
app/Interfaces/Utility/TransactionNumber.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Interfaces\Utility;
|
||||||
|
|
||||||
|
use App\Models\Common\Contact;
|
||||||
|
|
||||||
|
interface TransactionNumber
|
||||||
|
{
|
||||||
|
public function getNextNumber(string $type, string $suffix, ?Contact $contact): string;
|
||||||
|
|
||||||
|
public function increaseNextNumber(string $type, string $suffix, ?Contact $contact): void;
|
||||||
|
}
|
@ -3,7 +3,9 @@
|
|||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
use App\Interfaces\Utility\DocumentNumber as DocumentNumberInterface;
|
use App\Interfaces\Utility\DocumentNumber as DocumentNumberInterface;
|
||||||
|
use App\Interfaces\Utility\TransactionNumber as TransactionNumberInterface;
|
||||||
use App\Utilities\DocumentNumber;
|
use App\Utilities\DocumentNumber;
|
||||||
|
use App\Utilities\TransactionNumber;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class Binding extends ServiceProvider
|
class Binding extends ServiceProvider
|
||||||
@ -15,5 +17,6 @@ class Binding extends ServiceProvider
|
|||||||
*/
|
*/
|
||||||
public array $bindings = [
|
public array $bindings = [
|
||||||
DocumentNumberInterface::class => DocumentNumber::class,
|
DocumentNumberInterface::class => DocumentNumber::class,
|
||||||
|
TransactionNumberInterface::class => TransactionNumber::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Traits;
|
|||||||
|
|
||||||
use App\Events\Banking\TransactionPrinting;
|
use App\Events\Banking\TransactionPrinting;
|
||||||
use App\Models\Banking\Transaction;
|
use App\Models\Banking\Transaction;
|
||||||
|
use App\Interfaces\Utility\TransactionNumber;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
trait Transactions
|
trait Transactions
|
||||||
@ -228,36 +229,13 @@ trait Transactions
|
|||||||
return Str::replace('-split', '', $transfer_type);
|
return Str::replace('-split', '', $transfer_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getNextTransactionNumber($suffix = ''): string
|
public function getNextTransactionNumber($type = 'income', $suffix = ''): string
|
||||||
{
|
{
|
||||||
$prefix = setting('transaction' . $suffix . '.number_prefix');
|
return app(TransactionNumber::class)->getNextNumber($type, $suffix, null);
|
||||||
$next = (string) setting('transaction' . $suffix . '.number_next');
|
|
||||||
$digit = (int) setting('transaction' . $suffix . '.number_digit');
|
|
||||||
|
|
||||||
$get_number = fn($prefix, $next, $digit) => $prefix . str_pad($next, $digit, '0', STR_PAD_LEFT);
|
|
||||||
$number_exists = fn($number) => Transaction::where('number', $number)->exists();
|
|
||||||
|
|
||||||
$transaction_number = $get_number($prefix, $next, $digit);
|
|
||||||
|
|
||||||
if ($number_exists($transaction_number)) {
|
|
||||||
do {
|
|
||||||
$next++;
|
|
||||||
|
|
||||||
$transaction_number = $get_number($prefix, $next, $digit);
|
|
||||||
} while ($number_exists($transaction_number));
|
|
||||||
|
|
||||||
setting(['transaction' . $suffix . '.number_next' => $next]);
|
|
||||||
setting()->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $transaction_number;
|
public function increaseNextTransactionNumber($type = 'income', $suffix = ''): void
|
||||||
}
|
|
||||||
|
|
||||||
public function increaseNextTransactionNumber($suffix = ''): void
|
|
||||||
{
|
{
|
||||||
$next = setting('transaction' . $suffix . '.number_next', 1) + 1;
|
app(TransactionNumber::class)->increaseNextNumber($type, $suffix, null);
|
||||||
|
|
||||||
setting(['transaction' . $suffix . '.number_next' => $next]);
|
|
||||||
setting()->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
44
app/Utilities/TransactionNumber.php
Normal file
44
app/Utilities/TransactionNumber.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Utilities;
|
||||||
|
|
||||||
|
use App\Interfaces\Utility\TransactionNumber as TransactionNumberInterface;
|
||||||
|
use App\Models\Banking\Transaction;
|
||||||
|
use App\Models\Common\Contact;
|
||||||
|
|
||||||
|
class TransactionNumber implements TransactionNumberInterface
|
||||||
|
{
|
||||||
|
public function getNextNumber($type, $suffix = '', ?Contact $contact): string
|
||||||
|
{
|
||||||
|
$prefix = setting('transaction' . $suffix . '.number_prefix');
|
||||||
|
$next = (string) setting('transaction' . $suffix . '.number_next');
|
||||||
|
$digit = (int) setting('transaction' . $suffix . '.number_digit');
|
||||||
|
|
||||||
|
$get_number = fn($prefix, $next, $digit) => $prefix . str_pad($next, $digit, '0', STR_PAD_LEFT);
|
||||||
|
$number_exists = fn($number) => Transaction::where('number', $number)->exists();
|
||||||
|
|
||||||
|
$transaction_number = $get_number($prefix, $next, $digit);
|
||||||
|
|
||||||
|
if ($number_exists($transaction_number)) {
|
||||||
|
do {
|
||||||
|
$next++;
|
||||||
|
|
||||||
|
$transaction_number = $get_number($prefix, $next, $digit);
|
||||||
|
} while ($number_exists($transaction_number));
|
||||||
|
|
||||||
|
setting(['transaction' . $suffix . '.number_next' => $next]);
|
||||||
|
setting()->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $transaction_number;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function increaseNextNumber($type, $suffix = '', ?Contact $contact): void
|
||||||
|
{
|
||||||
|
$next = setting('transaction' . $suffix . '.number_next', 1) + 1;
|
||||||
|
|
||||||
|
setting(['transaction' . $suffix . '.number_next' => $next]);
|
||||||
|
setting()->save();
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
namespace Database\Factories;
|
namespace Database\Factories;
|
||||||
|
|
||||||
use App\Abstracts\Factory;
|
use App\Abstracts\Factory;
|
||||||
|
use App\Interfaces\Utility\TransactionNumber;
|
||||||
use App\Models\Banking\Transaction as Model;
|
use App\Models\Banking\Transaction as Model;
|
||||||
use App\Models\Common\Contact;
|
use App\Models\Common\Contact;
|
||||||
use App\Traits\Transactions;
|
use App\Traits\Transactions;
|
||||||
@ -41,7 +42,7 @@ class Transaction extends Factory
|
|||||||
return [
|
return [
|
||||||
'company_id' => $this->company->id,
|
'company_id' => $this->company->id,
|
||||||
'type' => $this->type,
|
'type' => $this->type,
|
||||||
'number' => $this->getNumber(),
|
'number' => $this->getNumber($this->type),
|
||||||
'account_id' => setting('default.account'),
|
'account_id' => setting('default.account'),
|
||||||
'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'),
|
'paid_at' => $this->faker->dateTimeBetween(now()->startOfYear(), now()->endOfYear())->format('Y-m-d H:i:s'),
|
||||||
'amount' => $this->faker->randomFloat(2, 1, 1000),
|
'amount' => $this->faker->randomFloat(2, 1, 1000),
|
||||||
@ -73,6 +74,7 @@ class Transaction extends Factory
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'type' => 'income',
|
'type' => 'income',
|
||||||
|
'number' => $this->getNumber('income', '', $contact),
|
||||||
'contact_id' => $contact->id,
|
'contact_id' => $contact->id,
|
||||||
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
|
'category_id' => $this->company->categories()->income()->get()->random(1)->pluck('id')->first(),
|
||||||
];
|
];
|
||||||
@ -97,6 +99,7 @@ class Transaction extends Factory
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'type' => 'expense',
|
'type' => 'expense',
|
||||||
|
'number' => $this->getNumber('expense', '', $contact),
|
||||||
'contact_id' => $contact->id,
|
'contact_id' => $contact->id,
|
||||||
'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(),
|
'category_id' => $this->company->categories()->expense()->get()->random(1)->pluck('id')->first(),
|
||||||
];
|
];
|
||||||
@ -110,9 +113,11 @@ class Transaction extends Factory
|
|||||||
*/
|
*/
|
||||||
public function recurring()
|
public function recurring()
|
||||||
{
|
{
|
||||||
|
$type = $this->getRawAttribute('type') . '-recurring';
|
||||||
|
|
||||||
return $this->state([
|
return $this->state([
|
||||||
'type' => $this->getRawAttribute('type') . '-recurring',
|
'type' => $type,
|
||||||
'number' => $this->getNumber('-recurring'),
|
'number' => $this->getNumber($type, '-recurring'),
|
||||||
'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'),
|
'recurring_started_at' => Date::now()->format('Y-m-d H:i:s'),
|
||||||
'recurring_frequency' => 'daily',
|
'recurring_frequency' => 'daily',
|
||||||
'recurring_custom_frequency' => 'daily',
|
'recurring_custom_frequency' => 'daily',
|
||||||
@ -129,11 +134,13 @@ class Transaction extends Factory
|
|||||||
* Get transaction number
|
* Get transaction number
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function getNumber($suffix = '')
|
public function getNumber($type, $suffix = '', $contact = null)
|
||||||
{
|
{
|
||||||
$number = $this->getNextTransactionNumber($suffix);
|
$utility = app(TransactionNumber::class);
|
||||||
|
|
||||||
$this->increaseNextTransactionNumber($suffix);
|
$number = $utility->getNextNumber($type, $suffix, $contact);
|
||||||
|
|
||||||
|
$utility->increaseNextNumber($type, $suffix ,$contact);
|
||||||
|
|
||||||
return $number;
|
return $number;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user