/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

  1. <?php
  2. namespace common\models\organization;
  3. use common\models\organization\parent\Organizations;
  4. use Yii;
  5. /**
  6. * This is the model class for table "individual_entrepreneurs".
  7. *
  8. * @property integer $id
  9. * @property string $head
  10. * @property string $OGRN
  11. * @property string $OGRN_date
  12. * @property string $INN
  13. * @property string $address
  14. * @property string $RS
  15. * @property string $rs_bank
  16. * @property string $KS
  17. * @property string $BIK
  18. * @property string $vicarious_authority
  19. * @property string $fact_address
  20. * @property string $phones
  21. * @property integer $organization_id
  22. *
  23. * @property Organizations $organization
  24. */
  25. class IndividualEntrepreneurs extends \yii\db\ActiveRecord
  26. {
  27. private $CRC11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  28. private $CRC12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8, 0];
  29. 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];
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return 'individual_entrepreneurs';
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['head', 'OGRN'/*, 'OGRN_date'*/, 'INN', 'address', 'RS', 'KS', 'BIK', 'vicarious_authority',
  44. 'fact_address'], 'required'],
  45. [['organization_id', 'id'], 'integer'],
  46. [['head', 'address', 'rs_bank', 'vicarious_authority', 'fact_address', 'phones'], 'string', 'max' => 255],
  47. [['OGRN'], 'string', 'min' => 15, 'max' => 15],
  48. [['OGRN'], 'unique'],
  49. [['OGRN'], 'ogrnValidator'],
  50. [['INN'], 'string', 'min' => 12, 'max' => 12],
  51. [['INN'], 'unique'],
  52. [['INN'], 'innValidator'],
  53. [['RS'], 'string', 'min' => 20, 'max' => 20],
  54. [['RS'], 'rsValidator',],
  55. [['KS'], 'string', 'min' => 20, 'max' => 20],
  56. [['KS'], 'ksValidator',],
  57. [['BIK'], 'string', 'min' => 9,'max' => 9]
  58. ];
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => Yii::t('app', 'ID'),
  67. 'head' => Yii::t('app', 'Head'),
  68. 'OGRN' => Yii::t('app', 'Ogrn (OGRNIP)'),
  69. 'INN' => Yii::t('app', 'INN'),
  70. 'address' => Yii::t('app', 'Address'),
  71. 'RS' => Yii::t('app', 'Rs'),
  72. 'rs_bank' => Yii::t('app', 'Rs Bank'),
  73. 'KS' => Yii::t('app', 'Ks'),
  74. 'BIK' => Yii::t('app', 'BIK'),
  75. 'vicarious_authority' => Yii::t('app', 'Vicarious Authority'),
  76. 'fact_address' => Yii::t('app', 'Fact Address'),
  77. 'phones' => Yii::t('app', 'Phones'),
  78. 'organization_id' => Yii::t('app', 'Organization ID'),
  79. ];
  80. }
  81. /**
  82. * @return \yii\db\ActiveQuery
  83. */
  84. public function getOrganization()
  85. {
  86. return $this->hasOne(Organizations::className(), ['id' => 'organization_id']);
  87. }
  88. public function ogrnValidator($attribute, $params){
  89. preg_match('/(\d{14})(\d)/', $this->OGRN, $data);
  90. $tmp1 = floor($data[1] / 13);
  91. $tmp2 = $tmp1 * 13;
  92. $tmp3 = $data[1] - $tmp2;
  93. $tmp4 = ($tmp3 == $data[2]);
  94. if(!$tmp1 || !$tmp2 || !$tmp3)
  95. $this->addError($attribute, 'OGRN is not correct.');
  96. if($tmp4 !== true)
  97. $this->addError($attribute, 'OGRN is not correct.');
  98. }
  99. public function innValidator($attribute, $params){
  100. preg_match('/(\d)(\d)$/', $this->INN, $data);
  101. $crc = 0;
  102. $controlNum1 = null;
  103. $controlNum2 = null;
  104. $inn = (string)$this->INN;
  105. for($i = 0; $i < 10; $i ++){
  106. $crc += $inn[$i] * $this->CRC11[$i];
  107. }
  108. $controlNum1 = $crc % 11;
  109. if($controlNum1 > 9){
  110. $controlNum1 = $crc % 10;
  111. }
  112. $crc = 0;
  113. for($i = 0; $i < 12; $i ++){
  114. $crc += $inn[$i] * $this->CRC12[$i];
  115. }
  116. $controlNum2 = $crc % 11;
  117. if($controlNum2 > 9){
  118. $controlNum2 = $crc % 10;
  119. }
  120. if($controlNum1 != $data[1] || $controlNum2 != $data[2])
  121. $this->addError($attribute, 'INN is not correct.');
  122. }
  123. public function rsValidator($attribute, $params){
  124. /*
  125. * Для кредитных учреждений, БИК которых оканчивается на 000 и 001 (РКЦ и ГРКЦ),
  126. * 23х-значный номер для проверки формируется немного иначе, чем для других банков.
  127. * Сначала должен идти "0", потом 5 и 6 цифры БИК, потом 20-значный номер счета.
  128. * В остальном проверка аналогична.
  129. * */
  130. preg_match('/(\d{3})$/', $this->BIK, $data);
  131. $rs = null;
  132. if($data[1] == '000' || $data[1] == '001'){
  133. $bik = (string)$this->BIK;
  134. $rs = '0' . $bik[4] . $bik[5] . $this->RS;
  135. }else{
  136. $rs = $data[1] . $this->RS;
  137. }
  138. $crc = 0;
  139. $controlNum = null;
  140. for($i = 0; $i < 23; $i ++){
  141. $crc += $rs[$i] * $this->CRCRS[$i];
  142. }
  143. $controlNum = $crc % 10;
  144. if($controlNum != 0)
  145. $this->addError($attribute, 'RS or BIK is not correct.');
  146. }
  147. public function ksValidator($attribute, $params){
  148. $bik = (string)$this->BIK;
  149. $ks = '0' . $bik[4] . $bik[5] . $this->KS;
  150. $crc = 0;
  151. $controlNum = null;
  152. for($i = 0; $i < 23; $i ++){
  153. $crc += $ks[$i] * $this->CRCRS[$i];
  154. }
  155. $controlNum = $crc % 10;
  156. if($controlNum != 0)
  157. $this->addError($attribute, 'KS or BIK is not correct.');
  158. }
  159. // public function beforeSave($insert)
  160. // {
  161. // if (parent::beforeSave($insert)) {
  162. // $organization = new Organizations();
  163. // $relations = new RelationsUserOrganization();
  164. //
  165. // $organization->parent_id = null;
  166. // $organization->owner_type_id = $this->getOwnerTypeID('IP');
  167. //
  168. // $connection = Yii::$app->db;
  169. // $transaction = $connection->beginTransaction();
  170. //
  171. // try {
  172. // $organization->save();
  173. //
  174. // $this->organization_id = $organization->id;
  175. //
  176. // $relations->organization_id = $organization->id;
  177. // $relations->user_id = Yii::$app->user->id;
  178. // $relations->save();
  179. //
  180. // $transaction->commit();
  181. //
  182. // return true;
  183. // } catch (\Exception $e) {
  184. // $transaction->rollBack();
  185. //
  186. // return false;
  187. // }
  188. // }
  189. //
  190. // return false;
  191. // }
  192. //
  193. // protected function getOwnerTypeID($name){
  194. // return OwnerTypes::find()->select('id')->where("name = '$name'")->one()->id;
  195. // }
  196. }