OrderController.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. <?php
  2. namespace hd\controllers;
  3. use Yii;
  4. use biz\shop\classes\ShopExtClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use biz\sj\services\MerchantService;
  7. use bizHd\custom\classes\CustomClass;
  8. use bizHd\custom\classes\HdClass;
  9. use bizHd\goods\classes\GoodsClass;
  10. use bizHd\merchant\services\ShopService;
  11. use bizHd\order\classes\OrderClass;
  12. use bizHd\order\classes\OrderForwardClass;
  13. use bizHd\order\classes\OrderItemClass;
  14. use bizHd\order\classes\OrderSendClass;
  15. use bizHd\order\services\OrderService;
  16. use bizHd\product\classes\ProductClass;
  17. use bizHd\saas\services\RegionService;
  18. use bizHd\shop\classes\ShopClass;
  19. use bizHd\user\services\UserService;
  20. use bizHd\work\classes\WorkClass;
  21. use common\components\dict;
  22. use common\components\dateUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use common\components\payUtil;
  26. use common\services\xhPayToolService;
  27. use common\components\util;
  28. use common\components\stringUtil;
  29. use bizHd\promote\services\CouponService;
  30. use common\components\httpUtil;
  31. use biz\wx\classes\WxMessageClass;
  32. use common\components\lakala\Lakala;
  33. class OrderController extends BaseController
  34. {
  35. public $guestAccess = ['order-relate', 'fast-pay', 'create-order', 'list'];
  36. public function actionDebt()
  37. {
  38. $get = Yii::$app->request->get();
  39. $id = $get['id'] ?? 0;
  40. $info = OrderClass::getById($id, true);
  41. if (empty($info)) {
  42. util::fail('没有找到订单');
  43. }
  44. if ($info->mainId != $this->mainId) {
  45. util::fail('不是你的订单');
  46. }
  47. //4秒内不允许重复提交
  48. util::checkRepeatCommit($id, 4);
  49. if ($info->status != 1) {
  50. util::fail('订单已付款或取消');
  51. }
  52. $payWay = dict::getDict('payWay', 'debtPay');
  53. $connection = Yii::$app->db;
  54. $transaction = $connection->beginTransaction();
  55. try {
  56. $actPrice = $info->actPrice ?? 0;
  57. $info->debtPrice = $actPrice;
  58. $info->remainDebtPrice = $actPrice;
  59. $info->debt = 1;
  60. $info->save();
  61. OrderClass::payAfter($info, $payWay);
  62. $transaction->commit();
  63. util::complete('赊账成功');
  64. } catch (\Exception $exception) {
  65. $transaction->rollBack();
  66. Yii::info("赊账失败原因:" . $exception->getMessage());
  67. util::fail('操作失败');
  68. }
  69. }
  70. //取消订单 ssh 20221231
  71. public function actionCancel()
  72. {
  73. //避免重复提交
  74. $adminId = $this->adminId;
  75. util::checkRepeatCommit($adminId, 8);
  76. $get = Yii::$app->request->get();
  77. $id = $get['id'] ?? 0;
  78. $order = OrderClass::getById($id, true);
  79. OrderClass::valid($order, $this->mainId);
  80. $deadTime = $order->deadline ?? 0;
  81. $current = time();
  82. $diff = bcsub($deadTime, $current);
  83. if ($diff < 60) {
  84. util::fail('60秒后将自动取消');
  85. }
  86. $connection = Yii::$app->db;
  87. $transaction = $connection->beginTransaction();
  88. try {
  89. OrderClass::setExpire($order, true);
  90. $transaction->commit();
  91. util::complete();
  92. } catch (\Exception $exception) {
  93. $transaction->rollBack();
  94. Yii::info("取消原因:" . $exception->getMessage());
  95. util::fail('取消失败');
  96. }
  97. }
  98. //修改订单 ssh 20220926
  99. public function actionUpdate()
  100. {
  101. $post = Yii::$app->request->post();
  102. $id = $post['id'] ?? 0;
  103. $order = OrderClass::getById($id, true);
  104. OrderClass::valid($order, $this->mainId);
  105. unset($post['id']);
  106. if ($order->status == 3) {
  107. util::fail('订单已配送');
  108. }
  109. if ($order->status == 4) {
  110. util::fail('订单已完成');
  111. }
  112. if ($order->status == 5) {
  113. util::fail('订单已取消');
  114. }
  115. if ($order->fromType == 4) {
  116. util::fail('请在美团APP上修改');
  117. }
  118. $address = $post['address'] ?? '';
  119. $floor = $post['floor'] ?? '';
  120. $post['fullAddress'] = $address . $floor;
  121. OrderClass::updateById($id, $post);
  122. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  123. $remark = $post['remark'] ?? '';
  124. $reachDate = $post['reachDate'] ?? '';
  125. $reachPeriod = $post['reachPeriod'] ?? '';
  126. $cardInfo = $post['cardInfo'] ?? '';
  127. $anonymity = $post['anonymity'] ?? 0;
  128. $bookName = $post['bookName'] ?? '';
  129. if (!empty($workList)) {
  130. foreach ($workList as $work) {
  131. $work->remark = $remark;
  132. $work->reachDate = $reachDate;
  133. $work->reachPeriod = $reachPeriod;
  134. $work->cardInfo = $cardInfo;
  135. $work->anonymity = $anonymity;
  136. $work->bookName = $bookName;
  137. $work->save();
  138. }
  139. }
  140. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  141. ShopExtClass::orderUpdateRemind($shopExt, $order);
  142. util::complete();
  143. }
  144. //客户欠款的订单 ssh 2021.2.4
  145. public function actionDebtList()
  146. {
  147. $get = Yii::$app->request->get();
  148. $id = $get['id'] ?? 0;
  149. $info = CustomClass::getById($id, true);
  150. CustomClass::valid($info, $this->shopId);
  151. $where = ['customId' => $id, 'debt' => 1];
  152. $searchTime = $get['searchTime'] ?? '';
  153. if (!empty($searchTime)) {
  154. $startTime = $get['startTime'] ?? '';
  155. $endTime = $get['endTime'] ?? '';
  156. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  157. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  158. }
  159. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice', 'debtPrice']);
  160. if (!empty($list)) {
  161. foreach ($list as $key => $value) {
  162. $debtPrice = $value['debtPrice'] ?? 0;
  163. $remainDebtPrice = $value['remainDebtPrice'] ?? 0;
  164. $hasPayDebt = 0;
  165. if ($debtPrice > $remainDebtPrice) {
  166. $hasPayDebt = bcsub($debtPrice, $remainDebtPrice, 2);
  167. }
  168. $list[$key]['hasPayDebt'] = floatval($hasPayDebt);
  169. }
  170. }
  171. $result = ['customInfo' => $info, 'list' => $list];
  172. util::success($result);
  173. }
  174. //发货
  175. public function actionSend()
  176. {
  177. $get = Yii::$app->request->get();
  178. $id = isset($get['id']) ? $get['id'] : 0;
  179. $order = OrderClass::getById($id, true);
  180. if (empty($order)) {
  181. util::fail('没有找到订单');
  182. }
  183. OrderClass::valid($order, $this->mainId);
  184. if ($order->status != 2) {
  185. util::fail('订单不是待发货状态');
  186. }
  187. // $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*', null, true);
  188. // if (!empty($workList)) {
  189. // $unComplete = false;
  190. // foreach ($workList as $work) {
  191. // if ($work->status != 1) {
  192. // $unComplete = true;
  193. // }
  194. // }
  195. // if ($unComplete == true) {
  196. // util::fail('还有制作单没有完成');
  197. // }
  198. // }
  199. $order->status = 4;
  200. $order->save();
  201. util::complete('操作成功');
  202. }
  203. //打印订单 ssh 20220601
  204. public function actionPrintOrder()
  205. {
  206. $get = Yii::$app->request->get();
  207. $id = isset($get['id']) ? $get['id'] : 0;
  208. $order = OrderClass::getById($id, true);
  209. if (empty($order)) {
  210. util::fail('没有找到订单');
  211. }
  212. if ($order->forward == 1) {
  213. util::fail('售后付款单不能打印');
  214. }
  215. OrderClass::valid($order, $this->mainId);
  216. OrderClass::onlinePrint($order, true);
  217. util::complete();
  218. }
  219. //微信和支付宝付款码支付复查 ssh 20220422
  220. public function actionCodePayCheck()
  221. {
  222. ini_set('date.timezone', 'Asia/Shanghai');
  223. header("Content-type: text/html; charset=utf-8");
  224. $get = Yii::$app->request->get();
  225. $id = isset($get['id']) ? $get['id'] : 0;
  226. $order = OrderClass::getById($id, true);
  227. OrderClass::valid($order, $this->mainId);
  228. $orderSn = $order->orderSn ?? '';
  229. $totalFee = $order->mainPay ?? 0;
  230. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  231. util::success(['returnStatus' => 'SUCCESS']);
  232. }
  233. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  234. util::complete('订单已取消');
  235. }
  236. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  237. util::complete('订单不是待付款状态');
  238. }
  239. $connection = Yii::$app->db;
  240. $transaction = $connection->beginTransaction();
  241. try {
  242. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  243. $shop = $this->shop;
  244. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  245. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  246. $params = [
  247. 'appid' => 'OP00002119',
  248. 'serial_no' => '018b08cfddbd',
  249. 'merchant_no' => $shop->lklSjNo,
  250. 'term_no' => $shop->lklScanTermNo,
  251. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  252. 'lklCertificatePath' => $lklCertificatePath,
  253. ];
  254. $laResource = new Lakala($params);
  255. $scanParams = ['orderSn' => $orderSn,];
  256. $response = $laResource->query($scanParams);
  257. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  258. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  259. $payWayType = dict::getDict('payWay', 'wxPay');
  260. $account_type = $response['resp_data']['account_type'] ?? '';
  261. if ($account_type == 'WECHAT') {
  262. $payWayType = dict::getDict('payWay', 'wxPay');
  263. }
  264. if ($account_type == 'ALIPAY') {
  265. $payWayType = dict::getDict('payWay', 'alipay');
  266. }
  267. $transactionId = $response['resp_data']['trade_no'] ?? '';
  268. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  269. $order->save();
  270. $attach = '';
  271. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  272. $transaction->commit();
  273. //打印小票和语音播报
  274. $newOrder = OrderClass::getById($id, true);
  275. OrderClass::onlinePrint($newOrder);
  276. ShopExtClass::hdGatheringReport($newOrder);
  277. $shopId = $newOrder->shopId ?? 0;
  278. $shop = ShopClass::getById($shopId, true);
  279. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  280. util::success(['returnStatus' => 'SUCCESS']);
  281. } else {
  282. util::complete('未知状态..');
  283. }
  284. }
  285. util::complete('未知状态..');
  286. } catch (\Exception $e) {
  287. $transaction->rollBack();
  288. util::complete('支付失败');
  289. }
  290. }
  291. //微信和支付宝付款码支付 ssh 2021.4.11
  292. public function actionCodePay()
  293. {
  294. ini_set('date.timezone', 'Asia/Shanghai');
  295. header("Content-type: text/html; charset=utf-8");
  296. $get = Yii::$app->request->get();
  297. $id = isset($get['id']) ? $get['id'] : 0;
  298. $version = $get['version'] ?? 0;
  299. $order = OrderClass::getById($id, true);
  300. OrderClass::valid($order, $this->mainId);
  301. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  302. $msg = '订单已取消';
  303. util::success(['returnStatus' => 'FAILURE'], $msg);
  304. }
  305. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  306. $msg = '订单不是待付款状态';
  307. util::success(['returnStatus' => 'FAILURE'], $msg);
  308. }
  309. $shop = $this->shop;
  310. $sj = $shop->merchantName;
  311. $shopName = $shop->shopName;
  312. $name = $shopName == '首店' ? $sj : $sj . '-' . $shopName;
  313. $authCode = (string)$get['authCode'] ?? '';
  314. if ($version > 0) {
  315. if (empty($authCode)) {
  316. $msg = '没有扫到付款码';
  317. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  318. noticeUtil::push($remind . ' 编号005', '15280215347');
  319. util::success(['returnStatus' => 'FAILURE'], $msg);
  320. }
  321. //微信文档说明 https://pay.weixin.qq.com/doc/v2/merchant/4011936234
  322. $wxPattern = '/^(10|11|12|13|14|15)\d{16}$/';
  323. //支付宝文档说明 https://opendocs.alipay.com/open/194/106039/
  324. $aliPattern = '/^(25|26|27|28|29|30)\d{14,22}$/';
  325. if (!preg_match($wxPattern, $authCode) && !preg_match($aliPattern, $authCode)) {
  326. $msg = '付款码错误,请使用微信或支付宝付款码';
  327. $remind = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  328. //noticeUtil::push($remind . ' 编号009', '15280215347');
  329. util::success(['returnStatus' => 'FAILURE'], $msg);
  330. }
  331. } else {
  332. if (empty($authCode)) {
  333. util::fail('没有获取到支付码');
  334. }
  335. }
  336. $orderSn = $order->orderSn ?? '';
  337. $lsCodePayErrorKey = 'ls_code_pay_error_' . $id;
  338. $hasFailPay = Yii::$app->redis->executeCommand('GET', [$lsCodePayErrorKey]);
  339. //前面有付失败过,要重新生成单号
  340. if (!empty($hasFailPay)) {
  341. $oldOrderSn = $order->orderSn;
  342. $orderSn = orderSn::getOrderSn();
  343. $order->orderSn = $orderSn;
  344. $order->save();
  345. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  346. }
  347. $subject = '购买花材 ' . $orderSn;
  348. $totalFee = $order->mainPay ?? 0;
  349. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  350. $connection = Yii::$app->db;
  351. $transaction = $connection->beginTransaction();
  352. try {
  353. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  354. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  355. $params = [
  356. 'appid' => 'OP00002119',
  357. 'serial_no' => '018b08cfddbd',
  358. 'merchant_no' => $shop->lklSjNo,
  359. 'term_no' => $shop->lklScanTermNo,
  360. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  361. 'lklCertificatePath' => $lklCertificatePath,
  362. ];
  363. $laResource = new Lakala($params);
  364. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  365. $scanParams = [
  366. 'orderSn' => $orderSn,
  367. 'amount' => $totalFee,
  368. 'capitalType' => $capitalType,
  369. 'notifyUrl' => $notifyUrl,
  370. 'subject' => $subject,
  371. 'authCode' => $authCode,
  372. ];
  373. $response = $laResource->scanPay($scanParams);
  374. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  375. $account_type = $response['resp_data']['account_type'] ?? '';
  376. $payWayType = dict::getDict('payWay', 'wxPay');
  377. if ($account_type == 'WECHAT') {
  378. $payWayType = dict::getDict('payWay', 'wxPay');
  379. }
  380. if ($account_type == 'ALIPAY') {
  381. $payWayType = dict::getDict('payWay', 'alipay');
  382. }
  383. $order->payWay = $payWayType;
  384. $transactionId = $response['resp_data']['trade_no'] ?? '';
  385. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  386. $order->save();
  387. $attach = '';
  388. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  389. $transaction->commit();
  390. //解决重复通知
  391. $cacheKey = 'hd_shop_order_pay_' . $orderSn;
  392. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  393. if (empty($has)) {
  394. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 300, 'has']);
  395. $newOrder = OrderClass::getById($id, true);
  396. //打印小票
  397. OrderClass::onlinePrint($newOrder);
  398. //语音播报
  399. ShopExtClass::hdGatheringReport($newOrder);
  400. $shopId = $newOrder->shopId ?? 0;
  401. $shop = ShopClass::getById($shopId, true);
  402. WxMessageClass::gatheringIncomeInform($shop, $newOrder);
  403. }
  404. util::success(['returnStatus' => 'SUCCESS']);
  405. } else {
  406. //标记有没付成功的次数,原因有可能是余额不足等
  407. Yii::$app->redis->executeCommand('SETEX', [$lsCodePayErrorKey, 1200, 'hasFail']);
  408. $msg = $response['msg'] ?? '';
  409. $retCode = $response['code'] ?? '';
  410. $fullMsg = $msg . "(零售端付款码:{$authCode})" . '(' . $name . ')';
  411. if ($retCode == 'BBS10000' || $retCode == 'BBS11105') {
  412. $shopId = $order->shopId;
  413. ShopExtClass::remindInputPassword($shopId);
  414. }
  415. //noticeUtil::push($fullMsg . ' ' . $retCode . ' 编号008', '15280215347');
  416. util::success(['returnStatus' => 'FAILURE', 'returnCode' => $retCode], $msg);
  417. }
  418. } catch (\Exception $e) {
  419. $transaction->rollBack();
  420. util::fail('支付失败');
  421. }
  422. }
  423. //开单操作 ssh 20220316
  424. public function actionCreateOrder()
  425. {
  426. $post = Yii::$app->request->post();
  427. $post['sjId'] = $this->sjId;
  428. $post['shopId'] = $this->shopId;
  429. $post['mainId'] = $this->mainId ?? 0;
  430. $now = time();
  431. $orderValidTime = getenv('ORDER_VALID_TIME') == false ? 600 : getenv('ORDER_VALID_TIME');
  432. $expireTime = $now + $orderValidTime;
  433. $post['deadline'] = $expireTime;
  434. $post['staffId'] = $this->shopAdminId ?? 0;
  435. $post['staffName'] = $this->shopAdmin->name ?? '';
  436. $post['flowerNum'] = isset($post['flowerNum']) && $post['flowerNum'] > 0 ? $post['flowerNum'] : 0;
  437. //默认情况开单员工和收款员工是同个人
  438. $staffId = $this->shopAdminId ?? 0;
  439. $staffName = $this->shopAdmin->name ?? '';
  440. if (empty($post['shopAdminId'])) {
  441. $post['shopAdminId'] = $staffId;
  442. $post['shopAdminName'] = $staffName;
  443. }
  444. if (empty($post['getStaffId'])) {
  445. $post['getStaffId'] = $staffId;
  446. $post['getStaffName'] = $this->shopAdmin->name ?? '';
  447. }
  448. $groupId = !empty($post['groupId']) ? $post['groupId'] : 0;
  449. $post['fromType'] = $post['fromType'] ?? 1;
  450. if ($post['fromType'] == dict::getDict('fromType', 'friend')) {
  451. if (empty($post['bookName'])) {
  452. util::fail('请填写订花人姓名');
  453. }
  454. }
  455. $customId = $post['customId'] ?? 0;
  456. $custom = CustomClass::getById($customId, true);
  457. if (empty($custom)) {
  458. util::fail('没有找到客户哦');
  459. }
  460. $hdId = $custom->hdId ?? 0;
  461. $hd = HdClass::getById($hdId, true);
  462. if (empty($hd)) {
  463. util::fail('客户对应的花店信息缺失,编号869');
  464. }
  465. $hdName = $hd->name ?? '';
  466. $post['hdId'] = $hdId;
  467. $post['hdName'] = $hdName;
  468. $customName = $custom->name ?? '';
  469. $post['customName'] = $customName;
  470. $post['customNamePy'] = stringUtil::py($customName);
  471. if (!empty($post['receiveMobile'])) {
  472. if (!stringUtil::isMobile($post['receiveMobile'])) {
  473. util::fail('请填写正确的收花人手机号');
  474. }
  475. }
  476. if (!empty($post['bookMobile'])) {
  477. if (!stringUtil::isMobile($post['bookMobile'])) {
  478. util::fail('请填写正确的订花人手机号');
  479. }
  480. }
  481. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  482. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  483. //收银台开单默认到店自取
  484. $post['sendType'] = dict::getDict('sendType', 'shopGet');
  485. }
  486. $forward = $post['forward'] ?? 0;
  487. if ($forward == 1) {
  488. if ($hasPay != 1) {
  489. util::fail('售后付款单只能走线下转账');
  490. }
  491. } else {
  492. //如果不是售后付款单,并且选的不是余额支付,余额又够付当前订单,则自动切到余额支付
  493. $balance = $custom->balance ?? 0;
  494. $modifyPrice = $post['modifyPrice'] ?? 0;
  495. if ($hasPay != 4 && $balance > $modifyPrice) {
  496. $post['hasPay'] = 4;
  497. $hasPay = 4;
  498. }
  499. }
  500. $productJson = $post['product'] ?? '';
  501. $productList = [];
  502. if (!empty($productJson)) {
  503. $productList = json_decode($productJson, true);
  504. }
  505. if (empty($productList) && empty($groupId)) {
  506. //商家手机上开单并生成制作单
  507. if (!empty($post['unitGoodsPrice'])) {
  508. $unitGoodsPrice = $post['unitGoodsPrice'];
  509. if (!is_numeric($unitGoodsPrice)) {
  510. util::fail('请输入正确的单价');
  511. }
  512. $productList = GoodsClass::orderCreateGoods($post);
  513. }
  514. }
  515. if (empty($productList) && empty($groupId)) {
  516. if ($post['lsGoodsPrice'] <= 0) {
  517. $lsGoodsPrice = $post['lsGoodsPrice'];
  518. if (!is_numeric($lsGoodsPrice)) {
  519. util::fail('请输入正确的金额');
  520. }
  521. util::fail('请填写金额或选商品');
  522. }
  523. }
  524. $connection = Yii::$app->db;
  525. $transaction = $connection->beginTransaction();
  526. try {
  527. $post['product'] = $productList;
  528. //解决重复提交
  529. util::checkRepeatCommit($this->adminId, 2);
  530. $reduceStock = $post['reduceStock'] ?? 0;
  531. if ($reduceStock == 1) {
  532. //如果扣除库,则走另外一条路
  533. $return = OrderClass::reduceMyStock($post, $this->shop, $custom);
  534. $transaction->commit();
  535. util::success($return);
  536. }
  537. //解决开单提交订单时,别人修改价格,造成提交价格不是真实卖价问题!!
  538. $version = $post['version'] ?? 0;
  539. if ($version == 2) {
  540. $dealPrice = $post['dealPrice'] ?? 0;
  541. if ($dealPrice == 0) {
  542. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  543. $itemInfo = ProductClass::getByIds($ids, null, 'id');
  544. if (empty($itemInfo)) {
  545. util::fail('要选择花材哦');
  546. }
  547. $diff = [];
  548. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  549. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  550. $level = 0;
  551. foreach ($productList as $currentProduct) {
  552. $currentPrice = $currentProduct['unitPrice'] ?? 0;
  553. $currentId = $currentProduct['productId'] ?? 0;
  554. $systemInfo = $itemInfo[$currentId] ?? [];
  555. $name = $systemInfo['name'] ?? '';
  556. $systemPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  557. if (floatval($currentPrice) != floatval($systemPrice)) {
  558. $diff[] = [
  559. 'name' => $name,
  560. 'price' => $systemPrice,
  561. 'kdPrice' => $currentPrice,
  562. ];
  563. }
  564. }
  565. if (!empty($diff)) {
  566. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  567. }
  568. }
  569. }
  570. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  571. $return = OrderService::createHdOrder($post, $custom, $hasPay);
  572. $shop = $this->shop;
  573. $staff = $this->shopAdmin;
  574. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  575. OrderClass::shPayChange($return, $shop, $staff);
  576. $transaction->commit();
  577. //打印小票和语音播报
  578. $id = $return->id ?? 0;
  579. $order = OrderClass::getById($id, true);
  580. if ($order->fromType == 3 || $order->fromType == 4) {
  581. //除了客服号下单和美团下单,其它情况允许打小票
  582. } else {
  583. //前台打小票
  584. OrderClass::onlinePrint($order);
  585. }
  586. //前台提示收款多少钱
  587. ShopExtClass::hdGatheringReport($order);
  588. //前台提醒出示付款码
  589. if (isset($order->status) && $order->status == 1) {
  590. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  591. ShopExtClass::pleasePayReport($order);
  592. }
  593. }
  594. $shopId = $order->shopId ?? 0;
  595. $shop = ShopClass::getById($shopId, true);
  596. WxMessageClass::gatheringIncomeInform($shop, $order);
  597. //新制作单提示
  598. $shopExt = ShopExtClass::getByCondition(['shopId' => $this->shopId], true);
  599. ShopExtClass::newWorkRemind($shopExt, $order);
  600. util::success($return);
  601. } catch (\Exception $e) {
  602. $transaction->rollBack();
  603. Yii::error("失败原因:" . $e->getMessage());
  604. util::fail('下单失败');
  605. }
  606. }
  607. //下单要用到的相关信息 ssh 2019.12.6
  608. public function actionOrderRelate()
  609. {
  610. $regionTree = RegionService::tree();
  611. $shop = $this->shop->attributes;
  612. $freight = MerchantExtendService::getFreight($this->sjExtend);
  613. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  614. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  615. util::success($out);
  616. }
  617. //余额支付 ssh 2019.12.6
  618. public function actionBalancePay()
  619. {
  620. $post = Yii::$app->request->post();
  621. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  622. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  623. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  624. $order = OrderService::getByOrderSn($orderSn);
  625. $userId = $this->adminId;
  626. $user = UserService::getUserInfo($userId);
  627. //验证支付密码是否正确
  628. UserService::validPayPassword($payPassword, $user);
  629. //支付前验证订单有效性
  630. OrderService::checkBeforePay($order);
  631. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  632. $totalFee = $order['prePrice'];
  633. $sourceType = 0;
  634. $discountData = OrderService::getDiscountPrice(['price' => $totalFee, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  635. $actPrice = $discountData['price'];
  636. $callbackParams = [];
  637. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  638. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  639. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'balance' => $balance]);
  640. }
  641. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  642. public function actionWxPay()
  643. {
  644. ini_set('date.timezone', 'Asia/Shanghai');
  645. $post = Yii::$app->request->post();
  646. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  647. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  648. $order = OrderService::getByOrderSn($orderSn);
  649. $orderId = $order['id'];
  650. //验证优惠券是否还有效
  651. if (!empty($couponId)) {
  652. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  653. }
  654. //支付前验证订单有效性
  655. OrderService::checkBeforePay($order);
  656. $name = isset($order['orderName']) && !empty($order['orderName']) ? $order['orderName'] : '购买商品';
  657. $totalFee = $order['realPrice'];
  658. //小程序使用miniOpenId
  659. if (httpUtil::isMiniProgram()) {
  660. $openId = $this->user['miniOpenId'];
  661. } else {
  662. $openId = $this->user['openId'];
  663. }
  664. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  665. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  666. $wxPayType = 0;
  667. if (httpUtil::isMiniProgram()) {
  668. $wxPayType = 1;
  669. }
  670. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  671. //订单30分钟后过期
  672. $now = time();
  673. $expireTime = $now + 1800;
  674. $wx = Yii::getAlias("@vendor/weixin");
  675. require_once($wx . '/lib/WxPay.Api.php');
  676. require_once($wx . '/example/WxPay.JsApiPay.php');
  677. $input = new \WxPayUnifiedOrder();
  678. $input->SetBody($name);
  679. $input->SetOut_trade_no($orderSn);
  680. $input->SetTotal_fee($totalFee * 100);
  681. $input->SetTime_start(date("YmdHis", $now));
  682. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  683. $input->SetTime_expire(date("YmdHis", $expireTime));
  684. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  685. $input->SetTrade_type("JSAPI");
  686. //花卉宝代为申请的微信支付
  687. $merchantExtend = $this->sjExtend->attributes;
  688. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  689. $input->SetSub_openid($openId);
  690. } else {
  691. $input->SetOpenid($openId);
  692. }
  693. //小程序使用miniAppId
  694. if (httpUtil::isMiniProgram()) {
  695. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  696. }
  697. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  698. $tools = new \JsApiPay();
  699. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  700. $newParams = json_decode($jsApiParameters, true);
  701. //已请求微信不能修改价格
  702. $updateData = [];
  703. $updateData['modPrice'] = 0;
  704. if (empty($order['deadline'])) {
  705. $updateData['deadline'] = $expireTime;
  706. }
  707. OrderService::updateById($orderId, $updateData);
  708. util::success($newParams);
  709. }
  710. //快捷付款订单
  711. public function actionFastPay()
  712. {
  713. ini_set('date.timezone', 'Asia/Shanghai');
  714. $post = Yii::$app->request->post();
  715. $payWay = isset($post['payWay']) ? $post['payWay'] : 0;
  716. $shopId = !empty($this->shopId) ? $this->shopId : ShopService::getDefaultShopId($this->sj);
  717. //验证门店是否有效
  718. $shopInfo = ShopService::getById($shopId);
  719. ShopService::valid($shopInfo, $this->sjId);
  720. $post['shopId'] = $shopId;
  721. //默认门店订单
  722. $store = isset($post['store']) ? $post['store'] : 1;
  723. $post['store'] = $store;
  724. //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
  725. $sourceType = $this->isWx ? 0 : 1;
  726. if (httpUtil::isMiniProgram()) {
  727. $sourceType = 2;
  728. }
  729. $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
  730. //兼容旧系统sourceType=3表示朋友圈来源
  731. $fromType = $sourceType == 3 ? 2 : 0;
  732. $fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
  733. $post['userId'] = $this->adminId;
  734. $custom = $this->custom;
  735. $post['bookName'] = $custom['userName'] ?? '';
  736. $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  737. $bookMobile = empty($bookMobile) && isset($custom['mobile']) && !empty($custom['mobile']) ? $custom['mobile'] : $bookMobile;
  738. $post['bookMobile'] = $bookMobile;
  739. $prePrice = round($post['prePrice'], 2);
  740. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  741. $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
  742. $actPrice = $discountData['price'];
  743. $post['discountType'] = $discountData['discountType'];
  744. $post['discountAmount'] = $discountData['discountAmount'];
  745. $post['actPrice'] = $actPrice;
  746. $post['realPrice'] = $actPrice;
  747. $post['payStyle'] = 0;
  748. $post['sjId'] = $this->sjId;
  749. $now = time();
  750. $expireTime = $now + 300;//订单5分钟后过期
  751. $post['createTime'] = date("Y-m-d H:i:s", $now);
  752. $post['deadline'] = $expireTime;
  753. if ($actPrice <= 0) {
  754. util::fail('请填写正确的金额');
  755. }
  756. //微信支付订单提交不能修改价格
  757. $post['modPrice'] = $this->isWx ? 0 : 1;
  758. $post['sourceType'] = $sourceType;
  759. $post['goodsNum'] = 1;
  760. $post['fromType'] = $fromType;
  761. $order = OrderService::addOrder($post);
  762. $orderId = $order['id'];
  763. $orderSn = $order['orderSn'];
  764. $sjId = $this->sjId;
  765. if ($payWay == 0) {
  766. $orderId = $order['id'];
  767. $name = '购买商品';
  768. $totalFee = $actPrice;
  769. $user = UserService::getById($this->adminId);
  770. $openId = isset($user['openId']) ? $user['openId'] : 0;
  771. if (httpUtil::isMiniProgram()) {
  772. //小程序使用miniOpenId
  773. $openId = $user['miniOpenId'];
  774. }
  775. if (empty($openId)) {
  776. util::fail('没有找到客户的openId');
  777. }
  778. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  779. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  780. $wxPayType = 0;
  781. if (httpUtil::isMiniProgram()) {
  782. $wxPayType = 1;
  783. }
  784. $attach = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&wxPayType=' . $wxPayType;
  785. $wx = Yii::getAlias("@vendor/weixin");
  786. require_once($wx . '/lib/WxPay.Api.php');
  787. require_once($wx . '/example/WxPay.JsApiPay.php');
  788. $input = new \WxPayUnifiedOrder();
  789. $input->SetBody($name);
  790. $input->SetOut_trade_no($orderSn);
  791. $input->SetTotal_fee($totalFee * 100);
  792. $input->SetTime_start(date("YmdHis", $now));
  793. $input->SetAttach($attach);
  794. //设置订单有效期5分钟
  795. $input->SetTime_expire(date("YmdHis", $expireTime));
  796. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  797. $input->SetTrade_type("JSAPI");
  798. //花卉宝代为申请的微信支付
  799. $merchantExtend = $this->sjExtend->attributes;
  800. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  801. $input->SetSub_openid($openId);
  802. } else {
  803. $input->SetOpenid($openId);
  804. }
  805. //小程序使用miniAppId
  806. if (httpUtil::isMiniProgram()) {
  807. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  808. }
  809. Yii::info('支付发起使用小程序信息' . json_encode($merchantExtend));
  810. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  811. $tools = new \JsApiPay();
  812. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  813. $newParams = json_decode($jsApiParameters, true);
  814. $newParams['orderId'] = $orderId;
  815. util::success($newParams);
  816. } elseif ($payWay == 1) {
  817. $extend = MerchantExtendService::getBySjId($sjId);
  818. if (isset($extend['alipayInit']) == false || $extend['alipayInit'] == 0) {
  819. util::fail('支付宝付款即将开通...');
  820. }
  821. $config = [
  822. //应用ID,您的APPID。
  823. 'app_id' => $extend['alipayPId'],
  824. //商户私钥,您的原始格式RSA私钥
  825. 'merchant_private_key' => $extend['alipayKey'],
  826. //异步通知地址
  827. 'notify_url' => Yii::$app->params['hdHost'] . "/notice/ali-callback",
  828. //同步跳转
  829. 'return_url' => "",
  830. //编码格式
  831. 'charset' => "UTF-8",
  832. //签名方式
  833. 'sign_type' => "RSA2",
  834. //支付宝网关
  835. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  836. //支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
  837. 'alipay_public_key' => $extend['alipayPublicKey'],
  838. ];
  839. Yii::info(json_encode($config) . ' aplipay config');
  840. $alipayWap = Yii::getAlias("@vendor/alipayWap");
  841. require_once($alipayWap . '/wappay/service/AlipayTradeService.php');
  842. require_once($alipayWap . '/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php');
  843. $totalFee = $order['realPrice'];
  844. $out_trade_no = $orderSn;//商户订单号,商户网站订单系统中唯一订单号,必填
  845. $subject = '购买商品';//订单名称,必填
  846. $total_amount = $totalFee;//付款金额,必填
  847. $body = '';//商品描述,可空
  848. $timeout_express = "1m";//超时时间
  849. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  850. $passBackParams = 'capitalType=' . $capitalType . '&couponId=' . $couponId . '&sjId=' . $sjId;//将流水类型、优惠卷传过去
  851. $passBackParams = urlencode($passBackParams);
  852. $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
  853. $payRequestBuilder->setBody($body);
  854. $payRequestBuilder->setSubject($subject);
  855. $payRequestBuilder->setOutTradeNo($out_trade_no);
  856. $payRequestBuilder->setTotalAmount($total_amount);
  857. $payRequestBuilder->setTimeExpress($timeout_express);
  858. //在异步通知时将该参数原样返回
  859. $payRequestBuilder->setPassbackParams($passBackParams);
  860. //同步通知
  861. $returnUrl = Yii::$app->params['mallDomain'] . "/#/pages/callback/success?account={$sjId}&shopId={$shopId}&orderSn={$orderSn}&totalPrice={$totalFee}&pageStatus=3&payDiscountPrice=0&payDiscountType=0";
  862. //异步通知
  863. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/ali-callback";
  864. $payResponse = new \AlipayTradeService($config);
  865. $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);
  866. //直接将支付宝的html返回给前端
  867. echo $result;
  868. } elseif ($payWay == 2) {
  869. //余额支付,验证支付密码是否正确
  870. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : 0;
  871. $user = UserService::getUserInfo($this->adminId);
  872. UserService::validPayPassword($payPassword, $user);
  873. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  874. $callbackParams = [];
  875. $respond = xhPayToolService::balancePay($callbackParams, $orderSn, $actPrice, $capitalType, $couponId);
  876. $balance = isset($respond['balance']) ? $respond['balance'] : 0;
  877. util::success(['discountAmount' => $discountData['discountAmount'], 'discountType' => $discountData['discountType'], 'orderSn' => $orderSn, 'orderId' => $orderId, 'balance' => $balance]);
  878. } else {
  879. util::fail('无效的支付方式');
  880. }
  881. }
  882. //订单评价
  883. public function actionComment()
  884. {
  885. $post = Yii::$app->request->post();
  886. $id = $post['id'];
  887. $order = OrderService::getById($id);
  888. OrderService::valid($order, $this->mainId);
  889. if ($order['grade'] > 0) {
  890. util::fail('您已经评过了');
  891. }
  892. OrderService::comment($post);
  893. util::complete('提交成功');
  894. }
  895. //订单详情 ssh 2019.12.16
  896. public function actionDetail()
  897. {
  898. $id = Yii::$app->request->get('id', 0);
  899. $detail = OrderClass::getFullInfo($id);
  900. OrderService::valid($detail, $this->mainId);
  901. $forwardMap = [];
  902. if ($detail['forward'] == 1) {
  903. //当前单如果已经是售后付款单,则取出其对应的原单
  904. $map = OrderForwardClass::getByCondition(['shOrderId' => $id], true);
  905. if (!empty($map)) {
  906. $originOrderId = $map->orderId ?? 0;
  907. $forwardMap['orderId'] = $originOrderId;
  908. }
  909. }
  910. $detail['forwardMap'] = $forwardMap;
  911. $shOrderMap = [];
  912. if ($detail['hasForward'] == 1) {
  913. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  914. }
  915. $detail['shOrderMap'] = $shOrderMap;
  916. $main = $this->main;
  917. //是否走售后付款单模式判断 ssh
  918. $ret = OrderClass::ifShPay($detail, $main, 0);
  919. $shAddPay = $ret['shAddPay'];
  920. $shAddPayReason = $ret['shAddPayReason'];
  921. $detail['shAddPay'] = $shAddPay;
  922. $detail['shAddPayReason'] = $shAddPayReason;
  923. util::success($detail);
  924. }
  925. public function actionList()
  926. {
  927. $get = Yii::$app->request->get();
  928. $search = isset($get['searchText']) ? $get['searchText'] : '';
  929. $searchType = isset($get['searchType']) && is_numeric($get['searchType']) ? $get['searchType'] : '';
  930. $shopId = $this->shopId ?? 0;
  931. if (empty($shopId)) {
  932. //没有登录不显示数据
  933. util::success(['list' => [], 'moreData' => 0, 'shop' => [], 'totalNum' => 0, 'totalPage' => 0]);
  934. }
  935. $where = [];
  936. $where['shopId'] = $shopId;
  937. if (!empty($get['customId'])) {
  938. $where['customId'] = $get['customId'];
  939. }
  940. if (!empty($get['ids'])) {
  941. $stringIds = $get['ids'];
  942. $newIds = explode(',', $stringIds);
  943. $where['id'] = ['in', $newIds];
  944. }
  945. $status = $get['status'] ?? 0;
  946. if (!empty($status)) {
  947. $where['status'] = $get['status'];
  948. }
  949. $debt = $get['debt'] ?? -1;
  950. if ($debt > -1) {
  951. $where['debt'] = $debt;
  952. }
  953. if (!empty($get['shopAdminId'])) {
  954. $where['shopAdminId'] = $get['shopAdminId'];
  955. }
  956. if (!empty($search)) {
  957. if (is_numeric($searchType)) {
  958. if ($searchType == 0) {
  959. $where['actPrice'] = $search;
  960. } elseif ($searchType == 1) {
  961. $where['orderSn'] = $search;
  962. } elseif ($searchType == 2) {
  963. $where['bookMobile'] = $search;
  964. } elseif ($searchType == 3) {
  965. $where['customName'] = ['like', $search];
  966. } else {
  967. $where['actPrice'] = $search;
  968. }
  969. } else {
  970. //兼容旧版本
  971. if (stringUtil::isMobile($search)) {
  972. $where['bookMobile'] = $search;
  973. } else {
  974. $where['orderSn'] = strtoupper($search);
  975. }
  976. }
  977. }
  978. if (isset($get['repeat']) && $get['repeat'] > -1) {
  979. $where['repeat'] = $get['repeat'];
  980. }
  981. if (!empty($get['searchTime'])) {
  982. $startTime = $get['startTime'] ?? '';
  983. $endTime = $get['endTime'] ?? '';
  984. $period = dateUtil::formatTime($get['searchTime'], $startTime, $endTime);
  985. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  986. }
  987. $list = OrderService::getOrderList($where);
  988. $list['shop'] = $this->shop->attributes ?? [];
  989. util::success($list);
  990. }
  991. //订单归类 ssh 2019.12.17
  992. public function actionClassify()
  993. {
  994. $post = Yii::$app->request->post();
  995. $id = isset($post['id']) ? $post['id'] : 0;
  996. $order = OrderService::getById($id);
  997. OrderService::valid($order, $this->mainId);
  998. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  999. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  1000. OrderService::classify($id, $categoryId, $usageId);
  1001. util::complete();
  1002. }
  1003. //更新配送单 ssh 2019.12.17
  1004. public function actionUpdateSheet()
  1005. {
  1006. $post = Yii::$app->request->post();
  1007. $id = isset($post['id']) ? $post['id'] : 0;
  1008. unset($post['id']);
  1009. $order = OrderService::getById($id);
  1010. OrderService::valid($order, $this->mainId);
  1011. OrderService::updateSheet($order, $post);
  1012. util::complete('提交成功');
  1013. }
  1014. //商家收款与发货 ssh 2019.12.18
  1015. public function actionGathering()
  1016. {
  1017. $post = Yii::$app->request->post();
  1018. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1019. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1020. $userId = isset($post['userId']) ? $post['userId'] : 0;
  1021. $userInfo = UserService::getById($userId);
  1022. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1023. util::fail('您选择的客户无效');
  1024. }
  1025. if ($price <= 0) {
  1026. util::fail('请输入金额');
  1027. }
  1028. $post['prePrice'] = $price;
  1029. $post['actPrice'] = $price;
  1030. $post['payWay'] = $payWay;
  1031. $post['sourceType'] = 1;
  1032. $post['userId'] = $userId;
  1033. $post['goodsNum'] = 1;
  1034. $post['orderName'] = '商品';
  1035. $shopId = MerchantService::getDefaultShopId($this->sj);
  1036. $post['shopId'] = $shopId;
  1037. $order = OrderService::addOrder($post);
  1038. $id = $order['id'];
  1039. $orderSn = $order['orderSn'];
  1040. //流水类型列表
  1041. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1042. $callbackParams = [];
  1043. switch ($payWay) {
  1044. case 0:
  1045. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1046. break;
  1047. case 1:
  1048. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1049. break;
  1050. case 2:
  1051. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1052. break;
  1053. default:
  1054. util::fail('无效支付方式');
  1055. }
  1056. util::success($order);
  1057. }
  1058. //免发货管理 ssh 2020.1.6
  1059. public function actionWithoutSend()
  1060. {
  1061. $id = Yii::$app->request->get('id');
  1062. $order = OrderService::getById($id);
  1063. OrderService::valid($order, $this->mainId);
  1064. //配送流程变更
  1065. $currentFlow = OrderSendClass::withoutSend($order);
  1066. util::success(['currentFlow' => $currentFlow]);
  1067. }
  1068. //修改价格 ssh 2020.1.6
  1069. public function actionUpdatePrice()
  1070. {
  1071. $id = Yii::$app->request->get('id');
  1072. $price = Yii::$app->request->get('price', 1);
  1073. $order = OrderService::getById($id);
  1074. OrderService::valid($order, $this->mainId);
  1075. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1076. util::fail('已发起支付,不能修改价格');
  1077. }
  1078. OrderService::updateById($id, ['actPrice' => $price]);
  1079. util::complete('修改成功');
  1080. }
  1081. //配送单 ssh 2020.2.29
  1082. public function actionGetDeliverDetail()
  1083. {
  1084. $id = Yii::$app->request->get('id', 0);
  1085. $order = OrderService::getById($id);
  1086. OrderService::valid($order, $this->mainId);
  1087. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1088. $reachPeriodId = $order['reachPeriod'];
  1089. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1090. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1091. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1092. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1093. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1094. $bookName = '--';
  1095. $bookMobile = '--';
  1096. }
  1097. $data = [
  1098. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1099. 'orderSn' => $order['orderSn'],
  1100. 'receiveUserName' => $order['receiveUserName'],
  1101. 'receiveMobile' => $order['receiveMobile'],
  1102. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1103. 'cardInfo' => $order['cardInfo'],
  1104. 'bookMobile' => $bookMobile,
  1105. 'bookName' => $bookName,
  1106. 'remark' => $order['remark'],
  1107. 'sendNum' => $order['sendNum'],
  1108. 'telephone' => 18030142050,
  1109. 'printTime' => '',
  1110. 'img' => '',
  1111. ];
  1112. util::success($data);
  1113. }
  1114. //运费计算 lqh 2021.4.12 暂反回固定
  1115. public function actionFreight()
  1116. {
  1117. util::success(['sedCost' => 10]);
  1118. }
  1119. }