OrderController.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. print_r($productList);
  813. die;
  814. }
  815. //4秒内不允许重复开单
  816. $staffId = $this->shopAdminId ?? 0;
  817. $cacheKey = 'ghs_create_order_' . $staffId . '_' . $customId;
  818. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  819. if (!empty($has)) {
  820. util::fail('请稍等');
  821. }
  822. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  823. $connection = Yii::$app->db;//事务处理
  824. $transaction = $connection->beginTransaction();
  825. try {
  826. $post['product'] = $productList;
  827. //商家开单到时间会自动标记为欠款,所以360天内不过期,以保证足够时间标记欠款,具体参数查看dict local_gys_kd_auto_set_debt_time
  828. $time = time();
  829. $hasTime = dict::getDict('order_pay_has_time');
  830. $totalTime = bcadd($time, $hasTime);
  831. $post['deadline'] = date("Y-m-d H:i:s", $totalTime);
  832. $return = OrderService::createFinishOrder($post, $custom, $hasPay);
  833. $transaction->commit();
  834. $saleId = $return->id ?? 0;
  835. $order = OrderClass::getById($saleId, true);
  836. if (!empty($order)) {
  837. $shopId = $order->shopId ?? 0;
  838. $shop = ShopClass::getById($shopId, true);
  839. if (isset($order->payStatus) && $order->payStatus == 1) {
  840. if (!empty($shop)) {
  841. //花店采购供货商端收到微信通知
  842. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  843. $noticeText = json_encode(['orderId' => $saleId]);
  844. $noticeKey = "hdCgNoticeGhs";
  845. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  846. }
  847. }
  848. //订单生成时唤起在线打印
  849. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  850. if ($order->status != 1 && $order->status != 5) {
  851. OrderClass::onlinePrint($order);
  852. $order->printNum += 1;
  853. $order->save();
  854. }
  855. }
  856. //前台提醒出示付款码
  857. if (isset($order->status) && $order->status == 1) {
  858. if (isset($post['isCashier']) && $post['isCashier'] == 1) {
  859. ShopExtClass::ghsPleasePayReport($order);
  860. }
  861. }
  862. }
  863. util::success($return);
  864. } catch (\Exception $e) {
  865. $transaction->rollBack();
  866. Yii::error("下单失败原因:" . $e->getMessage());
  867. noticeUtil::push("零售下单失败原因:" . $e->getMessage(), '15280215347');
  868. util::fail('下单失败');
  869. }
  870. }
  871. //修改订单 ssh 20210810
  872. public function actionUpdateOrder()
  873. {
  874. $post = Yii::$app->request->post();
  875. util::fail('订单不可修改');
  876. //PC端开单
  877. if (isset($post['clearType'])) {
  878. $clearType = $post['clearType'];
  879. if ($clearType == OrderClass::CLEAR_DEBT) {
  880. $post['debt'] = OrderClass::DEBT_YES;
  881. $post['payWay'] = dict::getDict('payWay', 'debtPay');
  882. } else {
  883. $post['debt'] = OrderClass::DEBT_NO;
  884. }
  885. }
  886. //员工不能开已收款订单
  887. $debt = $post['debt'] ?? OrderClass::DEBT_YES;
  888. if ($debt == OrderClass::DEBT_NO) {
  889. $shopAdmin = $this->shopAdmin;
  890. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  891. util::fail('不能开已收款订单');
  892. }
  893. }
  894. //PC端开单
  895. if (isset($post['getType'])) {
  896. util::fail('暂不支持PC端修改订单');
  897. $getType = $post['getType'];
  898. //自取
  899. if ($getType == 1) {
  900. $post['sendType'] = OrderClass::SEND_TYPE_NO;
  901. } else {
  902. //选择配送时,在发货时让商家自己再确认一下用什么方式
  903. $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
  904. }
  905. }
  906. //商家开非自取订单,默认订单是待收款的,debt字段为非欠款
  907. if (isset($post['sendType']) && $post['sendType'] != OrderClass::SEND_TYPE_NO) {
  908. $post['debt'] = OrderClass::DEBT_NO;
  909. }
  910. $id = $post['id'] ?? 0;
  911. $order = OrderClass::getLockById($id);
  912. if (empty($order)) {
  913. util::fail('没有找到订单');
  914. }
  915. OrderClass::valid($order, $this->shopId);
  916. unset($post['id']);
  917. $shopAdmin = $this->shopAdmin;
  918. $post['shopAdminId'] = $this->shopAdminId;
  919. $post['shopAdminName'] = $shopAdmin->name ?? '';
  920. $post['fromType'] = 1;
  921. $post['sendCost'] = $post['sendCost'] ?? '0.00';
  922. $productJson = $post['product'] ?? '';
  923. if (empty($productJson)) {
  924. util::fail('请选择花材');
  925. }
  926. $productList = json_decode($productJson, true);
  927. if (empty($productList)) {
  928. util::fail('请选择花材');
  929. }
  930. $connection = Yii::$app->db;//事务处理
  931. $transaction = $connection->beginTransaction();
  932. try {
  933. //判断花材有效性
  934. ProductClass::valid($productList, $this->mainId);
  935. $post['product'] = $productList;
  936. //商家开单订单有效期,到期记欠款
  937. $validTime = dict::getDict('order_pay_has_time');
  938. $current = time() + $validTime;
  939. $post['deadline'] = date("Y-m-d H:i:s", $current);
  940. $upData = [
  941. 'payWay' => $post['payWay'],
  942. 'sendType' => $post['sendType'],
  943. 'debt' => $post['debt'],
  944. 'needPrint' => $post['needPrint'],
  945. 'modifyPrice' => $post['modifyPrice'],
  946. 'shopAdminId' => $post['shopAdminId'],
  947. 'shopAdminName' => $post['shopAdminName'],
  948. 'fromType' => 1,
  949. 'sendCost' => $post['sendCost'],
  950. 'product' => $post['product'],
  951. 'deadline' => $post['deadline'],
  952. 'debt' => $post['debt'],
  953. ];
  954. $return = OrderService::updateOrder($order, $upData);
  955. $transaction->commit();
  956. util::success($return);
  957. } catch (\Exception $e) {
  958. $transaction->rollBack();
  959. Yii::error("下单失败原因:" . $e->getMessage());
  960. util::fail('下单失败');
  961. }
  962. }
  963. //取消订单 shizhongqi 20220619
  964. public function actionCancelOrder()
  965. {
  966. $post = Yii::$app->request->post();
  967. $id = $post['orderId'] ?? 0;
  968. if (empty($id)) {
  969. util::fail('请选择订单');
  970. }
  971. $order = OrderClass::getById($id, true);
  972. if (empty($order)) {
  973. util::fail('请选择订单...');
  974. }
  975. OrderClass::valid($order, $this->shopId);
  976. if (isset($order->status) == false || $order->status != 1) {
  977. util::fail('不是待付款状态,无法取消');
  978. }
  979. $purchaseId = $order->purchaseId ?? 0;
  980. $purchase = PurchaseClass::getById($purchaseId);
  981. if (empty($purchase)) {
  982. util::fail('没有找到采购单');
  983. }
  984. $deadLineTime = strtotime($purchase['deadline']);
  985. $diff = $deadLineTime - time();
  986. if ($diff <= 60) {
  987. util::fail('1分钟后订单将自动取消');
  988. }
  989. $connection = Yii::$app->db;//事务处理
  990. $transaction = $connection->beginTransaction();
  991. $orderSn = $purchase['orderSn'];
  992. try {
  993. $current = PurchaseClass::getById($purchaseId, true);
  994. PurchaseService::expire($current);
  995. $transaction->commit();
  996. //noticeUtil::push("采购单过期未付款,库存已回滚,单号:{$orderSn}");
  997. } catch (\Exception $e) {
  998. $transaction->rollBack();
  999. $msg = $e->getMessage();
  1000. noticeUtil::push("采购单过期处理报错了!订单号:{$orderSn},错误信息:{$msg}", '15280215347');
  1001. }
  1002. util::complete();
  1003. }
  1004. //延期支付 ssh 2021.1.19
  1005. public function actionDebt()
  1006. {
  1007. $id = Yii::$app->request->get('id');
  1008. $order = OrderClass::getById($id, true);
  1009. OrderClass::valid($order, $this->shopId);
  1010. $payWay = dict::getDict('payWay', 'debtPay');
  1011. $cgId = $order->purchaseId ?? 0;
  1012. $cg = PurchaseClass::getById($cgId, true);
  1013. if (empty($cg)) {
  1014. util::fail('没有找到采购单');
  1015. }
  1016. if ($order->book == 1) {
  1017. util::fail('预订单不能记欠款');
  1018. }
  1019. PurchaseService::payAfter($cg, $payWay);
  1020. OrderService::payAfter($order, $payWay);
  1021. $order = OrderClass::getById($id, true);
  1022. if (!empty($order)) {
  1023. $shopId = $order->shopId ?? 0;
  1024. $shop = ShopClass::getById($shopId, true);
  1025. if (!empty($shop)) {
  1026. //花店采购供货商端微信收到通知
  1027. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  1028. $noticeText = json_encode(['orderId' => $id]);
  1029. $noticeKey = "hdCgNoticeGhs";
  1030. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  1031. }
  1032. //订单生成时唤起在线打印
  1033. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1034. if ($order->status != 1 && $order->status != 5) {
  1035. OrderClass::onlinePrint($order);
  1036. $order->printNum += 1;
  1037. $order->save();
  1038. }
  1039. }
  1040. if ($order->status != 1 && $order->status != 5) {
  1041. //花店采购供货商端APP收到通知
  1042. //NoticeClass::ghsNewOrderNotice($order);
  1043. }
  1044. }
  1045. util::complete();
  1046. }
  1047. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1048. public function actionWxPay()
  1049. {
  1050. ini_set('date.timezone', 'Asia/Shanghai');
  1051. $post = Yii::$app->request->post();
  1052. $couponId = $post['couponId'] ?? 0;
  1053. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1054. $order = OrderClass::getByOrderSn($orderSn);
  1055. if (empty($order)) {
  1056. util::fail('订单号无效');
  1057. }
  1058. $id = $order['id'];
  1059. //支付前验证订单有效性
  1060. if (isset($order['adminId']) == false || $order['adminId'] != $this->adminId) {
  1061. util::fail('无法操作');
  1062. }
  1063. $name = $order['orderName'] ?? '购买商品';
  1064. $totalFee = $order['actPrice'];
  1065. //强制使用小程序的miniOpenId
  1066. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  1067. if (empty($openId)) {
  1068. util::fail('没有找到openId');
  1069. }
  1070. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  1071. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  1072. $wxPayType = 0;
  1073. if (httpUtil::isMiniProgram()) {
  1074. $wxPayType = 1;
  1075. }
  1076. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  1077. //订单30分钟后过期
  1078. $now = time();
  1079. $expireTime = $now + 1800;
  1080. $wx = Yii::getAlias("@vendor/weixin");
  1081. require_once($wx . '/lib/WxPay.Api.php');
  1082. require_once($wx . '/example/WxPay.JsApiPay.php');
  1083. $input = new \WxPayUnifiedOrder();
  1084. $input->SetBody($name);
  1085. $input->SetOut_trade_no($orderSn);
  1086. $input->SetTotal_fee($totalFee * 100);
  1087. $input->SetTime_start(date("YmdHis", $now));
  1088. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  1089. $input->SetTime_expire(date("YmdHis", $expireTime));
  1090. $input->SetNotify_url(Yii::$app->params['ghsHost'] . '/notice/wx-callback/');
  1091. $input->SetTrade_type("JSAPI");
  1092. //花卉宝代为申请的微信支付
  1093. //$sjExtend = $this->sjExtend->attributes;
  1094. $sjExtend = WxOpenClass::getGhsWxInfo();
  1095. if (isset($sjExtend['wxPayApply']) && $sjExtend['wxPayApply'] == 1) {
  1096. $input->SetSub_openid($openId);
  1097. } else {
  1098. $input->SetOpenid($openId);
  1099. }
  1100. //强制使用小程序的miniAppId
  1101. $sjExtend['wxAppId'] = $sjExtend['miniAppId'];
  1102. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $sjExtend);
  1103. $tools = new \JsApiPay();
  1104. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $sjExtend);
  1105. $newParams = json_decode($jsApiParameters, true);
  1106. //微信不能修改价格
  1107. $current = date("Y-m-d H:i:s");
  1108. $updateData = ['deadline' => $current, 'modPrice' => 0];
  1109. OrderClass::updateById($id, $updateData);
  1110. util::success($newParams);
  1111. }
  1112. //获取订单详情 ssh 2021.1.21
  1113. public function actionDetail()
  1114. {
  1115. $get = Yii::$app->request->get();
  1116. $id = $get['id'] ?? 0;
  1117. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1118. OrderClass::valid($info, $this->shopId);
  1119. //是否有云打印判断
  1120. $hasCloudPrint = 0;
  1121. $shopExt = $this->shopExt;
  1122. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1123. $hasCloudPrint = 1;
  1124. }
  1125. $info['hasCloudPrint'] = $hasCloudPrint;
  1126. $purchaseId = $info['purchaseId'] ?? 0;
  1127. $cg = PurchaseClass::getById($purchaseId);
  1128. $info['cgInfo'] = $cg;
  1129. //店长信息
  1130. $mainId = $cg['mainId'] ?? 0;
  1131. $superInfo = ShopAdminClass::getManager($mainId);
  1132. $info['customSuper'] = $superInfo;
  1133. util::success($info);
  1134. }
  1135. //获取订单详情,不需要登录可以访问,后面要删除此方法 ssh 20220819
  1136. public function actionInfo()
  1137. {
  1138. $get = Yii::$app->request->get();
  1139. $id = $get['id'] ?? 0;
  1140. $info = OrderService::getOrderInfo($id, true, true, true, false);
  1141. if (empty($info)) {
  1142. util::fail('没有找到订单');
  1143. }
  1144. //是否有云打印判断
  1145. $hasCloudPrint = 0;
  1146. $shopExt = $this->shopExt;
  1147. if (isset($shopExt->printSn) && !empty($shopExt->printSn) && isset($shopExt->printKey) && !empty($shopExt->printKey)) {
  1148. $hasCloudPrint = 1;
  1149. }
  1150. $info['hasCloudPrint'] = $hasCloudPrint;
  1151. $purchaseId = $info['purchaseId'] ?? 0;
  1152. $cg = PurchaseClass::getById($purchaseId);
  1153. $info['cgInfo'] = $cg;
  1154. //店长信息
  1155. $mainId = $cg['mainId'] ?? 0;
  1156. $superInfo = ShopAdminClass::getManager($mainId);
  1157. $info['customSuper'] = $superInfo;
  1158. util::success($info);
  1159. }
  1160. //自取免发货流程处理 ssh 2021.1.22
  1161. public function actionWithoutSend()
  1162. {
  1163. util::complete();
  1164. }
  1165. //本店送 ssh 2021.1.24
  1166. public function actionSelfSend()
  1167. {
  1168. $get = Yii::$app->request->get();
  1169. $id = isset($get['id']) ? $get['id'] : 0;
  1170. $info = OrderClass::getById($id, true);
  1171. OrderClass::valid($info->attributes, $this->shopId);
  1172. $connection = Yii::$app->db;
  1173. $transaction = $connection->beginTransaction();
  1174. try {
  1175. OrderClass::selfSend($info);
  1176. $transaction->commit();
  1177. } catch (\Exception $exception) {
  1178. $transaction->rollBack();
  1179. Yii::info("本店送操作报错:" . $exception->getMessage());
  1180. util::fail('操作失败');
  1181. }
  1182. util::complete();
  1183. }
  1184. //运费计算 ssh 2021.1.24
  1185. public function actionFreight()
  1186. {
  1187. //2021.3.28 接入达达
  1188. $get = Yii::$app->request->get();
  1189. $post = Yii::$app->request->post();
  1190. if (empty($get)) {
  1191. $get = $post;
  1192. }
  1193. $orderId = $get['id'] ?? 0; //订单ID
  1194. $customId = $get['customId'] ?? 0;
  1195. if (empty($orderId)) {
  1196. //自定义运费
  1197. //freight::getCost();
  1198. if (empty($customId)) {
  1199. util::fail("请选择客户");
  1200. }
  1201. $customInfo = CustomClass::getById($customId);
  1202. if (empty($customInfo)) {
  1203. util::fail('没有找到客户');
  1204. }
  1205. $shop = $this->shop;
  1206. //花材信息
  1207. $productJson = $get['product'] ?? '';
  1208. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1209. if (empty($productJson)) {
  1210. util::fail('请选择花材');
  1211. }
  1212. $productList = json_decode($productJson, true);
  1213. if (empty($productList)) {
  1214. util::fail('请选择花材');
  1215. }
  1216. $productList = ProductClass::mergeItemInfo($productList);
  1217. $weight = 0;
  1218. $price = 0;
  1219. $productData = ProductClass::getProductMapData($productList);
  1220. $bigNum = 0;
  1221. foreach ($productList as $key => $val) {
  1222. $productId = $val['productId'];
  1223. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1224. $bigNum += $val['bigNum'];
  1225. $weight = bcadd($w, $weight, 2);
  1226. $ratio = $productData[$productId]['ratio'] ?? 0;
  1227. //大小数量合并多少扎
  1228. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1229. //售卖价格
  1230. $itemPrice = $productData[$productId]['price'] ?? 0;
  1231. //合计价格
  1232. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1233. }
  1234. //没有经纬度的客户运费直接返回空 ssh 20210612
  1235. if (empty($customInfo['lat']) || empty($customInfo['long'])) {
  1236. util::success(['sedCost' => '']);
  1237. }
  1238. $cost = freight::getCost($shop->lat, $shop->long, $customInfo['lat'], $customInfo['long'], $weight);
  1239. util::success(['sedCost' => $cost]);
  1240. }
  1241. $province = $get['province'] ?? '';
  1242. $city = $get['city'] ?? '';
  1243. $address = $get['address'] ?? '';
  1244. $floor = $get['floor'] ?? '';
  1245. $customName = $get['customName'] ?? '';
  1246. $customMobile = $get['customMobile'] ?? '';
  1247. $fullAddress = $province . $city . $address . $floor;
  1248. $sendTime = $get['sendTime'] ?? '';
  1249. $lat = $get['lat'] ?? '';
  1250. $lng = $get['long'] ?? '';
  1251. if (empty($lat) || empty($lng) || empty($fullAddress)) {
  1252. util::fail('请填写客户地址');
  1253. }
  1254. ini_set('date.timezone', 'Asia/Shanghai');
  1255. if (!empty($sendTime)) {
  1256. //配送时间不为空
  1257. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1258. $sendTimeInt = strtotime($sendTime);
  1259. $now = time();
  1260. $diff = $sendTimeInt - $now;
  1261. //因服务器写入时间,改为至少提前6分钟
  1262. if ($diff <= 360) {
  1263. util::fail('配送时间至少提前6分钟');
  1264. }
  1265. } else {
  1266. //默认立即发送
  1267. $sendTime = 0;
  1268. }
  1269. //更新订单表
  1270. $info = OrderService::getOrderInfo($orderId);
  1271. OrderClass::valid($info, $this->shopId);
  1272. if ($sendTime) {
  1273. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1274. } else {
  1275. $sendTime = '0000-00-00 00:00:00';
  1276. }
  1277. $expressAddInfo = [
  1278. 'customName' => $customName,
  1279. 'customMobile' => $customMobile,
  1280. 'province' => $province,
  1281. 'city' => $city,
  1282. 'address' => $address,
  1283. 'floor' => $floor,
  1284. 'lat' => $lat,
  1285. 'long' => $lng,
  1286. 'sendTime' => $sendTime,
  1287. ];
  1288. OrderClass::updateCustomExpressInfo($orderId, $expressAddInfo);
  1289. //计算运费
  1290. $info['province'] = $province;
  1291. $info['city'] = $city;
  1292. $info['address'] = $address;
  1293. $info['floor'] = $floor;
  1294. $info['fullAddress'] = $fullAddress;
  1295. $info['lat'] = $lat;
  1296. $info['long'] = $lng;
  1297. $info['sendTime'] = $expressAddInfo['sendTime'];
  1298. $res = DadaExpressServices::queryDeliverFee($info);
  1299. util::success(['sedCost' => $res['fee']]);
  1300. }
  1301. //发快递和第三方配送 ssh 2021.1.24
  1302. public function actionThirdSend()
  1303. {
  1304. $get = Yii::$app->request->get();
  1305. $id = isset($get['id']) ? $get['id'] : 0;
  1306. $info = OrderService::getById($id, true);
  1307. OrderClass::valid($info->attributes, $this->shopId);
  1308. if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
  1309. util::fail('请填写客户地址');
  1310. }
  1311. $connection = Yii::$app->db;
  1312. $transaction = $connection->beginTransaction();
  1313. try {
  1314. $respond = OrderClass::thirdSend($info, $get);
  1315. $transaction->commit();
  1316. util::success($respond);
  1317. } catch (\Exception $exception) {
  1318. $transaction->rollBack();
  1319. Yii::info("发快递操作报错:" . $exception->getMessage());
  1320. util::fail('操作失败' . $exception->getMessage());
  1321. }
  1322. }
  1323. //确认送达 ssh 2021.1.24
  1324. public function actionReach()
  1325. {
  1326. $get = Yii::$app->request->get();
  1327. $id = isset($get['id']) ? $get['id'] : 0;
  1328. $info = OrderClass::getById($id, true);
  1329. OrderClass::valid($info->attributes, $this->shopId);
  1330. $connection = Yii::$app->db;
  1331. $transaction = $connection->beginTransaction();
  1332. try {
  1333. OrderService::reach($info);
  1334. $transaction->commit();
  1335. } catch (\Exception $exception) {
  1336. $transaction->rollBack();
  1337. Yii::info("送达操作报错:" . $exception->getMessage());
  1338. util::fail('操作失败');
  1339. }
  1340. util::complete();
  1341. }
  1342. //下单要用到的相关信息 ssh 2019.12.6
  1343. public function actionOrderRelate()
  1344. {
  1345. $regionTree = RegionService::tree();
  1346. $shop = $this->shop->attributes;
  1347. $freight = ['first' => 5, 'add' => 2];
  1348. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1349. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'txMapKey' => $mapKey, 'merchant' => $shop];
  1350. util::success($out);
  1351. }
  1352. //客户欠款的订单 ssh 2021.2.4
  1353. public function actionDebtList()
  1354. {
  1355. $get = Yii::$app->request->get();
  1356. $id = $get['id'] ?? 0;
  1357. $info = CustomClass::getCustom($id);
  1358. CustomClass::valid($info, $this->shopId);
  1359. $where = ['customId' => $id, 'debt' => 1];
  1360. $searchTime = $get['searchTime'] ?? '';
  1361. if (!empty($searchTime)) {
  1362. $startTime = $get['startTime'] ?? '';
  1363. $endTime = $get['endTime'] ?? '';
  1364. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1365. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1366. }
  1367. $list = OrderClass::getAllByCondition($where, 'addTime DESC', ['id', 'orderSn', 'actPrice', 'addTime', 'remainDebtPrice']);
  1368. $result = ['customInfo' => $info, 'list' => $list];
  1369. util::success($result);
  1370. }
  1371. //更新打印次数
  1372. public function actionAddPrintNum()
  1373. {
  1374. $id = Yii::$app->request->get('id', 0);
  1375. $order = OrderClass::getById($id, true);
  1376. OrderClass::valid($order, $this->shopId);
  1377. $order->printNum += 1;
  1378. $order->save();
  1379. util::complete();
  1380. }
  1381. //打印订单 ssh 20210714
  1382. public function actionCloudPrintOrder()
  1383. {
  1384. $get = Yii::$app->request->get();
  1385. $id = $get['id'] ?? 0;
  1386. $showPrice = $get['showPrice'] ?? 1;
  1387. $order = OrderClass::getById($id, true);
  1388. OrderClass::valid($order, $this->shopId);
  1389. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  1390. util::fail('订单还没有付款');
  1391. }
  1392. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  1393. util::fail('订单已取消');
  1394. }
  1395. $ext = $this->shopExt;
  1396. if (isset($ext->printSn) == false || isset($ext->printKey) == false || empty($ext->printSn) || empty($ext->printKey)) {
  1397. util::success(['hasNoPrint' => 1]);
  1398. }
  1399. OrderClass::onlinePrint($order, $showPrice);
  1400. $order->printNum += 1;
  1401. $order->save();
  1402. util::complete();
  1403. }
  1404. //订单确认完成 ssh 20210809
  1405. public function actionConfirmFinish()
  1406. {
  1407. $payWay = Yii::$app->request->get('payWay', -1);
  1408. $id = Yii::$app->request->get('id', 0);
  1409. $clearType = Yii::$app->request->get('clearType', 0);
  1410. $connection = Yii::$app->db;
  1411. $transaction = $connection->beginTransaction();
  1412. try {
  1413. $order = OrderClass::getById($id, true);
  1414. OrderClass::valid($order, $this->shopId);
  1415. $purchaseId = $order->purchaseId ?? 0;
  1416. $purchase = PurchaseClass::getById($purchaseId, true);
  1417. if (empty($purchase)) {
  1418. util::fail('没有采购信息');
  1419. }
  1420. if ($clearType == 1) {
  1421. //欠款
  1422. $payWay = dict::getDict('payWay', 'debtPay');
  1423. } elseif ($clearType == 2) {
  1424. //已收款
  1425. if ($payWay == -1) {
  1426. util::fail('请选择收款方式');
  1427. }
  1428. $shopAdmin = $this->shopAdmin;
  1429. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1430. util::fail('超管才能发起已收款');
  1431. }
  1432. } else {
  1433. util::fail('请选择结算方式');
  1434. }
  1435. //不需要打印
  1436. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  1437. $order->save();
  1438. //支付
  1439. PurchaseService::payAfter($purchase, $payWay);
  1440. OrderService::payAfter($order, $payWay);
  1441. //自己送
  1442. $info = OrderClass::getById($id, true);
  1443. OrderClass::selfSend($info);
  1444. //确认送达,并且不需要微信通知
  1445. Yii::$app->params['noNeedWxNotice'] = 1;
  1446. $info = OrderClass::getById($id, true);
  1447. OrderService::reach($info);
  1448. $transaction->commit();
  1449. util::complete('操作成功');
  1450. } catch (\Exception $exception) {
  1451. $transaction->rollBack();
  1452. Yii::info("确认完成订单报错:" . $exception->getMessage());
  1453. util::fail('操作失败');
  1454. }
  1455. }
  1456. //取消订单 ssh 20220110
  1457. public function actionCancel()
  1458. {
  1459. $shopAdmin = $this->shopAdmin;
  1460. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1461. util::fail('超管才能取消订单');
  1462. }
  1463. $id = Yii::$app->request->get('id');
  1464. $order = OrderClass::getById($id, true);
  1465. if ($order->orderType == 2) {
  1466. util::fail('客户下单,不能主动取消');
  1467. }
  1468. OrderClass::valid($order, $this->shopId);
  1469. OrderService::expire($order, true);
  1470. util::complete('已取消');
  1471. }
  1472. //获取收款语音播报地址 ssh 20211231
  1473. public function actionGetPayVoice()
  1474. {
  1475. $id = Yii::$app->request->get('id');
  1476. $order = OrderClass::getById($id, true);
  1477. $payWay = $order->payWay ?? 0;
  1478. $money = floatval($order->actPrice);
  1479. $payment = $payWay == dict::getDict('payWay', 'alipay') ? '支付宝' : '微信';
  1480. $msg = $payment . '收款' . $money . '元';
  1481. $audio = baiduAip::transfer($msg, 4, 0);
  1482. //上传到oss
  1483. $file = time() . rand(10000, 999999) . '.mp3';
  1484. $filePath = 'tmp/arrival/money/' . $file;
  1485. oss::uploadTmpAudio($filePath, $audio);
  1486. $url = Yii::$app->params['ghsImgHost'] . $filePath;
  1487. util::success(['url' => $url]);
  1488. }
  1489. //修改备注
  1490. public function actionModifyRemark()
  1491. {
  1492. $post = Yii::$app->request->post();
  1493. $id = $post['id'] ?? 0;
  1494. $remark = $post['remark'] ?? '';
  1495. $order = OrderClass::getById($id, true);
  1496. if (empty($order)) {
  1497. util::fail('没有找到订单');
  1498. }
  1499. if ($order->mainId != $this->mainId) {
  1500. util::fail('不是你的订单');
  1501. }
  1502. $cgId = $order->purchaseId ?? 0;
  1503. $cg = PurchaseClass::getById($cgId, true);
  1504. if (empty($cg)) {
  1505. util::fail('采购单没有找到');
  1506. }
  1507. $order->remark = $remark;
  1508. $order->save();
  1509. $cg->remark = $remark;
  1510. $cg->save();
  1511. util::complete('修改成功');
  1512. }
  1513. }