| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace addons\AvoidTax\common\models;
- /**
- * This is the model class for table "{{%addons_tax_account_book}}".
- *
- * @property int $id
- * @property int $account_id 账户ID
- * @property int|null $mall_id
- * @property int $book_type 1 税务方 2用工方
- * @property int|null $is_default 是否默认 0 1
- * @property string|null $merchant_user_id
- * @property string|null $account_book_id
- * @property string|null $ext_card_info
- * @property int|null $created_at
- * @property string|null $agreement_no
- */
- class TaxAccountBook extends \yii\db\ActiveRecord
- {
- /**
- * 外部商户系统会员的唯一标识前缀
- */
- const PREFIX = 'book_';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_tax_account_book}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['account_id', 'mall_id', 'book_type', 'is_default', 'created_at'], 'integer'],
- [['ext_card_info'], 'string'],
- [['merchant_user_id', 'account_book_id', 'agreement_no'], 'string', 'max' => 64],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'account_id' => 'Account ID',
- 'mall_id' => 'Mall ID',
- 'book_type' => 'Book Type',
- 'is_default' => 'Is Default',
- 'merchant_user_id' => 'Merchant User ID',
- 'account_book_id' => 'Account Book ID',
- 'ext_card_info' => 'Ext Card Info',
- 'created_at' => 'Created At',
- 'agreement_no' => 'Agreement No',
- ];
- }
- /**
- * 查询外部商户系统会员的唯一标识是否存在
- *
- * @param string $account_book_id
- * @return boolean
- */
- public static function bookIdExists($account_book_id)
- {
- return empty(self::find()->select('id')->where(['account_book_id' => $account_book_id])->one())
- ? true
- : false;
- }
- /**
- * 外部商户系统会员的唯一标识
- *
- * @return string
- */
- public static function getMerchantUserId()
- {
- $book_id = self::PREFIX . date('YmdHi') . rand(100, 999);
- return self::bookIdExists($book_id)
- ? $book_id
- : self::getMerchantUserId();
- }
- /**
- * 添加记帐本
- *
- * @param array $data
- * @return boolean
- */
- public static function addBook($data)
- {
- // 是否有默认的记账本
- $book = self::find()->where(['mall_id' => $data['mall_id'], 'is_default' => 1])->one();
- if (empty($book)) {
- $data['is_default'] = 1;
- }
- $data = array_merge($data, [
- 'created_at' => time(),
- ]);
- $model = new self();
- $model->load($data, '');
- return $model->save();
- }
- /**
- * 获取默认记账本
- *
- * @param int $mall_id
- * @return self
- */
- public static function byMallGetBook($mall_id)
- {
- return self::find()
- ->where(['mall_id' => $mall_id, 'book_type' => 1, 'is_default' => 1])
- ->one();
- }
- /**
- * 获取税务方默认记账本
- *
- * @param integer $mall_id
- * @return static|null
- */
- public static function getTaxation(int $mall_id)
- {
- return self::find()
- ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::TAXATION, 'is_default' => 1])
- ->one();
- }
- /**
- * 获取用工方默认记账本
- *
- * @param integer $mall_id
- * @return static|null
- */
- public static function getEmploy(int $mall_id)
- {
- return self::find()
- ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::EMPLOY, 'is_default' => 1])
- ->one();
- }
- /**
- * 获取流水方默认记账本
- *
- * @param integer $mall_id
- * @return static|null
- */
- public static function getPaymentFlow(int $mall_id)
- {
- return self::find()
- ->where(['mall_id' => $mall_id, 'book_type' => TaxAccount::PAYMENT_FLOW, 'is_default' => 1])
- ->one();
- }
- }
|