| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace addons\Mch\common\models;
- use addons\Mch\common\enums\MchEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\Goods;
- use Yii;
- use common\models\order\Order;
- /**
- * This is the model class for table "qimall_addons_mch".
- *
- * @property int $id
- * @property int $mall_id
- * @property int $user_id 绑定会员id
- * @property string $shop_owner 店长姓名
- * @property string $shop_mobile 店长手机号
- * @property string $store_name 门店名称
- * @property int $c_id 门店分类
- * @property string $store_desc 门店简介
- * @property string $logo_img 门店logo
- * @property string $license_img 营业执照
- * @property int|null $province_id 省
- * @property int|null $city_id 市
- * @property int|null $district_id 区
- * @property string $address 详细地址
- * @property int $expire_time 失效时间,0代表无限期
- * @property float $total_money 累计收入
- * @property float $balance 当前余额
- * @property float $total_expenses 累计支出
- * @property int|null $user_nums 会员总数
- * @property string $longitude 经度
- * @property string $latitude 维度
- * @property int $check_status 审核状态:0:待审核;1:已通过;2:未通过
- * @property string $check_remark 审核备注
- * @property int $store_style 门店样式 1:样式1;2:样式2
- * @property string $commission_authority 分佣/营销权限
- * @property string $share_title 分享标题
- * @property string $share_desc 分享描述
- * @property string $share_pic 分享图片
- * @property string $freight_type 配送设置
- * @property string $intra_city_distribution 同城配送
- * @property float $total_order_money 订单总金额
- * @property int $total_num 总数
- * @property float $total_settled_amount 已结算总金额
- * @property float $unsettled_amount 未结算金额
- * @property int $sales_volume 销量
- * @property float $praise 评分
- * @property int $status 状态[0禁用 1启用 -1删除]
- * @property int $shop_status 门店营业状态,1:正在营业;0:休息中
- * @property int $scan_code_bind 门店收款码-扫码绑定开关:0:否;1:是
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property string $customer_service 客服设置
- */
- class Mch extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_mch}}';
- }
- /**
- * {@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'], 'integer'],
- [['total_money', 'balance', 'total_expenses', 'total_order_money', 'total_settled_amount', 'unsettled_amount', 'praise'], 'number'],
- [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'address', 'longitude', 'latitude', 'check_remark','commission_authority', 'share_title', 'share_desc', 'share_pic', 'freight_type','store_background'], 'string', 'max' => 255],
- [['shop_mobile'], 'string', 'max' => 11],
- [['intra_city_distribution','customer_service'], 'string', 'max' => 500],
- ];
- }
- /**
- * {@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' => '区',
- 'address' => '详细地址',
- 'expire_time' => '失效时间,0代表无限期',
- 'total_money' => '累计收入',
- 'balance' => '当前余额',
- 'total_expenses' => '累计支出',
- 'user_nums' => '会员总数',
- 'longitude' => '经度',
- 'latitude' => '维度',
- 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过',
- 'check_remark' => '审核备注',
- 'store_style' => '门店样式 1:样式1;2:样式2',
- 'commission_authority' => '分佣/营销权限',
- 'share_title' => '分享标题',
- 'share_desc' => '分享描述',
- 'share_pic' => '分享图片',
- 'freight_type' => '配送设置',
- 'intra_city_distribution' => '同城配送',
- 'total_order_money' => '订单总金额',
- 'total_num' => '总数',
- 'total_settled_amount' => '已结算总金额',
- 'unsettled_amount' => '未结算金额',
- 'sales_volume' => '销量',
- 'praise' => '评分',
- 'status' => '状态[0禁用 1启用 -1删除]',
- 'shop_status' => '门店营业状态,1:正在营业;0:休息中',
- 'scan_code_bind' => '门店收款码-扫码绑定开关:0:否;1:是',
- 'created_at' => '创建时间',
- 'updated_at' => '修改时间',
- 'customer_service' => '客服设置',
- ];
- }
- public static function setData(array $params)
- {
- 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();
- }
- 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 (!empty($params['customer_service'])) $params['customer_service'] = json_encode($params['customer_service']);
- if (!$model->load($params, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
- return false;
- }
- }
- public static function addStore(array $params)
- {
- $transaction = \Yii::$app->db->beginTransaction();
- $res = self::setData($params);
- if (false === $res) {
- $transaction->rollBack();
- return false;
- }
- $pwd_str = substr($params['shop_mobile'], -6);
- $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,
- 'mch_id' => $res->id,
- ];
- $admin_model = MchAdmin::find()
- ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'username' => $params['shop_mobile']])
- ->one();
- if (!empty($admin_model)) {
- $transaction->rollBack();
- self::$static_error = '多商户管理员账号已经存在';
- return false;
- }
- $admin_model = new MchAdmin();
- $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;
- }
- $transaction->commit();
- return true;
- }
- /**
- * @name:
- * @msg: 装修页面获取门店列表
- * @param {int} $mall_id
- * @param {array} $params
- * @return {*}
- */
- public static function getMchListInDiy(int $mall_id, array $params = [])
- {
- $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => MchEnum::CHECK_ADOPT,'shop_status' => 1];
- 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 getGoodListInDiy(int $mall_id, array $params)
- {
- $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => MchEnum::CHECK_ADOPT, 'is_on_sale' => 1];
- if (!empty($params['keyword'])) {
- $wheres[] = ['like', 'goods_name', $params['keyword']];
- }
- $params = [
- 'where' => $wheres,
- 'order' => 'sort asc,created_at desc',
- 'page' => $params['page'] ?? 1,
- 'limit' => $params['limit'] ?? 15,
- ];
- $list = Goods::listPage($params);
- if (false === $list) {
- return Goods::$static_error;
- }
- foreach ($list['list'] as &$lv) {
- $lv['title'] = $lv['goods_name'];
- }
- return $list;
- }
- //结算货款
- public static function settlementStoreCommission($order_id, $mch_id, $order_no)
- {
- $mch = Mch::findOne(['id' => $mch_id]);
- $store_commission_model = MchCommission::findOne(['order_no' => $order_no, 'is_settlement' => 0,'status'=>StatusEnum::ENABLED]);
- if ($store_commission_model) {
- $mch->balance += $store_commission_model->amount_received;
- $mch->total_settled_amount += $store_commission_model->amount_received;
- $mch->unsettled_amount -= $store_commission_model->amount_received;
- if ($mch->unsettled_amount < 0) $mch->unsettled_amount = 0;
- $res = $mch->save();
- if ($res == false) throw new \Exception('多商户订单完成结算货款,更新店铺结算金额失败,order_id:' . $order_id);
- $store_commission_model->is_settlement = 1;
- $res = $store_commission_model->save();
- if ($res == false) throw new \Exception('多商户订单完成结算货款,更新提成记录失败,order_id:' . $order_id);
- }
- }
- public static function delStore($mall_id,$mch_id){
- $mch = Mch::findOne(['id'=>$mch_id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if(empty($mch)) throw new \Exception('商户不存在');
- if($mch['balance']>0) throw new \Exception('商户余额不为0,请先提现后再删除');
- $order = Order::getOne([
- ['mch_id'=>$mch_id],
- ['order_status'=>[OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING]],
- ['is_feedback'=>[0,2]]
- ]);
- if(!empty($order)) throw new \Exception('请等订单完成后再删除');
- $mch->status = StatusEnum::DELETE;
- $res = $mch->save();
- if(empty($res)) throw new \Exception('删除失败');
- MchAdmin::updateAll(['status'=>StatusEnum::DELETE],['mch_id'=>$mch_id,'mall_id'=>$mall_id]);
- return true;
- }
- }
|