OrderController.php 74 KB

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