TaxAccountBook.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\AvoidTax\common\models;
  3. /**
  4. * This is the model class for table "{{%addons_tax_account_book}}".
  5. *
  6. * @property int $id
  7. * @property int $account_id 账户ID
  8. * @property int|null $mall_id
  9. * @property int $book_type 1 税务方 2用工方
  10. * @property int|null $is_default 是否默认 0 1
  11. * @property string|null $merchant_user_id
  12. * @property string|null $account_book_id
  13. * @property string|null $ext_card_info
  14. * @property int|null $created_at
  15. * @property string|null $agreement_no
  16. */
  17. class TaxAccountBook extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * 外部商户系统会员的唯一标识前缀
  21. */
  22. const PREFIX = 'book_';
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_tax_account_book}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['account_id', 'mall_id', 'book_type', 'is_default', 'created_at'], 'integer'],
  37. [['ext_card_info'], 'string'],
  38. [['merchant_user_id', 'account_book_id', 'agreement_no'], 'string', 'max' => 64],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function attributeLabels()
  45. {
  46. return [
  47. 'id' => 'ID',
  48. 'account_id' => 'Account ID',
  49. 'mall_id' => 'Mall ID',
  50. 'book_type' => 'Book Type',
  51. 'is_default' => 'Is Default',
  52. 'merchant_user_id' => 'Merchant User ID',
  53. 'account_book_id' => 'Account Book ID',
  54. 'ext_card_info' => 'Ext Card Info',
  55. 'created_at' => 'Created At',
  56. 'agreement_no' => 'Agreement No',
  57. ];
  58. }
  59. /**
  60. * 查询外部商户系统会员的唯一标识是否存在
  61. *
  62. * @param string $account_book_id
  63. * @return boolean
  64. */
  65. public static function bookIdExists($account_book_id)
  66. {
  67. return empty(self::find()->select('id')->where(['account_book_id' => $account_book_id])->one())
  68. ? true
  69. : false;
  70. }
  71. /**
  72. * 外部商户系统会员的唯一标识
  73. *
  74. * @return string
  75. */
  76. public static function getMerchantUserId()
  77. {
  78. $book_id = self::PREFIX . date('YmdHi') . rand(100, 999);
  79. return self::bookIdExists($book_id)
  80. ? $book_id
  81. : self::getMerchantUserId();
  82. }
  83. /**
  84. * 添加记帐本
  85. *
  86. * @param array $data
  87. * @return boolean
  88. */
  89. public static function addBook($data)
  90. {
  91. // 是否有默认的记账本
  92. $book = self::find()->where(['mall_id' => $data['mall_id'], 'is_default' => 1])->one();
  93. if (empty($book)) {
  94. $data['is_default'] = 1;
  95. }
  96. $data = array_merge($data, [
  97. 'created_at' => time(),
  98. ]);
  99. $model = new self();
  100. $model->load($data, '');
  101. return $model->save();
  102. }
  103. /**
  104. * 获取默认记账本
  105. *
  106. * @param int $mall_id
  107. * @return self
  108. */
  109. public static function byMallGetBook($mall_id)
  110. {
  111. return self::find()
  112. ->where(['mall_id' => $mall_id, 'book_type' => 1, 'is_default' => 1])
  113. ->one();
  114. }
  115. /**
  116. * 获取税务方默认记账本
  117. *
  118. * @param integer $mall_id
  119. * @return static|null
  120. */
  121. public static function getTaxation(int $mall_id)
  122. {
  123. return self::find()
  124. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::TAXATION, 'is_default' => 1])
  125. ->one();
  126. }
  127. /**
  128. * 获取用工方默认记账本
  129. *
  130. * @param integer $mall_id
  131. * @return static|null
  132. */
  133. public static function getEmploy(int $mall_id)
  134. {
  135. return self::find()
  136. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::EMPLOY, 'is_default' => 1])
  137. ->one();
  138. }
  139. /**
  140. * 获取流水方默认记账本
  141. *
  142. * @param integer $mall_id
  143. * @return static|null
  144. */
  145. public static function getPaymentFlow(int $mall_id)
  146. {
  147. return self::find()
  148. ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::PAYMENT_FLOW, 'is_default' => 1])
  149. ->one();
  150. }
  151. }