Accounts.php 8.13 KB
<?php

namespace common\models;

use Yii;
use yii\web\IdentityInterface;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
/**
 * This is the model class for table "w_accounts".
 *
 *
 * @property integer $id
 * @property integer $if_manager
 * @property string $email
 * @property string $pass
 * @property integer $margin_id
 * @property string $name
 * @property string $phones
 * @property integer $country
 * @property integer $city
 * @property string $address
 * @property string $comment
 * @property integer $rating
 * @property string $dt
 * @property integer $is_active
 * @property integer $is_firm
 * @property string $last_loginin
 * @property string $firm_inn
 * @property string $firm_bank
 * @property double $balance
 * @property integer $office_id
 * @property integer $is_scribe
 * @property integer $set_manager_id
 * @property string $phones2
 * @property string $phones3
 * @property integer $car
 * @property integer $mod
 * @property string $snumb
 * @property integer $deliveries
 * @property integer $scode
 * @property string $firm_ur_adr
 * @property string $firm_fiz_adr
 * @property string $firm_code_eg
 * @property string $firm_rs
 * @property string $firm_mfo
 * @property string $firm_site
 * @property string $company
 */
class Accounts extends ActiveRecord implements IdentityInterface
{

    public $re_pass;
    public $surname;
    public $country_region;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'w_accounts';
    }


    public function beforeSave()
    {
        $this->dt = time();
        return true;

    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['if_manager', 'margin_id', 'country', 'city', 'rating',  'is_active', 'is_firm', 'office_id', 'is_scribe', 'set_manager_id', 'car', 'mod', 'deliveries', 'scode'], 'integer'],
            [['company','email', 'pass', 'name','surname', 'phones',], 'required'],
            [['comment'], 'string'],
            [['balance'], 'number'],
            [['email', 'name','surname', 'firm_site'], 'string', 'max' => 150],
            [['pass','re_pass'], 'string', 'max' => 30],
            [['phones', 'phones2', 'phones3'], 'string', 'max' => 50],
            [['address', 'firm_inn', 'firm_bank'], 'string', 'max' => 254],
            [['last_loginin'], 'string', 'max' => 15],
            [['snumb', 'firm_ur_adr', 'firm_fiz_adr', 'firm_code_eg', 'firm_rs', 'firm_mfo', 'company'], 'string', 'max' => 255],
            [['email'], 'unique'],
            [['email'], 'email'],
            ['re_pass', 'compare', 'compareAttribute' => 'pass'],
            ['dt', 'date', 'format' => 'Y.m.d']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'if_manager' => 'Статус менеджера',
            'email' => 'E-mail (Логин)',
            'pass' => 'Пароль',
            'margin_id' => 'Тип цены',
            'name' => 'Имя',
            'phones' => 'Телефоны',
            'country' => Yii::t('app', 'Country'),
            'city' =>'Город',
            'address' => 'Адрес',
            'comment' => 'Комментарий',
            'rating' => Yii::t('app', 'Rating'),
            'dt' =>'Дата регистрации',
            'is_active' => 'Активный',
            'is_firm' => 'Юридическое лицо',
            'last_loginin' => Yii::t('app', 'Last Loginin'),
            'firm_inn' => 'ИНН',
            'firm_bank' => 'Банк',
            'balance' => Yii::t('app', 'Balance'),
            'office_id' => Yii::t('app', 'Office ID'),
            'is_scribe' => 'Подписка',
            'set_manager_id' => 'Персональный менеджер',
            'phones2' => 'Телефоны 2',
            'phones3' => 'Телефоны 3',
            'car' => Yii::t('app', 'Car'),
            'mod' => Yii::t('app', 'Mod'),
            'snumb' => 'snumb',
            'deliveries' => Yii::t('app', 'Deliveries'),
            'scode' => 'Код в 1С',
            'firm_ur_adr' => 'Юридический адрес',
            'firm_fiz_adr' => 'Физический адрес',
            'firm_code_eg' => 'Код ЭГ',
            'firm_rs' => 'Расчётный счёт',
            'firm_mfo' => 'МФО',
            'firm_site' => 'Сайт',
            'company' => 'Название фирмы',
            'country_region' => 'Регион'
        ];
    }


    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {

        if ($this->validate()) {
            $this->name = $this->surname.' '.$this->name;

            if( $this->is_firm ){
                $this->if_manager = '0';
                $this->margin_id = '1';
                $this->address = '';
                $this->comment = 'Новый пользователь';
                $this->rating = '0';
                $this->is_active = '1';
                $this->last_loginin = '';
                $this->firm_inn = '';
                $this->firm_bank ='';
                $this->balance ='0.00';
                $this->office_id ='1';
                $this->is_scribe ='1';
                $this->set_manager_id ='0';
                $this->phones2='';
                $this->phones3 ='';
                $this->snumb ='';
                $this->firm_ur_adr ='';
                $this->firm_fiz_adr = '';
                $this->firm_code_eg = '';
                $this->firm_rs = '';
                $this->firm_mfo = '';
                $this->firm_site = '';
            }else{

                $this->if_manager = '0';
                $this->margin_id = '1';
                $this->address = '';
                $this->comment = 'Новый пользователь';
                $this->rating = '0';
                $this->is_active = '1';
                $this->last_loginin = '';
                $this->firm_inn = '';
                $this->firm_bank ='';
                $this->balance ='0.00';
                $this->office_id ='1';
                $this->is_scribe ='1';
                $this->set_manager_id ='0';
                $this->phones2='';
                $this->phones3 ='';
                $this->snumb ='';
                $this->firm_ur_adr ='';
                $this->firm_fiz_adr = '';
                $this->firm_code_eg = '';
                $this->firm_rs = '';
                $this->firm_mfo = '';
                $this->firm_site = '';
            }

            if ($this->save()) {
                return $this;
            }
        }

        return null;
    }
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['name' => $username]);
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }



    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }
}