255], [['huifu_no'], 'string', 'max' => 100], [['intra_city_distribution','no'], 'string', 'max' => 500], [['shop_mobile'], 'string', 'max' => 11], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'user_id' => '绑定会员id', 'no' => '编号', 'business_id'=>'业务员ID', 'shop_owner' => '店长姓名', 'shop_mobile' => '店长手机号', 'store_name' => '门店名称', 'store_desc' => '门店简介', 'logo_img' => '门店logo', 'license_img' => '营业执照', 'province_id' => '省', 'city_id' => '市', 'district_id' => '区', 'street_id' => '街道', 'community_id' => '社区', 'address' => '详细地址', 'expire_time' => '失效时间,0代表无限期', 'total_money' => '累计收入', 'balance' => '当前余额', 'frozen_balance' => '冻结余额', 'total_expenses' => '累计支出', 'user_nums' => '会员总数', 'longitude' => '经度', 'latitude' => '维度', 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过', 'check_remark' => '审核备注', 'business_time_start' => '营业时间开始', 'business_time_end' => '营业时间结束', 'freight_type' => '配送设置', 'intra_city_distribution' => '同城配送', 'total_order_money' => '订单总金额', 'total_num' => '总数', 'total_settled_amount' => '已结算总金额', 'unsettled_amount' => '未结算金额', 'refund_money' => '退款金额', 'refund_num'=>'退款订单数量', 'file_money' => '罚款金额', 'status' => '状态[0禁用 1启用 -1删除]', 'shop_status' => '门店营业状态', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } public static function setData(array $params, bool $is_custom_pwd = false) { $t = Yii::$app->db->beginTransaction(); try { if (!empty($params['id'])) { $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($model)) throw new \Exception('门店不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); // $model->expire_time = strtotime("+1 year"); } // if (!empty($params['freight_type'])) $params['freight_type'] = json_encode($params['freight_type']); if (!empty($params['intra_city_distribution'])) $params['intra_city_distribution'] = json_encode($params['intra_city_distribution']); if (!$model->load($params, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } $store_addmin = HappinessStoreAdmin::findOne(['mall_id' => $params['mall_id'], 'store_id' => $model->id, 'status' => StatusEnum::ENABLED]); // 同步一下账号 if ($store_addmin) { if ($model->shop_mobile != $store_addmin->username) { $pwd_str = substr($model->shop_mobile, -6); if ($is_custom_pwd) $pwd_str = $params['password']; $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str); $store_addmin->username = $model->shop_mobile; $store_addmin->password = $pwd_hash_str; if (!$store_addmin->save()) throw new \Exception($store_addmin->getErrorMessage()); } } $t->commit(); return $model; } catch (\Exception $e) { $t->rollBack(); self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile(); return false; } } /** * @param array $params * @param \Closure|null $callback * @param bool $is_custom_pwd //是否自定义密码 * @return bool {*} * @throws Exception * @throws \yii\base\Exception * @msg: 添加一个门店,同时添加门店后台的管理员。默认账号是门店绑定手机号,默认登录密码是手机号后六位 */ public static function addStore(array $params, \Closure $callback = null, bool $is_custom_pwd = false) { //查询业务员 $business = User::find() ->andWhere(['mall_id' => $params['mall_id']]) ->andWhere([ 'or', ['id' => $params['business_id']], ['mobile' => $params['business_id']] ]) ->one(); if(empty($business)) { self::$static_error = '推广员不存在'; return false; } $business=HappinessBusiness::find()->where(['mall_id' => $params['mall_id'], 'user_id' => $business->id]) ->one(); if(empty($business)) { self::$static_error = '用户'.$business->id.'不是推广员'; return false; } $params['business_id'] = $business->user_id; $transaction = \Yii::$app->db->beginTransaction(); $res = self::setData($params, $is_custom_pwd); if (false === $res) { $transaction->rollBack(); return false; } $pwd_str = substr($params['shop_mobile'], -6); if ($is_custom_pwd) $pwd_str = $params['password']; $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str); $admin_info = [ 'username' => $params['shop_mobile'], 'account' => $params['shop_owner'], 'password' => $pwd_hash_str, 'mall_id' => $params['mall_id'], 'admin_type' => 2, 'status' => StatusEnum::ENABLED, 'user_id' => $params['user_id'], 'store_id' => $res->id, ]; $admin_model = HappinessStoreAdmin::find() ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'username' => $params['shop_mobile']]) ->one(); if (!empty($admin_model) && $admin_model['store_id'] != $res->id) { $transaction->rollBack(); self::$static_error = '门店管理员账号已经存在'; return false; } $admin_model = new HappinessStoreAdmin(); $admin_model->loadDefaultValues(); $admin_model->attributes = $admin_info; $admin_model->updated_at = time(); $admin_model->created_at = time(); $res_ad = $admin_model->save(); if (false === $res_ad) { $transaction->rollBack(); self::$static_error = $admin_model->getErrorMessage(); return false; } // 小店入驻新增参数(斗拱银行卡信息) $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']; $cardInfoInsert = []; foreach ($cardInfoField as $field) { $cardInfoInsert[$field] = $params[$field] ? $params[$field] : 0; } $cardInfoInsert['store_id'] = $res->id; // $cardInfoInsert = [ // 'card_type' => 2, // 'card_name' => '史晨', // 'card_no' => 6212260200076546403, // 'prov_id' => 1, // 'area_id' => 1, // 'branch_code' => '', // 'cert_type' => '00', // 'cert_no' => '371481199602054512', // 'cert_validity_type' => '', // 'cert_begin_date' => 20250922, // 'cert_end_date' => 20350922, // 'mp' => 18600648353, // 'is_settle_default' => 'Y', // 'store_id' => 23 // ]; $storeCardModel = new HappinessStoreCardInfo(); $storeCardModel->attributes = $cardInfoInsert; $cardInfoRes = $storeCardModel->save(); if (false === $cardInfoRes){ $transaction->rollBack(); self::$static_error = $storeCardModel->getErrorMessage(); return false; } $transaction->commit(); return $res; } 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'], 'id' => $data['store_id'], 'mall_id' => $mall_id, 'intra_city_distribution' => $data['intra_city_distribution'] ?? [], 'merchant_distribution' => $data['merchant_distribution'], 'self_mention' => $data['self_mention'], 'is_alone' => $data['is_alone'], 'distribution_scope' => $data['distribution_scope'], ]; $store_res = self::setData($store_info); if (false === $store_res) { $transaction->rollBack(); self::$static_error = self::getStaticError(); return false; } //判断门店是否存在 $admin_model = HappinessStoreAdmin::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; } } $transaction->commit(); return true; } /** * @name: * @msg: 装修页面获取门店列表 * @param {int} $mall_id * @param {array} $params * @return {*} */ public static function getStoreListInDiy(int $mall_id, array $params = []) { $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]; if (!empty($params['store_id'])) { $wheres[] = ['id' => $params['store_id']]; } if (!empty($params['store_name'])) { $wheres[] = ['like', 'store_name', $params['store_name']]; } if (!empty($params['storeowner'])) { $wheres[] = ['like', 'shop_owner', $params['storeowner']]; } $params = [ 'select' => '*,store_name as title,logo_img as img', 'where' => $wheres, 'order' => 'created_at desc', ]; $store_list = self::listPage($params); if (false === $store_list) { return false; } return $store_list; } //获取门店更多 public static function MoreStore(string $storeList, int $mall_id) { $params = [ 'select' => '*,store_name as title,logo_img as img', 'where' => "id in ($storeList) and status = 1 and check_status = 1 and mall_id = $mall_id and shop_status = 1", 'order' => 'created_at desc', ]; $store_list = self::listpage($params); if (false === $store_list) { return false; } $decoration = \Yii::$app->services->setting->addons->get($mall_id, HappinessEnum::ADDONS_NAME, 'decoration'); foreach ($store_list['list'] as &$item) { if (!empty($decoration['custom_store_style'])) { $item['store_style'] = $item['show_style'] ?? 1; } else { $item['store_style'] = $decoration['default_store_style'] ?? 1; } } return $store_list; } /** * 结算货款 * * @param int $order_id * @param int $store_id * @param string $order_no * @param integer $is_settlement 是否已经结算订(用于虚拟订单直接结算, 虚拟订单生成的佣金记录是已结算的,所以需要传1查询) * @return void */ public static function settlementStoreCommission($order_id, $store_id, $order_no, $is_settlement = 0) { $store = \addons\Store\common\models\Store::findOne(['id' => $store_id]); $store_commission_model = StoreCommission::findOne(['order_no' => $order_no, 'is_settlement' => $is_settlement, 'status' => StatusEnum::ENABLED]); if ($store_commission_model) { // 配置信息 $storeSetting = StoreSettings::findOne(['store_id' => $store->id]); $order = StoreOrder::findOne(['id' => $order_id]); if ($storeSetting->store_service_fee == 2 && MallAddons::isInstall($store->mall_id, AddonsEnum::ADDONS_NAME_HUIFU_PAY) && $order->payment_type == PayTypeEnum::HUIFUPAY) { $store->share_amount += $store_commission_model->amount_received; // 已分帐金额 } else { $store->balance += $store_commission_model->amount_received; // 余额 $store->total_settled_amount += $store_commission_model->amount_received; // 已结算总金额 } $store->unsettled_amount -= $store_commission_model->amount_received; // 未结算 if ($store->unsettled_amount < 0) $store->unsettled_amount = 0; $res = $store->save(); if ($res == false) throw new Exception('o2o订单完成结算货款,更新店铺结算金额失败,order_no:' . $order_no); $store_commission_model->is_settlement = 1; $res = $store_commission_model->save(); if ($res == false) throw new Exception('o2o订单完成结算货款,更新提成记录失败,order_no:' . $order_no); StoreStatistics::updateIncomeMoney($store, $store_commission_model->amount_received); $key = HappinessEnum::STORE_NOT_SETTLE_AMOUNTS . ':' . $store['mall_id']; $res = Yii::$app->redis->get($key); $amount_received_temp = $store_commission_model->amount_received; if (!empty($res)) $amount_received_temp = bcsub($res, $store_commission_model->amount_received, 2); if ($amount_received_temp < 0) $amount_received_temp = 0; Yii::$app->redis->set($key, $amount_received_temp);//待结算 $amount_received_temp = $store_commission_model->amount_received; $key = HappinessEnum::STORE_SETTLE_AMOUNTS . ':' . $store['mall_id']; $res = \Yii::$app->redis->get($key); if (!empty($res)) $amount_received_temp = bcadd($res, $store_commission_model->amount_received, 2); Yii::$app->redis->set($key, $amount_received_temp);//已结算 //结算积分 if (!empty($store_commission_model['score_deduction_bear']) && $store_commission_model['score_deduction_bear'] == 1) { self::settlementScore($order_no, $store['user_id'], $store_commission_model['id']); } } } /** * 结算积分 * @param $order_no * @param $store_user_id * @param $source_table_id * @return void */ public static function settlementScore($order_no, $store_user_id, $source_table_id) { $store_order = StoreOrder::findOne(['order_no' => $order_no]); $store_order_detail_list = StoreOrderDetail::find()->where(['order_id' => $store_order['id'], 'is_feedback' => 0])->all(); $order_detail_ids = array_column($store_order_detail_list, 'id'); $source_table = StoreOrderDetail::tableName(); CapitalLog::$capitalType = 'score'; $score_frozen_log_list = CapitalLog::lists( [ 'where' => [ ['user_id' => $store_order['user_id']], ['change_type' => 'sub'], ['source_table' => $source_table], ['source_table_id' => $order_detail_ids], ['from_type' => UserWalletEnum::CONSUME], ['capital_type' => 'consume_store_order'], ] ] ); if (!empty($score_frozen_log_list)) { $received_score = array_sum(array_column($score_frozen_log_list, 'change_num')); if ($received_score > 0) { $desc = '门店订单:' . $order_no . ':积分结算' . $received_score; $data = [ 'message_id' => 0, 'mall_id' => $store_order['mall_id'], 'user_id' => $store_user_id, 'wallet_type' => 'score', 'is_frozen' => false, 'unfrozen' => false, 'money' => $received_score, 'desc' => $desc, 'source_table' => StoreCommission::tableName(), 'source_table_id' => $source_table_id, 'from_type' => AddonsEnum::ADDONS_NAME_STORE, 'capital_type' => 'consume_store_order', 'order_no' => $order_no ]; UserWalletService::handle(0, $data); } } } /** * 通过ID获取手机号码 * * @param integer $id * @return string|null */ public static function getMobileById(int $mall_id, int $id) { return static::find() ->where(['mall_id' => $mall_id, 'id' => $id]) ->select('shop_mobile') ->scalar(); } public static function delStore($mall_id, $store_id) { $store = self::findOne(['id' => $store_id, 'mall_id' => $mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (empty($store)) throw new \Exception('门店不存在'); if ($store['balance'] > 0) throw new \Exception('店铺余额不为0,请先提现后再删除'); $where = [ ['yo.mall_id' => $mall_id], ['yo.store_id' => $store_id], ['o.order_status' => [OrderStatusEnum::PAY, OrderStatusEnum::SHIPMENTS, OrderStatusEnum::SING, OrderStatusEnum::TAKE]], ]; $alias = 'yo'; $order = "id desc"; $join = [['LEFT JOIN', Order::tableName(). ' as o', "o.id = yo.order_id"]]; $select = 'o.*,yo.store_id'; $params = compact('where', 'order', 'alias', 'join', 'select'); $store_order = HappinessOrder::lists($params); if (!empty($store_order)) throw new \Exception('请等订单完成后再删除'); $t = Yii::$app->db->beginTransaction(); try { $store->status = StatusEnum::DELETE; $res = $store->save(); if (empty($res)) throw new \Exception('删除失败'); HappinessStoreAdmin::updateAll(['status' => StatusEnum::DELETE], ['store_id' => $store_id, 'mall_id' => $mall_id]); //删除门店关联的核销员 HappinessClerk::updateAll(['Status' => StatusEnum::DELETE], ['mall_id' => $mall_id, 'store_id' => $store_id]); // StoreAssociatedUser::updateAll(['status' => StatusEnum::DELETE], ['store_id' => $store_id, 'mall_id' => $mall_id]); $t->commit(); } catch (\Exception $e) { $t->rollBack(); throw new \Exception($e->getMessage()); } return true; } public static function fixData() { $mall_list = Mall::getEnableMallList(true); $mall_ids = array_column($mall_list, 'id'); if (empty($mall_ids)) { echo '没有可用商城,不做处理 ... ' . PHP_EOL; return ExitCode::UNSPECIFIED_ERROR; } foreach ($mall_ids as $mall_id) { $key = HappinessEnum::STORE_NOT_SETTLE_AMOUNTS . ':' . $mall_id; $not_settle_amounts = StoreCommission::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 0])->sum('amount_received'); if (empty($not_settle_amounts)) $not_settle_amounts = 0; \Yii::$app->redis->set($key, $not_settle_amounts); $key = HappinessEnum::STORE_SETTLE_AMOUNTS . ':' . $mall_id; $settle_amounts = StoreCommission::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_settlement' => 1])->sum('amount_received'); if (empty($settle_amounts)) $settle_amounts = 0; \Yii::$app->redis->set($key, $settle_amounts); var_dump($mall_id); } } /** * 门店自提码设置 * * @param integer $store_id * @return array */ public static function getStorePickup(int $store_id) { $pickup = static::find() ->where(['id' => $store_id]) ->select('pickup') ->scalar(); return empty($pickup) ? [] : json_decode($pickup, true); } public function getBusiness(){ return $this->hasOne(User::class, ['id' => 'business_id']); } }