| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace addons\Store\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%addons_store_settings}}".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $store_id
- * @property int $store_service_fee 是否收取服务费,1:收取;0:不收取
- * @property float $store_service_ratio 门店服务费比例
- * @property int $store_settle_type 服务费计算方式,1:门店商品售价(分佣平台承担);2:订单实际支付金额(分佣平台承担);3:商品成本+运费(不包含服务费)(分佣平台承担);4:订单实际支付减去(分销商提成、经销商提成、区域代理、股东分红)
- * @property int $store_settle_scene 服务费计算触发场景,1:订单支付;2:订单完成
- * @property string|null $store_commission_authority 分销/营销权限,存 json,key 区别各个不同应用
- * @property float $cachier_service_ratio 收银台服务费比例
- * @property int $cashier_member_status 是否启用会员价,1:启用;0:不启用
- * @property int $store_service_fee_type 是否启用会员价,1:启用;0:不启用
- * @property string|null $cashier_member_settings 会员各个等级比例设置,存 json
- * @property int $cashier_distribution_is_enable 是否开启分销设置,1:是;0:否
- * @property string|null $cashier_distribution_settings 分销各个等级佣金比例设置,存 json
- * @property int $cashier_agent_is_enable 是否开启经销设置,1:是;0:否
- * @property int $is_preferential_undertaking 是否开启优惠谁承担独立设置,1:是;0:否
- * @property int $is_preferential_gift 优惠赠送开关
- * @property int $preferential_undertaking 优惠谁承担,1:平台;2:门店
- * @property int $cashier_goods_complete_direct 收银页购买商品,订单是否直接完成[1是,0否]
- * @property string|null $cashier_agent_settings 经销各个等级佣金比例设置,存 json
- * @property int $enable_discount_price 启用折扣价格 1:启用;0禁用
- * @property float $discount_price_scale 折扣比例设置
- * @property int $status 状态,-1:已删除;0:禁用;1:正常
- * @property int $created_at
- * @property int $updated_at
- * @property string|null $merchant_settings
- * @property string $huifu_mch_no
- * @property int $is_enable_withdraw_setting
- * @property int $balance_cash_service_fee_limit
- * @property float $balance_cash_service_fee
- * @property float $balance_cash_service_fee_diff
- * @property float $balance_cash_service_fee_month
- * @property int $cashier_is_open_discount 收银台页面是否开启折扣输入框:1:是:0:否
- * @property int $is_allow_editing 收银台页面是否开启折扣输入框:1:是:0:否
- * @property string $score_deduction 积分抵扣设置
- * @property string $score_give 积分赠送设置
- */
- class StoreSettings extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_store_settings}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id'], 'required'],
- [['mall_id', 'store_id', 'store_service_fee', 'store_settle_type', 'store_settle_scene', 'cashier_member_status', 'cashier_distribution_is_enable','cashier_distribution_is_enable_score', 'cashier_agent_is_enable',
- 'is_preferential_undertaking','preferential_undertaking','status', 'created_at', 'updated_at','is_preferential_gift','cashier_goods_complete_direct','enable_discount_price', 'store_service_fee_type',
- 'is_enable_withdraw_setting', 'balance_cash_service_fee_limit','cashier_is_open_discount', 'is_customer','is_allow_editing'], 'integer'],
- [['store_service_ratio', 'cachier_service_ratio','discount_price_scale', 'balance_cash_service_fee', 'balance_cash_service_fee_diff', 'balance_cash_service_fee_month'], 'number'],
- [['store_commission_authority', 'cashier_member_settings', 'cashier_distribution_settings','cashier_distribution_settings_score', 'cashier_agent_settings','cashier_area_settings','huifu_mch_no', 'customer_types',
- 'score_deduction','score_give'], 'string'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'store_service_fee' => '是否收取服务费,1:收取;0:不收取',
- 'store_service_ratio' => '门店服务费比例',
- 'store_settle_type' => '服务费计算方式',
- 'store_settle_scene' => '服务费计算触发场景,1:订单支付;2:订单完成',
- 'store_commission_authority' => '分销/营销权限,存 json,key 区别各个不同应用',
- 'cachier_service_ratio' => '收银台服务费比例',
- 'cashier_member_status' => '是否启用会员价,1:启用;0:不启用',
- 'cashier_member_settings' => '会员各个等级比例设置,存 json',
- 'cashier_distribution_is_enable' => '是否开启分销设置,1:是;0:否',
- 'cashier_distribution_settings' => '分销各个等级佣金比例设置,存 json',
- 'cashier_agent_is_enable' => '是否开启经销设置,1:是;0:否',
- 'is_preferential_undertaking' => '是否开启优惠谁承担独立设置,1:是;0:否',
- 'is_preferential_gift' => '优惠赠送开关',
- 'cashier_goods_complete_direct' => '收银页购买商品,订单是否直接完成[1是,0否]',
- 'preferential_undertaking' => '优惠谁承担,1:平台;2:门店',
- 'cashier_agent_settings' => '经销各个等级佣金比例设置,存 json',
- 'cashier_area_settings' => '收银台区域奖励配置',
- 'enable_discount_price' => '启用折扣价格 1:启用;0禁用',
- 'discount_price_scale' => '折扣比例设置',
- 'status' => '状态,-1:已删除;0:禁用;1:正常',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- 'cashier_is_open_discount' => '收银台页面是否开启折扣输入框:1:是:0:否',
- 'is_customer' => '是否开启门店客服1 是 0 否',
- 'customer_types' => '门店客服显示字段',
- 'is_allow_editing' => '服务费计算方式平台承担下是否允许门店独立后台商品设置分佣模式',
- 'score_deduction' => '积分抵扣设置',
- 'score_give' => '积分赠送设置',
- ];
- }
- public static function setData(int $mall_id, array $data)
- {
- $query = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- if (!empty($data['id'])) {
- $model = $query->andWhere(['id' => $data['id']])->one();
- if (empty($model)) {
- self::$static_error = '数据异常';
- return false;
- }
- } elseif (!empty($data['store_id'])) {
- $model = $query->andWhere(['store_id' => $data['store_id']])->one();
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- $model->mall_id = $mall_id;
- $model->status = StatusEnum::ENABLED;
- }
- unset($data['id']);
- } else {
- self::$static_error = '参数异常';
- return false;
- }
- $json_keys = ['store_commission_authority', 'cashier_member_settings', 'cashier_distribution_settings', 'cashier_distribution_settings_score', 'cashier_agent_settings', 'cashier_area_settings'];
- foreach ($data as $key => $val) {
- if (in_array($key, $json_keys) && is_array($val)) {
- $val = json_encode($val);
- }
- $model->$key = $val;
- }
- $res = $model->save();
- if (!$res) {
- self::$static_error = $model->getErrorMessage();
- return false;
- }
- return $model;
- }
- public static function saveStoreSetting(int $mall_id, array $data, array $addons_settings = [])
- {
- $transaction = \Yii::$app->db->beginTransaction();
- $store_info = [
- 'shop_status' => $data['shop_status'],
- 'shop_mobile' => $data['username'],
- 'business_time_start' => $data['business_time_start'],
- 'business_time_end' => $data['business_time_end'],
- 'store_background' => $data['store_background'],
- 'id' => $data['store_id'],
- 'device_id' => $data['device_id']??'',
- 'mall_id' => $mall_id,
- ];
- isset($data['show_sync_goods']) && $store_info['show_sync_goods'] = $data['show_sync_goods'];
- $store_res = Store::setData($store_info);
- if (false === $store_res) {
- $transaction->rollBack();
- self::$static_error = Store::getStaticError();
- return false;
- }
- $store_commission_authority = [
- 'distribution' => 0,
- 'area' => 0,
- 'agent' => 0,
- 'boss' => 0,
- 'doubleCopy' => 0
- ];
- $cashier_info = [
- 'store_service_fee' => $data['store_service_fee'],
- 'huifu_mch_no' => $data['huifu_mch_no'] ?? '',
- 'store_service_ratio' => $data['store_service_ratio'],
- 'store_settle_type' => $data['store_settle_type'],
- 'store_settle_scene' => $data['store_settle_scene'],
- 'store_commission_authority' => array_merge($store_commission_authority, $data['store_commission_authority'] ?? []),
- 'store_id' => $data['store_id'],
- 'is_enable_withdraw_setting' => $data['is_enable_withdraw_setting'],
- 'balance_cash_service_fee_limit' => $data['balance_cash_service_fee_limit'],
- 'balance_cash_service_fee' => $data['balance_cash_service_fee'],
- 'store_service_fee_type' => $data['store_service_fee_type'] ?? StatusEnum::DISABLED,
- 'store_service_fee_settle_type' => $data['store_service_fee_settle_type'] ?? 1,
- 'balance_cash_service_fee_diff' => $data['balance_cash_service_fee_diff'],
- 'balance_cash_service_fee_month' => $data['balance_cash_service_fee_month'],
- 'is_allow_editing' => $data['is_allow_editing'] ?? 0,
- ];
- $setting_res = self::setData($mall_id, $cashier_info);
- if (false === $setting_res) {
- $transaction->rollBack();
- return false;
- }
- //判断门店是否存在
- $admin_model = StoreAdmin::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'username' => $store_res->shop_mobile]);
- if (empty($admin_model)) {
- $transaction->rollBack();
- self::$static_error = '门店管理员账号不存在';
- return false;
- }
- // 因为上一步已经有账号变更现在只要密码变更就行
- if (!empty($data['password'])) {
- $admin_model->password = \Yii::$app->security->generatePasswordHash($data['password']);
- $admin_model->updated_at = time();
- $admin_res = $admin_model->save();
- if (empty($admin_res)) {
- $transaction->rollBack();
- self::$static_error = $admin_model->getErrorMessage();
- return false;
- }
- }
- if (!empty($addons_settings)) {
- $param = [
- 'mall_id' => $mall_id,
- 'store_id' => $data['store_id'],
- 'status' => $data['status'],
- 'content' => json_encode($addons_settings),
- ];
- $res = StoreModel::setData($param);
- if ($res === false) {
- self::$static_error = StoreGoodsModel::getStaticError();
- return false;
- }
- }
- $transaction->commit();
- return true;
- }
- /**
- * 获取商户设置
- *
- * @param integer $store_id
- * @return string
- */
- public static function getMerchantSettings(int $store_id)
- {
- return static::find()
- ->select('merchant_settings')
- ->where(['store_id' => $store_id])
- ->scalar();
- }
- /**
- * 保存商户设置
- *
- * @param integer $mall_id
- * @param integer $store_id
- * @param array $setting
- * @return int the number of rows updated
- */
- public static function setMerchantSettings(int $mall_id, int $store_id, array $setting)
- {
- $setting = Json::encode($setting);
- $model = static::find()->where([
- 'mall_id' => $mall_id,
- 'store_id' => $store_id,
- 'status' => StatusEnum::ENABLED,
- ])->one();
- if (!empty($model)) {
- $model->merchant_settings = $setting;
- } else {
- $model = new static();
- $model->mall_id = $mall_id;
- $model->store_id = $store_id;
- $model->merchant_settings = $setting;
- }
- return $model->save();
- }
- public static function updateServiceFeeSetting($params)
- {
- $max_platform_ratio = $params['max_platform_ratio'];
- $min_platform_ratio = $params['min_platform_ratio'];
- $query = self::find()
- ->where([
- 'mall_id' => $params['mall_id'],
- 'status' => StatusEnum::ENABLED,
- // 'store_service_fee' => [1,2],
- ])
- ->select(['id', 'mall_id', 'store_id', 'store_service_fee', 'store_service_ratio']);
- foreach ($query->each() as $row) {
- $ratio = $row['store_service_ratio'];
- if ($ratio < $min_platform_ratio || $ratio > $max_platform_ratio) {
- \Yii::$app->db->createCommand()
- ->update(self::tableName(), [
- 'store_service_ratio' => $max_platform_ratio
- ], ['id' => $row['id']])
- ->execute();
- StoreServiceRatioLog::setData([
- 'mall_id' => $row['mall_id'],
- 'store_id' => $row['store_id'],
- 'before_store_service_ratio' => $row['store_service_ratio'],
- 'after_store_service_ratio' => $max_platform_ratio,
- ]);
- }
- if ($row['cachier_service_ratio'] < $min_platform_ratio || $row['cachier_service_ratio'] > $max_platform_ratio) {
- \Yii::$app->db->createCommand()
- ->update(self::tableName(), [
- 'cachier_service_ratio' => $max_platform_ratio
- ], ['id' => $row['id']])
- ->execute();
- }
- }
- }
- }
|