StoreSettings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "{{%addons_store_settings}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $store_id
  12. * @property int $store_service_fee 是否收取服务费,1:收取;0:不收取
  13. * @property float $store_service_ratio 门店服务费比例
  14. * @property int $store_settle_type 服务费计算方式,1:门店商品售价(分佣平台承担);2:订单实际支付金额(分佣平台承担);3:商品成本+运费(不包含服务费)(分佣平台承担);4:订单实际支付减去(分销商提成、经销商提成、区域代理、股东分红)
  15. * @property int $store_settle_scene 服务费计算触发场景,1:订单支付;2:订单完成
  16. * @property string|null $store_commission_authority 分销/营销权限,存 json,key 区别各个不同应用
  17. * @property float $cachier_service_ratio 收银台服务费比例
  18. * @property int $cashier_member_status 是否启用会员价,1:启用;0:不启用
  19. * @property int $store_service_fee_type 是否启用会员价,1:启用;0:不启用
  20. * @property string|null $cashier_member_settings 会员各个等级比例设置,存 json
  21. * @property int $cashier_distribution_is_enable 是否开启分销设置,1:是;0:否
  22. * @property string|null $cashier_distribution_settings 分销各个等级佣金比例设置,存 json
  23. * @property int $cashier_agent_is_enable 是否开启经销设置,1:是;0:否
  24. * @property int $is_preferential_undertaking 是否开启优惠谁承担独立设置,1:是;0:否
  25. * @property int $is_preferential_gift 优惠赠送开关
  26. * @property int $preferential_undertaking 优惠谁承担,1:平台;2:门店
  27. * @property int $cashier_goods_complete_direct 收银页购买商品,订单是否直接完成[1是,0否]
  28. * @property string|null $cashier_agent_settings 经销各个等级佣金比例设置,存 json
  29. * @property int $enable_discount_price 启用折扣价格 1:启用;0禁用
  30. * @property float $discount_price_scale 折扣比例设置
  31. * @property int $status 状态,-1:已删除;0:禁用;1:正常
  32. * @property int $created_at
  33. * @property int $updated_at
  34. * @property string|null $merchant_settings
  35. * @property string $huifu_mch_no
  36. * @property int $is_enable_withdraw_setting
  37. * @property int $balance_cash_service_fee_limit
  38. * @property float $balance_cash_service_fee
  39. * @property float $balance_cash_service_fee_diff
  40. * @property float $balance_cash_service_fee_month
  41. * @property int $cashier_is_open_discount 收银台页面是否开启折扣输入框:1:是:0:否
  42. * @property int $is_allow_editing 收银台页面是否开启折扣输入框:1:是:0:否
  43. * @property string $score_deduction 积分抵扣设置
  44. * @property string $score_give 积分赠送设置
  45. */
  46. class StoreSettings extends \common\models\base\BaseActiveRecord
  47. {
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public static function tableName()
  52. {
  53. return '{{%addons_store_settings}}';
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function rules()
  59. {
  60. return [
  61. [['mall_id', 'store_id'], 'required'],
  62. [['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',
  63. 'is_preferential_undertaking','preferential_undertaking','status', 'created_at', 'updated_at','is_preferential_gift','cashier_goods_complete_direct','enable_discount_price', 'store_service_fee_type',
  64. 'is_enable_withdraw_setting', 'balance_cash_service_fee_limit','cashier_is_open_discount', 'is_customer','is_allow_editing'], 'integer'],
  65. [['store_service_ratio', 'cachier_service_ratio','discount_price_scale', 'balance_cash_service_fee', 'balance_cash_service_fee_diff', 'balance_cash_service_fee_month'], 'number'],
  66. [['store_commission_authority', 'cashier_member_settings', 'cashier_distribution_settings','cashier_distribution_settings_score', 'cashier_agent_settings','cashier_area_settings','huifu_mch_no', 'customer_types',
  67. 'score_deduction','score_give'], 'string'],
  68. ];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function attributeLabels()
  74. {
  75. return [
  76. 'id' => 'ID',
  77. 'mall_id' => 'Mall ID',
  78. 'store_id' => 'Store ID',
  79. 'store_service_fee' => '是否收取服务费,1:收取;0:不收取',
  80. 'store_service_ratio' => '门店服务费比例',
  81. 'store_settle_type' => '服务费计算方式',
  82. 'store_settle_scene' => '服务费计算触发场景,1:订单支付;2:订单完成',
  83. 'store_commission_authority' => '分销/营销权限,存 json,key 区别各个不同应用',
  84. 'cachier_service_ratio' => '收银台服务费比例',
  85. 'cashier_member_status' => '是否启用会员价,1:启用;0:不启用',
  86. 'cashier_member_settings' => '会员各个等级比例设置,存 json',
  87. 'cashier_distribution_is_enable' => '是否开启分销设置,1:是;0:否',
  88. 'cashier_distribution_settings' => '分销各个等级佣金比例设置,存 json',
  89. 'cashier_agent_is_enable' => '是否开启经销设置,1:是;0:否',
  90. 'is_preferential_undertaking' => '是否开启优惠谁承担独立设置,1:是;0:否',
  91. 'is_preferential_gift' => '优惠赠送开关',
  92. 'cashier_goods_complete_direct' => '收银页购买商品,订单是否直接完成[1是,0否]',
  93. 'preferential_undertaking' => '优惠谁承担,1:平台;2:门店',
  94. 'cashier_agent_settings' => '经销各个等级佣金比例设置,存 json',
  95. 'cashier_area_settings' => '收银台区域奖励配置',
  96. 'enable_discount_price' => '启用折扣价格 1:启用;0禁用',
  97. 'discount_price_scale' => '折扣比例设置',
  98. 'status' => '状态,-1:已删除;0:禁用;1:正常',
  99. 'created_at' => 'Created At',
  100. 'updated_at' => 'Updated At',
  101. 'cashier_is_open_discount' => '收银台页面是否开启折扣输入框:1:是:0:否',
  102. 'is_customer' => '是否开启门店客服1 是 0 否',
  103. 'customer_types' => '门店客服显示字段',
  104. 'is_allow_editing' => '服务费计算方式平台承担下是否允许门店独立后台商品设置分佣模式',
  105. 'score_deduction' => '积分抵扣设置',
  106. 'score_give' => '积分赠送设置',
  107. ];
  108. }
  109. public static function setData(int $mall_id, array $data)
  110. {
  111. $query = self::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  112. if (!empty($data['id'])) {
  113. $model = $query->andWhere(['id' => $data['id']])->one();
  114. if (empty($model)) {
  115. self::$static_error = '数据异常';
  116. return false;
  117. }
  118. } elseif (!empty($data['store_id'])) {
  119. $model = $query->andWhere(['store_id' => $data['store_id']])->one();
  120. if (empty($model)) {
  121. $model = new self();
  122. $model->loadDefaultValues();
  123. $model->mall_id = $mall_id;
  124. $model->status = StatusEnum::ENABLED;
  125. }
  126. unset($data['id']);
  127. } else {
  128. self::$static_error = '参数异常';
  129. return false;
  130. }
  131. $json_keys = ['store_commission_authority', 'cashier_member_settings', 'cashier_distribution_settings', 'cashier_distribution_settings_score', 'cashier_agent_settings', 'cashier_area_settings'];
  132. foreach ($data as $key => $val) {
  133. if (in_array($key, $json_keys) && is_array($val)) {
  134. $val = json_encode($val);
  135. }
  136. $model->$key = $val;
  137. }
  138. $res = $model->save();
  139. if (!$res) {
  140. self::$static_error = $model->getErrorMessage();
  141. return false;
  142. }
  143. return $model;
  144. }
  145. public static function saveStoreSetting(int $mall_id, array $data, array $addons_settings = [])
  146. {
  147. $transaction = \Yii::$app->db->beginTransaction();
  148. $store_info = [
  149. 'shop_status' => $data['shop_status'],
  150. 'shop_mobile' => $data['username'],
  151. 'business_time_start' => $data['business_time_start'],
  152. 'business_time_end' => $data['business_time_end'],
  153. 'store_background' => $data['store_background'],
  154. 'id' => $data['store_id'],
  155. 'device_id' => $data['device_id']??'',
  156. 'mall_id' => $mall_id,
  157. ];
  158. isset($data['show_sync_goods']) && $store_info['show_sync_goods'] = $data['show_sync_goods'];
  159. $store_res = Store::setData($store_info);
  160. if (false === $store_res) {
  161. $transaction->rollBack();
  162. self::$static_error = Store::getStaticError();
  163. return false;
  164. }
  165. $store_commission_authority = [
  166. 'distribution' => 0,
  167. 'area' => 0,
  168. 'agent' => 0,
  169. 'boss' => 0,
  170. 'doubleCopy' => 0
  171. ];
  172. $cashier_info = [
  173. 'store_service_fee' => $data['store_service_fee'],
  174. 'huifu_mch_no' => $data['huifu_mch_no'] ?? '',
  175. 'store_service_ratio' => $data['store_service_ratio'],
  176. 'store_settle_type' => $data['store_settle_type'],
  177. 'store_settle_scene' => $data['store_settle_scene'],
  178. 'store_commission_authority' => array_merge($store_commission_authority, $data['store_commission_authority'] ?? []),
  179. 'store_id' => $data['store_id'],
  180. 'is_enable_withdraw_setting' => $data['is_enable_withdraw_setting'],
  181. 'balance_cash_service_fee_limit' => $data['balance_cash_service_fee_limit'],
  182. 'balance_cash_service_fee' => $data['balance_cash_service_fee'],
  183. 'store_service_fee_type' => $data['store_service_fee_type'] ?? StatusEnum::DISABLED,
  184. 'store_service_fee_settle_type' => $data['store_service_fee_settle_type'] ?? 1,
  185. 'balance_cash_service_fee_diff' => $data['balance_cash_service_fee_diff'],
  186. 'balance_cash_service_fee_month' => $data['balance_cash_service_fee_month'],
  187. 'is_allow_editing' => $data['is_allow_editing'] ?? 0,
  188. ];
  189. $setting_res = self::setData($mall_id, $cashier_info);
  190. if (false === $setting_res) {
  191. $transaction->rollBack();
  192. return false;
  193. }
  194. //判断门店是否存在
  195. $admin_model = StoreAdmin::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'username' => $store_res->shop_mobile]);
  196. if (empty($admin_model)) {
  197. $transaction->rollBack();
  198. self::$static_error = '门店管理员账号不存在';
  199. return false;
  200. }
  201. // 因为上一步已经有账号变更现在只要密码变更就行
  202. if (!empty($data['password'])) {
  203. $admin_model->password = \Yii::$app->security->generatePasswordHash($data['password']);
  204. $admin_model->updated_at = time();
  205. $admin_res = $admin_model->save();
  206. if (empty($admin_res)) {
  207. $transaction->rollBack();
  208. self::$static_error = $admin_model->getErrorMessage();
  209. return false;
  210. }
  211. }
  212. if (!empty($addons_settings)) {
  213. $param = [
  214. 'mall_id' => $mall_id,
  215. 'store_id' => $data['store_id'],
  216. 'status' => $data['status'],
  217. 'content' => json_encode($addons_settings),
  218. ];
  219. $res = StoreModel::setData($param);
  220. if ($res === false) {
  221. self::$static_error = StoreGoodsModel::getStaticError();
  222. return false;
  223. }
  224. }
  225. $transaction->commit();
  226. return true;
  227. }
  228. /**
  229. * 获取商户设置
  230. *
  231. * @param integer $store_id
  232. * @return string
  233. */
  234. public static function getMerchantSettings(int $store_id)
  235. {
  236. return static::find()
  237. ->select('merchant_settings')
  238. ->where(['store_id' => $store_id])
  239. ->scalar();
  240. }
  241. /**
  242. * 保存商户设置
  243. *
  244. * @param integer $mall_id
  245. * @param integer $store_id
  246. * @param array $setting
  247. * @return int the number of rows updated
  248. */
  249. public static function setMerchantSettings(int $mall_id, int $store_id, array $setting)
  250. {
  251. $setting = Json::encode($setting);
  252. $model = static::find()->where([
  253. 'mall_id' => $mall_id,
  254. 'store_id' => $store_id,
  255. 'status' => StatusEnum::ENABLED,
  256. ])->one();
  257. if (!empty($model)) {
  258. $model->merchant_settings = $setting;
  259. } else {
  260. $model = new static();
  261. $model->mall_id = $mall_id;
  262. $model->store_id = $store_id;
  263. $model->merchant_settings = $setting;
  264. }
  265. return $model->save();
  266. }
  267. public static function updateServiceFeeSetting($params)
  268. {
  269. $max_platform_ratio = $params['max_platform_ratio'];
  270. $min_platform_ratio = $params['min_platform_ratio'];
  271. $query = self::find()
  272. ->where([
  273. 'mall_id' => $params['mall_id'],
  274. 'status' => StatusEnum::ENABLED,
  275. // 'store_service_fee' => [1,2],
  276. ])
  277. ->select(['id', 'mall_id', 'store_id', 'store_service_fee', 'store_service_ratio']);
  278. foreach ($query->each() as $row) {
  279. $ratio = $row['store_service_ratio'];
  280. if ($ratio < $min_platform_ratio || $ratio > $max_platform_ratio) {
  281. \Yii::$app->db->createCommand()
  282. ->update(self::tableName(), [
  283. 'store_service_ratio' => $max_platform_ratio
  284. ], ['id' => $row['id']])
  285. ->execute();
  286. StoreServiceRatioLog::setData([
  287. 'mall_id' => $row['mall_id'],
  288. 'store_id' => $row['store_id'],
  289. 'before_store_service_ratio' => $row['store_service_ratio'],
  290. 'after_store_service_ratio' => $max_platform_ratio,
  291. ]);
  292. }
  293. if ($row['cachier_service_ratio'] < $min_platform_ratio || $row['cachier_service_ratio'] > $max_platform_ratio) {
  294. \Yii::$app->db->createCommand()
  295. ->update(self::tableName(), [
  296. 'cachier_service_ratio' => $max_platform_ratio
  297. ], ['id' => $row['id']])
  298. ->execute();
  299. }
  300. }
  301. }
  302. }