OrderController.php 71 KB

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