ShareController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\models\BusinessMember;
  9. use app\models\BusinessMemberOrder;
  10. use app\models\Cash;
  11. use app\models\Option;
  12. use app\models\Order;
  13. use app\models\OrderTransit;
  14. use app\models\PurchaseOrder;
  15. use app\models\PurchaseOrderDetail;
  16. use app\models\SaasSupplierReferral;
  17. use app\models\ScanCodePayOrder;
  18. use app\models\OrderDetail;
  19. use app\models\OrderRefund;
  20. use app\models\SaasShareMoney;
  21. use app\models\SaasUser;
  22. use app\models\Share;
  23. use app\models\ShareDetail;
  24. use app\models\StoreShareMoney;
  25. use app\models\Supplier;
  26. use app\models\User;
  27. use app\models\UserShareLog;
  28. use app\models\UserShareMoney;
  29. use app\models\SaasStoreReferral;
  30. use app\models\Store;
  31. use app\models\SharingReceiver;
  32. use app\modules\alliance\controllers\BaseController;
  33. use app\modules\alliance\models\ShareForm;
  34. use app\modules\alliance\models\CashForm;
  35. use app\modules\alliance\models\CashListForm;
  36. use app\modules\alliance\models\TeamForm;
  37. use app\models\SaasUserPriceLog;
  38. use app\modules\alliance\models\ShareQrcodeForm;
  39. use app\utils\Notice\NoticeAction;
  40. use GuzzleHttp\Psr7\Query;
  41. use yii\base\BaseObject;
  42. use yii\data\Pagination;
  43. use yii\helpers\Json;
  44. class ShareController extends BaseController
  45. {
  46. private $pageLimit = 10;
  47. public function behaviors()
  48. {
  49. return parent::behaviors();
  50. }
  51. /**
  52. * @return mixed|string
  53. * 绑定上下级关系
  54. */
  55. public function actionBindParent()
  56. {
  57. $parent_id = get_params('parent_id');
  58. if($parent_id > 0){
  59. $saas_user = get_saas_user();
  60. if($saas_user->parent_id > 0) {
  61. $data = [
  62. 'code' => 0,
  63. 'msg' => '用户已经有上级',
  64. ];
  65. } else {
  66. if ($parent_id == $saas_user->id) {
  67. $data = [
  68. 'code' => 0,
  69. 'msg' => '上级不能是自己',
  70. ];
  71. } else {
  72. $saas = SaasUser::findOne(['id' => $saas_user->id, 'is_delete' => 0]);
  73. $saas->parent_id = $parent_id;
  74. if ($saas->save()) {
  75. $data = [
  76. 'code' => 0,
  77. 'msg' => '绑定成功',
  78. ];
  79. } else {
  80. $data = [
  81. 'code' => 0,
  82. 'msg' => '绑定失败请稍后再试',
  83. ];
  84. }
  85. }
  86. }
  87. } else {
  88. $data = [
  89. 'code' => 0,
  90. 'msg' => '用户未找到',
  91. ];
  92. }
  93. return $this->asJson($data);
  94. }
  95. /**
  96. * 获取提现方式
  97. */
  98. public function actionGetCashMethod()
  99. {
  100. $saas_user = get_saas_user();
  101. $cash_method = [];
  102. if (!empty($saas_user->withdraw_method)) {
  103. $decode = json_decode($saas_user->withdraw_method, true);
  104. if (is_array($decode)) {
  105. $cash_method = $decode;
  106. }
  107. }
  108. return $this->asJson([
  109. 'code' => 0,
  110. 'msg' => 'success',
  111. 'data' => [
  112. 'list' => $cash_method,
  113. ],
  114. ]);
  115. }
  116. /**
  117. * 删除提现方式
  118. */
  119. public function actionDelCashMethod()
  120. {
  121. $saas_user = get_saas_user();
  122. $type = post_params('type');
  123. if (empty($type)) {
  124. return $this->asJson([
  125. 'code' => 1,
  126. 'msg' => '参数错误',
  127. ]);
  128. }
  129. if (empty($saas_user->withdraw_method)) {
  130. return $this->asJson([
  131. 'code' => 0,
  132. 'msg' => '操作成功',
  133. ]);
  134. }
  135. $cash_method = json_decode($saas_user->withdraw_method, true);
  136. if (is_array($cash_method)) {
  137. foreach ($cash_method as $key => $value) {
  138. if ($value['type'] == $type) {
  139. unset($cash_method[$key]);
  140. }
  141. }
  142. $saas_user->withdraw_method = json_encode(array_values($cash_method));
  143. } else {
  144. $saas_user->withdraw_method = '';
  145. }
  146. if ($saas_user->save()) {
  147. return $this->asJson([
  148. 'code' => 0,
  149. 'msg' => '操作成功',
  150. ]);
  151. }
  152. return $this->asJson([
  153. 'code' => 0,
  154. 'msg' => '操作失败',
  155. ]);
  156. }
  157. /**
  158. * 添加提现方式
  159. */
  160. public function actionAddCashMethod()
  161. {
  162. $type = post_params('type');
  163. if (empty($type)) {
  164. return $this->asJson([
  165. 'code' => 1,
  166. 'msg' => '参数错误',
  167. ]);
  168. }
  169. $name = post_params('name');
  170. $account = post_params('account');
  171. $bank = post_params('bank');
  172. $branch = post_params('branch');
  173. if (empty($name) || empty($account)) {
  174. return $this->asJson([
  175. 'code' => 1,
  176. 'msg' => '账号和姓名不能为空',
  177. ]);
  178. }
  179. if ($type == 'bank_card' && (empty($bank) || empty($branch))) {
  180. return $this->asJson([
  181. 'code' => 1,
  182. 'msg' => '银行或支行不能为空',
  183. ]);
  184. }
  185. $data = [
  186. 'type' => $type,
  187. 'name' => $name,
  188. 'account' => $account,
  189. ];
  190. if ($type == 'bank_card') {
  191. $data['bank'] = $bank;
  192. $data['branch'] = $branch;
  193. }
  194. $saas_user = get_saas_user();
  195. if (empty($saas_user->withdraw_method)) {
  196. $saas_user->withdraw_method = json_encode(array_values([$data]));
  197. } else {
  198. $decode = json_decode($saas_user->withdraw_method, true);
  199. if (!$decode) {
  200. $saas_user->withdraw_method = json_encode(array_values([$data]));
  201. } else {
  202. foreach ($decode as $key => $value) {
  203. if ($value['type'] == $type) {
  204. unset($decode[$key]);
  205. break;
  206. }
  207. }
  208. $decode[] = $data;
  209. $saas_user->withdraw_method = json_encode(array_values($decode));
  210. }
  211. }
  212. if ($saas_user->save()) {
  213. return $this->asJson([
  214. 'code' => 0,
  215. 'msg' => '操作成功',
  216. ]);
  217. }
  218. return $this->asJson([
  219. 'code' => 1,
  220. 'msg' => '操作失败',
  221. ]);
  222. }
  223. /**
  224. * @return mixed|string
  225. * 获取佣金相关
  226. */
  227. public function actionGetPrice()
  228. {
  229. $saas_id = get_saas_user_id();
  230. $form = new ShareForm();
  231. $form->store_id = get_store_id();
  232. //$form->user_id = get_user_id();
  233. $form->saas_id = $saas_id;
  234. $res['data']['price'] = $form->getPrice();
  235. $setting = Option::get('share_basic_setting', get_store_id());
  236. $setting = $setting ? Json::decode($setting['value']) : [];
  237. $res['data']['pay_type'] = $setting['pay_type']['value'];
  238. //暂时去掉
  239. //$res['data']['bank'] = $setting['bank']['value'];
  240. //余额支付 商盟没有
  241. //$res['data']['remaining_sum'] = $setting['remaining_sum']['value'];
  242. $cash_last = Cash::find()->where(['saas_id' => $saas_id, 'is_delete' => 0])
  243. ->orderBy(['id' => SORT_DESC])->select(['name', 'mobile', 'type', 'bank_name'])->asArray()->one();
  244. $res['data']['cash_last'] = $cash_last;
  245. $cash_max_day = floatval($setting['cash_max_day']['value']);
  246. if ($cash_max_day) {
  247. $cash_sum = Cash::find()->where([
  248. 'store_id' => get_store_id(),
  249. 'is_delete' => 0,
  250. 'status' => [0, 1, 2, 5],
  251. ])->andWhere([
  252. 'AND',
  253. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  254. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  255. ])->sum('price');
  256. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  257. $res['data']['cash_max_day'] = max(0, floatval(sprintf('%.2f', $cash_max_day)));
  258. } else {
  259. $res['data']['cash_max_day'] = -1;
  260. }
  261. $cashServiceCharge = floatval($setting['cash_service_charge']['value']);
  262. $res['data']['cash_service_charge'] = $cashServiceCharge;
  263. if($cashServiceCharge == 0){
  264. $res['data']['service_content'] = "";
  265. }else{
  266. $res['data']['service_content'] = "提现需要加收{$cashServiceCharge}%手续费";
  267. }
  268. $wxappUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/wxapp/images';
  269. $payTypeList = [
  270. [
  271. 'icon' => $wxappUrl . '/icon-share-wechat.png',
  272. 'name' => '微信',
  273. 'is_show' => false
  274. ],
  275. [
  276. 'icon' => $wxappUrl . '/icon-share-ant.png',
  277. 'name' => '支付宝',
  278. 'is_show' => false
  279. ],
  280. [
  281. 'icon' => $wxappUrl . '/icon-share-bank.png',
  282. 'name' => '银行卡',
  283. 'is_show' => false
  284. ],
  285. // [
  286. // 'icon' => $wxappUrl . '/gold.png',
  287. // 'name' => '余额',
  288. // 'is_show' => false
  289. // ],
  290. ];
  291. if(isset($res['data']['pay_type'])){
  292. foreach($res['data']['pay_type'] as $val){
  293. if($val == 'alipay'){
  294. $payTypeList[1]['is_show'] = true;
  295. }
  296. if($val == 'wechat'){
  297. $payTypeList[0]['is_show'] = true;
  298. }
  299. if($val == 'bank'){
  300. $payTypeList[2]['is_show'] = true;
  301. }
  302. }
  303. }
  304. // if($res['data']['remaining_sum'] && $res['data']['remaining_sum'] == 1){
  305. // $payTypeList[3]['is_show'] = true;
  306. // }
  307. $res['data']['pay_type_list'] = $payTypeList;
  308. $res['data']["cash_success_tpl"] = '';
  309. $res['data']["cash_fail_tpl"]= '';
  310. //获取分销订单
  311. //$res['data']['fx_list'] = Cash::find()->Where(['saas_id' => $saas_id, 'is_delete' => 0])
  312. // ->orderBy('id desc')->asArray()->one();
  313. //新提现方式
  314. $res['data']['cash_method_list'] = [];
  315. $saas_user = get_saas_user();
  316. if (!empty($saas_user->withdraw_method)) {
  317. $cash_method = json_decode($saas_user->withdraw_method, true);
  318. if (is_array($cash_method)) {
  319. $res['data']['cash_method_list'] = $cash_method;
  320. }
  321. }
  322. return $this->asJson(['code' => 0, 'msg' => 'success', 'data' => $res['data']]);
  323. }
  324. /**
  325. * @return mixed|string
  326. * 申请提现
  327. */
  328. public function actionApply()
  329. {
  330. $form = new CashForm();
  331. $form->saas_id = get_saas_user_id();
  332. $form->store_id = get_store_id();
  333. $form->attributes = post_params();
  334. return $this->asJson($form->save());
  335. }
  336. /**
  337. * @return mixed|string
  338. * 申请提现
  339. */
  340. public function actionIndex()
  341. {
  342. $form = new ShareForm();
  343. return $this->asJson($form->getShareSetting());
  344. }
  345. /**
  346. * 提现明细列表
  347. */
  348. public function actionCashDetail()
  349. {
  350. $form = new CashListForm();
  351. $form->attributes = get_params();
  352. $form->store_id = get_store_id();
  353. $form->user_id = get_user_id();
  354. $form->saas_id = get_saas_user_id();
  355. return $this->asJson($form->getList());
  356. }
  357. /**
  358. * @return mixed|string
  359. * 获取团队详情
  360. */
  361. public function actionGetTeam()
  362. {
  363. $form = new TeamForm();
  364. $form->attributes = get_params();
  365. $form->user_id = get_user_id();
  366. $form->store_id = get_store_id();
  367. $form->saas_id = get_saas_user_id();
  368. $form->scenario = "TEAM";
  369. $form->level = get_params('level',1);
  370. return $this->asJson($form->getTeam());
  371. }
  372. /**
  373. * @return mixed|string
  374. * 获取团队详情
  375. */
  376. public function actionGetTeamCount()
  377. {
  378. $form = new TeamForm();
  379. $form->attributes = get_params();
  380. $form->saas_id = get_saas_user_id();
  381. $form->scenario = "TEAM";
  382. $info = $form->getTeamCount();
  383. $data = [
  384. 'code' => 0,
  385. 'msg' => '',
  386. 'data' => $info
  387. ];
  388. return $this->asJson($data);
  389. }
  390. /**
  391. * 推荐人列表
  392. * @return Response
  393. * @throws InvalidConfigException
  394. * @throws Throwable
  395. * @throws Exception
  396. */
  397. public function actionReferralList()
  398. {
  399. $status = get_params('status', 1);
  400. $saas_id = get_saas_user_id();
  401. $query = SaasStoreReferral::find()->alias('ssr')->leftJoin(['su' => SaasUser::tableName()], 'ssr.referral_id = su.id');
  402. $query->leftJoin(['s' => Store::tableName()], 'ssr.store_id = s.id');
  403. $query->where(['ssr.is_enable' => $status,'su.id'=>$saas_id]);
  404. $query->select('ssr.id, ssr.store_id, ssr.referral_id, ssr.is_enable, s.name as store_name,s.created_at,s.user_name,s.contact_tel ,s.logo, ssr.referral_rebate, su.avatar, su.name as nickname, su.mobile, su.id as saas_id');
  405. $list = pagination_make($query);
  406. return $this->asJson([
  407. 'code' => 0,
  408. 'msg' => 'success',
  409. 'data' => [
  410. 'data' => $list['list'],
  411. 'pageNo' => $list['pageNo'],
  412. 'totalCount' => $list['totalCount'],
  413. ]
  414. ]);
  415. }
  416. /**
  417. * 店铺分销订单
  418. */
  419. public function actionDistributionOrder(){
  420. $this->getOrderInfo(0);
  421. }
  422. /**
  423. * 推荐店铺订单
  424. */
  425. public function actionRecommendOrder(){
  426. $this->getOrderInfo(2);
  427. }
  428. /**
  429. * 会员分销订单
  430. */
  431. public function actionMemberOrder(){
  432. $this->getOrderInfo(1);
  433. }
  434. /**
  435. * 会员分销订单
  436. */
  437. public function actionSelfOrder(){
  438. $this->getOrderInfo(5);
  439. }
  440. /**
  441. * 灵活分润
  442. */
  443. public function actionCustomSharingOrder(){
  444. $this->getOrderInfo(6);
  445. }
  446. /**
  447. * 供货商分润订单(非分账)
  448. */
  449. public function actionSupplierOrder() {
  450. $saas_id = get_saas_user_id();
  451. $status = intval(get_params('status'));
  452. $query = SaasUserPriceLog::find()->alias('pl')
  453. ->leftJoin(['ot' => OrderTransit::tableName()],
  454. "pl.order_no LIKE 'PO%' AND pl.order_no = ot.cloud_order_no AND ot.is_delete = 0")
  455. ->leftJoin(['po' => PurchaseOrder::tableName()],
  456. "pl.order_no LIKE 'AP%' AND pl.order_no = po.order_no AND po.is_delete = 0")
  457. ->where([
  458. 'pl.saas_id' => $saas_id,
  459. 'pl.log_type' => SaasUserPriceLog::LOG_TYPE_INCOME,
  460. 'pl.amount_type' => SaasUserPriceLog::AMOUNT_TYPE_SUPPLIER_PARENT
  461. ]);
  462. if ($status === 1) {
  463. $query->andWhere(['OR', [//查询未完成的订单
  464. 'ot.status' => [-1, 0, 1]//取消 / 转单创建成功 / 已发货
  465. ], [
  466. 'po.trade_status' => [-1, 0, 1, 2]//默认 / 待发货 / 已取消 / 已发货
  467. ]]);
  468. }
  469. if ($status === 2) {
  470. $query->andWhere(['OR', [//查询未完成的订单
  471. 'ot.status' => 2//已确认收货
  472. ], [
  473. 'po.trade_status' => 3//已确认
  474. ]]);
  475. }
  476. $query->orderBy('pl.created_at DESC')->select([
  477. 'pl.id',
  478. 'pl.saas_id',
  479. 'pl.amount',
  480. 'pl.created_at',
  481. 'COALESCE(ot.cloud_order_no, po.order_no) AS order_no',
  482. 'COALESCE(ot.status, po.trade_status) AS trade_status',
  483. 'ot.goods_info'
  484. ]);
  485. $list = pagination_make($query);
  486. foreach ($list['list'] as &$item) {
  487. $orderNoHead = substr($item['order_no'], 0, 2);
  488. $item['saasInfo'] = [
  489. 'name' => '',
  490. 'avatar' => ''
  491. ];
  492. if ($orderNoHead === 'PO') {
  493. $order_detail = [];
  494. $goods_info = json_decode($item['goods_info'], true);
  495. foreach ($goods_info as $goods_index => $goods_item) {
  496. $order_detail[$goods_index]['num'] = $goods_item['num'];
  497. $order_detail[$goods_index]['attr'] = $goods_item['attr'];
  498. $order_detail[$goods_index]['total_price'] = $goods_item['price'];
  499. $order_detail[$goods_index]['pic'] = $goods_item['pic_url'];
  500. $order_detail[$goods_index]['goods_name'] = $goods_item['goods_name'] ?: '';
  501. }
  502. if (intval($item['trade_status']) === -1) {
  503. $item['trade_status'] = 1;
  504. }
  505. if (intval($item['trade_status']) === 1) {
  506. $item['trade_status'] = 2;
  507. }
  508. if (intval($item['trade_status']) === 2) {
  509. $item['trade_status'] = 3;
  510. }
  511. $orderTransit = OrderTransit::findOne(['cloud_order_no' => $item['order_no'], 'is_delete' => 0]);
  512. $order = Order::findOne($orderTransit->order_id);
  513. if ($order->saas_id) {
  514. $saasInfo = SaasUser::findOne($order->saas_id);
  515. $item['saasInfo'] = [
  516. 'name' => $saasInfo->name,
  517. 'avatar' => $saasInfo->avatar
  518. ];
  519. } else {
  520. $userInfo = User::findOne($order->user_id);
  521. if ($userInfo->binding) {
  522. $saasInfo = SaasUser::findOne(['mobile' => $userInfo->binding, 'is_delete' => 0]);
  523. $item['saasInfo'] = [
  524. 'name' => $saasInfo->name,
  525. 'avatar' => $saasInfo->avatar
  526. ];
  527. }
  528. }
  529. } else {
  530. $purchaseOrder = PurchaseOrder::findOne(['order_no' => $item['order_no'], 'is_delete' => 0]);
  531. $order_detail = PurchaseOrderDetail::find()->where(['order_id' => $purchaseOrder->id, 'is_delete' => 0])
  532. ->select('goods_name, num, pic, total_price, attr')->asArray()->all();
  533. if(!empty($purchaseOrder->saas_id)){
  534. $saasInfo = SaasUser::find()->where(['id' => $purchaseOrder['saas_id'], 'is_delete'=>0])
  535. ->select('name, avatar')->asArray()->one();
  536. $item['saasInfo'] = $saasInfo;
  537. }
  538. }
  539. foreach ($order_detail as &$order_detail_item) {
  540. $order_detail_item['attr'] = json_decode($order_detail_item['attr'], true);
  541. }
  542. $item['goods'] = $order_detail;
  543. $item['trade_status'] = intval($item['trade_status']);
  544. $item['status'] = 1;
  545. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  546. }
  547. return $this->asJson([
  548. 'code' => 0,
  549. 'msg' => 'success',
  550. 'data' => [
  551. 'data' => $list['list'],
  552. 'pageNo' => $list['pageNo'],
  553. 'totalCount' => $list['totalCount'],
  554. ]
  555. ]);
  556. }
  557. //
  558. private function getOrderInfo($from = 0){
  559. $status = get_params('status',0);
  560. $page = get_params('page',1);
  561. //会员分销订单
  562. $saas_id = get_saas_user_id();
  563. $saasIds = SaasUser::find()->where(['parent_id'=>$saas_id])->asArray()->all();
  564. $levelSaasIds = '';
  565. if(!empty($saasIds) && count($saasIds) > 0) {
  566. foreach($saasIds as $val){
  567. $levelSaasIds .= $val['id'].',';
  568. }
  569. }
  570. $levelSaasIds .= $saas_id;
  571. $sql_count = "select count(o.order_no) num from `cyy_sharing_receiver` as s inner join ( ( select id order_id,order_no,trade_status,is_pay,pay_type from cyy_order ) union (select id order_id,order_no,trade_status,is_pay,pay_type from cyy_scan_code_pay_order ) ) as o on s.`order_no`= o.`order_no` where s.saas_id={$saas_id} ";
  572. $sql = "select o.*,s.amount, s.is_pay as status from `cyy_sharing_receiver` as s inner join ( ( select id order_id,order_no,trade_status,is_pay,pay_type,saas_id,0 as order_type, created_at from cyy_order ) union (select id order_id,order_no,trade_status,is_pay,pay_type,saas_id,1 as order_type, created_at from cyy_scan_code_pay_order ) ) as o on s.`order_no`= o.`order_no` where s.saas_id={$saas_id} ";
  573. if(in_array($from, [
  574. SharingReceiver::FROM_STORE,
  575. SharingReceiver::FROM_PLATFORM,
  576. SharingReceiver::FROM_RECOMMEND,
  577. SharingReceiver::FROM_SELF,
  578. SharingReceiver::FROM_CUSTOM
  579. ])){
  580. $sql_count .= " and s.from = {$from}";
  581. $sql .= " and s.from = {$from}";
  582. }
  583. // if ($status == 1) {//待付款
  584. // $is_pay = Order::IS_PAY_FALSE;
  585. // $sql_count .= " and o.is_pay = {$is_pay} and o.pay_type !=2 and o.trade_status !=1";
  586. // $sql .= " and o.is_pay = {$is_pay} and o.pay_type !=2 and o.trade_status !=1";
  587. // }
  588. if ($status == 1) {//已付款
  589. $trade_status = Order::ORDER_FLOW_NO_SEND;
  590. $sql_count .= " and o.trade_status = {$trade_status} and o.is_pay = 1";
  591. $sql .= " and o.trade_status = {$trade_status} and o.is_pay = 1";
  592. }
  593. if ($status == 2) {//已完成
  594. $trade_status = Order::ORDER_FLOW_CONFIRM;
  595. $sql_count .= " and o.trade_status = {$trade_status} and o.is_pay = 1";
  596. $sql .= " and o.trade_status = {$trade_status} and o.is_pay = 1";
  597. }
  598. $connection_count = \Yii::$app->db;
  599. $command_count = $connection_count->createCommand($sql_count);
  600. $info = $command_count->queryOne();
  601. $start = ($page - 1) * $this->pageLimit;
  602. $sql .= " limit {$start} , {$this->pageLimit} ";
  603. $command = $connection_count->createCommand($sql);
  604. $list['list'] = $command->queryAll();
  605. $list['pageNo'] = $page;
  606. $list['totalCount'] = $info['num'];
  607. if(count($list['list']) > 0 ){
  608. foreach($list['list'] as $key=>$val){
  609. if(isset($val['order_id']) && !empty($val['order_id']) ){
  610. $orderNoHead = substr($val['order_no'], 0, 2);
  611. if($orderNoHead == 'SC'){
  612. $list['list'][$key]['goods'] = [
  613. [
  614. "goods_id" => '10',
  615. "goods_name" => '扫码支付',
  616. "num" => '1',
  617. "pic" => '',
  618. "goods_info" => null,
  619. "total_price" => '',
  620. ]
  621. ];
  622. }else{
  623. $goodsInfo = OrderDetail::find()->where(['order_id' => $val['order_id'], 'is_delete' => 0])->select('goods_id, goods_name, num, pic, attr, total_price')->asArray()->all();
  624. foreach ($goodsInfo as &$goods_item) {
  625. $goods_item['attr'] = json_decode($goods_item['attr'], true);
  626. }
  627. $list['list'][$key]['goods'] = $goodsInfo;
  628. }
  629. }
  630. if(isset($val['saas_id']) && $val['saas_id'] > 0){
  631. $saasInfo = SaasUser::find()->where(['id'=>$val['saas_id'],'is_delete'=>0])->select('name,avatar')->asArray()->one();
  632. $list['list'][$key]['saasInfo'] = $saasInfo;
  633. }
  634. $list['list'][$key]['status'] = intval($val['status']) ? 1 : 0;
  635. $list['list'][$key]['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
  636. }
  637. }
  638. return $this->asJson([
  639. 'code' => 0,
  640. 'msg' => 'success',
  641. 'data' => [
  642. 'data' => $list['list'],
  643. 'pageNo' => $list['pageNo'],
  644. 'totalCount' => $list['totalCount'],
  645. ]
  646. ]);
  647. }
  648. /**
  649. * 下级供货商
  650. */
  651. public function actionSubSupplier(){
  652. $saas_id = get_saas_user_id();
  653. $query = SaasSupplierReferral::find()->alias('ssr')
  654. ->leftJoin(['s' => Supplier::tableName()], 'ssr.supplier_id = s.id')
  655. ->where(['ssr.referral_id' => $saas_id, 's.is_delete' => 0])
  656. ->andWhere(['IS NOT', 's.id', NULL])
  657. ->select('ssr.supplier_id, ssr.referral_rebate, ssr.is_enable');
  658. $list = pagination_make($query);
  659. foreach ($list['list'] as &$item) {
  660. $supplier = Supplier::findOne($item['supplier_id']);
  661. $item['supplier_name'] = $supplier->supplier_name;
  662. $item['logo'] = $supplier->logo;
  663. $item['is_enable'] = intval($item['is_enable']);
  664. $item['created_at'] = $item['created_at'] > 0 ? date('Y-m-d H:i:s', $item['created_at']) : '';
  665. $item['amount'] = SaasUserPriceLog::find()->where([
  666. 'log_type' => SaasUserPriceLog::LOG_TYPE_INCOME,
  667. 'amount_type' => SaasUserPriceLog::AMOUNT_TYPE_SUPPLIER_PARENT,
  668. 'supplier_id' => $item['supplier_id']
  669. ])->sum('amount') ?: 0;
  670. }
  671. return $this->asJson([
  672. 'code' => 0,
  673. 'msg' => 'success',
  674. 'data' => [
  675. 'data' => $list['list'],
  676. 'pageNo' => $list['pageNo'],
  677. 'totalCount' => $list['totalCount'],
  678. ]
  679. ]);
  680. }
  681. /**
  682. * 会员卡
  683. */
  684. public function actionBusinessOrder(){
  685. $saas_id = get_saas_user_id();
  686. // $saasIds = SaasUser::find()->where(['parent_id'=>$saas_id])->select('id')->asArray()->all();
  687. // var_dump($saasIds);exit;
  688. // if(!empty($saasIds) && count($saasIds)>0 ){
  689. $query = SaasShareMoney::find()->alias('s')->where(['s.saas_id'=> $saas_id,'s.is_delete'=>0]);
  690. $query->leftJoin(['o' => BusinessMemberOrder::tableName()], 'o.id = s.order_id');
  691. $query->select('o.saas_id,o.id order_id,o.member_id,o.pay_price,s.money,o.created_at');
  692. $list = pagination_make($query);
  693. if(count($list['list']) > 0 ){
  694. foreach($list['list'] as $key=>$val){
  695. if(isset($val['member_id']) && $val['member_id'] > 0 ){
  696. $businessInfo = BusinessMember::findOne($val['member_id']);
  697. if($businessInfo['is_delete']==0){
  698. $list['list'][$key]['businessInfo'] = $businessInfo;
  699. }else{
  700. $list['list'][$key]['businessInfo'] = [];
  701. }
  702. }
  703. if(isset($val['saas_id']) && $val['saas_id'] > 0){
  704. $saasInfo = SaasUser::find()->where(['id'=>$val['saas_id'],'is_delete'=>0])->select('name,avatar')->asArray()->one();
  705. $list['list'][$key]['saasInfo'] = $saasInfo;
  706. }
  707. }
  708. }
  709. $data['data'] = $list['list'];
  710. $data['pageNo'] = $list['pageNo'];
  711. $data['totalCount'] = $list['totalCount'];
  712. // }else{
  713. // $data['data'] = [];
  714. // $data['pageNo'] = 0;
  715. // $data['totalCount'] = 0;
  716. // }
  717. return $this->asJson([
  718. 'code' => 0,
  719. 'msg' => 'success',
  720. 'data' => $data
  721. ]);
  722. }
  723. /**
  724. * 结算明细
  725. */
  726. public function actionDetailedOrder(){
  727. $status = get_params('status',0);
  728. //会员分销订单
  729. $saas_id = get_saas_user_id();
  730. //$saas_id = 533;
  731. $store_share_type = implode(',', [
  732. SharingReceiver::FROM_AREA_AGENT_PROVIDER,
  733. SharingReceiver::FROM_AREA_AGENT_CITY,
  734. SharingReceiver::FROM_AREA_AGENT_DISTRICT,
  735. SharingReceiver::FROM_BD_AGENT
  736. ]);
  737. $query = SharingReceiver::find()->alias('sr')->where(['sr.is_delete' => 0])
  738. ->leftJoin(['o' => Order::tableName()], 'sr.order_no = o.order_no AND o.is_delete = 0')
  739. ->leftJoin(['sup' => SaasUserPriceLog::tableName()], "sr.order_no = sup.order_no AND sup.saas_id = {$saas_id}")
  740. ->leftJoin(['ssm' => StoreShareMoney::tableName()], "o.id = ssm.order_id AND sr.from in ({$store_share_type}) AND ssm.user_id = {$saas_id} ")
  741. ->andWhere(['sr.saas_id' => $saas_id])
  742. ->select('sr.id as sr_id, sr.order_no, sr.from, sup.log_type, sup.desc remark, sup.amount, sr.created_at, sup.created_at updated_at, sr.is_pay, sr.amount sharing_amount, sr.remark sharing_remark');
  743. if ($status == 1) {//未到账
  744. $query->andWhere([
  745. 'sr.is_pay' => [0, 2]
  746. ]);
  747. }
  748. if ($status == 2) {//已到账
  749. $query->andWhere([
  750. 'sr.is_pay' => 1
  751. ]);
  752. }
  753. $query->groupBy(new \yii\db\Expression("IF(sr.from IN (7, 8), IFNULL(ssm.id, 'default'), IFNULL(sup.id, 'default'))"))
  754. ->orderBy(new \yii\db\Expression("IF(sr.from IN (7, 8), IFNULL(ssm.id, 'default'), IFNULL(sup.id, 'default')) DESC"));
  755. $list = pagination_make($query);
  756. foreach ($list['list'] as &$item) {
  757. if (!isset($item['log_type'])) {
  758. $item['log_type'] = 1;
  759. }
  760. if (!isset($item['remark'])) {
  761. $item['remark'] = $item['sharing_remark'];
  762. }
  763. if (!isset($item['amount'])) {
  764. $item['amount'] = $item['sharing_amount'];
  765. }
  766. $item['log_type'] = intval($item['log_type']);
  767. }
  768. return $this->asJson([
  769. 'code' => 0,
  770. 'msg' => 'success',
  771. 'data' => [
  772. 'data' => $list['list'],
  773. 'pageNo' => $list['pageNo'],
  774. 'totalCount' => $list['totalCount'],
  775. ]
  776. ]);
  777. }
  778. //佣金明细
  779. public function actionPriceLog() {
  780. $log_type = get_params('log_type', 0);
  781. $saas_id = get_saas_user_id();
  782. $query = SaasUserPriceLog::find()->where(['saas_id' => $saas_id]);
  783. if($log_type){
  784. $query->andWhere(['log_type' => $log_type]);
  785. }
  786. $query->orderBy('id DESC');
  787. $list = pagination_make($query);
  788. return $this->asJson([
  789. 'code' => 0,
  790. 'msg' => 'success',
  791. 'data' => [
  792. 'data' => $list['list'],
  793. 'pageNo' => $list['pageNo'],
  794. 'totalCount' => $list['totalCount'],
  795. ]
  796. ]);
  797. }
  798. /**
  799. * @return mixed|string
  800. * 获取推广海报
  801. */
  802. public function actionGetQrcode()
  803. {
  804. $form = new ShareQrcodeForm();
  805. $form->type = 4;
  806. $form->user = get_user();
  807. $form->user_id = get_user_id();
  808. $form->saas_id = get_saas_user_id();
  809. $form->store_id = -1;
  810. return $this->asJson($form->search());
  811. }
  812. }