OrderController.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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, 3);
  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'] ?? 2;
  950. if ($debt != 2) {
  951. $where['debt'] = $debt;
  952. }
  953. if (isset($get['shopAdminId']) && !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. $searchTime = $get['searchTime'] ?? '';
  979. if (!empty($searchTime)) {
  980. $startTime = $get['startTime'] ?? '';
  981. $endTime = $get['endTime'] ?? '';
  982. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  983. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  984. }
  985. $list = OrderService::getOrderList($where);
  986. $list['shop'] = $this->shop->attributes ?? [];
  987. util::success($list);
  988. }
  989. //订单归类 ssh 2019.12.17
  990. public function actionClassify()
  991. {
  992. $post = Yii::$app->request->post();
  993. $id = isset($post['id']) ? $post['id'] : 0;
  994. $order = OrderService::getById($id);
  995. OrderService::valid($order, $this->mainId);
  996. $categoryId = isset($post['categoryId']) ? $post['categoryId'] : $post['categoryId'];
  997. $usageId = isset($post['usageId']) ? $post['usageId'] : $post['usageId'];
  998. OrderService::classify($id, $categoryId, $usageId);
  999. util::complete();
  1000. }
  1001. //更新配送单 ssh 2019.12.17
  1002. public function actionUpdateSheet()
  1003. {
  1004. $post = Yii::$app->request->post();
  1005. $id = isset($post['id']) ? $post['id'] : 0;
  1006. unset($post['id']);
  1007. $order = OrderService::getById($id);
  1008. OrderService::valid($order, $this->mainId);
  1009. OrderService::updateSheet($order, $post);
  1010. util::complete('提交成功');
  1011. }
  1012. //商家收款与发货 ssh 2019.12.18
  1013. public function actionGathering()
  1014. {
  1015. $post = Yii::$app->request->post();
  1016. $price = isset($post['price']) && is_numeric($post['price']) ? $post['price'] : 0;
  1017. $payWay = isset($post['payWay']) && is_numeric($post['payWay']) ? $post['payWay'] : 0;
  1018. $userId = isset($post['userId']) ? $post['userId'] : 0;
  1019. $userInfo = UserService::getById($userId);
  1020. if (empty($userInfo) || $userInfo['sjId'] != $this->sjId) {
  1021. util::fail('您选择的客户无效');
  1022. }
  1023. if ($price <= 0) {
  1024. util::fail('请输入金额');
  1025. }
  1026. $post['prePrice'] = $price;
  1027. $post['actPrice'] = $price;
  1028. $post['payWay'] = $payWay;
  1029. $post['sourceType'] = 1;
  1030. $post['userId'] = $userId;
  1031. $post['goodsNum'] = 1;
  1032. $post['orderName'] = '商品';
  1033. $shopId = MerchantService::getDefaultShopId($this->sj);
  1034. $post['shopId'] = $shopId;
  1035. $order = OrderService::addOrder($post);
  1036. $id = $order['id'];
  1037. $orderSn = $order['orderSn'];
  1038. //流水类型列表
  1039. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1040. $callbackParams = [];
  1041. switch ($payWay) {
  1042. case 0:
  1043. xhPayToolService::wxPay($callbackParams, $orderSn, $price, $capitalType, 0);
  1044. break;
  1045. case 1:
  1046. xhPayToolService::alipay($callbackParams, $orderSn, $price, $capitalType, 0, []);
  1047. break;
  1048. case 2:
  1049. xhPayToolService::balancePay($callbackParams, $orderSn, $price, $capitalType, 0);
  1050. break;
  1051. default:
  1052. util::fail('无效支付方式');
  1053. }
  1054. util::success($order);
  1055. }
  1056. //免发货管理 ssh 2020.1.6
  1057. public function actionWithoutSend()
  1058. {
  1059. $id = Yii::$app->request->get('id');
  1060. $order = OrderService::getById($id);
  1061. OrderService::valid($order, $this->mainId);
  1062. //配送流程变更
  1063. $currentFlow = OrderSendClass::withoutSend($order);
  1064. util::success(['currentFlow' => $currentFlow]);
  1065. }
  1066. //修改价格 ssh 2020.1.6
  1067. public function actionUpdatePrice()
  1068. {
  1069. $id = Yii::$app->request->get('id');
  1070. $price = Yii::$app->request->get('price', 1);
  1071. $order = OrderService::getById($id);
  1072. OrderService::valid($order, $this->mainId);
  1073. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1074. util::fail('已发起支付,不能修改价格');
  1075. }
  1076. OrderService::updateById($id, ['actPrice' => $price]);
  1077. util::complete('修改成功');
  1078. }
  1079. //配送单 ssh 2020.2.29
  1080. public function actionGetDeliverDetail()
  1081. {
  1082. $id = Yii::$app->request->get('id', 0);
  1083. $order = OrderService::getById($id);
  1084. OrderService::valid($order, $this->mainId);
  1085. $reachDate = isset($order['reachDate']) && !empty($order['reachDate']) ? $order['reachDate'] : '';
  1086. $reachPeriodId = $order['reachPeriod'];
  1087. $reachPeriodArr = [0 => '上午', 1 => '下午', 2 => '晚上'];
  1088. $reachPeriod = isset($reachPeriodArr[$reachPeriodId]) ? $reachPeriodArr[$reachPeriodId] : '';
  1089. $bookName = isset($order['bookName']) ? $order['bookName'] : '';
  1090. $bookMobile = isset($order['bookMobile']) ? $order['bookMobile'] : '';
  1091. if (isset($order['anonymity']) && $order['anonymity'] == 1) {
  1092. $bookName = '--';
  1093. $bookMobile = '--';
  1094. }
  1095. $data = [
  1096. 'reachDate' => $reachDate . ' ' . $reachPeriod,
  1097. 'orderSn' => $order['orderSn'],
  1098. 'receiveUserName' => $order['receiveUserName'],
  1099. 'receiveMobile' => $order['receiveMobile'],
  1100. 'fullAddress' => $order['fullAddress'] . "(" . $order['showAddress'] . ")",
  1101. 'cardInfo' => $order['cardInfo'],
  1102. 'bookMobile' => $bookMobile,
  1103. 'bookName' => $bookName,
  1104. 'remark' => $order['remark'],
  1105. 'sendNum' => $order['sendNum'],
  1106. 'telephone' => 18030142050,
  1107. 'printTime' => '',
  1108. 'img' => '',
  1109. ];
  1110. util::success($data);
  1111. }
  1112. //运费计算 lqh 2021.4.12 暂反回固定
  1113. public function actionFreight()
  1114. {
  1115. util::success(['sedCost' => 10]);
  1116. }
  1117. }