TemplateProgramDetail.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. class TemplateProgramDetail extends Model
  8. {
  9. protected $table = 'template_program_detail';
  10. protected $fillable = [
  11. 'id_template_program',
  12. 'nama',
  13. 'created_at',
  14. 'updated_at'
  15. ];
  16. public function templateProgram(){
  17. return $this->belongsTo('App\Model\TemplateProgram', 'id_template_program');
  18. }
  19. public function itemProgram(){
  20. return $this->hasMany('App\Model\ItemProgram', 'id_template_program_detail');
  21. }
  22. /**
  23. * @return mixed
  24. */
  25. public function getTotalPeriodeActiveAttribute()
  26. {
  27. $total_periode = 0;
  28. if(!empty($this->tahunProgramDetail)){
  29. $total_periode = TahunProgramDetail::where('id_template_program',$this->id);
  30. $year = date('Y');
  31. $total_periode = $total_periode->whereHas('tahunProgram',function($q) use($year){
  32. $q->where('value',$year);
  33. });
  34. //if user not admin
  35. if(!empty(Auth::user())){
  36. $getStatus = Role::where('id',Auth::user()->role_id)->value('akses');
  37. if(!empty($getStatus) && $getStatus!='administrator'){
  38. $id_upt = Auth::user()->upt;
  39. $total_periode = $total_periode->where('unit',$id_upt);
  40. }
  41. }
  42. $total_periode = $total_periode->count();
  43. }
  44. return $total_periode;
  45. }
  46. /**
  47. * @return mixed
  48. */
  49. public function getTotalPeriodeAttribute()
  50. {
  51. $total_periode = 0;
  52. if(!empty($this->tahunProgramDetail)){
  53. $total_periode = $this->tahunProgramDetail;
  54. //if user not admin
  55. if(!empty(Auth::user())){
  56. $getStatus = Role::where('id',Auth::user()->role_id)->value('akses');
  57. if(!empty($getStatus) && $getStatus!='administrator'){
  58. $id_upt = Auth::user()->upt;
  59. $total_periode = $total_periode->where('unit',$id_upt);
  60. }
  61. }
  62. $total_periode = $total_periode->count();
  63. }
  64. return $total_periode;
  65. }
  66. }