OrderController.php 63 KB

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