| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace addons\ScoreBonus\common\models;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use Yii;
- /**
- * This is the model class for table "{{%addons_score_bonus_commission_item_setting}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $is_enable 是否启用带货奖励:0:否;1是
- * @property int $is_alone 是否独立设置:0:否;1是
- * @property int $is_extra 是否开启额外分红:0:否;1是
- * @property int $item_id 项目id
- * @property string $item_type 项目类型:商品goods
- * @property string $commission_type 分佣类型:佣金:commission
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class ScoreBonusCommissionItemSetting extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_bonus_commission_item_setting}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id'], 'required'],
- [['mall_id', 'is_enable', 'is_alone', 'item_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['item_type', 'commission_type'], 'string', 'max' => 255],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'is_enable' => 'Is Enable',
- 'is_alone' => 'Is Alone',
- 'item_id' => 'Item ID',
- 'item_type' => 'Item Type',
- 'commission_type' => 'Commission Type',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getDetail()
- {
- return $this->hasOne(ScoreBonusCommissionItemSettingDetail::class, ['score_bonus_commission_item_setting_id' => 'id']);
- }
- public static function setData($params, $data)
- {
- try {
- $transaction = \Yii::$app->db->beginTransaction();
- foreach ($data as $commission_type => $item) {
- $detail_params = $item['detail'];
- unset($item['detail']);
- $model = self::findOne([
- 'mall_id' => $params['mall_id'],
- 'item_type' => $item['item_type'],
- 'item_id' => $params['item_id'],
- 'commission_type' => $item['commission_type'],
- 'status' => [StatusEnum::ENABLED]
- ]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->item_id = $params['item_id'];
- $model->loadDefaultValues();
- }
- if(!$model->load($item,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- // 插入配置详情
- $detail = array_merge($detail_params, ['mall_id' => $params['mall_id'], 'score_bonus_commission_item_setting_id' => $model->id]);
- $res = ScoreBonusCommissionItemSettingDetail::setData($detail);
- if ($res == false) {
- throw new \Exception(ScoreBonusCommissionItemSettingDetail::getStaticError());
- }
- }
- $transaction->commit();
- return true;
- } catch (\Exception $e) {
- $transaction->rollBack();
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @name:
- * @msg: 计算带货奖励
- * @param {int} $mall_id
- * @param {int} $good_id
- * @param {int} $good_price 商品售价
- * @return {bool | array} commission:带货奖励佣金;
- */
- public static function calculateReward(int $mall_id, int $good_id, float $good_price)
- {
- $good = Goods::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_id]);
- if (empty($good)) {
- self::$static_error = '商品不存在';
- return false;
- }
- $item_stting = self::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'item_id' => $good_id]);
- $commission = 0;
- if (1 == $item_stting['is_alone']) {
- // 独立设置带货奖励
- $setting_detail = $item_stting->detail;
- $commission = bcmul($good_price, $setting_detail['value'] / 100, 2);
- } else {
- // 未独立设置
- $config = Yii::$app->services->setting->addons->get($mall_id, 'ScoreBonus', 'basic');
- $commission = bcmul($good_price, $config['bonus_pre'] / 100, 2);
- }
- return $commission;
- }
- }
|