<?
namespace cab\aspmo;
use i_db_object;

class i_med_org extends i_db_object
{
    static $table_name='catalog_med_orgs';
    static $clss=307;

    public static function create($form_data, $options=array())
    {
        ob_start();

        // формируем запись для базы
        $data=array();
        $data['clss']=static::$clss;
        $data['parent']=0;
        $data['uid']=ENGINE()->generate_UID();
        $data['director_uid']=ENGINE()->generate_UID();
        $data['obj_name']=$form_data['name'];

        $id=adding_rec_to_table(static::$table_name, $data);
        $result['id']=$id;

        // проверка существования записи в базе
        $new_rec=execSQL_van('select pkey,tkey,obj_name from ' . static::$table_name . ' where pkey=' . $result['id']);
        if ($new_rec['pkey'])
        {
            $result['reffer']=$new_rec['pkey'] . '.' . $new_rec['tkey'];
            $result['status']='success';
            $result['info']='Успешно - "<strong>' . $result['id'] . '</strong>"';
        }
        else
        {
            $result['status']='failer';
            $result['info']='Не удалось добавить запись';
        }

        $text=ob_get_clean();
        $result['debug']=$text;
        return ($result);
    }


    public static function save($pkey, $form_data, $options=[])
    {
        $cur_rec=static::get_rec_by_id($pkey);
        $data=[];
        //if (isset($form_data['uid'])) $data['uid']=$form_data['uid'];
        if (isset($form_data['uid_1c'])) $data['uid_1c']=$form_data['uid_1c'];
        if (isset($form_data['name'])) $data['obj_name']=$form_data['name'];
        if (isset($form_data['full_name'])) $data['full_name']=$form_data['full_name'];
        if (isset($form_data['inn'])) $data['inn']=$form_data['inn'];
        if (isset($form_data['kpp'])) $data['kpp']=$form_data['kpp'];
        if (isset($form_data['ogrn'])) $data['ogrn']=mb_substr($form_data['ogrn'], 0, 13);
        if (isset($form_data['phone'])) $data['phone']=$form_data['phone'];
        if (isset($form_data['email'])) $data['email']=$form_data['email'];
        if (isset($form_data['adres1'])) $data['adres1']=$form_data['adres1'];
        if (isset($form_data['adres2'])) $data['adres2']=$form_data['adres2'];
        if (isset($form_data['license_number'])) $data['license_number']=$form_data['license_number'];
        if (isset($form_data['license_seria'])) $data['license_seria']=$form_data['license_seria'];
        if (isset($form_data['license_data_from'])) $data['license_data_from']=(strtotime($form_data['license_data_from']))?:0;
        if (isset($form_data['license_data_to'])) $data['license_data_to']=(strtotime($form_data['license_data_to']))?:0;

        if (isset($form_data['director_name'])) $data['director_name']=$form_data['director_name'];
        if (isset($form_data['director_inn'])) $data['director_inn']=$form_data['director_inn'];
        if (isset($form_data['director_snils'])) $data['director_snils']=$form_data['director_snils'];
        if (isset($form_data['director_job_title'])) $data['director_job_title']=$form_data['director_job_title'];
        //if (isset($form_data['director_date_birth'])) $data['director_date_birth']=$form_data['director_date_birth'];
        //if (isset($form_data['director_place_birth'])) $data['director_place_birth']=$form_data['director_place_birth'];
        //if (isset($form_data['director_gender'])) $data['director_gender']=$form_data['director_gender'];
        if (isset($form_data['director_email'])) $data['director_email']=$form_data['director_email'];
        if (isset($form_data['director_phone'])) $data['director_phone']=$form_data['director_phone'];
        if (!$cur_rec['director_uid']) $data['director_uid']=ENGINE()->generate_UID();
        damp_array($data);
        $result=update_rec_in_table(static::$table_name, $data, 'pkey=' . $pkey);
        if (!$result['warning'])
        { $result['status']='success';
          $result['result']=true;
        }
        return ($result);

        /*
         * $descr_class[307]['fields']['inn']=array('type'=>'varchar(16)') ;
             $descr_class[307]['fields']['kpp']=array('type'=>'varchar(16)') ;
             $descr_class[307]['fields']['ogrn']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okpo']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okato']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['oktmo']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okogu']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okfs']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okved']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['okveds']=array('type'=>'varchar(13)') ;
             $descr_class[307]['fields']['adres1']=array('type'=>'varchar(255)') ;
             $descr_class[307]['fields']['adres2']=array('type'=>'varchar(255)') ;
         * */
    }

    public static function delete($pkey, $options=[])
    {
        $cnt=execSQL_update('delete from ' . static::$table_name . ' where pkey=' . $pkey);
        $result['status']='success';
        return ($result);
    }

}

?>
