OrderController.php 64 KB

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