TaxAccount.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace addons\AvoidTax\common\models;
  3. use Yii;
  4. use yii\db\ActiveQuery;
  5. /**
  6. * This is the model class for table "{{%addons_tax_account}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $book_type 1 税务方 2用工方
  11. * @property string|null $external_agreement_no 代扣协议中标示用户的唯一签约号(确保在商户系统中唯一)。
  12. * @property string|null $agreement_no 支付宝系统中用以唯一标识用户签约记录的编号。
  13. * @property string|null $alipay_logon_id 返回脱敏的支付宝账号,如需要返回不脱敏的支付宝用户账号,需要用户在签约页面上授权
  14. * @property string|null $personal_product_code 协议产品码,商户和支付宝签约时确定,不同业务场景对应不同的签约产品码。
  15. * @property int|null $valid_time 用户代扣协议的实际生效时间
  16. * @property int|null $sign_time 支付宝代扣协议的实际签约时间
  17. * @property int $is_default 是否默认
  18. * @property string|null $status 协议当前状态 1. TEMP:暂存,协议未生效过; 2. NORMAL:正常; 3. STOP:暂停
  19. * @property string|null $sign_scene 签约协议的场景。
  20. * @property string|null $third_party_type 签约第三方主体类型。对于三方协议,表示当前用户和哪一类的第三方主体进行签约。 取值范围: 1. PARTNER(平台商户); 2. MERCHANT(集团商户),集团下子商户可共享用户签约内容; 默认为PARTNER。
  21. * @property TaxAccountBook $book
  22. */
  23. class TaxAccount extends \yii\db\ActiveRecord
  24. {
  25. /**
  26. * 税务方
  27. */
  28. const TAXATION = 1;
  29. /**
  30. * 用工方
  31. */
  32. const EMPLOY = 2;
  33. /**
  34. * 九尾(过流水)
  35. */
  36. const PAYMENT_FLOW = 3;
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%addons_tax_account}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['mall_id', 'book_type', 'valid_time', 'sign_time', 'is_default'], 'integer'],
  51. [['external_agreement_no', 'agreement_no', 'status', 'third_party_type'], 'string', 'max' => 32],
  52. [['alipay_logon_id', 'personal_product_code', 'sign_scene'], 'string', 'max' => 64],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => 'Mall ID',
  63. 'book_type' => 'Book Type',
  64. 'external_agreement_no' => 'External Agreement No',
  65. 'agreement_no' => 'Agreement No',
  66. 'alipay_logon_id' => 'Alipay Logon ID',
  67. 'personal_product_code' => 'Personal Product Code',
  68. 'valid_time' => 'Valid Time',
  69. 'sign_time' => 'Sign Time',
  70. 'is_default' => 'Is Default',
  71. 'status' => 'Status',
  72. 'sign_scene' => 'Sign Scene',
  73. 'third_party_type' => 'Third Party Type',
  74. ];
  75. }
  76. /**
  77. * 获取关联记帐本
  78. *
  79. * @return ActiveQuery
  80. */
  81. public function getBook()
  82. {
  83. return $this->hasOne(TaxAccountBook::class, ['account_id' => 'id']);
  84. }
  85. /**
  86. * 查询协议产品码是否存在
  87. *
  88. * @param string $no
  89. * @return boolean
  90. */
  91. public static function noExists($no)
  92. {
  93. return empty(self::find()->select('id')->where(['external_agreement_no' => $no])->one())
  94. ? true
  95. : false;
  96. }
  97. /**
  98. * 获取一个商户签约号
  99. *
  100. * @return string
  101. */
  102. public static function getAgreementNo()
  103. {
  104. $no = date('YmdH') . rand(100, 999);
  105. return self::noExists($no)
  106. ? $no
  107. : self::getAgreementNo();
  108. }
  109. /**
  110. * 新增一个账户
  111. *
  112. * @param array $data
  113. * @return boolean
  114. */
  115. public static function addAccount($data)
  116. {
  117. $model = new self;
  118. $model->load($data, '');
  119. // return $model->save();
  120. return $model->save() ? $model : false;
  121. }
  122. /**
  123. * 获取默认账户
  124. *
  125. * @param integer $mall_id
  126. * @return boolean|TaxAccount
  127. */
  128. public static function getDefaultAccount(int $mall_id, int $book_type = TaxAccount::TAXATION)
  129. {
  130. return self::find()
  131. ->with('book')
  132. ->where(['mall_id' => $mall_id, 'is_default' => 1, 'book_type' => $book_type])
  133. ->one();
  134. }
  135. /**
  136. * id和type获取记账本
  137. *
  138. * @param integer $mall_id
  139. * @param integer $id
  140. * @param integer|array $type
  141. * @return static|null
  142. */
  143. public static function getAccountByIdAndType(int $mall_id, int $id, $type)
  144. {
  145. return static::find()
  146. ->where([
  147. 'mall_id' => $mall_id, 'book_type' => $type, 'id' => $id
  148. ])
  149. ->one();
  150. }
  151. /**
  152. * id获取未签约的账户
  153. *
  154. * @param integer $mall_id
  155. * @param integer $id
  156. * @return static|null
  157. */
  158. public static function getAccountByIdAndNotSign(int $mall_id, int $id)
  159. {
  160. return static::find()
  161. ->where(['mall_id' => $mall_id, 'id' => $id, 'status' => 0])
  162. ->one();
  163. }
  164. /**
  165. * 获取税务方默认记账本
  166. *
  167. * @param integer $mall_id
  168. * @return static|null
  169. */
  170. public static function getTaxation(int $mall_id)
  171. {
  172. return self::find()
  173. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::TAXATION, 'is_default' => 1])
  174. ->one();
  175. }
  176. /**
  177. * 获取用工方默认记账本
  178. *
  179. * @param integer $mall_id
  180. * @return static|null
  181. */
  182. public static function getEmploy(int $mall_id)
  183. {
  184. return self::find()
  185. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::EMPLOY, 'is_default' => 1])
  186. ->one();
  187. }
  188. /**
  189. * 获取流水方默认记账本
  190. *
  191. * @param integer $mall_id
  192. * @return static|null
  193. */
  194. public static function getPaymentFlow(int $mall_id)
  195. {
  196. return self::find()
  197. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::PAYMENT_FLOW, 'is_default' => 1])
  198. ->one();
  199. }
  200. /**
  201. * 判断是否为税务方记账本
  202. *
  203. * @return boolean
  204. */
  205. public function isTaxation()
  206. {
  207. return $this->book_type == TaxAccount::TAXATION;
  208. }
  209. /**
  210. * 该记账本是过流水记账本
  211. *
  212. * @return boolean
  213. */
  214. public function isPaymentFlow()
  215. {
  216. return $this->book_type == TaxAccount::PAYMENT_FLOW;
  217. }
  218. }