TemplateProgram.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Model;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Model\Privillage\Role;
  5. use App\Model\TahunProgramDetail;
  6. use Auth;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. class TemplateProgram extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table = 'template_program';
  12. protected $fillable = [
  13. 'nama',
  14. 'status',
  15. 'created_by',
  16. 'modified_by',
  17. 'logo',
  18. 'is_required'
  19. ];
  20. public function itemProgram(){
  21. return $this->hasMany('App\Model\ItemProgram', 'id_template');
  22. }
  23. public function templateProgramDetail(){
  24. return $this->hasMany('App\Model\TemplateProgramDetail', 'id_template_program');
  25. }
  26. public function templateProgramRole(){
  27. return $this->hasMany('App\Model\TemplateProgramRole', 'id_template_program');
  28. }
  29. public function tahunProgram(){
  30. return $this->belongsToMany('App\Model\TahunProgram', 'tb_tahun_program_detail','id_tahun_program', 'id_template_program');
  31. }
  32. public function tahunProgramDetail(){
  33. return $this->hasMany('App\Model\TahunProgramDetail', 'id_template_program');
  34. }
  35. /**
  36. * @return mixed
  37. */
  38. public function getTotalPeriodeActiveAttribute()
  39. {
  40. $total_periode = 0;
  41. if(!empty($this->tahunProgramDetail)){
  42. $total_periode = TahunProgramDetail::where('id_template_program',$this->id);
  43. $year = date('Y');
  44. $total_periode = $total_periode->whereHas('tahunProgram',function($q) use($year){
  45. $q->where('value',$year);
  46. });
  47. //if user not admin
  48. if(!empty(Auth::user())){
  49. $getStatus = Role::where('id',Auth::user()->role_id)->value('akses');
  50. if(!empty($getStatus) && $getStatus!='administrator'){
  51. $id_upt = Auth::user()->upt;
  52. $total_periode = $total_periode->where('unit',$id_upt);
  53. }
  54. }
  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->tahunProgramDetail)){
  66. $total_periode = $this->tahunProgramDetail;
  67. //if user not admin
  68. if(!empty(Auth::user())){
  69. $getStatus = Role::where('id',Auth::user()->role_id)->value('akses');
  70. if(!empty($getStatus) && $getStatus!='administrator'){
  71. $id_upt = Auth::user()->upt;
  72. $total_periode = $total_periode->where('unit',$id_upt);
  73. }
  74. }
  75. $total_periode = $total_periode->count();
  76. }
  77. return $total_periode;
  78. }
  79. }