OrderController.php 62 KB

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