fix duplicate calculation ids
This commit is contained in:
@@ -56,7 +56,38 @@ class RetributionCalculation extends Model
|
|||||||
*/
|
*/
|
||||||
public static function generateCalculationId(): string
|
public static function generateCalculationId(): string
|
||||||
{
|
{
|
||||||
return 'CALC-' . date('Ymd') . '-' . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
|
$maxAttempts = 10;
|
||||||
|
$attempt = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
// Use microseconds for better uniqueness but keep within 20 char limit
|
||||||
|
// Format: CALC-YYYYMMDD-XXXXX (20 chars exactly)
|
||||||
|
$microseconds = (int) (microtime(true) * 1000) % 100000; // 5 digits max
|
||||||
|
$id = 'CALC-' . date('Ymd') . '-' . str_pad($microseconds, 5, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
|
// Check if ID already exists
|
||||||
|
if (!self::where('calculation_id', $id)->exists()) {
|
||||||
|
return $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$attempt++;
|
||||||
|
// Add small delay to ensure different microsecond values
|
||||||
|
usleep(1000); // 1ms delay
|
||||||
|
|
||||||
|
} while ($attempt < $maxAttempts);
|
||||||
|
|
||||||
|
// Fallback to random 5-digit number if all attempts fail
|
||||||
|
for ($i = 0; $i < 100; $i++) {
|
||||||
|
$random = mt_rand(10000, 99999);
|
||||||
|
$id = 'CALC-' . date('Ymd') . '-' . $random;
|
||||||
|
|
||||||
|
if (!self::where('calculation_id', $id)->exists()) {
|
||||||
|
return $id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final fallback - use current timestamp seconds
|
||||||
|
return 'CALC-' . date('Ymd') . '-' . str_pad(time() % 100000, 5, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user