TahunProgramDetail.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Model\TahunProgram;
  5. use App\Model\Privillage\Role;
  6. use Auth;
  7. class TahunProgramDetail extends Model
  8. {
  9. protected $table = "tahun_program_detail";
  10. protected $fillable = [
  11. 'id_tahun_program',
  12. 'id_template_program',
  13. 'id_template_program_detail',
  14. 'tipe_target',
  15. 'satuan',
  16. 'satuan_target',
  17. 'jumlah',
  18. 'status'
  19. ];
  20. protected $appends = ['total_periode_active','total_periode','target_next_periode'];
  21. public function templateProgram(){
  22. return $this->belongsTo('App\Model\TemplateProgram', 'id_template_program');
  23. }
  24. public function templateProgramDetail(){
  25. return $this->belongsTo('App\Model\TemplateProgramDetail', 'id_template_program_detail');
  26. }
  27. public function tahunProgram(){
  28. return $this->belongsTo('App\Model\TahunProgram', 'id_tahun_program');
  29. }
  30. public function tahunProgramDetailUpt(){
  31. return $this->hasMany('App\Model\TahunProgramDetailUpt', 'id_tahun_program_detail');
  32. }
  33. public function metadataProgram(){
  34. return $this->hasMany('App\Model\MetadataProgram', 'id_tb_tahun_program_detail');
  35. }
  36. /**
  37. * @return mixed
  38. */
  39. public function getTotalActiveUptAttribute()
  40. {
  41. $total = 0;
  42. if(!empty($this->metadataProgram)){
  43. $total = $this->metadataProgram->unique('id_upt')->count();
  44. }
  45. return $total;
  46. }
  47. /**
  48. * @return mixed
  49. */
  50. public function getTotalPeriodeActiveAttribute()
  51. {
  52. $total_periode = 0;
  53. if(!empty($this->tahunProgram)){
  54. $total_periode = MetadataProgram::where('id_tb_tahun_program_detail',$this->id)->where('id_upt',Auth::user()->upt);
  55. $total_periode = $total_periode->count();
  56. }
  57. return $total_periode;
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getTotalPeriodeAttribute()
  63. {
  64. $total_periode = 0;
  65. if(!empty($this->tahunProgram)){
  66. $total_periode = TahunProgramDetail::where('id_template_program',$this->id_template_program)
  67. ->where('id_tahun_program',$this->id_tahun_program);
  68. $total_periode = $total_periode->count();
  69. }
  70. return $total_periode;
  71. }
  72. /**
  73. * @return mixed
  74. */
  75. public function getTotalTargetAttribute()
  76. {
  77. $target = 0;
  78. if(!empty($this->tahunProgramDetailUpt)){
  79. $getAkses = !empty(Auth::user())?Role::where('id',Auth::user()->role_id)->value('akses'):[];
  80. if(!empty($getAkses) && $getAkses=='administrator'){
  81. $target = $this->tahunProgramDetailUpt->sum('target');
  82. $jml_upt = $this->tahunProgramDetailUpt->unique('id_upt')->count();
  83. $target = !empty($jml_upt)?($target/$jml_upt):$target;
  84. }else{
  85. $target = $this->tahunProgramDetailUpt->where('id_upt',Auth::user()->upt)->sum('target');
  86. }
  87. }
  88. return $target;
  89. }
  90. /**
  91. * @return mixed
  92. */
  93. public function getTargetNextPeriodeAttribute()
  94. {
  95. $target = 0;
  96. $active_periode = $this->getTotalPeriodeActiveAttribute();
  97. $next_periode = ($active_periode+1);
  98. $tahunProgramDetailUpt = $this->tahunProgramDetailUpt->where('id_upt',Auth::user()->upt)->where('periode',$next_periode)->first();
  99. if(!empty($tahunProgramDetailUpt)){
  100. $target = $tahunProgramDetailUpt->target;
  101. }
  102. return $target;
  103. }
  104. }