HappinessStore.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. namespace addons\Happiness\common\models;
  3. use addons\Happiness\common\models\HappinessStoreAdmin;
  4. use addons\Happiness\common\enums\HappinessEnum;
  5. use addons\Store\common\models\StoreAssociatedUser;
  6. use addons\Store\common\models\StoreCommission;
  7. use addons\Store\common\models\StoreMerchantReduceProfit;
  8. use addons\Store\common\models\StoreOrder;
  9. use addons\Store\common\models\StoreOrderDetail;
  10. use addons\Store\common\models\StoreSettings;
  11. use addons\Store\common\models\StoreStatistics;
  12. use common\enums\AddonsEnum;
  13. use common\enums\OrderStatusEnum;
  14. use common\enums\PayTypeEnum;
  15. use common\enums\StatusEnum;
  16. use common\enums\UserWalletEnum;
  17. use common\models\base\BaseActiveRecord;
  18. use common\models\common\CapitalLog;
  19. use common\models\mall\Mall;
  20. use common\models\mall\MallAddons;
  21. use common\models\order\Order;
  22. use common\models\user\User;
  23. use services\common\UserWalletService;
  24. use Yii;
  25. use yii\console\ExitCode;
  26. use yii\db\Exception;
  27. /**
  28. * This is the model class for table "qimall_addons_happiness_store".
  29. *
  30. * @property int $id
  31. * @property int $mall_id
  32. * @property int $user_id 绑定会员id
  33. * @property string $shop_owner 店长姓名
  34. * @property string $shop_mobile 店长手机号
  35. * @property string $store_name 门店名称
  36. * @property string $store_desc 门店简介
  37. * @property string $logo_img 门店logo
  38. * @property string $store_img 门店图片
  39. * @property string $license_img 营业执照
  40. * @property int|null $province_id 省
  41. * @property int|null $city_id 市
  42. * @property int|null $district_id 区
  43. * @property string $address 详细地址
  44. * @property int $expire_time 失效时间,0代表无限期
  45. * @property float $total_money 累计收入
  46. * @property float $share_amount 累计收入
  47. * @property float $balance 当前余额
  48. * @property float $frozen_balance 冻结余额
  49. * @property float $total_expenses 累计支出
  50. * @property int|null $user_nums 会员总数
  51. * @property string $longitude 经度
  52. * @property string $latitude 维度
  53. * @property int $check_status 审核状态:0:待审核;1:已通过;2:未通过
  54. * @property string $check_remark 审核备注
  55. * @property string $business_time_start 营业时间开始
  56. * @property string $business_time_end 营业时间结束
  57. * @property string $freight_type 配送设置
  58. * @property string $intra_city_distribution 同城配送
  59. * @property float $total_order_money 订单总金额
  60. * @property int $refund_money 退款金额
  61. * @property float $refund_num 退款订单数量
  62. * @property float $fine_money 罚款金额
  63. * @property int $total_num 总数
  64. * @property float $total_settled_amount 已结算总金额
  65. * @property float $unsettled_amount 未结算金额
  66. * @property int $sales_volume 销量
  67. * @property int $no 编号
  68. * @property int $merchant_distribution 商家配送
  69. * @property float $praise 评分
  70. * @property int $status 状态[0禁用 1启用 -1删除]
  71. * @property int $shop_status 门店营业状态,1:正在营业;0:休息中
  72. * @property int $created_at 创建时间
  73. * @property int $updated_at 修改时间
  74. * @property int $business_id 业务员ID
  75. * @property string $huifu_no 汇付号
  76. */
  77. class HappinessStore extends BaseActiveRecord
  78. {
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public static function tableName()
  83. {
  84. return '{{%addons_happiness_store}}';
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function rules()
  90. {
  91. return [
  92. [['mall_id', 'user_id', 'province_id', 'city_id', 'district_id','street_id','expire_time', 'user_nums',
  93. 'total_num', 'sales_volume', 'status', 'shop_status', 'created_at', 'updated_at', 'merchant_distribution', 'self_mention', 'is_alone','business_id','check_status'], 'integer'],
  94. [['total_money', 'balance','frozen_balance','refund_money','refund_num', 'fine_money', 'total_expenses', 'total_order_money', 'total_settled_amount', 'unsettled_amount', 'praise', 'share_amount', 'distribution_scope'], 'number'],
  95. [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'address', 'longitude', 'latitude',
  96. 'business_time_start', 'business_time_end','check_remark'], 'string', 'max' => 255],
  97. [['huifu_no'], 'string', 'max' => 100],
  98. [['intra_city_distribution','no'], 'string', 'max' => 500],
  99. [['shop_mobile'], 'string', 'max' => 11],
  100. ];
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function attributeLabels()
  106. {
  107. return [
  108. 'id' => 'ID',
  109. 'mall_id' => 'Mall ID',
  110. 'user_id' => '绑定会员id',
  111. 'no' => '编号',
  112. 'business_id'=>'业务员ID',
  113. 'shop_owner' => '店长姓名',
  114. 'shop_mobile' => '店长手机号',
  115. 'store_name' => '门店名称',
  116. 'store_desc' => '门店简介',
  117. 'logo_img' => '门店logo',
  118. 'license_img' => '营业执照',
  119. 'province_id' => '省',
  120. 'city_id' => '市',
  121. 'district_id' => '区',
  122. 'street_id' => '街道',
  123. 'community_id' => '社区',
  124. 'address' => '详细地址',
  125. 'expire_time' => '失效时间,0代表无限期',
  126. 'total_money' => '累计收入',
  127. 'balance' => '当前余额',
  128. 'frozen_balance' => '冻结余额',
  129. 'total_expenses' => '累计支出',
  130. 'user_nums' => '会员总数',
  131. 'longitude' => '经度',
  132. 'latitude' => '维度',
  133. 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过',
  134. 'check_remark' => '审核备注',
  135. 'business_time_start' => '营业时间开始',
  136. 'business_time_end' => '营业时间结束',
  137. 'freight_type' => '配送设置',
  138. 'intra_city_distribution' => '同城配送',
  139. 'total_order_money' => '订单总金额',
  140. 'total_num' => '总数',
  141. 'total_settled_amount' => '已结算总金额',
  142. 'unsettled_amount' => '未结算金额',
  143. 'refund_money' => '退款金额',
  144. 'refund_num'=>'退款订单数量',
  145. 'file_money' => '罚款金额',
  146. 'status' => '状态[0禁用 1启用 -1删除]',
  147. 'shop_status' => '门店营业状态',
  148. 'created_at' => '创建时间',
  149. 'updated_at' => '修改时间',
  150. ];
  151. }
  152. public static function setData(array $params, bool $is_custom_pwd = false)
  153. {
  154. $t = Yii::$app->db->beginTransaction();
  155. try {
  156. if (!empty($params['id'])) {
  157. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  158. if (empty($model)) throw new \Exception('门店不存在或已删除');
  159. } else {
  160. $model = new self();
  161. $model->loadDefaultValues();
  162. // $model->expire_time = strtotime("+1 year");
  163. }
  164. // if (!empty($params['freight_type'])) $params['freight_type'] = json_encode($params['freight_type']);
  165. if (!empty($params['intra_city_distribution'])) $params['intra_city_distribution'] = json_encode($params['intra_city_distribution']);
  166. if (!$model->load($params, '') || !$model->save()) {
  167. throw new \Exception($model->getErrorMessage());
  168. }
  169. $store_addmin = HappinessStoreAdmin::findOne(['mall_id' => $params['mall_id'], 'store_id' => $model->id, 'status' => StatusEnum::ENABLED]);
  170. // 同步一下账号
  171. if ($store_addmin) {
  172. if ($model->shop_mobile != $store_addmin->username) {
  173. $pwd_str = substr($model->shop_mobile, -6);
  174. if ($is_custom_pwd) $pwd_str = $params['password'];
  175. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  176. $store_addmin->username = $model->shop_mobile;
  177. $store_addmin->password = $pwd_hash_str;
  178. if (!$store_addmin->save()) throw new \Exception($store_addmin->getErrorMessage());
  179. }
  180. }
  181. $t->commit();
  182. return $model;
  183. } catch (\Exception $e) {
  184. $t->rollBack();
  185. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  186. return false;
  187. }
  188. }
  189. /**
  190. * @param array $params
  191. * @param \Closure|null $callback
  192. * @param bool $is_custom_pwd //是否自定义密码
  193. * @return bool {*}
  194. * @throws Exception
  195. * @throws \yii\base\Exception
  196. * @msg: 添加一个门店,同时添加门店后台的管理员。默认账号是门店绑定手机号,默认登录密码是手机号后六位
  197. */
  198. public static function addStore(array $params, \Closure $callback = null, bool $is_custom_pwd = false)
  199. {
  200. //查询业务员
  201. $business = User::find()
  202. ->andWhere(['mall_id' => $params['mall_id']])
  203. ->andWhere([
  204. 'or',
  205. ['id' => $params['business_id']],
  206. ['mobile' => $params['business_id']]
  207. ])
  208. ->one();
  209. if(empty($business))
  210. {
  211. self::$static_error = '推广员不存在';
  212. return false;
  213. }
  214. $business=HappinessBusiness::find()->where(['mall_id' => $params['mall_id'], 'user_id' => $business->id])
  215. ->one();
  216. if(empty($business))
  217. {
  218. self::$static_error = '用户'.$business->id.'不是推广员';
  219. return false;
  220. }
  221. $params['business_id'] = $business->user_id;
  222. $transaction = \Yii::$app->db->beginTransaction();
  223. $res = self::setData($params, $is_custom_pwd);
  224. if (false === $res) {
  225. $transaction->rollBack();
  226. return false;
  227. }
  228. $pwd_str = substr($params['shop_mobile'], -6);
  229. if ($is_custom_pwd) $pwd_str = $params['password'];
  230. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  231. $admin_info = [
  232. 'username' => $params['shop_mobile'],
  233. 'account' => $params['shop_owner'],
  234. 'password' => $pwd_hash_str,
  235. 'mall_id' => $params['mall_id'],
  236. 'admin_type' => 2,
  237. 'status' => StatusEnum::ENABLED,
  238. 'user_id' => $params['user_id'],
  239. 'store_id' => $res->id,
  240. ];
  241. $admin_model = HappinessStoreAdmin::find()
  242. ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'username' => $params['shop_mobile']])
  243. ->one();
  244. if (!empty($admin_model) && $admin_model['store_id'] != $res->id) {
  245. $transaction->rollBack();
  246. self::$static_error = '门店管理员账号已经存在';
  247. return false;
  248. }
  249. $admin_model = new HappinessStoreAdmin();
  250. $admin_model->loadDefaultValues();
  251. $admin_model->attributes = $admin_info;
  252. $admin_model->updated_at = time();
  253. $admin_model->created_at = time();
  254. $res_ad = $admin_model->save();
  255. if (false === $res_ad) {
  256. $transaction->rollBack();
  257. self::$static_error = $admin_model->getErrorMessage();
  258. return false;
  259. }
  260. // 小店入驻新增参数(斗拱银行卡信息)
  261. $cardInfoField = ['card_type','card_name','card_no','prov_id','area_id','branch_code','cert_type','cert_no','cert_validity_type','cert_begin_date','cert_end_date','mp','is_settle_default'];
  262. $cardInfoInsert = [];
  263. foreach ($cardInfoField as $field) {
  264. $cardInfoInsert[$field] = $params[$field] ? $params[$field] : 0;
  265. }
  266. $cardInfoInsert['store_id'] = $res->id;
  267. // $cardInfoInsert = [
  268. // 'card_type' => 2,
  269. // 'card_name' => '史晨',
  270. // 'card_no' => 6212260200076546403,
  271. // 'prov_id' => 1,
  272. // 'area_id' => 1,
  273. // 'branch_code' => '',
  274. // 'cert_type' => '00',
  275. // 'cert_no' => '371481199602054512',
  276. // 'cert_validity_type' => '',
  277. // 'cert_begin_date' => 20250922,
  278. // 'cert_end_date' => 20350922,
  279. // 'mp' => 18600648353,
  280. // 'is_settle_default' => 'Y',
  281. // 'store_id' => 23
  282. // ];
  283. $storeCardModel = new HappinessStoreCardInfo();
  284. $storeCardModel->attributes = $cardInfoInsert;
  285. $cardInfoRes = $storeCardModel->save();
  286. if (false === $cardInfoRes){
  287. $transaction->rollBack();
  288. self::$static_error = $storeCardModel->getErrorMessage();
  289. return false;
  290. }
  291. $transaction->commit();
  292. return $res;
  293. }
  294. public static function saveStoreSetting(int $mall_id, array $data, array $addons_settings = [])
  295. {
  296. $transaction = \Yii::$app->db->beginTransaction();
  297. $store_info = [
  298. 'shop_status' => $data['shop_status'],
  299. 'shop_mobile' => $data['username'],
  300. 'business_time_start' => $data['business_time_start'],
  301. 'business_time_end' => $data['business_time_end'],
  302. 'id' => $data['store_id'],
  303. 'mall_id' => $mall_id,
  304. 'intra_city_distribution' => $data['intra_city_distribution'] ?? [],
  305. 'merchant_distribution' => $data['merchant_distribution'],
  306. 'self_mention' => $data['self_mention'],
  307. 'is_alone' => $data['is_alone'],
  308. 'distribution_scope' => $data['distribution_scope'],
  309. ];
  310. $store_res = self::setData($store_info);
  311. if (false === $store_res) {
  312. $transaction->rollBack();
  313. self::$static_error = self::getStaticError();
  314. return false;
  315. }
  316. //判断门店是否存在
  317. $admin_model = HappinessStoreAdmin::findOne(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'username' => $store_res->shop_mobile]);
  318. if (empty($admin_model)) {
  319. $transaction->rollBack();
  320. self::$static_error = '门店管理员账号不存在';
  321. return false;
  322. }
  323. // 因为上一步已经有账号变更现在只要密码变更就行
  324. if (!empty($data['password'])) {
  325. $admin_model->password = \Yii::$app->security->generatePasswordHash($data['password']);
  326. $admin_model->updated_at = time();
  327. $admin_res = $admin_model->save();
  328. if (empty($admin_res)) {
  329. $transaction->rollBack();
  330. self::$static_error = $admin_model->getErrorMessage();
  331. return false;
  332. }
  333. }
  334. $transaction->commit();
  335. return true;
  336. }
  337. /**
  338. * @name:
  339. * @msg: 装修页面获取门店列表
  340. * @param {int} $mall_id
  341. * @param {array} $params
  342. * @return {*}
  343. */
  344. public static function getStoreListInDiy(int $mall_id, array $params = [])
  345. {
  346. $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED];
  347. if (!empty($params['store_id'])) {
  348. $wheres[] = ['id' => $params['store_id']];
  349. }
  350. if (!empty($params['store_name'])) {
  351. $wheres[] = ['like', 'store_name', $params['store_name']];
  352. }
  353. if (!empty($params['storeowner'])) {
  354. $wheres[] = ['like', 'shop_owner', $params['storeowner']];
  355. }
  356. $params = [
  357. 'select' => '*,store_name as title,logo_img as img',
  358. 'where' => $wheres,
  359. 'order' => 'created_at desc',
  360. ];
  361. $store_list = self::listPage($params);
  362. if (false === $store_list) {
  363. return false;
  364. }
  365. return $store_list;
  366. }
  367. //获取门店更多
  368. public static function MoreStore(string $storeList, int $mall_id)
  369. {
  370. $params = [
  371. 'select' => '*,store_name as title,logo_img as img',
  372. 'where' => "id in ($storeList) and status = 1 and check_status = 1 and mall_id = $mall_id and shop_status = 1",
  373. 'order' => 'created_at desc',
  374. ];
  375. $store_list = self::listpage($params);
  376. if (false === $store_list) {
  377. return false;
  378. }
  379. $decoration = \Yii::$app->services->setting->addons->get($mall_id, HappinessEnum::ADDONS_NAME, 'decoration');
  380. foreach ($store_list['list'] as &$item) {
  381. if (!empty($decoration['custom_store_style'])) {
  382. $item['store_style'] = $item['show_style'] ?? 1;
  383. } else {
  384. $item['store_style'] = $decoration['default_store_style'] ?? 1;
  385. }
  386. }
  387. return $store_list;
  388. }
  389. /**
  390. * 结算货款
  391. *
  392. * @param int $order_id
  393. * @param int $store_id
  394. * @param string $order_no
  395. * @param integer $is_settlement 是否已经结算订(用于虚拟订单直接结算, 虚拟订单生成的佣金记录是已结算的,所以需要传1查询)
  396. * @return void
  397. */
  398. public static function settlementStoreCommission($order_id, $store_id, $order_no, $is_settlement = 0)
  399. {
  400. $store = \addons\Store\common\models\Store::findOne(['id' => $store_id]);
  401. $store_commission_model = StoreCommission::findOne(['order_no' => $order_no, 'is_settlement' => $is_settlement, 'status' => StatusEnum::ENABLED]);
  402. if ($store_commission_model) {
  403. // 配置信息
  404. $storeSetting = StoreSettings::findOne(['store_id' => $store->id]);
  405. $order = StoreOrder::findOne(['id' => $order_id]);
  406. if ($storeSetting->store_service_fee == 2 && MallAddons::isInstall($store->mall_id, AddonsEnum::ADDONS_NAME_HUIFU_PAY) && $order->payment_type == PayTypeEnum::HUIFUPAY) {
  407. $store->share_amount += $store_commission_model->amount_received; // 已分帐金额
  408. } else {
  409. $store->balance += $store_commission_model->amount_received; // 余额
  410. $store->total_settled_amount += $store_commission_model->amount_received; // 已结算总金额
  411. }
  412. $store->unsettled_amount -= $store_commission_model->amount_received; // 未结算
  413. if ($store->unsettled_amount < 0) $store->unsettled_amount = 0;
  414. $res = $store->save();
  415. if ($res == false) throw new Exception('o2o订单完成结算货款,更新店铺结算金额失败,order_no:' . $order_no);
  416. $store_commission_model->is_settlement = 1;
  417. $res = $store_commission_model->save();
  418. if ($res == false) throw new Exception('o2o订单完成结算货款,更新提成记录失败,order_no:' . $order_no);
  419. StoreStatistics::updateIncomeMoney($store, $store_commission_model->amount_received);
  420. $key = HappinessEnum::STORE_NOT_SETTLE_AMOUNTS . ':' . $store['mall_id'];
  421. $res = Yii::$app->redis->get($key);
  422. $amount_received_temp = $store_commission_model->amount_received;
  423. if (!empty($res)) $amount_received_temp = bcsub($res, $store_commission_model->amount_received, 2);
  424. if ($amount_received_temp < 0) $amount_received_temp = 0;
  425. Yii::$app->redis->set($key, $amount_received_temp);//待结算
  426. $amount_received_temp = $store_commission_model->amount_received;
  427. $key = HappinessEnum::STORE_SETTLE_AMOUNTS . ':' . $store['mall_id'];
  428. $res = \Yii::$app->redis->get($key);
  429. if (!empty($res)) $amount_received_temp = bcadd($res, $store_commission_model->amount_received, 2);
  430. Yii::$app->redis->set($key, $amount_received_temp);//已结算
  431. //结算积分
  432. if (!empty($store_commission_model['score_deduction_bear']) && $store_commission_model['score_deduction_bear'] == 1) {
  433. self::settlementScore($order_no, $store['user_id'], $store_commission_model['id']);
  434. }
  435. }
  436. }
  437. /**
  438. * 结算积分
  439. * @param $order_no
  440. * @param $store_user_id
  441. * @param $source_table_id
  442. * @return void
  443. */
  444. public static function settlementScore($order_no, $store_user_id, $source_table_id)
  445. {
  446. $store_order = StoreOrder::findOne(['order_no' => $order_no]);
  447. $store_order_detail_list = StoreOrderDetail::find()->where(['order_id' => $store_order['id'], 'is_feedback' => 0])->all();
  448. $order_detail_ids = array_column($store_order_detail_list, 'id');
  449. $source_table = StoreOrderDetail::tableName();
  450. CapitalLog::$capitalType = 'score';
  451. $score_frozen_log_list = CapitalLog::lists(
  452. [
  453. 'where' => [
  454. ['user_id' => $store_order['user_id']],
  455. ['change_type' => 'sub'],
  456. ['source_table' => $source_table],
  457. ['source_table_id' => $order_detail_ids],
  458. ['from_type' => UserWalletEnum::CONSUME],
  459. ['capital_type' => 'consume_store_order'],
  460. ]
  461. ]
  462. );
  463. if (!empty($score_frozen_log_list)) {
  464. $received_score = array_sum(array_column($score_frozen_log_list, 'change_num'));
  465. if ($received_score > 0) {
  466. $desc = '门店订单:' . $order_no . ':积分结算' . $received_score;
  467. $data = [
  468. 'message_id' => 0,
  469. 'mall_id' => $store_order['mall_id'],
  470. 'user_id' => $store_user_id,
  471. 'wallet_type' => 'score',
  472. 'is_frozen' => false,
  473. 'unfrozen' => false,
  474. 'money' => $received_score,
  475. 'desc' => $desc,
  476. 'source_table' => StoreCommission::tableName(),
  477. 'source_table_id' => $source_table_id,
  478. 'from_type' => AddonsEnum::ADDONS_NAME_STORE,
  479. 'capital_type' => 'consume_store_order',
  480. 'order_no' => $order_no
  481. ];
  482. UserWalletService::handle(0, $data);
  483. }
  484. }
  485. }
  486. /**
  487. * 通过ID获取手机号码
  488. *
  489. * @param integer $id
  490. * @return string|null
  491. */
  492. public static function getMobileById(int $mall_id, int $id)
  493. {
  494. return static::find()
  495. ->where(['mall_id' => $mall_id, 'id' => $id])
  496. ->select('shop_mobile')
  497. ->scalar();
  498. }
  499. public static function delStore($mall_id, $store_id)
  500. {
  501. $store = self::findOne(['id' => $store_id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  502. if (empty($store)) throw new \Exception('门店不存在');
  503. if ($store['balance'] > 0) throw new \Exception('店铺余额不为0,请先提现后再删除');
  504. $where = [
  505. ['yo.mall_id' => $mall_id],
  506. ['yo.store_id' => $store_id],
  507. ['o.order_status' => [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS, OrderStatusEnum::SING, OrderStatusEnum::TAKE]],
  508. ];
  509. $alias = 'yo';
  510. $order = "id desc";
  511. $join = [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"]];
  512. $select = 'o.*,yo.store_id';
  513. $params = compact('where', 'order', 'alias', 'join', 'select');
  514. $store_order = HappinessOrder::lists($params);
  515. if (!empty($store_order)) throw new \Exception('请等订单完成后再删除');
  516. $t = Yii::$app->db->beginTransaction();
  517. try {
  518. $store->status = StatusEnum::DELETE;
  519. $res = $store->save();
  520. if (empty($res)) throw new \Exception('删除失败');
  521. HappinessStoreAdmin::updateAll(['status' => StatusEnum::DELETE], ['store_id' => $store_id, 'mall_id' => $mall_id]);
  522. //删除门店关联的核销员
  523. HappinessClerk::updateAll(['Status' => StatusEnum::DELETE], ['mall_id' => $mall_id, 'store_id' => $store_id]);
  524. // StoreAssociatedUser::updateAll(['status' => StatusEnum::DELETE], ['store_id' => $store_id, 'mall_id' => $mall_id]);
  525. $t->commit();
  526. } catch (\Exception $e) {
  527. $t->rollBack();
  528. throw new \Exception($e->getMessage());
  529. }
  530. return true;
  531. }
  532. public static function fixData()
  533. {
  534. $mall_list = Mall::getEnableMallList(true);
  535. $mall_ids = array_column($mall_list, 'id');
  536. if (empty($mall_ids)) {
  537. echo '没有可用商城,不做处理 ... ' . PHP_EOL;
  538. return ExitCode::UNSPECIFIED_ERROR;
  539. }
  540. foreach ($mall_ids as $mall_id) {
  541. $key = HappinessEnum::STORE_NOT_SETTLE_AMOUNTS . ':' . $mall_id;
  542. $not_settle_amounts = StoreCommission::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 0])->sum('amount_received');
  543. if (empty($not_settle_amounts)) $not_settle_amounts = 0;
  544. \Yii::$app->redis->set($key, $not_settle_amounts);
  545. $key = HappinessEnum::STORE_SETTLE_AMOUNTS . ':' . $mall_id;
  546. $settle_amounts = StoreCommission::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 1])->sum('amount_received');
  547. if (empty($settle_amounts)) $settle_amounts = 0;
  548. \Yii::$app->redis->set($key, $settle_amounts);
  549. var_dump($mall_id);
  550. }
  551. }
  552. /**
  553. * 门店自提码设置
  554. *
  555. * @param integer $store_id
  556. * @return array
  557. */
  558. public static function getStorePickup(int $store_id)
  559. {
  560. $pickup = static::find()
  561. ->where(['id' => $store_id])
  562. ->select('pickup')
  563. ->scalar();
  564. return empty($pickup) ? [] : json_decode($pickup, true);
  565. }
  566. public function getBusiness(){
  567. return $this->hasOne(User::class, ['id' => 'business_id']);
  568. }
  569. }