OrderController.php 63 KB

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