<?php
class report_pl_med_orgs extends c_report
{
 var $title='Справочник медицинских организаций (ПЛ)' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 var $allow_export_to_csv=1 ;
 var $group_ids=0 ;
 var $arr_status=[] ;

 function setting($options=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_input_text('obj_name','Наименование:',2,array('_auto_complete'=>"autocomplette_personal_name")) ;
                             $this->filter_section_input_text('email','Email:',2) ;
                             $this->filter_section_input_text('phone','Телефон:',2) ;
                             $this->filter_section_input_text('inn','ИНН:',2) ;
                             $this->filter_section_input_text('address','Адрес:',2) ;
                          ?>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='true' ;
     //damp_array($this->arr_status);
     if (_sizeof($this->arr_status)) $usl[]='status in ('.implode(',',$this->arr_status).')' ;
     //$usl[]='skd_id>0' ;
     if ($this->filter['obj_name'])          $usl[]='t1.obj_name like "%'.$this->filter['obj_name'].'%"' ;
     if ($this->filter['inn'])           $usl[]='t1.inn="'.$this->filter['inn'].'"' ;
     if ($this->filter['email'])           $usl[]='t1.email like "%'.$this->filter['email'].'%"' ;
     if ($this->filter['phone'])           $usl[]='t1.phone like "%'.$this->filter['phone'].'%"' ;
     if ($this->filter['address'])           $usl[]='t1.client_info like "%'.$this->filter['address'].'%"' ;
     $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['obj_name'])                 $info['Наименование']=$this->filter['obj_name'] ;
   if ($this->filter['inn'])                 $info['ИНН']=$this->filter['inn'] ;
   if ($this->filter['email'])                 $info['Email']=$this->filter['email'] ;
   if ($this->filter['phone'])                 $info['Телефон']=$this->filter['phone'] ;
   if ($this->filter['address'])                 $info['Адрес']=$this->filter['address'] ;
   return $info ;
 }

  function get_cnt_items($usl,$options=array())
  {  $cnt=execSQL_value('select count(pkey)
                         from  catalog_med_orgs t1
                         where '.$usl,$options) ;
    return($cnt) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=' order by t1.pkey desc' ;
    $list_recs=execSQL('select t1.* 
                       from  catalog_med_orgs t1
                       where '.$usl.$order.$limit,$options) ;
    //if (_sizeof($list_recs)) foreach($list_recs as $id=>$rec) PL()->prepare_public_info_to_client($list_recs[$id]) ;
    return($list_recs) ;
  }


    // шаблон - вывод ссылками без разделитея
   function print_template_HTML($list_recs,$options=array())
    { $id=($options['id'])? 'id='.$options['id']:'' ;
      ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
        <tr>
            <th>ID</th>
            <th>UID</th>
            <th>Наименование организации</th>
            <th>Контакты</th>
            <th>Реквизиты</th>
            <th>Адрес</th>
            <th>Лицензия</th>
            <th>Рабочая группа ЭСМО</th>
            <th></th>
        </tr><? ;
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   //$stats=execSQL_row('select clss,count(pkey) from obj_site_personals where parent2='.$rec['pkey'].' and enabled=1 and _enabled=1 group by clss') ;
              $arr_groups=execSQL_row('select pkey,obj_name from obj_site_account where clss=206 and med_org_id='.$rec['pkey']);
              ?><tr class=item>
                  <td><?echo $rec['pkey']?></td>
                  <td class="left">
                      <strong>UID ЭСМО:</strong> <?echo $rec['uid']?><br>
                      <strong>UID 1C:</strong> <?echo $rec['uid_1c']?>
                  </td>
                  <td class="left">
                      <strong>Краткое:</strong> <?echo $rec['obj_name']?><br>
                      <strong>Полное:</strong> <?echo $rec['full_name']?><br>
                  </td>
                  <td class="left">
                      <strong>Email:</strong> <?echo $rec['email']?><br>
                      <strong>Телефон:</strong> <?echo $rec['phone']?><br>
                  </td>
                  <td class="left">
                      <strong>ИНН:</strong> <?echo $rec['inn']?><br>
                      <strong>КПП:</strong> <?echo $rec['kpp']?><br>
                      <strong>ОГРН:</strong> <?echo $rec['ogrn']?><br>
                  </td>
                  <td class="left">
                      <strong>Адрес юридический:</strong> <?echo $rec['adres1']?><br>
                      <strong>Адрес фактический:</strong> <?echo $rec['adres2']?><br>
                  </td>
                  <td class="left">
                      <strong>Лицензия серия:</strong> <?echo $rec['license_seria']?><br>
                      <strong>Лицензия номер:</strong> <?echo $rec['license_number']?><br>
                      <strong>Срок действия:</strong> <?echo date('d.m.Y',$rec['license_data_from']).' - '.date('d.m.Y',$rec['license_data_to'])?><br>
                  </td>
                  <td><? if (sizeof($arr_groups)) echo implode('<br>',$arr_groups)?></td>
                  <td>
                       <button class="v1" href="/cab/medical_organizations/card/<?echo $rec['pkey']?>/">Редактировать</button><br>
                  </td>
              </tr><?
          }
        ?></table>
        <script type="text/javascript">
            $j('select.select_group').on('change',function()
            {   send_ajax_request({cmd:'aspmo_personal/set_org_group',org_id:$j(this).attr('org_id'),group_id:$j(this).val()});

            })  ;
        </script>

        <?
        //print_2x_arr($list_recs) ;
    }

	function view_as_XML($options=array())
    { header("Content-type: text/xml;charset=UTF-8") ;
      $imp = new DOMImplementation; // Creates an instance of the DOMImplementation class
      $this->xml_doc = $imp->createDocument("",""); // Creates a DOMDocument instance
      $this->xml_doc->encoding = 'utf-8';
      $this->xml_doc->formatOutput = true;
      //$this->xml = add_element($this->xml_doc,$this->xml_doc,'xml','',array('version'=>"1.0",'encoding'=>"utf-8",'date'=>date('Y-m-d H:i'))) ;
      $this->xml = add_element($this->xml_doc,$this->xml_doc,'report_med_orgs','',array('date'=>date('Y-m-d H:i'))) ;
      $this->show_list_items('','print_template_XML',$options) ;
      echo $this->xml_doc->saveXML() ;
    }

	function print_template_XML($list_recs,$options=array())
	{ if (_sizeof($list_recs)) foreach($list_recs as $rec)
		{   $item=add_element($this->xml_doc,$this->xml,'item','',array('uid'=>$rec['uid'])) ;
				add_element($this->xml_doc,$item,'name_short_with_opf',$rec['obj_name']) ;
				add_element($this->xml_doc,$item,'name_full_with_opf',$rec['full_name']) ;
				add_element($this->xml_doc,$item,'inn',$rec['inn']) ;
				add_element($this->xml_doc,$item,'kpp',$rec['kpp']) ;
				add_element($this->xml_doc,$item,'ogrn',$rec['ogrn']) ;
				add_element($this->xml_doc,$item,'email',$rec['email']) ;
				add_element($this->xml_doc,$item,'phone',str_replace(['+','(',')','-'], '', $rec['phone'])) ;
				add_element($this->xml_doc,$item,'adres1',$rec['adres1']) ;
				add_element($this->xml_doc,$item,'adres2',$rec['adres2']) ;
				add_element($this->xml_doc,$item,'license_seria',$rec['license_seria']) ;
				add_element($this->xml_doc,$item,'license_number',$rec['license_number']) ;
				add_element($this->xml_doc,$item,'license_data_from',date('d.m.Y',$rec['license_data_from'])) ;
				add_element($this->xml_doc,$item,'license_data_to',date('d.m.Y',$rec['license_data_to'])) ;
		}
	}

}

?>
