123 lines
3.4 KiB
PHP
123 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
use DB;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class RegNumber extends Model
|
|
{
|
|
const INC = 1;
|
|
const OUT = 2;
|
|
const ITR = 3;
|
|
const TMP = 4;
|
|
|
|
protected $fillable = ['reg_number', 'user_id'];
|
|
static $doctype = ['1' => 'seq_inc', '2'=>'seq_out', '3'=>'seq_int', '4' => 'seq_tmp'];
|
|
static $prefix = ['1' => 'registration_number_prefix', '2'=>'outgoing_registration_number_prefix', '3'=>'internal_registration_number_prefix', '4' => 'temporary_registration_number_prefix'];
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function getIsCancelledAttribute($attribute)
|
|
{
|
|
return ($attribute == 1) ? 'Yes' : 'No';
|
|
}
|
|
|
|
public static function updateRegNumber($rn)
|
|
{
|
|
if(DB::table('reg_numbers')->where('reg_number', $rn)->exists())
|
|
DB::table('reg_numbers')->where('reg_number', $rn)->update(['is_cancelled'=>0]);
|
|
}
|
|
public static function getNextRegNumber($userid, $dt)
|
|
{
|
|
|
|
if(isset(self::$doctype[$dt]))
|
|
{
|
|
$setting = Setting::first();
|
|
$vprefix = self::$prefix[$dt];
|
|
|
|
$registration_number= (($setting->$vprefix && $setting->$vprefix!='') ? $setting->$vprefix.'-' : '') . static::getRegFormat($setting->is_registration_number_with_date) . '-' . sprintf('%04d', (static::getNextSeq($dt)));
|
|
|
|
if(RegNumber::create(['user_id' => $userid, 'reg_number' => $registration_number]))
|
|
return $registration_number;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static function getRegFormat($val)
|
|
{
|
|
$reg_format = [
|
|
'0' => 'PRX-YYYY-MM-DD-SEQ',
|
|
'1' => 'PRX-YYYY-MM-SEQ',
|
|
'2' => 'PRX-YYYY-SEQ',
|
|
'3' => 'PRX-SEQ'
|
|
];
|
|
$res='';
|
|
if($val == 0)
|
|
$res = date('Y-m-d');
|
|
else if($val == 1)
|
|
$res = date('Y-m');
|
|
else if($val == 2)
|
|
$res = date('Y');
|
|
|
|
return $res;
|
|
}
|
|
|
|
public static function getLatestSeq($dt)
|
|
{
|
|
if(isset(self::$doctype[$dt]))
|
|
{
|
|
$dtype = self::$doctype[$dt];
|
|
return DB::table('reg_number_seq')->first()->$dtype;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function getNextSeq($dt)
|
|
{
|
|
if(isset(self::$doctype[$dt]))
|
|
{
|
|
$dtype = self::$doctype[$dt];
|
|
try {
|
|
$seq = static::getLatestSeq($dt)+1;
|
|
DB::table('reg_number_seq')->update([$dtype=>$seq]);
|
|
return $seq;
|
|
} catch (\Throwable $th) {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function resetSeqNumber($seq, $type=null)
|
|
{
|
|
if($type == null)
|
|
{
|
|
try{
|
|
DB::table('reg_number_seq')->update([
|
|
'seq_inc' => $seq,
|
|
'seq_out' => $seq,
|
|
'seq_int' => $seq,
|
|
'seq_tmp' => $seq
|
|
]);
|
|
} catch(\Throwable $th){
|
|
return null;
|
|
}
|
|
}
|
|
else if(isset(self::$doctype[$type]))
|
|
{
|
|
$dtype = self::$doctype[$type];
|
|
try {
|
|
DB::table('reg_number_seq')->update([$dtype=>$seq]);
|
|
} catch (\Throwable $th) {
|
|
return null;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|