OrderController.php 63 KB

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