OrderController.php 72 KB

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