| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- namespace App\Model;
- use Illuminate\Database\Eloquent\Model;
- use App\Model\TahunProgram;
- use App\Model\Privillage\Role;
- use Auth;
- class TahunProgramDetail extends Model
- {
- protected $table = "tahun_program_detail";
- protected $fillable = [
- 'id_tahun_program',
- 'id_template_program',
- 'id_template_program_detail',
- 'tipe_target',
- 'satuan',
- 'satuan_target',
- 'jumlah',
- 'status'
- ];
- protected $appends = ['total_periode_active','total_periode','target_next_periode'];
- public function templateProgram(){
- return $this->belongsTo('App\Model\TemplateProgram', 'id_template_program');
- }
- public function templateProgramDetail(){
- return $this->belongsTo('App\Model\TemplateProgramDetail', 'id_template_program_detail');
- }
- public function tahunProgram(){
- return $this->belongsTo('App\Model\TahunProgram', 'id_tahun_program');
- }
-
- public function tahunProgramDetailUpt(){
- return $this->hasMany('App\Model\TahunProgramDetailUpt', 'id_tahun_program_detail');
- }
- public function metadataProgram(){
- return $this->hasMany('App\Model\MetadataProgram', 'id_tb_tahun_program_detail');
- }
- /**
- * @return mixed
- */
- public function getTotalActiveUptAttribute()
- {
- $total = 0;
- if(!empty($this->metadataProgram)){
- $total = $this->metadataProgram->unique('id_upt')->count();
- }
- return $total;
-
- }
- /**
- * @return mixed
- */
- public function getTotalPeriodeActiveAttribute()
- {
- $total_periode = 0;
- if(!empty($this->tahunProgram)){
- $total_periode = MetadataProgram::where('id_tb_tahun_program_detail',$this->id)->where('id_upt',Auth::user()->upt);
-
- $total_periode = $total_periode->count();
- }
- return $total_periode;
-
- }
- /**
- * @return mixed
- */
- public function getTotalPeriodeAttribute()
- {
- $total_periode = 0;
- if(!empty($this->tahunProgram)){
- $total_periode = TahunProgramDetail::where('id_template_program',$this->id_template_program)
- ->where('id_tahun_program',$this->id_tahun_program);
- $total_periode = $total_periode->count();
- }
- return $total_periode;
-
- }
- /**
- * @return mixed
- */
- public function getTotalTargetAttribute()
- {
- $target = 0;
- if(!empty($this->tahunProgramDetailUpt)){
- $getAkses = !empty(Auth::user())?Role::where('id',Auth::user()->role_id)->value('akses'):[];
- if(!empty($getAkses) && $getAkses=='administrator'){
- $target = $this->tahunProgramDetailUpt->sum('target');
- $jml_upt = $this->tahunProgramDetailUpt->unique('id_upt')->count();
- $target = !empty($jml_upt)?($target/$jml_upt):$target;
- }else{
- $target = $this->tahunProgramDetailUpt->where('id_upt',Auth::user()->upt)->sum('target');
- }
-
- }
- return $target;
-
- }
- /**
- * @return mixed
- */
- public function getTargetNextPeriodeAttribute()
- {
- $target = 0;
- $active_periode = $this->getTotalPeriodeActiveAttribute();
- $next_periode = ($active_periode+1);
- $tahunProgramDetailUpt = $this->tahunProgramDetailUpt->where('id_upt',Auth::user()->upt)->where('periode',$next_periode)->first();
- if(!empty($tahunProgramDetailUpt)){
- $target = $tahunProgramDetailUpt->target;
- }
- return $target;
-
- }
- }
|