OrderController.php 64 KB

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