diff --git a/app/AddTunai.php b/app/AddTunai.php index 2aadf68..635f389 100644 --- a/app/AddTunai.php +++ b/app/AddTunai.php @@ -13,7 +13,7 @@ class AddTunai extends Model * * @var array */ - protected $fillable = ['tarikh','kodcaw','kodlaluan','tambah','kurang','nota']; + protected $fillable = ['tarikh','kodcaw','kodlaluan','tambah','kurang','nota','created_at']; public $timestamps = false; /** diff --git a/app/Http/Controllers/TunaiController.php b/app/Http/Controllers/TunaiController.php index 904d2ec..aa93a4c 100644 --- a/app/Http/Controllers/TunaiController.php +++ b/app/Http/Controllers/TunaiController.php @@ -11,7 +11,7 @@ use App\Denominasi; use App\Vault; use Illuminate\Http\Request; use Carbon\Carbon; - +use Illuminate\Support\Facades\Log; class TunaiController extends Controller { @@ -194,7 +194,28 @@ class TunaiController extends Controller $current = new Carbon(); $cawangan = Cawangan::on('mysql7')->where('kodcaw','=',$kodcaw)->first(); + //Same Note e.g P1 and S1 + $same_note = AddTunai::on($cawangan['mysql']) + ->where('kodlaluan', $request->kodlaluan) + ->where('nota',$request->nota) + ->where('tarikh',$current->toDateString())->first(); + if($same_note){ + Log::error('Error: same-note :' . $kodcaw . ': Name :' . $request->kodlaluan); + return 'same-note'; + } + + //Time Interval + $latestRecord = AddTunai::on($cawangan['mysql'])->where('kodlaluan', $request->kodlaluan)->latest()->first(); + if($latestRecord){ + $time = Carbon::parse($latestRecord->created_at); + $time_end = $time->copy()->addMinutes(1); + + if($current->between($time,$time_end)){ + Log::error('Error: within 1 minute :' . $kodcaw . ': Name :' . $request->kodlaluan); + return 'within 1 minute'; + } + } $addtunai=AddTunai::on($cawangan['mysql'])->create([ @@ -203,7 +224,8 @@ class TunaiController extends Controller 'kodlaluan'=>$request->kodlaluan, 'tambah'=>$request->tambah, 'kurang'=>$request->kurang, - 'nota'=>$request->nota + 'nota'=>$request->nota, + 'created_at'=>$current ]); return response()->json($addtunai,201); diff --git a/database/migrations/2024_09_02_153041_add_column_timestamp_to_addtunai_table.php b/database/migrations/2024_09_02_153041_add_column_timestamp_to_addtunai_table.php new file mode 100644 index 0000000..d47036c --- /dev/null +++ b/database/migrations/2024_09_02_153041_add_column_timestamp_to_addtunai_table.php @@ -0,0 +1,73 @@ +getPdo(); + return true; + } catch(\Exception $e){ + return false; + } + }); + return $db_connection; + } + + public function up() + { + $this->db_connection = $this->getActiveDB(); + + foreach($this->db_connection as $dbase){ + Schema::connection($dbase)->table('addtunai', function (Blueprint $table) use($dbase) { + + $isCreatedAt = Schema::connection($dbase)->hasColumn('addtunai', 'created_at'); + + if (! $isCreatedAt) { + $table->timestamp('created_at'); + } + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + $this->db_connection = $this->getActiveDB(); + + foreach($this->db_connection as $dbase){ + $columns = ['created_at']; + + $columns_exist = array_filter($columns,function($column) use($dbase){ + return Schema::connection($dbase)->hasColumn('addtunai', $column); + }); + + Schema::connection($dbase)->table('addtunai', function(Blueprint $table) use($columns_exist){ + foreach($columns_exist as $column){ + $table->dropColumn($column); + } + }); + } + } +}