<?php
class report_magnit_scan_actions extends c_report
{
 var $title='Сканы документов' ;
 var $view_by_space_filter=1 ;
 var $no_view_title=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 var $doc_types_names=array() ;
    var $allow_export_to_csv=1 ;

 function setting($options=array())
 {
   //damp_array(ENGINE()->reports_list['napravlenie']['']) ;
      ?><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">
                          <?if (MEMBER()->rol!=2)
                          {$arr_groups=execSQL_row('select pkey,obj_name,clss from obj_site_account where clss=206')?>
                            <section class="col col-2">
                              <label class="label">АТП:</label>
                              <label class="select">
                                  <select name="filter[group_id]"><option value="0">Все</option><?
                                    if (_sizeof($arr_groups)) foreach($arr_groups as $id=>$name) {?><option value="<?echo $id?>" <?if($this->filter['group_id']==$id) echo 'selected'?>><?echo $name?></option><?}?>
                                  </select>
                              </label>
                             </section>
                              <section class="col col-2">
                                  <label class="label">Фельдшер:</label>
                                  <label class="select">
                                      <select id=doctor_id name="filter[doctor_id]"><option value="0">Все</option>
                                          <? $members=ACCOUNTS()->get_list_members() ;
                                             if (_sizeof($members))foreach($members as $rec_member) if ($rec_member['rol']==2 or $rec_member['rol']==3)
                                            {?><option value="<?echo $rec_member['pkey']?>" <?if ($this->filter['doctor_id']==$rec_member['pkey']) echo 'selected'?>><?echo $rec_member['_rol_name'].' '.$rec_member['obj_name']?></option><?}
                                          ?>
                                      </select><i></i>
                                  </label>
                              </section><?
                          }?>
                          <section class="col col-2">
                              <label class="label">Дата приема скана, от:</label>
                              <label class="input"><input type="text" name="filter[data_from]" class="datepicker" value="<?echo $this->filter['data_from']?>"></label>
                          </section>
                          <section class="col col-2">
                              <label class="label">Дата приема скана, до:</label>
                              <label class="input"><input type="text" name="filter[data_to]" class="datepicker" value="<?echo $this->filter['data_to']?>"></label>
                          </section>
                        <section class="col col-2">
                             <label class="label">Статус:</label>
                             <label class="select">
                                 <select name="filter[status]"><option></option><?
                                     if (_sizeof($_SESSION['ARR_status_scan'])) foreach($_SESSION['ARR_status_scan'] as $id=>$name) {?><option value="<?echo $id?>" <?if ($id==$this->filter['status']) echo 'selected'?>><?echo $name?></option><?} ?>
                                 </select><i></i>
                             </label>
                           </section>
                          <section class="col col-1">
                              <label class="label">Номер НАГ</label>
                              <label class="input">
                                  <input type="text" name="filter[doc_number]" placeholder="" value="<?echo $this->filter['doc_number']?>">
                              </label>
                          </section>
                        <section class="col col-1">
                             <label class="label">Источник</label>
                             <label class="checkbox"><input type="radio" name="filter[is_sign]" value="1" <?if ($this->filter['is_sign']==1) echo 'checked'?>><i></i> Почта</label>
                             <label class="checkbox"><input type="radio" name="filter[is_sign]" value="2" <?if ($this->filter['is_sign']==2) echo 'checked'?>><i></i> Файл</label>
                           </section>
                        </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function create_usl_select_obj($_params,&$options)
  {  //damp_array($this->filter) ;
     $usl=array() ;
     $usl[]='clss=218' ;
     $usl[]='parent=1' ;
     $usl[]='status in (1,2,5,6)' ;
     $usl[]='(group_upload=0 or group_upload in ('.MEMBER()->cur_groups_visible.') or account_upload='.MEMBER()->id.')' ;

     if ($this->filter['group_id'])        $usl[]='group_id='.$this->filter['group_id'] ;
     if ($this->filter['doctor_id'])        $usl[]='member_id='.$this->filter['doctor_id'] ;
     if ($this->filter['doc_type'])           $usl[]='report_code="'.$this->filter['doc_type'].'"' ;
     if ($this->filter['doc_number'])           $usl[]='indx="'.$this->filter['doc_number'].'"' ;
     if ($this->filter['is_sign']==1)           $usl[]='sign_member>0' ;
     if ($this->filter['is_sign']==2)           $usl[]='sign_member=0' ;
     if ($this->filter['data_from'])        $usl[]='c_data>='.strtotime($this->filter['data_from']) ;
     if ($this->filter['data_to'])          $usl[]='c_data<='.strtotime($this->filter['data_to']) ;
     if ($this->filter['personal_id'])        $usl[]='personal_id='.$this->filter['personal_id'] ;
     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;
   $info['Группа загрузки']=MEMBER()->cur_group_name ;
   if ($this->filter['doctor_id'])       $info['Фельдшер']=ACCOUNTS()->get_member_name_by_id($this->filter['doctor_id']) ;
   if ($this->filter['doc_type'])                       $info['Тип документа']=$this->doc_types_names[$this->filter['doc_type']] ;
   if ($this->filter['doc_number'])                       $info['№']=$this->filter['doc_number'] ;
   if ($this->filter['is_sign']==1)           $info['Подпись']='Есть' ;
   if ($this->filter['is_sign']==2)          $info['Подпись']='Нет' ;
     if ($this->filter['data_from'])                     $info['дата от']='"'.$this->filter['data_from'].'"' ;
     if ($this->filter['data_to'])                     $info['дата до']='"'.$this->filter['data_to'].'"' ;
   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }

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

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
 	$list_rec=execSQL('select *
                       from  view_scan
                       where '.$usl.$order.$limit,$options) ;
    if (_sizeof($list_rec)) foreach($list_rec as $id=>$rec) DOCS()->prepare_public_info_to_scan($list_rec[$id],$options) ;
    return($list_rec) ;
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  { $id=($options['id'])? 'id='.$options['id']:'' ;
    $recs_account=execSQL_row('select pkey,obj_name from obj_site_account where clss=210') ;
    ?><table class="basic fz_small full auto"<?echo $id?>>
        <tr><th>№</th><th>Дата/время<br>загрузки</th><th>АТП загрузки</th><th>Источник</th><th>Дата/время<br>обработки</th><th>Обрабатывается</th><th>Статус</th><th>Действия</th></tr><?
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   if ($rec['status']==2 and !$rec['account_work'])
              { execSQL_update('update obj_site_scan set status=1 where pkey='.$rec['pkey']) ;
                $rec['status']=1 ;
              }
              ?><tr <?if ($options['selected'] and $options['selected']==$rec['pkey']) echo 'class=selected'?>>
                  <td><?echo $rec['pkey']?></td>
                  <td><?echo date('d.m.Y H:i',$rec['c_data'])?><??></td>
                  <td><?echo $rec['_group_upload_name']?><??></td>
                  <td class="left"><?echo $rec['_source']?></td>
                  <td><?if ($rec['data_work']) echo date('d.m.Y H:i',$rec['data_work'])?></td>
                  <td><?echo $rec['_account_work_name']?></td>
                  <td><?echo $_SESSION['ARR_status_scan'][$rec['status']]?></td>
                  <td><?
                        if ($rec['status']==1){?><button class="v2" cmd=esmo_magnit/work_scan scan_id="<?echo $rec['pkey']?>">Обработать</button><?}
                        /*if (($rec['status']==2 or $rec['status']==5) and $rec['account_work']==MEMBER()->id){?><button class="v2" cmd=esmo_magnit/work_scan scan_id="<?echo $rec['pkey']?>">Продолжить обработку</button><?}*/
                	if (($rec['status']==2 or $rec['status']==5 or $rec['status']==6))
                        {   echo 'Отв.: '.ACCOUNTS()->get_member_name_by_id($rec['account_work']).'<br>' ;
                            ?><button class="v2" cmd=esmo_magnit/work_scan scan_id="<?echo $rec['pkey']?>">Продолжить обработку</button><?
                        }
                      ?></td>
              </tr><?
          }
      ?></table>
        <style type="text/css">
           table.basic tr:hover td{background:#f1f1f1;}
           table.basic tr.selected td{background: #ccffe3;}
        </style>
      <?
      //damp_array($rec) ;
  }

  function print_template_CSV($list_recs,$options=array())
   { $i=$options['from'] ;
     $recs_account=execSQL_row('select pkey,obj_name from obj_site_account where clss=210') ;
     if (_sizeof($list_recs)) foreach($list_recs as $rec)
       { //$sign_info=($rec['sign'])? unserialize($rec['sign']):array() ;
         $data=array() ;
         $data['id']=$rec['pkey'] ;
         $data['mo_id']=$rec['mo_id'] ;
         $data['Дата/время']=date('d.m.Y H:i:s',$rec['c_data']) ;
         $data['АТП']=$rec['_group_name'] ;
         $data['Код документа']=$rec['report_code'] ;
         $data['Наименование документа']=$rec['obj_name'] ;
         $data['Подпиcан']=($rec['sign'])? 'Да':'' ;
         $data['Автор документа']=$recs_account[$rec['member_id']] ;
         $data['ФИО сотрудника']=$rec['_personal_name'] ;
         $data['Дата рождения']=$rec['_personal_dr'] ;
         $data['Организация']=$rec['_personal_org_name'] ;

         $this->put_data_to_CSV($data,$i) ;
         $i++ ;
       }

   }

}

?>
