<?php
class report_personals_score_graf extends c_report
{
 var $title='Сердечно-сосудистый риск у сотрудников (SCORE) - графики' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
    public $use_fusioncharts=1 ;
 var $group_ids=0 ;
 var $arr_grafs=array(0=>'Проценты') ;

    function init($options=array())
    {
       // if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;
    }

 function setting($options=array())
 {   if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
     //$usl_groups=(MEMBER()->rol>2 and MEMBER()->cur_group_id!=113)? ' and group_id in ('.ACCOUNTS()->tree[MEMBER()->cur_group_id]->get_list_child().') ':'';
     //$recs_orgs=execSQL('select * from obj_site_personals where parent=1 and clss=207 '.$usl_groups.' order by obj_name') ;

     $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
     $arr_orgs_ids=PERSONAL()->get_arr_orgs_to_cur_account('',array('view_disabled'=>$this->filter['status'])) ;
     $arr_otdel_ids=(_sizeof($this->filter['org_ids']))? PERSONAL()->get_arr_otdels_to_cur_account($this->filter['org_ids']):array() ;


     ?><div id="panel_filter">
             <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                  <fieldset>
                      <div class="row">
                          <? $this->filter_section_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                             $this->filter_section_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                             $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                             $this->filter_section_select('mode','График:',2,$this->arr_grafs) ;
                          ?>
                      </div>
                  </fieldset>
                  <?$this->panel_button_apply();?>
              </form>
       </div><br>
     <?
  }



  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='clss=211' ;
     if ($this->filter['group_id'])
     { $rec_group=ACCOUNTS()->get_workgroup_by_id($this->filter['group_id']) ;
       if (_sizeof($rec_group['_settings']['personal']['filter']))  $usl['group_personal_filter']=$rec_group['_settings']['personal']['usl_select'] ;
     }
     if (MEMBER()->personal_viewer_name) $usl[]='pkey in (select pkey from '.MEMBER()->personal_viewer_name.')' ;

     if ($this->filter['org_ids'])          $usl[]='parent2 in ('.implode(',',array_keys($this->filter['org_ids'])).')' ;
     if ($this->filter['otdel_ids'])        $usl[]='parent3 in ('.implode(',',array_keys($this->filter['otdel_ids'])).')' ;

     if ($this->filter['org_id'])        $usl[]='_org_id="'.$this->filter['org_id'].'"' ;
     if ($this->filter['otdel_id'])      $usl[]='_otdel_id in ('.$this->filter['otdel_id'].')' ;

     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function get_arr_info_filters()
 { $info=parent::get_arr_info_filters() ;
   if ($this->filter['status']==1)                      $info['статус']='"уволенные"' ;
   else if ($this->filter['status']==2)                 $info['статус']='"все"' ;
   if ($this->filter['ext']==1)                         $info['Доп.списки']='Да' ;
   if ($this->filter['score_from'])                     $info['Мин.значение SCORE']=$this->filter['score_from'] ;
   if (MEMBER()->setting['personal']['usl_select'])     $info[MEMBER()->setting['personal']['name']]=strip_tags(MEMBER()->setting['personal']['info_filter']);
   //damp_array(MEMBER()->setting['personal']) ;
   return $info ;
 }

 function HTML_report_body($options=array())
  {        ?><div id="reports_itog2"><?
              $this->panel_diagram_by_all_personal() ;
              $this->panel_diagram_by_work() ;
           ?></div><?
  }

