| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use app\models\StoreAccountLog;
- /**
- * Class Store
- * @package app\models
- * @property integer $id
- * @property integer $admin_id
- * @property integer $is_delete
- * @property string $share_setting
- * @property float $transfer_profit
- * @property float $scan_transfer_profit
- * @property integer $is_alliance
- * @property string $douyin_url
- * @property string $recommend_keyword
- * @property string $coordinate
- * @property string $address
- * @property string $parent_id
- * @property string $app_auth_token
- * @property string $shop_id
- * @property integer $created_at
- * @property integer $updated_at
- * @property string $info
- * @property integer $store_type
- * @property string $category_id
- * @property string $name
- * @property string $logo
- * @property string $store_front_photo
- * @property integer $province_id
- * @property integer $city_id
- * @property integer $district_id
- * @property integer $sales
- * @property integer $rank
- * @property integer $per_spend
- * @property string $tags
- * @property string $user_name
- * @property string $contact_tel
- * @property string $wechat_msg_token
- * @property string $delivery_category
- * @property integer $delivery_type
- * @property integer $salesman_id
- * @property string $md_agreement
- * @property string $md_banner
- * @property string $open_price
- * @property string $renew_price
- * @property int $end_time
- * @property string $profit
- * @property string $food_payment
- * @property int $food_pay_type
- * @property int $open_status
- * @property int $shop_time_type
- * @property int $is_time_forbid
- * @property int $is_set_distance
- * @property int $distance
- * @property string $s_time
- * @property string $e_time
- * @property int $store_admin
- * @property double $rate
- * @property int $store_share_switch // 店铺间分销开关
- * @property int $store_share_type // 店铺间分销佣金类型:0,百分比 1,固定金额
- * @property string $store_share_value // 店铺间分销值
- * @property int $business_model
- * @property string $auth
- * @property int $self_rebate_switch
- * @property float $self_rebate_value
- * @property string $device_name
- * @property string $is_auth_trans
- * @property int $is_use_default
- * @property string $kefu_id
- * @property float $longitude
- * @property float $latitude
- * @property float $price
- * @property float $total_price
- * @property float $league_price
- * @property int $wechat_delivery_type
- * @property string $store_number
- * @property string $cusid
- * @property float $ratio
- * @property string $yunst_user_id
- * @property float $food_transfer_profit
- * @property float $cashier_transfer_profit
- * @property float $pv_transfer_profit
- * @property string $store_url_info
- * @property string $small_model_proportion
- */
- class Store extends \yii\db\ActiveRecord
- {
- const TYPE_BRAND = 1;
- const TYPE_STORE = 2;
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%store}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['rank', 'per_spend', 'self_rebate_value'], 'number'],
- [['id', 'admin_id', 'salesman_id', 'is_delete', 'is_alliance', 'created_at', 'updated_at', 'parent_id',
- 'category_id', 'province_id', 'city_id', 'district_id', 'sales', 'delivery_type', 'end_time', 'food_pay_type',
- 'open_status', 'shop_time_type', 'is_time_forbid', 'is_set_distance', 'store_admin', 'store_share_switch',
- 'store_share_type', 'business_model', 'self_rebate_switch', 'is_auth_trans', 'is_use_default', 'wechat_delivery_type','is_platform_transfers'], 'integer'],
- [['delivery_category', 'share_setting', 'douyin_url', 'recommend_keyword', 'app_auth_token', 'coordinate',
- 'address', 'shop_id', 'name', 'logo','store_front_photo', 'tags', 'contact_tel', 'user_name', 'wechat_msg_token', 'md_banner',
- 'md_agreement', 'food_payment', 's_time', 'e_time', 'auth', 'device_name', 'kefu_id', 'store_number', 'cusid', 'yunst_user_id'], 'string'],
- [['transfer_profit', 'open_price', 'renew_price', 'profit', 'distance', 'rate', 'store_share_value', 'latitude', 'longitude', 'price', 'total_price', 'scan_transfer_profit', 'ratio', 'food_transfer_profit', 'cashier_transfer_profit','small_model_proportion'], 'double'],
- [['created_at', 'update_at', 'info','store_url_info'], 'safe']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'admin_id' => '管理员ID',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'share_setting' => 'saas分销配置',
- 'transfer_profit' => '商城让利比例',
- 'scan_transfer_profit' => '当面付让利比例',
- 'is_alliance' => '是否加入联盟', // 0=未加入, 1=加入, 2=待审核
- 'recommend_keyword' => '店铺推荐关键词',
- 'is_delete' => '是否已删除',
- 'douyin_url' => '抖口令',
- 'parent_id' => '父级ID',
- 'coordinate' => '坐标',
- 'address' => '商城地址',
- 'shop_id' => '蚂蚁门店id',
- 'app_auth_token' => '授权token',
- 'category_id' => '分类id',
- 'info' => '类目筛选信息',
- 'name' => '商城名称',
- 'logo' => '商城logo',
- 'province_id' => '省id',
- 'city_id' => '市id',
- 'district_id' => '区域id',
- 'sales' => '销量',
- 'rank' => '评分',
- 'per_spend' => '人均花费',
- 'tags' => '标签',
- 'wechat_msg_token' => '微信推送消息token',
- 'open_price'=>"开铺金额",
- 'renew_price'=>"续费金额",
- 'end_time'=>"有效期",
- 'profit'=>"分佣金额",
- 'store_admin' => '商城管理员saas_user_id',
- 'rate' => '销售溢价比',
- 'store_share_switch' => '店铺间分销开关',
- 'store_share_type' => '店铺间分销佣金类型',
- 'store_share_value' => '店铺间分销值',
- 'business_model' => '运营模式',
- 'auth' => '商盟对店铺的权限控制',
- 'device_name' => '语音播报设备名称',
- 'is_auth_trans' => '是否自动转单',
- 'is_use_default' => '是否使用默认费用设置',
- 'kefu_id' => '客服id',
- 'price' => '店铺可提现金额',
- 'total_price' => '店铺累计金额',
- 'league_price' => '店铺联盟券金额',
- 'wechat_delivery_type' => '微信配送方式0即时配送(旧) 1同城配送',
- 'store_number' => '商城编号',
- 'cusid' => '进件商户号',
- 'ratio' => '通联签约抽佣比例',
- 'yunst_user_id' => '通联生成的会员UID',
- ];
- }
- public function afterSave($insert, $changedAttributes)
- {
- parent::afterSave($insert, $changedAttributes);
- // 云仓溢价比例修改后,同步云仓商品价格
- if (isset($changedAttributes['rate']) && $changedAttributes['rate'] != $this->rate) {
- $job = new \app\jobs\SyncCloudGoodsPriceJob();
- $job->store_id = $this->id;
- \queue_push($job);
- }
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- if (\Yii::$app->prod_is_shangmeng()) {
- // 商盟版本没有独立运营, 如果传的是1,改成默认2
- if ($this->business_model == 1 || empty($this->business_model)) {
- $this->business_model = 2;
- }
- }
- if($insert){
- $count = 0;
- if($this->salesman_id > 0){
- $salesman = Salesman::findOne($this->salesman_id);
- $currentAdmin = Admin::findOne($salesman->admin_id);
- }else{
- $currentAdmin = Admin::findOne($this->admin_id);
- }
- if($currentAdmin && $currentAdmin->type == Admin::ADMIN_TYPE_BD_AGENT){
- $count = Store::find()->where(['is_delete' => 0])
- ->andWhere([
- 'or',
- ['admin_id' => $currentAdmin->id],
- ['salesman_id' => Salesman::find()->where(['admin_id' => $currentAdmin->id, 'is_delete' => 0])->select('id')]
- ])->count();
- $canStoreCount = $currentAdmin->max_app_count - $count;
- if($canStoreCount <= 0){
- \Yii::error([__METHOD__, $this->admin_id, $currentAdmin->max_app_count, $count, $this->attributes, '商城数量超过限制']);
- $this->addError('max_app_count', '操作失败,商城数量超过限制'. $count . '/' . $currentAdmin->max_app_count);
- return false;
- }
- }
- }
- return true;
- }
- return false;
- }
- public function getCategory(){
- return $this->hasOne(SaasCategory::className(),['id'=>'category_id']);
- }
- public function getOrder(){
- return $this->hasMany(Order::className(),['store_id'=>'id']);
- }
- public function getReorder(){
- return $this->hasMany(StoreReOrder::className(),['store_id'=>'id']);
- }
- public function getAdmin(){
- return $this->hasOne(Admin::className(),['id'=>'admin_id']);
- }
- public static function nameList() {
- return self::find()->andWhere(['is_delete' => 0])->asArray()->select('id,name,salesman_id')->all();
- }
- // 是否进件 // yunst是否是通联支付
- public static function hasIncoming($store_id, $platform = 'wechat')
- {
- if (($platform == 'alipay') || is_alipay_platform()) {
- $data = StoreAliMini::findOne(['store_id' => $store_id, 'is_cancel' => 0, 'batch_status' => [2, 6]]);
- if (!$data) {
- return false;
- }
- return true;
- } elseif (($platform == 'wechat' || $platform == 'yunst') || is_wechat_platform()) {
- $data = MerchantInfo::find()->where([
- 'bind_store_id' => $store_id,
- 'status' => 3,
- 'is_use' => 1,
- 'is_yunst' => intval($platform == 'yunst'),
- 'is_delete' => 0
- ])->one();
- if (!$data) {
- return false;
- }
- if (empty($data->sub_mch_id)) {
- return false;
- }
- return true;
- }
- }
- public static function isStoreCurrency($store_id)
- {
- $saleman_id = Store::find()->where(['id'=>$store_id,'is_delete'=>0])->select('salesman_id')->scalar();
- if (!$saleman_id) return false;
- $store_id = Salesman::find()->alias('s')->leftJoin(['a'=>Admin::tableName()],'s.admin_id = a.id')
- ->where(['s.id'=>$store_id,'s.is_delete'=>0])->select('a.store_id')->scalar();
- $coin = \Yii::$app->db->createCommand("SELECT money,user_percent FROM cyy_user_wallet_coin where store_id= :store_id and is_status=1 and shop_status = 1")
- ->bindValues([
- ':store_id' => $store_id,
- ])->queryOne();
- if (!$coin || $coin['money'] <= 0) return false;
- $setting = Option::getShareDefaultSetting($store_id);
- if (!isset($setting['currceny_percent_scan']) || $setting['currceny_percent_scan']<=0) return false;
- $data['percent'] = ($setting['currceny_percent_scan'] * $coin['user_percent'])/100;
- $data['money'] = $coin['money'];
- return $data;
- }
- public static function is_red_package($store_id)
- {
- $coin = Option::getShareSaleSetting($store_id);
- $setting = Option::getShareDefaultSetting($store_id);
- if (!isset($setting['scan_qr_code_percent']) || $setting['scan_qr_code_percent']<=0) return false;
- if (!isset($coin['string_code_store_red_packet']) || $coin['string_code_store_red_packet']<=0) return false;
- $data['percent'] = ($setting['scan_qr_code_percent'] * $coin['string_code_store_red_packet'])/100;
- return $data;
- }
- // 店铺累计金额
- public static function getTotalMoney($store)
- {
- return $store->total_price;
- }
- // 店铺金额
- public static function getMoney($store)
- {
- return $store->price;
- }
- // 店铺减少金额
- public static function subMoney($store, $price, $desc = '账户提现',$cash_id=0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $before = $store->price;
- $store->price -= $price;
- if (!$store->save()) {
- throw new \Exception('商城金额减少失败');
- }
- $log = new StoreAccountLog();
- $log->store_id = $store->id;
- $log->price = $price;
- $log->desc = $desc;
- $log->before = $before;
- $log->after = $store->price;
- $log->type = 2;
- $log->time = time();
- $log->order_id = $cash_id;
- if (!$log->save()) {
- throw new \Exception('商城金额日志记录失败');
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return false;
- }
- return true;
- }
- // 店铺增加金额
- public static function addMoney($store, $price, $desc = '用户下单', $order_id = 0, $user_id = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if($order_id){
- $StoreAccountLog = StoreAccountLog::findOne(['order_id' => $order_id, 'user_id' => $user_id, 'store_id' => $store->id, 'type' => 1]);
- if($StoreAccountLog){
- throw new \Exception('此订单已存在商城金额增加记录');
- }
- }
- $before = $store->price;
- $store->price += $price;
- $store->total_price += $price;
- if (!$store->save()) {
- throw new \Exception('商城金额增加失败');
- }
- $log = new StoreAccountLog();
- $log->store_id = $store->id;
- $log->order_id = $order_id;
- $log->user_id = $user_id;
- $log->price = $price;
- $log->desc = $desc;
- $log->before = $before;
- $log->after = $store->price;
- $log->type = 1;
- $log->time = time();
- if (!$log->save()) {
- throw new \Exception('商城金额日志记录失败');
- }
- $t->commit();
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return false;
- }
- return true;
- }
- //小程序是否开启审核
- public static function mpAudit($store_id) {
- $audit_status = Option::get('is_shenhe', $store_id, 'wechat', 0)['value'];
- $is_ali_shenhe = Option::get('is_ali_shenhe', $store_id, 'wechat', 0)['value'];
- $is_component_ali_shenhe = Option::get('is_component_ali_shenhe', $store_id, 'wechat', 0)['value'];
- if ((int)$is_ali_shenhe === 1 && is_alipay_platform()) {
- $audit_status = 1;
- }
- if ((int)$audit_status === 1 && (int)$is_ali_shenhe === 0 && is_alipay_platform()) {
- $audit_status = 0;
- }
- if ((int)$is_ali_shenhe === 1 && (int)$is_component_ali_shenhe === 0) {
- $is_ali_shenhe = 0;
- } elseif ((int)$is_ali_shenhe === 1 && (int)$is_component_ali_shenhe === 1 && is_alipay_platform()) {
- $is_ali_shenhe = 1;
- $audit_status = 1;
- }
- if (\Yii::$app->prod_is_dandianpu() && is_single_store()) {
- if (is_wechat_platform()) {
- $audit_status = Option::get('one_store_is_shenhe', 0, 'saas', 0)['value'];
- }
- if (is_alipay_platform()) {
- $one_store_alipay_config = json_decode(Option::get('one_store_alipay_config', 0, 'saas', '')['value'], true);
- $audit_status = !empty($one_store_alipay_config['one_store_is_shenhe']) ? $one_store_alipay_config['one_store_is_shenhe'] : 0;
- }
- }
- $wechat_login_toggle_article = Option::get('wechat_login_toggle_article', $store_id, 'wechat', 0)['value']; //审核页面文章
- $article = null;
- if ($wechat_login_toggle_article) {
- $aboutArticle = AboutArticle::findOne($wechat_login_toggle_article);
- $article = [
- 'name' => $aboutArticle->name ?? '',
- 'desc' => $aboutArticle->desc ?? ''
- ];
- }
- // 根据版本判断是否开启审核,低版本的不开启,最新版本开启
- if (\client_version() < \cyy_version()) {
- $audit_status = 0;
- $is_ali_shenhe = 0;
- }
- // 是否允许切换门店
- $mdSetting = MdSetting::findOne(['store_id' => $store_id]);
- $changeShop = $mdSetting->change_shop ?? 1;
- return [
- 'status' => $audit_status,
- 'is_ali_shenhe' => intval($is_ali_shenhe),
- 'article' => $article,
- 'change_shop' => $changeShop
- ];
- }
- // 店铺结算减少pv奖励金额
- public static function subStoreMoney($store, $price, $desc = '扣pv奖励金额', $order_id = 0, $user_id = 0 ,$huifuId = 0)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $before = $store->price;
- $store->price -= $price;
- if (!$store->save()) {
- throw new \Exception('店铺金额减少失败');
- }
- $log = new StoreAccountLog();
- $log->store_id = $store->id;
- $log->order_id = $order_id;
- $log->user_id = $user_id;
- $log->price = $price;
- $log->desc = $desc;
- $log->before = $before;
- $log->after = $store->price;
- $log->type = 2;
- $log->time = time();
- if (!$log->save()) {
- throw new \Exception('商城金额日志记录失败');
- }
- $t->commit();
- return $log->id;
- } catch (\Exception $e) {
- \Yii::error($e);
- $t->rollBack();
- return false;
- }
- }
- }
|