OrderController.php 71 KB

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