 function panel_diagram_by_all_personal()
 {     // риск ССЗ по всем сотрудникам
       $recs=execSQL_row('select score,count(pkey) from obj_site_personals where clss=211 and enabled=1 group by score order by score') ;
       $cnt_all=execSQL_value('select count(pkey) from obj_site_personals where clss=211 and enabled=1') ;
       $data=array() ; $str_data=array() ;
       $data['менее 1%']=$recs[0] ;
       $data['1%']=$recs[1] ;
       $data['2%']=$recs[2] ;
       $data['3%-4%']=$recs[3]+$recs[4] ;
       $data['5%-9%']=$recs[5]+$recs[6]+$recs[7]+$recs[8]+$recs[9] ;
       $data['10%-14%']=$recs[10]+$recs[11]+$recs[12]+$recs[13]+$recs[14] ;
       if (_sizeof($recs)) foreach($recs as $score=>$cnt) if ($score>=15) $data['15% и более']+=$score ;
       if (_sizeof($data)) foreach($data as $score=>$cnt) $str_data[]='{"label": "'.$score.'","value": "'.$cnt.'"}' ;

       //damp_array($str_data) ;
     ?>  <div id="panel_diagram_by_all_personal">FusionCharts will render here</div>
         <script type="text/javascript">
          $j(document).ready(function(){
           FusionCharts.ready(function(){

               var revenueChart = new FusionCharts({
                                   type: 'doughnut2d',
                                   renderAt: 'panel_diagram_by_all_personal',
                                   width: '450',
                                   height: '450',
                                   dataFormat: 'json',
                                   dataSource: {
                                       "chart": {
                                           "caption": "Диаграмма риска сердечно-сосудистых заболеваний",
                                           "subCaption": "по всем сотрудникам",
                                           "numberPrefix": "",
                                           "paletteColors": "#00b050,#92d050,#ffff00,#ffc000,#ff0000,#e20000,#c00000",
                                           "bgColor": "#ffffff",
                                           "showBorder": "0",
                                           "use3DLighting": "0",
                                           "showShadow": "0",
                                           "enableSmartLabels": "1",
                                           "startingAngle": "310",
                                           "showLabels": "0",
                                           "showPercentValues": "1",
                                           "showLegend": "1",
                                           "legendShadow": "0",
                                           "legendBorderAlpha": "0",
                                           "defaultCenterLabel": "Всего сотрудников: <?echo $cnt_all?>",
                                           "centerLabel": "Сотрудников $label: $value",
                                           "centerLabelBold": "1",
                                           "showTooltip": "1",
                                           "decimals": "0",
                                           "captionFontSize": "14",
                                           "subcaptionFontSize": "14",
                                           "subcaptionFontBold": "0"
                                       },
                                       "data": [<? if (_sizeof($str_data)) echo implode(',',$str_data) ;?>]
                                   }
                               }) ;
        revenueChart.render();


    });
          }) ;
         </script>
         <style type="text/css">#chart-container{margin:0 auto;width:1000px;}</style>
       <?
 }

 function panel_diagram_by_work()
 {     // риск ССЗ по всем сотрудникам
     ?><div id="panel_diagram_by_work">FusionCharts will render here</div><?
       $recs=execSQL_row('select working,round(avg(score)) as score_avg from obj_site_personals where clss=211 and enabled=1 and working>0 group by working having score_avg>0 order by score_avg') ;
       $cnt_all=execSQL_value('select count(pkey) from obj_site_personals where clss=211 and enabled=1') ;
       $data=array() ; $str_data=array() ; $arr_colors=array() ;

       if (_sizeof($recs)) foreach($recs as $work_id=>$score)
       {  $str_data[]='{"label": "'._IL('IL_works')->recs[$work_id]['obj_name'].'","value": "'.$score.'"}' ;
          $color='#000000' ;
          if ($score>=15) $color='#c00000' ;
          elseif ($score>=10) $color='#e20000' ;
          elseif ($score>=5) $color='#ff0000' ;
          elseif ($score>=3) $color='#ffc000' ;
          elseif ($score>=2) $color='#ffff00' ;
          elseif ($score>=1) $color='#92d050' ;
          elseif ($score>=0) $color='#00b050' ;
          $arr_colors[]=$color ;
       }

       //damp_array($str_data) ;
     ?>  <script type="text/javascript">
         $j(document).ready(function(){
           FusionCharts.ready(function(){

               var revenueChart = new FusionCharts({
                                   type: 'doughnut2d',
                                   renderAt: 'panel_diagram_by_work',
                                   width: '800',
                                   height: '800',
                                   dataFormat: 'json',
                                   dataSource: {
                                       "chart": {
                                           "caption": "Диаграмма риска сердечно-сосудистых заболеваний",
                                           "subCaption": "по всем сотрудникам",
                                           "numberPrefix": "",
                                           "paletteColors": "<?echo implode(',',$arr_colors)?>",
                                           "bgColor": "#ffffff",
                                           "showBorder": "0",
                                           "use3DLighting": "0",
                                           "showShadow": "0",
                                           "enableSmartLabels": "1",
                                           "startingAngle": "310",
                                           "showLabels": "0",
                                           "showPercentValues": "1",
                                           "showLegend": "1",
                                           "legendShadow": "0",
                                           "legendBorderAlpha": "0",
                                           "defaultCenterLabel": "Всего сотрудников: <?echo $cnt_all?>",
                                           "centerLabel": "Сотрудников $label: $value",
                                           "centerLabelBold": "1",
                                           "showTooltip": "1",
                                           "decimals": "0",
                                           "captionFontSize": "14",
                                           "subcaptionFontSize": "14",
                                           "subcaptionFontBold": "0"
                                       },
                                       "data": [<? if (_sizeof($str_data)) echo implode(',',$str_data) ;?>]
                                   }
                               }) ;
        revenueChart.render();


    });
         }) ;
         </script>
         <style type="text/css">#chart-container{margin:0 auto;width:1000px;}</style>
       <?
 }






}

?>
