fix invoice bugs

This commit is contained in:
Omair Saleh
2023-09-01 04:32:37 +08:00
parent 85ca64ac39
commit 39d8ed2e9e
@@ -3,6 +3,7 @@
namespace App\Classes\Modules\Transactions\Services;
use App\Classes\Exceptions\InternalServerErrorException;
use Carbon\Carbon;
class GeneratesTransactionBillNumber
@@ -27,14 +28,21 @@ class GeneratesTransactionBillNumber
* @return string
*/
public function execute(string $prefix, ?Carbon $date = null): string {
if(!$date){
$date = carbon::now();
if (!$date) {
$date = Carbon::now();
}
$billNumber = $prefix.$date->format('Y').$date->format('m').'-'.mt_rand(10000, 99999);
return !$this->checksIfTransactionBillNumberExists->execute($billNumber) ? $billNumber : self::execute($prefix);
$attempt = 0;
while ($attempt < 10) { // Retry up to 10 times
$billNumber = $prefix . $date->format('Y') . $date->format('m') . '-' . microtime(true);
if (!$this->checksIfTransactionBillNumberExists->execute($billNumber)) {
return $billNumber;
}
$attempt++;
}
throw new InternalServerErrorException("Unable to generate unique bill number after {$attempt} attempts.");
}
}
}