0:h5申请,需要审核,记录审核人ID * @property int $limit_distance 匹配距离限制 */ class Store extends BaseActiveRecord { /** * {@inheritdoc} */ public static function tableName() { return '{{%addons_store}}'; } /** * {@inheritdoc} */ public function rules() { return [ [['mall_id', 'user_id', 'c_id', 'province_id', 'city_id', 'district_id', 'expire_time', 'user_nums', 'check_status', 'store_style', 'total_num','sales_volume', 'status', 'shop_status','scan_code_bind', 'created_at', 'updated_at','is_open_credit', 'payment_settlement_type','show_style','is_freeze', 'show_sync_goods','street_id','community_id','is_show_wechat_video_number', 'store_category_switch', 'is_show_coupon','default_store_cate_style', 'is_sync', 'sync_status', 'is_examine'], 'integer'], [['total_money', 'balance', 'total_expenses', 'total_order_money', 'total_settled_amount', 'unsettled_amount','praise', 'share_amount', 'limit_distance'], 'number'], [['shop_owner', 'store_name', 'store_desc', 'logo_img','store_img', 'license_img', 'address', 'longitude', 'latitude', 'check_remark', 'business_time_start', 'business_time_end', 'store_background', 'commission_authority', 'share_title', 'share_desc', 'share_pic', 'freight_type'], 'string', 'max' => 255], [['intra_city_distribution'],'string', 'max' => 500], [['shop_mobile'], 'string', 'max' => 11], [['device_id'], 'string', 'max' => 255], [['bannar_pic','introduce', 'pickup'], 'string'], [['wechat_video_number_id'],'safe'] ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'user_id' => '绑定会员id', 'shop_owner' => '店长姓名', 'shop_mobile' => '店长手机号', 'store_name' => '门店名称', 'c_id' => '门店分类', 'store_desc' => '门店简介', 'logo_img' => '门店logo', 'license_img' => '营业执照', 'province_id' => '省', 'city_id' => '市', 'district_id' => '区', 'street_id' => '街道', 'community_id' => '社区', 'address' => '详细地址', 'expire_time' => '失效时间,0代表无限期', 'total_money' => '累计收入', 'balance' => '当前余额', 'total_expenses' => '累计支出', 'user_nums' => '会员总数', 'longitude' => '经度', 'latitude' => '维度', 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过', 'check_remark' => '审核备注', 'business_time_start' => '营业时间开始', 'business_time_end' => '营业时间结束', 'store_style' => '门店样式 1:样式1;2:样式2', 'store_background' => '门店背景', 'commission_authority' => '分佣/营销权限', 'share_title' => '分享标题', 'share_desc' => '分享描述', 'share_pic' => '分享图片', 'freight_type' => '配送设置', 'intra_city_distribution' => '同城配送', 'total_order_money' => '订单总金额', 'total_num' => '总数', 'total_settled_amount' => '已结算总金额', 'unsettled_amount' => '未结算金额', 'status' => '状态[0禁用 1启用 -1删除]', 'shop_status' => '门店营业状态', 'scan_code_bind' => '门店收款码-扫码绑定开关:0:否;1:是', 'created_at' => '创建时间', 'updated_at' => '修改时间', 'payment_settlement_type'=>'门店货款结算方式,是否开启上传凭证,0:否;1:是', 'device_id' => '芸众喇叭设备id', 'show_style' => '门店装修风格', 'bannar_pic' => '门店轮播图', 'introduce' => '门店介绍', 'is_freeze' => '是否冻结', 'show_sync_goods' => '是否显示同步的商品[0否,1是]', 'pickup' => '', 'is_show_wechat_video_number' => '是否显示微信视频号:0否;1是', 'is_show_coupon' => '是否显示优惠券:0否;1是', 'wechat_video_number_id' => '微信视频号id', 'default_store_cate_style' => '门店分类样式', 'store_category_switch' => '门店分类开关', 'is_sync' => '是否需要同步:0:否,1:是', 'sync_status' => '同步状态:0:待同步;1:同步中;2:同步完成', 'is_examine' => '审核方式:-1:后台添加,0:h5申请,不用审核,>0:h5申请,需要审核,记录审核人ID', 'limit_distance' => '匹配距离限制', ]; } 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(); $settings = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'enter'); if (1 == $settings['time_status']) { $seconds = $settings['expired_days'] * 86400 + time(); $model->expire_time = $seconds; } else { $model->expire_time = 0; } $decorationSetting = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'decoration'); $model->show_style = $decorationSetting['default_store_style'] ?? 1; $model->default_store_cate_style = $decorationSetting['default_store_cate_style'] ?? 1; $model->store_category_switch = $decorationSetting['store_category_switch'] ?? 0; } if(!empty($params['pickup'])) $params['pickup'] = json_encode($params['pickup']); if(!empty($params['freight_type'])) $params['freight_type'] = json_encode($params['freight_type']); if (empty($model->intra_city_distribution)) $params['intra_city_distribution'] = []; if (isset($params['intra_city_distribution'])) $params['intra_city_distribution'] = json_encode($params['intra_city_distribution']); if(!empty($params['introduce'])) $params['introduce'] = json_encode($params['introduce']); if(!empty($params['bannar_pic'])) $params['bannar_pic'] = json_encode($params['bannar_pic']); if (!$model->load($params, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } $store_addmin = StoreAdmin::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()); } } if (!empty($params['id']) && MallAddons::isInstall($params['mall_id'], AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE)) { ReservationServiceUser::updateAll(['store_lng' => $model->longitude, 'store_lat' => $model->latitude], ['store_id' => $model->id]); } $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) { $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, 'store_id' => $res->id, ]; $admin_model = StoreAdmin::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 StoreAdmin(); $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; } if ($params['is_admin']) { self::updateStoreServiceRatio($params['mall_id'], $res->id); } $cate = StoreCate::findOne(['id'=>$res['c_id']]); if(!empty($cate['is_collect_service_fee'])&&!empty($res['check_status'])&&$res['check_status']==1){ $exist_setting = StoreSettings::find() ->where([ 'store_id' => $res['id'], ])->one(); $cashier_info = [ 'store_service_fee' => $cate['is_collect_service_fee'], 'store_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'], 'cachier_service_ratio' => $exist_setting['store_service_ratio'] ?? $cate['collect_service_fee'], 'store_settle_type' => 1, 'store_settle_scene' =>1, 'store_id' => $res['id'], ]; StoreSettings::setData($res->mall_id,$cashier_info); } // 设置让利 if (!empty($callback)) { $ret = $callback->call(new self(), $res->id); if (is_string($ret)) { $transaction->rollBack(); self::$static_error = $ret; return false; } } $transaction->commit(); return $res; } public static function updateStoreServiceRatio($mall_id, $store_id) { $basic = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic'); if ($basic['service_fee'] == 1 && $basic['service_fee_type'] == 1) { StoreSettings::setData($mall_id, [ 'store_id'=> $store_id, 'store_service_fee'=> 1, 'store_service_ratio'=> $basic['max_platform_ratio'], 'cachier_service_ratio'=> $basic['max_platform_ratio'], ]); } } /** * @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, 'check_status' => StoreEnum::CHECK_ADOPT]; 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(array $data) { $storeList = $data['store']; $mall_id = $data['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, StoreEnum::ADDONS_NAME, 'decoration'); foreach ($store_list['list'] as &$item){ // 根据当前定位计算门店距离 $item['distance'] = 0; if (!empty($data['longitude']) && !empty($data['latitude'])) { $distance = round( 2 * 6378.137 * ASIN( SQRT( POW( SIN( PI() * ($data['longitude'] - $item['longitude']) / 360 ), 2 ) + COS(PI() * $data['latitude'] / 180) * COS($item['latitude'] * PI() / 180) * POW( SIN( PI() * ($data['latitude'] - $item['latitude']) / 360 ), 2 ) ) ) ,1); $item['distance'] = bcmul($distance, 1, 1); } 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 = 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 = StoreEnum::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 = StoreEnum::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 = Store::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,请先提现后再删除'); $store_order = StoreOrder::getOne([ ['store_id'=>$store_id], ['order_status'=>[OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING]], ['is_feedback'=>[0,2]] ]); 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('删除失败'); StoreAdmin::updateAll(['status'=>StatusEnum::DELETE],['store_id'=>$store_id,'mall_id'=>$mall_id]); StoreMerchantReduceProfit::updateAll(['status' => StatusEnum::DELETE], ['source_id' => $store_id, 'mall_id' => $mall_id, 'source' => StoreEnum::ADDONS_NAME, 'status' => StatusEnum::ENABLED]); 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 = StoreEnum::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 = StoreEnum::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 getCate() { return $this->hasOne(StoreCate::class, ['id' => 'c_id']); } /** * @param integer $mall_id * @return array */ public static function getStoreList(int $mall_id) { return static::find() ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]) ->select('id, store_name') ->asArray() ->all(); } }