Mch.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace addons\Mch\common\models;
  3. use addons\Mch\common\enums\MchEnum;
  4. use common\enums\OrderStatusEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use common\models\goods\Goods;
  8. use Yii;
  9. use common\models\order\Order;
  10. /**
  11. * This is the model class for table "qimall_addons_mch".
  12. *
  13. * @property int $id
  14. * @property int $mall_id
  15. * @property int $user_id 绑定会员id
  16. * @property string $shop_owner 店长姓名
  17. * @property string $shop_mobile 店长手机号
  18. * @property string $store_name 门店名称
  19. * @property int $c_id 门店分类
  20. * @property string $store_desc 门店简介
  21. * @property string $logo_img 门店logo
  22. * @property string $license_img 营业执照
  23. * @property int|null $province_id 省
  24. * @property int|null $city_id 市
  25. * @property int|null $district_id 区
  26. * @property string $address 详细地址
  27. * @property int $expire_time 失效时间,0代表无限期
  28. * @property float $total_money 累计收入
  29. * @property float $balance 当前余额
  30. * @property float $total_expenses 累计支出
  31. * @property int|null $user_nums 会员总数
  32. * @property string $longitude 经度
  33. * @property string $latitude 维度
  34. * @property int $check_status 审核状态:0:待审核;1:已通过;2:未通过
  35. * @property string $check_remark 审核备注
  36. * @property int $store_style 门店样式 1:样式1;2:样式2
  37. * @property string $commission_authority 分佣/营销权限
  38. * @property string $share_title 分享标题
  39. * @property string $share_desc 分享描述
  40. * @property string $share_pic 分享图片
  41. * @property string $freight_type 配送设置
  42. * @property string $intra_city_distribution 同城配送
  43. * @property float $total_order_money 订单总金额
  44. * @property int $total_num 总数
  45. * @property float $total_settled_amount 已结算总金额
  46. * @property float $unsettled_amount 未结算金额
  47. * @property int $sales_volume 销量
  48. * @property float $praise 评分
  49. * @property int $status 状态[0禁用 1启用 -1删除]
  50. * @property int $shop_status 门店营业状态,1:正在营业;0:休息中
  51. * @property int $scan_code_bind 门店收款码-扫码绑定开关:0:否;1:是
  52. * @property int $created_at 创建时间
  53. * @property int $updated_at 修改时间
  54. * @property string $customer_service 客服设置
  55. */
  56. class Mch extends BaseActiveRecord
  57. {
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public static function tableName()
  62. {
  63. return '{{%addons_mch}}';
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function rules()
  69. {
  70. return [
  71. [['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'],
  72. [['total_money', 'balance', 'total_expenses', 'total_order_money', 'total_settled_amount', 'unsettled_amount', 'praise'], 'number'],
  73. [['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],
  74. [['shop_mobile'], 'string', 'max' => 11],
  75. [['intra_city_distribution','customer_service'], 'string', 'max' => 500],
  76. ];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function attributeLabels()
  82. {
  83. return [
  84. 'id' => 'ID',
  85. 'mall_id' => 'Mall ID',
  86. 'user_id' => '绑定会员id',
  87. 'shop_owner' => '店长姓名',
  88. 'shop_mobile' => '店长手机号',
  89. 'store_name' => '门店名称',
  90. 'c_id' => '门店分类',
  91. 'store_desc' => '门店简介',
  92. 'logo_img' => '门店logo',
  93. 'license_img' => '营业执照',
  94. 'province_id' => '省',
  95. 'city_id' => '市',
  96. 'district_id' => '区',
  97. 'address' => '详细地址',
  98. 'expire_time' => '失效时间,0代表无限期',
  99. 'total_money' => '累计收入',
  100. 'balance' => '当前余额',
  101. 'total_expenses' => '累计支出',
  102. 'user_nums' => '会员总数',
  103. 'longitude' => '经度',
  104. 'latitude' => '维度',
  105. 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过',
  106. 'check_remark' => '审核备注',
  107. 'store_style' => '门店样式 1:样式1;2:样式2',
  108. 'commission_authority' => '分佣/营销权限',
  109. 'share_title' => '分享标题',
  110. 'share_desc' => '分享描述',
  111. 'share_pic' => '分享图片',
  112. 'freight_type' => '配送设置',
  113. 'intra_city_distribution' => '同城配送',
  114. 'total_order_money' => '订单总金额',
  115. 'total_num' => '总数',
  116. 'total_settled_amount' => '已结算总金额',
  117. 'unsettled_amount' => '未结算金额',
  118. 'sales_volume' => '销量',
  119. 'praise' => '评分',
  120. 'status' => '状态[0禁用 1启用 -1删除]',
  121. 'shop_status' => '门店营业状态,1:正在营业;0:休息中',
  122. 'scan_code_bind' => '门店收款码-扫码绑定开关:0:否;1:是',
  123. 'created_at' => '创建时间',
  124. 'updated_at' => '修改时间',
  125. 'customer_service' => '客服设置',
  126. ];
  127. }
  128. public static function setData(array $params)
  129. {
  130. try {
  131. if (!empty($params['id'])) {
  132. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  133. if (empty($model)) throw new \Exception('门店不存在或已删除');
  134. } else {
  135. $model = new self();
  136. $model->loadDefaultValues();
  137. }
  138. if (!empty($params['freight_type'])) $params['freight_type'] = json_encode($params['freight_type']);
  139. if (!empty($params['intra_city_distribution'])) $params['intra_city_distribution'] = json_encode($params['intra_city_distribution']);
  140. if (!empty($params['customer_service'])) $params['customer_service'] = json_encode($params['customer_service']);
  141. if (!$model->load($params, '') || !$model->save()) {
  142. throw new \Exception($model->getErrorMessage());
  143. }
  144. return $model;
  145. } catch (\Exception $e) {
  146. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  147. return false;
  148. }
  149. }
  150. public static function addStore(array $params)
  151. {
  152. $transaction = \Yii::$app->db->beginTransaction();
  153. $res = self::setData($params);
  154. if (false === $res) {
  155. $transaction->rollBack();
  156. return false;
  157. }
  158. $pwd_str = substr($params['shop_mobile'], -6);
  159. $pwd_hash_str = \Yii::$app->security->generatePasswordHash($pwd_str);
  160. $admin_info = [
  161. 'username' => $params['shop_mobile'],
  162. 'account' => $params['shop_owner'],
  163. 'password' => $pwd_hash_str,
  164. 'mall_id' => $params['mall_id'],
  165. 'admin_type' => 2,
  166. 'status' => StatusEnum::ENABLED,
  167. 'mch_id' => $res->id,
  168. ];
  169. $admin_model = MchAdmin::find()
  170. ->where(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'username' => $params['shop_mobile']])
  171. ->one();
  172. if (!empty($admin_model)) {
  173. $transaction->rollBack();
  174. self::$static_error = '多商户管理员账号已经存在';
  175. return false;
  176. }
  177. $admin_model = new MchAdmin();
  178. $admin_model->loadDefaultValues();
  179. $admin_model->attributes = $admin_info;
  180. $admin_model->updated_at = time();
  181. $admin_model->created_at = time();
  182. $res_ad = $admin_model->save();
  183. if (false === $res_ad) {
  184. $transaction->rollBack();
  185. self::$static_error = $admin_model->getErrorMessage();
  186. return false;
  187. }
  188. $transaction->commit();
  189. return true;
  190. }
  191. /**
  192. * @name:
  193. * @msg: 装修页面获取门店列表
  194. * @param {int} $mall_id
  195. * @param {array} $params
  196. * @return {*}
  197. */
  198. public static function getMchListInDiy(int $mall_id, array $params = [])
  199. {
  200. $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => MchEnum::CHECK_ADOPT,'shop_status' => 1];
  201. if (!empty($params['store_id'])) {
  202. $wheres[] = ['id' => $params['store_id']];
  203. }
  204. if (!empty($params['store_name'])) {
  205. $wheres[] = ['like', 'store_name', $params['store_name']];
  206. }
  207. if (!empty($params['storeowner'])) {
  208. $wheres[] = ['like', 'shop_owner', $params['storeowner']];
  209. }
  210. $params = [
  211. 'select' => '*,store_name as title,logo_img as img',
  212. 'where' => $wheres,
  213. 'order' => 'created_at desc',
  214. ];
  215. $store_list = self::listPage($params);
  216. if (false === $store_list) {
  217. return false;
  218. }
  219. return $store_list;
  220. }
  221. public static function getGoodListInDiy(int $mall_id, array $params)
  222. {
  223. $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'check_status' => MchEnum::CHECK_ADOPT, 'is_on_sale' => 1];
  224. if (!empty($params['keyword'])) {
  225. $wheres[] = ['like', 'goods_name', $params['keyword']];
  226. }
  227. $params = [
  228. 'where' => $wheres,
  229. 'order' => 'sort asc,created_at desc',
  230. 'page' => $params['page'] ?? 1,
  231. 'limit' => $params['limit'] ?? 15,
  232. ];
  233. $list = Goods::listPage($params);
  234. if (false === $list) {
  235. return Goods::$static_error;
  236. }
  237. foreach ($list['list'] as &$lv) {
  238. $lv['title'] = $lv['goods_name'];
  239. }
  240. return $list;
  241. }
  242. //结算货款
  243. public static function settlementStoreCommission($order_id, $mch_id, $order_no)
  244. {
  245. $mch = Mch::findOne(['id' => $mch_id]);
  246. $store_commission_model = MchCommission::findOne(['order_no' => $order_no, 'is_settlement' => 0,'status'=>StatusEnum::ENABLED]);
  247. if ($store_commission_model) {
  248. $mch->balance += $store_commission_model->amount_received;
  249. $mch->total_settled_amount += $store_commission_model->amount_received;
  250. $mch->unsettled_amount -= $store_commission_model->amount_received;
  251. if ($mch->unsettled_amount < 0) $mch->unsettled_amount = 0;
  252. $res = $mch->save();
  253. if ($res == false) throw new \Exception('多商户订单完成结算货款,更新店铺结算金额失败,order_id:' . $order_id);
  254. $store_commission_model->is_settlement = 1;
  255. $res = $store_commission_model->save();
  256. if ($res == false) throw new \Exception('多商户订单完成结算货款,更新提成记录失败,order_id:' . $order_id);
  257. }
  258. }
  259. public static function delStore($mall_id,$mch_id){
  260. $mch = Mch::findOne(['id'=>$mch_id,'mall_id'=>$mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  261. if(empty($mch)) throw new \Exception('商户不存在');
  262. if($mch['balance']>0) throw new \Exception('商户余额不为0,请先提现后再删除');
  263. $order = Order::getOne([
  264. ['mch_id'=>$mch_id],
  265. ['order_status'=>[OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING]],
  266. ['is_feedback'=>[0,2]]
  267. ]);
  268. if(!empty($order)) throw new \Exception('请等订单完成后再删除');
  269. $mch->status = StatusEnum::DELETE;
  270. $res = $mch->save();
  271. if(empty($res)) throw new \Exception('删除失败');
  272. MchAdmin::updateAll(['status'=>StatusEnum::DELETE],['mch_id'=>$mch_id,'mall_id'=>$mall_id]);
  273. return true;
  274. }
  275. }