OrderController.php 63 KB

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