OrderController.php 63 KB

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