added transaction number interface
This commit is contained in:
parent
c8a5127f65
commit
7a3aa98c56
@ -101,7 +101,7 @@ class Transactions extends Controller
|
||||
$type = request()->get('type', 'income');
|
||||
$real_type = $this->getRealTypeTransaction($type);
|
||||
|
||||
$number = $this->getNextTransactionNumber();
|
||||
$number = $this->getNextTransactionNumber($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;
|
||||
|
||||
use App\Interfaces\Utility\DocumentNumber as DocumentNumberInterface;
|
||||
use App\Interfaces\Utility\TransactionNumber as TransactionNumberInterface;
|
||||
use App\Utilities\DocumentNumber;
|
||||
use App\Utilities\TransactionNumber;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class Binding extends ServiceProvider
|
||||
@ -15,5 +17,6 @@ class Binding extends ServiceProvider
|
||||
*/
|
||||
public array $bindings = [
|
||||
DocumentNumberInterface::class => DocumentNumber::class,
|
||||
TransactionNumberInterface::class => TransactionNumber::class,
|
||||
];
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ namespace App\Traits;
|
||||
|
||||
use App\Events\Banking\TransactionPrinting;
|
||||
use App\Models\Banking\Transaction;
|
||||
use App\Interfaces\Utility\TransactionNumber;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait Transactions
|
||||
@ -228,36 +229,13 @@ trait Transactions
|
||||
return Str::replace('-split', '', $transfer_type);
|
||||
}
|
||||
|
||||
public function getNextTransactionNumber($suffix = ''): string
|
||||
public function getNextTransactionNumber($type = 'income', $suffix = ''): 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;
|
||||
return app(TransactionNumber::class)->getNextNumber($type, $suffix, null);
|
||||
}
|
||||
|
||||
public function increaseNextTransactionNumber($suffix = ''): void
|
||||
public function increaseNextTransactionNumber($type = 'income', $suffix = ''): void
|
||||
{
|
||||
$next = setting('transaction' . $suffix . '.number_next', 1) + 1;
|
||||
|
||||
setting(['transaction' . $suffix . '.number_next' => $next]);
|
||||
setting()->save();
|
||||
app(TransactionNumber::class)->increaseNextNumber($type, $suffix, null);
|
||||
}
|
||||
}
|
||||
|
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($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($suffix = '', ?Contact $contact): void
|
||||
{
|
||||
$next = setting('transaction' . $suffix . '.number_next', 1) + 1;
|
||||
|
||||
setting(['transaction' . $suffix . '.number_next' => $next]);
|
||||
setting()->save();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user