| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- namespace addons\Store\common\services;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\models\StoreScoreStoreApply;
- use addons\Store\common\models\StoreSettings;
- use common\enums\StatusEnum;
- /**
- * 门店service
- * @package addons\Store\common\services
- */
- class StoreSettingService extends BaseService
- {
- public function updateScoreDeduction($params)
- {
- $model = StoreSettings::find()->where(['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'status' => StatusEnum::ENABLED])->one();
- if (empty($model)) {
- $model = new StoreSettings();
- $model->loadDefaultValues();
- $model->mall_id = $this->mall_id;
- $model->store_id = $this->store_id;
- $model->status = StatusEnum::ENABLED;
- }
- if(empty($params['score_deduction'])){
- $params['score_deduction'] = [];
- }
- $model->score_deduction = json_encode($params['score_deduction'], JSON_UNESCAPED_UNICODE);
- if(empty($params['score_give'])){
- $params['score_give'] = [];
- }
- $model->score_give = json_encode($params['score_give'], JSON_UNESCAPED_UNICODE);
- $res = $model->save();
- if (!$res) {
- throw new \Exception($model->getErrorMessage());
- }
- return $model;
- }
- public function getScoreDeduction()
- {
- $score_deduction = [
- 'is_score_deduction' => 0,
- 'is_alone_score_deduction' => 0,
- 'effective' => 1,
- 'deduction_proportion' => [['min' => '', 'max' => '', 'deduction_ratio' => '']],
- 'max_deduction_percent' => 0
- ];
- $score_give = ['is_alone_score_give'=>0, 'condition'=>1, 'give_type'=>1, 'give_num'=>0, 'settlement'=>0,];
- $deduction_proportion = [['min'=>'','max'=>'','deduction_ratio'=>'']];
- $platform_score_deduction = [
- 'effective' => 1,
- 'deduction_proportion' => $deduction_proportion,
- ];
- $platform_score_give = ['condition' => 1, 'give_type'=>1, 'give_num'=>0, 'settlement'=>0,'bear'=>0];
- $deduction_bear_text = '平台';
- $is_score_deduction = 0;
- $platform_basic_payment_config = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'payment');
- $platform_basic_score_deduction = $platform_basic_payment_config['score_deduction'];
- $deduction_store_limit_protocol = $platform_basic_payment_config['store_limit_protocol'] ?? '';
- if (!empty($platform_basic_score_deduction)) {
- $platform_basic_score_deduction = json_decode($platform_basic_score_deduction, true);
- $platform_basic_score_deduction['max_deduction_percent'] = $platform_basic_score_deduction['max_deduction_percent'] ?? 0;
- $platform_basic_score_deduction['deduction_type'] = $platform_basic_score_deduction['deduction_type'] ?? 0;
- $platform_score_deduction = $platform_basic_score_deduction;
- $is_score_deduction = $platform_basic_score_deduction['is_score_deduction'];
- // 判断当前门店是否有“积分抵扣”权限限制
- if (!empty($is_score_deduction) && !empty($platform_basic_score_deduction['store_limit_type']) && isset($platform_basic_score_deduction['store_limit_stores_deduction'])) {
- $store_limit_stores = array_keys($platform_basic_score_deduction['store_limit_stores_deduction']);
- $platform_score_deduction['max_deduction_percent'] =
- isset($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) && is_numeric($platform_score_deduction['store_limit_stores_deduction'][$this->store_id]) ?
- $platform_score_deduction['store_limit_stores_deduction'][$this->store_id] : $platform_score_deduction['max_deduction_percent'];
- !in_array($this->store_id, $store_limit_stores) && $is_score_deduction = -1;
- // 判断申请记录是否存在
- $score_deduction_store_apply = StoreScoreStoreApply::getOne([['mall_id' => $this->mall_id], ['store_id' => $this->store_id], ['score_method' => 2], ['status' => StatusEnum::ENABLED]], true, [], 'created_at desc', 'check_status, reason');
- }
- if (!empty($platform_basic_score_deduction['bear'])) {
- $deduction_bear_text = '门店';
- }
- if (isset($platfSorm_score_deduction['store_limit_stores_deduction'])) unset($platform_score_deduction['store_limit_stores_deduction']);
- }
- $setting = StoreSettings::getOne([['mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'status' => StatusEnum::ENABLED]], true, [], 'id desc', 'score_deduction,score_give');
- if (!empty($setting['score_deduction'])) {
- $score_deduction = json_decode($setting['score_deduction'], true);
- if(empty($score_deduction['deduction_proportion'])){
- $score_deduction['deduction_proportion'] = $deduction_proportion;
- }
- !isset($score_deduction['max_deduction_percent']) && $score_deduction['max_deduction_percent'] = 0;
- }
- if (!empty($setting['score_give'])) {
- $score_give = json_decode($setting['score_give'], true);
- }
- $score_give_config = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'scoreGive');
- $score_give_setting = $score_give_config['score_give_setting'];
- $store_limit_protocol = $score_give_config['store_limit_protocol'] ?? '';
- $bear_text = '平台';
- $is_score_give = 0;
- if (!empty($score_give_setting)) {
- $score_give_setting = json_decode($score_give_setting, true);
- $platform_score_give = $score_give_setting;
- $is_score_give = $score_give_setting['is_open'];
- // 判断当前门店是否有“积分赠送”权限限制
- if (!empty($is_score_give) && !empty($score_give_config['store_limit_type']) && isset($score_give_config['store_limit_stores'])) {
- $store_limit_stores = json_decode($score_give_config['store_limit_stores'], true);
- !in_array($this->store_id, $store_limit_stores) && $is_score_give = -1;
- // 判断申请记录是否存在
- $score_give_store_apply = StoreScoreStoreApply::getOne([['mall_id' => $this->mall_id], ['store_id' => $this->store_id], ['score_method' => 1], ['status' => StatusEnum::ENABLED]], true, [], 'created_at desc', 'check_status, reason');
- }
- if (!empty($score_give_setting['bear'])) {
- $bear_text = '门店';
- }
- }
- $data['score_deduction'] = $score_deduction;
- $data['score_give'] = $score_give;
- $data['platform_is_score_deduction'] = $is_score_deduction;
- $data['platform_is_score_give'] = $is_score_give;
- $data['bear_text'] = $bear_text;
- $data['deduction_bear_text'] = $deduction_bear_text;
- $data['platform_score_deduction'] = $platform_score_deduction;
- $data['platform_score_give'] = $platform_score_give;
- $data['platform_store_limit_protocol'] = $store_limit_protocol;
- $data['platform_deduction_store_limit_protocol'] = $deduction_store_limit_protocol;
- $data['platform_score_give_store_apply'] = $score_give_store_apply ?? null;
- $data['platform_score_deduction_store_apply'] = $score_deduction_store_apply ?? null;
- return $data;
- }
- /**
- * 获取积分抵扣区间列表
- * @param $params
- * @return array
- */
- public function getDeductionProportion($params)
- {
- $platform_basic_score_deduction = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'payment.score_deduction');
- if (empty($platform_basic_score_deduction)) {
- return [];
- }
- $platform_basic_score_deduction = json_decode($platform_basic_score_deduction, true);
- if (empty($platform_basic_score_deduction['is_score_deduction'])) {
- return [];
- }
- $effective = $platform_basic_score_deduction['effective'];//平台基础设置的积分抵扣生效对象1全部2收银台订单3非收银台订单
- $deduction_proportion = $platform_basic_score_deduction['deduction_proportion'];
- $store_settings_list = StoreSettings::lists([
- 'where' => [
- ['mall_id' => $this->mall_id, 'store_id' => $params['store_id'], 'status' => StatusEnum::ENABLED]
- ],
- 'select' => 'store_id,score_deduction',
- 'index' => 'store_id'
- ]);
- $data = [];
- foreach ($params['store_id'] as $store_id) {
- $data[$store_id] = $deduction_proportion;
- if (!empty($store_settings_list[$store_id])) {
- $store_score_deduction = json_decode($store_settings_list[$store_id]['score_deduction'], true);
- if (isset($store_score_deduction['is_score_deduction']) && $store_score_deduction['is_score_deduction'] == 0) {
- $data[$store_id] = [];
- continue;
- }
- if (!empty($store_score_deduction['is_alone_score_deduction']) && $store_score_deduction['is_score_deduction'] == 1) {
- //门店独立配置
- $data[$store_id] = $store_score_deduction['deduction_proportion'];
- $effective = $store_score_deduction['effective'];//门店独立设置的积分抵扣生效对象1全部2收银台订单3非收银台订单
- }
- }
- }
- if (isset($params['order_type'])) {
- switch ($params['order_type']) {
- case 'store_order'://非收银台订单
- if ($effective == 2) {
- return [];
- }
- break;
- case 'store_cashier_order'://收银台订单
- if ($effective == 3) {
- return [];
- }
- break;
- }
- }
- return $data;
- }
- /**
- * 获取抵扣比例
- * @param $money
- * @param $store_score_deduction
- * @return int
- */
- public function getDeductionRatio($money, $store_score_deduction,&$is_can_use=0)
- {
- $ratio = 0;
- foreach ($store_score_deduction as $val) {
- if ($money >= $val['min'] && $money <= $val['max']) {
- $ratio = $val['deduction_ratio'];
- $is_can_use = 1;
- break;
- }
- }
- return $ratio;
- }
- }
|