/common/models/organization/IndividualEntrepreneurs.php
https://gitlab.com/aintenebris/memoria · PHP · 234 lines · 121 code · 38 blank · 75 comment · 23 complexity · 53d399ad2d9b51a0286b6ca56cc29235 MD5 · raw file
- <?php
- namespace common\models\organization;
- use common\models\organization\parent\Organizations;
- use Yii;
- /**
- * This is the model class for table "individual_entrepreneurs".
- *
- * @property integer $id
- * @property string $head
- * @property string $OGRN
- * @property string $OGRN_date
- * @property string $INN
- * @property string $address
- * @property string $RS
- * @property string $rs_bank
- * @property string $KS
- * @property string $BIK
- * @property string $vicarious_authority
- * @property string $fact_address
- * @property string $phones
- * @property integer $organization_id
- *
- * @property Organizations $organization
- */
- class IndividualEntrepreneurs extends \yii\db\ActiveRecord
- {
- private $CRC11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
- private $CRC12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
- private $CRCRS = [7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1, 3, 7, 1];
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'individual_entrepreneurs';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['head', 'OGRN'/*, 'OGRN_date'*/, 'INN', 'address', 'RS', 'KS', 'BIK', 'vicarious_authority',
- 'fact_address'], 'required'],
- [['organization_id', 'id'], 'integer'],
- [['head', 'address', 'rs_bank', 'vicarious_authority', 'fact_address', 'phones'], 'string', 'max' => 255],
- [['OGRN'], 'string', 'min' => 15, 'max' => 15],
- [['OGRN'], 'unique'],
- [['OGRN'], 'ogrnValidator'],
- [['INN'], 'string', 'min' => 12, 'max' => 12],
- [['INN'], 'unique'],
- [['INN'], 'innValidator'],
- [['RS'], 'string', 'min' => 20, 'max' => 20],
- [['RS'], 'rsValidator',],
- [['KS'], 'string', 'min' => 20, 'max' => 20],
- [['KS'], 'ksValidator',],
- [['BIK'], 'string', 'min' => 9,'max' => 9]
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => Yii::t('app', 'ID'),
- 'head' => Yii::t('app', 'Head'),
- 'OGRN' => Yii::t('app', 'Ogrn (OGRNIP)'),
- 'INN' => Yii::t('app', 'INN'),
- 'address' => Yii::t('app', 'Address'),
- 'RS' => Yii::t('app', 'Rs'),
- 'rs_bank' => Yii::t('app', 'Rs Bank'),
- 'KS' => Yii::t('app', 'Ks'),
- 'BIK' => Yii::t('app', 'BIK'),
- 'vicarious_authority' => Yii::t('app', 'Vicarious Authority'),
- 'fact_address' => Yii::t('app', 'Fact Address'),
- 'phones' => Yii::t('app', 'Phones'),
- 'organization_id' => Yii::t('app', 'Organization ID'),
- ];
- }
- /**
- * @return \yii\db\ActiveQuery
- */
- public function getOrganization()
- {
- return $this->hasOne(Organizations::className(), ['id' => 'organization_id']);
- }
- public function ogrnValidator($attribute, $params){
- preg_match('/(\d{14})(\d)/', $this->OGRN, $data);
- $tmp1 = floor($data[1] / 13);
- $tmp2 = $tmp1 * 13;
- $tmp3 = $data[1] - $tmp2;
- $tmp4 = ($tmp3 == $data[2]);
- if(!$tmp1 || !$tmp2 || !$tmp3)
- $this->addError($attribute, 'OGRN is not correct.');
- if($tmp4 !== true)
- $this->addError($attribute, 'OGRN is not correct.');
- }
- public function innValidator($attribute, $params){
- preg_match('/(\d)(\d)$/', $this->INN, $data);
- $crc = 0;
- $controlNum1 = null;
- $controlNum2 = null;
- $inn = (string)$this->INN;
- for($i = 0; $i < 10; $i ++){
- $crc += $inn[$i] * $this->CRC11[$i];
- }
- $controlNum1 = $crc % 11;
- if($controlNum1 > 9){
- $controlNum1 = $crc % 10;
- }
- $crc = 0;
- for($i = 0; $i < 12; $i ++){
- $crc += $inn[$i] * $this->CRC12[$i];
- }
- $controlNum2 = $crc % 11;
- if($controlNum2 > 9){
- $controlNum2 = $crc % 10;
- }
- if($controlNum1 != $data[1] || $controlNum2 != $data[2])
- $this->addError($attribute, 'INN is not correct.');
- }
- public function rsValidator($attribute, $params){
- /*
- * Для кредитных учреждений, БИК которых оканчивается на 000 и 001 (РКЦ и ГРКЦ),
- * 23х-значный номер для проверки формируется немного иначе, чем для других банков.
- * Сначала должен идти "0", потом 5 и 6 цифры БИК, потом 20-значный номер счета.
- * В остальном проверка аналогична.
- * */
- preg_match('/(\d{3})$/', $this->BIK, $data);
- $rs = null;
- if($data[1] == '000' || $data[1] == '001'){
- $bik = (string)$this->BIK;
- $rs = '0' . $bik[4] . $bik[5] . $this->RS;
- }else{
- $rs = $data[1] . $this->RS;
- }
- $crc = 0;
- $controlNum = null;
- for($i = 0; $i < 23; $i ++){
- $crc += $rs[$i] * $this->CRCRS[$i];
- }
- $controlNum = $crc % 10;
- if($controlNum != 0)
- $this->addError($attribute, 'RS or BIK is not correct.');
- }
- public function ksValidator($attribute, $params){
- $bik = (string)$this->BIK;
- $ks = '0' . $bik[4] . $bik[5] . $this->KS;
- $crc = 0;
- $controlNum = null;
- for($i = 0; $i < 23; $i ++){
- $crc += $ks[$i] * $this->CRCRS[$i];
- }
- $controlNum = $crc % 10;
- if($controlNum != 0)
- $this->addError($attribute, 'KS or BIK is not correct.');
- }
- // public function beforeSave($insert)
- // {
- // if (parent::beforeSave($insert)) {
- // $organization = new Organizations();
- // $relations = new RelationsUserOrganization();
- //
- // $organization->parent_id = null;
- // $organization->owner_type_id = $this->getOwnerTypeID('IP');
- //
- // $connection = Yii::$app->db;
- // $transaction = $connection->beginTransaction();
- //
- // try {
- // $organization->save();
- //
- // $this->organization_id = $organization->id;
- //
- // $relations->organization_id = $organization->id;
- // $relations->user_id = Yii::$app->user->id;
- // $relations->save();
- //
- // $transaction->commit();
- //
- // return true;
- // } catch (\Exception $e) {
- // $transaction->rollBack();
- //
- // return false;
- // }
- // }
- //
- // return false;
- // }
- //
- // protected function getOwnerTypeID($name){
- // return OwnerTypes::find()->select('id')->where("name = '$name'")->one()->id;
- // }
- }