OrderController.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizGhs\express\services\DadaExpressServices;
  5. use bizHd\purchase\classes\PurchaseClass;
  6. use bizHd\purchase\services\PurchaseService;
  7. use bizHd\wx\classes\WxOpenClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizGhs\order\services\OrderService;
  11. use bizHd\saas\services\RegionService;
  12. use bizGhs\product\classes\ProductClass;
  13. use common\components\baiduAip;
  14. use common\components\dict;
  15. use common\components\dateUtil;
  16. use common\components\freight;
  17. use common\components\httpUtil;
  18. use common\components\noticeUtil;
  19. use common\components\oss;
  20. use common\components\payUtil;
  21. use common\components\qrCodeUtil;
  22. use Yii;
  23. use common\components\util;
  24. use biz\shop\classes\ShopAdminClass;
  25. use biz\notice\classes\NoticeClass;
  26. use biz\wx\classes\WxMessageClass;
  27. use biz\shop\classes\ShopClass;
  28. use common\components\lakala\Lakala;
  29. class OrderController extends BaseController
  30. {
  31. public $guestAccess = ['create-order', 'order-relate', 'fast-pay', 'info'];
  32. //确认送达
  33. public function actionConfirmReach()
  34. {
  35. $get = Yii::$app->request->get();
  36. $id = $get['id'] ?? '';
  37. $order = OrderClass::getById($id, true);
  38. if (empty($order)) {
  39. util::fail('没有找到订单');
  40. }
  41. if ($order->mainId != $this->mainId) {
  42. util::fail('不是你的订单');
  43. }
  44. if ($order->status == 1) {
  45. util::fail('订单待付款');
  46. }
  47. if ($order->status == 4) {
  48. util::fail('订单已完成');
  49. }
  50. if ($order->status == 5) {
  51. util::fail('订单已取消');
  52. }
  53. OrderClass::confirmReach($order);
  54. util::complete('操作成功');
  55. }
  56. public function actionAllFh()
  57. {
  58. $post = Yii::$app->request->post();
  59. $ids = $post['ids'] ?? '';
  60. if (empty($ids)) {
  61. util::fail('请选择要合并发货的订单');
  62. }
  63. $arr = explode(',', $ids);
  64. if (empty($arr)) {
  65. util::fail('请选择要合并发货的订单哦');
  66. }
  67. $fhWl = $post['fhWl'] ?? '';
  68. $fhWlNo = $post['fhWlNo'] ?? '';
  69. $fhType = 0;
  70. $params = [
  71. 'fhType' => $fhType,
  72. 'fhWl' => $fhWl,
  73. 'fhWlNo' => $fhWlNo,
  74. ];
  75. $connection = Yii::$app->db;
  76. $transaction = $connection->beginTransaction();
  77. try {
  78. $all = OrderClass::getAllByCondition(['id' => ['in', $arr]], null, '*', null, true);
  79. foreach ($all as $order) {
  80. if ($order->book == 1) {
  81. util::fail('有预订单,不能一起发货');
  82. }
  83. if ($order->mainId != $this->mainId) {
  84. util::fail('不是你的订单哦');
  85. }
  86. OrderClass::orderSend($order, $params, true);
  87. }
  88. $transaction->commit();
  89. util::complete();
  90. } catch (\Exception $e) {
  91. $transaction->rollBack();
  92. Yii::error("操作失败原因:" . $e->getMessage());
  93. util::fail('操作失败');
  94. }
  95. }
  96. //发货 ssh 20230613
  97. public function actionFh()
  98. {
  99. $post = Yii::$app->request->post();
  100. $id = $post['id'] ?? '';
  101. $order = OrderClass::getById($id, true);
  102. OrderClass::valid($order, $this->shopId);
  103. $fhWl = $post['fhWl'] ?? '';
  104. $fhWlNo = $post['fhWlNo'] ?? '';
  105. $fhType = $post['fhType'] ?? '';
  106. $params = [
  107. 'fhType' => $fhType,
  108. 'fhWl' => $fhWl,
  109. 'fhWlNo' => $fhWlNo,
  110. ];
  111. if ($order->book == 1) {
  112. util::fail('预订单不支持这个发货方式');
  113. }
  114. $connection = Yii::$app->db;
  115. $transaction = $connection->beginTransaction();
  116. try {
  117. OrderClass::orderSend($order, $params);
  118. $transaction->commit();
  119. util::complete();
  120. } catch (\Exception $e) {
  121. $transaction->rollBack();
  122. Yii::error("操作失败原因:" . $e->getMessage());
  123. util::fail('操作失败');
  124. }
  125. }
  126. //合并打印 ssh 20230929
  127. public function actionMergePrint()
  128. {
  129. $post = Yii::$app->request->post();
  130. $ids = $post['ids'] ?? '';
  131. if (empty($ids)) {
  132. util::fail('请选择要合并打印的订单');
  133. }
  134. $arr = explode(',', $ids);
  135. if (empty($arr)) {
  136. util::fail('请选择要合并打印的订单哦');
  137. }
  138. if (getenv('YII_ENV') == 'production') {
  139. if ($this->mainId != 10536 && $this->mainId != 50) {
  140. //util::fail('功能开发中');
  141. }
  142. } else {
  143. if ($this->mainId != 11) {
  144. //util::fail('功能开发中');
  145. }
  146. }
  147. OrderClass::mergePrintOrder($arr, $this->mainId);
  148. util::complete('打印成功');
  149. }
  150. //修改分工 ssh 20221129
  151. public function actionChangeWorker()
  152. {
  153. $post = Yii::$app->request->post();
  154. $id = $post['id'] ?? 0;
  155. $shopAdminId = $post['shopAdminId'] ?? 0;
  156. $shopAdminName = $post['shopAdminName'] ?? '';
  157. $order = OrderClass::getById($id, true);
  158. OrderClass::valid($order, $this->shopId);
  159. $preShopAdminId = $order->shopAdminId ?? 0;
  160. if (!empty($preShopAdminId)) {
  161. $shopAdmin = $this->shopAdmin;
  162. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  163. util::fail('管理员才能操作');
  164. }
  165. }
  166. $order->shopAdminId = $shopAdminId;
  167. $order->shopAdminName = $shopAdminName;
  168. $order->save();
  169. util::complete();
  170. }
  171. //预订单到货 ssh 20220321
  172. public function actionArrival()
  173. {
  174. $post = Yii::$app->request->post();
  175. $id = $post['id'] ?? 0;
  176. $connection = Yii::$app->db;
  177. $transaction = $connection->beginTransaction();
  178. try {
  179. $order = OrderClass::getById($id, true);
  180. OrderClass::valid($order, $this->shopId);
  181. $post['shopAdminId'] = $this->shopAdminId;
  182. $post['shopAdminName'] = $this->shopAdmin ? $this->shopAdmin->name : '';
  183. OrderService::arrival($order, $post);
  184. $transaction->commit();
  185. util::complete();
  186. } catch (\Exception $e) {
  187. $transaction->rollBack();
  188. Yii::error("操作失败原因:" . $e->getMessage());
  189. util::fail('操作失败');
  190. }
  191. }
  192. //到货获取订单详情 ssh 20220323
  193. public function actionArrivalDetail()
  194. {
  195. $get = Yii::$app->request->get();
  196. $id = $get['id'] ?? 0;
  197. $info = OrderClass::getById($id);
  198. OrderClass::valid($info, $this->shopId);
  199. $showItemData = OrderClass::getOriginalShowItem(true, $info);
  200. $itemList = $showItemData['list'] ?? [];
  201. $info['product'] = $itemList;
  202. $kiloFee = $this->shop->kiloFee ?? 0;
  203. $miniKilo = $this->shop->miniKilo ?? 0;
  204. $info['kiloFee'] = $kiloFee;
  205. $info['miniKilo'] = $miniKilo;
  206. util::success($info);
  207. }
  208. //支付宝扫码支付 ssh 20210103
  209. public function actionScanPayCheck()
  210. {
  211. $get = Yii::$app->request->get();
  212. $id = isset($get['id']) ? $get['id'] : 0;
  213. $order = OrderClass::getById($id, true);
  214. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY && $order->status != OrderClass::ORDER_STATUS_CANCEL) {
  215. util::success(['returnStatus' => 'SUCCESS']);
  216. }
  217. $deadline = $order->deadline ?? '';
  218. $deadTime = strtotime($deadline);
  219. $current = time();
  220. $diff = bcsub($deadTime, $current);
  221. if ($diff <= 60) {
  222. util::success(['returnStatus' => 'EXPIRE']);
  223. }
  224. util::success(['returnStatus' => 'FAILURE']);
  225. }
  226. //微信和支付宝扫码支付 ssh 20210103
  227. public function actionScanPay()
  228. {
  229. ini_set('date.timezone', 'Asia/Shanghai');
  230. header("Content-type: text/html; charset=utf-8");
  231. $get = Yii::$app->request->get();
  232. $id = isset($get['id']) ? $get['id'] : 0;
  233. $order = OrderClass::getById($id, true);
  234. if (isset($order->status) == false || $order->status != 1) {
  235. util::success(['returnStatus' => 'FAILURE'], '不是待付款订单');
  236. }
  237. OrderClass::valid($order, $this->shopId);
  238. $cgId = $order->purchaseId ?? 0;
  239. $cg = PurchaseClass::getById($cgId, true);
  240. $orderSn = $cg->orderSn ?? '';
  241. if (empty($orderSn)) {
  242. util::success(['returnStatus' => 'FAILURE'], '没有找到采购单');
  243. }
  244. $deadline = $cg->deadline;
  245. $current = date("Y-m-d H:i:s");
  246. if ((strtotime($deadline) + 20) < strtotime($current)) {
  247. util::success(['returnStatus' => 'FAILURE'], '订单已经失效');
  248. }
  249. $totalFee = $cg->actPrice ?? 0;
  250. $subject = '购买鲜花';
  251. $totalAmount = $totalFee;
  252. $body = "到店订单";
  253. //标记为扫码支付 和 自取订单
  254. $order->codePay = 2;
  255. $order->sendType = OrderClass::SEND_TYPE_NO;
  256. $order->save();
  257. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  258. $cg->codePay = 2;
  259. $cg->save();
  260. $shop = $this->shop;
  261. $currentCapitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  262. $wxPayWay = dict::getDict('payWay', 'wxPay');
  263. $aliPayWay = dict::getDict('payWay', 'alipay');
  264. $payWay = Yii::$app->request->get('payWay', $wxPayWay);
  265. if ($payWay == $wxPayWay) {
  266. if (isset($order->wxPayUrl) && !empty($order->wxPayUrl)) {
  267. $url = $order->wxPayUrl;
  268. } else {
  269. util::fail('暂不支持');
  270. $merchantExtend = WxOpenClass::getWxInfo();
  271. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  272. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  273. $params = [
  274. 'appid' => 'OP00002119',
  275. 'serial_no' => '018b08cfddbd',
  276. 'merchant_no' => $shop->lklSjNo,
  277. 'term_no' => $shop->lklScanTermNo,
  278. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  279. 'lklCertificatePath' => $lklCertificatePath,
  280. ];
  281. $laResource = new Lakala($params);
  282. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  283. $wxParams = [
  284. 'orderSn' => $orderSn,
  285. 'amount' => $totalFee,
  286. 'capitalType' => $currentCapitalType,
  287. 'notifyUrl' => $notifyUrl,
  288. 'subject' => $subject,
  289. 'couponId' => 0,
  290. 'wxAppId' => $merchantExtend['miniAppId'],
  291. ];
  292. $response = $laResource->driveNativeWxPay($wxParams);
  293. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  294. $msg = $response['msg'] ?? '';
  295. noticeUtil::push($msg, '15280215347');
  296. util::fail('二维码生成失败');
  297. }
  298. $url = isset($response['resp_data']['acc_resp_fields']['code']) ? $response['resp_data']['acc_resp_fields']['code'] : '';
  299. if (empty($url)) {
  300. util::fail('二维码生成失败..');
  301. }
  302. $order->wxPayUrl = $url;
  303. $order->save();
  304. }
  305. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  306. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  307. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  308. }
  309. if ($payWay == $aliPayWay) {
  310. if (isset($order->aliPayUrl) && !empty($order->aliPayUrl)) {
  311. $url = $order->aliPayUrl;
  312. } else {
  313. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  314. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  315. $params = [
  316. 'appid' => 'OP00002119',
  317. 'serial_no' => '018b08cfddbd',
  318. 'merchant_no' => $shop->lklSjNo,
  319. 'term_no' => $shop->lklScanTermNo,
  320. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  321. 'lklCertificatePath' => $lklCertificatePath,
  322. ];
  323. $laResource = new Lakala($params);
  324. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  325. $aliParams = [
  326. 'orderSn' => $orderSn,
  327. 'amount' => $totalFee,
  328. 'capitalType' => $currentCapitalType,
  329. 'notifyUrl' => $notifyUrl,
  330. 'subject' => $subject,
  331. ];
  332. $response = $laResource->driveAliPay($aliParams);
  333. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  334. util::fail('二维码生成失败');
  335. }
  336. $url = isset($response['resp_data']['acc_resp_fields']['code']) ? $response['resp_data']['acc_resp_fields']['code'] : '';
  337. if (empty($url)) {
  338. util::fail('二维码生成失败..');
  339. }
  340. $order->aliPayUrl = $url;
  341. $order->save();
  342. }
  343. $suffixUrl = qrCodeUtil::generateShortTimeGatheringQrCode($url, $this->sjId, $this->shopId, $payWay);
  344. $payUrl = Yii::$app->params['ghsImgHost'] . $suffixUrl;
  345. util::success(['url' => $payUrl, 'returnStatus' => 'SUCCESS']);
  346. }
  347. util::success(['returnStatus' => 'FAILURE'], '不支持的收款方式');
  348. }
  349. //微信和支付宝付款码支付复查 ssh 20211231
  350. public function actionCodePayCheck()
  351. {
  352. ini_set('date.timezone', 'Asia/Shanghai');
  353. header("Content-type: text/html; charset=utf-8");
  354. $get = Yii::$app->request->get();
  355. $id = isset($get['id']) ? $get['id'] : 0;
  356. $order = OrderClass::getById($id, true);
  357. if (empty($order)) {
  358. util::success(['returnStatus' => 'SUCCESS']);
  359. }
  360. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  361. util::complete('订单已付款');
  362. }
  363. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  364. util::complete('订单不是待付款状态');
  365. }
  366. OrderClass::valid($order, $this->shopId);
  367. $cgId = $order->purchaseId ?? 0;
  368. $cg = PurchaseClass::getById($cgId, true);
  369. $orderSn = $cg->orderSn ?? '';
  370. $totalFee = $cg->actPrice ?? 0;
  371. if (empty($orderSn)) {
  372. util::complete('没有找到采购单');
  373. }
  374. $connection = Yii::$app->db;
  375. $transaction = $connection->beginTransaction();
  376. try {
  377. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  378. $shop = $this->shop;
  379. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  380. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  381. $params = [
  382. 'appid' => 'OP00002119',
  383. 'serial_no' => '018b08cfddbd',
  384. 'merchant_no' => $shop->lklSjNo,
  385. 'term_no' => $shop->lklScanTermNo,
  386. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  387. 'lklCertificatePath' => $lklCertificatePath,
  388. ];
  389. $laResource = new Lakala($params);
  390. $scanParams = ['orderSn' => $orderSn,];
  391. $response = $laResource->query($scanParams);
  392. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  393. if (isset($response['resp_data']['trade_state']) && $response['resp_data']['trade_state'] == 'SUCCESS') {
  394. //支付成功
  395. $transactionId = $response['resp_data']['trade_no'] ?? '';
  396. $openId = '';
  397. $aliUserId = '';
  398. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  399. $cg->codePay = 1;
  400. $cg->payOpenId = $openId;
  401. $cg->aliUserId = $aliUserId;
  402. //自取订单
  403. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  404. $cg->save();
  405. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  406. $order->codePay = 1;
  407. //自取订单
  408. $order->sendType = OrderClass::SEND_TYPE_NO;
  409. $order->save();
  410. $payWayType = dict::getDict('payWay', 'wxPay');
  411. $account_type = $response['resp_data']['account_type'] ?? '';
  412. if ($account_type == 'WECHAT') {
  413. $payWayType = dict::getDict('payWay', 'wxPay');
  414. }
  415. if ($account_type == 'ALIPAY') {
  416. $payWayType = dict::getDict('payWay', 'alipay');
  417. }
  418. $order->payWay = $payWayType;
  419. $order->save();
  420. $cg->payWay = $payWayType;
  421. $cg->save();
  422. $attach = '';
  423. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  424. $transaction->commit();
  425. $saleId = $order->id ?? 0;
  426. $order = OrderClass::getById($saleId, true);
  427. if (!empty($order)) {
  428. //订单生成时唤起在线打印
  429. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  430. if ($order->status != 1 && $order->status != 5) {
  431. OrderClass::onlinePrint($order);
  432. $order->printNum += 1;
  433. $order->save();
  434. }
  435. }
  436. ShopExtClass::ghsGatheringReport($order);
  437. }
  438. util::success(['returnStatus' => 'SUCCESS']);
  439. } else {
  440. util::complete('未知状态..');
  441. }
  442. } else {
  443. util::complete('未知状态....');
  444. }
  445. } catch (\Exception $e) {
  446. $transaction->rollBack();
  447. noticeUtil::push('查询订单状态失败:' . $e->getMessage(), '15280215347');
  448. util::fail('支付失败');
  449. }
  450. }
  451. //微信和支付宝付款码支付 ssh 2021.4.11
  452. public function actionCodePay()
  453. {
  454. ini_set('date.timezone', 'Asia/Shanghai');
  455. header("Content-type: text/html; charset=utf-8");
  456. $get = Yii::$app->request->get();
  457. $id = isset($get['id']) ? $get['id'] : 0;
  458. $order = OrderClass::getById($id, true);
  459. if (isset($order->status) == false || $order->status != 1) {
  460. util::fail('不是待付款订单');
  461. }
  462. OrderClass::valid($order, $this->shopId);
  463. $cgId = $order->purchaseId ?? 0;
  464. $cg = PurchaseClass::getById($cgId, true);
  465. $orderSn = $cg->orderSn ?? '';
  466. if (empty($orderSn)) {
  467. util::fail('没有找到采购单');
  468. }
  469. //付款码
  470. $authCode = (string)$get['authCode'] ?? '';
  471. if (empty($authCode)) {
  472. util::fail('支付失败');
  473. }
  474. $deadline = $cg->deadline;
  475. $current = date("Y-m-d H:i:s");
  476. if ((strtotime($deadline) + 20) < strtotime($current)) {
  477. util::fail('订单已经失效');
  478. }
  479. $totalFee = $cg->actPrice ?? 0;
  480. $connection = Yii::$app->db;
  481. $transaction = $connection->beginTransaction();
  482. try {
  483. $subject = '购买花材';
  484. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  485. $shop = $this->shop;
  486. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  487. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  488. $params = [
  489. 'appid' => 'OP00002119',
  490. 'serial_no' => '018b08cfddbd',
  491. 'merchant_no' => $shop->lklSjNo,
  492. 'term_no' => $shop->lklScanTermNo,
  493. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  494. 'lklCertificatePath' => $lklCertificatePath,
  495. ];
  496. $laResource = new Lakala($params);
  497. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  498. $scanParams = [
  499. 'orderSn' => $orderSn,
  500. 'amount' => $totalFee,
  501. 'capitalType' => $capitalType,
  502. 'notifyUrl' => $notifyUrl,
  503. 'subject' => $subject,
  504. 'authCode' => $authCode,
  505. ];
  506. $response = $laResource->scanPay($scanParams);
  507. if (isset($response['code']) && $response['code'] == 'BBS00000') {
  508. $account_type = $response['resp_data']['account_type'] ?? '';
  509. $payWayType = dict::getDict('payWay', 'wxPay');
  510. if ($account_type == 'WECHAT') {
  511. $payWayType = dict::getDict('payWay', 'wxPay');
  512. }
  513. if ($account_type == 'ALIPAY') {
  514. $payWayType = dict::getDict('payWay', 'alipay');
  515. }
  516. $order->payWay = $payWayType;
  517. $order->save();
  518. $cg->payWay = $payWayType;
  519. $cg->save();
  520. $transactionId = $response['resp_data']['trade_no'] ?? '';
  521. $openId = '';
  522. $aliUserId = '';
  523. $cg->onlinePay = dict::getDict('onlinePay', 'yes');
  524. $cg->codePay = 1;
  525. $cg->payOpenId = $openId;
  526. $cg->aliUserId = $aliUserId;
  527. //自取订单
  528. $cg->getType = PurchaseClass::GET_TYPE_SELF_GET;
  529. $cg->save();
  530. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  531. $order->codePay = 1;
  532. //自取订单
  533. $order->sendType = OrderClass::SEND_TYPE_NO;
  534. $order->save();
  535. $attach = '';
  536. payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
  537. $transaction->commit();
  538. $saleId = $order->id ?? 0;
  539. $order = OrderClass::getById($saleId, true);
  540. if (!empty($order)) {
  541. //订单生成时唤起在线打印
  542. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  543. if ($order->status != 1 && $order->status != 5) {
  544. OrderClass::onlinePrint($order);
  545. $order->printNum += 1;
  546. $order->save();
  547. }
  548. }
  549. ShopExtClass::ghsGatheringReport($order);
  550. }
  551. util::success(['returnStatus' => 'SUCCESS']);
  552. } else {
  553. $msg = $response['msg'] ?? '';
  554. util::success(['returnStatus' => 'FAILURE'], $msg);
  555. }
  556. } catch (\Exception $e) {
  557. $transaction->rollBack();
  558. Yii::error("支付失败原因:" . $e->getMessage());
  559. util::fail('支付失败');
  560. }
  561. }
  562. //订单列表 ssh 2021.1.20
  563. public function actionList()
  564. {
  565. $get = Yii::$app->request->get();
  566. $where['mainId'] = $this->mainId;
  567. $status = $get['status'] ?? 0;
  568. if (!empty($status)) {
  569. $where['status'] = $status;
  570. }
  571. $book = $get['book'] ?? -1;
  572. if ($book > -1) {
  573. $where['book'] = $book;
  574. }
  575. $searchTime = $get['searchTime'] ?? '';
  576. if (!empty($searchTime)) {
  577. $startTime = $get['startTime'] ?? '';
  578. $endTime = $get['endTime'] ?? '';
  579. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  580. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  581. }
  582. $name = $get['name'] ?? '';
  583. if (!empty($name)) {
  584. $searchType = $get['searchType'] ?? 0;
  585. if (empty($searchType)) {
  586. //订单编号
  587. if (strpos($name, 'XSD') === 0) {
  588. $where['orderSn'] = $name;
  589. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  590. $where['customNamePy'] = ['like', $name];
  591. } else {
  592. $where['customName'] = ['like', $name];
  593. }
  594. } elseif ($searchType == 1) {
  595. $where['sendNum'] = $name;
  596. } else {
  597. }
  598. }
  599. if (isset($get['customId']) && !empty($get['customId'])) {
  600. $where['customId'] = $get['customId'];
  601. }
  602. if (isset($get['ids']) && !empty($get['ids'])) {
  603. $stringIds = $get['ids'];
  604. $newIds = explode(',', $stringIds);
  605. $where['id'] = ['in', $newIds];
  606. }
  607. $respond = OrderClass::getOrderList($where);
  608. $respond['shop'] = $this->shop;
  609. $respond['shopExt'] = $this->shopExt;
  610. util::success($respond);
  611. }
  612. // 新订单列表详情 -- 可能与 actionList 很相似,但为不影响 actionList,重新创建个方法 shizhongqi 2022.06.23
  613. public function actionNewOrderList()
  614. {
  615. $get = Yii::$app->request->get();
  616. $where['shopId'] = $this->shopId;
  617. /*$status = $get['status'] ?? 0;
  618. if (!empty($status)) {
  619. $where['status'] = $status;
  620. }*/
  621. // 按类别生成查询条件:1.今日实收 2.今日欠款 3.总欠款(所有历史欠款)
  622. if (isset($get['type'])) {
  623. $type = $get['type'];
  624. if ($type == 1) { // 今日实收
  625. $get['searchTime'] = 'today';
  626. $where['debt'] = 0;
  627. $where['debtPrice'] = '0.00';
  628. $where['status'] = ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]];
  629. } elseif ($type == 2) { // 今日欠款
  630. $get['searchTime'] = 'today';
  631. $where['debt'] = 1;
  632. } elseif ($type == 3) { // 总欠款
  633. $where['debt'] = 1;
  634. } else {
  635. util::fail('查询的订单类别不存在');
  636. }
  637. }
  638. $searchTime = $get['searchTime'] ?? '';
  639. if (!empty($searchTime)) {
  640. $startTime = '';
  641. $endTime = '';
  642. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  643. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  644. }
  645. $name = $get['name'] ?? '';
  646. if (!empty($name)) {
  647. $searchType = $get['searchType'] ?? 0;
  648. if (empty($searchType)) {
  649. //订单编号
  650. if (strpos($name, 'XSD') === 0) {
  651. $where['orderSn'] = $name;
  652. } elseif (preg_match("/^[a-zA-Z\s]+$/", $name)) {
  653. $where['customNamePy'] = ['like', $name];
  654. } else {
  655. $where['customName'] = ['like', $name];
  656. }
  657. } elseif ($searchType == 1) {
  658. $where['sendNum'] = $name;
  659. } else {
  660. util::fail('搜索的订单类别不存在');
  661. }
  662. }
  663. $respond = OrderClass::getOrderList($where);
  664. $respond['shop'] = $this->shop;
  665. $respond['shopExt'] = $this->shopExt;
  666. $respond['type'] = $type; //把查询类别返回
  667. util::success($respond);
  668. }
  669. //商城下单操作 ssh 2021.1.14
  670. public function actionCreateOrder()
  671. {
  672. $post = Yii::$app->request->post();
  673. $shopId = $this->shopId;
  674. $customId = $post['customId'] ?? 0;
  675. $post['customId'] = $customId;
  676. //开单必须选客户
  677. if (empty($customId)) {
  678. util::fail('请选择客户');
  679. }
  680. $custom = CustomClass::getCustom($customId);
  681. if (empty($custom)) {
  682. util::fail('请选客户哦');
  683. }
  684. CustomClass::valid($custom, $this->shopId);
  685. if (isset($custom['mainId']) && !empty($custom['mainId']) && $custom['mainId'] == $custom['ownMainId']) {
  686. util::fail('不能给自己的零售店开单');
  687. }
  688. $post['ghsId'] = $custom['ghsId'] ?? 0;
  689. $post['customMobile'] = $custom['mobile'] ?? '';
  690. $customName = $custom['name'] ?? '';
  691. $post['customName'] = $customName;
  692. $post['customNamePy'] = $custom['py'] ?? '';
  693. $post['customAvatar'] = $custom['shortSmallAvatar'] ?? '';
  694. $post['province'] = $custom['province'] ?? '';
  695. $post['city'] = $custom['city'] ?? '';
  696. $post['dist'] = $custom['dist'] ?? '';
  697. $post['address'] = $custom['address'] ?? '';
  698. $post['floor'] = $custom['floor'] ?? '';
  699. $post['fullAddress'] = $custom['fullAddress'] ?? '';
  700. $post['showAddress'] = $custom['showAddress'] ?? '';
  701. $post['long'] = $custom['long'] ?? '';
  702. $post['lat'] = $custom['lat'] ?? '';
  703. $shopAdmin = $this->shopAdmin;
  704. $shopAdminId = $this->shopAdminId;
  705. $shopAdminName = $shopAdmin->name ?? '';
  706. if (isset($post['getStaffId']) == true && !empty($post['getStaffId'])) {
  707. $getStaffName = $post['getStaffName'] ?? '';
  708. $shopAdminId = $post['getStaffId'];
  709. $shopAdminName = $getStaffName;
  710. }
  711. //开单人和收款人必定是同个人
  712. $post['shopAdminId'] = $shopAdminId;
  713. $post['shopAdminName'] = $shopAdminName;
  714. $post['getStaffId'] = $shopAdminId;
  715. $post['getStaffName'] = $shopAdminName;
  716. $post['sjId'] = $this->sjId;
  717. $post['shopId'] = $shopId;
  718. $post['mainId'] = $this->mainId;
  719. $post['fromType'] = 1;
  720. $post['expressId'] = $post['expressId'] ?? 0;
  721. $book = $post['book'] ?? 0;
  722. $post['book'] = $book;
  723. $sendType = $post['sendType'] ?? 0;
  724. $transType = $post['transType'] ?? 0;
  725. $shop = $this->shop;
  726. if (isset($shop->pfLevel) && $shop->pfLevel == 1) {
  727. //昆明发往全国
  728. $sendCost = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  729. $post['sendCost'] = $sendCost;
  730. $packCost = isset($post['packCost']) && $post['packCost'] > 0 ? $post['packCost'] : 0;
  731. $post['packCost'] = $packCost;
  732. $post['transType'] = $transType;
  733. $post['localOrder'] = 1;
  734. } else {
  735. //同步配送
  736. $sendCost = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  737. if ($sendType != dict::getDict('sendType', 'thirdSend') && $sendCost > 0) {
  738. util::fail('填了运费,请选择代叫跑腿');
  739. }
  740. $post['sendCost'] = $sendCost;
  741. $post['packCost'] = isset($post['packCost']) && $post['packCost'] > 0 ? $post['packCost'] : 0;
  742. }
  743. $hasPay = $post['hasPay'] ?? dict::getDict('hasPay', 'unPay');
  744. $post['debt'] = OrderClass::DEBT_NO;
  745. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  746. $post['debt'] = OrderClass::DEBT_YES;
  747. }
  748. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  749. if ($hasPay == dict::getDict('hasPay', 'payed')) {
  750. //util::fail('没有权限');
  751. }
  752. }
  753. //多颜色数据整理
  754. $colorItem = $post['xj'] ?? [];
  755. $newXj = [];
  756. if (!empty($colorItem)) {
  757. $colorData = json_decode($colorItem, true);
  758. if (!empty($colorData) && is_array($colorData)) {
  759. foreach ($colorData as $colorInfo) {
  760. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  761. $colorList = $colorInfo['list'] ?? [];
  762. if (!empty($colorList)) {
  763. foreach ($colorList as $colorItemKey => $colorItem) {
  764. $newXj[$ptItemId][] = $colorItem;
  765. }
  766. }
  767. }
  768. }
  769. }
  770. $post['xj'] = $newXj;
  771. $productJson = $post['product'] ?? '';
  772. if (empty($productJson)) {
  773. util::fail('请选择花材');
  774. }
  775. $productList = json_decode($productJson, true);
  776. if (empty($productList)) {
  777. util::fail('请选择花材');
  778. }
  779. $dealPrice = $post['dealPrice'] ?? 0;
  780. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  781. $itemInfo = ProductClass::getByIds($ids, null, 'id');
  782. if (empty($itemInfo)) {
  783. util::fail('请选择花材哦');
  784. }
  785. //判断花材有效性
  786. foreach ($itemInfo as $itemData) {
  787. if (isset($itemData['mainId']) == false || $itemData['mainId'] != $this->mainId) {
  788. util::fail('只能选择同一家的商品哈');
  789. }
  790. }
  791. //开单提交订单时,别人修改价格,暂成提交价格不是真实卖价问题解决!!
  792. if ($dealPrice == 0) {
  793. $diff = [];
  794. $priceMap = CustomClass::$levelPriceKeyMap;
  795. $addPriceMap = CustomClass::$levelAddPriceKeyMap;
  796. $level = $custom['level'] ?? 1;
  797. foreach ($productList as $currentKey => $currentProduct) {
  798. $currentPrice = $currentProduct['price'] ?? 0;
  799. $currentId = $currentProduct['productId'] ?? 0;
  800. $systemInfo = $itemInfo[$currentId] ?? [];
  801. $name = $systemInfo['name'] ?? '';
  802. $systemPrice = ProductClass::getFinalPrice($systemInfo, $level, $priceMap, $addPriceMap);
  803. if (floatval($currentPrice) != floatval($systemPrice)) {
  804. $diff[] = [
  805. 'name' => $name,
  806. 'price' => $systemPrice,
  807. 'kdPrice' => $currentPrice,
  808. ];
  809. }
  810. }
  811. if (!empty($diff)) {
  812. util::success(['diff' => $diff, 'respondType' => 'priceError']);
  813. }
  814. }
  815. if ($book == 1) {
  816. foreach ($productList as $currentKey => $currentProduct) {
  817. //帮开预订单没有价格
  818. $productList[$currentKey]['price'] = 0.06;
  819. }
  820. $post['hasPay'] = 2;
  821. $post['payWay'] = 0;
  822. } else {
  823. foreach ($productList as $currentKey => $currentProduct) {
  824. //预订单用了0.06这个价格
  825. $nowPrice = $currentProduct['price'];
  826. if ($nowPrice == 0.06) {
  827. util::fail('花材不允许使用0.06元,请改其它价');
  828. }
  829. }
  830. }
  831. //4秒内不允许重复开单
  832. $staffId = $this->shopAdminId ?? 0;
  833. $cacheKey = 'ghs_create_order_' . $staffId . '_' . $customId;
  834. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  835. if (!empty($has)) {
  836. util::fail('请稍等');
  837. }
  838. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  839. $connection = Yii::$app->db;//事务处理
  840. $transaction = $connection->beginTransaction();
  841. try {
  842. $post['product'] = $productList;
  843. //商家开单到时间会自动标记为欠款,所以360天内不过期,以保证足够时间标记欠款,具体参数查看dict local_gys_kd_auto_set_debt_time
  844. $time = time();
  845. $hasTime = dict::getDict('order_pay_has_time');
  846. $totalTime = bcadd($time, $hasTime);
  847. $post['deadline'] = date("Y-m-d H:i:s", $totalTime);
  848. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  849. $transaction->commit();
  850. $saleId = $return->id ?? 0;
  851. $order = OrderClass::getById($saleId, true);
  852. if (!empty($order)) {
  853. $shopId = $order->shopId ?? 0;
  854. $shop = ShopClass::getById($shopId, true);
  855. if (isset($order->payStatus) && $order->payStatus == 1) {
  856. if (!empty($shop)) {
  857. //通知员工
  858. $noticeText = json_encode(['orderId' => $saleId]);
  859. $noticeKey = "hdCgNoticeGhs";
  860. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  861. //通知花店
  862. // $noticeText = json_encode(['id' => $order->purchaseId]);
  863. // $noticeKey = "ghsKdNoticeHd";
  864. // Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  865. }
  866. }
  867. //订单生成时唤起在线打印
  868. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  869. if ($order->status != 1 && $order->status != 5) {
  870. OrderClass::onlinePrint($order);
  871. $order->printNum += 1;
  872. $order->save();
  873. }
  874. }
  875. //前台提醒出示付款码
  876. if (isset($order->status) && $order->status == 1) {
  877. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  878. ShopExtClass::ghsPleasePayReport($order);
  879. }
  880. }
  881. }
  882. util::success($return);
  883. } catch (\Exception $e) {
  884. $transaction->rollBack();
  885. Yii::error("下单失败原因:" . $e->getMessage());
  886. noticeUtil::push("零售下单失败原因:" . $e->getMessage(), '15280215347');
  887. util::fail('下单失败');
  888. }
  889. }
  890. //修改订单 ssh 20210810
  891. public function actionUpdateOrder()
  892. {
  893. $post = Yii::$app->request->post();
  894. util::fail('订单不可修改');
  895. //PC端开单
  896. if (isset($post['clearType'])) {
  897. $clearType = $post['clearType'];
  898. if ($clearType == OrderClass::CLEAR_DEBT) {
  899. $post['debt'] = OrderClass::DEBT_YES;
  900. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  901. } else {
  902. $post['debt'] = OrderClass::DEBT_NO;
  903. }
  904. }
  905. //员工不能开已收款订单
  906. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  907. if ($debt == OrderClass::DEBT_NO) {
  908. $shopAdmin = $this->shopAdmin;
  909. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  910. util::fail('不能开已收款订单');
  911. }
  912. }
  913. //PC端开单
  914. if (isset($post['getType'])) {
  915. util::fail('暂不支持PC端修改订单');
  916. $getType = $post['getType'];
  917. //自取
  918. if ($getType == 1) {
  919. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  920. } else {
  921. //选择配送时,在发货时让商家自己再确认一下用什么方式
  922. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  923. }
  924. }
  925. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  926. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  927. $post['debt'] = OrderClass::DEBT_NO;
  928. }
  929. $id = $post['id'] ?? 0;
  930. $order = OrderClass::getLockById($id);
  931. if (empty($order)) {
  932. util::fail('没有找到订单');
  933. }
  934. OrderClass::valid($order, $this->shopId);
  935. unset($post['id']);
  936. $shopAdmin = $this->shopAdmin;
  937. $post['shopAdminId'] = $this->shopAdminId;
  938. $post['shopAdminName'] = $shopAdmin->name ?? '';
  939. $post['fromType'] = 1;
  940. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  941. $productJson = $post['product'] ?? '';
  942. if (empty($productJson)) {
  943. util::fail('请选择花材');
  944. }
  945. $productList = json_decode($productJson, true);
  946. if (empty($productList)) {
  947. util::fail('请选择花材');
  948. }
  949. $connection = Yii::$app->db;//事务处理
  950. $transaction = $connection->beginTransaction();
  951. try {
  952. //判断花材有效性
  953. ProductClass::valid($productList, $this->mainId);
  954. $post['product'] = $productList;
  955. //商家开单订单有效期,到期记欠款
  956. $validTime = dict::getDict('order_pay_has_time');
  957. $current = time() + $validTime;
  958. $post['deadline'] = date("Y-m-d H:i:s", $current);
  959. $upData = [
  960. 'payWay' => $post['payWay'],
  961. 'sendType' => $post['sendType'],
  962. 'debt' => $post['debt'],
  963. 'needPrint' => $post['needPrint'],
  964. 'modifyPrice' => $post['modifyPrice'],
  965. 'shopAdminId' => $post['shopAdminId'],
  966. 'shopAdminName' => $post['shopAdminName'],
  967. 'fromType' => 1,
  968. 'sendCost' => $post['sendCost'],
  969. 'product' => $post['product'],
  970. 'deadline' => $post['deadline'],
  971. 'debt' => $post['debt'],
  972. ];
  973. $return = OrderService::updateOrder($order, $upData);
  974. $transaction->commit();
  975. util::success($return);
  976. } catch (\Exception $e) {
  977. $transaction->rollBack();
  978. Yii::error("下单失败原因:" . $e->getMessage());
  979. util::fail('下单失败');
  980. }
  981. }
  982. //取消订单 shizhongqi 20220619
  983. public function actionCancelOrder()
  984. {
  985. $post = Yii::$app->request->post();
  986. $id = $post['orderId'] ?? 0;
  987. if (empty($id)) {
  988. util::fail('请选择订单');
  989. }
  990. $order = OrderClass::getById($id, true);
  991. if (empty($order)) {
  992. util::fail('请选择订单...');
  993. }
  994. OrderClass::valid($order, $this->shopId);
  995. if (isset($order->status) == false || $order->status != 1) {
  996. util::fail('不是待付款状态,无法取消');
  997. }
  998. $purchaseId = $order->purchaseId ?? 0;
  999. $purchase = PurchaseClass::getById($purchaseId);
  1000. if (empty($purchase)) {
  1001. util::fail('没有找到采购单');
  1002. }
  1003. $deadLineTime = strtotime($purchase['deadline']);
  1004. $diff = $deadLineTime - time();
  1005. if ($diff <= 60) {
  1006. util::fail('1分钟后订单将自动取消');
  1007. }
  1008. $connection = Yii::$app->db;//事务处理
  1009. $transaction = $connection->beginTransaction();
  1010. $orderSn = $purchase['orderSn'];
  1011. try {
  1012. $current = PurchaseClass::getById($purchaseId, true);
  1013. PurchaseService::expire($current);
  1014. $transaction->commit();
  1015. //noticeUtil::push("采购单过期未付款,库存已回滚,单号:{$orderSn}");
  1016. } catch (\Exception $e) {
  1017. $transaction->rollBack();
  1018. $msg = $e->getMessage();
  1019. noticeUtil::push("采购单过期处理报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  1020. }
  1021. util::complete();
  1022. }
  1023. //延期支付 ssh 2021.1.19
  1024. public function actionDebt()
  1025. {
  1026. $id = Yii::$app->request->get('id');
  1027. $order = OrderClass::getById($id, true);
  1028. OrderClass::valid($order, $this->shopId);
  1029. $payWay = dict::getDict('payWay', 'debtPay');
  1030. $cgId = $order->purchaseId ?? 0;
  1031. $cg = PurchaseClass::getById($cgId, true);
  1032. if (empty($cg)) {
  1033. util::fail('没有找到采购单');
  1034. }
  1035. if ($order->book == 1) {
  1036. util::fail('预订单不能记欠款');
  1037. }
  1038. PurchaseService::payAfter($cg, $payWay);
  1039. OrderService::payAfter($order, $payWay);
  1040. $order = OrderClass::getById($id, true);
  1041. if (!empty($order)) {
  1042. $shopId = $order->shopId ?? 0;
  1043. $shop = ShopClass::getById($shopId, true);
  1044. if (!empty($shop)) {
  1045. //花店采购供货商端微信收到通知
  1046. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  1047. $noticeText = json_encode(['orderId' => $id]);
  1048. $noticeKey = "hdCgNoticeGhs";
  1049. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  1050. }
  1051. //订单生成时唤起在线打印
  1052. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1053. if ($order->status != 1 && $order->status != 5) {
  1054. OrderClass::onlinePrint($order);
  1055. $order->printNum += 1;
  1056. $order->save();
  1057. }
  1058. }
  1059. if ($order->status != 1 && $order->status != 5) {
  1060. //花店采购供货商端APP收到通知
  1061. //NoticeClass::ghsNewOrderNotice($order);
  1062. }
  1063. }
  1064. util::complete();
  1065. }
  1066. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1067. public function actionWxPay()
  1068. {
  1069. ini_set('date.timezone', 'Asia/Shanghai');
  1070. $post = Yii::$app->request->post();
  1071. $couponId = $post['couponId'] ?? 0;
  1072. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1073. $order = OrderClass::getByOrderSn($orderSn);
  1074. if (empty($order)) {
  1075. util::fail('订单号无效');
  1076. }
  1077. $id = $order['id'];
  1078. //支付前验证订单有效性
  1079. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  1080. util::fail('无法操作');
  1081. }
  1082. $name = $order['orderName'] ?? '购买商品';
  1083. $totalFee = $order['actPrice'];
  1084. //强制使用小程序的miniOpenId
  1085. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  1086. if (empty($openId)) {
  1087. util::fail('没有找到openId');
  1088. }
  1089. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  1090. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  1091. $wxPayType = 0;
  1092. if (httpUtil::isMiniProgram()) {
  1093. $wxPayType = 1;
  1094. }
  1095. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  1096. //订单30分钟后过期
  1097. $now = time();
  1098. $expireTime = $now + 1800;
  1099. $wx = Yii::getAlias("@vendor/weixin");
  1100. require_once($wx . '/lib/WxPay.Api.php');
  1101. require_once($wx . '/example/WxPay.JsApiPay.php');
  1102. $input = new \WxPayUnifiedOrder();
  1103. $input->SetBody($name);
  1104. $input->SetOut_trade_no($orderSn);
  1105. $input->SetTotal_fee($totalFee * 100);
  1106. $input->SetTime_start(date("YmdHis", $now));
  1107. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  1108. $input->SetTime_expire(date("YmdHis", $expireTime));
  1109. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  1110. $input->SetTrade_type("JSAPI");
  1111. //花卉宝代为申请的微信支付
  1112. //$sjExtend = $this->sjExtend->attributes;
  1113. $sjExtend = WxOpenClass::getGhsWxInfo();
  1114. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  1115. $input->SetSub_openid($openId);
  1116. } else {
  1117. $input->SetOpenid($openId);
  1118. }
  1119. //强制使用小程序的miniAppId
  1120. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  1121. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  1122. $tools = new \JsApiPay();
  1123. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  1124. $newParams = json_decode($jsApiParameters, true);
  1125. //微信不能修改价格
  1126. $current = date("Y-m-d H:i:s");
  1127. $updateData = ['deadline' => $current, 'modPrice' => 0];
  1128. OrderClass::updateById($id, $updateData);
  1129. util::success($newParams);
  1130. }
  1131. //获取订单详情 ssh 2021.1.21
  1132. public function actionDetail()
  1133. {
  1134. $get = Yii::$app->request->get();
  1135. $id = $get['id'] ?? 0;
  1136. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1137. OrderClass::valid($info, $this->shopId);
  1138. //是否有云打印判断
  1139. $hasCloudPrint = 0;
  1140. $shopExt = $this->shopExt;
  1141. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1142. $hasCloudPrint = 1;
  1143. }
  1144. $info['hasCloudPrint'] = $hasCloudPrint;
  1145. $purchaseId = $info['purchaseId'] ?? 0;
  1146. $cg = PurchaseClass::getById($purchaseId);
  1147. $info['cgInfo'] = $cg;
  1148. //店长信息
  1149. $mainId = $cg['mainId'] ?? 0;
  1150. $superInfo = ShopAdminClass::getManager($mainId);
  1151. $info['customSuper'] = $superInfo;
  1152. util::success($info);
  1153. }
  1154. //获取订单详情,不需要登录可以访问,后面要删除此方法 ssh 20220819
  1155. public function actionInfo()
  1156. {
  1157. $get = Yii::$app->request->get();
  1158. $id = $get['id'] ?? 0;
  1159. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1160. if (empty($info)) {
  1161. util::fail('没有找到订单');
  1162. }
  1163. //是否有云打印判断
  1164. $hasCloudPrint = 0;
  1165. $shopExt = $this->shopExt;
  1166. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1167. $hasCloudPrint = 1;
  1168. }
  1169. $info['hasCloudPrint'] = $hasCloudPrint;
  1170. $purchaseId = $info['purchaseId'] ?? 0;
  1171. $cg = PurchaseClass::getById($purchaseId);
  1172. $info['cgInfo'] = $cg;
  1173. //店长信息
  1174. $mainId = $cg['mainId'] ?? 0;
  1175. $superInfo = ShopAdminClass::getManager($mainId);
  1176. $info['customSuper'] = $superInfo;
  1177. util::success($info);
  1178. }
  1179. //自取免发货流程处理 ssh 2021.1.22
  1180. public function actionWithoutSend()
  1181. {
  1182. util::complete();
  1183. }
  1184. //本店送 ssh 2021.1.24
  1185. public function actionSelfSend()
  1186. {
  1187. $get = Yii::$app->request->get();
  1188. $id = isset($get['id']) ? $get['id'] : 0;
  1189. $info = OrderClass::getById($id, true);
  1190. OrderClass::valid($info->attributes, $this->shopId);
  1191. $connection = Yii::$app->db;
  1192. $transaction = $connection->beginTransaction();
  1193. try {
  1194. OrderClass::selfSend($info);
  1195. $transaction->commit();
  1196. } catch (\Exception $exception) {
  1197. $transaction->rollBack();
  1198. Yii::info("本店送操作报错:" . $exception->getMessage());
  1199. util::fail('操作失败');
  1200. }
  1201. util::complete();
  1202. }
  1203. //运费计算 ssh 2021.1.24
  1204. public function actionFreight()
  1205. {
  1206. //2021.3.28 接入达达
  1207. $get = Yii::$app->request->get();
  1208. $post = Yii::$app->request->post();
  1209. if (empty($get)) {
  1210. $get = $post;
  1211. }
  1212. $orderId = $get['id'] ?? 0; //订单ID
  1213. $customId = $get['customId'] ?? 0;
  1214. if (empty($orderId)) {
  1215. //自定义运费
  1216. //freight::getCost();
  1217. if (empty($customId)) {
  1218. util::fail("请选择客户");
  1219. }
  1220. $customInfo = CustomClass::getById($customId);
  1221. if (empty($customInfo)) {
  1222. util::fail('没有找到客户');
  1223. }
  1224. $shop = $this->shop;
  1225. //花材信息
  1226. $productJson = $get['product'] ?? '';
  1227. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1228. if (empty($productJson)) {
  1229. util::fail('请选择花材');
  1230. }
  1231. $productList = json_decode($productJson, true);
  1232. if (empty($productList)) {
  1233. util::fail('请选择花材');
  1234. }
  1235. $productList = ProductClass::mergeItemInfo($productList);
  1236. $weight = 0;
  1237. $price = 0;
  1238. $productData = ProductClass::getProductMapData($productList);
  1239. $bigNum = 0;
  1240. foreach ($productList as $key => $val) {
  1241. $productId = $val['productId'];
  1242. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1243. $bigNum += $val['bigNum'];
  1244. $weight = bcadd($w, $weight, 2);
  1245. $ratio = $productData[$productId]['ratio'] ?? 0;
  1246. //大小数量合并多少扎
  1247. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1248. //售卖价格
  1249. $itemPrice = $productData[$productId]['price'] ?? 0;
  1250. //合计价格
  1251. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1252. }
  1253. //没有经纬度的客户运费直接返回空 ssh 20210612
  1254. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  1255. util::success(['sedCost' => '']);
  1256. }
  1257. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  1258. util::success(['sedCost' => $cost]);
  1259. }
  1260. $province = $get['province'] ?? '';
  1261. $city = $get['city'] ?? '';
  1262. $address = $get['address'] ?? '';
  1263. $floor = $get['floor'] ?? '';
  1264. $customName = $get['customName'] ?? '';
  1265. $customMobile = $get['customMobile'] ?? '';
  1266. $fullAddress = $province . $city . $address . $floor;
  1267. $sendTime = $get['sendTime'] ?? '';
  1268. $lat = $get['lat'] ?? '';
  1269. $lng = $get['long'] ?? '';
  1270. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  1271. util::fail('请填写客户地址');
  1272. }
  1273. ini_set('date.timezone', 'Asia/Shanghai');
  1274. if (!empty($sendTime)) {
  1275. //配送时间不为空
  1276. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1277. $sendTimeInt = strtotime($sendTime);
  1278. $now = time();
  1279. $diff = $sendTimeInt - $now;
  1280. //因服务器写入时间,改为至少提前6分钟
  1281. if ($diff <= 360) {
  1282. util::fail('配送时间至少提前6分钟');
  1283. }
  1284. } else {
  1285. //默认立即发送
  1286. $sendTime = 0;
  1287. }
  1288. //更新订单表
  1289. $info = OrderService::getOrderInfo($orderId);
  1290. OrderClass::valid($info, $this->shopId);
  1291. if ($sendTime) {
  1292. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1293. } else {
  1294. $sendTime = '0000-00-00 00:00:00';
  1295. }
  1296. $expressAddInfo = [
  1297. 'customName' => $customName,
  1298. 'customMobile' => $customMobile,
  1299. 'province' => $province,
  1300. 'city' => $city,
  1301. 'address' => $address,
  1302. 'floor' => $floor,
  1303. 'lat' => $lat,
  1304. 'long' => $lng,
  1305. 'sendTime' => $sendTime,
  1306. ];
  1307. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  1308. //计算运费
  1309. $info['province'] = $province;
  1310. $info['city'] = $city;
  1311. $info['address'] = $address;
  1312. $info['floor'] = $floor;
  1313. $info['fullAddress'] = $fullAddress;
  1314. $info['lat'] = $lat;
  1315. $info['long'] = $lng;
  1316. $info['sendTime'] = $expressAddInfo['sendTime'];
  1317. $res = DadaExpressServices::queryDeliverFee($info);
  1318. util::success(['sedCost' => $res['fee']]);
  1319. }
  1320. //发快递和第三方配送 ssh 2021.1.24
  1321. public function actionThirdSend()
  1322. {
  1323. $get = Yii::$app->request->get();
  1324. $id = isset($get['id']) ? $get['id'] : 0;
  1325. $info = OrderService::getById($id, true);
  1326. OrderClass::valid($info->attributes, $this->shopId);
  1327. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  1328. util::fail('请填写客户地址');
  1329. }
  1330. $connection = Yii::$app->db;
  1331. $transaction = $connection->beginTransaction();
  1332. try {
  1333. $respond = OrderClass::thirdSend($info, $get);
  1334. $transaction->commit();
  1335. util::success($respond);
  1336. } catch (\Exception $exception) {
  1337. $transaction->rollBack();
  1338. Yii::info("发快递操作报错:" . $exception->getMessage());
  1339. util::fail('操作失败' . $exception->getMessage());
  1340. }
  1341. }
  1342. //确认送达 ssh 2021.1.24
  1343. public function actionReach()
  1344. {
  1345. $get = Yii::$app->request->get();
  1346. $id = isset($get['id']) ? $get['id'] : 0;
  1347. $info = OrderClass::getById($id, true);
  1348. OrderClass::valid($info->attributes, $this->shopId);
  1349. $connection = Yii::$app->db;
  1350. $transaction = $connection->beginTransaction();
  1351. try {
  1352. OrderService::reach($info);
  1353. $transaction->commit();
  1354. } catch (\Exception $exception) {
  1355. $transaction->rollBack();
  1356. Yii::info("送达操作报错:" . $exception->getMessage());
  1357. util::fail('操作失败');
  1358. }
  1359. util::complete();
  1360. }
  1361. //下单要用到的相关信息 ssh 2019.12.6
  1362. public function actionOrderRelate()
  1363. {
  1364. $regionTree = RegionService::tree();
  1365. $shop = $this->shop->attributes;
  1366. $freight = ['first' => 5, 'add' => 2];
  1367. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1368. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  1369. util::success($out);
  1370. }
  1371. //客户欠款的订单 ssh 2021.2.4
  1372. public function actionDebtList()
  1373. {
  1374. $get = Yii::$app->request->get();
  1375. $id = $get['id'] ?? 0;
  1376. $info = CustomClass::getCustom($id);
  1377. CustomClass::valid($info, $this->shopId);
  1378. $where = ['customId' => $id, 'debt' => 1];
  1379. $searchTime = $get['searchTime'] ?? '';
  1380. if (!empty($searchTime)) {
  1381. $startTime = $get['startTime'] ?? '';
  1382. $endTime = $get['endTime'] ?? '';
  1383. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1384. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1385. }
  1386. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  1387. $result = ['customInfo' => $info, 'list' => $list];
  1388. util::success($result);
  1389. }
  1390. //更新打印次数
  1391. public function actionAddPrintNum()
  1392. {
  1393. $id = Yii::$app->request->get('id', 0);
  1394. $order = OrderClass::getById($id, true);
  1395. OrderClass::valid($order, $this->shopId);
  1396. $order->printNum += 1;
  1397. $order->save();
  1398. util::complete();
  1399. }
  1400. //打印订单 ssh 20210714
  1401. public function actionCloudPrintOrder()
  1402. {
  1403. $get = Yii::$app->request->get();
  1404. $id = $get['id'] ?? 0;
  1405. $showPrice = $get['showPrice'] ?? 1;
  1406. $order = OrderClass::getById($id, true);
  1407. OrderClass::valid($order, $this->shopId);
  1408. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  1409. util::fail('订单还没有付款');
  1410. }
  1411. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  1412. util::fail('订单已取消');
  1413. }
  1414. $ext = $this->shopExt;
  1415. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  1416. util::success(['hasNoPrint' => 1]);
  1417. }
  1418. OrderClass::onlinePrint($order, $showPrice);
  1419. $order->printNum += 1;
  1420. $order->save();
  1421. util::complete();
  1422. }
  1423. //订单确认完成 ssh 20210809
  1424. public function actionConfirmFinish()
  1425. {
  1426. $payWay = Yii::$app->request->get('payWay', -1);
  1427. $id = Yii::$app->request->get('id', 0);
  1428. $clearType = Yii::$app->request->get('clearType', 0);
  1429. $connection = Yii::$app->db;
  1430. $transaction = $connection->beginTransaction();
  1431. try {
  1432. $order = OrderClass::getById($id, true);
  1433. OrderClass::valid($order, $this->shopId);
  1434. $purchaseId = $order->purchaseId ?? 0;
  1435. $purchase = PurchaseClass::getById($purchaseId, true);
  1436. if (empty($purchase)) {
  1437. util::fail('没有采购信息');
  1438. }
  1439. if ($clearType == 1) {
  1440. //欠款
  1441. $payWay = dict::getDict('payWay', 'debtPay');
  1442. } elseif ($clearType == 2) {
  1443. //已收款
  1444. if ($payWay == -1) {
  1445. util::fail('请选择收款方式');
  1446. }
  1447. $shopAdmin = $this->shopAdmin;
  1448. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1449. util::fail('超管才能发起已收款');
  1450. }
  1451. } else {
  1452. util::fail('请选择结算方式');
  1453. }
  1454. //不需要打印
  1455. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1456. $order->save();
  1457. //支付
  1458. PurchaseService::payAfter($purchase, $payWay);
  1459. OrderService::payAfter($order, $payWay);
  1460. //自己送
  1461. $info = OrderClass::getById($id, true);
  1462. OrderClass::selfSend($info);
  1463. //确认送达,并且不需要微信通知
  1464. Yii::$app->params['noNeedWxNotice'] = 1;
  1465. $info = OrderClass::getById($id, true);
  1466. OrderService::reach($info);
  1467. $transaction->commit();
  1468. util::complete('操作成功');
  1469. } catch (\Exception $exception) {
  1470. $transaction->rollBack();
  1471. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1472. util::fail('操作失败');
  1473. }
  1474. }
  1475. //取消订单 ssh 20220110
  1476. public function actionCancel()
  1477. {
  1478. $shopAdmin = $this->shopAdmin;
  1479. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1480. util::fail('超管才能取消订单');
  1481. }
  1482. $id = Yii::$app->request->get('id');
  1483. $order = OrderClass::getById($id, true);
  1484. if ($order->orderType == 2) {
  1485. util::fail('客户下单,不能主动取消');
  1486. }
  1487. OrderClass::valid($order, $this->shopId);
  1488. OrderService::expire($order, true);
  1489. util::complete('已取消');
  1490. }
  1491. //获取收款语音播报地址 ssh 20211231
  1492. public function actionGetPayVoice()
  1493. {
  1494. $id = Yii::$app->request->get('id');
  1495. $order = OrderClass::getById($id, true);
  1496. $payWay = $order->payWay ?? 0;
  1497. $money = floatval($order->actPrice);
  1498. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1499. $msg = $payment . '收款' . $money . '元';
  1500. $audio = baiduAip::transfer($msg, 4, 0);
  1501. //上传到oss
  1502. $file = time() . rand(10000, 999999) . '.mp3';
  1503. $filePath = 'tmp/arrival/money/' . $file;
  1504. oss::uploadTmpAudio($filePath, $audio);
  1505. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1506. util::success(['url' => $url]);
  1507. }
  1508. //修改备注
  1509. public function actionModifyRemark()
  1510. {
  1511. $post = Yii::$app->request->post();
  1512. $id = $post['id'] ?? 0;
  1513. $remark = $post['remark'] ?? '';
  1514. $order = OrderClass::getById($id, true);
  1515. if (empty($order)) {
  1516. util::fail('没有找到订单');
  1517. }
  1518. if ($order->mainId != $this->mainId) {
  1519. util::fail('不是你的订单');
  1520. }
  1521. $cgId = $order->purchaseId ?? 0;
  1522. $cg = PurchaseClass::getById($cgId, true);
  1523. if (empty($cg)) {
  1524. util::fail('采购单没有找到');
  1525. }
  1526. $order->remark = $remark;
  1527. $order->save();
  1528. $cg->remark = $remark;
  1529. $cg->save();
  1530. util::complete('修改成功');
  1531. }
  1532. }