OrderController.php 74 KB

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